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 pack_info *find_pack_by_name(const char *name)
61 for (i = 0; i < num_pack; i++) {
62 struct packed_git *p = info[i]->p;
63 /* skip "/pack/" after ".git/objects" */
64 if (!strcmp(p->pack_name + objdirlen + 6, name))
70 /* Returns non-zero when we detect that the info in the
71 * old file is useless.
73 static int parse_pack_def(const char *line, int old_cnt)
75 struct pack_info *i = find_pack_by_name(line + 2);
81 /* The file describes a pack that is no longer here */
86 /* Returns non-zero when we detect that the info in the
87 * old file is useless.
89 static int read_pack_info_file(const char *infofile)
95 fp = fopen(infofile, "r");
97 return 1; /* nonexisting is not an error. */
99 while (fgets(line, sizeof(line), fp)) {
100 int len = strlen(line);
101 if (line[len-1] == '\n')
105 case 'P': /* P name */
106 if (parse_pack_def(line, old_cnt++))
109 case 'D': /* we used to emit D but that was misguided. */
112 case 'T': /* we used to emit T but nobody uses it. */
116 error("unrecognized: %s", line);
127 static int compare_info(const void *a_, const void *b_)
129 struct pack_info * const* a = a_;
130 struct pack_info * const* b = b_;
132 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
133 /* Keep the order in the original */
134 return (*a)->old_num - (*b)->old_num;
135 else if (0 <= (*a)->old_num)
136 /* Only A existed in the original so B is obviously newer */
138 else if (0 <= (*b)->old_num)
139 /* The other way around. */
142 /* then it does not matter but at least keep the comparison stable */
143 if ((*a)->p == (*b)->p)
145 else if ((*a)->p < (*b)->p)
151 static void init_pack_info(const char *infofile, int force)
153 struct packed_git *p;
157 objdir = get_object_directory();
158 objdirlen = strlen(objdir);
160 prepare_packed_git();
161 for (p = packed_git; p; p = p->next) {
162 /* we ignore things on alternate path since they are
163 * not available to the pullers in general.
170 info = xcalloc(num_pack, sizeof(struct pack_info *));
171 for (i = 0, p = packed_git; p; p = p->next) {
174 info[i] = xcalloc(1, sizeof(struct pack_info));
176 info[i]->old_num = -1;
180 if (infofile && !force)
181 stale = read_pack_info_file(infofile);
185 for (i = 0; i < num_pack; i++) {
187 info[i]->old_num = -1;
188 info[i]->nr_heads = 0;
193 qsort(info, num_pack, sizeof(info[0]), compare_info);
194 for (i = 0; i < num_pack; i++)
195 info[i]->new_num = i;
198 static void write_pack_info_file(FILE *fp)
201 for (i = 0; i < num_pack; i++)
202 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
206 static int update_info_packs(int force)
208 char infofile[PATH_MAX];
213 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
214 strcpy(name, infofile);
215 strcpy(name + namelen, "+");
217 init_pack_info(infofile, force);
219 safe_create_leading_directories(name);
220 fp = fopen(name, "w");
222 return error("cannot open %s", name);
223 write_pack_info_file(fp);
225 rename(name, infofile);
230 int update_server_info(int force)
232 /* We would add more dumb-server support files later,
233 * including index of available pack files and their
234 * intended audiences.
238 errs = errs | update_info_refs(force);
239 errs = errs | update_info_packs(force);
241 /* remove leftover rev-cache file if there is any */
242 unlink(git_path("info/rev-cache"));