3 #include "repository.h"
9 #include "object-store.h"
12 * Create the file "path" by writing to a temporary file and renaming
13 * it into place. The contents of the file come from "generate", which
14 * should return non-zero if it encounters an error.
16 static int update_info_file(char *path, int (*generate)(FILE *))
18 char *tmp = mkpathdup("%s_XXXXXX", path);
21 FILE *fp = NULL, *to_close;
23 safe_create_leading_directories(path);
24 fd = git_mkstemp_mode(tmp, 0666);
27 to_close = fp = fdopen(fd, "w");
37 if (adjust_shared_perm(tmp) < 0)
39 if (rename(tmp, path) < 0)
45 error_errno("unable to update %s", path);
56 static int add_info_ref(const char *path, const struct object_id *oid,
57 int flag, void *cb_data)
60 struct object *o = parse_object(the_repository, oid);
64 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
67 if (o->type == OBJ_TAG) {
68 o = deref_tag(the_repository, o, path, 0);
70 if (fprintf(fp, "%s %s^{}\n",
71 oid_to_hex(&o->oid), path) < 0)
77 static int generate_info_refs(FILE *fp)
79 return for_each_ref(add_info_ref, fp);
82 static int update_info_refs(void)
84 char *path = git_pathdup("info/refs");
85 int ret = update_info_file(path, generate_info_refs);
91 static struct pack_info {
98 static struct pack_info *find_pack_by_name(const char *name)
101 for (i = 0; i < num_pack; i++) {
102 struct packed_git *p = info[i]->p;
103 if (!strcmp(pack_basename(p), name))
109 /* Returns non-zero when we detect that the info in the
110 * old file is useless.
112 static int parse_pack_def(const char *packname, int old_cnt)
114 struct pack_info *i = find_pack_by_name(packname);
116 i->old_num = old_cnt;
120 /* The file describes a pack that is no longer here */
125 /* Returns non-zero when we detect that the info in the
126 * old file is useless.
128 static int read_pack_info_file(const char *infofile)
131 struct strbuf line = STRBUF_INIT;
135 fp = fopen_or_warn(infofile, "r");
137 return 1; /* nonexistent is not an error. */
139 while (strbuf_getline(&line, fp) != EOF) {
145 if (skip_prefix(line.buf, "P ", &arg)) {
147 if (parse_pack_def(arg, old_cnt++))
149 } else if (line.buf[0] == 'D') {
150 /* we used to emit D but that was misguided. */
152 } else if (line.buf[0] == 'T') {
153 /* we used to emit T but nobody uses it. */
156 error("unrecognized: %s", line.buf);
162 strbuf_release(&line);
167 static int compare_info(const void *a_, const void *b_)
169 struct pack_info *const *a = a_;
170 struct pack_info *const *b = b_;
172 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
173 /* Keep the order in the original */
174 return (*a)->old_num - (*b)->old_num;
175 else if (0 <= (*a)->old_num)
176 /* Only A existed in the original so B is obviously newer */
178 else if (0 <= (*b)->old_num)
179 /* The other way around. */
182 /* then it does not matter but at least keep the comparison stable */
183 if ((*a)->p == (*b)->p)
185 else if ((*a)->p < (*b)->p)
191 static void init_pack_info(const char *infofile, int force)
193 struct packed_git *p;
198 for (p = get_all_packs(the_repository); p; p = p->next) {
199 /* we ignore things on alternate path since they are
200 * not available to the pullers in general.
202 if (!p->pack_local || !file_exists(p->pack_name))
206 ALLOC_GROW(info, num_pack, alloc);
207 info[i] = xcalloc(1, sizeof(struct pack_info));
209 info[i]->old_num = -1;
212 if (infofile && !force)
213 stale = read_pack_info_file(infofile);
217 for (i = 0; i < num_pack; i++)
219 info[i]->old_num = -1;
222 QSORT(info, num_pack, compare_info);
223 for (i = 0; i < num_pack; i++)
224 info[i]->new_num = i;
227 static void free_pack_info(void)
230 for (i = 0; i < num_pack; i++)
235 static int write_pack_info_file(FILE *fp)
238 for (i = 0; i < num_pack; i++) {
239 if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0)
242 if (fputc('\n', fp) == EOF)
247 static int update_info_packs(int force)
249 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
252 init_pack_info(infofile, force);
253 ret = update_info_file(infofile, write_pack_info_file);
260 int update_server_info(int force)
262 /* We would add more dumb-server support files later,
263 * including index of available pack files and their
264 * intended audiences.
268 errs = errs | update_info_refs();
269 errs = errs | update_info_packs(force);
271 /* remove leftover rev-cache file if there is any */
272 unlink_or_warn(git_path("info/rev-cache"));