ref_resolves_to_object(): new function
[git/git.git] / refs / files-backend.c
CommitLineData
7bd9bcf3
MH
1#include "../cache.h"
2#include "../refs.h"
3#include "refs-internal.h"
4#include "../lockfile.h"
5#include "../object.h"
6#include "../dir.h"
7
8struct ref_lock {
9 char *ref_name;
7bd9bcf3
MH
10 struct lock_file *lk;
11 struct object_id old_oid;
12};
13
14struct ref_entry;
15
16/*
17 * Information used (along with the information in ref_entry) to
18 * describe a single cached reference. This data structure only
19 * occurs embedded in a union in struct ref_entry, and only when
20 * (ref_entry->flag & REF_DIR) is zero.
21 */
22struct ref_value {
23 /*
24 * The name of the object to which this reference resolves
25 * (which may be a tag object). If REF_ISBROKEN, this is
26 * null. If REF_ISSYMREF, then this is the name of the object
27 * referred to by the last reference in the symlink chain.
28 */
29 struct object_id oid;
30
31 /*
32 * If REF_KNOWS_PEELED, then this field holds the peeled value
33 * of this reference, or null if the reference is known not to
34 * be peelable. See the documentation for peel_ref() for an
35 * exact definition of "peelable".
36 */
37 struct object_id peeled;
38};
39
40struct ref_cache;
41
42/*
43 * Information used (along with the information in ref_entry) to
44 * describe a level in the hierarchy of references. This data
45 * structure only occurs embedded in a union in struct ref_entry, and
46 * only when (ref_entry.flag & REF_DIR) is set. In that case,
47 * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
48 * in the directory have already been read:
49 *
50 * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
51 * or packed references, already read.
52 *
53 * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
54 * references that hasn't been read yet (nor has any of its
55 * subdirectories).
56 *
57 * Entries within a directory are stored within a growable array of
58 * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
59 * sorted are sorted by their component name in strcmp() order and the
60 * remaining entries are unsorted.
61 *
62 * Loose references are read lazily, one directory at a time. When a
63 * directory of loose references is read, then all of the references
64 * in that directory are stored, and REF_INCOMPLETE stubs are created
65 * for any subdirectories, but the subdirectories themselves are not
66 * read. The reading is triggered by get_ref_dir().
67 */
68struct ref_dir {
69 int nr, alloc;
70
71 /*
72 * Entries with index 0 <= i < sorted are sorted by name. New
73 * entries are appended to the list unsorted, and are sorted
74 * only when required; thus we avoid the need to sort the list
75 * after the addition of every reference.
76 */
77 int sorted;
78
79 /* A pointer to the ref_cache that contains this ref_dir. */
80 struct ref_cache *ref_cache;
81
82 struct ref_entry **entries;
83};
84
85/*
86 * Bit values for ref_entry::flag. REF_ISSYMREF=0x01,
87 * REF_ISPACKED=0x02, REF_ISBROKEN=0x04 and REF_BAD_NAME=0x08 are
88 * public values; see refs.h.
89 */
90
91/*
92 * The field ref_entry->u.value.peeled of this value entry contains
93 * the correct peeled value for the reference, which might be
94 * null_sha1 if the reference is not a tag or if it is broken.
95 */
96#define REF_KNOWS_PEELED 0x10
97
98/* ref_entry represents a directory of references */
99#define REF_DIR 0x20
100
101/*
102 * Entry has not yet been read from disk (used only for REF_DIR
103 * entries representing loose references)
104 */
105#define REF_INCOMPLETE 0x40
106
107/*
108 * A ref_entry represents either a reference or a "subdirectory" of
109 * references.
110 *
111 * Each directory in the reference namespace is represented by a
112 * ref_entry with (flags & REF_DIR) set and containing a subdir member
113 * that holds the entries in that directory that have been read so
114 * far. If (flags & REF_INCOMPLETE) is set, then the directory and
115 * its subdirectories haven't been read yet. REF_INCOMPLETE is only
116 * used for loose reference directories.
117 *
118 * References are represented by a ref_entry with (flags & REF_DIR)
119 * unset and a value member that describes the reference's value. The
120 * flag member is at the ref_entry level, but it is also needed to
121 * interpret the contents of the value field (in other words, a
122 * ref_value object is not very much use without the enclosing
123 * ref_entry).
124 *
125 * Reference names cannot end with slash and directories' names are
126 * always stored with a trailing slash (except for the top-level
127 * directory, which is always denoted by ""). This has two nice
128 * consequences: (1) when the entries in each subdir are sorted
129 * lexicographically by name (as they usually are), the references in
130 * a whole tree can be generated in lexicographic order by traversing
131 * the tree in left-to-right, depth-first order; (2) the names of
132 * references and subdirectories cannot conflict, and therefore the
133 * presence of an empty subdirectory does not block the creation of a
134 * similarly-named reference. (The fact that reference names with the
135 * same leading components can conflict *with each other* is a
136 * separate issue that is regulated by verify_refname_available().)
137 *
138 * Please note that the name field contains the fully-qualified
139 * reference (or subdirectory) name. Space could be saved by only
140 * storing the relative names. But that would require the full names
141 * to be generated on the fly when iterating in do_for_each_ref(), and
142 * would break callback functions, who have always been able to assume
143 * that the name strings that they are passed will not be freed during
144 * the iteration.
145 */
146struct ref_entry {
147 unsigned char flag; /* ISSYMREF? ISPACKED? */
148 union {
149 struct ref_value value; /* if not (flags&REF_DIR) */
150 struct ref_dir subdir; /* if (flags&REF_DIR) */
151 } u;
152 /*
153 * The full name of the reference (e.g., "refs/heads/master")
154 * or the full name of the directory with a trailing slash
155 * (e.g., "refs/heads/"):
156 */
157 char name[FLEX_ARRAY];
158};
159
160static void read_loose_refs(const char *dirname, struct ref_dir *dir);
161static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
162static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
163 const char *dirname, size_t len,
164 int incomplete);
165static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
166
167static struct ref_dir *get_ref_dir(struct ref_entry *entry)
168{
169 struct ref_dir *dir;
170 assert(entry->flag & REF_DIR);
171 dir = &entry->u.subdir;
172 if (entry->flag & REF_INCOMPLETE) {
173 read_loose_refs(entry->name, dir);
174
175 /*
176 * Manually add refs/bisect, which, being
177 * per-worktree, might not appear in the directory
178 * listing for refs/ in the main repo.
179 */
180 if (!strcmp(entry->name, "refs/")) {
181 int pos = search_ref_dir(dir, "refs/bisect/", 12);
182 if (pos < 0) {
183 struct ref_entry *child_entry;
184 child_entry = create_dir_entry(dir->ref_cache,
185 "refs/bisect/",
186 12, 1);
187 add_entry_to_dir(dir, child_entry);
188 read_loose_refs("refs/bisect",
189 &child_entry->u.subdir);
190 }
191 }
192 entry->flag &= ~REF_INCOMPLETE;
193 }
194 return dir;
195}
196
197static struct ref_entry *create_ref_entry(const char *refname,
198 const unsigned char *sha1, int flag,
199 int check_name)
200{
7bd9bcf3
MH
201 struct ref_entry *ref;
202
203 if (check_name &&
204 check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
205 die("Reference has invalid format: '%s'", refname);
96ffc06f 206 FLEX_ALLOC_STR(ref, name, refname);
7bd9bcf3
MH
207 hashcpy(ref->u.value.oid.hash, sha1);
208 oidclr(&ref->u.value.peeled);
7bd9bcf3
MH
209 ref->flag = flag;
210 return ref;
211}
212
213static void clear_ref_dir(struct ref_dir *dir);
214
215static void free_ref_entry(struct ref_entry *entry)
216{
217 if (entry->flag & REF_DIR) {
218 /*
219 * Do not use get_ref_dir() here, as that might
220 * trigger the reading of loose refs.
221 */
222 clear_ref_dir(&entry->u.subdir);
223 }
224 free(entry);
225}
226
227/*
228 * Add a ref_entry to the end of dir (unsorted). Entry is always
229 * stored directly in dir; no recursion into subdirectories is
230 * done.
231 */
232static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry)
233{
234 ALLOC_GROW(dir->entries, dir->nr + 1, dir->alloc);
235 dir->entries[dir->nr++] = entry;
236 /* optimize for the case that entries are added in order */
237 if (dir->nr == 1 ||
238 (dir->nr == dir->sorted + 1 &&
239 strcmp(dir->entries[dir->nr - 2]->name,
240 dir->entries[dir->nr - 1]->name) < 0))
241 dir->sorted = dir->nr;
242}
243
244/*
245 * Clear and free all entries in dir, recursively.
246 */
247static void clear_ref_dir(struct ref_dir *dir)
248{
249 int i;
250 for (i = 0; i < dir->nr; i++)
251 free_ref_entry(dir->entries[i]);
252 free(dir->entries);
253 dir->sorted = dir->nr = dir->alloc = 0;
254 dir->entries = NULL;
255}
256
257/*
258 * Create a struct ref_entry object for the specified dirname.
259 * dirname is the name of the directory with a trailing slash (e.g.,
260 * "refs/heads/") or "" for the top-level directory.
261 */
262static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
263 const char *dirname, size_t len,
264 int incomplete)
265{
266 struct ref_entry *direntry;
96ffc06f 267 FLEX_ALLOC_MEM(direntry, name, dirname, len);
7bd9bcf3
MH
268 direntry->u.subdir.ref_cache = ref_cache;
269 direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
270 return direntry;
271}
272
273static int ref_entry_cmp(const void *a, const void *b)
274{
275 struct ref_entry *one = *(struct ref_entry **)a;
276 struct ref_entry *two = *(struct ref_entry **)b;
277 return strcmp(one->name, two->name);
278}
279
280static void sort_ref_dir(struct ref_dir *dir);
281
282struct string_slice {
283 size_t len;
284 const char *str;
285};
286
287static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
288{
289 const struct string_slice *key = key_;
290 const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
291 int cmp = strncmp(key->str, ent->name, key->len);
292 if (cmp)
293 return cmp;
294 return '\0' - (unsigned char)ent->name[key->len];
295}
296
297/*
298 * Return the index of the entry with the given refname from the
299 * ref_dir (non-recursively), sorting dir if necessary. Return -1 if
300 * no such entry is found. dir must already be complete.
301 */
302static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len)
303{
304 struct ref_entry **r;
305 struct string_slice key;
306
307 if (refname == NULL || !dir->nr)
308 return -1;
309
310 sort_ref_dir(dir);
311 key.len = len;
312 key.str = refname;
313 r = bsearch(&key, dir->entries, dir->nr, sizeof(*dir->entries),
314 ref_entry_cmp_sslice);
315
316 if (r == NULL)
317 return -1;
318
319 return r - dir->entries;
320}
321
322/*
323 * Search for a directory entry directly within dir (without
324 * recursing). Sort dir if necessary. subdirname must be a directory
325 * name (i.e., end in '/'). If mkdir is set, then create the
326 * directory if it is missing; otherwise, return NULL if the desired
327 * directory cannot be found. dir must already be complete.
328 */
329static struct ref_dir *search_for_subdir(struct ref_dir *dir,
330 const char *subdirname, size_t len,
331 int mkdir)
332{
333 int entry_index = search_ref_dir(dir, subdirname, len);
334 struct ref_entry *entry;
335 if (entry_index == -1) {
336 if (!mkdir)
337 return NULL;
338 /*
339 * Since dir is complete, the absence of a subdir
340 * means that the subdir really doesn't exist;
341 * therefore, create an empty record for it but mark
342 * the record complete.
343 */
344 entry = create_dir_entry(dir->ref_cache, subdirname, len, 0);
345 add_entry_to_dir(dir, entry);
346 } else {
347 entry = dir->entries[entry_index];
348 }
349 return get_ref_dir(entry);
350}
351
352/*
353 * If refname is a reference name, find the ref_dir within the dir
354 * tree that should hold refname. If refname is a directory name
355 * (i.e., ends in '/'), then return that ref_dir itself. dir must
356 * represent the top-level directory and must already be complete.
357 * Sort ref_dirs and recurse into subdirectories as necessary. If
358 * mkdir is set, then create any missing directories; otherwise,
359 * return NULL if the desired directory cannot be found.
360 */
361static struct ref_dir *find_containing_dir(struct ref_dir *dir,
362 const char *refname, int mkdir)
363{
364 const char *slash;
365 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
366 size_t dirnamelen = slash - refname + 1;
367 struct ref_dir *subdir;
368 subdir = search_for_subdir(dir, refname, dirnamelen, mkdir);
369 if (!subdir) {
370 dir = NULL;
371 break;
372 }
373 dir = subdir;
374 }
375
376 return dir;
377}
378
379/*
380 * Find the value entry with the given name in dir, sorting ref_dirs
381 * and recursing into subdirectories as necessary. If the name is not
382 * found or it corresponds to a directory entry, return NULL.
383 */
384static struct ref_entry *find_ref(struct ref_dir *dir, const char *refname)
385{
386 int entry_index;
387 struct ref_entry *entry;
388 dir = find_containing_dir(dir, refname, 0);
389 if (!dir)
390 return NULL;
391 entry_index = search_ref_dir(dir, refname, strlen(refname));
392 if (entry_index == -1)
393 return NULL;
394 entry = dir->entries[entry_index];
395 return (entry->flag & REF_DIR) ? NULL : entry;
396}
397
398/*
399 * Remove the entry with the given name from dir, recursing into
400 * subdirectories as necessary. If refname is the name of a directory
401 * (i.e., ends with '/'), then remove the directory and its contents.
402 * If the removal was successful, return the number of entries
403 * remaining in the directory entry that contained the deleted entry.
404 * If the name was not found, return -1. Please note that this
405 * function only deletes the entry from the cache; it does not delete
406 * it from the filesystem or ensure that other cache entries (which
407 * might be symbolic references to the removed entry) are updated.
408 * Nor does it remove any containing dir entries that might be made
409 * empty by the removal. dir must represent the top-level directory
410 * and must already be complete.
411 */
412static int remove_entry(struct ref_dir *dir, const char *refname)
413{
414 int refname_len = strlen(refname);
415 int entry_index;
416 struct ref_entry *entry;
417 int is_dir = refname[refname_len - 1] == '/';
418 if (is_dir) {
419 /*
420 * refname represents a reference directory. Remove
421 * the trailing slash; otherwise we will get the
422 * directory *representing* refname rather than the
423 * one *containing* it.
424 */
425 char *dirname = xmemdupz(refname, refname_len - 1);
426 dir = find_containing_dir(dir, dirname, 0);
427 free(dirname);
428 } else {
429 dir = find_containing_dir(dir, refname, 0);
430 }
431 if (!dir)
432 return -1;
433 entry_index = search_ref_dir(dir, refname, refname_len);
434 if (entry_index == -1)
435 return -1;
436 entry = dir->entries[entry_index];
437
438 memmove(&dir->entries[entry_index],
439 &dir->entries[entry_index + 1],
440 (dir->nr - entry_index - 1) * sizeof(*dir->entries)
441 );
442 dir->nr--;
443 if (dir->sorted > entry_index)
444 dir->sorted--;
445 free_ref_entry(entry);
446 return dir->nr;
447}
448
449/*
450 * Add a ref_entry to the ref_dir (unsorted), recursing into
451 * subdirectories as necessary. dir must represent the top-level
452 * directory. Return 0 on success.
453 */
454static int add_ref(struct ref_dir *dir, struct ref_entry *ref)
455{
456 dir = find_containing_dir(dir, ref->name, 1);
457 if (!dir)
458 return -1;
459 add_entry_to_dir(dir, ref);
460 return 0;
461}
462
463/*
464 * Emit a warning and return true iff ref1 and ref2 have the same name
465 * and the same sha1. Die if they have the same name but different
466 * sha1s.
467 */
468static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2)
469{
470 if (strcmp(ref1->name, ref2->name))
471 return 0;
472
473 /* Duplicate name; make sure that they don't conflict: */
474
475 if ((ref1->flag & REF_DIR) || (ref2->flag & REF_DIR))
476 /* This is impossible by construction */
477 die("Reference directory conflict: %s", ref1->name);
478
479 if (oidcmp(&ref1->u.value.oid, &ref2->u.value.oid))
480 die("Duplicated ref, and SHA1s don't match: %s", ref1->name);
481
482 warning("Duplicated ref: %s", ref1->name);
483 return 1;
484}
485
486/*
487 * Sort the entries in dir non-recursively (if they are not already
488 * sorted) and remove any duplicate entries.
489 */
490static void sort_ref_dir(struct ref_dir *dir)
491{
492 int i, j;
493 struct ref_entry *last = NULL;
494
495 /*
496 * This check also prevents passing a zero-length array to qsort(),
497 * which is a problem on some platforms.
498 */
499 if (dir->sorted == dir->nr)
500 return;
501
502 qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
503
504 /* Remove any duplicates: */
505 for (i = 0, j = 0; j < dir->nr; j++) {
506 struct ref_entry *entry = dir->entries[j];
507 if (last && is_dup_ref(last, entry))
508 free_ref_entry(entry);
509 else
510 last = dir->entries[i++] = entry;
511 }
512 dir->sorted = dir->nr = i;
513}
514
7bd9bcf3 515/*
a8739244
MH
516 * Return true if refname, which has the specified oid and flags, can
517 * be resolved to an object in the database. If the referred-to object
518 * does not exist, emit a warning and return false.
7bd9bcf3 519 */
a8739244
MH
520static int ref_resolves_to_object(const char *refname,
521 const struct object_id *oid,
522 unsigned int flags)
7bd9bcf3 523{
a8739244 524 if (flags & REF_ISBROKEN)
7bd9bcf3 525 return 0;
a8739244
MH
526 if (!has_sha1_file(oid->hash)) {
527 error("%s does not point to a valid object!", refname);
7bd9bcf3
MH
528 return 0;
529 }
530 return 1;
531}
532
a8739244
MH
533/*
534 * Return true if the reference described by entry can be resolved to
535 * an object in the database; otherwise, emit a warning and return
536 * false.
537 */
538static int entry_resolves_to_object(struct ref_entry *entry)
539{
540 return ref_resolves_to_object(entry->name,
541 &entry->u.value.oid, entry->flag);
542}
543
7bd9bcf3
MH
544/*
545 * current_ref is a performance hack: when iterating over references
546 * using the for_each_ref*() functions, current_ref is set to the
547 * current reference's entry before calling the callback function. If
548 * the callback function calls peel_ref(), then peel_ref() first
549 * checks whether the reference to be peeled is the current reference
550 * (it usually is) and if so, returns that reference's peeled version
551 * if it is available. This avoids a refname lookup in a common case.
552 */
553static struct ref_entry *current_ref;
554
555typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
556
557struct ref_entry_cb {
4633a846 558 const char *prefix;
7bd9bcf3
MH
559 int trim;
560 int flags;
561 each_ref_fn *fn;
562 void *cb_data;
563};
564
565/*
566 * Handle one reference in a do_for_each_ref*()-style iteration,
567 * calling an each_ref_fn for each entry.
568 */
569static int do_one_ref(struct ref_entry *entry, void *cb_data)
570{
571 struct ref_entry_cb *data = cb_data;
572 struct ref_entry *old_current_ref;
573 int retval;
574
4633a846 575 if (!starts_with(entry->name, data->prefix))
7bd9bcf3
MH
576 return 0;
577
578 if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
ffeef642 579 !entry_resolves_to_object(entry))
7bd9bcf3
MH
580 return 0;
581
582 /* Store the old value, in case this is a recursive call: */
583 old_current_ref = current_ref;
584 current_ref = entry;
585 retval = data->fn(entry->name + data->trim, &entry->u.value.oid,
586 entry->flag, data->cb_data);
587 current_ref = old_current_ref;
588 return retval;
589}
590
591/*
592 * Call fn for each reference in dir that has index in the range
593 * offset <= index < dir->nr. Recurse into subdirectories that are in
594 * that index range, sorting them before iterating. This function
595 * does not sort dir itself; it should be sorted beforehand. fn is
596 * called for all references, including broken ones.
597 */
598static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
599 each_ref_entry_fn fn, void *cb_data)
600{
601 int i;
602 assert(dir->sorted == dir->nr);
603 for (i = offset; i < dir->nr; i++) {
604 struct ref_entry *entry = dir->entries[i];
605 int retval;
606 if (entry->flag & REF_DIR) {
607 struct ref_dir *subdir = get_ref_dir(entry);
608 sort_ref_dir(subdir);
609 retval = do_for_each_entry_in_dir(subdir, 0, fn, cb_data);
610 } else {
611 retval = fn(entry, cb_data);
612 }
613 if (retval)
614 return retval;
615 }
616 return 0;
617}
618
619/*
620 * Call fn for each reference in the union of dir1 and dir2, in order
621 * by refname. Recurse into subdirectories. If a value entry appears
622 * in both dir1 and dir2, then only process the version that is in
623 * dir2. The input dirs must already be sorted, but subdirs will be
624 * sorted as needed. fn is called for all references, including
625 * broken ones.
626 */
627static int do_for_each_entry_in_dirs(struct ref_dir *dir1,
628 struct ref_dir *dir2,
629 each_ref_entry_fn fn, void *cb_data)
630{
631 int retval;
632 int i1 = 0, i2 = 0;
633
634 assert(dir1->sorted == dir1->nr);
635 assert(dir2->sorted == dir2->nr);
636 while (1) {
637 struct ref_entry *e1, *e2;
638 int cmp;
639 if (i1 == dir1->nr) {
640 return do_for_each_entry_in_dir(dir2, i2, fn, cb_data);
641 }
642 if (i2 == dir2->nr) {
643 return do_for_each_entry_in_dir(dir1, i1, fn, cb_data);
644 }
645 e1 = dir1->entries[i1];
646 e2 = dir2->entries[i2];
647 cmp = strcmp(e1->name, e2->name);
648 if (cmp == 0) {
649 if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
650 /* Both are directories; descend them in parallel. */
651 struct ref_dir *subdir1 = get_ref_dir(e1);
652 struct ref_dir *subdir2 = get_ref_dir(e2);
653 sort_ref_dir(subdir1);
654 sort_ref_dir(subdir2);
655 retval = do_for_each_entry_in_dirs(
656 subdir1, subdir2, fn, cb_data);
657 i1++;
658 i2++;
659 } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) {
660 /* Both are references; ignore the one from dir1. */
661 retval = fn(e2, cb_data);
662 i1++;
663 i2++;
664 } else {
665 die("conflict between reference and directory: %s",
666 e1->name);
667 }
668 } else {
669 struct ref_entry *e;
670 if (cmp < 0) {
671 e = e1;
672 i1++;
673 } else {
674 e = e2;
675 i2++;
676 }
677 if (e->flag & REF_DIR) {
678 struct ref_dir *subdir = get_ref_dir(e);
679 sort_ref_dir(subdir);
680 retval = do_for_each_entry_in_dir(
681 subdir, 0, fn, cb_data);
682 } else {
683 retval = fn(e, cb_data);
684 }
685 }
686 if (retval)
687 return retval;
688 }
689}
690
691/*
692 * Load all of the refs from the dir into our in-memory cache. The hard work
693 * of loading loose refs is done by get_ref_dir(), so we just need to recurse
694 * through all of the sub-directories. We do not even need to care about
695 * sorting, as traversal order does not matter to us.
696 */
697static void prime_ref_dir(struct ref_dir *dir)
698{
699 int i;
700 for (i = 0; i < dir->nr; i++) {
701 struct ref_entry *entry = dir->entries[i];
702 if (entry->flag & REF_DIR)
703 prime_ref_dir(get_ref_dir(entry));
704 }
705}
706
707struct nonmatching_ref_data {
708 const struct string_list *skip;
709 const char *conflicting_refname;
710};
711
712static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata)
713{
714 struct nonmatching_ref_data *data = vdata;
715
716 if (data->skip && string_list_has_string(data->skip, entry->name))
717 return 0;
718
719 data->conflicting_refname = entry->name;
720 return 1;
721}
722
723/*
724 * Return 0 if a reference named refname could be created without
725 * conflicting with the name of an existing reference in dir.
726 * See verify_refname_available for more information.
727 */
728static int verify_refname_available_dir(const char *refname,
729 const struct string_list *extras,
730 const struct string_list *skip,
731 struct ref_dir *dir,
732 struct strbuf *err)
733{
734 const char *slash;
0845122c 735 const char *extra_refname;
7bd9bcf3
MH
736 int pos;
737 struct strbuf dirname = STRBUF_INIT;
738 int ret = -1;
739
740 /*
741 * For the sake of comments in this function, suppose that
742 * refname is "refs/foo/bar".
743 */
744
745 assert(err);
746
747 strbuf_grow(&dirname, strlen(refname) + 1);
748 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
749 /* Expand dirname to the new prefix, not including the trailing slash: */
750 strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
751
752 /*
753 * We are still at a leading dir of the refname (e.g.,
754 * "refs/foo"; if there is a reference with that name,
755 * it is a conflict, *unless* it is in skip.
756 */
757 if (dir) {
758 pos = search_ref_dir(dir, dirname.buf, dirname.len);
759 if (pos >= 0 &&
760 (!skip || !string_list_has_string(skip, dirname.buf))) {
761 /*
762 * We found a reference whose name is
763 * a proper prefix of refname; e.g.,
764 * "refs/foo", and is not in skip.
765 */
766 strbuf_addf(err, "'%s' exists; cannot create '%s'",
767 dirname.buf, refname);
768 goto cleanup;
769 }
770 }
771
772 if (extras && string_list_has_string(extras, dirname.buf) &&
773 (!skip || !string_list_has_string(skip, dirname.buf))) {
774 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
775 refname, dirname.buf);
776 goto cleanup;
777 }
778
779 /*
780 * Otherwise, we can try to continue our search with
781 * the next component. So try to look up the
782 * directory, e.g., "refs/foo/". If we come up empty,
783 * we know there is nothing under this whole prefix,
784 * but even in that case we still have to continue the
785 * search for conflicts with extras.
786 */
787 strbuf_addch(&dirname, '/');
788 if (dir) {
789 pos = search_ref_dir(dir, dirname.buf, dirname.len);
790 if (pos < 0) {
791 /*
792 * There was no directory "refs/foo/",
793 * so there is nothing under this
794 * whole prefix. So there is no need
795 * to continue looking for conflicting
796 * references. But we need to continue
797 * looking for conflicting extras.
798 */
799 dir = NULL;
800 } else {
801 dir = get_ref_dir(dir->entries[pos]);
802 }
803 }
804 }
805
806 /*
807 * We are at the leaf of our refname (e.g., "refs/foo/bar").
808 * There is no point in searching for a reference with that
809 * name, because a refname isn't considered to conflict with
810 * itself. But we still need to check for references whose
811 * names are in the "refs/foo/bar/" namespace, because they
812 * *do* conflict.
813 */
814 strbuf_addstr(&dirname, refname + dirname.len);
815 strbuf_addch(&dirname, '/');
816
817 if (dir) {
818 pos = search_ref_dir(dir, dirname.buf, dirname.len);
819
820 if (pos >= 0) {
821 /*
822 * We found a directory named "$refname/"
823 * (e.g., "refs/foo/bar/"). It is a problem
824 * iff it contains any ref that is not in
825 * "skip".
826 */
827 struct nonmatching_ref_data data;
828
829 data.skip = skip;
830 data.conflicting_refname = NULL;
831 dir = get_ref_dir(dir->entries[pos]);
832 sort_ref_dir(dir);
833 if (do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
834 strbuf_addf(err, "'%s' exists; cannot create '%s'",
835 data.conflicting_refname, refname);
836 goto cleanup;
837 }
838 }
839 }
840
0845122c
DT
841 extra_refname = find_descendant_ref(dirname.buf, extras, skip);
842 if (extra_refname)
843 strbuf_addf(err, "cannot process '%s' and '%s' at the same time",
844 refname, extra_refname);
845 else
846 ret = 0;
7bd9bcf3
MH
847
848cleanup:
849 strbuf_release(&dirname);
850 return ret;
851}
852
853struct packed_ref_cache {
854 struct ref_entry *root;
855
856 /*
857 * Count of references to the data structure in this instance,
858 * including the pointer from ref_cache::packed if any. The
859 * data will not be freed as long as the reference count is
860 * nonzero.
861 */
862 unsigned int referrers;
863
864 /*
865 * Iff the packed-refs file associated with this instance is
866 * currently locked for writing, this points at the associated
867 * lock (which is owned by somebody else). The referrer count
868 * is also incremented when the file is locked and decremented
869 * when it is unlocked.
870 */
871 struct lock_file *lock;
872
873 /* The metadata from when this packed-refs cache was read */
874 struct stat_validity validity;
875};
876
877/*
878 * Future: need to be in "struct repository"
879 * when doing a full libification.
880 */
881static struct ref_cache {
882 struct ref_cache *next;
883 struct ref_entry *loose;
884 struct packed_ref_cache *packed;
885 /*
886 * The submodule name, or "" for the main repo. We allocate
887 * length 1 rather than FLEX_ARRAY so that the main ref_cache
888 * is initialized correctly.
889 */
890 char name[1];
891} ref_cache, *submodule_ref_caches;
892
893/* Lock used for the main packed-refs file: */
894static struct lock_file packlock;
895
896/*
897 * Increment the reference count of *packed_refs.
898 */
899static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
900{
901 packed_refs->referrers++;
902}
903
904/*
905 * Decrease the reference count of *packed_refs. If it goes to zero,
906 * free *packed_refs and return true; otherwise return false.
907 */
908static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
909{
910 if (!--packed_refs->referrers) {
911 free_ref_entry(packed_refs->root);
912 stat_validity_clear(&packed_refs->validity);
913 free(packed_refs);
914 return 1;
915 } else {
916 return 0;
917 }
918}
919
920static void clear_packed_ref_cache(struct ref_cache *refs)
921{
922 if (refs->packed) {
923 struct packed_ref_cache *packed_refs = refs->packed;
924
925 if (packed_refs->lock)
926 die("internal error: packed-ref cache cleared while locked");
927 refs->packed = NULL;
928 release_packed_ref_cache(packed_refs);
929 }
930}
931
932static void clear_loose_ref_cache(struct ref_cache *refs)
933{
934 if (refs->loose) {
935 free_ref_entry(refs->loose);
936 refs->loose = NULL;
937 }
938}
939
a2d5156c
JK
940/*
941 * Create a new submodule ref cache and add it to the internal
942 * set of caches.
943 */
7bd9bcf3
MH
944static struct ref_cache *create_ref_cache(const char *submodule)
945{
7bd9bcf3
MH
946 struct ref_cache *refs;
947 if (!submodule)
948 submodule = "";
96ffc06f 949 FLEX_ALLOC_STR(refs, name, submodule);
a2d5156c
JK
950 refs->next = submodule_ref_caches;
951 submodule_ref_caches = refs;
7bd9bcf3
MH
952 return refs;
953}
954
a2d5156c 955static struct ref_cache *lookup_ref_cache(const char *submodule)
7bd9bcf3
MH
956{
957 struct ref_cache *refs;
958
959 if (!submodule || !*submodule)
960 return &ref_cache;
961
962 for (refs = submodule_ref_caches; refs; refs = refs->next)
963 if (!strcmp(submodule, refs->name))
964 return refs;
a2d5156c
JK
965 return NULL;
966}
7bd9bcf3 967
a2d5156c
JK
968/*
969 * Return a pointer to a ref_cache for the specified submodule. For
2eed2780
MH
970 * the main repository, use submodule==NULL; such a call cannot fail.
971 * For a submodule, the submodule must exist and be a nonbare
972 * repository, otherwise return NULL.
973 *
974 * The returned structure will be allocated and initialized but not
975 * necessarily populated; it should not be freed.
a2d5156c
JK
976 */
977static struct ref_cache *get_ref_cache(const char *submodule)
978{
979 struct ref_cache *refs = lookup_ref_cache(submodule);
2eed2780
MH
980
981 if (!refs) {
982 struct strbuf submodule_sb = STRBUF_INIT;
983
984 strbuf_addstr(&submodule_sb, submodule);
985 if (is_nonbare_repository_dir(&submodule_sb))
986 refs = create_ref_cache(submodule);
987 strbuf_release(&submodule_sb);
988 }
989
7bd9bcf3
MH
990 return refs;
991}
992
993/* The length of a peeled reference line in packed-refs, including EOL: */
994#define PEELED_LINE_LENGTH 42
995
996/*
997 * The packed-refs header line that we write out. Perhaps other
998 * traits will be added later. The trailing space is required.
999 */
1000static const char PACKED_REFS_HEADER[] =
1001 "# pack-refs with: peeled fully-peeled \n";
1002
1003/*
1004 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
1005 * Return a pointer to the refname within the line (null-terminated),
1006 * or NULL if there was a problem.
1007 */
1008static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
1009{
1010 const char *ref;
1011
1012 /*
1013 * 42: the answer to everything.
1014 *
1015 * In this case, it happens to be the answer to
1016 * 40 (length of sha1 hex representation)
1017 * +1 (space in between hex and name)
1018 * +1 (newline at the end of the line)
1019 */
1020 if (line->len <= 42)
1021 return NULL;
1022
1023 if (get_sha1_hex(line->buf, sha1) < 0)
1024 return NULL;
1025 if (!isspace(line->buf[40]))
1026 return NULL;
1027
1028 ref = line->buf + 41;
1029 if (isspace(*ref))
1030 return NULL;
1031
1032 if (line->buf[line->len - 1] != '\n')
1033 return NULL;
1034 line->buf[--line->len] = 0;
1035
1036 return ref;
1037}
1038
1039/*
1040 * Read f, which is a packed-refs file, into dir.
1041 *
1042 * A comment line of the form "# pack-refs with: " may contain zero or
1043 * more traits. We interpret the traits as follows:
1044 *
1045 * No traits:
1046 *
1047 * Probably no references are peeled. But if the file contains a
1048 * peeled value for a reference, we will use it.
1049 *
1050 * peeled:
1051 *
1052 * References under "refs/tags/", if they *can* be peeled, *are*
1053 * peeled in this file. References outside of "refs/tags/" are
1054 * probably not peeled even if they could have been, but if we find
1055 * a peeled value for such a reference we will use it.
1056 *
1057 * fully-peeled:
1058 *
1059 * All references in the file that can be peeled are peeled.
1060 * Inversely (and this is more important), any references in the
1061 * file for which no peeled value is recorded is not peelable. This
1062 * trait should typically be written alongside "peeled" for
1063 * compatibility with older clients, but we do not require it
1064 * (i.e., "peeled" is a no-op if "fully-peeled" is set).
1065 */
1066static void read_packed_refs(FILE *f, struct ref_dir *dir)
1067{
1068 struct ref_entry *last = NULL;
1069 struct strbuf line = STRBUF_INIT;
1070 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
1071
1072 while (strbuf_getwholeline(&line, f, '\n') != EOF) {
1073 unsigned char sha1[20];
1074 const char *refname;
1075 const char *traits;
1076
1077 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
1078 if (strstr(traits, " fully-peeled "))
1079 peeled = PEELED_FULLY;
1080 else if (strstr(traits, " peeled "))
1081 peeled = PEELED_TAGS;
1082 /* perhaps other traits later as well */
1083 continue;
1084 }
1085
1086 refname = parse_ref_line(&line, sha1);
1087 if (refname) {
1088 int flag = REF_ISPACKED;
1089
1090 if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1091 if (!refname_is_safe(refname))
1092 die("packed refname is dangerous: %s", refname);
1093 hashclr(sha1);
1094 flag |= REF_BAD_NAME | REF_ISBROKEN;
1095 }
1096 last = create_ref_entry(refname, sha1, flag, 0);
1097 if (peeled == PEELED_FULLY ||
1098 (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
1099 last->flag |= REF_KNOWS_PEELED;
1100 add_ref(dir, last);
1101 continue;
1102 }
1103 if (last &&
1104 line.buf[0] == '^' &&
1105 line.len == PEELED_LINE_LENGTH &&
1106 line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
1107 !get_sha1_hex(line.buf + 1, sha1)) {
1108 hashcpy(last->u.value.peeled.hash, sha1);
1109 /*
1110 * Regardless of what the file header said,
1111 * we definitely know the value of *this*
1112 * reference:
1113 */
1114 last->flag |= REF_KNOWS_PEELED;
1115 }
1116 }
1117
1118 strbuf_release(&line);
1119}
1120
1121/*
1122 * Get the packed_ref_cache for the specified ref_cache, creating it
1123 * if necessary.
1124 */
1125static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs)
1126{
1127 char *packed_refs_file;
1128
1129 if (*refs->name)
1130 packed_refs_file = git_pathdup_submodule(refs->name, "packed-refs");
1131 else
1132 packed_refs_file = git_pathdup("packed-refs");
1133
1134 if (refs->packed &&
1135 !stat_validity_check(&refs->packed->validity, packed_refs_file))
1136 clear_packed_ref_cache(refs);
1137
1138 if (!refs->packed) {
1139 FILE *f;
1140
1141 refs->packed = xcalloc(1, sizeof(*refs->packed));
1142 acquire_packed_ref_cache(refs->packed);
1143 refs->packed->root = create_dir_entry(refs, "", 0, 0);
1144 f = fopen(packed_refs_file, "r");
1145 if (f) {
1146 stat_validity_update(&refs->packed->validity, fileno(f));
1147 read_packed_refs(f, get_ref_dir(refs->packed->root));
1148 fclose(f);
1149 }
1150 }
1151 free(packed_refs_file);
1152 return refs->packed;
1153}
1154
1155static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
1156{
1157 return get_ref_dir(packed_ref_cache->root);
1158}
1159
1160static struct ref_dir *get_packed_refs(struct ref_cache *refs)
1161{
1162 return get_packed_ref_dir(get_packed_ref_cache(refs));
1163}
1164
1165/*
1166 * Add a reference to the in-memory packed reference cache. This may
1167 * only be called while the packed-refs file is locked (see
1168 * lock_packed_refs()). To actually write the packed-refs file, call
1169 * commit_packed_refs().
1170 */
1171static void add_packed_ref(const char *refname, const unsigned char *sha1)
1172{
1173 struct packed_ref_cache *packed_ref_cache =
1174 get_packed_ref_cache(&ref_cache);
1175
1176 if (!packed_ref_cache->lock)
1177 die("internal error: packed refs not locked");
1178 add_ref(get_packed_ref_dir(packed_ref_cache),
1179 create_ref_entry(refname, sha1, REF_ISPACKED, 1));
1180}
1181
1182/*
1183 * Read the loose references from the namespace dirname into dir
1184 * (without recursing). dirname must end with '/'. dir must be the
1185 * directory entry corresponding to dirname.
1186 */
1187static void read_loose_refs(const char *dirname, struct ref_dir *dir)
1188{
1189 struct ref_cache *refs = dir->ref_cache;
1190 DIR *d;
1191 struct dirent *de;
1192 int dirnamelen = strlen(dirname);
1193 struct strbuf refname;
1194 struct strbuf path = STRBUF_INIT;
1195 size_t path_baselen;
1196
1197 if (*refs->name)
1198 strbuf_git_path_submodule(&path, refs->name, "%s", dirname);
1199 else
1200 strbuf_git_path(&path, "%s", dirname);
1201 path_baselen = path.len;
1202
1203 d = opendir(path.buf);
1204 if (!d) {
1205 strbuf_release(&path);
1206 return;
1207 }
1208
1209 strbuf_init(&refname, dirnamelen + 257);
1210 strbuf_add(&refname, dirname, dirnamelen);
1211
1212 while ((de = readdir(d)) != NULL) {
1213 unsigned char sha1[20];
1214 struct stat st;
1215 int flag;
1216
1217 if (de->d_name[0] == '.')
1218 continue;
1219 if (ends_with(de->d_name, ".lock"))
1220 continue;
1221 strbuf_addstr(&refname, de->d_name);
1222 strbuf_addstr(&path, de->d_name);
1223 if (stat(path.buf, &st) < 0) {
1224 ; /* silently ignore */
1225 } else if (S_ISDIR(st.st_mode)) {
1226 strbuf_addch(&refname, '/');
1227 add_entry_to_dir(dir,
1228 create_dir_entry(refs, refname.buf,
1229 refname.len, 1));
1230 } else {
1231 int read_ok;
1232
1233 if (*refs->name) {
1234 hashclr(sha1);
1235 flag = 0;
1236 read_ok = !resolve_gitlink_ref(refs->name,
1237 refname.buf, sha1);
1238 } else {
1239 read_ok = !read_ref_full(refname.buf,
1240 RESOLVE_REF_READING,
1241 sha1, &flag);
1242 }
1243
1244 if (!read_ok) {
1245 hashclr(sha1);
1246 flag |= REF_ISBROKEN;
1247 } else if (is_null_sha1(sha1)) {
1248 /*
1249 * It is so astronomically unlikely
1250 * that NULL_SHA1 is the SHA-1 of an
1251 * actual object that we consider its
1252 * appearance in a loose reference
1253 * file to be repo corruption
1254 * (probably due to a software bug).
1255 */
1256 flag |= REF_ISBROKEN;
1257 }
1258
1259 if (check_refname_format(refname.buf,
1260 REFNAME_ALLOW_ONELEVEL)) {
1261 if (!refname_is_safe(refname.buf))
1262 die("loose refname is dangerous: %s", refname.buf);
1263 hashclr(sha1);
1264 flag |= REF_BAD_NAME | REF_ISBROKEN;
1265 }
1266 add_entry_to_dir(dir,
1267 create_ref_entry(refname.buf, sha1, flag, 0));
1268 }
1269 strbuf_setlen(&refname, dirnamelen);
1270 strbuf_setlen(&path, path_baselen);
1271 }
1272 strbuf_release(&refname);
1273 strbuf_release(&path);
1274 closedir(d);
1275}
1276
1277static struct ref_dir *get_loose_refs(struct ref_cache *refs)
1278{
1279 if (!refs->loose) {
1280 /*
1281 * Mark the top-level directory complete because we
1282 * are about to read the only subdirectory that can
1283 * hold references:
1284 */
1285 refs->loose = create_dir_entry(refs, "", 0, 0);
1286 /*
1287 * Create an incomplete entry for "refs/":
1288 */
1289 add_entry_to_dir(get_ref_dir(refs->loose),
1290 create_dir_entry(refs, "refs/", 5, 1));
1291 }
1292 return get_ref_dir(refs->loose);
1293}
1294
7bd9bcf3
MH
1295#define MAXREFLEN (1024)
1296
1297/*
1298 * Called by resolve_gitlink_ref_recursive() after it failed to read
1299 * from the loose refs in ref_cache refs. Find <refname> in the
1300 * packed-refs file for the submodule.
1301 */
1302static int resolve_gitlink_packed_ref(struct ref_cache *refs,
1303 const char *refname, unsigned char *sha1)
1304{
1305 struct ref_entry *ref;
1306 struct ref_dir *dir = get_packed_refs(refs);
1307
1308 ref = find_ref(dir, refname);
1309 if (ref == NULL)
1310 return -1;
1311
1312 hashcpy(sha1, ref->u.value.oid.hash);
1313 return 0;
1314}
1315
1316static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
1317 const char *refname, unsigned char *sha1,
1318 int recursion)
1319{
1320 int fd, len;
1321 char buffer[128], *p;
1322 char *path;
1323
2d0663b2 1324 if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
7bd9bcf3
MH
1325 return -1;
1326 path = *refs->name
1327 ? git_pathdup_submodule(refs->name, "%s", refname)
1328 : git_pathdup("%s", refname);
1329 fd = open(path, O_RDONLY);
1330 free(path);
1331 if (fd < 0)
1332 return resolve_gitlink_packed_ref(refs, refname, sha1);
1333
1334 len = read(fd, buffer, sizeof(buffer)-1);
1335 close(fd);
1336 if (len < 0)
1337 return -1;
1338 while (len && isspace(buffer[len-1]))
1339 len--;
1340 buffer[len] = 0;
1341
1342 /* Was it a detached head or an old-fashioned symlink? */
1343 if (!get_sha1_hex(buffer, sha1))
1344 return 0;
1345
1346 /* Symref? */
1347 if (strncmp(buffer, "ref:", 4))
1348 return -1;
1349 p = buffer + 4;
1350 while (isspace(*p))
1351 p++;
1352
1353 return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
1354}
1355
1356int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
1357{
1358 int len = strlen(path), retval;
a2d5156c 1359 struct strbuf submodule = STRBUF_INIT;
7bd9bcf3
MH
1360 struct ref_cache *refs;
1361
1362 while (len && path[len-1] == '/')
1363 len--;
1364 if (!len)
1365 return -1;
a2d5156c
JK
1366
1367 strbuf_add(&submodule, path, len);
2eed2780 1368 refs = get_ref_cache(submodule.buf);
a2d5156c 1369 if (!refs) {
2eed2780
MH
1370 strbuf_release(&submodule);
1371 return -1;
a2d5156c
JK
1372 }
1373 strbuf_release(&submodule);
7bd9bcf3
MH
1374
1375 retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
1376 return retval;
1377}
1378
1379/*
1380 * Return the ref_entry for the given refname from the packed
1381 * references. If it does not exist, return NULL.
1382 */
1383static struct ref_entry *get_packed_ref(const char *refname)
1384{
1385 return find_ref(get_packed_refs(&ref_cache), refname);
1386}
1387
1388/*
419c6f4c 1389 * A loose ref file doesn't exist; check for a packed ref.
7bd9bcf3
MH
1390 */
1391static int resolve_missing_loose_ref(const char *refname,
7bd9bcf3 1392 unsigned char *sha1,
89e82389 1393 unsigned int *flags)
7bd9bcf3
MH
1394{
1395 struct ref_entry *entry;
1396
1397 /*
1398 * The loose reference file does not exist; check for a packed
1399 * reference.
1400 */
1401 entry = get_packed_ref(refname);
1402 if (entry) {
1403 hashcpy(sha1, entry->u.value.oid.hash);
a70a93b7 1404 *flags |= REF_ISPACKED;
7bd9bcf3
MH
1405 return 0;
1406 }
419c6f4c
MH
1407 /* refname is not a packed reference. */
1408 return -1;
7bd9bcf3
MH
1409}
1410
2d0663b2 1411int read_raw_ref(const char *refname, unsigned char *sha1,
92b38093 1412 struct strbuf *referent, unsigned int *type)
7bd9bcf3 1413{
42a38cf7
MH
1414 struct strbuf sb_contents = STRBUF_INIT;
1415 struct strbuf sb_path = STRBUF_INIT;
7048653a
DT
1416 const char *path;
1417 const char *buf;
1418 struct stat st;
1419 int fd;
42a38cf7
MH
1420 int ret = -1;
1421 int save_errno;
7bd9bcf3 1422
fa96ea1b 1423 *type = 0;
42a38cf7
MH
1424 strbuf_reset(&sb_path);
1425 strbuf_git_path(&sb_path, "%s", refname);
1426 path = sb_path.buf;
7bd9bcf3 1427
7048653a
DT
1428stat_ref:
1429 /*
1430 * We might have to loop back here to avoid a race
1431 * condition: first we lstat() the file, then we try
1432 * to read it as a link or as a file. But if somebody
1433 * changes the type of the file (file <-> directory
1434 * <-> symlink) between the lstat() and reading, then
1435 * we don't want to report that as an error but rather
1436 * try again starting with the lstat().
1437 */
7bd9bcf3 1438
7048653a
DT
1439 if (lstat(path, &st) < 0) {
1440 if (errno != ENOENT)
42a38cf7 1441 goto out;
3a0b6b9a 1442 if (resolve_missing_loose_ref(refname, sha1, type)) {
7048653a 1443 errno = ENOENT;
42a38cf7 1444 goto out;
7bd9bcf3 1445 }
42a38cf7
MH
1446 ret = 0;
1447 goto out;
7bd9bcf3 1448 }
7bd9bcf3 1449
7048653a
DT
1450 /* Follow "normalized" - ie "refs/.." symlinks by hand */
1451 if (S_ISLNK(st.st_mode)) {
42a38cf7
MH
1452 strbuf_reset(&sb_contents);
1453 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
7048653a 1454 if (errno == ENOENT || errno == EINVAL)
7bd9bcf3
MH
1455 /* inconsistent with lstat; retry */
1456 goto stat_ref;
1457 else
42a38cf7 1458 goto out;
7bd9bcf3 1459 }
42a38cf7
MH
1460 if (starts_with(sb_contents.buf, "refs/") &&
1461 !check_refname_format(sb_contents.buf, 0)) {
92b38093 1462 strbuf_swap(&sb_contents, referent);
3a0b6b9a 1463 *type |= REF_ISSYMREF;
42a38cf7
MH
1464 ret = 0;
1465 goto out;
7bd9bcf3 1466 }
7048653a 1467 }
7bd9bcf3 1468
7048653a
DT
1469 /* Is it a directory? */
1470 if (S_ISDIR(st.st_mode)) {
e167a567
MH
1471 /*
1472 * Even though there is a directory where the loose
1473 * ref is supposed to be, there could still be a
1474 * packed ref:
1475 */
3a0b6b9a 1476 if (resolve_missing_loose_ref(refname, sha1, type)) {
e167a567
MH
1477 errno = EISDIR;
1478 goto out;
1479 }
1480 ret = 0;
42a38cf7 1481 goto out;
7048653a
DT
1482 }
1483
1484 /*
1485 * Anything else, just open it and try to use it as
1486 * a ref
1487 */
1488 fd = open(path, O_RDONLY);
1489 if (fd < 0) {
1490 if (errno == ENOENT)
1491 /* inconsistent with lstat; retry */
1492 goto stat_ref;
1493 else
42a38cf7 1494 goto out;
7048653a 1495 }
42a38cf7
MH
1496 strbuf_reset(&sb_contents);
1497 if (strbuf_read(&sb_contents, fd, 256) < 0) {
7048653a
DT
1498 int save_errno = errno;
1499 close(fd);
1500 errno = save_errno;
42a38cf7 1501 goto out;
7048653a
DT
1502 }
1503 close(fd);
42a38cf7
MH
1504 strbuf_rtrim(&sb_contents);
1505 buf = sb_contents.buf;
7048653a
DT
1506 if (starts_with(buf, "ref:")) {
1507 buf += 4;
7bd9bcf3
MH
1508 while (isspace(*buf))
1509 buf++;
7048653a 1510
92b38093
MH
1511 strbuf_reset(referent);
1512 strbuf_addstr(referent, buf);
3a0b6b9a 1513 *type |= REF_ISSYMREF;
42a38cf7
MH
1514 ret = 0;
1515 goto out;
7bd9bcf3 1516 }
7bd9bcf3 1517
7048653a
DT
1518 /*
1519 * Please note that FETCH_HEAD has additional
1520 * data after the sha.
1521 */
1522 if (get_sha1_hex(buf, sha1) ||
1523 (buf[40] != '\0' && !isspace(buf[40]))) {
3a0b6b9a 1524 *type |= REF_ISBROKEN;
7048653a 1525 errno = EINVAL;
42a38cf7 1526 goto out;
7048653a
DT
1527 }
1528
42a38cf7 1529 ret = 0;
7bd9bcf3 1530
42a38cf7
MH
1531out:
1532 save_errno = errno;
7bd9bcf3
MH
1533 strbuf_release(&sb_path);
1534 strbuf_release(&sb_contents);
42a38cf7 1535 errno = save_errno;
7bd9bcf3
MH
1536 return ret;
1537}
1538
8415d247
MH
1539static void unlock_ref(struct ref_lock *lock)
1540{
1541 /* Do not free lock->lk -- atexit() still looks at them */
1542 if (lock->lk)
1543 rollback_lock_file(lock->lk);
1544 free(lock->ref_name);
8415d247
MH
1545 free(lock);
1546}
1547
92b1551b
MH
1548/*
1549 * Lock refname, without following symrefs, and set *lock_p to point
1550 * at a newly-allocated lock object. Fill in lock->old_oid, referent,
1551 * and type similarly to read_raw_ref().
1552 *
1553 * The caller must verify that refname is a "safe" reference name (in
1554 * the sense of refname_is_safe()) before calling this function.
1555 *
1556 * If the reference doesn't already exist, verify that refname doesn't
1557 * have a D/F conflict with any existing references. extras and skip
1558 * are passed to verify_refname_available_dir() for this check.
1559 *
1560 * If mustexist is not set and the reference is not found or is
1561 * broken, lock the reference anyway but clear sha1.
1562 *
1563 * Return 0 on success. On failure, write an error message to err and
1564 * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
1565 *
1566 * Implementation note: This function is basically
1567 *
1568 * lock reference
1569 * read_raw_ref()
1570 *
1571 * but it includes a lot more code to
1572 * - Deal with possible races with other processes
1573 * - Avoid calling verify_refname_available_dir() when it can be
1574 * avoided, namely if we were successfully able to read the ref
1575 * - Generate informative error messages in the case of failure
1576 */
1577static int lock_raw_ref(const char *refname, int mustexist,
1578 const struct string_list *extras,
1579 const struct string_list *skip,
1580 struct ref_lock **lock_p,
1581 struct strbuf *referent,
1582 unsigned int *type,
1583 struct strbuf *err)
1584{
1585 struct ref_lock *lock;
1586 struct strbuf ref_file = STRBUF_INIT;
1587 int attempts_remaining = 3;
1588 int ret = TRANSACTION_GENERIC_ERROR;
1589
1590 assert(err);
1591 *type = 0;
1592
1593 /* First lock the file so it can't change out from under us. */
1594
1595 *lock_p = lock = xcalloc(1, sizeof(*lock));
1596
1597 lock->ref_name = xstrdup(refname);
92b1551b
MH
1598 strbuf_git_path(&ref_file, "%s", refname);
1599
1600retry:
1601 switch (safe_create_leading_directories(ref_file.buf)) {
1602 case SCLD_OK:
1603 break; /* success */
1604 case SCLD_EXISTS:
1605 /*
1606 * Suppose refname is "refs/foo/bar". We just failed
1607 * to create the containing directory, "refs/foo",
1608 * because there was a non-directory in the way. This
1609 * indicates a D/F conflict, probably because of
1610 * another reference such as "refs/foo". There is no
1611 * reason to expect this error to be transitory.
1612 */
1613 if (verify_refname_available(refname, extras, skip, err)) {
1614 if (mustexist) {
1615 /*
1616 * To the user the relevant error is
1617 * that the "mustexist" reference is
1618 * missing:
1619 */
1620 strbuf_reset(err);
1621 strbuf_addf(err, "unable to resolve reference '%s'",
1622 refname);
1623 } else {
1624 /*
1625 * The error message set by
1626 * verify_refname_available_dir() is OK.
1627 */
1628 ret = TRANSACTION_NAME_CONFLICT;
1629 }
1630 } else {
1631 /*
1632 * The file that is in the way isn't a loose
1633 * reference. Report it as a low-level
1634 * failure.
1635 */
1636 strbuf_addf(err, "unable to create lock file %s.lock; "
1637 "non-directory in the way",
1638 ref_file.buf);
1639 }
1640 goto error_return;
1641 case SCLD_VANISHED:
1642 /* Maybe another process was tidying up. Try again. */
1643 if (--attempts_remaining > 0)
1644 goto retry;
1645 /* fall through */
1646 default:
1647 strbuf_addf(err, "unable to create directory for %s",
1648 ref_file.buf);
1649 goto error_return;
1650 }
1651
1652 if (!lock->lk)
1653 lock->lk = xcalloc(1, sizeof(struct lock_file));
1654
1655 if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
1656 if (errno == ENOENT && --attempts_remaining > 0) {
1657 /*
1658 * Maybe somebody just deleted one of the
1659 * directories leading to ref_file. Try
1660 * again:
1661 */
1662 goto retry;
1663 } else {
1664 unable_to_lock_message(ref_file.buf, errno, err);
1665 goto error_return;
1666 }
1667 }
1668
1669 /*
1670 * Now we hold the lock and can read the reference without
1671 * fear that its value will change.
1672 */
1673
1674 if (read_raw_ref(refname, lock->old_oid.hash, referent, type)) {
1675 if (errno == ENOENT) {
1676 if (mustexist) {
1677 /* Garden variety missing reference. */
1678 strbuf_addf(err, "unable to resolve reference '%s'",
1679 refname);
1680 goto error_return;
1681 } else {
1682 /*
1683 * Reference is missing, but that's OK. We
1684 * know that there is not a conflict with
1685 * another loose reference because
1686 * (supposing that we are trying to lock
1687 * reference "refs/foo/bar"):
1688 *
1689 * - We were successfully able to create
1690 * the lockfile refs/foo/bar.lock, so we
1691 * know there cannot be a loose reference
1692 * named "refs/foo".
1693 *
1694 * - We got ENOENT and not EISDIR, so we
1695 * know that there cannot be a loose
1696 * reference named "refs/foo/bar/baz".
1697 */
1698 }
1699 } else if (errno == EISDIR) {
1700 /*
1701 * There is a directory in the way. It might have
1702 * contained references that have been deleted. If
1703 * we don't require that the reference already
1704 * exists, try to remove the directory so that it
1705 * doesn't cause trouble when we want to rename the
1706 * lockfile into place later.
1707 */
1708 if (mustexist) {
1709 /* Garden variety missing reference. */
1710 strbuf_addf(err, "unable to resolve reference '%s'",
1711 refname);
1712 goto error_return;
1713 } else if (remove_dir_recursively(&ref_file,
1714 REMOVE_DIR_EMPTY_ONLY)) {
1715 if (verify_refname_available_dir(
1716 refname, extras, skip,
1717 get_loose_refs(&ref_cache),
1718 err)) {
1719 /*
1720 * The error message set by
1721 * verify_refname_available() is OK.
1722 */
1723 ret = TRANSACTION_NAME_CONFLICT;
1724 goto error_return;
1725 } else {
1726 /*
1727 * We can't delete the directory,
1728 * but we also don't know of any
1729 * references that it should
1730 * contain.
1731 */
1732 strbuf_addf(err, "there is a non-empty directory '%s' "
1733 "blocking reference '%s'",
1734 ref_file.buf, refname);
1735 goto error_return;
1736 }
1737 }
1738 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
1739 strbuf_addf(err, "unable to resolve reference '%s': "
1740 "reference broken", refname);
1741 goto error_return;
1742 } else {
1743 strbuf_addf(err, "unable to resolve reference '%s': %s",
1744 refname, strerror(errno));
1745 goto error_return;
1746 }
1747
1748 /*
1749 * If the ref did not exist and we are creating it,
1750 * make sure there is no existing packed ref whose
1751 * name begins with our refname, nor a packed ref
1752 * whose name is a proper prefix of our refname.
1753 */
1754 if (verify_refname_available_dir(
1755 refname, extras, skip,
1756 get_packed_refs(&ref_cache),
1757 err)) {
1758 goto error_return;
1759 }
1760 }
1761
1762 ret = 0;
1763 goto out;
1764
1765error_return:
1766 unlock_ref(lock);
1767 *lock_p = NULL;
1768
1769out:
1770 strbuf_release(&ref_file);
1771 return ret;
1772}
1773
7bd9bcf3
MH
1774/*
1775 * Peel the entry (if possible) and return its new peel_status. If
1776 * repeel is true, re-peel the entry even if there is an old peeled
1777 * value that is already stored in it.
1778 *
1779 * It is OK to call this function with a packed reference entry that
1780 * might be stale and might even refer to an object that has since
1781 * been garbage-collected. In such a case, if the entry has
1782 * REF_KNOWS_PEELED then leave the status unchanged and return
1783 * PEEL_PEELED or PEEL_NON_TAG; otherwise, return PEEL_INVALID.
1784 */
1785static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
1786{
1787 enum peel_status status;
1788
1789 if (entry->flag & REF_KNOWS_PEELED) {
1790 if (repeel) {
1791 entry->flag &= ~REF_KNOWS_PEELED;
1792 oidclr(&entry->u.value.peeled);
1793 } else {
1794 return is_null_oid(&entry->u.value.peeled) ?
1795 PEEL_NON_TAG : PEEL_PEELED;
1796 }
1797 }
1798 if (entry->flag & REF_ISBROKEN)
1799 return PEEL_BROKEN;
1800 if (entry->flag & REF_ISSYMREF)
1801 return PEEL_IS_SYMREF;
1802
1803 status = peel_object(entry->u.value.oid.hash, entry->u.value.peeled.hash);
1804 if (status == PEEL_PEELED || status == PEEL_NON_TAG)
1805 entry->flag |= REF_KNOWS_PEELED;
1806 return status;
1807}
1808
1809int peel_ref(const char *refname, unsigned char *sha1)
1810{
1811 int flag;
1812 unsigned char base[20];
1813
1814 if (current_ref && (current_ref->name == refname
1815 || !strcmp(current_ref->name, refname))) {
1816 if (peel_entry(current_ref, 0))
1817 return -1;
1818 hashcpy(sha1, current_ref->u.value.peeled.hash);
1819 return 0;
1820 }
1821
1822 if (read_ref_full(refname, RESOLVE_REF_READING, base, &flag))
1823 return -1;
1824
1825 /*
1826 * If the reference is packed, read its ref_entry from the
1827 * cache in the hope that we already know its peeled value.
1828 * We only try this optimization on packed references because
1829 * (a) forcing the filling of the loose reference cache could
1830 * be expensive and (b) loose references anyway usually do not
1831 * have REF_KNOWS_PEELED.
1832 */
1833 if (flag & REF_ISPACKED) {
1834 struct ref_entry *r = get_packed_ref(refname);
1835 if (r) {
1836 if (peel_entry(r, 0))
1837 return -1;
1838 hashcpy(sha1, r->u.value.peeled.hash);
1839 return 0;
1840 }
1841 }
1842
1843 return peel_object(base, sha1);
1844}
1845
1846/*
1847 * Call fn for each reference in the specified ref_cache, omitting
4633a846
MH
1848 * references not in the containing_dir of prefix. Call fn for all
1849 * references, including broken ones. If fn ever returns a non-zero
7bd9bcf3
MH
1850 * value, stop the iteration and return that value; otherwise, return
1851 * 0.
1852 */
4633a846 1853static int do_for_each_entry(struct ref_cache *refs, const char *prefix,
7bd9bcf3
MH
1854 each_ref_entry_fn fn, void *cb_data)
1855{
1856 struct packed_ref_cache *packed_ref_cache;
1857 struct ref_dir *loose_dir;
1858 struct ref_dir *packed_dir;
1859 int retval = 0;
1860
1861 /*
1862 * We must make sure that all loose refs are read before accessing the
1863 * packed-refs file; this avoids a race condition in which loose refs
1864 * are migrated to the packed-refs file by a simultaneous process, but
1865 * our in-memory view is from before the migration. get_packed_ref_cache()
1866 * takes care of making sure our view is up to date with what is on
1867 * disk.
1868 */
1869 loose_dir = get_loose_refs(refs);
4633a846
MH
1870 if (prefix && *prefix) {
1871 loose_dir = find_containing_dir(loose_dir, prefix, 0);
7bd9bcf3
MH
1872 }
1873 if (loose_dir)
1874 prime_ref_dir(loose_dir);
1875
1876 packed_ref_cache = get_packed_ref_cache(refs);
1877 acquire_packed_ref_cache(packed_ref_cache);
1878 packed_dir = get_packed_ref_dir(packed_ref_cache);
4633a846
MH
1879 if (prefix && *prefix) {
1880 packed_dir = find_containing_dir(packed_dir, prefix, 0);
7bd9bcf3
MH
1881 }
1882
1883 if (packed_dir && loose_dir) {
1884 sort_ref_dir(packed_dir);
1885 sort_ref_dir(loose_dir);
1886 retval = do_for_each_entry_in_dirs(
1887 packed_dir, loose_dir, fn, cb_data);
1888 } else if (packed_dir) {
1889 sort_ref_dir(packed_dir);
1890 retval = do_for_each_entry_in_dir(
1891 packed_dir, 0, fn, cb_data);
1892 } else if (loose_dir) {
1893 sort_ref_dir(loose_dir);
1894 retval = do_for_each_entry_in_dir(
1895 loose_dir, 0, fn, cb_data);
1896 }
1897
1898 release_packed_ref_cache(packed_ref_cache);
1899 return retval;
1900}
1901
4633a846 1902int do_for_each_ref(const char *submodule, const char *prefix,
93770590 1903 each_ref_fn fn, int trim, int flags, void *cb_data)
7bd9bcf3
MH
1904{
1905 struct ref_entry_cb data;
93770590
DT
1906 struct ref_cache *refs;
1907
1908 refs = get_ref_cache(submodule);
2eed2780
MH
1909 if (!refs)
1910 return 0;
1911
4633a846 1912 data.prefix = prefix;
7bd9bcf3
MH
1913 data.trim = trim;
1914 data.flags = flags;
1915 data.fn = fn;
1916 data.cb_data = cb_data;
1917
1918 if (ref_paranoia < 0)
1919 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1920 if (ref_paranoia)
1921 data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1922
4633a846 1923 return do_for_each_entry(refs, prefix, do_one_ref, &data);
7bd9bcf3
MH
1924}
1925
7bd9bcf3
MH
1926/*
1927 * Verify that the reference locked by lock has the value old_sha1.
1928 * Fail if the reference doesn't exist and mustexist is set. Return 0
1929 * on success. On error, write an error message to err, set errno, and
1930 * return a negative value.
1931 */
1932static int verify_lock(struct ref_lock *lock,
1933 const unsigned char *old_sha1, int mustexist,
1934 struct strbuf *err)
1935{
1936 assert(err);
1937
1938 if (read_ref_full(lock->ref_name,
1939 mustexist ? RESOLVE_REF_READING : 0,
1940 lock->old_oid.hash, NULL)) {
6294dcb4
JK
1941 if (old_sha1) {
1942 int save_errno = errno;
0568c8e9 1943 strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
6294dcb4
JK
1944 errno = save_errno;
1945 return -1;
1946 } else {
1947 hashclr(lock->old_oid.hash);
1948 return 0;
1949 }
7bd9bcf3 1950 }
6294dcb4 1951 if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
0568c8e9 1952 strbuf_addf(err, "ref '%s' is at %s but expected %s",
7bd9bcf3
MH
1953 lock->ref_name,
1954 sha1_to_hex(lock->old_oid.hash),
1955 sha1_to_hex(old_sha1));
1956 errno = EBUSY;
1957 return -1;
1958 }
1959 return 0;
1960}
1961
1962static int remove_empty_directories(struct strbuf *path)
1963{
1964 /*
1965 * we want to create a file but there is a directory there;
1966 * if that is an empty directory (or a directory that contains
1967 * only empty directories), remove them.
1968 */
1969 return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1970}
1971
1972/*
1973 * Locks a ref returning the lock on success and NULL on failure.
1974 * On failure errno is set to something meaningful.
1975 */
1976static struct ref_lock *lock_ref_sha1_basic(const char *refname,
1977 const unsigned char *old_sha1,
1978 const struct string_list *extras,
1979 const struct string_list *skip,
bcb497d0 1980 unsigned int flags, int *type,
7bd9bcf3
MH
1981 struct strbuf *err)
1982{
1983 struct strbuf ref_file = STRBUF_INIT;
7bd9bcf3
MH
1984 struct ref_lock *lock;
1985 int last_errno = 0;
7a418f3a 1986 int lflags = LOCK_NO_DEREF;
7bd9bcf3 1987 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
7a418f3a 1988 int resolve_flags = RESOLVE_REF_NO_RECURSE;
7bd9bcf3 1989 int attempts_remaining = 3;
7a418f3a 1990 int resolved;
7bd9bcf3
MH
1991
1992 assert(err);
1993
1994 lock = xcalloc(1, sizeof(struct ref_lock));
1995
1996 if (mustexist)
1997 resolve_flags |= RESOLVE_REF_READING;
2859dcd4 1998 if (flags & REF_DELETING)
7bd9bcf3 1999 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
7bd9bcf3 2000
7a418f3a
MH
2001 strbuf_git_path(&ref_file, "%s", refname);
2002 resolved = !!resolve_ref_unsafe(refname, resolve_flags,
2003 lock->old_oid.hash, type);
2004 if (!resolved && errno == EISDIR) {
7bd9bcf3
MH
2005 /*
2006 * we are trying to lock foo but we used to
2007 * have foo/bar which now does not exist;
2008 * it is normal for the empty directory 'foo'
2009 * to remain.
2010 */
7a418f3a 2011 if (remove_empty_directories(&ref_file)) {
7bd9bcf3 2012 last_errno = errno;
7a418f3a 2013 if (!verify_refname_available_dir(refname, extras, skip,
7bd9bcf3
MH
2014 get_loose_refs(&ref_cache), err))
2015 strbuf_addf(err, "there are still refs under '%s'",
7a418f3a 2016 refname);
7bd9bcf3
MH
2017 goto error_return;
2018 }
7a418f3a
MH
2019 resolved = !!resolve_ref_unsafe(refname, resolve_flags,
2020 lock->old_oid.hash, type);
7bd9bcf3 2021 }
7a418f3a 2022 if (!resolved) {
7bd9bcf3
MH
2023 last_errno = errno;
2024 if (last_errno != ENOTDIR ||
7a418f3a 2025 !verify_refname_available_dir(refname, extras, skip,
7bd9bcf3 2026 get_loose_refs(&ref_cache), err))
0568c8e9 2027 strbuf_addf(err, "unable to resolve reference '%s': %s",
7a418f3a 2028 refname, strerror(last_errno));
7bd9bcf3
MH
2029
2030 goto error_return;
2031 }
2859dcd4 2032
7bd9bcf3
MH
2033 /*
2034 * If the ref did not exist and we are creating it, make sure
2035 * there is no existing packed ref whose name begins with our
2036 * refname, nor a packed ref whose name is a proper prefix of
2037 * our refname.
2038 */
2039 if (is_null_oid(&lock->old_oid) &&
2040 verify_refname_available_dir(refname, extras, skip,
2041 get_packed_refs(&ref_cache), err)) {
2042 last_errno = ENOTDIR;
2043 goto error_return;
2044 }
2045
2046 lock->lk = xcalloc(1, sizeof(struct lock_file));
2047
7bd9bcf3 2048 lock->ref_name = xstrdup(refname);
7bd9bcf3
MH
2049
2050 retry:
2051 switch (safe_create_leading_directories_const(ref_file.buf)) {
2052 case SCLD_OK:
2053 break; /* success */
2054 case SCLD_VANISHED:
2055 if (--attempts_remaining > 0)
2056 goto retry;
2057 /* fall through */
2058 default:
2059 last_errno = errno;
0568c8e9 2060 strbuf_addf(err, "unable to create directory for '%s'",
7bd9bcf3
MH
2061 ref_file.buf);
2062 goto error_return;
2063 }
2064
2065 if (hold_lock_file_for_update(lock->lk, ref_file.buf, lflags) < 0) {
2066 last_errno = errno;
2067 if (errno == ENOENT && --attempts_remaining > 0)
2068 /*
2069 * Maybe somebody just deleted one of the
2070 * directories leading to ref_file. Try
2071 * again:
2072 */
2073 goto retry;
2074 else {
2075 unable_to_lock_message(ref_file.buf, errno, err);
2076 goto error_return;
2077 }
2078 }
6294dcb4 2079 if (verify_lock(lock, old_sha1, mustexist, err)) {
7bd9bcf3
MH
2080 last_errno = errno;
2081 goto error_return;
2082 }
2083 goto out;
2084
2085 error_return:
2086 unlock_ref(lock);
2087 lock = NULL;
2088
2089 out:
2090 strbuf_release(&ref_file);
7bd9bcf3
MH
2091 errno = last_errno;
2092 return lock;
2093}
2094
2095/*
2096 * Write an entry to the packed-refs file for the specified refname.
2097 * If peeled is non-NULL, write it as the entry's peeled value.
2098 */
2099static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
2100 unsigned char *peeled)
2101{
2102 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
2103 if (peeled)
2104 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
2105}
2106
2107/*
2108 * An each_ref_entry_fn that writes the entry to a packed-refs file.
2109 */
2110static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
2111{
2112 enum peel_status peel_status = peel_entry(entry, 0);
2113
2114 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
2115 error("internal error: %s is not a valid packed reference!",
2116 entry->name);
2117 write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
2118 peel_status == PEEL_PEELED ?
2119 entry->u.value.peeled.hash : NULL);
2120 return 0;
2121}
2122
2123/*
2124 * Lock the packed-refs file for writing. Flags is passed to
2125 * hold_lock_file_for_update(). Return 0 on success. On errors, set
2126 * errno appropriately and return a nonzero value.
2127 */
2128static int lock_packed_refs(int flags)
2129{
2130 static int timeout_configured = 0;
2131 static int timeout_value = 1000;
2132
2133 struct packed_ref_cache *packed_ref_cache;
2134
2135 if (!timeout_configured) {
2136 git_config_get_int("core.packedrefstimeout", &timeout_value);
2137 timeout_configured = 1;
2138 }
2139
2140 if (hold_lock_file_for_update_timeout(
2141 &packlock, git_path("packed-refs"),
2142 flags, timeout_value) < 0)
2143 return -1;
2144 /*
2145 * Get the current packed-refs while holding the lock. If the
2146 * packed-refs file has been modified since we last read it,
2147 * this will automatically invalidate the cache and re-read
2148 * the packed-refs file.
2149 */
2150 packed_ref_cache = get_packed_ref_cache(&ref_cache);
2151 packed_ref_cache->lock = &packlock;
2152 /* Increment the reference count to prevent it from being freed: */
2153 acquire_packed_ref_cache(packed_ref_cache);
2154 return 0;
2155}
2156
2157/*
2158 * Write the current version of the packed refs cache from memory to
2159 * disk. The packed-refs file must already be locked for writing (see
2160 * lock_packed_refs()). Return zero on success. On errors, set errno
2161 * and return a nonzero value
2162 */
2163static int commit_packed_refs(void)
2164{
2165 struct packed_ref_cache *packed_ref_cache =
2166 get_packed_ref_cache(&ref_cache);
2167 int error = 0;
2168 int save_errno = 0;
2169 FILE *out;
2170
2171 if (!packed_ref_cache->lock)
2172 die("internal error: packed-refs not locked");
2173
2174 out = fdopen_lock_file(packed_ref_cache->lock, "w");
2175 if (!out)
2176 die_errno("unable to fdopen packed-refs descriptor");
2177
2178 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
2179 do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
2180 0, write_packed_entry_fn, out);
2181
2182 if (commit_lock_file(packed_ref_cache->lock)) {
2183 save_errno = errno;
2184 error = -1;
2185 }
2186 packed_ref_cache->lock = NULL;
2187 release_packed_ref_cache(packed_ref_cache);
2188 errno = save_errno;
2189 return error;
2190}
2191
2192/*
2193 * Rollback the lockfile for the packed-refs file, and discard the
2194 * in-memory packed reference cache. (The packed-refs file will be
2195 * read anew if it is needed again after this function is called.)
2196 */
2197static void rollback_packed_refs(void)
2198{
2199 struct packed_ref_cache *packed_ref_cache =
2200 get_packed_ref_cache(&ref_cache);
2201
2202 if (!packed_ref_cache->lock)
2203 die("internal error: packed-refs not locked");
2204 rollback_lock_file(packed_ref_cache->lock);
2205 packed_ref_cache->lock = NULL;
2206 release_packed_ref_cache(packed_ref_cache);
2207 clear_packed_ref_cache(&ref_cache);
2208}
2209
2210struct ref_to_prune {
2211 struct ref_to_prune *next;
2212 unsigned char sha1[20];
2213 char name[FLEX_ARRAY];
2214};
2215
2216struct pack_refs_cb_data {
2217 unsigned int flags;
2218 struct ref_dir *packed_refs;
2219 struct ref_to_prune *ref_to_prune;
2220};
2221
2222/*
2223 * An each_ref_entry_fn that is run over loose references only. If
2224 * the loose reference can be packed, add an entry in the packed ref
2225 * cache. If the reference should be pruned, also add it to
2226 * ref_to_prune in the pack_refs_cb_data.
2227 */
2228static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
2229{
2230 struct pack_refs_cb_data *cb = cb_data;
2231 enum peel_status peel_status;
2232 struct ref_entry *packed_entry;
2233 int is_tag_ref = starts_with(entry->name, "refs/tags/");
2234
2235 /* Do not pack per-worktree refs: */
2236 if (ref_type(entry->name) != REF_TYPE_NORMAL)
2237 return 0;
2238
2239 /* ALWAYS pack tags */
2240 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
2241 return 0;
2242
2243 /* Do not pack symbolic or broken refs: */
ffeef642 2244 if ((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))
7bd9bcf3
MH
2245 return 0;
2246
2247 /* Add a packed ref cache entry equivalent to the loose entry. */
2248 peel_status = peel_entry(entry, 1);
2249 if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
2250 die("internal error peeling reference %s (%s)",
2251 entry->name, oid_to_hex(&entry->u.value.oid));
2252 packed_entry = find_ref(cb->packed_refs, entry->name);
2253 if (packed_entry) {
2254 /* Overwrite existing packed entry with info from loose entry */
2255 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
2256 oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);
2257 } else {
2258 packed_entry = create_ref_entry(entry->name, entry->u.value.oid.hash,
2259 REF_ISPACKED | REF_KNOWS_PEELED, 0);
2260 add_ref(cb->packed_refs, packed_entry);
2261 }
2262 oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);
2263
2264 /* Schedule the loose reference for pruning if requested. */
2265 if ((cb->flags & PACK_REFS_PRUNE)) {
96ffc06f
JK
2266 struct ref_to_prune *n;
2267 FLEX_ALLOC_STR(n, name, entry->name);
7bd9bcf3 2268 hashcpy(n->sha1, entry->u.value.oid.hash);
7bd9bcf3
MH
2269 n->next = cb->ref_to_prune;
2270 cb->ref_to_prune = n;
2271 }
2272 return 0;
2273}
2274
2275/*
2276 * Remove empty parents, but spare refs/ and immediate subdirs.
2277 * Note: munges *name.
2278 */
2279static void try_remove_empty_parents(char *name)
2280{
2281 char *p, *q;
2282 int i;
2283 p = name;
2284 for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
2285 while (*p && *p != '/')
2286 p++;
2287 /* tolerate duplicate slashes; see check_refname_format() */
2288 while (*p == '/')
2289 p++;
2290 }
2291 for (q = p; *q; q++)
2292 ;
2293 while (1) {
2294 while (q > p && *q != '/')
2295 q--;
2296 while (q > p && *(q-1) == '/')
2297 q--;
2298 if (q == p)
2299 break;
2300 *q = '\0';
2301 if (rmdir(git_path("%s", name)))
2302 break;
2303 }
2304}
2305
2306/* make sure nobody touched the ref, and unlink */
2307static void prune_ref(struct ref_to_prune *r)
2308{
2309 struct ref_transaction *transaction;
2310 struct strbuf err = STRBUF_INIT;
2311
2312 if (check_refname_format(r->name, 0))
2313 return;
2314
2315 transaction = ref_transaction_begin(&err);
2316 if (!transaction ||
2317 ref_transaction_delete(transaction, r->name, r->sha1,
c52ce248 2318 REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
7bd9bcf3
MH
2319 ref_transaction_commit(transaction, &err)) {
2320 ref_transaction_free(transaction);
2321 error("%s", err.buf);
2322 strbuf_release(&err);
2323 return;
2324 }
2325 ref_transaction_free(transaction);
2326 strbuf_release(&err);
2327 try_remove_empty_parents(r->name);
2328}
2329
2330static void prune_refs(struct ref_to_prune *r)
2331{
2332 while (r) {
2333 prune_ref(r);
2334 r = r->next;
2335 }
2336}
2337
2338int pack_refs(unsigned int flags)
2339{
2340 struct pack_refs_cb_data cbdata;
2341
2342 memset(&cbdata, 0, sizeof(cbdata));
2343 cbdata.flags = flags;
2344
2345 lock_packed_refs(LOCK_DIE_ON_ERROR);
2346 cbdata.packed_refs = get_packed_refs(&ref_cache);
2347
2348 do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,
2349 pack_if_possible_fn, &cbdata);
2350
2351 if (commit_packed_refs())
2352 die_errno("unable to overwrite old ref-pack file");
2353
2354 prune_refs(cbdata.ref_to_prune);
2355 return 0;
2356}
2357
2358/*
2359 * Rewrite the packed-refs file, omitting any refs listed in
2360 * 'refnames'. On error, leave packed-refs unchanged, write an error
2361 * message to 'err', and return a nonzero value.
2362 *
2363 * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
2364 */
2365static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
2366{
2367 struct ref_dir *packed;
2368 struct string_list_item *refname;
2369 int ret, needs_repacking = 0, removed = 0;
2370
2371 assert(err);
2372
2373 /* Look for a packed ref */
2374 for_each_string_list_item(refname, refnames) {
2375 if (get_packed_ref(refname->string)) {
2376 needs_repacking = 1;
2377 break;
2378 }
2379 }
2380
2381 /* Avoid locking if we have nothing to do */
2382 if (!needs_repacking)
2383 return 0; /* no refname exists in packed refs */
2384
2385 if (lock_packed_refs(0)) {
2386 unable_to_lock_message(git_path("packed-refs"), errno, err);
2387 return -1;
2388 }
2389 packed = get_packed_refs(&ref_cache);
2390
2391 /* Remove refnames from the cache */
2392 for_each_string_list_item(refname, refnames)
2393 if (remove_entry(packed, refname->string) != -1)
2394 removed = 1;
2395 if (!removed) {
2396 /*
2397 * All packed entries disappeared while we were
2398 * acquiring the lock.
2399 */
2400 rollback_packed_refs();
2401 return 0;
2402 }
2403
2404 /* Write what remains */
2405 ret = commit_packed_refs();
2406 if (ret)
2407 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
2408 strerror(errno));
2409 return ret;
2410}
2411
2412static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
2413{
2414 assert(err);
2415
2416 if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
2417 /*
2418 * loose. The loose file name is the same as the
2419 * lockfile name, minus ".lock":
2420 */
2421 char *loose_filename = get_locked_file_path(lock->lk);
2422 int res = unlink_or_msg(loose_filename, err);
2423 free(loose_filename);
2424 if (res)
2425 return 1;
2426 }
2427 return 0;
2428}
2429
c5f04ddd 2430int delete_refs(struct string_list *refnames, unsigned int flags)
7bd9bcf3
MH
2431{
2432 struct strbuf err = STRBUF_INIT;
2433 int i, result = 0;
2434
2435 if (!refnames->nr)
2436 return 0;
2437
2438 result = repack_without_refs(refnames, &err);
2439 if (result) {
2440 /*
2441 * If we failed to rewrite the packed-refs file, then
2442 * it is unsafe to try to remove loose refs, because
2443 * doing so might expose an obsolete packed value for
2444 * a reference that might even point at an object that
2445 * has been garbage collected.
2446 */
2447 if (refnames->nr == 1)
2448 error(_("could not delete reference %s: %s"),
2449 refnames->items[0].string, err.buf);
2450 else
2451 error(_("could not delete references: %s"), err.buf);
2452
2453 goto out;
2454 }
2455
2456 for (i = 0; i < refnames->nr; i++) {
2457 const char *refname = refnames->items[i].string;
2458
c5f04ddd 2459 if (delete_ref(refname, NULL, flags))
7bd9bcf3
MH
2460 result |= error(_("could not remove reference %s"), refname);
2461 }
2462
2463out:
2464 strbuf_release(&err);
2465 return result;
2466}
2467
2468/*
2469 * People using contrib's git-new-workdir have .git/logs/refs ->
2470 * /some/other/path/.git/logs/refs, and that may live on another device.
2471 *
2472 * IOW, to avoid cross device rename errors, the temporary renamed log must
2473 * live into logs/refs.
2474 */
2475#define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
2476
2477static int rename_tmp_log(const char *newrefname)
2478{
2479 int attempts_remaining = 4;
2480 struct strbuf path = STRBUF_INIT;
2481 int ret = -1;
2482
2483 retry:
2484 strbuf_reset(&path);
2485 strbuf_git_path(&path, "logs/%s", newrefname);
2486 switch (safe_create_leading_directories_const(path.buf)) {
2487 case SCLD_OK:
2488 break; /* success */
2489 case SCLD_VANISHED:
2490 if (--attempts_remaining > 0)
2491 goto retry;
2492 /* fall through */
2493 default:
2494 error("unable to create directory for %s", newrefname);
2495 goto out;
2496 }
2497
2498 if (rename(git_path(TMP_RENAMED_LOG), path.buf)) {
2499 if ((errno==EISDIR || errno==ENOTDIR) && --attempts_remaining > 0) {
2500 /*
2501 * rename(a, b) when b is an existing
2502 * directory ought to result in ISDIR, but
2503 * Solaris 5.8 gives ENOTDIR. Sheesh.
2504 */
2505 if (remove_empty_directories(&path)) {
2506 error("Directory not empty: logs/%s", newrefname);
2507 goto out;
2508 }
2509 goto retry;
2510 } else if (errno == ENOENT && --attempts_remaining > 0) {
2511 /*
2512 * Maybe another process just deleted one of
2513 * the directories in the path to newrefname.
2514 * Try again from the beginning.
2515 */
2516 goto retry;
2517 } else {
2518 error("unable to move logfile "TMP_RENAMED_LOG" to logs/%s: %s",
2519 newrefname, strerror(errno));
2520 goto out;
2521 }
2522 }
2523 ret = 0;
2524out:
2525 strbuf_release(&path);
2526 return ret;
2527}
2528
2529int verify_refname_available(const char *newname,
3a8af7be
MH
2530 const struct string_list *extras,
2531 const struct string_list *skip,
7bd9bcf3
MH
2532 struct strbuf *err)
2533{
2534 struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
2535 struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
2536
2537 if (verify_refname_available_dir(newname, extras, skip,
2538 packed_refs, err) ||
2539 verify_refname_available_dir(newname, extras, skip,
2540 loose_refs, err))
2541 return -1;
2542
2543 return 0;
2544}
2545
7bd9bcf3
MH
2546static int write_ref_to_lockfile(struct ref_lock *lock,
2547 const unsigned char *sha1, struct strbuf *err);
2548static int commit_ref_update(struct ref_lock *lock,
2549 const unsigned char *sha1, const char *logmsg,
5d9b2de4 2550 struct strbuf *err);
7bd9bcf3
MH
2551
2552int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
2553{
2554 unsigned char sha1[20], orig_sha1[20];
2555 int flag = 0, logmoved = 0;
2556 struct ref_lock *lock;
2557 struct stat loginfo;
2558 int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
7bd9bcf3
MH
2559 struct strbuf err = STRBUF_INIT;
2560
2561 if (log && S_ISLNK(loginfo.st_mode))
2562 return error("reflog for %s is a symlink", oldrefname);
2563
12fd3496
DT
2564 if (!resolve_ref_unsafe(oldrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2565 orig_sha1, &flag))
e711b1af
MH
2566 return error("refname %s not found", oldrefname);
2567
7bd9bcf3
MH
2568 if (flag & REF_ISSYMREF)
2569 return error("refname %s is a symbolic ref, renaming it is not supported",
2570 oldrefname);
7bd9bcf3
MH
2571 if (!rename_ref_available(oldrefname, newrefname))
2572 return 1;
2573
2574 if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG)))
2575 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
2576 oldrefname, strerror(errno));
2577
2578 if (delete_ref(oldrefname, orig_sha1, REF_NODEREF)) {
2579 error("unable to delete old %s", oldrefname);
2580 goto rollback;
2581 }
2582
12fd3496
DT
2583 /*
2584 * Since we are doing a shallow lookup, sha1 is not the
2585 * correct value to pass to delete_ref as old_sha1. But that
2586 * doesn't matter, because an old_sha1 check wouldn't add to
2587 * the safety anyway; we want to delete the reference whatever
2588 * its current value.
2589 */
2590 if (!read_ref_full(newrefname, RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
2591 sha1, NULL) &&
2592 delete_ref(newrefname, NULL, REF_NODEREF)) {
7bd9bcf3
MH
2593 if (errno==EISDIR) {
2594 struct strbuf path = STRBUF_INIT;
2595 int result;
2596
2597 strbuf_git_path(&path, "%s", newrefname);
2598 result = remove_empty_directories(&path);
2599 strbuf_release(&path);
2600
2601 if (result) {
2602 error("Directory not empty: %s", newrefname);
2603 goto rollback;
2604 }
2605 } else {
2606 error("unable to delete existing %s", newrefname);
2607 goto rollback;
2608 }
2609 }
2610
2611 if (log && rename_tmp_log(newrefname))
2612 goto rollback;
2613
2614 logmoved = log;
2615
12fd3496
DT
2616 lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, REF_NODEREF,
2617 NULL, &err);
7bd9bcf3
MH
2618 if (!lock) {
2619 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
2620 strbuf_release(&err);
2621 goto rollback;
2622 }
2623 hashcpy(lock->old_oid.hash, orig_sha1);
2624
2625 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
5d9b2de4 2626 commit_ref_update(lock, orig_sha1, logmsg, &err)) {
7bd9bcf3
MH
2627 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
2628 strbuf_release(&err);
2629 goto rollback;
2630 }
2631
2632 return 0;
2633
2634 rollback:
12fd3496
DT
2635 lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, REF_NODEREF,
2636 NULL, &err);
7bd9bcf3
MH
2637 if (!lock) {
2638 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
2639 strbuf_release(&err);
2640 goto rollbacklog;
2641 }
2642
2643 flag = log_all_ref_updates;
2644 log_all_ref_updates = 0;
2645 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
5d9b2de4 2646 commit_ref_update(lock, orig_sha1, NULL, &err)) {
7bd9bcf3
MH
2647 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
2648 strbuf_release(&err);
2649 }
2650 log_all_ref_updates = flag;
2651
2652 rollbacklog:
2653 if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
2654 error("unable to restore logfile %s from %s: %s",
2655 oldrefname, newrefname, strerror(errno));
2656 if (!logmoved && log &&
2657 rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
2658 error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
2659 oldrefname, strerror(errno));
2660
2661 return 1;
2662}
2663
2664static int close_ref(struct ref_lock *lock)
2665{
2666 if (close_lock_file(lock->lk))
2667 return -1;
2668 return 0;
2669}
2670
2671static int commit_ref(struct ref_lock *lock)
2672{
5387c0d8
MH
2673 char *path = get_locked_file_path(lock->lk);
2674 struct stat st;
2675
2676 if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
2677 /*
2678 * There is a directory at the path we want to rename
2679 * the lockfile to. Hopefully it is empty; try to
2680 * delete it.
2681 */
2682 size_t len = strlen(path);
2683 struct strbuf sb_path = STRBUF_INIT;
2684
2685 strbuf_attach(&sb_path, path, len, len);
2686
2687 /*
2688 * If this fails, commit_lock_file() will also fail
2689 * and will report the problem.
2690 */
2691 remove_empty_directories(&sb_path);
2692 strbuf_release(&sb_path);
2693 } else {
2694 free(path);
2695 }
2696
7bd9bcf3
MH
2697 if (commit_lock_file(lock->lk))
2698 return -1;
2699 return 0;
2700}
2701
2702/*
2703 * Create a reflog for a ref. If force_create = 0, the reflog will
2704 * only be created for certain refs (those for which
2705 * should_autocreate_reflog returns non-zero. Otherwise, create it
2706 * regardless of the ref name. Fill in *err and return -1 on failure.
2707 */
2708static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
2709{
2710 int logfd, oflags = O_APPEND | O_WRONLY;
2711
2712 strbuf_git_path(logfile, "logs/%s", refname);
2713 if (force_create || should_autocreate_reflog(refname)) {
2714 if (safe_create_leading_directories(logfile->buf) < 0) {
0568c8e9 2715 strbuf_addf(err, "unable to create directory for '%s': "
7bd9bcf3
MH
2716 "%s", logfile->buf, strerror(errno));
2717 return -1;
2718 }
2719 oflags |= O_CREAT;
2720 }
2721
2722 logfd = open(logfile->buf, oflags, 0666);
2723 if (logfd < 0) {
2724 if (!(oflags & O_CREAT) && (errno == ENOENT || errno == EISDIR))
2725 return 0;
2726
2727 if (errno == EISDIR) {
2728 if (remove_empty_directories(logfile)) {
0568c8e9 2729 strbuf_addf(err, "there are still logs under "
7bd9bcf3
MH
2730 "'%s'", logfile->buf);
2731 return -1;
2732 }
2733 logfd = open(logfile->buf, oflags, 0666);
2734 }
2735
2736 if (logfd < 0) {
0568c8e9 2737 strbuf_addf(err, "unable to append to '%s': %s",
7bd9bcf3
MH
2738 logfile->buf, strerror(errno));
2739 return -1;
2740 }
2741 }
2742
2743 adjust_shared_perm(logfile->buf);
2744 close(logfd);
2745 return 0;
2746}
2747
2748
2749int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
2750{
2751 int ret;
2752 struct strbuf sb = STRBUF_INIT;
2753
2754 ret = log_ref_setup(refname, &sb, err, force_create);
2755 strbuf_release(&sb);
2756 return ret;
2757}
2758
2759static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2760 const unsigned char *new_sha1,
2761 const char *committer, const char *msg)
2762{
2763 int msglen, written;
2764 unsigned maxlen, len;
2765 char *logrec;
2766
2767 msglen = msg ? strlen(msg) : 0;
2768 maxlen = strlen(committer) + msglen + 100;
2769 logrec = xmalloc(maxlen);
2770 len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2771 sha1_to_hex(old_sha1),
2772 sha1_to_hex(new_sha1),
2773 committer);
2774 if (msglen)
2775 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2776
2777 written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2778 free(logrec);
2779 if (written != len)
2780 return -1;
2781
2782 return 0;
2783}
2784
2785static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
2786 const unsigned char *new_sha1, const char *msg,
2787 struct strbuf *logfile, int flags,
2788 struct strbuf *err)
2789{
2790 int logfd, result, oflags = O_APPEND | O_WRONLY;
2791
2792 if (log_all_ref_updates < 0)
2793 log_all_ref_updates = !is_bare_repository();
2794
2795 result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
2796
2797 if (result)
2798 return result;
2799
2800 logfd = open(logfile->buf, oflags);
2801 if (logfd < 0)
2802 return 0;
2803 result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2804 git_committer_info(0), msg);
2805 if (result) {
0568c8e9 2806 strbuf_addf(err, "unable to append to '%s': %s", logfile->buf,
7bd9bcf3
MH
2807 strerror(errno));
2808 close(logfd);
2809 return -1;
2810 }
2811 if (close(logfd)) {
0568c8e9 2812 strbuf_addf(err, "unable to append to '%s': %s", logfile->buf,
7bd9bcf3
MH
2813 strerror(errno));
2814 return -1;
2815 }
2816 return 0;
2817}
2818
2819static int log_ref_write(const char *refname, const unsigned char *old_sha1,
2820 const unsigned char *new_sha1, const char *msg,
2821 int flags, struct strbuf *err)
5f3c3a4e
DT
2822{
2823 return files_log_ref_write(refname, old_sha1, new_sha1, msg, flags,
2824 err);
2825}
2826
2827int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
2828 const unsigned char *new_sha1, const char *msg,
2829 int flags, struct strbuf *err)
7bd9bcf3
MH
2830{
2831 struct strbuf sb = STRBUF_INIT;
2832 int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
2833 err);
2834 strbuf_release(&sb);
2835 return ret;
2836}
2837
2838/*
2839 * Write sha1 into the open lockfile, then close the lockfile. On
2840 * errors, rollback the lockfile, fill in *err and
2841 * return -1.
2842 */
2843static int write_ref_to_lockfile(struct ref_lock *lock,
2844 const unsigned char *sha1, struct strbuf *err)
2845{
2846 static char term = '\n';
2847 struct object *o;
2848 int fd;
2849
2850 o = parse_object(sha1);
2851 if (!o) {
2852 strbuf_addf(err,
0568c8e9 2853 "trying to write ref '%s' with nonexistent object %s",
7bd9bcf3
MH
2854 lock->ref_name, sha1_to_hex(sha1));
2855 unlock_ref(lock);
2856 return -1;
2857 }
2858 if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2859 strbuf_addf(err,
0568c8e9 2860 "trying to write non-commit object %s to branch '%s'",
7bd9bcf3
MH
2861 sha1_to_hex(sha1), lock->ref_name);
2862 unlock_ref(lock);
2863 return -1;
2864 }
2865 fd = get_lock_file_fd(lock->lk);
2866 if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2867 write_in_full(fd, &term, 1) != 1 ||
2868 close_ref(lock) < 0) {
2869 strbuf_addf(err,
0568c8e9 2870 "couldn't write '%s'", get_lock_file_path(lock->lk));
7bd9bcf3
MH
2871 unlock_ref(lock);
2872 return -1;
2873 }
2874 return 0;
2875}
2876
2877/*
2878 * Commit a change to a loose reference that has already been written
2879 * to the loose reference lockfile. Also update the reflogs if
2880 * necessary, using the specified lockmsg (which can be NULL).
2881 */
2882static int commit_ref_update(struct ref_lock *lock,
2883 const unsigned char *sha1, const char *logmsg,
5d9b2de4 2884 struct strbuf *err)
7bd9bcf3
MH
2885{
2886 clear_loose_ref_cache(&ref_cache);
7a418f3a 2887 if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
7bd9bcf3 2888 char *old_msg = strbuf_detach(err, NULL);
0568c8e9 2889 strbuf_addf(err, "cannot update the ref '%s': %s",
7bd9bcf3
MH
2890 lock->ref_name, old_msg);
2891 free(old_msg);
2892 unlock_ref(lock);
2893 return -1;
2894 }
7a418f3a
MH
2895
2896 if (strcmp(lock->ref_name, "HEAD") != 0) {
7bd9bcf3
MH
2897 /*
2898 * Special hack: If a branch is updated directly and HEAD
2899 * points to it (may happen on the remote side of a push
2900 * for example) then logically the HEAD reflog should be
2901 * updated too.
2902 * A generic solution implies reverse symref information,
2903 * but finding all symrefs pointing to the given branch
2904 * would be rather costly for this rare event (the direct
2905 * update of a branch) to be worth it. So let's cheat and
2906 * check with HEAD only which should cover 99% of all usage
2907 * scenarios (even 100% of the default ones).
2908 */
2909 unsigned char head_sha1[20];
2910 int head_flag;
2911 const char *head_ref;
7a418f3a 2912
7bd9bcf3
MH
2913 head_ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
2914 head_sha1, &head_flag);
2915 if (head_ref && (head_flag & REF_ISSYMREF) &&
2916 !strcmp(head_ref, lock->ref_name)) {
2917 struct strbuf log_err = STRBUF_INIT;
2918 if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
2919 logmsg, 0, &log_err)) {
2920 error("%s", log_err.buf);
2921 strbuf_release(&log_err);
2922 }
2923 }
2924 }
7a418f3a 2925
5d9b2de4 2926 if (commit_ref(lock)) {
0568c8e9 2927 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
7bd9bcf3
MH
2928 unlock_ref(lock);
2929 return -1;
2930 }
2931
2932 unlock_ref(lock);
2933 return 0;
2934}
2935
370e5ad6 2936static int create_ref_symlink(struct ref_lock *lock, const char *target)
7bd9bcf3 2937{
370e5ad6 2938 int ret = -1;
7bd9bcf3 2939#ifndef NO_SYMLINK_HEAD
370e5ad6
JK
2940 char *ref_path = get_locked_file_path(lock->lk);
2941 unlink(ref_path);
2942 ret = symlink(target, ref_path);
2943 free(ref_path);
2944
2945 if (ret)
7bd9bcf3 2946 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
7bd9bcf3 2947#endif
370e5ad6
JK
2948 return ret;
2949}
7bd9bcf3 2950
370e5ad6
JK
2951static void update_symref_reflog(struct ref_lock *lock, const char *refname,
2952 const char *target, const char *logmsg)
2953{
2954 struct strbuf err = STRBUF_INIT;
2955 unsigned char new_sha1[20];
b9badadd 2956 if (logmsg && !read_ref(target, new_sha1) &&
370e5ad6 2957 log_ref_write(refname, lock->old_oid.hash, new_sha1, logmsg, 0, &err)) {
7bd9bcf3
MH
2958 error("%s", err.buf);
2959 strbuf_release(&err);
2960 }
370e5ad6 2961}
7bd9bcf3 2962
370e5ad6
JK
2963static int create_symref_locked(struct ref_lock *lock, const char *refname,
2964 const char *target, const char *logmsg)
2965{
2966 if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
2967 update_symref_reflog(lock, refname, target, logmsg);
2968 return 0;
2969 }
2970
2971 if (!fdopen_lock_file(lock->lk, "w"))
2972 return error("unable to fdopen %s: %s",
2973 lock->lk->tempfile.filename.buf, strerror(errno));
2974
396da8f7
JK
2975 update_symref_reflog(lock, refname, target, logmsg);
2976
370e5ad6
JK
2977 /* no error check; commit_ref will check ferror */
2978 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2979 if (commit_ref(lock) < 0)
2980 return error("unable to write symref for %s: %s", refname,
2981 strerror(errno));
7bd9bcf3
MH
2982 return 0;
2983}
2984
370e5ad6
JK
2985int create_symref(const char *refname, const char *target, const char *logmsg)
2986{
2987 struct strbuf err = STRBUF_INIT;
2988 struct ref_lock *lock;
2989 int ret;
2990
2991 lock = lock_ref_sha1_basic(refname, NULL, NULL, NULL, REF_NODEREF, NULL,
2992 &err);
2993 if (!lock) {
2994 error("%s", err.buf);
2995 strbuf_release(&err);
2996 return -1;
2997 }
2998
2999 ret = create_symref_locked(lock, refname, target, logmsg);
3000 unlock_ref(lock);
3001 return ret;
3002}
3003
2233066e
KY
3004int set_worktree_head_symref(const char *gitdir, const char *target)
3005{
3006 static struct lock_file head_lock;
3007 struct ref_lock *lock;
2233066e
KY
3008 struct strbuf head_path = STRBUF_INIT;
3009 const char *head_rel;
3010 int ret;
3011
3012 strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
3013 if (hold_lock_file_for_update(&head_lock, head_path.buf,
3014 LOCK_NO_DEREF) < 0) {
18eb3a9c
KY
3015 struct strbuf err = STRBUF_INIT;
3016 unable_to_lock_message(head_path.buf, errno, &err);
2233066e
KY
3017 error("%s", err.buf);
3018 strbuf_release(&err);
3019 strbuf_release(&head_path);
3020 return -1;
3021 }
3022
3023 /* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
3024 linked trees */
3025 head_rel = remove_leading_path(head_path.buf,
3026 absolute_path(get_git_common_dir()));
3027 /* to make use of create_symref_locked(), initialize ref_lock */
3028 lock = xcalloc(1, sizeof(struct ref_lock));
3029 lock->lk = &head_lock;
3030 lock->ref_name = xstrdup(head_rel);
2233066e
KY
3031
3032 ret = create_symref_locked(lock, head_rel, target, NULL);
3033
3034 unlock_ref(lock); /* will free lock */
3035 strbuf_release(&head_path);
3036 return ret;
3037}
3038
7bd9bcf3
MH
3039int reflog_exists(const char *refname)
3040{
3041 struct stat st;
3042
3043 return !lstat(git_path("logs/%s", refname), &st) &&
3044 S_ISREG(st.st_mode);
3045}
3046
3047int delete_reflog(const char *refname)
3048{
3049 return remove_path(git_path("logs/%s", refname));
3050}
3051
3052static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
3053{
3054 unsigned char osha1[20], nsha1[20];
3055 char *email_end, *message;
3056 unsigned long timestamp;
3057 int tz;
3058
3059 /* old SP new SP name <email> SP time TAB msg LF */
3060 if (sb->len < 83 || sb->buf[sb->len - 1] != '\n' ||
3061 get_sha1_hex(sb->buf, osha1) || sb->buf[40] != ' ' ||
3062 get_sha1_hex(sb->buf + 41, nsha1) || sb->buf[81] != ' ' ||
3063 !(email_end = strchr(sb->buf + 82, '>')) ||
3064 email_end[1] != ' ' ||
3065 !(timestamp = strtoul(email_end + 2, &message, 10)) ||
3066 !message || message[0] != ' ' ||
3067 (message[1] != '+' && message[1] != '-') ||
3068 !isdigit(message[2]) || !isdigit(message[3]) ||
3069 !isdigit(message[4]) || !isdigit(message[5]))
3070 return 0; /* corrupt? */
3071 email_end[1] = '\0';
3072 tz = strtol(message + 1, NULL, 10);
3073 if (message[6] != '\t')
3074 message += 6;
3075 else
3076 message += 7;
3077 return fn(osha1, nsha1, sb->buf + 82, timestamp, tz, message, cb_data);
3078}
3079
3080static char *find_beginning_of_line(char *bob, char *scan)
3081{
3082 while (bob < scan && *(--scan) != '\n')
3083 ; /* keep scanning backwards */
3084 /*
3085 * Return either beginning of the buffer, or LF at the end of
3086 * the previous line.
3087 */
3088 return scan;
3089}
3090
3091int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)
3092{
3093 struct strbuf sb = STRBUF_INIT;
3094 FILE *logfp;
3095 long pos;
3096 int ret = 0, at_tail = 1;
3097
3098 logfp = fopen(git_path("logs/%s", refname), "r");
3099 if (!logfp)
3100 return -1;
3101
3102 /* Jump to the end */
3103 if (fseek(logfp, 0, SEEK_END) < 0)
3104 return error("cannot seek back reflog for %s: %s",
3105 refname, strerror(errno));
3106 pos = ftell(logfp);
3107 while (!ret && 0 < pos) {
3108 int cnt;
3109 size_t nread;
3110 char buf[BUFSIZ];
3111 char *endp, *scanp;
3112
3113 /* Fill next block from the end */
3114 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
3115 if (fseek(logfp, pos - cnt, SEEK_SET))
3116 return error("cannot seek back reflog for %s: %s",
3117 refname, strerror(errno));
3118 nread = fread(buf, cnt, 1, logfp);
3119 if (nread != 1)
3120 return error("cannot read %d bytes from reflog for %s: %s",
3121 cnt, refname, strerror(errno));
3122 pos -= cnt;
3123
3124 scanp = endp = buf + cnt;
3125 if (at_tail && scanp[-1] == '\n')
3126 /* Looking at the final LF at the end of the file */
3127 scanp--;
3128 at_tail = 0;
3129
3130 while (buf < scanp) {
3131 /*
3132 * terminating LF of the previous line, or the beginning
3133 * of the buffer.
3134 */
3135 char *bp;
3136
3137 bp = find_beginning_of_line(buf, scanp);
3138
3139 if (*bp == '\n') {
3140 /*
3141 * The newline is the end of the previous line,
3142 * so we know we have complete line starting
3143 * at (bp + 1). Prefix it onto any prior data
3144 * we collected for the line and process it.
3145 */
3146 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
3147 scanp = bp;
3148 endp = bp + 1;
3149 ret = show_one_reflog_ent(&sb, fn, cb_data);
3150 strbuf_reset(&sb);
3151 if (ret)
3152 break;
3153 } else if (!pos) {
3154 /*
3155 * We are at the start of the buffer, and the
3156 * start of the file; there is no previous
3157 * line, and we have everything for this one.
3158 * Process it, and we can end the loop.
3159 */
3160 strbuf_splice(&sb, 0, 0, buf, endp - buf);
3161 ret = show_one_reflog_ent(&sb, fn, cb_data);
3162 strbuf_reset(&sb);
3163 break;
3164 }
3165
3166 if (bp == buf) {
3167 /*
3168 * We are at the start of the buffer, and there
3169 * is more file to read backwards. Which means
3170 * we are in the middle of a line. Note that we
3171 * may get here even if *bp was a newline; that
3172 * just means we are at the exact end of the
3173 * previous line, rather than some spot in the
3174 * middle.
3175 *
3176 * Save away what we have to be combined with
3177 * the data from the next read.
3178 */
3179 strbuf_splice(&sb, 0, 0, buf, endp - buf);
3180 break;
3181 }
3182 }
3183
3184 }
3185 if (!ret && sb.len)
3186 die("BUG: reverse reflog parser had leftover data");
3187
3188 fclose(logfp);
3189 strbuf_release(&sb);
3190 return ret;
3191}
3192
3193int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
3194{
3195 FILE *logfp;
3196 struct strbuf sb = STRBUF_INIT;
3197 int ret = 0;
3198
3199 logfp = fopen(git_path("logs/%s", refname), "r");
3200 if (!logfp)
3201 return -1;
3202
3203 while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
3204 ret = show_one_reflog_ent(&sb, fn, cb_data);
3205 fclose(logfp);
3206 strbuf_release(&sb);
3207 return ret;
3208}
3209/*
3210 * Call fn for each reflog in the namespace indicated by name. name
3211 * must be empty or end with '/'. Name will be used as a scratch
3212 * space, but its contents will be restored before return.
3213 */
3214static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
3215{
3216 DIR *d = opendir(git_path("logs/%s", name->buf));
3217 int retval = 0;
3218 struct dirent *de;
3219 int oldlen = name->len;
3220
3221 if (!d)
3222 return name->len ? errno : 0;
3223
3224 while ((de = readdir(d)) != NULL) {
3225 struct stat st;
3226
3227 if (de->d_name[0] == '.')
3228 continue;
3229 if (ends_with(de->d_name, ".lock"))
3230 continue;
3231 strbuf_addstr(name, de->d_name);
3232 if (stat(git_path("logs/%s", name->buf), &st) < 0) {
3233 ; /* silently ignore */
3234 } else {
3235 if (S_ISDIR(st.st_mode)) {
3236 strbuf_addch(name, '/');
3237 retval = do_for_each_reflog(name, fn, cb_data);
3238 } else {
3239 struct object_id oid;
3240
3241 if (read_ref_full(name->buf, 0, oid.hash, NULL))
3242 retval = error("bad ref for %s", name->buf);
3243 else
3244 retval = fn(name->buf, &oid, 0, cb_data);
3245 }
3246 if (retval)
3247 break;
3248 }
3249 strbuf_setlen(name, oldlen);
3250 }
3251 closedir(d);
3252 return retval;
3253}
3254
3255int for_each_reflog(each_ref_fn fn, void *cb_data)
3256{
3257 int retval;
3258 struct strbuf name;
3259 strbuf_init(&name, PATH_MAX);
3260 retval = do_for_each_reflog(&name, fn, cb_data);
3261 strbuf_release(&name);
3262 return retval;
3263}
3264
3265static int ref_update_reject_duplicates(struct string_list *refnames,
3266 struct strbuf *err)
3267{
3268 int i, n = refnames->nr;
3269
3270 assert(err);
3271
3272 for (i = 1; i < n; i++)
3273 if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
3274 strbuf_addf(err,
0568c8e9 3275 "multiple updates for ref '%s' not allowed.",
7bd9bcf3
MH
3276 refnames->items[i].string);
3277 return 1;
3278 }
3279 return 0;
3280}
3281
165056b2 3282/*
92b1551b
MH
3283 * If update is a direct update of head_ref (the reference pointed to
3284 * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
3285 */
3286static int split_head_update(struct ref_update *update,
3287 struct ref_transaction *transaction,
3288 const char *head_ref,
3289 struct string_list *affected_refnames,
3290 struct strbuf *err)
3291{
3292 struct string_list_item *item;
3293 struct ref_update *new_update;
3294
3295 if ((update->flags & REF_LOG_ONLY) ||
3296 (update->flags & REF_ISPRUNING) ||
3297 (update->flags & REF_UPDATE_VIA_HEAD))
3298 return 0;
3299
3300 if (strcmp(update->refname, head_ref))
3301 return 0;
3302
3303 /*
3304 * First make sure that HEAD is not already in the
3305 * transaction. This insertion is O(N) in the transaction
3306 * size, but it happens at most once per transaction.
3307 */
3308 item = string_list_insert(affected_refnames, "HEAD");
3309 if (item->util) {
3310 /* An entry already existed */
3311 strbuf_addf(err,
3312 "multiple updates for 'HEAD' (including one "
3313 "via its referent '%s') are not allowed",
3314 update->refname);
3315 return TRANSACTION_NAME_CONFLICT;
3316 }
3317
3318 new_update = ref_transaction_add_update(
3319 transaction, "HEAD",
3320 update->flags | REF_LOG_ONLY | REF_NODEREF,
3321 update->new_sha1, update->old_sha1,
3322 update->msg);
3323
3324 item->util = new_update;
3325
3326 return 0;
3327}
3328
3329/*
3330 * update is for a symref that points at referent and doesn't have
3331 * REF_NODEREF set. Split it into two updates:
3332 * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
3333 * - A new, separate update for the referent reference
3334 * Note that the new update will itself be subject to splitting when
3335 * the iteration gets to it.
3336 */
3337static int split_symref_update(struct ref_update *update,
3338 const char *referent,
3339 struct ref_transaction *transaction,
3340 struct string_list *affected_refnames,
3341 struct strbuf *err)
3342{
3343 struct string_list_item *item;
3344 struct ref_update *new_update;
3345 unsigned int new_flags;
3346
3347 /*
3348 * First make sure that referent is not already in the
3349 * transaction. This insertion is O(N) in the transaction
3350 * size, but it happens at most once per symref in a
3351 * transaction.
3352 */
3353 item = string_list_insert(affected_refnames, referent);
3354 if (item->util) {
3355 /* An entry already existed */
3356 strbuf_addf(err,
3357 "multiple updates for '%s' (including one "
3358 "via symref '%s') are not allowed",
3359 referent, update->refname);
3360 return TRANSACTION_NAME_CONFLICT;
3361 }
3362
3363 new_flags = update->flags;
3364 if (!strcmp(update->refname, "HEAD")) {
3365 /*
3366 * Record that the new update came via HEAD, so that
3367 * when we process it, split_head_update() doesn't try
3368 * to add another reflog update for HEAD. Note that
3369 * this bit will be propagated if the new_update
3370 * itself needs to be split.
3371 */
3372 new_flags |= REF_UPDATE_VIA_HEAD;
3373 }
3374
3375 new_update = ref_transaction_add_update(
3376 transaction, referent, new_flags,
3377 update->new_sha1, update->old_sha1,
3378 update->msg);
3379
6e30b2f6
MH
3380 new_update->parent_update = update;
3381
3382 /*
3383 * Change the symbolic ref update to log only. Also, it
3384 * doesn't need to check its old SHA-1 value, as that will be
3385 * done when new_update is processed.
3386 */
92b1551b 3387 update->flags |= REF_LOG_ONLY | REF_NODEREF;
6e30b2f6 3388 update->flags &= ~REF_HAVE_OLD;
92b1551b
MH
3389
3390 item->util = new_update;
3391
3392 return 0;
3393}
3394
6e30b2f6
MH
3395/*
3396 * Return the refname under which update was originally requested.
3397 */
3398static const char *original_update_refname(struct ref_update *update)
3399{
3400 while (update->parent_update)
3401 update = update->parent_update;
3402
3403 return update->refname;
3404}
3405
92b1551b
MH
3406/*
3407 * Prepare for carrying out update:
3408 * - Lock the reference referred to by update.
3409 * - Read the reference under lock.
3410 * - Check that its old SHA-1 value (if specified) is correct, and in
3411 * any case record it in update->lock->old_oid for later use when
3412 * writing the reflog.
3413 * - If it is a symref update without REF_NODEREF, split it up into a
3414 * REF_LOG_ONLY update of the symref and add a separate update for
3415 * the referent to transaction.
3416 * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
3417 * update of HEAD.
165056b2
MH
3418 */
3419static int lock_ref_for_update(struct ref_update *update,
3420 struct ref_transaction *transaction,
92b1551b 3421 const char *head_ref,
165056b2
MH
3422 struct string_list *affected_refnames,
3423 struct strbuf *err)
3424{
92b1551b
MH
3425 struct strbuf referent = STRBUF_INIT;
3426 int mustexist = (update->flags & REF_HAVE_OLD) &&
3427 !is_null_sha1(update->old_sha1);
165056b2 3428 int ret;
92b1551b 3429 struct ref_lock *lock;
165056b2 3430
92b1551b 3431 if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
165056b2 3432 update->flags |= REF_DELETING;
92b1551b
MH
3433
3434 if (head_ref) {
3435 ret = split_head_update(update, transaction, head_ref,
3436 affected_refnames, err);
3437 if (ret)
3438 return ret;
3439 }
3440
3441 ret = lock_raw_ref(update->refname, mustexist,
3442 affected_refnames, NULL,
3443 &update->lock, &referent,
3444 &update->type, err);
3445
3446 if (ret) {
165056b2
MH
3447 char *reason;
3448
165056b2
MH
3449 reason = strbuf_detach(err, NULL);
3450 strbuf_addf(err, "cannot lock ref '%s': %s",
3451 update->refname, reason);
3452 free(reason);
3453 return ret;
3454 }
92b1551b
MH
3455
3456 lock = update->lock;
3457
8169d0d0 3458 if (update->type & REF_ISSYMREF) {
6e30b2f6
MH
3459 if (update->flags & REF_NODEREF) {
3460 /*
3461 * We won't be reading the referent as part of
3462 * the transaction, so we have to read it here
3463 * to record and possibly check old_sha1:
3464 */
3465 if (read_ref_full(update->refname,
3466 mustexist ? RESOLVE_REF_READING : 0,
3467 lock->old_oid.hash, NULL)) {
3468 if (update->flags & REF_HAVE_OLD) {
3469 strbuf_addf(err, "cannot lock ref '%s': "
3470 "can't resolve old value",
3471 update->refname);
3472 return TRANSACTION_GENERIC_ERROR;
3473 } else {
3474 hashclr(lock->old_oid.hash);
3475 }
3476 }
3477 if ((update->flags & REF_HAVE_OLD) &&
3478 hashcmp(lock->old_oid.hash, update->old_sha1)) {
3479 strbuf_addf(err, "cannot lock ref '%s': "
3480 "is at %s but expected %s",
3481 update->refname,
3482 sha1_to_hex(lock->old_oid.hash),
3483 sha1_to_hex(update->old_sha1));
8169d0d0 3484 return TRANSACTION_GENERIC_ERROR;
8169d0d0 3485 }
92b1551b 3486
6e30b2f6
MH
3487 } else {
3488 /*
3489 * Create a new update for the reference this
3490 * symref is pointing at. Also, we will record
3491 * and verify old_sha1 for this update as part
3492 * of processing the split-off update, so we
3493 * don't have to do it here.
3494 */
92b1551b
MH
3495 ret = split_symref_update(update, referent.buf, transaction,
3496 affected_refnames, err);
3497 if (ret)
3498 return ret;
3499 }
6e30b2f6
MH
3500 } else {
3501 struct ref_update *parent_update;
8169d0d0 3502
6e30b2f6
MH
3503 /*
3504 * If this update is happening indirectly because of a
3505 * symref update, record the old SHA-1 in the parent
3506 * update:
3507 */
3508 for (parent_update = update->parent_update;
3509 parent_update;
3510 parent_update = parent_update->parent_update) {
3511 oidcpy(&parent_update->lock->old_oid, &lock->old_oid);
3512 }
3513
3514 if ((update->flags & REF_HAVE_OLD) &&
3515 hashcmp(lock->old_oid.hash, update->old_sha1)) {
3516 if (is_null_sha1(update->old_sha1))
3517 strbuf_addf(err, "cannot lock ref '%s': reference already exists",
3518 original_update_refname(update));
3519 else
3520 strbuf_addf(err, "cannot lock ref '%s': is at %s but expected %s",
3521 original_update_refname(update),
3522 sha1_to_hex(lock->old_oid.hash),
3523 sha1_to_hex(update->old_sha1));
3524
3525 return TRANSACTION_GENERIC_ERROR;
3526 }
92b1551b
MH
3527 }
3528
165056b2
MH
3529 if ((update->flags & REF_HAVE_NEW) &&
3530 !(update->flags & REF_DELETING) &&
3531 !(update->flags & REF_LOG_ONLY)) {
92b1551b
MH
3532 if (!(update->type & REF_ISSYMREF) &&
3533 !hashcmp(lock->old_oid.hash, update->new_sha1)) {
165056b2
MH
3534 /*
3535 * The reference already has the desired
3536 * value, so we don't need to write it.
3537 */
92b1551b 3538 } else if (write_ref_to_lockfile(lock, update->new_sha1,
165056b2
MH
3539 err)) {
3540 char *write_err = strbuf_detach(err, NULL);
3541
3542 /*
3543 * The lock was freed upon failure of
3544 * write_ref_to_lockfile():
3545 */
3546 update->lock = NULL;
3547 strbuf_addf(err,
3548 "cannot update the ref '%s': %s",
3549 update->refname, write_err);
3550 free(write_err);
3551 return TRANSACTION_GENERIC_ERROR;
3552 } else {
3553 update->flags |= REF_NEEDS_COMMIT;
3554 }
3555 }
3556 if (!(update->flags & REF_NEEDS_COMMIT)) {
3557 /*
3558 * We didn't call write_ref_to_lockfile(), so
3559 * the lockfile is still open. Close it to
3560 * free up the file descriptor:
3561 */
92b1551b 3562 if (close_ref(lock)) {
165056b2
MH
3563 strbuf_addf(err, "couldn't close '%s.lock'",
3564 update->refname);
3565 return TRANSACTION_GENERIC_ERROR;
3566 }
3567 }
3568 return 0;
3569}
3570
7bd9bcf3
MH
3571int ref_transaction_commit(struct ref_transaction *transaction,
3572 struct strbuf *err)
3573{
3574 int ret = 0, i;
7bd9bcf3
MH
3575 struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
3576 struct string_list_item *ref_to_delete;
3577 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
92b1551b
MH
3578 char *head_ref = NULL;
3579 int head_type;
3580 struct object_id head_oid;
7bd9bcf3
MH
3581
3582 assert(err);
3583
3584 if (transaction->state != REF_TRANSACTION_OPEN)
3585 die("BUG: commit called for transaction that is not open");
3586
efe47281 3587 if (!transaction->nr) {
7bd9bcf3
MH
3588 transaction->state = REF_TRANSACTION_CLOSED;
3589 return 0;
3590 }
3591
92b1551b
MH
3592 /*
3593 * Fail if a refname appears more than once in the
3594 * transaction. (If we end up splitting up any updates using
3595 * split_symref_update() or split_head_update(), those
3596 * functions will check that the new updates don't have the
3597 * same refname as any existing ones.)
3598 */
3599 for (i = 0; i < transaction->nr; i++) {
3600 struct ref_update *update = transaction->updates[i];
3601 struct string_list_item *item =
3602 string_list_append(&affected_refnames, update->refname);
3603
3604 /*
3605 * We store a pointer to update in item->util, but at
3606 * the moment we never use the value of this field
3607 * except to check whether it is non-NULL.
3608 */
3609 item->util = update;
3610 }
7bd9bcf3
MH
3611 string_list_sort(&affected_refnames);
3612 if (ref_update_reject_duplicates(&affected_refnames, err)) {
3613 ret = TRANSACTION_GENERIC_ERROR;
3614 goto cleanup;
3615 }
3616
92b1551b
MH
3617 /*
3618 * Special hack: If a branch is updated directly and HEAD
3619 * points to it (may happen on the remote side of a push
3620 * for example) then logically the HEAD reflog should be
3621 * updated too.
3622 *
3623 * A generic solution would require reverse symref lookups,
3624 * but finding all symrefs pointing to a given branch would be
3625 * rather costly for this rare event (the direct update of a
3626 * branch) to be worth it. So let's cheat and check with HEAD
3627 * only, which should cover 99% of all usage scenarios (even
3628 * 100% of the default ones).
3629 *
3630 * So if HEAD is a symbolic reference, then record the name of
3631 * the reference that it points to. If we see an update of
3632 * head_ref within the transaction, then split_head_update()
3633 * arranges for the reflog of HEAD to be updated, too.
3634 */
3635 head_ref = resolve_refdup("HEAD", RESOLVE_REF_NO_RECURSE,
3636 head_oid.hash, &head_type);
3637
3638 if (head_ref && !(head_type & REF_ISSYMREF)) {
3639 free(head_ref);
3640 head_ref = NULL;
3641 }
3642
7bd9bcf3
MH
3643 /*
3644 * Acquire all locks, verify old values if provided, check
3645 * that new values are valid, and write new values to the
3646 * lockfiles, ready to be activated. Only keep one lockfile
3647 * open at a time to avoid running out of file descriptors.
3648 */
efe47281
MH
3649 for (i = 0; i < transaction->nr; i++) {
3650 struct ref_update *update = transaction->updates[i];
7bd9bcf3 3651
92b1551b 3652 ret = lock_ref_for_update(update, transaction, head_ref,
165056b2
MH
3653 &affected_refnames, err);
3654 if (ret)
7bd9bcf3 3655 goto cleanup;
7bd9bcf3
MH
3656 }
3657
3658 /* Perform updates first so live commits remain referenced */
efe47281
MH
3659 for (i = 0; i < transaction->nr; i++) {
3660 struct ref_update *update = transaction->updates[i];
92b1551b 3661 struct ref_lock *lock = update->lock;
7bd9bcf3 3662
d99aa884
DT
3663 if (update->flags & REF_NEEDS_COMMIT ||
3664 update->flags & REF_LOG_ONLY) {
92b1551b
MH
3665 if (log_ref_write(lock->ref_name, lock->old_oid.hash,
3666 update->new_sha1,
3667 update->msg, update->flags, err)) {
3668 char *old_msg = strbuf_detach(err, NULL);
3669
3670 strbuf_addf(err, "cannot update the ref '%s': %s",
3671 lock->ref_name, old_msg);
3672 free(old_msg);
3673 unlock_ref(lock);
7bd9bcf3
MH
3674 update->lock = NULL;
3675 ret = TRANSACTION_GENERIC_ERROR;
3676 goto cleanup;
92b1551b
MH
3677 }
3678 }
3679 if (update->flags & REF_NEEDS_COMMIT) {
3680 clear_loose_ref_cache(&ref_cache);
3681 if (commit_ref(lock)) {
3682 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3683 unlock_ref(lock);
7bd9bcf3 3684 update->lock = NULL;
92b1551b
MH
3685 ret = TRANSACTION_GENERIC_ERROR;
3686 goto cleanup;
7bd9bcf3
MH
3687 }
3688 }
3689 }
7bd9bcf3 3690 /* Perform deletes now that updates are safely completed */
efe47281
MH
3691 for (i = 0; i < transaction->nr; i++) {
3692 struct ref_update *update = transaction->updates[i];
7bd9bcf3 3693
d99aa884
DT
3694 if (update->flags & REF_DELETING &&
3695 !(update->flags & REF_LOG_ONLY)) {
7bd9bcf3
MH
3696 if (delete_ref_loose(update->lock, update->type, err)) {
3697 ret = TRANSACTION_GENERIC_ERROR;
3698 goto cleanup;
3699 }
3700
3701 if (!(update->flags & REF_ISPRUNING))
3702 string_list_append(&refs_to_delete,
3703 update->lock->ref_name);
3704 }
3705 }
3706
3707 if (repack_without_refs(&refs_to_delete, err)) {
3708 ret = TRANSACTION_GENERIC_ERROR;
3709 goto cleanup;
3710 }
3711 for_each_string_list_item(ref_to_delete, &refs_to_delete)
3712 unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
3713 clear_loose_ref_cache(&ref_cache);
3714
3715cleanup:
3716 transaction->state = REF_TRANSACTION_CLOSED;
3717
efe47281
MH
3718 for (i = 0; i < transaction->nr; i++)
3719 if (transaction->updates[i]->lock)
3720 unlock_ref(transaction->updates[i]->lock);
7bd9bcf3 3721 string_list_clear(&refs_to_delete, 0);
92b1551b 3722 free(head_ref);
7bd9bcf3 3723 string_list_clear(&affected_refnames, 0);
92b1551b 3724
7bd9bcf3
MH
3725 return ret;
3726}
3727
3728static int ref_present(const char *refname,
3729 const struct object_id *oid, int flags, void *cb_data)
3730{
3731 struct string_list *affected_refnames = cb_data;
3732
3733 return string_list_has_string(affected_refnames, refname);
3734}
3735
3736int initial_ref_transaction_commit(struct ref_transaction *transaction,
3737 struct strbuf *err)
3738{
3739 int ret = 0, i;
7bd9bcf3
MH
3740 struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3741
3742 assert(err);
3743
3744 if (transaction->state != REF_TRANSACTION_OPEN)
3745 die("BUG: commit called for transaction that is not open");
3746
3747 /* Fail if a refname appears more than once in the transaction: */
efe47281
MH
3748 for (i = 0; i < transaction->nr; i++)
3749 string_list_append(&affected_refnames,
3750 transaction->updates[i]->refname);
7bd9bcf3
MH
3751 string_list_sort(&affected_refnames);
3752 if (ref_update_reject_duplicates(&affected_refnames, err)) {
3753 ret = TRANSACTION_GENERIC_ERROR;
3754 goto cleanup;
3755 }
3756
3757 /*
3758 * It's really undefined to call this function in an active
3759 * repository or when there are existing references: we are
3760 * only locking and changing packed-refs, so (1) any
3761 * simultaneous processes might try to change a reference at
3762 * the same time we do, and (2) any existing loose versions of
3763 * the references that we are setting would have precedence
3764 * over our values. But some remote helpers create the remote
3765 * "HEAD" and "master" branches before calling this function,
3766 * so here we really only check that none of the references
3767 * that we are creating already exists.
3768 */
3769 if (for_each_rawref(ref_present, &affected_refnames))
3770 die("BUG: initial ref transaction called with existing refs");
3771
efe47281
MH
3772 for (i = 0; i < transaction->nr; i++) {
3773 struct ref_update *update = transaction->updates[i];
7bd9bcf3
MH
3774
3775 if ((update->flags & REF_HAVE_OLD) &&
3776 !is_null_sha1(update->old_sha1))
3777 die("BUG: initial ref transaction with old_sha1 set");
3778 if (verify_refname_available(update->refname,
3779 &affected_refnames, NULL,
3780 err)) {
3781 ret = TRANSACTION_NAME_CONFLICT;
3782 goto cleanup;
3783 }
3784 }
3785
3786 if (lock_packed_refs(0)) {
3787 strbuf_addf(err, "unable to lock packed-refs file: %s",
3788 strerror(errno));
3789 ret = TRANSACTION_GENERIC_ERROR;
3790 goto cleanup;
3791 }
3792
efe47281
MH
3793 for (i = 0; i < transaction->nr; i++) {
3794 struct ref_update *update = transaction->updates[i];
7bd9bcf3
MH
3795
3796 if ((update->flags & REF_HAVE_NEW) &&
3797 !is_null_sha1(update->new_sha1))
3798 add_packed_ref(update->refname, update->new_sha1);
3799 }
3800
3801 if (commit_packed_refs()) {
3802 strbuf_addf(err, "unable to commit packed-refs file: %s",
3803 strerror(errno));
3804 ret = TRANSACTION_GENERIC_ERROR;
3805 goto cleanup;
3806 }
3807
3808cleanup:
3809 transaction->state = REF_TRANSACTION_CLOSED;
3810 string_list_clear(&affected_refnames, 0);
3811 return ret;
3812}
3813
3814struct expire_reflog_cb {
3815 unsigned int flags;
3816 reflog_expiry_should_prune_fn *should_prune_fn;
3817 void *policy_cb;
3818 FILE *newlog;
3819 unsigned char last_kept_sha1[20];
3820};
3821
3822static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
3823 const char *email, unsigned long timestamp, int tz,
3824 const char *message, void *cb_data)
3825{
3826 struct expire_reflog_cb *cb = cb_data;
3827 struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3828
3829 if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3830 osha1 = cb->last_kept_sha1;
3831
3832 if ((*cb->should_prune_fn)(osha1, nsha1, email, timestamp, tz,
3833 message, policy_cb)) {
3834 if (!cb->newlog)
3835 printf("would prune %s", message);
3836 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3837 printf("prune %s", message);
3838 } else {
3839 if (cb->newlog) {
3840 fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
3841 sha1_to_hex(osha1), sha1_to_hex(nsha1),
3842 email, timestamp, tz, message);
3843 hashcpy(cb->last_kept_sha1, nsha1);
3844 }
3845 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3846 printf("keep %s", message);
3847 }
3848 return 0;
3849}
3850
3851int reflog_expire(const char *refname, const unsigned char *sha1,
3852 unsigned int flags,
3853 reflog_expiry_prepare_fn prepare_fn,
3854 reflog_expiry_should_prune_fn should_prune_fn,
3855 reflog_expiry_cleanup_fn cleanup_fn,
3856 void *policy_cb_data)
3857{
3858 static struct lock_file reflog_lock;
3859 struct expire_reflog_cb cb;
3860 struct ref_lock *lock;
3861 char *log_file;
3862 int status = 0;
3863 int type;
3864 struct strbuf err = STRBUF_INIT;
3865
3866 memset(&cb, 0, sizeof(cb));
3867 cb.flags = flags;
3868 cb.policy_cb = policy_cb_data;
3869 cb.should_prune_fn = should_prune_fn;
3870
3871 /*
3872 * The reflog file is locked by holding the lock on the
3873 * reference itself, plus we might need to update the
3874 * reference if --updateref was specified:
3875 */
41d796ed
DT
3876 lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, REF_NODEREF,
3877 &type, &err);
7bd9bcf3
MH
3878 if (!lock) {
3879 error("cannot lock ref '%s': %s", refname, err.buf);
3880 strbuf_release(&err);
3881 return -1;
3882 }
3883 if (!reflog_exists(refname)) {
3884 unlock_ref(lock);
3885 return 0;
3886 }
3887
3888 log_file = git_pathdup("logs/%s", refname);
3889 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3890 /*
3891 * Even though holding $GIT_DIR/logs/$reflog.lock has
3892 * no locking implications, we use the lock_file
3893 * machinery here anyway because it does a lot of the
3894 * work we need, including cleaning up if the program
3895 * exits unexpectedly.
3896 */
3897 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3898 struct strbuf err = STRBUF_INIT;
3899 unable_to_lock_message(log_file, errno, &err);
3900 error("%s", err.buf);
3901 strbuf_release(&err);
3902 goto failure;
3903 }
3904 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3905 if (!cb.newlog) {
3906 error("cannot fdopen %s (%s)",
3907 get_lock_file_path(&reflog_lock), strerror(errno));
3908 goto failure;
3909 }
3910 }
3911
3912 (*prepare_fn)(refname, sha1, cb.policy_cb);
3913 for_each_reflog_ent(refname, expire_reflog_ent, &cb);
3914 (*cleanup_fn)(cb.policy_cb);
3915
3916 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3917 /*
3918 * It doesn't make sense to adjust a reference pointed
3919 * to by a symbolic ref based on expiring entries in
3920 * the symbolic reference's reflog. Nor can we update
3921 * a reference if there are no remaining reflog
3922 * entries.
3923 */
3924 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3925 !(type & REF_ISSYMREF) &&
3926 !is_null_sha1(cb.last_kept_sha1);
3927
3928 if (close_lock_file(&reflog_lock)) {
3929 status |= error("couldn't write %s: %s", log_file,
3930 strerror(errno));
3931 } else if (update &&
3932 (write_in_full(get_lock_file_fd(lock->lk),
3933 sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
3934 write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3935 close_ref(lock) < 0)) {
3936 status |= error("couldn't write %s",
3937 get_lock_file_path(lock->lk));
3938 rollback_lock_file(&reflog_lock);
3939 } else if (commit_lock_file(&reflog_lock)) {
e0048d3e 3940 status |= error("unable to write reflog '%s' (%s)",
7bd9bcf3
MH
3941 log_file, strerror(errno));
3942 } else if (update && commit_ref(lock)) {
3943 status |= error("couldn't set %s", lock->ref_name);
3944 }
3945 }
3946 free(log_file);
3947 unlock_ref(lock);
3948 return status;
3949
3950 failure:
3951 rollback_lock_file(&reflog_lock);
3952 free(log_file);
3953