8 static FILE *info_ref_fp;
10 static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
12 struct object *o = parse_object(sha1);
16 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
17 if (o->type == OBJ_TAG) {
18 o = deref_tag(o, path, 0);
20 fprintf(info_ref_fp, "%s %s^{}\n",
21 sha1_to_hex(o->sha1), path);
26 static int update_info_refs(int force)
28 char *path0 = xstrdup(git_path("info/refs"));
29 int len = strlen(path0);
30 char *path1 = xmalloc(len + 2);
33 strcpy(path1 + len, "+");
35 safe_create_leading_directories(path0);
36 info_ref_fp = fopen(path1, "w");
38 return error("unable to update %s", path0);
39 for_each_ref(add_info_ref, NULL);
48 static struct pack_info {
54 unsigned char (*head)[20];
57 static const char *objdir;
60 static struct pack_info *find_pack_by_name(const char *name)
63 for (i = 0; i < num_pack; i++) {
64 struct packed_git *p = info[i]->p;
65 /* skip "/pack/" after ".git/objects" */
66 if (!strcmp(p->pack_name + objdirlen + 6, name))
72 /* Returns non-zero when we detect that the info in the
73 * old file is useless.
75 static int parse_pack_def(const char *line, int old_cnt)
77 struct pack_info *i = find_pack_by_name(line + 2);
83 /* The file describes a pack that is no longer here */
88 /* Returns non-zero when we detect that the info in the
89 * old file is useless.
91 static int read_pack_info_file(const char *infofile)
97 fp = fopen(infofile, "r");
99 return 1; /* nonexistent is not an error. */
101 while (fgets(line, sizeof(line), fp)) {
102 int len = strlen(line);
103 if (line[len-1] == '\n')
110 case 'P': /* P name */
111 if (parse_pack_def(line, old_cnt++))
114 case 'D': /* we used to emit D but that was misguided. */
117 case 'T': /* we used to emit T but nobody uses it. */
121 error("unrecognized: %s", line);
132 static int compare_info(const void *a_, const void *b_)
134 struct pack_info * const* a = a_;
135 struct pack_info * const* b = b_;
137 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
138 /* Keep the order in the original */
139 return (*a)->old_num - (*b)->old_num;
140 else if (0 <= (*a)->old_num)
141 /* Only A existed in the original so B is obviously newer */
143 else if (0 <= (*b)->old_num)
144 /* The other way around. */
147 /* then it does not matter but at least keep the comparison stable */
148 if ((*a)->p == (*b)->p)
150 else if ((*a)->p < (*b)->p)
156 static void init_pack_info(const char *infofile, int force)
158 struct packed_git *p;
162 objdir = get_object_directory();
163 objdirlen = strlen(objdir);
165 prepare_packed_git();
166 for (p = packed_git; p; p = p->next) {
167 /* we ignore things on alternate path since they are
168 * not available to the pullers in general.
175 info = xcalloc(num_pack, sizeof(struct pack_info *));
176 for (i = 0, p = packed_git; p; p = p->next) {
179 info[i] = xcalloc(1, sizeof(struct pack_info));
181 info[i]->old_num = -1;
185 if (infofile && !force)
186 stale = read_pack_info_file(infofile);
190 for (i = 0; i < num_pack; i++) {
192 info[i]->old_num = -1;
193 info[i]->nr_heads = 0;
198 qsort(info, num_pack, sizeof(info[0]), compare_info);
199 for (i = 0; i < num_pack; i++)
200 info[i]->new_num = i;
203 static void write_pack_info_file(FILE *fp)
206 for (i = 0; i < num_pack; i++)
207 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
211 static int update_info_packs(int force)
213 char infofile[PATH_MAX];
218 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
219 strcpy(name, infofile);
220 strcpy(name + namelen, "+");
222 init_pack_info(infofile, force);
224 safe_create_leading_directories(name);
225 fp = fopen(name, "w");
227 return error("cannot open %s", name);
228 write_pack_info_file(fp);
230 rename(name, infofile);
235 int update_server_info(int force)
237 /* We would add more dumb-server support files later,
238 * including index of available pack files and their
239 * intended audiences.
243 errs = errs | update_info_refs(force);
244 errs = errs | update_info_packs(force);
246 /* remove leftover rev-cache file if there is any */
247 unlink(git_path("info/rev-cache"));