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