2 #include "repository.h"
8 #include "object-store.h"
11 * Create the file "path" by writing to a temporary file and renaming
12 * it into place. The contents of the file come from "generate", which
13 * should return non-zero if it encounters an error.
15 static int update_info_file(char *path, int (*generate)(FILE *))
17 char *tmp = mkpathdup("%s_XXXXXX", path);
20 FILE *fp = NULL, *to_close;
22 safe_create_leading_directories(path);
23 fd = git_mkstemp_mode(tmp, 0666);
26 to_close = fp = fdopen(fd, "w");
36 if (adjust_shared_perm(tmp) < 0)
38 if (rename(tmp, path) < 0)
44 error_errno("unable to update %s", path);
55 static int add_info_ref(const char *path, const struct object_id *oid,
56 int flag, void *cb_data)
59 struct object *o = parse_object(the_repository, oid);
63 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
66 if (o->type == OBJ_TAG) {
67 o = deref_tag(the_repository, o, path, 0);
69 if (fprintf(fp, "%s %s^{}\n",
70 oid_to_hex(&o->oid), path) < 0)
76 static int generate_info_refs(FILE *fp)
78 return for_each_ref(add_info_ref, fp);
81 static int update_info_refs(int force)
83 char *path = git_pathdup("info/refs");
84 int ret = update_info_file(path, generate_info_refs);
90 static struct pack_info {
97 static const char *objdir;
100 static struct pack_info *find_pack_by_name(const char *name)
103 for (i = 0; i < num_pack; i++) {
104 struct packed_git *p = info[i]->p;
105 /* skip "/pack/" after ".git/objects" */
106 if (!strcmp(p->pack_name + objdirlen + 6, name))
112 /* Returns non-zero when we detect that the info in the
113 * old file is useless.
115 static int parse_pack_def(const char *packname, int old_cnt)
117 struct pack_info *i = find_pack_by_name(packname);
119 i->old_num = old_cnt;
123 /* The file describes a pack that is no longer here */
128 /* Returns non-zero when we detect that the info in the
129 * old file is useless.
131 static int read_pack_info_file(const char *infofile)
134 struct strbuf line = STRBUF_INIT;
138 fp = fopen_or_warn(infofile, "r");
140 return 1; /* nonexistent is not an error. */
142 while (strbuf_getline(&line, fp) != EOF) {
148 if (skip_prefix(line.buf, "P ", &arg)) {
150 if (parse_pack_def(arg, old_cnt++))
152 } else if (line.buf[0] == 'D') {
153 /* we used to emit D but that was misguided. */
155 } else if (line.buf[0] == 'T') {
156 /* we used to emit T but nobody uses it. */
159 error("unrecognized: %s", line.buf);
165 strbuf_release(&line);
170 static int compare_info(const void *a_, const void *b_)
172 struct pack_info *const *a = a_;
173 struct pack_info *const *b = b_;
175 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
176 /* Keep the order in the original */
177 return (*a)->old_num - (*b)->old_num;
178 else if (0 <= (*a)->old_num)
179 /* Only A existed in the original so B is obviously newer */
181 else if (0 <= (*b)->old_num)
182 /* The other way around. */
185 /* then it does not matter but at least keep the comparison stable */
186 if ((*a)->p == (*b)->p)
188 else if ((*a)->p < (*b)->p)
194 static void init_pack_info(const char *infofile, int force)
196 struct packed_git *p;
200 objdir = get_object_directory();
201 objdirlen = strlen(objdir);
203 for (p = get_all_packs(the_repository); p; p = p->next) {
204 /* we ignore things on alternate path since they are
205 * not available to the pullers in general.
212 info = xcalloc(num_pack, sizeof(struct pack_info *));
213 for (i = 0, p = get_all_packs(the_repository); p; p = p->next) {
216 info[i] = xcalloc(1, sizeof(struct pack_info));
218 info[i]->old_num = -1;
222 if (infofile && !force)
223 stale = read_pack_info_file(infofile);
227 for (i = 0; i < num_pack; i++)
229 info[i]->old_num = -1;
232 QSORT(info, num_pack, compare_info);
233 for (i = 0; i < num_pack; i++)
234 info[i]->new_num = i;
237 static void free_pack_info(void)
240 for (i = 0; i < num_pack; i++)
245 static int write_pack_info_file(FILE *fp)
248 for (i = 0; i < num_pack; i++) {
249 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
252 if (fputc('\n', fp) == EOF)
257 static int update_info_packs(int force)
259 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
262 init_pack_info(infofile, force);
263 ret = update_info_file(infofile, write_pack_info_file);
270 int update_server_info(int force)
272 /* We would add more dumb-server support files later,
273 * including index of available pack files and their
274 * intended audiences.
278 errs = errs | update_info_refs(force);
279 errs = errs | update_info_packs(force);
281 /* remove leftover rev-cache file if there is any */
282 unlink_or_warn(git_path("info/rev-cache"));