8 static FILE *info_ref_fp;
10 static int add_info_ref(const char *path, const unsigned char *sha1)
12 struct object *o = parse_object(sha1);
14 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
15 if (o->type == tag_type) {
16 o = deref_tag(o, path, 0);
18 fprintf(info_ref_fp, "%s %s^{}\n",
19 sha1_to_hex(o->sha1), path);
24 static int update_info_refs(int force)
26 char *path0 = strdup(git_path("info/refs"));
27 int len = strlen(path0);
28 char *path1 = xmalloc(len + 2);
31 strcpy(path1 + len, "+");
33 safe_create_leading_directories(path0);
34 info_ref_fp = fopen(path1, "w");
36 return error("unable to update %s", path0);
37 for_each_ref(add_info_ref);
46 static struct pack_info {
52 unsigned char (*head)[20];
55 static const char *objdir;
58 static struct object *parse_object_cheap(const unsigned char *sha1)
62 if ((o = parse_object(sha1)) == NULL)
64 if (o->type == commit_type) {
65 struct commit *commit = (struct commit *)o;
67 commit->buffer = NULL;
68 } else if (o->type == tree_type) {
69 struct tree *tree = (struct tree *)o;
70 struct tree_entry_list *e, *n;
71 for (e = tree->entries; e; e = n) {
82 static struct pack_info *find_pack_by_name(const char *name)
85 for (i = 0; i < num_pack; i++) {
86 struct packed_git *p = info[i]->p;
87 /* skip "/pack/" after ".git/objects" */
88 if (!strcmp(p->pack_name + objdirlen + 6, name))
94 static struct pack_info *find_pack_by_old_num(int old_num)
97 for (i = 0; i < num_pack; i++)
98 if (info[i]->old_num == old_num)
103 /* Returns non-zero when we detect that the info in the
104 * old file is useless.
106 static int parse_pack_def(const char *line, int old_cnt)
108 struct pack_info *i = find_pack_by_name(line + 2);
110 i->old_num = old_cnt;
114 /* The file describes a pack that is no longer here */
119 /* Returns non-zero when we detect that the info in the
120 * old file is useless.
122 static int read_pack_info_file(const char *infofile)
128 fp = fopen(infofile, "r");
130 return 1; /* nonexisting is not an error. */
132 while (fgets(line, sizeof(line), fp)) {
133 int len = strlen(line);
134 if (line[len-1] == '\n')
138 case 'P': /* P name */
139 if (parse_pack_def(line, old_cnt++))
142 case 'D': /* we used to emit D but that was misguided. */
145 case 'T': /* we used to emit T but nobody uses it. */
149 error("unrecognized: %s", line);
160 static int compare_info(const void *a_, const void *b_)
162 struct pack_info * const* a = a_;
163 struct pack_info * const* b = b_;
165 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
166 /* Keep the order in the original */
167 return (*a)->old_num - (*b)->old_num;
168 else if (0 <= (*a)->old_num)
169 /* Only A existed in the original so B is obviously newer */
171 else if (0 <= (*b)->old_num)
172 /* The other way around. */
175 /* then it does not matter but at least keep the comparison stable */
176 return (*a)->p - (*b)->p;
179 static void init_pack_info(const char *infofile, int force)
181 struct packed_git *p;
185 objdir = get_object_directory();
186 objdirlen = strlen(objdir);
188 prepare_packed_git();
189 for (p = packed_git; p; p = p->next) {
190 /* we ignore things on alternate path since they are
191 * not available to the pullers in general.
193 if (strncmp(p->pack_name, objdir, objdirlen) ||
194 strncmp(p->pack_name + objdirlen, "/pack/", 6))
199 info = xcalloc(num_pack, sizeof(struct pack_info *));
200 for (i = 0, p = packed_git; p; p = p->next) {
201 if (strncmp(p->pack_name, objdir, objdirlen) ||
202 p->pack_name[objdirlen] != '/')
204 info[i] = xcalloc(1, sizeof(struct pack_info));
206 info[i]->old_num = -1;
210 if (infofile && !force)
211 stale = read_pack_info_file(infofile);
215 for (i = 0; i < num_pack; i++) {
217 info[i]->old_num = -1;
218 info[i]->nr_heads = 0;
223 qsort(info, num_pack, sizeof(info[0]), compare_info);
224 for (i = 0; i < num_pack; i++)
225 info[i]->new_num = i;
228 static void write_pack_info_file(FILE *fp)
231 for (i = 0; i < num_pack; i++)
232 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
235 static int update_info_packs(int force)
237 char infofile[PATH_MAX];
242 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
243 strcpy(name, infofile);
244 strcpy(name + namelen, "+");
246 init_pack_info(infofile, force);
248 safe_create_leading_directories(name);
249 fp = fopen(name, "w");
251 return error("cannot open %s", name);
252 write_pack_info_file(fp);
254 rename(name, infofile);
259 int update_server_info(int force)
261 /* We would add more dumb-server support files later,
262 * including index of available pack files and their
263 * intended audiences.
267 errs = errs | update_info_refs(force);
268 errs = errs | update_info_packs(force);
270 /* remove leftover rev-cache file if there is any */
271 unlink(git_path("info/rev-cache"));