]>
Commit | Line | Data |
---|---|---|
7bd9bcf3 | 1 | #include "../cache.h" |
b2141fc1 | 2 | #include "../config.h" |
7bd9bcf3 MH |
3 | #include "../refs.h" |
4 | #include "refs-internal.h" | |
958f9646 | 5 | #include "ref-cache.h" |
3bc581b9 | 6 | #include "../iterator.h" |
2880d16f | 7 | #include "../dir-iterator.h" |
7bd9bcf3 MH |
8 | #include "../lockfile.h" |
9 | #include "../object.h" | |
10 | #include "../dir.h" | |
11 | ||
12 | struct ref_lock { | |
13 | char *ref_name; | |
7bd9bcf3 MH |
14 | struct lock_file *lk; |
15 | struct object_id old_oid; | |
16 | }; | |
17 | ||
7bd9bcf3 | 18 | /* |
a8739244 MH |
19 | * Return true if refname, which has the specified oid and flags, can |
20 | * be resolved to an object in the database. If the referred-to object | |
21 | * does not exist, emit a warning and return false. | |
7bd9bcf3 | 22 | */ |
a8739244 MH |
23 | static int ref_resolves_to_object(const char *refname, |
24 | const struct object_id *oid, | |
25 | unsigned int flags) | |
7bd9bcf3 | 26 | { |
a8739244 | 27 | if (flags & REF_ISBROKEN) |
7bd9bcf3 | 28 | return 0; |
a8739244 MH |
29 | if (!has_sha1_file(oid->hash)) { |
30 | error("%s does not point to a valid object!", refname); | |
7bd9bcf3 MH |
31 | return 0; |
32 | } | |
33 | return 1; | |
34 | } | |
35 | ||
7bd9bcf3 | 36 | struct packed_ref_cache { |
7c22bc8a | 37 | struct ref_cache *cache; |
7bd9bcf3 MH |
38 | |
39 | /* | |
40 | * Count of references to the data structure in this instance, | |
65a0a8e5 MH |
41 | * including the pointer from files_ref_store::packed if any. |
42 | * The data will not be freed as long as the reference count | |
43 | * is nonzero. | |
7bd9bcf3 MH |
44 | */ |
45 | unsigned int referrers; | |
46 | ||
7bd9bcf3 MH |
47 | /* The metadata from when this packed-refs cache was read */ |
48 | struct stat_validity validity; | |
49 | }; | |
50 | ||
51 | /* | |
52 | * Future: need to be in "struct repository" | |
53 | * when doing a full libification. | |
54 | */ | |
00eebe35 MH |
55 | struct files_ref_store { |
56 | struct ref_store base; | |
9e7ec634 | 57 | unsigned int store_flags; |
32c597e7 | 58 | |
f57f37e2 NTND |
59 | char *gitdir; |
60 | char *gitcommondir; | |
33dfb9f3 NTND |
61 | char *packed_refs_path; |
62 | ||
7c22bc8a | 63 | struct ref_cache *loose; |
7bd9bcf3 | 64 | struct packed_ref_cache *packed; |
55c6bc37 MH |
65 | |
66 | /* | |
00d17448 MH |
67 | * Lock used for the "packed-refs" file. Note that this (and |
68 | * thus the enclosing `files_ref_store`) must not be freed. | |
55c6bc37 | 69 | */ |
00d17448 | 70 | struct lock_file packed_refs_lock; |
00eebe35 | 71 | }; |
7bd9bcf3 | 72 | |
7bd9bcf3 MH |
73 | /* |
74 | * Increment the reference count of *packed_refs. | |
75 | */ | |
76 | static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
77 | { | |
78 | packed_refs->referrers++; | |
79 | } | |
80 | ||
81 | /* | |
82 | * Decrease the reference count of *packed_refs. If it goes to zero, | |
83 | * free *packed_refs and return true; otherwise return false. | |
84 | */ | |
85 | static int release_packed_ref_cache(struct packed_ref_cache *packed_refs) | |
86 | { | |
87 | if (!--packed_refs->referrers) { | |
7c22bc8a | 88 | free_ref_cache(packed_refs->cache); |
7bd9bcf3 MH |
89 | stat_validity_clear(&packed_refs->validity); |
90 | free(packed_refs); | |
91 | return 1; | |
92 | } else { | |
93 | return 0; | |
94 | } | |
95 | } | |
96 | ||
65a0a8e5 | 97 | static void clear_packed_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
98 | { |
99 | if (refs->packed) { | |
100 | struct packed_ref_cache *packed_refs = refs->packed; | |
101 | ||
00d17448 | 102 | if (is_lock_file_locked(&refs->packed_refs_lock)) |
04aea8d4 | 103 | die("BUG: packed-ref cache cleared while locked"); |
7bd9bcf3 MH |
104 | refs->packed = NULL; |
105 | release_packed_ref_cache(packed_refs); | |
106 | } | |
107 | } | |
108 | ||
65a0a8e5 | 109 | static void clear_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
110 | { |
111 | if (refs->loose) { | |
7c22bc8a | 112 | free_ref_cache(refs->loose); |
7bd9bcf3 MH |
113 | refs->loose = NULL; |
114 | } | |
115 | } | |
116 | ||
a2d5156c JK |
117 | /* |
118 | * Create a new submodule ref cache and add it to the internal | |
119 | * set of caches. | |
120 | */ | |
9e7ec634 NTND |
121 | static struct ref_store *files_ref_store_create(const char *gitdir, |
122 | unsigned int flags) | |
7bd9bcf3 | 123 | { |
00eebe35 MH |
124 | struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); |
125 | struct ref_store *ref_store = (struct ref_store *)refs; | |
f57f37e2 | 126 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 127 | |
fbfd0a29 | 128 | base_ref_store_init(ref_store, &refs_be_files); |
9e7ec634 | 129 | refs->store_flags = flags; |
7bd9bcf3 | 130 | |
f57f37e2 NTND |
131 | refs->gitdir = xstrdup(gitdir); |
132 | get_common_dir_noenv(&sb, gitdir); | |
133 | refs->gitcommondir = strbuf_detach(&sb, NULL); | |
134 | strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir); | |
135 | refs->packed_refs_path = strbuf_detach(&sb, NULL); | |
7bd9bcf3 | 136 | |
00eebe35 | 137 | return ref_store; |
a2d5156c | 138 | } |
7bd9bcf3 | 139 | |
32c597e7 | 140 | /* |
9e7ec634 NTND |
141 | * Die if refs is not the main ref store. caller is used in any |
142 | * necessary error messages. | |
32c597e7 MH |
143 | */ |
144 | static void files_assert_main_repository(struct files_ref_store *refs, | |
145 | const char *caller) | |
146 | { | |
9e7ec634 NTND |
147 | if (refs->store_flags & REF_STORE_MAIN) |
148 | return; | |
149 | ||
150 | die("BUG: operation %s only allowed for main ref store", caller); | |
32c597e7 MH |
151 | } |
152 | ||
a2d5156c | 153 | /* |
00eebe35 | 154 | * Downcast ref_store to files_ref_store. Die if ref_store is not a |
9e7ec634 NTND |
155 | * files_ref_store. required_flags is compared with ref_store's |
156 | * store_flags to ensure the ref_store has all required capabilities. | |
157 | * "caller" is used in any necessary error messages. | |
a2d5156c | 158 | */ |
9e7ec634 NTND |
159 | static struct files_ref_store *files_downcast(struct ref_store *ref_store, |
160 | unsigned int required_flags, | |
161 | const char *caller) | |
a2d5156c | 162 | { |
32c597e7 MH |
163 | struct files_ref_store *refs; |
164 | ||
00eebe35 MH |
165 | if (ref_store->be != &refs_be_files) |
166 | die("BUG: ref_store is type \"%s\" not \"files\" in %s", | |
167 | ref_store->be->name, caller); | |
2eed2780 | 168 | |
32c597e7 MH |
169 | refs = (struct files_ref_store *)ref_store; |
170 | ||
9e7ec634 NTND |
171 | if ((refs->store_flags & required_flags) != required_flags) |
172 | die("BUG: operation %s requires abilities 0x%x, but only have 0x%x", | |
173 | caller, required_flags, refs->store_flags); | |
2eed2780 | 174 | |
32c597e7 | 175 | return refs; |
7bd9bcf3 MH |
176 | } |
177 | ||
178 | /* The length of a peeled reference line in packed-refs, including EOL: */ | |
179 | #define PEELED_LINE_LENGTH 42 | |
180 | ||
181 | /* | |
182 | * The packed-refs header line that we write out. Perhaps other | |
183 | * traits will be added later. The trailing space is required. | |
184 | */ | |
185 | static const char PACKED_REFS_HEADER[] = | |
186 | "# pack-refs with: peeled fully-peeled \n"; | |
187 | ||
188 | /* | |
189 | * Parse one line from a packed-refs file. Write the SHA1 to sha1. | |
190 | * Return a pointer to the refname within the line (null-terminated), | |
191 | * or NULL if there was a problem. | |
192 | */ | |
4417df8c | 193 | static const char *parse_ref_line(struct strbuf *line, struct object_id *oid) |
7bd9bcf3 MH |
194 | { |
195 | const char *ref; | |
196 | ||
4417df8c | 197 | if (parse_oid_hex(line->buf, oid, &ref) < 0) |
7bd9bcf3 | 198 | return NULL; |
4417df8c | 199 | if (!isspace(*ref++)) |
7bd9bcf3 MH |
200 | return NULL; |
201 | ||
7bd9bcf3 MH |
202 | if (isspace(*ref)) |
203 | return NULL; | |
204 | ||
205 | if (line->buf[line->len - 1] != '\n') | |
206 | return NULL; | |
207 | line->buf[--line->len] = 0; | |
208 | ||
209 | return ref; | |
210 | } | |
211 | ||
212 | /* | |
099a912a MH |
213 | * Read from `packed_refs_file` into a newly-allocated |
214 | * `packed_ref_cache` and return it. The return value will already | |
215 | * have its reference count incremented. | |
7bd9bcf3 MH |
216 | * |
217 | * A comment line of the form "# pack-refs with: " may contain zero or | |
218 | * more traits. We interpret the traits as follows: | |
219 | * | |
220 | * No traits: | |
221 | * | |
222 | * Probably no references are peeled. But if the file contains a | |
223 | * peeled value for a reference, we will use it. | |
224 | * | |
225 | * peeled: | |
226 | * | |
227 | * References under "refs/tags/", if they *can* be peeled, *are* | |
228 | * peeled in this file. References outside of "refs/tags/" are | |
229 | * probably not peeled even if they could have been, but if we find | |
230 | * a peeled value for such a reference we will use it. | |
231 | * | |
232 | * fully-peeled: | |
233 | * | |
234 | * All references in the file that can be peeled are peeled. | |
235 | * Inversely (and this is more important), any references in the | |
236 | * file for which no peeled value is recorded is not peelable. This | |
237 | * trait should typically be written alongside "peeled" for | |
238 | * compatibility with older clients, but we do not require it | |
239 | * (i.e., "peeled" is a no-op if "fully-peeled" is set). | |
240 | */ | |
099a912a | 241 | static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file) |
7bd9bcf3 | 242 | { |
099a912a MH |
243 | FILE *f; |
244 | struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs)); | |
7bd9bcf3 MH |
245 | struct ref_entry *last = NULL; |
246 | struct strbuf line = STRBUF_INIT; | |
247 | enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE; | |
099a912a | 248 | struct ref_dir *dir; |
7bd9bcf3 | 249 | |
099a912a MH |
250 | acquire_packed_ref_cache(packed_refs); |
251 | packed_refs->cache = create_ref_cache(NULL, NULL); | |
252 | packed_refs->cache->root->flag &= ~REF_INCOMPLETE; | |
253 | ||
254 | f = fopen(packed_refs_file, "r"); | |
89c571da MH |
255 | if (!f) { |
256 | if (errno == ENOENT) { | |
257 | /* | |
258 | * This is OK; it just means that no | |
259 | * "packed-refs" file has been written yet, | |
260 | * which is equivalent to it being empty. | |
261 | */ | |
262 | return packed_refs; | |
263 | } else { | |
264 | die_errno("couldn't read %s", packed_refs_file); | |
265 | } | |
266 | } | |
099a912a MH |
267 | |
268 | stat_validity_update(&packed_refs->validity, fileno(f)); | |
269 | ||
270 | dir = get_ref_dir(packed_refs->cache->root); | |
7bd9bcf3 | 271 | while (strbuf_getwholeline(&line, f, '\n') != EOF) { |
4417df8c | 272 | struct object_id oid; |
7bd9bcf3 MH |
273 | const char *refname; |
274 | const char *traits; | |
275 | ||
276 | if (skip_prefix(line.buf, "# pack-refs with:", &traits)) { | |
277 | if (strstr(traits, " fully-peeled ")) | |
278 | peeled = PEELED_FULLY; | |
279 | else if (strstr(traits, " peeled ")) | |
280 | peeled = PEELED_TAGS; | |
281 | /* perhaps other traits later as well */ | |
282 | continue; | |
283 | } | |
284 | ||
4417df8c | 285 | refname = parse_ref_line(&line, &oid); |
7bd9bcf3 MH |
286 | if (refname) { |
287 | int flag = REF_ISPACKED; | |
288 | ||
289 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) { | |
290 | if (!refname_is_safe(refname)) | |
291 | die("packed refname is dangerous: %s", refname); | |
4417df8c | 292 | oidclr(&oid); |
7bd9bcf3 MH |
293 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
294 | } | |
c1da06c6 | 295 | last = create_ref_entry(refname, &oid, flag); |
7bd9bcf3 MH |
296 | if (peeled == PEELED_FULLY || |
297 | (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/"))) | |
298 | last->flag |= REF_KNOWS_PEELED; | |
a3ade2ba | 299 | add_ref_entry(dir, last); |
7bd9bcf3 MH |
300 | continue; |
301 | } | |
302 | if (last && | |
303 | line.buf[0] == '^' && | |
304 | line.len == PEELED_LINE_LENGTH && | |
305 | line.buf[PEELED_LINE_LENGTH - 1] == '\n' && | |
4417df8c | 306 | !get_oid_hex(line.buf + 1, &oid)) { |
307 | oidcpy(&last->u.value.peeled, &oid); | |
7bd9bcf3 MH |
308 | /* |
309 | * Regardless of what the file header said, | |
310 | * we definitely know the value of *this* | |
311 | * reference: | |
312 | */ | |
313 | last->flag |= REF_KNOWS_PEELED; | |
314 | } | |
315 | } | |
316 | ||
099a912a | 317 | fclose(f); |
7bd9bcf3 | 318 | strbuf_release(&line); |
099a912a MH |
319 | |
320 | return packed_refs; | |
7bd9bcf3 MH |
321 | } |
322 | ||
33dfb9f3 NTND |
323 | static const char *files_packed_refs_path(struct files_ref_store *refs) |
324 | { | |
325 | return refs->packed_refs_path; | |
326 | } | |
327 | ||
802de3da NTND |
328 | static void files_reflog_path(struct files_ref_store *refs, |
329 | struct strbuf *sb, | |
330 | const char *refname) | |
331 | { | |
332 | if (!refname) { | |
f57f37e2 NTND |
333 | /* |
334 | * FIXME: of course this is wrong in multi worktree | |
335 | * setting. To be fixed real soon. | |
336 | */ | |
337 | strbuf_addf(sb, "%s/logs", refs->gitcommondir); | |
802de3da NTND |
338 | return; |
339 | } | |
340 | ||
f57f37e2 NTND |
341 | switch (ref_type(refname)) { |
342 | case REF_TYPE_PER_WORKTREE: | |
343 | case REF_TYPE_PSEUDOREF: | |
344 | strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname); | |
345 | break; | |
346 | case REF_TYPE_NORMAL: | |
347 | strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname); | |
348 | break; | |
349 | default: | |
350 | die("BUG: unknown ref type %d of ref %s", | |
351 | ref_type(refname), refname); | |
352 | } | |
802de3da NTND |
353 | } |
354 | ||
19e02f4f NTND |
355 | static void files_ref_path(struct files_ref_store *refs, |
356 | struct strbuf *sb, | |
357 | const char *refname) | |
358 | { | |
f57f37e2 NTND |
359 | switch (ref_type(refname)) { |
360 | case REF_TYPE_PER_WORKTREE: | |
361 | case REF_TYPE_PSEUDOREF: | |
362 | strbuf_addf(sb, "%s/%s", refs->gitdir, refname); | |
363 | break; | |
364 | case REF_TYPE_NORMAL: | |
365 | strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname); | |
366 | break; | |
367 | default: | |
368 | die("BUG: unknown ref type %d of ref %s", | |
369 | ref_type(refname), refname); | |
370 | } | |
19e02f4f NTND |
371 | } |
372 | ||
fed6ebeb MH |
373 | /* |
374 | * Check that the packed refs cache (if any) still reflects the | |
375 | * contents of the file. If not, clear the cache. | |
376 | */ | |
377 | static void validate_packed_ref_cache(struct files_ref_store *refs) | |
378 | { | |
379 | if (refs->packed && | |
380 | !stat_validity_check(&refs->packed->validity, | |
381 | files_packed_refs_path(refs))) | |
382 | clear_packed_ref_cache(refs); | |
383 | } | |
384 | ||
7bd9bcf3 | 385 | /* |
65a0a8e5 | 386 | * Get the packed_ref_cache for the specified files_ref_store, |
28ed9830 MH |
387 | * creating and populating it if it hasn't been read before or if the |
388 | * file has been changed (according to its `validity` field) since it | |
389 | * was last read. On the other hand, if we hold the lock, then assume | |
390 | * that the file hasn't been changed out from under us, so skip the | |
391 | * extra `stat()` call in `stat_validity_check()`. | |
7bd9bcf3 | 392 | */ |
65a0a8e5 | 393 | static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 | 394 | { |
33dfb9f3 | 395 | const char *packed_refs_file = files_packed_refs_path(refs); |
7bd9bcf3 | 396 | |
fed6ebeb MH |
397 | if (!is_lock_file_locked(&refs->packed_refs_lock)) |
398 | validate_packed_ref_cache(refs); | |
7bd9bcf3 | 399 | |
099a912a MH |
400 | if (!refs->packed) |
401 | refs->packed = read_packed_refs(packed_refs_file); | |
402 | ||
7bd9bcf3 MH |
403 | return refs->packed; |
404 | } | |
405 | ||
406 | static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache) | |
407 | { | |
7c22bc8a | 408 | return get_ref_dir(packed_ref_cache->cache->root); |
7bd9bcf3 MH |
409 | } |
410 | ||
65a0a8e5 | 411 | static struct ref_dir *get_packed_refs(struct files_ref_store *refs) |
7bd9bcf3 MH |
412 | { |
413 | return get_packed_ref_dir(get_packed_ref_cache(refs)); | |
414 | } | |
415 | ||
416 | /* | |
417 | * Add a reference to the in-memory packed reference cache. This may | |
418 | * only be called while the packed-refs file is locked (see | |
419 | * lock_packed_refs()). To actually write the packed-refs file, call | |
420 | * commit_packed_refs(). | |
421 | */ | |
d99825ab | 422 | static void add_packed_ref(struct files_ref_store *refs, |
4417df8c | 423 | const char *refname, const struct object_id *oid) |
7bd9bcf3 | 424 | { |
00eebe35 | 425 | struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs); |
7bd9bcf3 | 426 | |
00d17448 | 427 | if (!is_lock_file_locked(&refs->packed_refs_lock)) |
04aea8d4 | 428 | die("BUG: packed refs not locked"); |
c1da06c6 MH |
429 | |
430 | if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) | |
431 | die("Reference has invalid format: '%s'", refname); | |
432 | ||
a3ade2ba | 433 | add_ref_entry(get_packed_ref_dir(packed_ref_cache), |
c1da06c6 | 434 | create_ref_entry(refname, oid, REF_ISPACKED)); |
7bd9bcf3 MH |
435 | } |
436 | ||
437 | /* | |
438 | * Read the loose references from the namespace dirname into dir | |
439 | * (without recursing). dirname must end with '/'. dir must be the | |
440 | * directory entry corresponding to dirname. | |
441 | */ | |
df308759 MH |
442 | static void loose_fill_ref_dir(struct ref_store *ref_store, |
443 | struct ref_dir *dir, const char *dirname) | |
7bd9bcf3 | 444 | { |
df308759 MH |
445 | struct files_ref_store *refs = |
446 | files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir"); | |
7bd9bcf3 MH |
447 | DIR *d; |
448 | struct dirent *de; | |
449 | int dirnamelen = strlen(dirname); | |
450 | struct strbuf refname; | |
451 | struct strbuf path = STRBUF_INIT; | |
452 | size_t path_baselen; | |
453 | ||
19e02f4f | 454 | files_ref_path(refs, &path, dirname); |
7bd9bcf3 MH |
455 | path_baselen = path.len; |
456 | ||
457 | d = opendir(path.buf); | |
458 | if (!d) { | |
459 | strbuf_release(&path); | |
460 | return; | |
461 | } | |
462 | ||
463 | strbuf_init(&refname, dirnamelen + 257); | |
464 | strbuf_add(&refname, dirname, dirnamelen); | |
465 | ||
466 | while ((de = readdir(d)) != NULL) { | |
4417df8c | 467 | struct object_id oid; |
7bd9bcf3 MH |
468 | struct stat st; |
469 | int flag; | |
470 | ||
471 | if (de->d_name[0] == '.') | |
472 | continue; | |
473 | if (ends_with(de->d_name, ".lock")) | |
474 | continue; | |
475 | strbuf_addstr(&refname, de->d_name); | |
476 | strbuf_addstr(&path, de->d_name); | |
477 | if (stat(path.buf, &st) < 0) { | |
478 | ; /* silently ignore */ | |
479 | } else if (S_ISDIR(st.st_mode)) { | |
480 | strbuf_addch(&refname, '/'); | |
481 | add_entry_to_dir(dir, | |
e00d1a4f | 482 | create_dir_entry(dir->cache, refname.buf, |
7bd9bcf3 MH |
483 | refname.len, 1)); |
484 | } else { | |
7d2df051 | 485 | if (!refs_resolve_ref_unsafe(&refs->base, |
3c0cb0cb MH |
486 | refname.buf, |
487 | RESOLVE_REF_READING, | |
4417df8c | 488 | oid.hash, &flag)) { |
489 | oidclr(&oid); | |
7bd9bcf3 | 490 | flag |= REF_ISBROKEN; |
4417df8c | 491 | } else if (is_null_oid(&oid)) { |
7bd9bcf3 MH |
492 | /* |
493 | * It is so astronomically unlikely | |
494 | * that NULL_SHA1 is the SHA-1 of an | |
495 | * actual object that we consider its | |
496 | * appearance in a loose reference | |
497 | * file to be repo corruption | |
498 | * (probably due to a software bug). | |
499 | */ | |
500 | flag |= REF_ISBROKEN; | |
501 | } | |
502 | ||
503 | if (check_refname_format(refname.buf, | |
504 | REFNAME_ALLOW_ONELEVEL)) { | |
505 | if (!refname_is_safe(refname.buf)) | |
506 | die("loose refname is dangerous: %s", refname.buf); | |
4417df8c | 507 | oidclr(&oid); |
7bd9bcf3 MH |
508 | flag |= REF_BAD_NAME | REF_ISBROKEN; |
509 | } | |
510 | add_entry_to_dir(dir, | |
c1da06c6 | 511 | create_ref_entry(refname.buf, &oid, flag)); |
7bd9bcf3 MH |
512 | } |
513 | strbuf_setlen(&refname, dirnamelen); | |
514 | strbuf_setlen(&path, path_baselen); | |
515 | } | |
516 | strbuf_release(&refname); | |
517 | strbuf_release(&path); | |
518 | closedir(d); | |
e3bf2989 MH |
519 | |
520 | /* | |
521 | * Manually add refs/bisect, which, being per-worktree, might | |
522 | * not appear in the directory listing for refs/ in the main | |
523 | * repo. | |
524 | */ | |
525 | if (!strcmp(dirname, "refs/")) { | |
526 | int pos = search_ref_dir(dir, "refs/bisect/", 12); | |
527 | ||
528 | if (pos < 0) { | |
529 | struct ref_entry *child_entry = create_dir_entry( | |
530 | dir->cache, "refs/bisect/", 12, 1); | |
531 | add_entry_to_dir(dir, child_entry); | |
532 | } | |
533 | } | |
7bd9bcf3 MH |
534 | } |
535 | ||
a714b19c | 536 | static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) |
7bd9bcf3 MH |
537 | { |
538 | if (!refs->loose) { | |
539 | /* | |
540 | * Mark the top-level directory complete because we | |
541 | * are about to read the only subdirectory that can | |
542 | * hold references: | |
543 | */ | |
df308759 | 544 | refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir); |
7c22bc8a MH |
545 | |
546 | /* We're going to fill the top level ourselves: */ | |
547 | refs->loose->root->flag &= ~REF_INCOMPLETE; | |
548 | ||
7bd9bcf3 | 549 | /* |
7c22bc8a MH |
550 | * Add an incomplete entry for "refs/" (to be filled |
551 | * lazily): | |
7bd9bcf3 | 552 | */ |
7c22bc8a | 553 | add_entry_to_dir(get_ref_dir(refs->loose->root), |
e00d1a4f | 554 | create_dir_entry(refs->loose, "refs/", 5, 1)); |
7bd9bcf3 | 555 | } |
a714b19c | 556 | return refs->loose; |
7bd9bcf3 MH |
557 | } |
558 | ||
7bd9bcf3 MH |
559 | /* |
560 | * Return the ref_entry for the given refname from the packed | |
561 | * references. If it does not exist, return NULL. | |
562 | */ | |
f0d21efc MH |
563 | static struct ref_entry *get_packed_ref(struct files_ref_store *refs, |
564 | const char *refname) | |
7bd9bcf3 | 565 | { |
bc1c696e | 566 | return find_ref_entry(get_packed_refs(refs), refname); |
7bd9bcf3 MH |
567 | } |
568 | ||
569 | /* | |
419c6f4c | 570 | * A loose ref file doesn't exist; check for a packed ref. |
7bd9bcf3 | 571 | */ |
611118d0 MH |
572 | static int resolve_packed_ref(struct files_ref_store *refs, |
573 | const char *refname, | |
574 | unsigned char *sha1, unsigned int *flags) | |
7bd9bcf3 MH |
575 | { |
576 | struct ref_entry *entry; | |
577 | ||
578 | /* | |
579 | * The loose reference file does not exist; check for a packed | |
580 | * reference. | |
581 | */ | |
f0d21efc | 582 | entry = get_packed_ref(refs, refname); |
7bd9bcf3 MH |
583 | if (entry) { |
584 | hashcpy(sha1, entry->u.value.oid.hash); | |
a70a93b7 | 585 | *flags |= REF_ISPACKED; |
7bd9bcf3 MH |
586 | return 0; |
587 | } | |
419c6f4c MH |
588 | /* refname is not a packed reference. */ |
589 | return -1; | |
7bd9bcf3 MH |
590 | } |
591 | ||
e1e33b72 MH |
592 | static int files_read_raw_ref(struct ref_store *ref_store, |
593 | const char *refname, unsigned char *sha1, | |
594 | struct strbuf *referent, unsigned int *type) | |
7bd9bcf3 | 595 | { |
4308651c | 596 | struct files_ref_store *refs = |
9e7ec634 | 597 | files_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); |
42a38cf7 MH |
598 | struct strbuf sb_contents = STRBUF_INIT; |
599 | struct strbuf sb_path = STRBUF_INIT; | |
7048653a DT |
600 | const char *path; |
601 | const char *buf; | |
602 | struct stat st; | |
603 | int fd; | |
42a38cf7 MH |
604 | int ret = -1; |
605 | int save_errno; | |
e8c42cb9 | 606 | int remaining_retries = 3; |
7bd9bcf3 | 607 | |
fa96ea1b | 608 | *type = 0; |
42a38cf7 | 609 | strbuf_reset(&sb_path); |
34c7ad8f | 610 | |
19e02f4f | 611 | files_ref_path(refs, &sb_path, refname); |
34c7ad8f | 612 | |
42a38cf7 | 613 | path = sb_path.buf; |
7bd9bcf3 | 614 | |
7048653a DT |
615 | stat_ref: |
616 | /* | |
617 | * We might have to loop back here to avoid a race | |
618 | * condition: first we lstat() the file, then we try | |
619 | * to read it as a link or as a file. But if somebody | |
620 | * changes the type of the file (file <-> directory | |
621 | * <-> symlink) between the lstat() and reading, then | |
622 | * we don't want to report that as an error but rather | |
623 | * try again starting with the lstat(). | |
e8c42cb9 JK |
624 | * |
625 | * We'll keep a count of the retries, though, just to avoid | |
626 | * any confusing situation sending us into an infinite loop. | |
7048653a | 627 | */ |
7bd9bcf3 | 628 | |
e8c42cb9 JK |
629 | if (remaining_retries-- <= 0) |
630 | goto out; | |
631 | ||
7048653a DT |
632 | if (lstat(path, &st) < 0) { |
633 | if (errno != ENOENT) | |
42a38cf7 | 634 | goto out; |
611118d0 | 635 | if (resolve_packed_ref(refs, refname, sha1, type)) { |
7048653a | 636 | errno = ENOENT; |
42a38cf7 | 637 | goto out; |
7bd9bcf3 | 638 | } |
42a38cf7 MH |
639 | ret = 0; |
640 | goto out; | |
7bd9bcf3 | 641 | } |
7bd9bcf3 | 642 | |
7048653a DT |
643 | /* Follow "normalized" - ie "refs/.." symlinks by hand */ |
644 | if (S_ISLNK(st.st_mode)) { | |
42a38cf7 MH |
645 | strbuf_reset(&sb_contents); |
646 | if (strbuf_readlink(&sb_contents, path, 0) < 0) { | |
7048653a | 647 | if (errno == ENOENT || errno == EINVAL) |
7bd9bcf3 MH |
648 | /* inconsistent with lstat; retry */ |
649 | goto stat_ref; | |
650 | else | |
42a38cf7 | 651 | goto out; |
7bd9bcf3 | 652 | } |
42a38cf7 MH |
653 | if (starts_with(sb_contents.buf, "refs/") && |
654 | !check_refname_format(sb_contents.buf, 0)) { | |
92b38093 | 655 | strbuf_swap(&sb_contents, referent); |
3a0b6b9a | 656 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
657 | ret = 0; |
658 | goto out; | |
7bd9bcf3 | 659 | } |
3f7bd767 JK |
660 | /* |
661 | * It doesn't look like a refname; fall through to just | |
662 | * treating it like a non-symlink, and reading whatever it | |
663 | * points to. | |
664 | */ | |
7048653a | 665 | } |
7bd9bcf3 | 666 | |
7048653a DT |
667 | /* Is it a directory? */ |
668 | if (S_ISDIR(st.st_mode)) { | |
e167a567 MH |
669 | /* |
670 | * Even though there is a directory where the loose | |
671 | * ref is supposed to be, there could still be a | |
672 | * packed ref: | |
673 | */ | |
611118d0 | 674 | if (resolve_packed_ref(refs, refname, sha1, type)) { |
e167a567 MH |
675 | errno = EISDIR; |
676 | goto out; | |
677 | } | |
678 | ret = 0; | |
42a38cf7 | 679 | goto out; |
7048653a DT |
680 | } |
681 | ||
682 | /* | |
683 | * Anything else, just open it and try to use it as | |
684 | * a ref | |
685 | */ | |
686 | fd = open(path, O_RDONLY); | |
687 | if (fd < 0) { | |
3f7bd767 | 688 | if (errno == ENOENT && !S_ISLNK(st.st_mode)) |
7048653a DT |
689 | /* inconsistent with lstat; retry */ |
690 | goto stat_ref; | |
691 | else | |
42a38cf7 | 692 | goto out; |
7048653a | 693 | } |
42a38cf7 MH |
694 | strbuf_reset(&sb_contents); |
695 | if (strbuf_read(&sb_contents, fd, 256) < 0) { | |
7048653a DT |
696 | int save_errno = errno; |
697 | close(fd); | |
698 | errno = save_errno; | |
42a38cf7 | 699 | goto out; |
7048653a DT |
700 | } |
701 | close(fd); | |
42a38cf7 MH |
702 | strbuf_rtrim(&sb_contents); |
703 | buf = sb_contents.buf; | |
7048653a DT |
704 | if (starts_with(buf, "ref:")) { |
705 | buf += 4; | |
7bd9bcf3 MH |
706 | while (isspace(*buf)) |
707 | buf++; | |
7048653a | 708 | |
92b38093 MH |
709 | strbuf_reset(referent); |
710 | strbuf_addstr(referent, buf); | |
3a0b6b9a | 711 | *type |= REF_ISSYMREF; |
42a38cf7 MH |
712 | ret = 0; |
713 | goto out; | |
7bd9bcf3 | 714 | } |
7bd9bcf3 | 715 | |
7048653a DT |
716 | /* |
717 | * Please note that FETCH_HEAD has additional | |
718 | * data after the sha. | |
719 | */ | |
720 | if (get_sha1_hex(buf, sha1) || | |
721 | (buf[40] != '\0' && !isspace(buf[40]))) { | |
3a0b6b9a | 722 | *type |= REF_ISBROKEN; |
7048653a | 723 | errno = EINVAL; |
42a38cf7 | 724 | goto out; |
7048653a DT |
725 | } |
726 | ||
42a38cf7 | 727 | ret = 0; |
7bd9bcf3 | 728 | |
42a38cf7 MH |
729 | out: |
730 | save_errno = errno; | |
7bd9bcf3 MH |
731 | strbuf_release(&sb_path); |
732 | strbuf_release(&sb_contents); | |
42a38cf7 | 733 | errno = save_errno; |
7bd9bcf3 MH |
734 | return ret; |
735 | } | |
736 | ||
8415d247 MH |
737 | static void unlock_ref(struct ref_lock *lock) |
738 | { | |
739 | /* Do not free lock->lk -- atexit() still looks at them */ | |
740 | if (lock->lk) | |
741 | rollback_lock_file(lock->lk); | |
742 | free(lock->ref_name); | |
8415d247 MH |
743 | free(lock); |
744 | } | |
745 | ||
92b1551b MH |
746 | /* |
747 | * Lock refname, without following symrefs, and set *lock_p to point | |
748 | * at a newly-allocated lock object. Fill in lock->old_oid, referent, | |
749 | * and type similarly to read_raw_ref(). | |
750 | * | |
751 | * The caller must verify that refname is a "safe" reference name (in | |
752 | * the sense of refname_is_safe()) before calling this function. | |
753 | * | |
754 | * If the reference doesn't already exist, verify that refname doesn't | |
755 | * have a D/F conflict with any existing references. extras and skip | |
524a9fdb | 756 | * are passed to refs_verify_refname_available() for this check. |
92b1551b MH |
757 | * |
758 | * If mustexist is not set and the reference is not found or is | |
759 | * broken, lock the reference anyway but clear sha1. | |
760 | * | |
761 | * Return 0 on success. On failure, write an error message to err and | |
762 | * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR. | |
763 | * | |
764 | * Implementation note: This function is basically | |
765 | * | |
766 | * lock reference | |
767 | * read_raw_ref() | |
768 | * | |
769 | * but it includes a lot more code to | |
770 | * - Deal with possible races with other processes | |
524a9fdb | 771 | * - Avoid calling refs_verify_refname_available() when it can be |
92b1551b MH |
772 | * avoided, namely if we were successfully able to read the ref |
773 | * - Generate informative error messages in the case of failure | |
774 | */ | |
f7b0a987 MH |
775 | static int lock_raw_ref(struct files_ref_store *refs, |
776 | const char *refname, int mustexist, | |
92b1551b MH |
777 | const struct string_list *extras, |
778 | const struct string_list *skip, | |
779 | struct ref_lock **lock_p, | |
780 | struct strbuf *referent, | |
781 | unsigned int *type, | |
782 | struct strbuf *err) | |
783 | { | |
784 | struct ref_lock *lock; | |
785 | struct strbuf ref_file = STRBUF_INIT; | |
786 | int attempts_remaining = 3; | |
787 | int ret = TRANSACTION_GENERIC_ERROR; | |
788 | ||
789 | assert(err); | |
32c597e7 | 790 | files_assert_main_repository(refs, "lock_raw_ref"); |
f7b0a987 | 791 | |
92b1551b MH |
792 | *type = 0; |
793 | ||
794 | /* First lock the file so it can't change out from under us. */ | |
795 | ||
796 | *lock_p = lock = xcalloc(1, sizeof(*lock)); | |
797 | ||
798 | lock->ref_name = xstrdup(refname); | |
19e02f4f | 799 | files_ref_path(refs, &ref_file, refname); |
92b1551b MH |
800 | |
801 | retry: | |
802 | switch (safe_create_leading_directories(ref_file.buf)) { | |
803 | case SCLD_OK: | |
804 | break; /* success */ | |
805 | case SCLD_EXISTS: | |
806 | /* | |
807 | * Suppose refname is "refs/foo/bar". We just failed | |
808 | * to create the containing directory, "refs/foo", | |
809 | * because there was a non-directory in the way. This | |
810 | * indicates a D/F conflict, probably because of | |
811 | * another reference such as "refs/foo". There is no | |
812 | * reason to expect this error to be transitory. | |
813 | */ | |
7d2df051 NTND |
814 | if (refs_verify_refname_available(&refs->base, refname, |
815 | extras, skip, err)) { | |
92b1551b MH |
816 | if (mustexist) { |
817 | /* | |
818 | * To the user the relevant error is | |
819 | * that the "mustexist" reference is | |
820 | * missing: | |
821 | */ | |
822 | strbuf_reset(err); | |
823 | strbuf_addf(err, "unable to resolve reference '%s'", | |
824 | refname); | |
825 | } else { | |
826 | /* | |
827 | * The error message set by | |
524a9fdb MH |
828 | * refs_verify_refname_available() is |
829 | * OK. | |
92b1551b MH |
830 | */ |
831 | ret = TRANSACTION_NAME_CONFLICT; | |
832 | } | |
833 | } else { | |
834 | /* | |
835 | * The file that is in the way isn't a loose | |
836 | * reference. Report it as a low-level | |
837 | * failure. | |
838 | */ | |
839 | strbuf_addf(err, "unable to create lock file %s.lock; " | |
840 | "non-directory in the way", | |
841 | ref_file.buf); | |
842 | } | |
843 | goto error_return; | |
844 | case SCLD_VANISHED: | |
845 | /* Maybe another process was tidying up. Try again. */ | |
846 | if (--attempts_remaining > 0) | |
847 | goto retry; | |
848 | /* fall through */ | |
849 | default: | |
850 | strbuf_addf(err, "unable to create directory for %s", | |
851 | ref_file.buf); | |
852 | goto error_return; | |
853 | } | |
854 | ||
855 | if (!lock->lk) | |
856 | lock->lk = xcalloc(1, sizeof(struct lock_file)); | |
857 | ||
4ff0f01c MH |
858 | if (hold_lock_file_for_update_timeout( |
859 | lock->lk, ref_file.buf, LOCK_NO_DEREF, | |
860 | get_files_ref_lock_timeout_ms()) < 0) { | |
92b1551b MH |
861 | if (errno == ENOENT && --attempts_remaining > 0) { |
862 | /* | |
863 | * Maybe somebody just deleted one of the | |
864 | * directories leading to ref_file. Try | |
865 | * again: | |
866 | */ | |
867 | goto retry; | |
868 | } else { | |
869 | unable_to_lock_message(ref_file.buf, errno, err); | |
870 | goto error_return; | |
871 | } | |
872 | } | |
873 | ||
874 | /* | |
875 | * Now we hold the lock and can read the reference without | |
876 | * fear that its value will change. | |
877 | */ | |
878 | ||
f7b0a987 | 879 | if (files_read_raw_ref(&refs->base, refname, |
e1e33b72 | 880 | lock->old_oid.hash, referent, type)) { |
92b1551b MH |
881 | if (errno == ENOENT) { |
882 | if (mustexist) { | |
883 | /* Garden variety missing reference. */ | |
884 | strbuf_addf(err, "unable to resolve reference '%s'", | |
885 | refname); | |
886 | goto error_return; | |
887 | } else { | |
888 | /* | |
889 | * Reference is missing, but that's OK. We | |
890 | * know that there is not a conflict with | |
891 | * another loose reference because | |
892 | * (supposing that we are trying to lock | |
893 | * reference "refs/foo/bar"): | |
894 | * | |
895 | * - We were successfully able to create | |
896 | * the lockfile refs/foo/bar.lock, so we | |
897 | * know there cannot be a loose reference | |
898 | * named "refs/foo". | |
899 | * | |
900 | * - We got ENOENT and not EISDIR, so we | |
901 | * know that there cannot be a loose | |
902 | * reference named "refs/foo/bar/baz". | |
903 | */ | |
904 | } | |
905 | } else if (errno == EISDIR) { | |
906 | /* | |
907 | * There is a directory in the way. It might have | |
908 | * contained references that have been deleted. If | |
909 | * we don't require that the reference already | |
910 | * exists, try to remove the directory so that it | |
911 | * doesn't cause trouble when we want to rename the | |
912 | * lockfile into place later. | |
913 | */ | |
914 | if (mustexist) { | |
915 | /* Garden variety missing reference. */ | |
916 | strbuf_addf(err, "unable to resolve reference '%s'", | |
917 | refname); | |
918 | goto error_return; | |
919 | } else if (remove_dir_recursively(&ref_file, | |
920 | REMOVE_DIR_EMPTY_ONLY)) { | |
b05855b5 MH |
921 | if (refs_verify_refname_available( |
922 | &refs->base, refname, | |
923 | extras, skip, err)) { | |
92b1551b MH |
924 | /* |
925 | * The error message set by | |
926 | * verify_refname_available() is OK. | |
927 | */ | |
928 | ret = TRANSACTION_NAME_CONFLICT; | |
929 | goto error_return; | |
930 | } else { | |
931 | /* | |
932 | * We can't delete the directory, | |
933 | * but we also don't know of any | |
934 | * references that it should | |
935 | * contain. | |
936 | */ | |
937 | strbuf_addf(err, "there is a non-empty directory '%s' " | |
938 | "blocking reference '%s'", | |
939 | ref_file.buf, refname); | |
940 | goto error_return; | |
941 | } | |
942 | } | |
943 | } else if (errno == EINVAL && (*type & REF_ISBROKEN)) { | |
944 | strbuf_addf(err, "unable to resolve reference '%s': " | |
945 | "reference broken", refname); | |
946 | goto error_return; | |
947 | } else { | |
948 | strbuf_addf(err, "unable to resolve reference '%s': %s", | |
949 | refname, strerror(errno)); | |
950 | goto error_return; | |
951 | } | |
952 | ||
953 | /* | |
954 | * If the ref did not exist and we are creating it, | |
524a9fdb MH |
955 | * make sure there is no existing ref that conflicts |
956 | * with refname: | |
92b1551b | 957 | */ |
524a9fdb MH |
958 | if (refs_verify_refname_available( |
959 | &refs->base, refname, | |
960 | extras, skip, err)) | |
92b1551b | 961 | goto error_return; |
92b1551b MH |
962 | } |
963 | ||
964 | ret = 0; | |
965 | goto out; | |
966 | ||
967 | error_return: | |
968 | unlock_ref(lock); | |
969 | *lock_p = NULL; | |
970 | ||
971 | out: | |
972 | strbuf_release(&ref_file); | |
973 | return ret; | |
974 | } | |
975 | ||
bd427cf2 MH |
976 | static int files_peel_ref(struct ref_store *ref_store, |
977 | const char *refname, unsigned char *sha1) | |
7bd9bcf3 | 978 | { |
9e7ec634 NTND |
979 | struct files_ref_store *refs = |
980 | files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB, | |
981 | "peel_ref"); | |
7bd9bcf3 MH |
982 | int flag; |
983 | unsigned char base[20]; | |
984 | ||
4c4de895 MH |
985 | if (current_ref_iter && current_ref_iter->refname == refname) { |
986 | struct object_id peeled; | |
987 | ||
988 | if (ref_iterator_peel(current_ref_iter, &peeled)) | |
7bd9bcf3 | 989 | return -1; |
4c4de895 | 990 | hashcpy(sha1, peeled.hash); |
7bd9bcf3 MH |
991 | return 0; |
992 | } | |
993 | ||
2f40e954 NTND |
994 | if (refs_read_ref_full(ref_store, refname, |
995 | RESOLVE_REF_READING, base, &flag)) | |
7bd9bcf3 MH |
996 | return -1; |
997 | ||
998 | /* | |
999 | * If the reference is packed, read its ref_entry from the | |
1000 | * cache in the hope that we already know its peeled value. | |
1001 | * We only try this optimization on packed references because | |
1002 | * (a) forcing the filling of the loose reference cache could | |
1003 | * be expensive and (b) loose references anyway usually do not | |
1004 | * have REF_KNOWS_PEELED. | |
1005 | */ | |
1006 | if (flag & REF_ISPACKED) { | |
f0d21efc | 1007 | struct ref_entry *r = get_packed_ref(refs, refname); |
7bd9bcf3 MH |
1008 | if (r) { |
1009 | if (peel_entry(r, 0)) | |
1010 | return -1; | |
1011 | hashcpy(sha1, r->u.value.peeled.hash); | |
1012 | return 0; | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | return peel_object(base, sha1); | |
1017 | } | |
1018 | ||
3bc581b9 MH |
1019 | struct files_ref_iterator { |
1020 | struct ref_iterator base; | |
1021 | ||
7bd9bcf3 | 1022 | struct packed_ref_cache *packed_ref_cache; |
3bc581b9 MH |
1023 | struct ref_iterator *iter0; |
1024 | unsigned int flags; | |
1025 | }; | |
7bd9bcf3 | 1026 | |
3bc581b9 MH |
1027 | static int files_ref_iterator_advance(struct ref_iterator *ref_iterator) |
1028 | { | |
1029 | struct files_ref_iterator *iter = | |
1030 | (struct files_ref_iterator *)ref_iterator; | |
1031 | int ok; | |
7bd9bcf3 | 1032 | |
3bc581b9 | 1033 | while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) { |
0c09ec07 DT |
1034 | if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY && |
1035 | ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE) | |
1036 | continue; | |
1037 | ||
3bc581b9 MH |
1038 | if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) && |
1039 | !ref_resolves_to_object(iter->iter0->refname, | |
1040 | iter->iter0->oid, | |
1041 | iter->iter0->flags)) | |
1042 | continue; | |
1043 | ||
1044 | iter->base.refname = iter->iter0->refname; | |
1045 | iter->base.oid = iter->iter0->oid; | |
1046 | iter->base.flags = iter->iter0->flags; | |
1047 | return ITER_OK; | |
7bd9bcf3 MH |
1048 | } |
1049 | ||
3bc581b9 MH |
1050 | iter->iter0 = NULL; |
1051 | if (ref_iterator_abort(ref_iterator) != ITER_DONE) | |
1052 | ok = ITER_ERROR; | |
1053 | ||
1054 | return ok; | |
7bd9bcf3 MH |
1055 | } |
1056 | ||
3bc581b9 MH |
1057 | static int files_ref_iterator_peel(struct ref_iterator *ref_iterator, |
1058 | struct object_id *peeled) | |
7bd9bcf3 | 1059 | { |
3bc581b9 MH |
1060 | struct files_ref_iterator *iter = |
1061 | (struct files_ref_iterator *)ref_iterator; | |
93770590 | 1062 | |
3bc581b9 MH |
1063 | return ref_iterator_peel(iter->iter0, peeled); |
1064 | } | |
1065 | ||
1066 | static int files_ref_iterator_abort(struct ref_iterator *ref_iterator) | |
1067 | { | |
1068 | struct files_ref_iterator *iter = | |
1069 | (struct files_ref_iterator *)ref_iterator; | |
1070 | int ok = ITER_DONE; | |
1071 | ||
1072 | if (iter->iter0) | |
1073 | ok = ref_iterator_abort(iter->iter0); | |
1074 | ||
1075 | release_packed_ref_cache(iter->packed_ref_cache); | |
1076 | base_ref_iterator_free(ref_iterator); | |
1077 | return ok; | |
1078 | } | |
1079 | ||
1080 | static struct ref_iterator_vtable files_ref_iterator_vtable = { | |
1081 | files_ref_iterator_advance, | |
1082 | files_ref_iterator_peel, | |
1083 | files_ref_iterator_abort | |
1084 | }; | |
1085 | ||
1a769003 | 1086 | static struct ref_iterator *files_ref_iterator_begin( |
37b6f6d5 | 1087 | struct ref_store *ref_store, |
3bc581b9 MH |
1088 | const char *prefix, unsigned int flags) |
1089 | { | |
9e7ec634 | 1090 | struct files_ref_store *refs; |
3bc581b9 MH |
1091 | struct ref_iterator *loose_iter, *packed_iter; |
1092 | struct files_ref_iterator *iter; | |
1093 | struct ref_iterator *ref_iterator; | |
0a0865b8 | 1094 | unsigned int required_flags = REF_STORE_READ; |
3bc581b9 | 1095 | |
0a0865b8 MH |
1096 | if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) |
1097 | required_flags |= REF_STORE_ODB; | |
3bc581b9 | 1098 | |
0a0865b8 | 1099 | refs = files_downcast(ref_store, required_flags, "ref_iterator_begin"); |
9e7ec634 | 1100 | |
3bc581b9 MH |
1101 | iter = xcalloc(1, sizeof(*iter)); |
1102 | ref_iterator = &iter->base; | |
1103 | base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable); | |
1104 | ||
1105 | /* | |
1106 | * We must make sure that all loose refs are read before | |
1107 | * accessing the packed-refs file; this avoids a race | |
1108 | * condition if loose refs are migrated to the packed-refs | |
1109 | * file by a simultaneous process, but our in-memory view is | |
1110 | * from before the migration. We ensure this as follows: | |
059ae35a MH |
1111 | * First, we call start the loose refs iteration with its |
1112 | * `prime_ref` argument set to true. This causes the loose | |
1113 | * references in the subtree to be pre-read into the cache. | |
1114 | * (If they've already been read, that's OK; we only need to | |
1115 | * guarantee that they're read before the packed refs, not | |
1116 | * *how much* before.) After that, we call | |
1117 | * get_packed_ref_cache(), which internally checks whether the | |
1118 | * packed-ref cache is up to date with what is on disk, and | |
1119 | * re-reads it if not. | |
3bc581b9 MH |
1120 | */ |
1121 | ||
059ae35a MH |
1122 | loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), |
1123 | prefix, 1); | |
3bc581b9 MH |
1124 | |
1125 | iter->packed_ref_cache = get_packed_ref_cache(refs); | |
1126 | acquire_packed_ref_cache(iter->packed_ref_cache); | |
059ae35a MH |
1127 | packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache, |
1128 | prefix, 0); | |
3bc581b9 MH |
1129 | |
1130 | iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter); | |
1131 | iter->flags = flags; | |
1132 | ||
1133 | return ref_iterator; | |
7bd9bcf3 MH |
1134 | } |
1135 | ||
7bd9bcf3 MH |
1136 | /* |
1137 | * Verify that the reference locked by lock has the value old_sha1. | |
1138 | * Fail if the reference doesn't exist and mustexist is set. Return 0 | |
1139 | * on success. On error, write an error message to err, set errno, and | |
1140 | * return a negative value. | |
1141 | */ | |
2f40e954 | 1142 | static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock, |
7bd9bcf3 MH |
1143 | const unsigned char *old_sha1, int mustexist, |
1144 | struct strbuf *err) | |
1145 | { | |
1146 | assert(err); | |
1147 | ||
2f40e954 NTND |
1148 | if (refs_read_ref_full(ref_store, lock->ref_name, |
1149 | mustexist ? RESOLVE_REF_READING : 0, | |
1150 | lock->old_oid.hash, NULL)) { | |
6294dcb4 JK |
1151 | if (old_sha1) { |
1152 | int save_errno = errno; | |
0568c8e9 | 1153 | strbuf_addf(err, "can't verify ref '%s'", lock->ref_name); |
6294dcb4 JK |
1154 | errno = save_errno; |
1155 | return -1; | |
1156 | } else { | |
c368dde9 | 1157 | oidclr(&lock->old_oid); |
6294dcb4 JK |
1158 | return 0; |
1159 | } | |
7bd9bcf3 | 1160 | } |
6294dcb4 | 1161 | if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) { |
0568c8e9 | 1162 | strbuf_addf(err, "ref '%s' is at %s but expected %s", |
7bd9bcf3 | 1163 | lock->ref_name, |
c368dde9 | 1164 | oid_to_hex(&lock->old_oid), |
7bd9bcf3 MH |
1165 | sha1_to_hex(old_sha1)); |
1166 | errno = EBUSY; | |
1167 | return -1; | |
1168 | } | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | static int remove_empty_directories(struct strbuf *path) | |
1173 | { | |
1174 | /* | |
1175 | * we want to create a file but there is a directory there; | |
1176 | * if that is an empty directory (or a directory that contains | |
1177 | * only empty directories), remove them. | |
1178 | */ | |
1179 | return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY); | |
1180 | } | |
1181 | ||
3b5d3c98 MH |
1182 | static int create_reflock(const char *path, void *cb) |
1183 | { | |
1184 | struct lock_file *lk = cb; | |
1185 | ||
4ff0f01c MH |
1186 | return hold_lock_file_for_update_timeout( |
1187 | lk, path, LOCK_NO_DEREF, | |
1188 | get_files_ref_lock_timeout_ms()) < 0 ? -1 : 0; | |
3b5d3c98 MH |
1189 | } |
1190 | ||
7bd9bcf3 MH |
1191 | /* |
1192 | * Locks a ref returning the lock on success and NULL on failure. | |
1193 | * On failure errno is set to something meaningful. | |
1194 | */ | |
7eb27cdf MH |
1195 | static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs, |
1196 | const char *refname, | |
7bd9bcf3 MH |
1197 | const unsigned char *old_sha1, |
1198 | const struct string_list *extras, | |
1199 | const struct string_list *skip, | |
bcb497d0 | 1200 | unsigned int flags, int *type, |
7bd9bcf3 MH |
1201 | struct strbuf *err) |
1202 | { | |
1203 | struct strbuf ref_file = STRBUF_INIT; | |
7bd9bcf3 MH |
1204 | struct ref_lock *lock; |
1205 | int last_errno = 0; | |
7bd9bcf3 | 1206 | int mustexist = (old_sha1 && !is_null_sha1(old_sha1)); |
7a418f3a | 1207 | int resolve_flags = RESOLVE_REF_NO_RECURSE; |
7a418f3a | 1208 | int resolved; |
7bd9bcf3 | 1209 | |
32c597e7 | 1210 | files_assert_main_repository(refs, "lock_ref_sha1_basic"); |
7bd9bcf3 MH |
1211 | assert(err); |
1212 | ||
1213 | lock = xcalloc(1, sizeof(struct ref_lock)); | |
1214 | ||
1215 | if (mustexist) | |
1216 | resolve_flags |= RESOLVE_REF_READING; | |
2859dcd4 | 1217 | if (flags & REF_DELETING) |
7bd9bcf3 | 1218 | resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME; |
7bd9bcf3 | 1219 | |
19e02f4f | 1220 | files_ref_path(refs, &ref_file, refname); |
2f40e954 NTND |
1221 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
1222 | refname, resolve_flags, | |
1223 | lock->old_oid.hash, type); | |
7a418f3a | 1224 | if (!resolved && errno == EISDIR) { |
7bd9bcf3 MH |
1225 | /* |
1226 | * we are trying to lock foo but we used to | |
1227 | * have foo/bar which now does not exist; | |
1228 | * it is normal for the empty directory 'foo' | |
1229 | * to remain. | |
1230 | */ | |
7a418f3a | 1231 | if (remove_empty_directories(&ref_file)) { |
7bd9bcf3 | 1232 | last_errno = errno; |
b05855b5 MH |
1233 | if (!refs_verify_refname_available( |
1234 | &refs->base, | |
1235 | refname, extras, skip, err)) | |
7bd9bcf3 | 1236 | strbuf_addf(err, "there are still refs under '%s'", |
7a418f3a | 1237 | refname); |
7bd9bcf3 MH |
1238 | goto error_return; |
1239 | } | |
2f40e954 NTND |
1240 | resolved = !!refs_resolve_ref_unsafe(&refs->base, |
1241 | refname, resolve_flags, | |
1242 | lock->old_oid.hash, type); | |
7bd9bcf3 | 1243 | } |
7a418f3a | 1244 | if (!resolved) { |
7bd9bcf3 MH |
1245 | last_errno = errno; |
1246 | if (last_errno != ENOTDIR || | |
b05855b5 MH |
1247 | !refs_verify_refname_available(&refs->base, refname, |
1248 | extras, skip, err)) | |
0568c8e9 | 1249 | strbuf_addf(err, "unable to resolve reference '%s': %s", |
7a418f3a | 1250 | refname, strerror(last_errno)); |
7bd9bcf3 MH |
1251 | |
1252 | goto error_return; | |
1253 | } | |
2859dcd4 | 1254 | |
7bd9bcf3 MH |
1255 | /* |
1256 | * If the ref did not exist and we are creating it, make sure | |
1257 | * there is no existing packed ref whose name begins with our | |
1258 | * refname, nor a packed ref whose name is a proper prefix of | |
1259 | * our refname. | |
1260 | */ | |
1261 | if (is_null_oid(&lock->old_oid) && | |
524a9fdb MH |
1262 | refs_verify_refname_available(&refs->base, refname, |
1263 | extras, skip, err)) { | |
7bd9bcf3 MH |
1264 | last_errno = ENOTDIR; |
1265 | goto error_return; | |
1266 | } | |
1267 | ||
1268 | lock->lk = xcalloc(1, sizeof(struct lock_file)); | |
1269 | ||
7bd9bcf3 | 1270 | lock->ref_name = xstrdup(refname); |
7bd9bcf3 | 1271 | |
3b5d3c98 | 1272 | if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) { |
7bd9bcf3 | 1273 | last_errno = errno; |
3b5d3c98 | 1274 | unable_to_lock_message(ref_file.buf, errno, err); |
7bd9bcf3 MH |
1275 | goto error_return; |
1276 | } | |
1277 | ||
2f40e954 | 1278 | if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) { |
7bd9bcf3 MH |
1279 | last_errno = errno; |
1280 | goto error_return; | |
1281 | } | |
1282 | goto out; | |
1283 | ||
1284 | error_return: | |
1285 | unlock_ref(lock); | |
1286 | lock = NULL; | |
1287 | ||
1288 | out: | |
1289 | strbuf_release(&ref_file); | |
7bd9bcf3 MH |
1290 | errno = last_errno; |
1291 | return lock; | |
1292 | } | |
1293 | ||
1294 | /* | |
1295 | * Write an entry to the packed-refs file for the specified refname. | |
1296 | * If peeled is non-NULL, write it as the entry's peeled value. | |
1297 | */ | |
1710fbaf MH |
1298 | static void write_packed_entry(FILE *fh, const char *refname, |
1299 | const unsigned char *sha1, | |
1300 | const unsigned char *peeled) | |
7bd9bcf3 MH |
1301 | { |
1302 | fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname); | |
1303 | if (peeled) | |
1304 | fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled)); | |
1305 | } | |
1306 | ||
7bd9bcf3 MH |
1307 | /* |
1308 | * Lock the packed-refs file for writing. Flags is passed to | |
1309 | * hold_lock_file_for_update(). Return 0 on success. On errors, set | |
1310 | * errno appropriately and return a nonzero value. | |
1311 | */ | |
49c0df6a | 1312 | static int lock_packed_refs(struct files_ref_store *refs, int flags) |
7bd9bcf3 MH |
1313 | { |
1314 | static int timeout_configured = 0; | |
1315 | static int timeout_value = 1000; | |
7bd9bcf3 MH |
1316 | struct packed_ref_cache *packed_ref_cache; |
1317 | ||
32c597e7 | 1318 | files_assert_main_repository(refs, "lock_packed_refs"); |
49c0df6a | 1319 | |
7bd9bcf3 MH |
1320 | if (!timeout_configured) { |
1321 | git_config_get_int("core.packedrefstimeout", &timeout_value); | |
1322 | timeout_configured = 1; | |
1323 | } | |
1324 | ||
1325 | if (hold_lock_file_for_update_timeout( | |
00d17448 | 1326 | &refs->packed_refs_lock, files_packed_refs_path(refs), |
7bd9bcf3 MH |
1327 | flags, timeout_value) < 0) |
1328 | return -1; | |
fed6ebeb | 1329 | |
7bd9bcf3 | 1330 | /* |
fed6ebeb MH |
1331 | * Now that we hold the `packed-refs` lock, make sure that our |
1332 | * cache matches the current version of the file. Normally | |
1333 | * `get_packed_ref_cache()` does that for us, but that | |
1334 | * function assumes that when the file is locked, any existing | |
1335 | * cache is still valid. We've just locked the file, but it | |
1336 | * might have changed the moment *before* we locked it. | |
7bd9bcf3 | 1337 | */ |
fed6ebeb MH |
1338 | validate_packed_ref_cache(refs); |
1339 | ||
00eebe35 | 1340 | packed_ref_cache = get_packed_ref_cache(refs); |
7bd9bcf3 MH |
1341 | /* Increment the reference count to prevent it from being freed: */ |
1342 | acquire_packed_ref_cache(packed_ref_cache); | |
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | /* | |
1347 | * Write the current version of the packed refs cache from memory to | |
1348 | * disk. The packed-refs file must already be locked for writing (see | |
1349 | * lock_packed_refs()). Return zero on success. On errors, set errno | |
1350 | * and return a nonzero value | |
1351 | */ | |
49c0df6a | 1352 | static int commit_packed_refs(struct files_ref_store *refs) |
7bd9bcf3 MH |
1353 | { |
1354 | struct packed_ref_cache *packed_ref_cache = | |
00eebe35 | 1355 | get_packed_ref_cache(refs); |
1710fbaf | 1356 | int ok, error = 0; |
7bd9bcf3 MH |
1357 | int save_errno = 0; |
1358 | FILE *out; | |
1710fbaf | 1359 | struct ref_iterator *iter; |
7bd9bcf3 | 1360 | |
32c597e7 | 1361 | files_assert_main_repository(refs, "commit_packed_refs"); |
49c0df6a | 1362 | |
00d17448 | 1363 | if (!is_lock_file_locked(&refs->packed_refs_lock)) |
04aea8d4 | 1364 | die("BUG: packed-refs not locked"); |
7bd9bcf3 | 1365 | |
00d17448 | 1366 | out = fdopen_lock_file(&refs->packed_refs_lock, "w"); |
7bd9bcf3 MH |
1367 | if (!out) |
1368 | die_errno("unable to fdopen packed-refs descriptor"); | |
1369 | ||
1370 | fprintf_or_die(out, "%s", PACKED_REFS_HEADER); | |
1710fbaf MH |
1371 | |
1372 | iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0); | |
1373 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
1374 | struct object_id peeled; | |
1375 | int peel_error = ref_iterator_peel(iter, &peeled); | |
1376 | ||
1377 | write_packed_entry(out, iter->refname, iter->oid->hash, | |
1378 | peel_error ? NULL : peeled.hash); | |
1379 | } | |
1380 | ||
1381 | if (ok != ITER_DONE) | |
1382 | die("error while iterating over references"); | |
7bd9bcf3 | 1383 | |
00d17448 | 1384 | if (commit_lock_file(&refs->packed_refs_lock)) { |
7bd9bcf3 MH |
1385 | save_errno = errno; |
1386 | error = -1; | |
1387 | } | |
7bd9bcf3 MH |
1388 | release_packed_ref_cache(packed_ref_cache); |
1389 | errno = save_errno; | |
1390 | return error; | |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * Rollback the lockfile for the packed-refs file, and discard the | |
1395 | * in-memory packed reference cache. (The packed-refs file will be | |
1396 | * read anew if it is needed again after this function is called.) | |
1397 | */ | |
49c0df6a | 1398 | static void rollback_packed_refs(struct files_ref_store *refs) |
7bd9bcf3 MH |
1399 | { |
1400 | struct packed_ref_cache *packed_ref_cache = | |
00eebe35 | 1401 | get_packed_ref_cache(refs); |
7bd9bcf3 | 1402 | |
32c597e7 | 1403 | files_assert_main_repository(refs, "rollback_packed_refs"); |
7bd9bcf3 | 1404 | |
00d17448 | 1405 | if (!is_lock_file_locked(&refs->packed_refs_lock)) |
04aea8d4 | 1406 | die("BUG: packed-refs not locked"); |
00d17448 | 1407 | rollback_lock_file(&refs->packed_refs_lock); |
7bd9bcf3 | 1408 | release_packed_ref_cache(packed_ref_cache); |
00eebe35 | 1409 | clear_packed_ref_cache(refs); |
7bd9bcf3 MH |
1410 | } |
1411 | ||
1412 | struct ref_to_prune { | |
1413 | struct ref_to_prune *next; | |
1414 | unsigned char sha1[20]; | |
1415 | char name[FLEX_ARRAY]; | |
1416 | }; | |
1417 | ||
a8f0db2d MH |
1418 | enum { |
1419 | REMOVE_EMPTY_PARENTS_REF = 0x01, | |
1420 | REMOVE_EMPTY_PARENTS_REFLOG = 0x02 | |
1421 | }; | |
1422 | ||
7bd9bcf3 | 1423 | /* |
a8f0db2d MH |
1424 | * Remove empty parent directories associated with the specified |
1425 | * reference and/or its reflog, but spare [logs/]refs/ and immediate | |
1426 | * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or | |
1427 | * REMOVE_EMPTY_PARENTS_REFLOG. | |
7bd9bcf3 | 1428 | */ |
802de3da NTND |
1429 | static void try_remove_empty_parents(struct files_ref_store *refs, |
1430 | const char *refname, | |
1431 | unsigned int flags) | |
7bd9bcf3 | 1432 | { |
8bdaecb4 | 1433 | struct strbuf buf = STRBUF_INIT; |
e9dcc305 | 1434 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 MH |
1435 | char *p, *q; |
1436 | int i; | |
8bdaecb4 MH |
1437 | |
1438 | strbuf_addstr(&buf, refname); | |
1439 | p = buf.buf; | |
7bd9bcf3 MH |
1440 | for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */ |
1441 | while (*p && *p != '/') | |
1442 | p++; | |
1443 | /* tolerate duplicate slashes; see check_refname_format() */ | |
1444 | while (*p == '/') | |
1445 | p++; | |
1446 | } | |
8bdaecb4 | 1447 | q = buf.buf + buf.len; |
a8f0db2d | 1448 | while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) { |
7bd9bcf3 MH |
1449 | while (q > p && *q != '/') |
1450 | q--; | |
1451 | while (q > p && *(q-1) == '/') | |
1452 | q--; | |
1453 | if (q == p) | |
1454 | break; | |
8bdaecb4 | 1455 | strbuf_setlen(&buf, q - buf.buf); |
e9dcc305 NTND |
1456 | |
1457 | strbuf_reset(&sb); | |
19e02f4f | 1458 | files_ref_path(refs, &sb, buf.buf); |
e9dcc305 | 1459 | if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf)) |
a8f0db2d | 1460 | flags &= ~REMOVE_EMPTY_PARENTS_REF; |
e9dcc305 NTND |
1461 | |
1462 | strbuf_reset(&sb); | |
802de3da | 1463 | files_reflog_path(refs, &sb, buf.buf); |
e9dcc305 | 1464 | if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf)) |
a8f0db2d | 1465 | flags &= ~REMOVE_EMPTY_PARENTS_REFLOG; |
7bd9bcf3 | 1466 | } |
8bdaecb4 | 1467 | strbuf_release(&buf); |
e9dcc305 | 1468 | strbuf_release(&sb); |
7bd9bcf3 MH |
1469 | } |
1470 | ||
1471 | /* make sure nobody touched the ref, and unlink */ | |
2f40e954 | 1472 | static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) |
7bd9bcf3 MH |
1473 | { |
1474 | struct ref_transaction *transaction; | |
1475 | struct strbuf err = STRBUF_INIT; | |
1476 | ||
1477 | if (check_refname_format(r->name, 0)) | |
1478 | return; | |
1479 | ||
2f40e954 | 1480 | transaction = ref_store_transaction_begin(&refs->base, &err); |
7bd9bcf3 MH |
1481 | if (!transaction || |
1482 | ref_transaction_delete(transaction, r->name, r->sha1, | |
c52ce248 | 1483 | REF_ISPRUNING | REF_NODEREF, NULL, &err) || |
7bd9bcf3 MH |
1484 | ref_transaction_commit(transaction, &err)) { |
1485 | ref_transaction_free(transaction); | |
1486 | error("%s", err.buf); | |
1487 | strbuf_release(&err); | |
1488 | return; | |
1489 | } | |
1490 | ref_transaction_free(transaction); | |
1491 | strbuf_release(&err); | |
7bd9bcf3 MH |
1492 | } |
1493 | ||
2f40e954 | 1494 | static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r) |
7bd9bcf3 MH |
1495 | { |
1496 | while (r) { | |
2f40e954 | 1497 | prune_ref(refs, r); |
7bd9bcf3 MH |
1498 | r = r->next; |
1499 | } | |
1500 | } | |
1501 | ||
531cc4a5 MH |
1502 | /* |
1503 | * Return true if the specified reference should be packed. | |
1504 | */ | |
1505 | static int should_pack_ref(const char *refname, | |
1506 | const struct object_id *oid, unsigned int ref_flags, | |
1507 | unsigned int pack_flags) | |
1508 | { | |
1509 | /* Do not pack per-worktree refs: */ | |
1510 | if (ref_type(refname) != REF_TYPE_NORMAL) | |
1511 | return 0; | |
1512 | ||
1513 | /* Do not pack non-tags unless PACK_REFS_ALL is set: */ | |
1514 | if (!(pack_flags & PACK_REFS_ALL) && !starts_with(refname, "refs/tags/")) | |
1515 | return 0; | |
1516 | ||
1517 | /* Do not pack symbolic refs: */ | |
1518 | if (ref_flags & REF_ISSYMREF) | |
1519 | return 0; | |
1520 | ||
1521 | /* Do not pack broken refs: */ | |
1522 | if (!ref_resolves_to_object(refname, oid, ref_flags)) | |
1523 | return 0; | |
1524 | ||
1525 | return 1; | |
1526 | } | |
1527 | ||
8231527e | 1528 | static int files_pack_refs(struct ref_store *ref_store, unsigned int flags) |
7bd9bcf3 | 1529 | { |
00eebe35 | 1530 | struct files_ref_store *refs = |
9e7ec634 NTND |
1531 | files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB, |
1532 | "pack_refs"); | |
50c2d855 MH |
1533 | struct ref_iterator *iter; |
1534 | struct ref_dir *packed_refs; | |
1535 | int ok; | |
1536 | struct ref_to_prune *refs_to_prune = NULL; | |
7bd9bcf3 | 1537 | |
49c0df6a | 1538 | lock_packed_refs(refs, LOCK_DIE_ON_ERROR); |
50c2d855 MH |
1539 | packed_refs = get_packed_refs(refs); |
1540 | ||
1541 | iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL, 0); | |
1542 | while ((ok = ref_iterator_advance(iter)) == ITER_OK) { | |
1543 | /* | |
1544 | * If the loose reference can be packed, add an entry | |
1545 | * in the packed ref cache. If the reference should be | |
1546 | * pruned, also add it to refs_to_prune. | |
1547 | */ | |
1548 | struct ref_entry *packed_entry; | |
7bd9bcf3 | 1549 | |
531cc4a5 MH |
1550 | if (!should_pack_ref(iter->refname, iter->oid, iter->flags, |
1551 | flags)) | |
50c2d855 MH |
1552 | continue; |
1553 | ||
1554 | /* | |
1555 | * Create an entry in the packed-refs cache equivalent | |
1556 | * to the one from the loose ref cache, except that | |
1557 | * we don't copy the peeled status, because we want it | |
1558 | * to be re-peeled. | |
1559 | */ | |
1560 | packed_entry = find_ref_entry(packed_refs, iter->refname); | |
1561 | if (packed_entry) { | |
1562 | /* Overwrite existing packed entry with info from loose entry */ | |
1563 | packed_entry->flag = REF_ISPACKED; | |
1564 | oidcpy(&packed_entry->u.value.oid, iter->oid); | |
1565 | } else { | |
4417df8c | 1566 | packed_entry = create_ref_entry(iter->refname, iter->oid, |
c1da06c6 | 1567 | REF_ISPACKED); |
50c2d855 MH |
1568 | add_ref_entry(packed_refs, packed_entry); |
1569 | } | |
1570 | oidclr(&packed_entry->u.value.peeled); | |
1571 | ||
1572 | /* Schedule the loose reference for pruning if requested. */ | |
1573 | if ((flags & PACK_REFS_PRUNE)) { | |
1574 | struct ref_to_prune *n; | |
1575 | FLEX_ALLOC_STR(n, name, iter->refname); | |
1576 | hashcpy(n->sha1, iter->oid->hash); | |
1577 | n->next = refs_to_prune; | |
1578 | refs_to_prune = n; | |
1579 | } | |
1580 | } | |
1581 | if (ok != ITER_DONE) | |
1582 | die("error while iterating over references"); | |
7bd9bcf3 | 1583 | |
49c0df6a | 1584 | if (commit_packed_refs(refs)) |
7bd9bcf3 MH |
1585 | die_errno("unable to overwrite old ref-pack file"); |
1586 | ||
50c2d855 | 1587 | prune_refs(refs, refs_to_prune); |
7bd9bcf3 MH |
1588 | return 0; |
1589 | } | |
1590 | ||
1591 | /* | |
1592 | * Rewrite the packed-refs file, omitting any refs listed in | |
1593 | * 'refnames'. On error, leave packed-refs unchanged, write an error | |
1594 | * message to 'err', and return a nonzero value. | |
1595 | * | |
1596 | * The refs in 'refnames' needn't be sorted. `err` must not be NULL. | |
1597 | */ | |
0a95ac5f MH |
1598 | static int repack_without_refs(struct files_ref_store *refs, |
1599 | struct string_list *refnames, struct strbuf *err) | |
7bd9bcf3 MH |
1600 | { |
1601 | struct ref_dir *packed; | |
1602 | struct string_list_item *refname; | |
1603 | int ret, needs_repacking = 0, removed = 0; | |
1604 | ||
32c597e7 | 1605 | files_assert_main_repository(refs, "repack_without_refs"); |
7bd9bcf3 MH |
1606 | assert(err); |
1607 | ||
1608 | /* Look for a packed ref */ | |
1609 | for_each_string_list_item(refname, refnames) { | |
f0d21efc | 1610 | if (get_packed_ref(refs, refname->string)) { |
7bd9bcf3 MH |
1611 | needs_repacking = 1; |
1612 | break; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | /* Avoid locking if we have nothing to do */ | |
1617 | if (!needs_repacking) | |
1618 | return 0; /* no refname exists in packed refs */ | |
1619 | ||
49c0df6a | 1620 | if (lock_packed_refs(refs, 0)) { |
33dfb9f3 | 1621 | unable_to_lock_message(files_packed_refs_path(refs), errno, err); |
7bd9bcf3 MH |
1622 | return -1; |
1623 | } | |
00eebe35 | 1624 | packed = get_packed_refs(refs); |
7bd9bcf3 MH |
1625 | |
1626 | /* Remove refnames from the cache */ | |
1627 | for_each_string_list_item(refname, refnames) | |
9fc3b063 | 1628 | if (remove_entry_from_dir(packed, refname->string) != -1) |
7bd9bcf3 MH |
1629 | removed = 1; |
1630 | if (!removed) { | |
1631 | /* | |
1632 | * All packed entries disappeared while we were | |
1633 | * acquiring the lock. | |
1634 | */ | |
49c0df6a | 1635 | rollback_packed_refs(refs); |
7bd9bcf3 MH |
1636 | return 0; |
1637 | } | |
1638 | ||
1639 | /* Write what remains */ | |
49c0df6a | 1640 | ret = commit_packed_refs(refs); |
7bd9bcf3 MH |
1641 | if (ret) |
1642 | strbuf_addf(err, "unable to overwrite old ref-pack file: %s", | |
1643 | strerror(errno)); | |
1644 | return ret; | |
1645 | } | |
1646 | ||
64da4199 | 1647 | static int files_delete_refs(struct ref_store *ref_store, const char *msg, |
a27dcf89 | 1648 | struct string_list *refnames, unsigned int flags) |
7bd9bcf3 | 1649 | { |
0a95ac5f | 1650 | struct files_ref_store *refs = |
9e7ec634 | 1651 | files_downcast(ref_store, REF_STORE_WRITE, "delete_refs"); |
7bd9bcf3 MH |
1652 | struct strbuf err = STRBUF_INIT; |
1653 | int i, result = 0; | |
1654 | ||
1655 | if (!refnames->nr) | |
1656 | return 0; | |
1657 | ||
0a95ac5f | 1658 | result = repack_without_refs(refs, refnames, &err); |
7bd9bcf3 MH |
1659 | if (result) { |
1660 | /* | |
1661 | * If we failed to rewrite the packed-refs file, then | |
1662 | * it is unsafe to try to remove loose refs, because | |
1663 | * doing so might expose an obsolete packed value for | |
1664 | * a reference that might even point at an object that | |
1665 | * has been garbage collected. | |
1666 | */ | |
1667 | if (refnames->nr == 1) | |
1668 | error(_("could not delete reference %s: %s"), | |
1669 | refnames->items[0].string, err.buf); | |
1670 | else | |
1671 | error(_("could not delete references: %s"), err.buf); | |
1672 | ||
1673 | goto out; | |
1674 | } | |
1675 | ||
1676 | for (i = 0; i < refnames->nr; i++) { | |
1677 | const char *refname = refnames->items[i].string; | |
1678 | ||
64da4199 | 1679 | if (refs_delete_ref(&refs->base, msg, refname, NULL, flags)) |
7bd9bcf3 MH |
1680 | result |= error(_("could not remove reference %s"), refname); |
1681 | } | |
1682 | ||
1683 | out: | |
1684 | strbuf_release(&err); | |
1685 | return result; | |
1686 | } | |
1687 | ||
1688 | /* | |
1689 | * People using contrib's git-new-workdir have .git/logs/refs -> | |
1690 | * /some/other/path/.git/logs/refs, and that may live on another device. | |
1691 | * | |
1692 | * IOW, to avoid cross device rename errors, the temporary renamed log must | |
1693 | * live into logs/refs. | |
1694 | */ | |
a5c1efd6 | 1695 | #define TMP_RENAMED_LOG "refs/.tmp-renamed-log" |
7bd9bcf3 | 1696 | |
e9dcc305 NTND |
1697 | struct rename_cb { |
1698 | const char *tmp_renamed_log; | |
1699 | int true_errno; | |
1700 | }; | |
1701 | ||
1702 | static int rename_tmp_log_callback(const char *path, void *cb_data) | |
7bd9bcf3 | 1703 | { |
e9dcc305 | 1704 | struct rename_cb *cb = cb_data; |
7bd9bcf3 | 1705 | |
e9dcc305 | 1706 | if (rename(cb->tmp_renamed_log, path)) { |
6a7f3631 MH |
1707 | /* |
1708 | * rename(a, b) when b is an existing directory ought | |
1709 | * to result in ISDIR, but Solaris 5.8 gives ENOTDIR. | |
1710 | * Sheesh. Record the true errno for error reporting, | |
1711 | * but report EISDIR to raceproof_create_file() so | |
1712 | * that it knows to retry. | |
1713 | */ | |
e9dcc305 | 1714 | cb->true_errno = errno; |
6a7f3631 MH |
1715 | if (errno == ENOTDIR) |
1716 | errno = EISDIR; | |
1717 | return -1; | |
1718 | } else { | |
1719 | return 0; | |
7bd9bcf3 | 1720 | } |
6a7f3631 | 1721 | } |
7bd9bcf3 | 1722 | |
802de3da | 1723 | static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname) |
6a7f3631 | 1724 | { |
e9dcc305 NTND |
1725 | struct strbuf path = STRBUF_INIT; |
1726 | struct strbuf tmp = STRBUF_INIT; | |
1727 | struct rename_cb cb; | |
1728 | int ret; | |
6a7f3631 | 1729 | |
802de3da NTND |
1730 | files_reflog_path(refs, &path, newrefname); |
1731 | files_reflog_path(refs, &tmp, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1732 | cb.tmp_renamed_log = tmp.buf; |
1733 | ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb); | |
6a7f3631 MH |
1734 | if (ret) { |
1735 | if (errno == EISDIR) | |
e9dcc305 | 1736 | error("directory not empty: %s", path.buf); |
6a7f3631 | 1737 | else |
990c98d2 | 1738 | error("unable to move logfile %s to %s: %s", |
e9dcc305 NTND |
1739 | tmp.buf, path.buf, |
1740 | strerror(cb.true_errno)); | |
7bd9bcf3 | 1741 | } |
6a7f3631 | 1742 | |
e9dcc305 NTND |
1743 | strbuf_release(&path); |
1744 | strbuf_release(&tmp); | |
7bd9bcf3 MH |
1745 | return ret; |
1746 | } | |
1747 | ||
7bd9bcf3 | 1748 | static int write_ref_to_lockfile(struct ref_lock *lock, |
4417df8c | 1749 | const struct object_id *oid, struct strbuf *err); |
f18a7892 MH |
1750 | static int commit_ref_update(struct files_ref_store *refs, |
1751 | struct ref_lock *lock, | |
4417df8c | 1752 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 1753 | struct strbuf *err); |
7bd9bcf3 | 1754 | |
9b6b40d9 DT |
1755 | static int files_rename_ref(struct ref_store *ref_store, |
1756 | const char *oldrefname, const char *newrefname, | |
1757 | const char *logmsg) | |
7bd9bcf3 | 1758 | { |
9b6b40d9 | 1759 | struct files_ref_store *refs = |
9e7ec634 | 1760 | files_downcast(ref_store, REF_STORE_WRITE, "rename_ref"); |
4417df8c | 1761 | struct object_id oid, orig_oid; |
7bd9bcf3 MH |
1762 | int flag = 0, logmoved = 0; |
1763 | struct ref_lock *lock; | |
1764 | struct stat loginfo; | |
e9dcc305 NTND |
1765 | struct strbuf sb_oldref = STRBUF_INIT; |
1766 | struct strbuf sb_newref = STRBUF_INIT; | |
1767 | struct strbuf tmp_renamed_log = STRBUF_INIT; | |
1768 | int log, ret; | |
7bd9bcf3 MH |
1769 | struct strbuf err = STRBUF_INIT; |
1770 | ||
802de3da NTND |
1771 | files_reflog_path(refs, &sb_oldref, oldrefname); |
1772 | files_reflog_path(refs, &sb_newref, newrefname); | |
1773 | files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG); | |
e9dcc305 NTND |
1774 | |
1775 | log = !lstat(sb_oldref.buf, &loginfo); | |
0a3f07d6 NTND |
1776 | if (log && S_ISLNK(loginfo.st_mode)) { |
1777 | ret = error("reflog for %s is a symlink", oldrefname); | |
1778 | goto out; | |
1779 | } | |
7bd9bcf3 | 1780 | |
2f40e954 NTND |
1781 | if (!refs_resolve_ref_unsafe(&refs->base, oldrefname, |
1782 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, | |
4417df8c | 1783 | orig_oid.hash, &flag)) { |
0a3f07d6 NTND |
1784 | ret = error("refname %s not found", oldrefname); |
1785 | goto out; | |
1786 | } | |
e711b1af | 1787 | |
0a3f07d6 NTND |
1788 | if (flag & REF_ISSYMREF) { |
1789 | ret = error("refname %s is a symbolic ref, renaming it is not supported", | |
1790 | oldrefname); | |
1791 | goto out; | |
1792 | } | |
7d2df051 | 1793 | if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) { |
0a3f07d6 NTND |
1794 | ret = 1; |
1795 | goto out; | |
1796 | } | |
7bd9bcf3 | 1797 | |
e9dcc305 | 1798 | if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) { |
a5c1efd6 | 1799 | ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s", |
0a3f07d6 NTND |
1800 | oldrefname, strerror(errno)); |
1801 | goto out; | |
1802 | } | |
7bd9bcf3 | 1803 | |
2f40e954 | 1804 | if (refs_delete_ref(&refs->base, logmsg, oldrefname, |
4417df8c | 1805 | orig_oid.hash, REF_NODEREF)) { |
7bd9bcf3 MH |
1806 | error("unable to delete old %s", oldrefname); |
1807 | goto rollback; | |
1808 | } | |
1809 | ||
12fd3496 | 1810 | /* |
4417df8c | 1811 | * Since we are doing a shallow lookup, oid is not the |
1812 | * correct value to pass to delete_ref as old_oid. But that | |
1813 | * doesn't matter, because an old_oid check wouldn't add to | |
12fd3496 DT |
1814 | * the safety anyway; we want to delete the reference whatever |
1815 | * its current value. | |
1816 | */ | |
2f40e954 NTND |
1817 | if (!refs_read_ref_full(&refs->base, newrefname, |
1818 | RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, | |
4417df8c | 1819 | oid.hash, NULL) && |
2f40e954 NTND |
1820 | refs_delete_ref(&refs->base, NULL, newrefname, |
1821 | NULL, REF_NODEREF)) { | |
58364324 | 1822 | if (errno == EISDIR) { |
7bd9bcf3 MH |
1823 | struct strbuf path = STRBUF_INIT; |
1824 | int result; | |
1825 | ||
19e02f4f | 1826 | files_ref_path(refs, &path, newrefname); |
7bd9bcf3 MH |
1827 | result = remove_empty_directories(&path); |
1828 | strbuf_release(&path); | |
1829 | ||
1830 | if (result) { | |
1831 | error("Directory not empty: %s", newrefname); | |
1832 | goto rollback; | |
1833 | } | |
1834 | } else { | |
1835 | error("unable to delete existing %s", newrefname); | |
1836 | goto rollback; | |
1837 | } | |
1838 | } | |
1839 | ||
802de3da | 1840 | if (log && rename_tmp_log(refs, newrefname)) |
7bd9bcf3 MH |
1841 | goto rollback; |
1842 | ||
1843 | logmoved = log; | |
1844 | ||
7eb27cdf MH |
1845 | lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL, |
1846 | REF_NODEREF, NULL, &err); | |
7bd9bcf3 MH |
1847 | if (!lock) { |
1848 | error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf); | |
1849 | strbuf_release(&err); | |
1850 | goto rollback; | |
1851 | } | |
4417df8c | 1852 | oidcpy(&lock->old_oid, &orig_oid); |
7bd9bcf3 | 1853 | |
4417df8c | 1854 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1855 | commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) { | |
7bd9bcf3 MH |
1856 | error("unable to write current sha1 into %s: %s", newrefname, err.buf); |
1857 | strbuf_release(&err); | |
1858 | goto rollback; | |
1859 | } | |
1860 | ||
0a3f07d6 NTND |
1861 | ret = 0; |
1862 | goto out; | |
7bd9bcf3 MH |
1863 | |
1864 | rollback: | |
7eb27cdf MH |
1865 | lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL, |
1866 | REF_NODEREF, NULL, &err); | |
7bd9bcf3 MH |
1867 | if (!lock) { |
1868 | error("unable to lock %s for rollback: %s", oldrefname, err.buf); | |
1869 | strbuf_release(&err); | |
1870 | goto rollbacklog; | |
1871 | } | |
1872 | ||
1873 | flag = log_all_ref_updates; | |
341fb286 | 1874 | log_all_ref_updates = LOG_REFS_NONE; |
4417df8c | 1875 | if (write_ref_to_lockfile(lock, &orig_oid, &err) || |
1876 | commit_ref_update(refs, lock, &orig_oid, NULL, &err)) { | |
7bd9bcf3 MH |
1877 | error("unable to write current sha1 into %s: %s", oldrefname, err.buf); |
1878 | strbuf_release(&err); | |
1879 | } | |
1880 | log_all_ref_updates = flag; | |
1881 | ||
1882 | rollbacklog: | |
e9dcc305 | 1883 | if (logmoved && rename(sb_newref.buf, sb_oldref.buf)) |
7bd9bcf3 MH |
1884 | error("unable to restore logfile %s from %s: %s", |
1885 | oldrefname, newrefname, strerror(errno)); | |
1886 | if (!logmoved && log && | |
e9dcc305 | 1887 | rename(tmp_renamed_log.buf, sb_oldref.buf)) |
a5c1efd6 | 1888 | error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s", |
7bd9bcf3 | 1889 | oldrefname, strerror(errno)); |
0a3f07d6 NTND |
1890 | ret = 1; |
1891 | out: | |
e9dcc305 NTND |
1892 | strbuf_release(&sb_newref); |
1893 | strbuf_release(&sb_oldref); | |
1894 | strbuf_release(&tmp_renamed_log); | |
1895 | ||
0a3f07d6 | 1896 | return ret; |
7bd9bcf3 MH |
1897 | } |
1898 | ||
1899 | static int close_ref(struct ref_lock *lock) | |
1900 | { | |
1901 | if (close_lock_file(lock->lk)) | |
1902 | return -1; | |
1903 | return 0; | |
1904 | } | |
1905 | ||
1906 | static int commit_ref(struct ref_lock *lock) | |
1907 | { | |
5387c0d8 MH |
1908 | char *path = get_locked_file_path(lock->lk); |
1909 | struct stat st; | |
1910 | ||
1911 | if (!lstat(path, &st) && S_ISDIR(st.st_mode)) { | |
1912 | /* | |
1913 | * There is a directory at the path we want to rename | |
1914 | * the lockfile to. Hopefully it is empty; try to | |
1915 | * delete it. | |
1916 | */ | |
1917 | size_t len = strlen(path); | |
1918 | struct strbuf sb_path = STRBUF_INIT; | |
1919 | ||
1920 | strbuf_attach(&sb_path, path, len, len); | |
1921 | ||
1922 | /* | |
1923 | * If this fails, commit_lock_file() will also fail | |
1924 | * and will report the problem. | |
1925 | */ | |
1926 | remove_empty_directories(&sb_path); | |
1927 | strbuf_release(&sb_path); | |
1928 | } else { | |
1929 | free(path); | |
1930 | } | |
1931 | ||
7bd9bcf3 MH |
1932 | if (commit_lock_file(lock->lk)) |
1933 | return -1; | |
1934 | return 0; | |
1935 | } | |
1936 | ||
1fb0c809 MH |
1937 | static int open_or_create_logfile(const char *path, void *cb) |
1938 | { | |
1939 | int *fd = cb; | |
1940 | ||
1941 | *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666); | |
1942 | return (*fd < 0) ? -1 : 0; | |
1943 | } | |
1944 | ||
7bd9bcf3 | 1945 | /* |
4533e534 MH |
1946 | * Create a reflog for a ref. If force_create = 0, only create the |
1947 | * reflog for certain refs (those for which should_autocreate_reflog | |
1948 | * returns non-zero). Otherwise, create it regardless of the reference | |
1949 | * name. If the logfile already existed or was created, return 0 and | |
1950 | * set *logfd to the file descriptor opened for appending to the file. | |
1951 | * If no logfile exists and we decided not to create one, return 0 and | |
1952 | * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and | |
1953 | * return -1. | |
7bd9bcf3 | 1954 | */ |
802de3da NTND |
1955 | static int log_ref_setup(struct files_ref_store *refs, |
1956 | const char *refname, int force_create, | |
4533e534 | 1957 | int *logfd, struct strbuf *err) |
7bd9bcf3 | 1958 | { |
802de3da NTND |
1959 | struct strbuf logfile_sb = STRBUF_INIT; |
1960 | char *logfile; | |
1961 | ||
1962 | files_reflog_path(refs, &logfile_sb, refname); | |
1963 | logfile = strbuf_detach(&logfile_sb, NULL); | |
7bd9bcf3 | 1964 | |
7bd9bcf3 | 1965 | if (force_create || should_autocreate_reflog(refname)) { |
4533e534 | 1966 | if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) { |
1fb0c809 MH |
1967 | if (errno == ENOENT) |
1968 | strbuf_addf(err, "unable to create directory for '%s': " | |
4533e534 | 1969 | "%s", logfile, strerror(errno)); |
1fb0c809 MH |
1970 | else if (errno == EISDIR) |
1971 | strbuf_addf(err, "there are still logs under '%s'", | |
4533e534 | 1972 | logfile); |
1fb0c809 | 1973 | else |
854bda6b | 1974 | strbuf_addf(err, "unable to append to '%s': %s", |
4533e534 | 1975 | logfile, strerror(errno)); |
7bd9bcf3 | 1976 | |
4533e534 | 1977 | goto error; |
7bd9bcf3 | 1978 | } |
854bda6b | 1979 | } else { |
4533e534 | 1980 | *logfd = open(logfile, O_APPEND | O_WRONLY, 0666); |
e404f459 | 1981 | if (*logfd < 0) { |
854bda6b MH |
1982 | if (errno == ENOENT || errno == EISDIR) { |
1983 | /* | |
1984 | * The logfile doesn't already exist, | |
1985 | * but that is not an error; it only | |
1986 | * means that we won't write log | |
1987 | * entries to it. | |
1988 | */ | |
1989 | ; | |
1990 | } else { | |
1991 | strbuf_addf(err, "unable to append to '%s': %s", | |
4533e534 MH |
1992 | logfile, strerror(errno)); |
1993 | goto error; | |
854bda6b | 1994 | } |
7bd9bcf3 MH |
1995 | } |
1996 | } | |
1997 | ||
e404f459 | 1998 | if (*logfd >= 0) |
4533e534 | 1999 | adjust_shared_perm(logfile); |
854bda6b | 2000 | |
4533e534 | 2001 | free(logfile); |
7bd9bcf3 | 2002 | return 0; |
7bd9bcf3 | 2003 | |
4533e534 MH |
2004 | error: |
2005 | free(logfile); | |
2006 | return -1; | |
7bd9bcf3 | 2007 | } |
7bd9bcf3 | 2008 | |
e3688bd6 DT |
2009 | static int files_create_reflog(struct ref_store *ref_store, |
2010 | const char *refname, int force_create, | |
2011 | struct strbuf *err) | |
7bd9bcf3 | 2012 | { |
802de3da | 2013 | struct files_ref_store *refs = |
9e7ec634 | 2014 | files_downcast(ref_store, REF_STORE_WRITE, "create_reflog"); |
e404f459 | 2015 | int fd; |
7bd9bcf3 | 2016 | |
802de3da | 2017 | if (log_ref_setup(refs, refname, force_create, &fd, err)) |
4533e534 MH |
2018 | return -1; |
2019 | ||
e404f459 MH |
2020 | if (fd >= 0) |
2021 | close(fd); | |
4533e534 MH |
2022 | |
2023 | return 0; | |
7bd9bcf3 MH |
2024 | } |
2025 | ||
4417df8c | 2026 | static int log_ref_write_fd(int fd, const struct object_id *old_oid, |
2027 | const struct object_id *new_oid, | |
7bd9bcf3 MH |
2028 | const char *committer, const char *msg) |
2029 | { | |
2030 | int msglen, written; | |
2031 | unsigned maxlen, len; | |
2032 | char *logrec; | |
2033 | ||
2034 | msglen = msg ? strlen(msg) : 0; | |
2035 | maxlen = strlen(committer) + msglen + 100; | |
2036 | logrec = xmalloc(maxlen); | |
2037 | len = xsnprintf(logrec, maxlen, "%s %s %s\n", | |
4417df8c | 2038 | oid_to_hex(old_oid), |
2039 | oid_to_hex(new_oid), | |
7bd9bcf3 MH |
2040 | committer); |
2041 | if (msglen) | |
2042 | len += copy_reflog_msg(logrec + len - 1, msg) - 1; | |
2043 | ||
2044 | written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; | |
2045 | free(logrec); | |
2046 | if (written != len) | |
2047 | return -1; | |
2048 | ||
2049 | return 0; | |
2050 | } | |
2051 | ||
802de3da | 2052 | static int files_log_ref_write(struct files_ref_store *refs, |
4417df8c | 2053 | const char *refname, const struct object_id *old_oid, |
2054 | const struct object_id *new_oid, const char *msg, | |
11f8457f | 2055 | int flags, struct strbuf *err) |
7bd9bcf3 | 2056 | { |
e404f459 | 2057 | int logfd, result; |
7bd9bcf3 | 2058 | |
341fb286 CW |
2059 | if (log_all_ref_updates == LOG_REFS_UNSET) |
2060 | log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL; | |
7bd9bcf3 | 2061 | |
802de3da NTND |
2062 | result = log_ref_setup(refs, refname, |
2063 | flags & REF_FORCE_CREATE_REFLOG, | |
4533e534 | 2064 | &logfd, err); |
7bd9bcf3 MH |
2065 | |
2066 | if (result) | |
2067 | return result; | |
2068 | ||
7bd9bcf3 MH |
2069 | if (logfd < 0) |
2070 | return 0; | |
4417df8c | 2071 | result = log_ref_write_fd(logfd, old_oid, new_oid, |
7bd9bcf3 MH |
2072 | git_committer_info(0), msg); |
2073 | if (result) { | |
e9dcc305 | 2074 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
2075 | int save_errno = errno; |
2076 | ||
802de3da | 2077 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 2078 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
2079 | sb.buf, strerror(save_errno)); |
2080 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2081 | close(logfd); |
2082 | return -1; | |
2083 | } | |
2084 | if (close(logfd)) { | |
e9dcc305 | 2085 | struct strbuf sb = STRBUF_INIT; |
87b21e05 MH |
2086 | int save_errno = errno; |
2087 | ||
802de3da | 2088 | files_reflog_path(refs, &sb, refname); |
87b21e05 | 2089 | strbuf_addf(err, "unable to append to '%s': %s", |
e9dcc305 NTND |
2090 | sb.buf, strerror(save_errno)); |
2091 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2092 | return -1; |
2093 | } | |
2094 | return 0; | |
2095 | } | |
2096 | ||
7bd9bcf3 MH |
2097 | /* |
2098 | * Write sha1 into the open lockfile, then close the lockfile. On | |
2099 | * errors, rollback the lockfile, fill in *err and | |
2100 | * return -1. | |
2101 | */ | |
2102 | static int write_ref_to_lockfile(struct ref_lock *lock, | |
4417df8c | 2103 | const struct object_id *oid, struct strbuf *err) |
7bd9bcf3 MH |
2104 | { |
2105 | static char term = '\n'; | |
2106 | struct object *o; | |
2107 | int fd; | |
2108 | ||
c251c83d | 2109 | o = parse_object(oid); |
7bd9bcf3 MH |
2110 | if (!o) { |
2111 | strbuf_addf(err, | |
0568c8e9 | 2112 | "trying to write ref '%s' with nonexistent object %s", |
4417df8c | 2113 | lock->ref_name, oid_to_hex(oid)); |
7bd9bcf3 MH |
2114 | unlock_ref(lock); |
2115 | return -1; | |
2116 | } | |
2117 | if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) { | |
2118 | strbuf_addf(err, | |
0568c8e9 | 2119 | "trying to write non-commit object %s to branch '%s'", |
4417df8c | 2120 | oid_to_hex(oid), lock->ref_name); |
7bd9bcf3 MH |
2121 | unlock_ref(lock); |
2122 | return -1; | |
2123 | } | |
2124 | fd = get_lock_file_fd(lock->lk); | |
4417df8c | 2125 | if (write_in_full(fd, oid_to_hex(oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ || |
7bd9bcf3 MH |
2126 | write_in_full(fd, &term, 1) != 1 || |
2127 | close_ref(lock) < 0) { | |
2128 | strbuf_addf(err, | |
0568c8e9 | 2129 | "couldn't write '%s'", get_lock_file_path(lock->lk)); |
7bd9bcf3 MH |
2130 | unlock_ref(lock); |
2131 | return -1; | |
2132 | } | |
2133 | return 0; | |
2134 | } | |
2135 | ||
2136 | /* | |
2137 | * Commit a change to a loose reference that has already been written | |
2138 | * to the loose reference lockfile. Also update the reflogs if | |
2139 | * necessary, using the specified lockmsg (which can be NULL). | |
2140 | */ | |
f18a7892 MH |
2141 | static int commit_ref_update(struct files_ref_store *refs, |
2142 | struct ref_lock *lock, | |
4417df8c | 2143 | const struct object_id *oid, const char *logmsg, |
5d9b2de4 | 2144 | struct strbuf *err) |
7bd9bcf3 | 2145 | { |
32c597e7 | 2146 | files_assert_main_repository(refs, "commit_ref_update"); |
00eebe35 MH |
2147 | |
2148 | clear_loose_ref_cache(refs); | |
802de3da | 2149 | if (files_log_ref_write(refs, lock->ref_name, |
4417df8c | 2150 | &lock->old_oid, oid, |
81b1b6d4 | 2151 | logmsg, 0, err)) { |
7bd9bcf3 | 2152 | char *old_msg = strbuf_detach(err, NULL); |
0568c8e9 | 2153 | strbuf_addf(err, "cannot update the ref '%s': %s", |
7bd9bcf3 MH |
2154 | lock->ref_name, old_msg); |
2155 | free(old_msg); | |
2156 | unlock_ref(lock); | |
2157 | return -1; | |
2158 | } | |
7a418f3a MH |
2159 | |
2160 | if (strcmp(lock->ref_name, "HEAD") != 0) { | |
7bd9bcf3 MH |
2161 | /* |
2162 | * Special hack: If a branch is updated directly and HEAD | |
2163 | * points to it (may happen on the remote side of a push | |
2164 | * for example) then logically the HEAD reflog should be | |
2165 | * updated too. | |
2166 | * A generic solution implies reverse symref information, | |
2167 | * but finding all symrefs pointing to the given branch | |
2168 | * would be rather costly for this rare event (the direct | |
2169 | * update of a branch) to be worth it. So let's cheat and | |
2170 | * check with HEAD only which should cover 99% of all usage | |
2171 | * scenarios (even 100% of the default ones). | |
2172 | */ | |
4417df8c | 2173 | struct object_id head_oid; |
7bd9bcf3 MH |
2174 | int head_flag; |
2175 | const char *head_ref; | |
7a418f3a | 2176 | |
2f40e954 NTND |
2177 | head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD", |
2178 | RESOLVE_REF_READING, | |
4417df8c | 2179 | head_oid.hash, &head_flag); |
7bd9bcf3 MH |
2180 | if (head_ref && (head_flag & REF_ISSYMREF) && |
2181 | !strcmp(head_ref, lock->ref_name)) { | |
2182 | struct strbuf log_err = STRBUF_INIT; | |
802de3da | 2183 | if (files_log_ref_write(refs, "HEAD", |
4417df8c | 2184 | &lock->old_oid, oid, |
802de3da | 2185 | logmsg, 0, &log_err)) { |
7bd9bcf3 MH |
2186 | error("%s", log_err.buf); |
2187 | strbuf_release(&log_err); | |
2188 | } | |
2189 | } | |
2190 | } | |
7a418f3a | 2191 | |
7bd9bcf3 | 2192 | if (commit_ref(lock)) { |
0568c8e9 | 2193 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); |
7bd9bcf3 MH |
2194 | unlock_ref(lock); |
2195 | return -1; | |
2196 | } | |
2197 | ||
2198 | unlock_ref(lock); | |
2199 | return 0; | |
2200 | } | |
2201 | ||
370e5ad6 | 2202 | static int create_ref_symlink(struct ref_lock *lock, const char *target) |
7bd9bcf3 | 2203 | { |
370e5ad6 | 2204 | int ret = -1; |
7bd9bcf3 | 2205 | #ifndef NO_SYMLINK_HEAD |
370e5ad6 JK |
2206 | char *ref_path = get_locked_file_path(lock->lk); |
2207 | unlink(ref_path); | |
2208 | ret = symlink(target, ref_path); | |
2209 | free(ref_path); | |
2210 | ||
2211 | if (ret) | |
7bd9bcf3 | 2212 | fprintf(stderr, "no symlink - falling back to symbolic ref\n"); |
7bd9bcf3 | 2213 | #endif |
370e5ad6 JK |
2214 | return ret; |
2215 | } | |
7bd9bcf3 | 2216 | |
802de3da NTND |
2217 | static void update_symref_reflog(struct files_ref_store *refs, |
2218 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
2219 | const char *target, const char *logmsg) |
2220 | { | |
2221 | struct strbuf err = STRBUF_INIT; | |
4417df8c | 2222 | struct object_id new_oid; |
2f40e954 NTND |
2223 | if (logmsg && |
2224 | !refs_read_ref_full(&refs->base, target, | |
4417df8c | 2225 | RESOLVE_REF_READING, new_oid.hash, NULL) && |
2226 | files_log_ref_write(refs, refname, &lock->old_oid, | |
2227 | &new_oid, logmsg, 0, &err)) { | |
7bd9bcf3 MH |
2228 | error("%s", err.buf); |
2229 | strbuf_release(&err); | |
2230 | } | |
370e5ad6 | 2231 | } |
7bd9bcf3 | 2232 | |
802de3da NTND |
2233 | static int create_symref_locked(struct files_ref_store *refs, |
2234 | struct ref_lock *lock, const char *refname, | |
370e5ad6 JK |
2235 | const char *target, const char *logmsg) |
2236 | { | |
2237 | if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { | |
802de3da | 2238 | update_symref_reflog(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
2239 | return 0; |
2240 | } | |
2241 | ||
2242 | if (!fdopen_lock_file(lock->lk, "w")) | |
2243 | return error("unable to fdopen %s: %s", | |
2244 | lock->lk->tempfile.filename.buf, strerror(errno)); | |
2245 | ||
802de3da | 2246 | update_symref_reflog(refs, lock, refname, target, logmsg); |
396da8f7 | 2247 | |
370e5ad6 JK |
2248 | /* no error check; commit_ref will check ferror */ |
2249 | fprintf(lock->lk->tempfile.fp, "ref: %s\n", target); | |
2250 | if (commit_ref(lock) < 0) | |
2251 | return error("unable to write symref for %s: %s", refname, | |
2252 | strerror(errno)); | |
7bd9bcf3 MH |
2253 | return 0; |
2254 | } | |
2255 | ||
284689ba MH |
2256 | static int files_create_symref(struct ref_store *ref_store, |
2257 | const char *refname, const char *target, | |
2258 | const char *logmsg) | |
370e5ad6 | 2259 | { |
7eb27cdf | 2260 | struct files_ref_store *refs = |
9e7ec634 | 2261 | files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); |
370e5ad6 JK |
2262 | struct strbuf err = STRBUF_INIT; |
2263 | struct ref_lock *lock; | |
2264 | int ret; | |
2265 | ||
7eb27cdf MH |
2266 | lock = lock_ref_sha1_basic(refs, refname, NULL, |
2267 | NULL, NULL, REF_NODEREF, NULL, | |
370e5ad6 JK |
2268 | &err); |
2269 | if (!lock) { | |
2270 | error("%s", err.buf); | |
2271 | strbuf_release(&err); | |
2272 | return -1; | |
2273 | } | |
2274 | ||
802de3da | 2275 | ret = create_symref_locked(refs, lock, refname, target, logmsg); |
370e5ad6 JK |
2276 | unlock_ref(lock); |
2277 | return ret; | |
2278 | } | |
2279 | ||
e3688bd6 DT |
2280 | static int files_reflog_exists(struct ref_store *ref_store, |
2281 | const char *refname) | |
7bd9bcf3 | 2282 | { |
802de3da | 2283 | struct files_ref_store *refs = |
9e7ec634 | 2284 | files_downcast(ref_store, REF_STORE_READ, "reflog_exists"); |
e9dcc305 | 2285 | struct strbuf sb = STRBUF_INIT; |
7bd9bcf3 | 2286 | struct stat st; |
e9dcc305 | 2287 | int ret; |
7bd9bcf3 | 2288 | |
802de3da | 2289 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2290 | ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode); |
2291 | strbuf_release(&sb); | |
2292 | return ret; | |
7bd9bcf3 MH |
2293 | } |
2294 | ||
e3688bd6 DT |
2295 | static int files_delete_reflog(struct ref_store *ref_store, |
2296 | const char *refname) | |
7bd9bcf3 | 2297 | { |
802de3da | 2298 | struct files_ref_store *refs = |
9e7ec634 | 2299 | files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog"); |
e9dcc305 NTND |
2300 | struct strbuf sb = STRBUF_INIT; |
2301 | int ret; | |
2302 | ||
802de3da | 2303 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2304 | ret = remove_path(sb.buf); |
2305 | strbuf_release(&sb); | |
2306 | return ret; | |
7bd9bcf3 MH |
2307 | } |
2308 | ||
2309 | static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data) | |
2310 | { | |
9461d272 | 2311 | struct object_id ooid, noid; |
7bd9bcf3 | 2312 | char *email_end, *message; |
dddbad72 | 2313 | timestamp_t timestamp; |
7bd9bcf3 | 2314 | int tz; |
43bc3b6c | 2315 | const char *p = sb->buf; |
7bd9bcf3 MH |
2316 | |
2317 | /* old SP new SP name <email> SP time TAB msg LF */ | |
43bc3b6c | 2318 | if (!sb->len || sb->buf[sb->len - 1] != '\n' || |
2319 | parse_oid_hex(p, &ooid, &p) || *p++ != ' ' || | |
2320 | parse_oid_hex(p, &noid, &p) || *p++ != ' ' || | |
2321 | !(email_end = strchr(p, '>')) || | |
7bd9bcf3 | 2322 | email_end[1] != ' ' || |
1aeb7e75 | 2323 | !(timestamp = parse_timestamp(email_end + 2, &message, 10)) || |
7bd9bcf3 MH |
2324 | !message || message[0] != ' ' || |
2325 | (message[1] != '+' && message[1] != '-') || | |
2326 | !isdigit(message[2]) || !isdigit(message[3]) || | |
2327 | !isdigit(message[4]) || !isdigit(message[5])) | |
2328 | return 0; /* corrupt? */ | |
2329 | email_end[1] = '\0'; | |
2330 | tz = strtol(message + 1, NULL, 10); | |
2331 | if (message[6] != '\t') | |
2332 | message += 6; | |
2333 | else | |
2334 | message += 7; | |
43bc3b6c | 2335 | return fn(&ooid, &noid, p, timestamp, tz, message, cb_data); |
7bd9bcf3 MH |
2336 | } |
2337 | ||
2338 | static char *find_beginning_of_line(char *bob, char *scan) | |
2339 | { | |
2340 | while (bob < scan && *(--scan) != '\n') | |
2341 | ; /* keep scanning backwards */ | |
2342 | /* | |
2343 | * Return either beginning of the buffer, or LF at the end of | |
2344 | * the previous line. | |
2345 | */ | |
2346 | return scan; | |
2347 | } | |
2348 | ||
e3688bd6 DT |
2349 | static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store, |
2350 | const char *refname, | |
2351 | each_reflog_ent_fn fn, | |
2352 | void *cb_data) | |
7bd9bcf3 | 2353 | { |
802de3da | 2354 | struct files_ref_store *refs = |
9e7ec634 NTND |
2355 | files_downcast(ref_store, REF_STORE_READ, |
2356 | "for_each_reflog_ent_reverse"); | |
7bd9bcf3 MH |
2357 | struct strbuf sb = STRBUF_INIT; |
2358 | FILE *logfp; | |
2359 | long pos; | |
2360 | int ret = 0, at_tail = 1; | |
2361 | ||
802de3da | 2362 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2363 | logfp = fopen(sb.buf, "r"); |
2364 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2365 | if (!logfp) |
2366 | return -1; | |
2367 | ||
2368 | /* Jump to the end */ | |
2369 | if (fseek(logfp, 0, SEEK_END) < 0) | |
be686f03 RS |
2370 | ret = error("cannot seek back reflog for %s: %s", |
2371 | refname, strerror(errno)); | |
7bd9bcf3 MH |
2372 | pos = ftell(logfp); |
2373 | while (!ret && 0 < pos) { | |
2374 | int cnt; | |
2375 | size_t nread; | |
2376 | char buf[BUFSIZ]; | |
2377 | char *endp, *scanp; | |
2378 | ||
2379 | /* Fill next block from the end */ | |
2380 | cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos; | |
be686f03 RS |
2381 | if (fseek(logfp, pos - cnt, SEEK_SET)) { |
2382 | ret = error("cannot seek back reflog for %s: %s", | |
2383 | refname, strerror(errno)); | |
2384 | break; | |
2385 | } | |
7bd9bcf3 | 2386 | nread = fread(buf, cnt, 1, logfp); |
be686f03 RS |
2387 | if (nread != 1) { |
2388 | ret = error("cannot read %d bytes from reflog for %s: %s", | |
2389 | cnt, refname, strerror(errno)); | |
2390 | break; | |
2391 | } | |
7bd9bcf3 MH |
2392 | pos -= cnt; |
2393 | ||
2394 | scanp = endp = buf + cnt; | |
2395 | if (at_tail && scanp[-1] == '\n') | |
2396 | /* Looking at the final LF at the end of the file */ | |
2397 | scanp--; | |
2398 | at_tail = 0; | |
2399 | ||
2400 | while (buf < scanp) { | |
2401 | /* | |
2402 | * terminating LF of the previous line, or the beginning | |
2403 | * of the buffer. | |
2404 | */ | |
2405 | char *bp; | |
2406 | ||
2407 | bp = find_beginning_of_line(buf, scanp); | |
2408 | ||
2409 | if (*bp == '\n') { | |
2410 | /* | |
2411 | * The newline is the end of the previous line, | |
2412 | * so we know we have complete line starting | |
2413 | * at (bp + 1). Prefix it onto any prior data | |
2414 | * we collected for the line and process it. | |
2415 | */ | |
2416 | strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1)); | |
2417 | scanp = bp; | |
2418 | endp = bp + 1; | |
2419 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2420 | strbuf_reset(&sb); | |
2421 | if (ret) | |
2422 | break; | |
2423 | } else if (!pos) { | |
2424 | /* | |
2425 | * We are at the start of the buffer, and the | |
2426 | * start of the file; there is no previous | |
2427 | * line, and we have everything for this one. | |
2428 | * Process it, and we can end the loop. | |
2429 | */ | |
2430 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
2431 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2432 | strbuf_reset(&sb); | |
2433 | break; | |
2434 | } | |
2435 | ||
2436 | if (bp == buf) { | |
2437 | /* | |
2438 | * We are at the start of the buffer, and there | |
2439 | * is more file to read backwards. Which means | |
2440 | * we are in the middle of a line. Note that we | |
2441 | * may get here even if *bp was a newline; that | |
2442 | * just means we are at the exact end of the | |
2443 | * previous line, rather than some spot in the | |
2444 | * middle. | |
2445 | * | |
2446 | * Save away what we have to be combined with | |
2447 | * the data from the next read. | |
2448 | */ | |
2449 | strbuf_splice(&sb, 0, 0, buf, endp - buf); | |
2450 | break; | |
2451 | } | |
2452 | } | |
2453 | ||
2454 | } | |
2455 | if (!ret && sb.len) | |
2456 | die("BUG: reverse reflog parser had leftover data"); | |
2457 | ||
2458 | fclose(logfp); | |
2459 | strbuf_release(&sb); | |
2460 | return ret; | |
2461 | } | |
2462 | ||
e3688bd6 DT |
2463 | static int files_for_each_reflog_ent(struct ref_store *ref_store, |
2464 | const char *refname, | |
2465 | each_reflog_ent_fn fn, void *cb_data) | |
7bd9bcf3 | 2466 | { |
802de3da | 2467 | struct files_ref_store *refs = |
9e7ec634 NTND |
2468 | files_downcast(ref_store, REF_STORE_READ, |
2469 | "for_each_reflog_ent"); | |
7bd9bcf3 MH |
2470 | FILE *logfp; |
2471 | struct strbuf sb = STRBUF_INIT; | |
2472 | int ret = 0; | |
2473 | ||
802de3da | 2474 | files_reflog_path(refs, &sb, refname); |
e9dcc305 NTND |
2475 | logfp = fopen(sb.buf, "r"); |
2476 | strbuf_release(&sb); | |
7bd9bcf3 MH |
2477 | if (!logfp) |
2478 | return -1; | |
2479 | ||
2480 | while (!ret && !strbuf_getwholeline(&sb, logfp, '\n')) | |
2481 | ret = show_one_reflog_ent(&sb, fn, cb_data); | |
2482 | fclose(logfp); | |
2483 | strbuf_release(&sb); | |
2484 | return ret; | |
2485 | } | |
7bd9bcf3 | 2486 | |
2880d16f MH |
2487 | struct files_reflog_iterator { |
2488 | struct ref_iterator base; | |
7bd9bcf3 | 2489 | |
2f40e954 | 2490 | struct ref_store *ref_store; |
2880d16f MH |
2491 | struct dir_iterator *dir_iterator; |
2492 | struct object_id oid; | |
2493 | }; | |
7bd9bcf3 | 2494 | |
2880d16f MH |
2495 | static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator) |
2496 | { | |
2497 | struct files_reflog_iterator *iter = | |
2498 | (struct files_reflog_iterator *)ref_iterator; | |
2499 | struct dir_iterator *diter = iter->dir_iterator; | |
2500 | int ok; | |
2501 | ||
2502 | while ((ok = dir_iterator_advance(diter)) == ITER_OK) { | |
2503 | int flags; | |
2504 | ||
2505 | if (!S_ISREG(diter->st.st_mode)) | |
7bd9bcf3 | 2506 | continue; |
2880d16f MH |
2507 | if (diter->basename[0] == '.') |
2508 | continue; | |
2509 | if (ends_with(diter->basename, ".lock")) | |
7bd9bcf3 | 2510 | continue; |
7bd9bcf3 | 2511 | |
2f40e954 NTND |
2512 | if (refs_read_ref_full(iter->ref_store, |
2513 | diter->relative_path, 0, | |
2514 | iter->oid.hash, &flags)) { | |
2880d16f MH |
2515 | error("bad ref for %s", diter->path.buf); |
2516 | continue; | |
7bd9bcf3 | 2517 | } |
2880d16f MH |
2518 | |
2519 | iter->base.refname = diter->relative_path; | |
2520 | iter->base.oid = &iter->oid; | |
2521 | iter->base.flags = flags; | |
2522 | return ITER_OK; | |
7bd9bcf3 | 2523 | } |
2880d16f MH |
2524 | |
2525 | iter->dir_iterator = NULL; | |
2526 | if (ref_iterator_abort(ref_iterator) == ITER_ERROR) | |
2527 | ok = ITER_ERROR; | |
2528 | return ok; | |
2529 | } | |
2530 | ||
2531 | static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator, | |
2532 | struct object_id *peeled) | |
2533 | { | |
2534 | die("BUG: ref_iterator_peel() called for reflog_iterator"); | |
2535 | } | |
2536 | ||
2537 | static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator) | |
2538 | { | |
2539 | struct files_reflog_iterator *iter = | |
2540 | (struct files_reflog_iterator *)ref_iterator; | |
2541 | int ok = ITER_DONE; | |
2542 | ||
2543 | if (iter->dir_iterator) | |
2544 | ok = dir_iterator_abort(iter->dir_iterator); | |
2545 | ||
2546 | base_ref_iterator_free(ref_iterator); | |
2547 | return ok; | |
2548 | } | |
2549 | ||
2550 | static struct ref_iterator_vtable files_reflog_iterator_vtable = { | |
2551 | files_reflog_iterator_advance, | |
2552 | files_reflog_iterator_peel, | |
2553 | files_reflog_iterator_abort | |
2554 | }; | |
2555 | ||
e3688bd6 | 2556 | static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store) |
2880d16f | 2557 | { |
802de3da | 2558 | struct files_ref_store *refs = |
9e7ec634 NTND |
2559 | files_downcast(ref_store, REF_STORE_READ, |
2560 | "reflog_iterator_begin"); | |
2880d16f MH |
2561 | struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter)); |
2562 | struct ref_iterator *ref_iterator = &iter->base; | |
e9dcc305 | 2563 | struct strbuf sb = STRBUF_INIT; |
2880d16f MH |
2564 | |
2565 | base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable); | |
802de3da | 2566 | files_reflog_path(refs, &sb, NULL); |
e9dcc305 | 2567 | iter->dir_iterator = dir_iterator_begin(sb.buf); |
2f40e954 | 2568 | iter->ref_store = ref_store; |
e9dcc305 | 2569 | strbuf_release(&sb); |
2880d16f | 2570 | return ref_iterator; |
7bd9bcf3 MH |
2571 | } |
2572 | ||
165056b2 | 2573 | /* |
92b1551b MH |
2574 | * If update is a direct update of head_ref (the reference pointed to |
2575 | * by HEAD), then add an extra REF_LOG_ONLY update for HEAD. | |
2576 | */ | |
2577 | static int split_head_update(struct ref_update *update, | |
2578 | struct ref_transaction *transaction, | |
2579 | const char *head_ref, | |
2580 | struct string_list *affected_refnames, | |
2581 | struct strbuf *err) | |
2582 | { | |
2583 | struct string_list_item *item; | |
2584 | struct ref_update *new_update; | |
2585 | ||
2586 | if ((update->flags & REF_LOG_ONLY) || | |
2587 | (update->flags & REF_ISPRUNING) || | |
2588 | (update->flags & REF_UPDATE_VIA_HEAD)) | |
2589 | return 0; | |
2590 | ||
2591 | if (strcmp(update->refname, head_ref)) | |
2592 | return 0; | |
2593 | ||
2594 | /* | |
2595 | * First make sure that HEAD is not already in the | |
2596 | * transaction. This insertion is O(N) in the transaction | |
2597 | * size, but it happens at most once per transaction. | |
2598 | */ | |
2599 | item = string_list_insert(affected_refnames, "HEAD"); | |
2600 | if (item->util) { | |
2601 | /* An entry already existed */ | |
2602 | strbuf_addf(err, | |
2603 | "multiple updates for 'HEAD' (including one " | |
2604 | "via its referent '%s') are not allowed", | |
2605 | update->refname); | |
2606 | return TRANSACTION_NAME_CONFLICT; | |
2607 | } | |
2608 | ||
2609 | new_update = ref_transaction_add_update( | |
2610 | transaction, "HEAD", | |
2611 | update->flags | REF_LOG_ONLY | REF_NODEREF, | |
98491298 | 2612 | update->new_oid.hash, update->old_oid.hash, |
92b1551b MH |
2613 | update->msg); |
2614 | ||
2615 | item->util = new_update; | |
2616 | ||
2617 | return 0; | |
2618 | } | |
2619 | ||
2620 | /* | |
2621 | * update is for a symref that points at referent and doesn't have | |
2622 | * REF_NODEREF set. Split it into two updates: | |
2623 | * - The original update, but with REF_LOG_ONLY and REF_NODEREF set | |
2624 | * - A new, separate update for the referent reference | |
2625 | * Note that the new update will itself be subject to splitting when | |
2626 | * the iteration gets to it. | |
2627 | */ | |
fcc42ea0 MH |
2628 | static int split_symref_update(struct files_ref_store *refs, |
2629 | struct ref_update *update, | |
92b1551b MH |
2630 | const char *referent, |
2631 | struct ref_transaction *transaction, | |
2632 | struct string_list *affected_refnames, | |
2633 | struct strbuf *err) | |
2634 | { | |
2635 | struct string_list_item *item; | |
2636 | struct ref_update *new_update; | |
2637 | unsigned int new_flags; | |
2638 | ||
2639 | /* | |
2640 | * First make sure that referent is not already in the | |
2641 | * transaction. This insertion is O(N) in the transaction | |
2642 | * size, but it happens at most once per symref in a | |
2643 | * transaction. | |
2644 | */ | |
2645 | item = string_list_insert(affected_refnames, referent); | |
2646 | if (item->util) { | |
2647 | /* An entry already existed */ | |
2648 | strbuf_addf(err, | |
2649 | "multiple updates for '%s' (including one " | |
2650 | "via symref '%s') are not allowed", | |
2651 | referent, update->refname); | |
2652 | return TRANSACTION_NAME_CONFLICT; | |
2653 | } | |
2654 | ||
2655 | new_flags = update->flags; | |
2656 | if (!strcmp(update->refname, "HEAD")) { | |
2657 | /* | |
2658 | * Record that the new update came via HEAD, so that | |
2659 | * when we process it, split_head_update() doesn't try | |
2660 | * to add another reflog update for HEAD. Note that | |
2661 | * this bit will be propagated if the new_update | |
2662 | * itself needs to be split. | |
2663 | */ | |
2664 | new_flags |= REF_UPDATE_VIA_HEAD; | |
2665 | } | |
2666 | ||
2667 | new_update = ref_transaction_add_update( | |
2668 | transaction, referent, new_flags, | |
98491298 | 2669 | update->new_oid.hash, update->old_oid.hash, |
92b1551b MH |
2670 | update->msg); |
2671 | ||
6e30b2f6 MH |
2672 | new_update->parent_update = update; |
2673 | ||
2674 | /* | |
2675 | * Change the symbolic ref update to log only. Also, it | |
2676 | * doesn't need to check its old SHA-1 value, as that will be | |
2677 | * done when new_update is processed. | |
2678 | */ | |
92b1551b | 2679 | update->flags |= REF_LOG_ONLY | REF_NODEREF; |
6e30b2f6 | 2680 | update->flags &= ~REF_HAVE_OLD; |
92b1551b MH |
2681 | |
2682 | item->util = new_update; | |
2683 | ||
2684 | return 0; | |
2685 | } | |
2686 | ||
6e30b2f6 MH |
2687 | /* |
2688 | * Return the refname under which update was originally requested. | |
2689 | */ | |
2690 | static const char *original_update_refname(struct ref_update *update) | |
2691 | { | |
2692 | while (update->parent_update) | |
2693 | update = update->parent_update; | |
2694 | ||
2695 | return update->refname; | |
2696 | } | |
2697 | ||
e3f51039 MH |
2698 | /* |
2699 | * Check whether the REF_HAVE_OLD and old_oid values stored in update | |
2700 | * are consistent with oid, which is the reference's current value. If | |
2701 | * everything is OK, return 0; otherwise, write an error message to | |
2702 | * err and return -1. | |
2703 | */ | |
2704 | static int check_old_oid(struct ref_update *update, struct object_id *oid, | |
2705 | struct strbuf *err) | |
2706 | { | |
2707 | if (!(update->flags & REF_HAVE_OLD) || | |
98491298 | 2708 | !oidcmp(oid, &update->old_oid)) |
e3f51039 MH |
2709 | return 0; |
2710 | ||
98491298 | 2711 | if (is_null_oid(&update->old_oid)) |
e3f51039 MH |
2712 | strbuf_addf(err, "cannot lock ref '%s': " |
2713 | "reference already exists", | |
2714 | original_update_refname(update)); | |
2715 | else if (is_null_oid(oid)) | |
2716 | strbuf_addf(err, "cannot lock ref '%s': " | |
2717 | "reference is missing but expected %s", | |
2718 | original_update_refname(update), | |
98491298 | 2719 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2720 | else |
2721 | strbuf_addf(err, "cannot lock ref '%s': " | |
2722 | "is at %s but expected %s", | |
2723 | original_update_refname(update), | |
2724 | oid_to_hex(oid), | |
98491298 | 2725 | oid_to_hex(&update->old_oid)); |
e3f51039 MH |
2726 | |
2727 | return -1; | |
2728 | } | |
2729 | ||
92b1551b MH |
2730 | /* |
2731 | * Prepare for carrying out update: | |
2732 | * - Lock the reference referred to by update. | |
2733 | * - Read the reference under lock. | |
2734 | * - Check that its old SHA-1 value (if specified) is correct, and in | |
2735 | * any case record it in update->lock->old_oid for later use when | |
2736 | * writing the reflog. | |
2737 | * - If it is a symref update without REF_NODEREF, split it up into a | |
2738 | * REF_LOG_ONLY update of the symref and add a separate update for | |
2739 | * the referent to transaction. | |
2740 | * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY | |
2741 | * update of HEAD. | |
165056b2 | 2742 | */ |
b3bbbc5c MH |
2743 | static int lock_ref_for_update(struct files_ref_store *refs, |
2744 | struct ref_update *update, | |
165056b2 | 2745 | struct ref_transaction *transaction, |
92b1551b | 2746 | const char *head_ref, |
165056b2 MH |
2747 | struct string_list *affected_refnames, |
2748 | struct strbuf *err) | |
2749 | { | |
92b1551b MH |
2750 | struct strbuf referent = STRBUF_INIT; |
2751 | int mustexist = (update->flags & REF_HAVE_OLD) && | |
98491298 | 2752 | !is_null_oid(&update->old_oid); |
165056b2 | 2753 | int ret; |
92b1551b | 2754 | struct ref_lock *lock; |
165056b2 | 2755 | |
32c597e7 | 2756 | files_assert_main_repository(refs, "lock_ref_for_update"); |
b3bbbc5c | 2757 | |
98491298 | 2758 | if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid)) |
165056b2 | 2759 | update->flags |= REF_DELETING; |
92b1551b MH |
2760 | |
2761 | if (head_ref) { | |
2762 | ret = split_head_update(update, transaction, head_ref, | |
2763 | affected_refnames, err); | |
2764 | if (ret) | |
2765 | return ret; | |
2766 | } | |
2767 | ||
f7b0a987 | 2768 | ret = lock_raw_ref(refs, update->refname, mustexist, |
92b1551b | 2769 | affected_refnames, NULL, |
7d618264 | 2770 | &lock, &referent, |
92b1551b | 2771 | &update->type, err); |
92b1551b | 2772 | if (ret) { |
165056b2 MH |
2773 | char *reason; |
2774 | ||
165056b2 MH |
2775 | reason = strbuf_detach(err, NULL); |
2776 | strbuf_addf(err, "cannot lock ref '%s': %s", | |
e3f51039 | 2777 | original_update_refname(update), reason); |
165056b2 MH |
2778 | free(reason); |
2779 | return ret; | |
2780 | } | |
92b1551b | 2781 | |
7d618264 | 2782 | update->backend_data = lock; |
92b1551b | 2783 | |
8169d0d0 | 2784 | if (update->type & REF_ISSYMREF) { |
6e30b2f6 MH |
2785 | if (update->flags & REF_NODEREF) { |
2786 | /* | |
2787 | * We won't be reading the referent as part of | |
2788 | * the transaction, so we have to read it here | |
2789 | * to record and possibly check old_sha1: | |
2790 | */ | |
2f40e954 NTND |
2791 | if (refs_read_ref_full(&refs->base, |
2792 | referent.buf, 0, | |
2793 | lock->old_oid.hash, NULL)) { | |
6e30b2f6 MH |
2794 | if (update->flags & REF_HAVE_OLD) { |
2795 | strbuf_addf(err, "cannot lock ref '%s': " | |
e3f51039 MH |
2796 | "error reading reference", |
2797 | original_update_refname(update)); | |
2798 | return -1; | |
6e30b2f6 | 2799 | } |
e3f51039 | 2800 | } else if (check_old_oid(update, &lock->old_oid, err)) { |
8169d0d0 | 2801 | return TRANSACTION_GENERIC_ERROR; |
8169d0d0 | 2802 | } |
6e30b2f6 MH |
2803 | } else { |
2804 | /* | |
2805 | * Create a new update for the reference this | |
2806 | * symref is pointing at. Also, we will record | |
2807 | * and verify old_sha1 for this update as part | |
2808 | * of processing the split-off update, so we | |
2809 | * don't have to do it here. | |
2810 | */ | |
fcc42ea0 MH |
2811 | ret = split_symref_update(refs, update, |
2812 | referent.buf, transaction, | |
92b1551b MH |
2813 | affected_refnames, err); |
2814 | if (ret) | |
2815 | return ret; | |
2816 | } | |
6e30b2f6 MH |
2817 | } else { |
2818 | struct ref_update *parent_update; | |
8169d0d0 | 2819 | |
e3f51039 MH |
2820 | if (check_old_oid(update, &lock->old_oid, err)) |
2821 | return TRANSACTION_GENERIC_ERROR; | |
2822 | ||
6e30b2f6 MH |
2823 | /* |
2824 | * If this update is happening indirectly because of a | |
2825 | * symref update, record the old SHA-1 in the parent | |
2826 | * update: | |
2827 | */ | |
2828 | for (parent_update = update->parent_update; | |
2829 | parent_update; | |
2830 | parent_update = parent_update->parent_update) { | |
7d618264 DT |
2831 | struct ref_lock *parent_lock = parent_update->backend_data; |
2832 | oidcpy(&parent_lock->old_oid, &lock->old_oid); | |
6e30b2f6 | 2833 | } |
92b1551b MH |
2834 | } |
2835 | ||
165056b2 MH |
2836 | if ((update->flags & REF_HAVE_NEW) && |
2837 | !(update->flags & REF_DELETING) && | |
2838 | !(update->flags & REF_LOG_ONLY)) { | |
92b1551b | 2839 | if (!(update->type & REF_ISSYMREF) && |
98491298 | 2840 | !oidcmp(&lock->old_oid, &update->new_oid)) { |
165056b2 MH |
2841 | /* |
2842 | * The reference already has the desired | |
2843 | * value, so we don't need to write it. | |
2844 | */ | |
4417df8c | 2845 | } else if (write_ref_to_lockfile(lock, &update->new_oid, |
165056b2 MH |
2846 | err)) { |
2847 | char *write_err = strbuf_detach(err, NULL); | |
2848 | ||
2849 | /* | |
2850 | * The lock was freed upon failure of | |
2851 | * write_ref_to_lockfile(): | |
2852 | */ | |
7d618264 | 2853 | update->backend_data = NULL; |
165056b2 | 2854 | strbuf_addf(err, |
e3f51039 | 2855 | "cannot update ref '%s': %s", |
165056b2 MH |
2856 | update->refname, write_err); |
2857 | free(write_err); | |
2858 | return TRANSACTION_GENERIC_ERROR; | |
2859 | } else { | |
2860 | update->flags |= REF_NEEDS_COMMIT; | |
2861 | } | |
2862 | } | |
2863 | if (!(update->flags & REF_NEEDS_COMMIT)) { | |
2864 | /* | |
2865 | * We didn't call write_ref_to_lockfile(), so | |
2866 | * the lockfile is still open. Close it to | |
2867 | * free up the file descriptor: | |
2868 | */ | |
92b1551b | 2869 | if (close_ref(lock)) { |
165056b2 MH |
2870 | strbuf_addf(err, "couldn't close '%s.lock'", |
2871 | update->refname); | |
2872 | return TRANSACTION_GENERIC_ERROR; | |
2873 | } | |
2874 | } | |
2875 | return 0; | |
2876 | } | |
2877 | ||
c0ca9357 MH |
2878 | /* |
2879 | * Unlock any references in `transaction` that are still locked, and | |
2880 | * mark the transaction closed. | |
2881 | */ | |
2882 | static void files_transaction_cleanup(struct ref_transaction *transaction) | |
2883 | { | |
2884 | size_t i; | |
2885 | ||
2886 | for (i = 0; i < transaction->nr; i++) { | |
2887 | struct ref_update *update = transaction->updates[i]; | |
2888 | struct ref_lock *lock = update->backend_data; | |
2889 | ||
2890 | if (lock) { | |
2891 | unlock_ref(lock); | |
2892 | update->backend_data = NULL; | |
2893 | } | |
2894 | } | |
2895 | ||
2896 | transaction->state = REF_TRANSACTION_CLOSED; | |
2897 | } | |
2898 | ||
30173b88 MH |
2899 | static int files_transaction_prepare(struct ref_store *ref_store, |
2900 | struct ref_transaction *transaction, | |
2901 | struct strbuf *err) | |
7bd9bcf3 | 2902 | { |
00eebe35 | 2903 | struct files_ref_store *refs = |
9e7ec634 | 2904 | files_downcast(ref_store, REF_STORE_WRITE, |
30173b88 | 2905 | "ref_transaction_prepare"); |
43a2dfde MH |
2906 | size_t i; |
2907 | int ret = 0; | |
7bd9bcf3 | 2908 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
92b1551b MH |
2909 | char *head_ref = NULL; |
2910 | int head_type; | |
2911 | struct object_id head_oid; | |
7bd9bcf3 MH |
2912 | |
2913 | assert(err); | |
2914 | ||
c0ca9357 MH |
2915 | if (!transaction->nr) |
2916 | goto cleanup; | |
7bd9bcf3 | 2917 | |
92b1551b MH |
2918 | /* |
2919 | * Fail if a refname appears more than once in the | |
2920 | * transaction. (If we end up splitting up any updates using | |
2921 | * split_symref_update() or split_head_update(), those | |
2922 | * functions will check that the new updates don't have the | |
2923 | * same refname as any existing ones.) | |
2924 | */ | |
2925 | for (i = 0; i < transaction->nr; i++) { | |
2926 | struct ref_update *update = transaction->updates[i]; | |
2927 | struct string_list_item *item = | |
2928 | string_list_append(&affected_refnames, update->refname); | |
2929 | ||
2930 | /* | |
2931 | * We store a pointer to update in item->util, but at | |
2932 | * the moment we never use the value of this field | |
2933 | * except to check whether it is non-NULL. | |
2934 | */ | |
2935 | item->util = update; | |
2936 | } | |
7bd9bcf3 MH |
2937 | string_list_sort(&affected_refnames); |
2938 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
2939 | ret = TRANSACTION_GENERIC_ERROR; | |
2940 | goto cleanup; | |
2941 | } | |
2942 | ||
92b1551b MH |
2943 | /* |
2944 | * Special hack: If a branch is updated directly and HEAD | |
2945 | * points to it (may happen on the remote side of a push | |
2946 | * for example) then logically the HEAD reflog should be | |
2947 | * updated too. | |
2948 | * | |
2949 | * A generic solution would require reverse symref lookups, | |
2950 | * but finding all symrefs pointing to a given branch would be | |
2951 | * rather costly for this rare event (the direct update of a | |
2952 | * branch) to be worth it. So let's cheat and check with HEAD | |
2953 | * only, which should cover 99% of all usage scenarios (even | |
2954 | * 100% of the default ones). | |
2955 | * | |
2956 | * So if HEAD is a symbolic reference, then record the name of | |
2957 | * the reference that it points to. If we see an update of | |
2958 | * head_ref within the transaction, then split_head_update() | |
2959 | * arranges for the reflog of HEAD to be updated, too. | |
2960 | */ | |
2f40e954 NTND |
2961 | head_ref = refs_resolve_refdup(ref_store, "HEAD", |
2962 | RESOLVE_REF_NO_RECURSE, | |
2963 | head_oid.hash, &head_type); | |
92b1551b MH |
2964 | |
2965 | if (head_ref && !(head_type & REF_ISSYMREF)) { | |
6a83d902 | 2966 | FREE_AND_NULL(head_ref); |
92b1551b MH |
2967 | } |
2968 | ||
7bd9bcf3 MH |
2969 | /* |
2970 | * Acquire all locks, verify old values if provided, check | |
2971 | * that new values are valid, and write new values to the | |
2972 | * lockfiles, ready to be activated. Only keep one lockfile | |
2973 | * open at a time to avoid running out of file descriptors. | |
30173b88 MH |
2974 | * Note that lock_ref_for_update() might append more updates |
2975 | * to the transaction. | |
7bd9bcf3 | 2976 | */ |
efe47281 MH |
2977 | for (i = 0; i < transaction->nr; i++) { |
2978 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 | 2979 | |
b3bbbc5c MH |
2980 | ret = lock_ref_for_update(refs, update, transaction, |
2981 | head_ref, &affected_refnames, err); | |
165056b2 | 2982 | if (ret) |
30173b88 MH |
2983 | break; |
2984 | } | |
2985 | ||
2986 | cleanup: | |
2987 | free(head_ref); | |
2988 | string_list_clear(&affected_refnames, 0); | |
2989 | ||
2990 | if (ret) | |
2991 | files_transaction_cleanup(transaction); | |
2992 | else | |
2993 | transaction->state = REF_TRANSACTION_PREPARED; | |
2994 | ||
2995 | return ret; | |
2996 | } | |
2997 | ||
2998 | static int files_transaction_finish(struct ref_store *ref_store, | |
2999 | struct ref_transaction *transaction, | |
3000 | struct strbuf *err) | |
3001 | { | |
3002 | struct files_ref_store *refs = | |
3003 | files_downcast(ref_store, 0, "ref_transaction_finish"); | |
3004 | size_t i; | |
3005 | int ret = 0; | |
3006 | struct string_list refs_to_delete = STRING_LIST_INIT_NODUP; | |
3007 | struct string_list_item *ref_to_delete; | |
3008 | struct strbuf sb = STRBUF_INIT; | |
3009 | ||
3010 | assert(err); | |
3011 | ||
3012 | if (!transaction->nr) { | |
3013 | transaction->state = REF_TRANSACTION_CLOSED; | |
3014 | return 0; | |
7bd9bcf3 | 3015 | } |
7bd9bcf3 | 3016 | |
7bd9bcf3 | 3017 | /* Perform updates first so live commits remain referenced */ |
efe47281 MH |
3018 | for (i = 0; i < transaction->nr; i++) { |
3019 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 3020 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 3021 | |
d99aa884 DT |
3022 | if (update->flags & REF_NEEDS_COMMIT || |
3023 | update->flags & REF_LOG_ONLY) { | |
802de3da NTND |
3024 | if (files_log_ref_write(refs, |
3025 | lock->ref_name, | |
4417df8c | 3026 | &lock->old_oid, |
3027 | &update->new_oid, | |
81b1b6d4 MH |
3028 | update->msg, update->flags, |
3029 | err)) { | |
92b1551b MH |
3030 | char *old_msg = strbuf_detach(err, NULL); |
3031 | ||
3032 | strbuf_addf(err, "cannot update the ref '%s': %s", | |
3033 | lock->ref_name, old_msg); | |
3034 | free(old_msg); | |
3035 | unlock_ref(lock); | |
7d618264 | 3036 | update->backend_data = NULL; |
7bd9bcf3 MH |
3037 | ret = TRANSACTION_GENERIC_ERROR; |
3038 | goto cleanup; | |
7bd9bcf3 MH |
3039 | } |
3040 | } | |
7bd9bcf3 | 3041 | if (update->flags & REF_NEEDS_COMMIT) { |
00eebe35 | 3042 | clear_loose_ref_cache(refs); |
92b1551b MH |
3043 | if (commit_ref(lock)) { |
3044 | strbuf_addf(err, "couldn't set '%s'", lock->ref_name); | |
3045 | unlock_ref(lock); | |
7d618264 | 3046 | update->backend_data = NULL; |
7bd9bcf3 MH |
3047 | ret = TRANSACTION_GENERIC_ERROR; |
3048 | goto cleanup; | |
7bd9bcf3 MH |
3049 | } |
3050 | } | |
3051 | } | |
7bd9bcf3 | 3052 | /* Perform deletes now that updates are safely completed */ |
efe47281 MH |
3053 | for (i = 0; i < transaction->nr; i++) { |
3054 | struct ref_update *update = transaction->updates[i]; | |
7d618264 | 3055 | struct ref_lock *lock = update->backend_data; |
7bd9bcf3 | 3056 | |
d99aa884 DT |
3057 | if (update->flags & REF_DELETING && |
3058 | !(update->flags & REF_LOG_ONLY)) { | |
ce0af24d MH |
3059 | if (!(update->type & REF_ISPACKED) || |
3060 | update->type & REF_ISSYMREF) { | |
3061 | /* It is a loose reference. */ | |
e9dcc305 | 3062 | strbuf_reset(&sb); |
19e02f4f | 3063 | files_ref_path(refs, &sb, lock->ref_name); |
e9dcc305 | 3064 | if (unlink_or_msg(sb.buf, err)) { |
ce0af24d MH |
3065 | ret = TRANSACTION_GENERIC_ERROR; |
3066 | goto cleanup; | |
3067 | } | |
44639777 | 3068 | update->flags |= REF_DELETED_LOOSE; |
7bd9bcf3 MH |
3069 | } |
3070 | ||
3071 | if (!(update->flags & REF_ISPRUNING)) | |
3072 | string_list_append(&refs_to_delete, | |
7d618264 | 3073 | lock->ref_name); |
7bd9bcf3 MH |
3074 | } |
3075 | } | |
3076 | ||
0a95ac5f | 3077 | if (repack_without_refs(refs, &refs_to_delete, err)) { |
7bd9bcf3 MH |
3078 | ret = TRANSACTION_GENERIC_ERROR; |
3079 | goto cleanup; | |
3080 | } | |
44639777 MH |
3081 | |
3082 | /* Delete the reflogs of any references that were deleted: */ | |
3083 | for_each_string_list_item(ref_to_delete, &refs_to_delete) { | |
e9dcc305 | 3084 | strbuf_reset(&sb); |
802de3da | 3085 | files_reflog_path(refs, &sb, ref_to_delete->string); |
e9dcc305 | 3086 | if (!unlink_or_warn(sb.buf)) |
802de3da | 3087 | try_remove_empty_parents(refs, ref_to_delete->string, |
44639777 MH |
3088 | REMOVE_EMPTY_PARENTS_REFLOG); |
3089 | } | |
3090 | ||
00eebe35 | 3091 | clear_loose_ref_cache(refs); |
7bd9bcf3 MH |
3092 | |
3093 | cleanup: | |
c0ca9357 | 3094 | files_transaction_cleanup(transaction); |
7bd9bcf3 | 3095 | |
44639777 MH |
3096 | for (i = 0; i < transaction->nr; i++) { |
3097 | struct ref_update *update = transaction->updates[i]; | |
44639777 MH |
3098 | |
3099 | if (update->flags & REF_DELETED_LOOSE) { | |
3100 | /* | |
3101 | * The loose reference was deleted. Delete any | |
3102 | * empty parent directories. (Note that this | |
3103 | * can only work because we have already | |
3104 | * removed the lockfile.) | |
3105 | */ | |
802de3da | 3106 | try_remove_empty_parents(refs, update->refname, |
44639777 MH |
3107 | REMOVE_EMPTY_PARENTS_REF); |
3108 | } | |
3109 | } | |
3110 | ||
30173b88 | 3111 | strbuf_release(&sb); |
7bd9bcf3 | 3112 | string_list_clear(&refs_to_delete, 0); |
7bd9bcf3 MH |
3113 | return ret; |
3114 | } | |
3115 | ||
30173b88 MH |
3116 | static int files_transaction_abort(struct ref_store *ref_store, |
3117 | struct ref_transaction *transaction, | |
3118 | struct strbuf *err) | |
3119 | { | |
3120 | files_transaction_cleanup(transaction); | |
3121 | return 0; | |
3122 | } | |
3123 | ||
7bd9bcf3 MH |
3124 | static int ref_present(const char *refname, |
3125 | const struct object_id *oid, int flags, void *cb_data) | |
3126 | { | |
3127 | struct string_list *affected_refnames = cb_data; | |
3128 | ||
3129 | return string_list_has_string(affected_refnames, refname); | |
3130 | } | |
3131 | ||
fc681463 DT |
3132 | static int files_initial_transaction_commit(struct ref_store *ref_store, |
3133 | struct ref_transaction *transaction, | |
3134 | struct strbuf *err) | |
7bd9bcf3 | 3135 | { |
d99825ab | 3136 | struct files_ref_store *refs = |
9e7ec634 NTND |
3137 | files_downcast(ref_store, REF_STORE_WRITE, |
3138 | "initial_ref_transaction_commit"); | |
43a2dfde MH |
3139 | size_t i; |
3140 | int ret = 0; | |
7bd9bcf3 MH |
3141 | struct string_list affected_refnames = STRING_LIST_INIT_NODUP; |
3142 | ||
3143 | assert(err); | |
3144 | ||
3145 | if (transaction->state != REF_TRANSACTION_OPEN) | |
3146 | die("BUG: commit called for transaction that is not open"); | |
3147 | ||
3148 | /* Fail if a refname appears more than once in the transaction: */ | |
efe47281 MH |
3149 | for (i = 0; i < transaction->nr; i++) |
3150 | string_list_append(&affected_refnames, | |
3151 | transaction->updates[i]->refname); | |
7bd9bcf3 MH |
3152 | string_list_sort(&affected_refnames); |
3153 | if (ref_update_reject_duplicates(&affected_refnames, err)) { | |
3154 | ret = TRANSACTION_GENERIC_ERROR; | |
3155 | goto cleanup; | |
3156 | } | |
3157 | ||
3158 | /* | |
3159 | * It's really undefined to call this function in an active | |
3160 | * repository or when there are existing references: we are | |
3161 | * only locking and changing packed-refs, so (1) any | |
3162 | * simultaneous processes might try to change a reference at | |
3163 | * the same time we do, and (2) any existing loose versions of | |
3164 | * the references that we are setting would have precedence | |
3165 | * over our values. But some remote helpers create the remote | |
3166 | * "HEAD" and "master" branches before calling this function, | |
3167 | * so here we really only check that none of the references | |
3168 | * that we are creating already exists. | |
3169 | */ | |
2f40e954 NTND |
3170 | if (refs_for_each_rawref(&refs->base, ref_present, |
3171 | &affected_refnames)) | |
7bd9bcf3 MH |
3172 | die("BUG: initial ref transaction called with existing refs"); |
3173 | ||
efe47281 MH |
3174 | for (i = 0; i < transaction->nr; i++) { |
3175 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 MH |
3176 | |
3177 | if ((update->flags & REF_HAVE_OLD) && | |
98491298 | 3178 | !is_null_oid(&update->old_oid)) |
7bd9bcf3 | 3179 | die("BUG: initial ref transaction with old_sha1 set"); |
7d2df051 NTND |
3180 | if (refs_verify_refname_available(&refs->base, update->refname, |
3181 | &affected_refnames, NULL, | |
3182 | err)) { | |
7bd9bcf3 MH |
3183 | ret = TRANSACTION_NAME_CONFLICT; |
3184 | goto cleanup; | |
3185 | } | |
3186 | } | |
3187 | ||
49c0df6a | 3188 | if (lock_packed_refs(refs, 0)) { |
7bd9bcf3 MH |
3189 | strbuf_addf(err, "unable to lock packed-refs file: %s", |
3190 | strerror(errno)); | |
3191 | ret = TRANSACTION_GENERIC_ERROR; | |
3192 | goto cleanup; | |
3193 | } | |
3194 | ||
efe47281 MH |
3195 | for (i = 0; i < transaction->nr; i++) { |
3196 | struct ref_update *update = transaction->updates[i]; | |
7bd9bcf3 MH |
3197 | |
3198 | if ((update->flags & REF_HAVE_NEW) && | |
98491298 | 3199 | !is_null_oid(&update->new_oid)) |
3200 | add_packed_ref(refs, update->refname, | |
4417df8c | 3201 | &update->new_oid); |
7bd9bcf3 MH |
3202 | } |
3203 | ||
49c0df6a | 3204 | if (commit_packed_refs(refs)) { |
7bd9bcf3 MH |
3205 | strbuf_addf(err, "unable to commit packed-refs file: %s", |
3206 | strerror(errno)); | |
3207 | ret = TRANSACTION_GENERIC_ERROR; | |
3208 | goto cleanup; | |
3209 | } | |
3210 | ||
3211 | cleanup: | |
3212 | transaction->state = REF_TRANSACTION_CLOSED; | |
3213 | string_list_clear(&affected_refnames, 0); | |
3214 | return ret; | |
3215 | } | |
3216 | ||
3217 | struct expire_reflog_cb { | |
3218 | unsigned int flags; | |
3219 | reflog_expiry_should_prune_fn *should_prune_fn; | |
3220 | void *policy_cb; | |
3221 | FILE *newlog; | |
9461d272 | 3222 | struct object_id last_kept_oid; |
7bd9bcf3 MH |
3223 | }; |
3224 | ||
9461d272 | 3225 | static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 3226 | const char *email, timestamp_t timestamp, int tz, |
7bd9bcf3 MH |
3227 | const char *message, void *cb_data) |
3228 | { | |
3229 | struct expire_reflog_cb *cb = cb_data; | |
3230 | struct expire_reflog_policy_cb *policy_cb = cb->policy_cb; | |
3231 | ||
3232 | if (cb->flags & EXPIRE_REFLOGS_REWRITE) | |
9461d272 | 3233 | ooid = &cb->last_kept_oid; |
7bd9bcf3 | 3234 | |
4322478a | 3235 | if ((*cb->should_prune_fn)(ooid, noid, email, timestamp, tz, |
7bd9bcf3 MH |
3236 | message, policy_cb)) { |
3237 | if (!cb->newlog) | |
3238 | printf("would prune %s", message); | |
3239 | else if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
3240 | printf("prune %s", message); | |
3241 | } else { | |
3242 | if (cb->newlog) { | |
cb71f8bd | 3243 | fprintf(cb->newlog, "%s %s %s %"PRItime" %+05d\t%s", |
9461d272 | 3244 | oid_to_hex(ooid), oid_to_hex(noid), |
7bd9bcf3 | 3245 | email, timestamp, tz, message); |
9461d272 | 3246 | oidcpy(&cb->last_kept_oid, noid); |
7bd9bcf3 MH |
3247 | } |
3248 | if (cb->flags & EXPIRE_REFLOGS_VERBOSE) | |
3249 | printf("keep %s", message); | |
3250 | } | |
3251 | return 0; | |
3252 | } | |
3253 | ||
e3688bd6 DT |
3254 | static int files_reflog_expire(struct ref_store *ref_store, |
3255 | const char *refname, const unsigned char *sha1, | |
3256 | unsigned int flags, | |
3257 | reflog_expiry_prepare_fn prepare_fn, | |
3258 | reflog_expiry_should_prune_fn should_prune_fn, | |
3259 | reflog_expiry_cleanup_fn cleanup_fn, | |
3260 | void *policy_cb_data) | |
7bd9bcf3 | 3261 | { |
7eb27cdf | 3262 | struct files_ref_store *refs = |
9e7ec634 | 3263 | files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); |
7bd9bcf3 MH |
3264 | static struct lock_file reflog_lock; |
3265 | struct expire_reflog_cb cb; | |
3266 | struct ref_lock *lock; | |
802de3da | 3267 | struct strbuf log_file_sb = STRBUF_INIT; |
7bd9bcf3 MH |
3268 | char *log_file; |
3269 | int status = 0; | |
3270 | int type; | |
3271 | struct strbuf err = STRBUF_INIT; | |
4322478a | 3272 | struct object_id oid; |
7bd9bcf3 MH |
3273 | |
3274 | memset(&cb, 0, sizeof(cb)); | |
3275 | cb.flags = flags; | |
3276 | cb.policy_cb = policy_cb_data; | |
3277 | cb.should_prune_fn = should_prune_fn; | |
3278 | ||
3279 | /* | |
3280 | * The reflog file is locked by holding the lock on the | |
3281 | * reference itself, plus we might need to update the | |
3282 | * reference if --updateref was specified: | |
3283 | */ | |
7eb27cdf MH |
3284 | lock = lock_ref_sha1_basic(refs, refname, sha1, |
3285 | NULL, NULL, REF_NODEREF, | |
41d796ed | 3286 | &type, &err); |
7bd9bcf3 MH |
3287 | if (!lock) { |
3288 | error("cannot lock ref '%s': %s", refname, err.buf); | |
3289 | strbuf_release(&err); | |
3290 | return -1; | |
3291 | } | |
2f40e954 | 3292 | if (!refs_reflog_exists(ref_store, refname)) { |
7bd9bcf3 MH |
3293 | unlock_ref(lock); |
3294 | return 0; | |
3295 | } | |
3296 | ||
802de3da NTND |
3297 | files_reflog_path(refs, &log_file_sb, refname); |
3298 | log_file = strbuf_detach(&log_file_sb, NULL); | |
7bd9bcf3 MH |
3299 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { |
3300 | /* | |
3301 | * Even though holding $GIT_DIR/logs/$reflog.lock has | |
3302 | * no locking implications, we use the lock_file | |
3303 | * machinery here anyway because it does a lot of the | |
3304 | * work we need, including cleaning up if the program | |
3305 | * exits unexpectedly. | |
3306 | */ | |
3307 | if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) { | |
3308 | struct strbuf err = STRBUF_INIT; | |
3309 | unable_to_lock_message(log_file, errno, &err); | |
3310 | error("%s", err.buf); | |
3311 | strbuf_release(&err); | |
3312 | goto failure; | |
3313 | } | |
3314 | cb.newlog = fdopen_lock_file(&reflog_lock, "w"); | |
3315 | if (!cb.newlog) { | |
3316 | error("cannot fdopen %s (%s)", | |
3317 | get_lock_file_path(&reflog_lock), strerror(errno)); | |
3318 | goto failure; | |
3319 | } | |
3320 | } | |
3321 | ||
4322478a | 3322 | hashcpy(oid.hash, sha1); |
3323 | ||
3324 | (*prepare_fn)(refname, &oid, cb.policy_cb); | |
2f40e954 | 3325 | refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb); |
7bd9bcf3 MH |
3326 | (*cleanup_fn)(cb.policy_cb); |
3327 | ||
3328 | if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) { | |
3329 | /* | |
3330 | * It doesn't make sense to adjust a reference pointed | |
3331 | * to by a symbolic ref based on expiring entries in | |
3332 | * the symbolic reference's reflog. Nor can we update | |
3333 | * a reference if there are no remaining reflog | |
3334 | * entries. | |
3335 | */ | |
3336 | int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) && | |
3337 | !(type & REF_ISSYMREF) && | |
9461d272 | 3338 | !is_null_oid(&cb.last_kept_oid); |
7bd9bcf3 MH |
3339 | |
3340 | if (close_lock_file(&reflog_lock)) { | |
3341 | status |= error("couldn't write %s: %s", log_file, | |
3342 | strerror(errno)); | |
3343 | } else if (update && | |
3344 | (write_in_full(get_lock_file_fd(lock->lk), | |
9461d272 | 3345 | oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ || |
7bd9bcf3 MH |
3346 | write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 || |
3347 | close_ref(lock) < 0)) { | |
3348 | status |= error("couldn't write %s", | |
3349 | get_lock_file_path(lock->lk)); | |
3350 | rollback_lock_file(&reflog_lock); | |
3351 | } else if (commit_lock_file(&reflog_lock)) { | |
e0048d3e | 3352 | status |= error("unable to write reflog '%s' (%s)", |
7bd9bcf3 MH |
3353 | log_file, strerror(errno)); |
3354 | } else if (update && commit_ref(lock)) { | |
3355 | status |= error("couldn't set %s", lock->ref_name); | |
3356 | } | |
3357 | } | |
3358 | free(log_file); | |
3359 | unlock_ref(lock); | |
3360 | return status; | |
3361 | ||
3362 | failure: | |
3363 | rollback_lock_file(&reflog_lock); | |
3364 | free(log_file); | |
3365 | unlock_ref(lock); | |
3366 | return -1; | |
3367 | } | |
3dce444f | 3368 | |
6fb5acfd DT |
3369 | static int files_init_db(struct ref_store *ref_store, struct strbuf *err) |
3370 | { | |
19e02f4f | 3371 | struct files_ref_store *refs = |
9e7ec634 | 3372 | files_downcast(ref_store, REF_STORE_WRITE, "init_db"); |
e9dcc305 NTND |
3373 | struct strbuf sb = STRBUF_INIT; |
3374 | ||
6fb5acfd DT |
3375 | /* |
3376 | * Create .git/refs/{heads,tags} | |
3377 | */ | |
19e02f4f | 3378 | files_ref_path(refs, &sb, "refs/heads"); |
e9dcc305 NTND |
3379 | safe_create_dir(sb.buf, 1); |
3380 | ||
3381 | strbuf_reset(&sb); | |
19e02f4f | 3382 | files_ref_path(refs, &sb, "refs/tags"); |
e9dcc305 NTND |
3383 | safe_create_dir(sb.buf, 1); |
3384 | ||
3385 | strbuf_release(&sb); | |
6fb5acfd DT |
3386 | return 0; |
3387 | } | |
3388 | ||
3dce444f RS |
3389 | struct ref_storage_be refs_be_files = { |
3390 | NULL, | |
00eebe35 | 3391 | "files", |
127b42a1 | 3392 | files_ref_store_create, |
6fb5acfd | 3393 | files_init_db, |
30173b88 MH |
3394 | files_transaction_prepare, |
3395 | files_transaction_finish, | |
3396 | files_transaction_abort, | |
fc681463 | 3397 | files_initial_transaction_commit, |
e1e33b72 | 3398 | |
8231527e | 3399 | files_pack_refs, |
bd427cf2 | 3400 | files_peel_ref, |
284689ba | 3401 | files_create_symref, |
a27dcf89 | 3402 | files_delete_refs, |
9b6b40d9 | 3403 | files_rename_ref, |
8231527e | 3404 | |
1a769003 | 3405 | files_ref_iterator_begin, |
62665823 | 3406 | files_read_raw_ref, |
e3688bd6 DT |
3407 | |
3408 | files_reflog_iterator_begin, | |
3409 | files_for_each_reflog_ent, | |
3410 | files_for_each_reflog_ent_reverse, | |
3411 | files_reflog_exists, | |
3412 | files_create_reflog, | |
3413 | files_delete_reflog, | |
3414 | files_reflog_expire | |
3dce444f | 3415 | }; |