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