8 * Create the file "path" by writing to a temporary file and renaming
9 * it into place. The contents of the file come from "generate", which
10 * should return non-zero if it encounters an error.
12 static int update_info_file(char *path
, int (*generate
)(FILE *))
14 char *tmp
= mkpathdup("%s_XXXXXX", path
);
19 safe_create_leading_directories(path
);
20 fd
= git_mkstemp_mode(tmp
, 0666);
31 if (adjust_shared_perm(tmp
) < 0)
33 if (rename(tmp
, path
) < 0)
39 error_errno("unable to update %s", path
);
50 static int add_info_ref(const char *path
, const struct object_id
*oid
,
51 int flag
, void *cb_data
)
54 struct object
*o
= parse_object(oid
->hash
);
58 if (fprintf(fp
, "%s %s\n", oid_to_hex(oid
), path
) < 0)
61 if (o
->type
== OBJ_TAG
) {
62 o
= deref_tag(o
, path
, 0);
64 if (fprintf(fp
, "%s %s^{}\n",
65 oid_to_hex(&o
->oid
), path
) < 0)
71 static int generate_info_refs(FILE *fp
)
73 return for_each_ref(add_info_ref
, fp
);
76 static int update_info_refs(int force
)
78 char *path
= git_pathdup("info/refs");
79 int ret
= update_info_file(path
, generate_info_refs
);
85 static struct pack_info
{
91 unsigned char (*head
)[20];
94 static const char *objdir
;
97 static struct pack_info
*find_pack_by_name(const char *name
)
100 for (i
= 0; i
< num_pack
; i
++) {
101 struct packed_git
*p
= info
[i
]->p
;
102 /* skip "/pack/" after ".git/objects" */
103 if (!strcmp(p
->pack_name
+ objdirlen
+ 6, 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 *line
, int old_cnt
)
114 struct pack_info
*i
= find_pack_by_name(line
+ 2);
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
)
134 fp
= fopen(infofile
, "r");
136 return 1; /* nonexistent is not an error. */
138 while (fgets(line
, sizeof(line
), fp
)) {
139 int len
= strlen(line
);
140 if (len
&& line
[len
-1] == '\n')
147 case 'P': /* P name */
148 if (parse_pack_def(line
, old_cnt
++))
151 case 'D': /* we used to emit D but that was misguided. */
152 case 'T': /* we used to emit T but nobody uses it. */
155 error("unrecognized: %s", line
);
166 static int compare_info(const void *a_
, const void *b_
)
168 struct pack_info
*const *a
= a_
;
169 struct pack_info
*const *b
= b_
;
171 if (0 <= (*a
)->old_num
&& 0 <= (*b
)->old_num
)
172 /* Keep the order in the original */
173 return (*a
)->old_num
- (*b
)->old_num
;
174 else if (0 <= (*a
)->old_num
)
175 /* Only A existed in the original so B is obviously newer */
177 else if (0 <= (*b
)->old_num
)
178 /* The other way around. */
181 /* then it does not matter but at least keep the comparison stable */
182 if ((*a
)->p
== (*b
)->p
)
184 else if ((*a
)->p
< (*b
)->p
)
190 static void init_pack_info(const char *infofile
, int force
)
192 struct packed_git
*p
;
196 objdir
= get_object_directory();
197 objdirlen
= strlen(objdir
);
199 prepare_packed_git();
200 for (p
= packed_git
; p
; p
= p
->next
) {
201 /* we ignore things on alternate path since they are
202 * not available to the pullers in general.
209 info
= xcalloc(num_pack
, sizeof(struct pack_info
*));
210 for (i
= 0, p
= packed_git
; p
; p
= p
->next
) {
213 info
[i
] = xcalloc(1, sizeof(struct pack_info
));
215 info
[i
]->old_num
= -1;
219 if (infofile
&& !force
)
220 stale
= read_pack_info_file(infofile
);
224 for (i
= 0; i
< num_pack
; i
++) {
226 info
[i
]->old_num
= -1;
227 info
[i
]->nr_heads
= 0;
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"));