8 * Make sure "ref" is something reasonable to have under ".git/refs/";
9 * We do not like it if:
11 * - any path component of it begins with ".", or
12 * - it has double dots "..", or
13 * - it has ASCII control character, "~", "^", ":" or SP, anywhere, or
14 * - it ends with a "/".
15 * - it ends with ".lock"
16 * - it contains a "\" (backslash)
19 /* Return true iff ch is not allowed in reference names. */
20 static inline int bad_ref_char(int ch
)
22 if (((unsigned) ch
) <= ' ' || ch
== 0x7f ||
23 ch
== '~' || ch
== '^' || ch
== ':' || ch
== '\\')
25 /* 2.13 Pattern Matching Notation */
26 if (ch
== '*' || ch
== '?' || ch
== '[') /* Unsupported */
32 * Try to read one refname component from the front of refname. Return
33 * the length of the component found, or -1 if the component is not
36 static int check_refname_component(const char *refname
, int flags
)
41 for (cp
= refname
; ; cp
++) {
43 if (ch
== '\0' || ch
== '/')
46 return -1; /* Illegal character in refname. */
47 if (last
== '.' && ch
== '.')
48 return -1; /* Refname contains "..". */
49 if (last
== '@' && ch
== '{')
50 return -1; /* Refname contains "@{". */
54 return 0; /* Component has zero length. */
55 if (refname
[0] == '.') {
56 if (!(flags
& REFNAME_DOT_COMPONENT
))
57 return -1; /* Component starts with '.'. */
59 * Even if leading dots are allowed, don't allow "."
60 * as a component (".." is prevented by a rule above).
62 if (refname
[1] == '\0')
63 return -1; /* Component equals ".". */
65 if (cp
- refname
>= 5 && !memcmp(cp
- 5, ".lock", 5))
66 return -1; /* Refname ends with ".lock". */
70 int check_refname_format(const char *refname
, int flags
)
72 int component_len
, component_count
= 0;
75 /* We are at the start of a path component. */
76 component_len
= check_refname_component(refname
, flags
);
77 if (component_len
<= 0) {
78 if ((flags
& REFNAME_REFSPEC_PATTERN
) &&
80 (refname
[1] == '\0' || refname
[1] == '/')) {
81 /* Accept one wildcard as a full refname component. */
82 flags
&= ~REFNAME_REFSPEC_PATTERN
;
89 if (refname
[component_len
] == '\0')
91 /* Skip to next component. */
92 refname
+= component_len
+ 1;
95 if (refname
[component_len
- 1] == '.')
96 return -1; /* Refname ends with '.'. */
97 if (!(flags
& REFNAME_ALLOW_ONELEVEL
) && component_count
< 2)
98 return -1; /* Refname has only one component. */
105 * Information used (along with the information in ref_entry) to
106 * describe a single cached reference. This data structure only
107 * occurs embedded in a union in struct ref_entry, and only when
108 * (ref_entry->flag & REF_DIR) is zero.
111 unsigned char sha1
[20];
112 unsigned char peeled
[20];
118 * Information used (along with the information in ref_entry) to
119 * describe a level in the hierarchy of references. This data
120 * structure only occurs embedded in a union in struct ref_entry, and
121 * only when (ref_entry.flag & REF_DIR) is set. In that case,
122 * (ref_entry.flag & REF_INCOMPLETE) determines whether the references
123 * in the directory have already been read:
125 * (ref_entry.flag & REF_INCOMPLETE) unset -- a directory of loose
126 * or packed references, already read.
128 * (ref_entry.flag & REF_INCOMPLETE) set -- a directory of loose
129 * references that hasn't been read yet (nor has any of its
132 * Entries within a directory are stored within a growable array of
133 * pointers to ref_entries (entries, nr, alloc). Entries 0 <= i <
134 * sorted are sorted by their component name in strcmp() order and the
135 * remaining entries are unsorted.
137 * Loose references are read lazily, one directory at a time. When a
138 * directory of loose references is read, then all of the references
139 * in that directory are stored, and REF_INCOMPLETE stubs are created
140 * for any subdirectories, but the subdirectories themselves are not
141 * read. The reading is triggered by get_ref_dir().
147 * Entries with index 0 <= i < sorted are sorted by name. New
148 * entries are appended to the list unsorted, and are sorted
149 * only when required; thus we avoid the need to sort the list
150 * after the addition of every reference.
154 /* A pointer to the ref_cache that contains this ref_dir. */
155 struct ref_cache
*ref_cache
;
157 struct ref_entry
**entries
;
160 /* ISSYMREF=0x01, ISPACKED=0x02, and ISBROKEN=0x04 are public interfaces */
161 #define REF_KNOWS_PEELED 0x08
163 /* ref_entry represents a directory of references */
167 * Entry has not yet been read from disk (used only for REF_DIR
168 * entries representing loose references)
170 #define REF_INCOMPLETE 0x20
173 * A ref_entry represents either a reference or a "subdirectory" of
176 * Each directory in the reference namespace is represented by a
177 * ref_entry with (flags & REF_DIR) set and containing a subdir member
178 * that holds the entries in that directory that have been read so
179 * far. If (flags & REF_INCOMPLETE) is set, then the directory and
180 * its subdirectories haven't been read yet. REF_INCOMPLETE is only
181 * used for loose reference directories.
183 * References are represented by a ref_entry with (flags & REF_DIR)
184 * unset and a value member that describes the reference's value. The
185 * flag member is at the ref_entry level, but it is also needed to
186 * interpret the contents of the value field (in other words, a
187 * ref_value object is not very much use without the enclosing
190 * Reference names cannot end with slash and directories' names are
191 * always stored with a trailing slash (except for the top-level
192 * directory, which is always denoted by ""). This has two nice
193 * consequences: (1) when the entries in each subdir are sorted
194 * lexicographically by name (as they usually are), the references in
195 * a whole tree can be generated in lexicographic order by traversing
196 * the tree in left-to-right, depth-first order; (2) the names of
197 * references and subdirectories cannot conflict, and therefore the
198 * presence of an empty subdirectory does not block the creation of a
199 * similarly-named reference. (The fact that reference names with the
200 * same leading components can conflict *with each other* is a
201 * separate issue that is regulated by is_refname_available().)
203 * Please note that the name field contains the fully-qualified
204 * reference (or subdirectory) name. Space could be saved by only
205 * storing the relative names. But that would require the full names
206 * to be generated on the fly when iterating in do_for_each_ref(), and
207 * would break callback functions, who have always been able to assume
208 * that the name strings that they are passed will not be freed during
212 unsigned char flag
; /* ISSYMREF? ISPACKED? */
214 struct ref_value value
; /* if not (flags&REF_DIR) */
215 struct ref_dir subdir
; /* if (flags&REF_DIR) */
218 * The full name of the reference (e.g., "refs/heads/master")
219 * or the full name of the directory with a trailing slash
220 * (e.g., "refs/heads/"):
222 char name
[FLEX_ARRAY
];
225 static void read_loose_refs(const char *dirname
, struct ref_dir
*dir
);
227 static struct ref_dir
*get_ref_dir(struct ref_entry
*entry
)
230 assert(entry
->flag
& REF_DIR
);
231 dir
= &entry
->u
.subdir
;
232 if (entry
->flag
& REF_INCOMPLETE
) {
233 read_loose_refs(entry
->name
, dir
);
234 entry
->flag
&= ~REF_INCOMPLETE
;
239 static struct ref_entry
*create_ref_entry(const char *refname
,
240 const unsigned char *sha1
, int flag
,
244 struct ref_entry
*ref
;
247 check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
|REFNAME_DOT_COMPONENT
))
248 die("Reference has invalid format: '%s'", refname
);
249 len
= strlen(refname
) + 1;
250 ref
= xmalloc(sizeof(struct ref_entry
) + len
);
251 hashcpy(ref
->u
.value
.sha1
, sha1
);
252 hashclr(ref
->u
.value
.peeled
);
253 memcpy(ref
->name
, refname
, len
);
258 static void clear_ref_dir(struct ref_dir
*dir
);
260 static void free_ref_entry(struct ref_entry
*entry
)
262 if (entry
->flag
& REF_DIR
)
263 clear_ref_dir(get_ref_dir(entry
));
268 * Add a ref_entry to the end of dir (unsorted). Entry is always
269 * stored directly in dir; no recursion into subdirectories is
272 static void add_entry_to_dir(struct ref_dir
*dir
, struct ref_entry
*entry
)
274 ALLOC_GROW(dir
->entries
, dir
->nr
+ 1, dir
->alloc
);
275 dir
->entries
[dir
->nr
++] = entry
;
279 * Clear and free all entries in dir, recursively.
281 static void clear_ref_dir(struct ref_dir
*dir
)
284 for (i
= 0; i
< dir
->nr
; i
++)
285 free_ref_entry(dir
->entries
[i
]);
287 dir
->sorted
= dir
->nr
= dir
->alloc
= 0;
292 * Create a struct ref_entry object for the specified dirname.
293 * dirname is the name of the directory with a trailing slash (e.g.,
294 * "refs/heads/") or "" for the top-level directory.
296 static struct ref_entry
*create_dir_entry(struct ref_cache
*ref_cache
,
297 const char *dirname
, size_t len
,
300 struct ref_entry
*direntry
;
301 direntry
= xcalloc(1, sizeof(struct ref_entry
) + len
+ 1);
302 memcpy(direntry
->name
, dirname
, len
);
303 direntry
->name
[len
] = '\0';
304 direntry
->u
.subdir
.ref_cache
= ref_cache
;
305 direntry
->flag
= REF_DIR
| (incomplete
? REF_INCOMPLETE
: 0);
309 static int ref_entry_cmp(const void *a
, const void *b
)
311 struct ref_entry
*one
= *(struct ref_entry
**)a
;
312 struct ref_entry
*two
= *(struct ref_entry
**)b
;
313 return strcmp(one
->name
, two
->name
);
316 static void sort_ref_dir(struct ref_dir
*dir
);
319 * Return the entry with the given refname from the ref_dir
320 * (non-recursively), sorting dir if necessary. Return NULL if no
321 * such entry is found. dir must already be complete.
323 static struct ref_entry
*search_ref_dir(struct ref_dir
*dir
,
324 const char *refname
, size_t len
)
326 struct ref_entry
*e
, **r
;
328 if (refname
== NULL
|| !dir
->nr
)
333 e
= xmalloc(sizeof(struct ref_entry
) + len
+ 1);
334 memcpy(e
->name
, refname
, len
);
337 r
= bsearch(&e
, dir
->entries
, dir
->nr
, sizeof(*dir
->entries
), ref_entry_cmp
);
348 * Search for a directory entry directly within dir (without
349 * recursing). Sort dir if necessary. subdirname must be a directory
350 * name (i.e., end in '/'). If mkdir is set, then create the
351 * directory if it is missing; otherwise, return NULL if the desired
352 * directory cannot be found. dir must already be complete.
354 static struct ref_dir
*search_for_subdir(struct ref_dir
*dir
,
355 const char *subdirname
, size_t len
,
358 struct ref_entry
*entry
= search_ref_dir(dir
, subdirname
, len
);
363 * Since dir is complete, the absence of a subdir
364 * means that the subdir really doesn't exist;
365 * therefore, create an empty record for it but mark
366 * the record complete.
368 entry
= create_dir_entry(dir
->ref_cache
, subdirname
, len
, 0);
369 add_entry_to_dir(dir
, entry
);
371 return get_ref_dir(entry
);
375 * If refname is a reference name, find the ref_dir within the dir
376 * tree that should hold refname. If refname is a directory name
377 * (i.e., ends in '/'), then return that ref_dir itself. dir must
378 * represent the top-level directory and must already be complete.
379 * Sort ref_dirs and recurse into subdirectories as necessary. If
380 * mkdir is set, then create any missing directories; otherwise,
381 * return NULL if the desired directory cannot be found.
383 static struct ref_dir
*find_containing_dir(struct ref_dir
*dir
,
384 const char *refname
, int mkdir
)
387 for (slash
= strchr(refname
, '/'); slash
; slash
= strchr(slash
+ 1, '/')) {
388 size_t dirnamelen
= slash
- refname
+ 1;
389 struct ref_dir
*subdir
;
390 subdir
= search_for_subdir(dir
, refname
, dirnamelen
, mkdir
);
402 * Find the value entry with the given name in dir, sorting ref_dirs
403 * and recursing into subdirectories as necessary. If the name is not
404 * found or it corresponds to a directory entry, return NULL.
406 static struct ref_entry
*find_ref(struct ref_dir
*dir
, const char *refname
)
408 struct ref_entry
*entry
;
409 dir
= find_containing_dir(dir
, refname
, 0);
412 entry
= search_ref_dir(dir
, refname
, strlen(refname
));
413 return (entry
&& !(entry
->flag
& REF_DIR
)) ? entry
: NULL
;
417 * Add a ref_entry to the ref_dir (unsorted), recursing into
418 * subdirectories as necessary. dir must represent the top-level
419 * directory. Return 0 on success.
421 static int add_ref(struct ref_dir
*dir
, struct ref_entry
*ref
)
423 dir
= find_containing_dir(dir
, ref
->name
, 1);
426 add_entry_to_dir(dir
, ref
);
431 * Emit a warning and return true iff ref1 and ref2 have the same name
432 * and the same sha1. Die if they have the same name but different
435 static int is_dup_ref(const struct ref_entry
*ref1
, const struct ref_entry
*ref2
)
437 if (strcmp(ref1
->name
, ref2
->name
))
440 /* Duplicate name; make sure that they don't conflict: */
442 if ((ref1
->flag
& REF_DIR
) || (ref2
->flag
& REF_DIR
))
443 /* This is impossible by construction */
444 die("Reference directory conflict: %s", ref1
->name
);
446 if (hashcmp(ref1
->u
.value
.sha1
, ref2
->u
.value
.sha1
))
447 die("Duplicated ref, and SHA1s don't match: %s", ref1
->name
);
449 warning("Duplicated ref: %s", ref1
->name
);
454 * Sort the entries in dir non-recursively (if they are not already
455 * sorted) and remove any duplicate entries.
457 static void sort_ref_dir(struct ref_dir
*dir
)
460 struct ref_entry
*last
= NULL
;
463 * This check also prevents passing a zero-length array to qsort(),
464 * which is a problem on some platforms.
466 if (dir
->sorted
== dir
->nr
)
469 qsort(dir
->entries
, dir
->nr
, sizeof(*dir
->entries
), ref_entry_cmp
);
471 /* Remove any duplicates: */
472 for (i
= 0, j
= 0; j
< dir
->nr
; j
++) {
473 struct ref_entry
*entry
= dir
->entries
[j
];
474 if (last
&& is_dup_ref(last
, entry
))
475 free_ref_entry(entry
);
477 last
= dir
->entries
[i
++] = entry
;
479 dir
->sorted
= dir
->nr
= i
;
482 #define DO_FOR_EACH_INCLUDE_BROKEN 01
484 static struct ref_entry
*current_ref
;
486 static int do_one_ref(const char *base
, each_ref_fn fn
, int trim
,
487 int flags
, void *cb_data
, struct ref_entry
*entry
)
490 if (prefixcmp(entry
->name
, base
))
493 if (!(flags
& DO_FOR_EACH_INCLUDE_BROKEN
)) {
494 if (entry
->flag
& REF_ISBROKEN
)
495 return 0; /* ignore broken refs e.g. dangling symref */
496 if (!has_sha1_file(entry
->u
.value
.sha1
)) {
497 error("%s does not point to a valid object!", entry
->name
);
502 retval
= fn(entry
->name
+ trim
, entry
->u
.value
.sha1
, entry
->flag
, cb_data
);
508 * Call fn for each reference in dir that has index in the range
509 * offset <= index < dir->nr. Recurse into subdirectories that are in
510 * that index range, sorting them before iterating. This function
511 * does not sort dir itself; it should be sorted beforehand.
513 static int do_for_each_ref_in_dir(struct ref_dir
*dir
, int offset
,
515 each_ref_fn fn
, int trim
, int flags
, void *cb_data
)
518 assert(dir
->sorted
== dir
->nr
);
519 for (i
= offset
; i
< dir
->nr
; i
++) {
520 struct ref_entry
*entry
= dir
->entries
[i
];
522 if (entry
->flag
& REF_DIR
) {
523 struct ref_dir
*subdir
= get_ref_dir(entry
);
524 sort_ref_dir(subdir
);
525 retval
= do_for_each_ref_in_dir(subdir
, 0,
526 base
, fn
, trim
, flags
, cb_data
);
528 retval
= do_one_ref(base
, fn
, trim
, flags
, cb_data
, entry
);
537 * Call fn for each reference in the union of dir1 and dir2, in order
538 * by refname. Recurse into subdirectories. If a value entry appears
539 * in both dir1 and dir2, then only process the version that is in
540 * dir2. The input dirs must already be sorted, but subdirs will be
543 static int do_for_each_ref_in_dirs(struct ref_dir
*dir1
,
544 struct ref_dir
*dir2
,
545 const char *base
, each_ref_fn fn
, int trim
,
546 int flags
, void *cb_data
)
551 assert(dir1
->sorted
== dir1
->nr
);
552 assert(dir2
->sorted
== dir2
->nr
);
554 struct ref_entry
*e1
, *e2
;
556 if (i1
== dir1
->nr
) {
557 return do_for_each_ref_in_dir(dir2
, i2
,
558 base
, fn
, trim
, flags
, cb_data
);
560 if (i2
== dir2
->nr
) {
561 return do_for_each_ref_in_dir(dir1
, i1
,
562 base
, fn
, trim
, flags
, cb_data
);
564 e1
= dir1
->entries
[i1
];
565 e2
= dir2
->entries
[i2
];
566 cmp
= strcmp(e1
->name
, e2
->name
);
568 if ((e1
->flag
& REF_DIR
) && (e2
->flag
& REF_DIR
)) {
569 /* Both are directories; descend them in parallel. */
570 struct ref_dir
*subdir1
= get_ref_dir(e1
);
571 struct ref_dir
*subdir2
= get_ref_dir(e2
);
572 sort_ref_dir(subdir1
);
573 sort_ref_dir(subdir2
);
574 retval
= do_for_each_ref_in_dirs(
576 base
, fn
, trim
, flags
, cb_data
);
579 } else if (!(e1
->flag
& REF_DIR
) && !(e2
->flag
& REF_DIR
)) {
580 /* Both are references; ignore the one from dir1. */
581 retval
= do_one_ref(base
, fn
, trim
, flags
, cb_data
, e2
);
585 die("conflict between reference and directory: %s",
597 if (e
->flag
& REF_DIR
) {
598 struct ref_dir
*subdir
= get_ref_dir(e
);
599 sort_ref_dir(subdir
);
600 retval
= do_for_each_ref_in_dir(
602 base
, fn
, trim
, flags
, cb_data
);
604 retval
= do_one_ref(base
, fn
, trim
, flags
, cb_data
, e
);
611 return do_for_each_ref_in_dir(dir1
, i1
,
612 base
, fn
, trim
, flags
, cb_data
);
614 return do_for_each_ref_in_dir(dir2
, i2
,
615 base
, fn
, trim
, flags
, cb_data
);
620 * Return true iff refname1 and refname2 conflict with each other.
621 * Two reference names conflict if one of them exactly matches the
622 * leading components of the other; e.g., "foo/bar" conflicts with
623 * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
626 static int names_conflict(const char *refname1
, const char *refname2
)
628 for (; *refname1
&& *refname1
== *refname2
; refname1
++, refname2
++)
630 return (*refname1
== '\0' && *refname2
== '/')
631 || (*refname1
== '/' && *refname2
== '\0');
634 struct name_conflict_cb
{
636 const char *oldrefname
;
637 const char *conflicting_refname
;
640 static int name_conflict_fn(const char *existingrefname
, const unsigned char *sha1
,
641 int flags
, void *cb_data
)
643 struct name_conflict_cb
*data
= (struct name_conflict_cb
*)cb_data
;
644 if (data
->oldrefname
&& !strcmp(data
->oldrefname
, existingrefname
))
646 if (names_conflict(data
->refname
, existingrefname
)) {
647 data
->conflicting_refname
= existingrefname
;
654 * Return true iff a reference named refname could be created without
655 * conflicting with the name of an existing reference in array. If
656 * oldrefname is non-NULL, ignore potential conflicts with oldrefname
657 * (e.g., because oldrefname is scheduled for deletion in the same
660 static int is_refname_available(const char *refname
, const char *oldrefname
,
663 struct name_conflict_cb data
;
664 data
.refname
= refname
;
665 data
.oldrefname
= oldrefname
;
666 data
.conflicting_refname
= NULL
;
669 if (do_for_each_ref_in_dir(dir
, 0, "", name_conflict_fn
,
670 0, DO_FOR_EACH_INCLUDE_BROKEN
,
672 error("'%s' exists; cannot create '%s'",
673 data
.conflicting_refname
, refname
);
680 * Future: need to be in "struct repository"
681 * when doing a full libification.
683 static struct ref_cache
{
684 struct ref_cache
*next
;
685 struct ref_entry
*loose
;
686 struct ref_entry
*packed
;
687 /* The submodule name, or "" for the main repo. */
688 char name
[FLEX_ARRAY
];
691 static void clear_packed_ref_cache(struct ref_cache
*refs
)
694 free_ref_entry(refs
->packed
);
699 static void clear_loose_ref_cache(struct ref_cache
*refs
)
702 free_ref_entry(refs
->loose
);
707 static struct ref_cache
*create_ref_cache(const char *submodule
)
710 struct ref_cache
*refs
;
713 len
= strlen(submodule
) + 1;
714 refs
= xcalloc(1, sizeof(struct ref_cache
) + len
);
715 memcpy(refs
->name
, submodule
, len
);
720 * Return a pointer to a ref_cache for the specified submodule. For
721 * the main repository, use submodule==NULL. The returned structure
722 * will be allocated and initialized but not necessarily populated; it
723 * should not be freed.
725 static struct ref_cache
*get_ref_cache(const char *submodule
)
727 struct ref_cache
*refs
= ref_cache
;
731 if (!strcmp(submodule
, refs
->name
))
736 refs
= create_ref_cache(submodule
);
737 refs
->next
= ref_cache
;
742 void invalidate_ref_cache(const char *submodule
)
744 struct ref_cache
*refs
= get_ref_cache(submodule
);
745 clear_packed_ref_cache(refs
);
746 clear_loose_ref_cache(refs
);
750 * Parse one line from a packed-refs file. Write the SHA1 to sha1.
751 * Return a pointer to the refname within the line (null-terminated),
752 * or NULL if there was a problem.
754 static const char *parse_ref_line(char *line
, unsigned char *sha1
)
757 * 42: the answer to everything.
759 * In this case, it happens to be the answer to
760 * 40 (length of sha1 hex representation)
761 * +1 (space in between hex and name)
762 * +1 (newline at the end of the line)
764 int len
= strlen(line
) - 42;
768 if (get_sha1_hex(line
, sha1
) < 0)
770 if (!isspace(line
[40]))
775 if (line
[len
] != '\n')
782 static void read_packed_refs(FILE *f
, struct ref_dir
*dir
)
784 struct ref_entry
*last
= NULL
;
785 char refline
[PATH_MAX
];
786 int flag
= REF_ISPACKED
;
788 while (fgets(refline
, sizeof(refline
), f
)) {
789 unsigned char sha1
[20];
791 static const char header
[] = "# pack-refs with:";
793 if (!strncmp(refline
, header
, sizeof(header
)-1)) {
794 const char *traits
= refline
+ sizeof(header
) - 1;
795 if (strstr(traits
, " peeled "))
796 flag
|= REF_KNOWS_PEELED
;
797 /* perhaps other traits later as well */
801 refname
= parse_ref_line(refline
, sha1
);
803 last
= create_ref_entry(refname
, sha1
, flag
, 1);
809 strlen(refline
) == 42 &&
810 refline
[41] == '\n' &&
811 !get_sha1_hex(refline
+ 1, sha1
))
812 hashcpy(last
->u
.value
.peeled
, sha1
);
816 static struct ref_dir
*get_packed_refs(struct ref_cache
*refs
)
819 const char *packed_refs_file
;
822 refs
->packed
= create_dir_entry(refs
, "", 0, 0);
824 packed_refs_file
= git_path_submodule(refs
->name
, "packed-refs");
826 packed_refs_file
= git_path("packed-refs");
827 f
= fopen(packed_refs_file
, "r");
829 read_packed_refs(f
, get_ref_dir(refs
->packed
));
833 return get_ref_dir(refs
->packed
);
836 void add_packed_ref(const char *refname
, const unsigned char *sha1
)
838 add_ref(get_packed_refs(get_ref_cache(NULL
)),
839 create_ref_entry(refname
, sha1
, REF_ISPACKED
, 1));
843 * Read the loose references from the namespace dirname into dir
844 * (without recursing). dirname must end with '/'. dir must be the
845 * directory entry corresponding to dirname.
847 static void read_loose_refs(const char *dirname
, struct ref_dir
*dir
)
849 struct ref_cache
*refs
= dir
->ref_cache
;
853 int dirnamelen
= strlen(dirname
);
854 struct strbuf refname
;
857 path
= git_path_submodule(refs
->name
, "%s", dirname
);
859 path
= git_path("%s", dirname
);
865 strbuf_init(&refname
, dirnamelen
+ 257);
866 strbuf_add(&refname
, dirname
, dirnamelen
);
868 while ((de
= readdir(d
)) != NULL
) {
869 unsigned char sha1
[20];
874 if (de
->d_name
[0] == '.')
876 if (has_extension(de
->d_name
, ".lock"))
878 strbuf_addstr(&refname
, de
->d_name
);
880 ? git_path_submodule(refs
->name
, "%s", refname
.buf
)
881 : git_path("%s", refname
.buf
);
882 if (stat(refdir
, &st
) < 0) {
883 ; /* silently ignore */
884 } else if (S_ISDIR(st
.st_mode
)) {
885 strbuf_addch(&refname
, '/');
886 add_entry_to_dir(dir
,
887 create_dir_entry(refs
, refname
.buf
,
893 if (resolve_gitlink_ref(refs
->name
, refname
.buf
, sha1
) < 0) {
895 flag
|= REF_ISBROKEN
;
897 } else if (read_ref_full(refname
.buf
, sha1
, 1, &flag
)) {
899 flag
|= REF_ISBROKEN
;
901 add_entry_to_dir(dir
,
902 create_ref_entry(refname
.buf
, sha1
, flag
, 1));
904 strbuf_setlen(&refname
, dirnamelen
);
906 strbuf_release(&refname
);
910 static struct ref_dir
*get_loose_refs(struct ref_cache
*refs
)
914 * Mark the top-level directory complete because we
915 * are about to read the only subdirectory that can
918 refs
->loose
= create_dir_entry(refs
, "", 0, 0);
920 * Create an incomplete entry for "refs/":
922 add_entry_to_dir(get_ref_dir(refs
->loose
),
923 create_dir_entry(refs
, "refs/", 5, 1));
925 return get_ref_dir(refs
->loose
);
928 /* We allow "recursive" symbolic refs. Only within reason, though */
930 #define MAXREFLEN (1024)
933 * Called by resolve_gitlink_ref_recursive() after it failed to read
934 * from the loose refs in ref_cache refs. Find <refname> in the
935 * packed-refs file for the submodule.
937 static int resolve_gitlink_packed_ref(struct ref_cache
*refs
,
938 const char *refname
, unsigned char *sha1
)
940 struct ref_entry
*ref
;
941 struct ref_dir
*dir
= get_packed_refs(refs
);
943 ref
= find_ref(dir
, refname
);
947 memcpy(sha1
, ref
->u
.value
.sha1
, 20);
951 static int resolve_gitlink_ref_recursive(struct ref_cache
*refs
,
952 const char *refname
, unsigned char *sha1
,
956 char buffer
[128], *p
;
959 if (recursion
> MAXDEPTH
|| strlen(refname
) > MAXREFLEN
)
962 ? git_path_submodule(refs
->name
, "%s", refname
)
963 : git_path("%s", refname
);
964 fd
= open(path
, O_RDONLY
);
966 return resolve_gitlink_packed_ref(refs
, refname
, sha1
);
968 len
= read(fd
, buffer
, sizeof(buffer
)-1);
972 while (len
&& isspace(buffer
[len
-1]))
976 /* Was it a detached head or an old-fashioned symlink? */
977 if (!get_sha1_hex(buffer
, sha1
))
981 if (strncmp(buffer
, "ref:", 4))
987 return resolve_gitlink_ref_recursive(refs
, p
, sha1
, recursion
+1);
990 int resolve_gitlink_ref(const char *path
, const char *refname
, unsigned char *sha1
)
992 int len
= strlen(path
), retval
;
994 struct ref_cache
*refs
;
996 while (len
&& path
[len
-1] == '/')
1000 submodule
= xstrndup(path
, len
);
1001 refs
= get_ref_cache(submodule
);
1004 retval
= resolve_gitlink_ref_recursive(refs
, refname
, sha1
, 0);
1009 * Try to read ref from the packed references. On success, set sha1
1010 * and return 0; otherwise, return -1.
1012 static int get_packed_ref(const char *refname
, unsigned char *sha1
)
1014 struct ref_dir
*packed
= get_packed_refs(get_ref_cache(NULL
));
1015 struct ref_entry
*entry
= find_ref(packed
, refname
);
1017 hashcpy(sha1
, entry
->u
.value
.sha1
);
1023 const char *resolve_ref_unsafe(const char *refname
, unsigned char *sha1
, int reading
, int *flag
)
1025 int depth
= MAXDEPTH
;
1028 static char refname_buffer
[256];
1033 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
))
1037 char path
[PATH_MAX
];
1045 git_snpath(path
, sizeof(path
), "%s", refname
);
1047 if (lstat(path
, &st
) < 0) {
1048 if (errno
!= ENOENT
)
1051 * The loose reference file does not exist;
1052 * check for a packed reference.
1054 if (!get_packed_ref(refname
, sha1
)) {
1056 *flag
|= REF_ISPACKED
;
1059 /* The reference is not a packed reference, either. */
1068 /* Follow "normalized" - ie "refs/.." symlinks by hand */
1069 if (S_ISLNK(st
.st_mode
)) {
1070 len
= readlink(path
, buffer
, sizeof(buffer
)-1);
1074 if (!prefixcmp(buffer
, "refs/") &&
1075 !check_refname_format(buffer
, 0)) {
1076 strcpy(refname_buffer
, buffer
);
1077 refname
= refname_buffer
;
1079 *flag
|= REF_ISSYMREF
;
1084 /* Is it a directory? */
1085 if (S_ISDIR(st
.st_mode
)) {
1091 * Anything else, just open it and try to use it as
1094 fd
= open(path
, O_RDONLY
);
1097 len
= read_in_full(fd
, buffer
, sizeof(buffer
)-1);
1101 while (len
&& isspace(buffer
[len
-1]))
1106 * Is it a symbolic ref?
1108 if (prefixcmp(buffer
, "ref:"))
1111 *flag
|= REF_ISSYMREF
;
1113 while (isspace(*buf
))
1115 if (check_refname_format(buf
, REFNAME_ALLOW_ONELEVEL
)) {
1117 *flag
|= REF_ISBROKEN
;
1120 refname
= strcpy(refname_buffer
, buf
);
1122 /* Please note that FETCH_HEAD has a second line containing other data. */
1123 if (get_sha1_hex(buffer
, sha1
) || (buffer
[40] != '\0' && !isspace(buffer
[40]))) {
1125 *flag
|= REF_ISBROKEN
;
1131 char *resolve_refdup(const char *ref
, unsigned char *sha1
, int reading
, int *flag
)
1133 const char *ret
= resolve_ref_unsafe(ref
, sha1
, reading
, flag
);
1134 return ret
? xstrdup(ret
) : NULL
;
1137 /* The argument to filter_refs */
1139 const char *pattern
;
1144 int read_ref_full(const char *refname
, unsigned char *sha1
, int reading
, int *flags
)
1146 if (resolve_ref_unsafe(refname
, sha1
, reading
, flags
))
1151 int read_ref(const char *refname
, unsigned char *sha1
)
1153 return read_ref_full(refname
, sha1
, 1, NULL
);
1156 int ref_exists(const char *refname
)
1158 unsigned char sha1
[20];
1159 return !!resolve_ref_unsafe(refname
, sha1
, 1, NULL
);
1162 static int filter_refs(const char *refname
, const unsigned char *sha1
, int flags
,
1165 struct ref_filter
*filter
= (struct ref_filter
*)data
;
1166 if (fnmatch(filter
->pattern
, refname
, 0))
1168 return filter
->fn(refname
, sha1
, flags
, filter
->cb_data
);
1171 int peel_ref(const char *refname
, unsigned char *sha1
)
1174 unsigned char base
[20];
1177 if (current_ref
&& (current_ref
->name
== refname
1178 || !strcmp(current_ref
->name
, refname
))) {
1179 if (current_ref
->flag
& REF_KNOWS_PEELED
) {
1180 hashcpy(sha1
, current_ref
->u
.value
.peeled
);
1183 hashcpy(base
, current_ref
->u
.value
.sha1
);
1187 if (read_ref_full(refname
, base
, 1, &flag
))
1190 if ((flag
& REF_ISPACKED
)) {
1191 struct ref_dir
*dir
= get_packed_refs(get_ref_cache(NULL
));
1192 struct ref_entry
*r
= find_ref(dir
, refname
);
1194 if (r
!= NULL
&& r
->flag
& REF_KNOWS_PEELED
) {
1195 hashcpy(sha1
, r
->u
.value
.peeled
);
1201 o
= parse_object(base
);
1202 if (o
&& o
->type
== OBJ_TAG
) {
1203 o
= deref_tag(o
, refname
, 0);
1205 hashcpy(sha1
, o
->sha1
);
1212 struct warn_if_dangling_data
{
1214 const char *refname
;
1215 const char *msg_fmt
;
1218 static int warn_if_dangling_symref(const char *refname
, const unsigned char *sha1
,
1219 int flags
, void *cb_data
)
1221 struct warn_if_dangling_data
*d
= cb_data
;
1222 const char *resolves_to
;
1223 unsigned char junk
[20];
1225 if (!(flags
& REF_ISSYMREF
))
1228 resolves_to
= resolve_ref_unsafe(refname
, junk
, 0, NULL
);
1229 if (!resolves_to
|| strcmp(resolves_to
, d
->refname
))
1232 fprintf(d
->fp
, d
->msg_fmt
, refname
);
1237 void warn_dangling_symref(FILE *fp
, const char *msg_fmt
, const char *refname
)
1239 struct warn_if_dangling_data data
;
1242 data
.refname
= refname
;
1243 data
.msg_fmt
= msg_fmt
;
1244 for_each_rawref(warn_if_dangling_symref
, &data
);
1247 static int do_for_each_ref(const char *submodule
, const char *base
, each_ref_fn fn
,
1248 int trim
, int flags
, void *cb_data
)
1250 struct ref_cache
*refs
= get_ref_cache(submodule
);
1251 struct ref_dir
*packed_dir
= get_packed_refs(refs
);
1252 struct ref_dir
*loose_dir
= get_loose_refs(refs
);
1255 if (base
&& *base
) {
1256 packed_dir
= find_containing_dir(packed_dir
, base
, 0);
1257 loose_dir
= find_containing_dir(loose_dir
, base
, 0);
1260 if (packed_dir
&& loose_dir
) {
1261 sort_ref_dir(packed_dir
);
1262 sort_ref_dir(loose_dir
);
1263 retval
= do_for_each_ref_in_dirs(
1264 packed_dir
, loose_dir
,
1265 base
, fn
, trim
, flags
, cb_data
);
1266 } else if (packed_dir
) {
1267 sort_ref_dir(packed_dir
);
1268 retval
= do_for_each_ref_in_dir(
1270 base
, fn
, trim
, flags
, cb_data
);
1271 } else if (loose_dir
) {
1272 sort_ref_dir(loose_dir
);
1273 retval
= do_for_each_ref_in_dir(
1275 base
, fn
, trim
, flags
, cb_data
);
1281 static int do_head_ref(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1283 unsigned char sha1
[20];
1287 if (resolve_gitlink_ref(submodule
, "HEAD", sha1
) == 0)
1288 return fn("HEAD", sha1
, 0, cb_data
);
1293 if (!read_ref_full("HEAD", sha1
, 1, &flag
))
1294 return fn("HEAD", sha1
, flag
, cb_data
);
1299 int head_ref(each_ref_fn fn
, void *cb_data
)
1301 return do_head_ref(NULL
, fn
, cb_data
);
1304 int head_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1306 return do_head_ref(submodule
, fn
, cb_data
);
1309 int for_each_ref(each_ref_fn fn
, void *cb_data
)
1311 return do_for_each_ref(NULL
, "", fn
, 0, 0, cb_data
);
1314 int for_each_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1316 return do_for_each_ref(submodule
, "", fn
, 0, 0, cb_data
);
1319 int for_each_ref_in(const char *prefix
, each_ref_fn fn
, void *cb_data
)
1321 return do_for_each_ref(NULL
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1324 int for_each_ref_in_submodule(const char *submodule
, const char *prefix
,
1325 each_ref_fn fn
, void *cb_data
)
1327 return do_for_each_ref(submodule
, prefix
, fn
, strlen(prefix
), 0, cb_data
);
1330 int for_each_tag_ref(each_ref_fn fn
, void *cb_data
)
1332 return for_each_ref_in("refs/tags/", fn
, cb_data
);
1335 int for_each_tag_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1337 return for_each_ref_in_submodule(submodule
, "refs/tags/", fn
, cb_data
);
1340 int for_each_branch_ref(each_ref_fn fn
, void *cb_data
)
1342 return for_each_ref_in("refs/heads/", fn
, cb_data
);
1345 int for_each_branch_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1347 return for_each_ref_in_submodule(submodule
, "refs/heads/", fn
, cb_data
);
1350 int for_each_remote_ref(each_ref_fn fn
, void *cb_data
)
1352 return for_each_ref_in("refs/remotes/", fn
, cb_data
);
1355 int for_each_remote_ref_submodule(const char *submodule
, each_ref_fn fn
, void *cb_data
)
1357 return for_each_ref_in_submodule(submodule
, "refs/remotes/", fn
, cb_data
);
1360 int for_each_replace_ref(each_ref_fn fn
, void *cb_data
)
1362 return do_for_each_ref(NULL
, "refs/replace/", fn
, 13, 0, cb_data
);
1365 int head_ref_namespaced(each_ref_fn fn
, void *cb_data
)
1367 struct strbuf buf
= STRBUF_INIT
;
1369 unsigned char sha1
[20];
1372 strbuf_addf(&buf
, "%sHEAD", get_git_namespace());
1373 if (!read_ref_full(buf
.buf
, sha1
, 1, &flag
))
1374 ret
= fn(buf
.buf
, sha1
, flag
, cb_data
);
1375 strbuf_release(&buf
);
1380 int for_each_namespaced_ref(each_ref_fn fn
, void *cb_data
)
1382 struct strbuf buf
= STRBUF_INIT
;
1384 strbuf_addf(&buf
, "%srefs/", get_git_namespace());
1385 ret
= do_for_each_ref(NULL
, buf
.buf
, fn
, 0, 0, cb_data
);
1386 strbuf_release(&buf
);
1390 int for_each_glob_ref_in(each_ref_fn fn
, const char *pattern
,
1391 const char *prefix
, void *cb_data
)
1393 struct strbuf real_pattern
= STRBUF_INIT
;
1394 struct ref_filter filter
;
1397 if (!prefix
&& prefixcmp(pattern
, "refs/"))
1398 strbuf_addstr(&real_pattern
, "refs/");
1400 strbuf_addstr(&real_pattern
, prefix
);
1401 strbuf_addstr(&real_pattern
, pattern
);
1403 if (!has_glob_specials(pattern
)) {
1404 /* Append implied '/' '*' if not present. */
1405 if (real_pattern
.buf
[real_pattern
.len
- 1] != '/')
1406 strbuf_addch(&real_pattern
, '/');
1407 /* No need to check for '*', there is none. */
1408 strbuf_addch(&real_pattern
, '*');
1411 filter
.pattern
= real_pattern
.buf
;
1413 filter
.cb_data
= cb_data
;
1414 ret
= for_each_ref(filter_refs
, &filter
);
1416 strbuf_release(&real_pattern
);
1420 int for_each_glob_ref(each_ref_fn fn
, const char *pattern
, void *cb_data
)
1422 return for_each_glob_ref_in(fn
, pattern
, NULL
, cb_data
);
1425 int for_each_rawref(each_ref_fn fn
, void *cb_data
)
1427 return do_for_each_ref(NULL
, "", fn
, 0,
1428 DO_FOR_EACH_INCLUDE_BROKEN
, cb_data
);
1431 const char *prettify_refname(const char *name
)
1434 !prefixcmp(name
, "refs/heads/") ? 11 :
1435 !prefixcmp(name
, "refs/tags/") ? 10 :
1436 !prefixcmp(name
, "refs/remotes/") ? 13 :
1440 const char *ref_rev_parse_rules
[] = {
1445 "refs/remotes/%.*s",
1446 "refs/remotes/%.*s/HEAD",
1450 int refname_match(const char *abbrev_name
, const char *full_name
, const char **rules
)
1453 const int abbrev_name_len
= strlen(abbrev_name
);
1455 for (p
= rules
; *p
; p
++) {
1456 if (!strcmp(full_name
, mkpath(*p
, abbrev_name_len
, abbrev_name
))) {
1464 static struct ref_lock
*verify_lock(struct ref_lock
*lock
,
1465 const unsigned char *old_sha1
, int mustexist
)
1467 if (read_ref_full(lock
->ref_name
, lock
->old_sha1
, mustexist
, NULL
)) {
1468 error("Can't verify ref %s", lock
->ref_name
);
1472 if (hashcmp(lock
->old_sha1
, old_sha1
)) {
1473 error("Ref %s is at %s but expected %s", lock
->ref_name
,
1474 sha1_to_hex(lock
->old_sha1
), sha1_to_hex(old_sha1
));
1481 static int remove_empty_directories(const char *file
)
1483 /* we want to create a file but there is a directory there;
1484 * if that is an empty directory (or a directory that contains
1485 * only empty directories), remove them.
1490 strbuf_init(&path
, 20);
1491 strbuf_addstr(&path
, file
);
1493 result
= remove_dir_recursively(&path
, REMOVE_DIR_EMPTY_ONLY
);
1495 strbuf_release(&path
);
1501 * *string and *len will only be substituted, and *string returned (for
1502 * later free()ing) if the string passed in is a magic short-hand form
1505 static char *substitute_branch_name(const char **string
, int *len
)
1507 struct strbuf buf
= STRBUF_INIT
;
1508 int ret
= interpret_branch_name(*string
, &buf
);
1512 *string
= strbuf_detach(&buf
, &size
);
1514 return (char *)*string
;
1520 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
1522 char *last_branch
= substitute_branch_name(&str
, &len
);
1527 for (p
= ref_rev_parse_rules
; *p
; p
++) {
1528 char fullref
[PATH_MAX
];
1529 unsigned char sha1_from_ref
[20];
1530 unsigned char *this_result
;
1533 this_result
= refs_found
? sha1_from_ref
: sha1
;
1534 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
1535 r
= resolve_ref_unsafe(fullref
, this_result
, 1, &flag
);
1539 if (!warn_ambiguous_refs
)
1541 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD")) {
1542 warning("ignoring dangling symref %s.", fullref
);
1543 } else if ((flag
& REF_ISBROKEN
) && strchr(fullref
, '/')) {
1544 warning("ignoring broken ref %s.", fullref
);
1551 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
1553 char *last_branch
= substitute_branch_name(&str
, &len
);
1558 for (p
= ref_rev_parse_rules
; *p
; p
++) {
1560 unsigned char hash
[20];
1561 char path
[PATH_MAX
];
1562 const char *ref
, *it
;
1564 mksnpath(path
, sizeof(path
), *p
, len
, str
);
1565 ref
= resolve_ref_unsafe(path
, hash
, 1, NULL
);
1568 if (!stat(git_path("logs/%s", path
), &st
) &&
1569 S_ISREG(st
.st_mode
))
1571 else if (strcmp(ref
, path
) &&
1572 !stat(git_path("logs/%s", ref
), &st
) &&
1573 S_ISREG(st
.st_mode
))
1577 if (!logs_found
++) {
1579 hashcpy(sha1
, hash
);
1581 if (!warn_ambiguous_refs
)
1588 static struct ref_lock
*lock_ref_sha1_basic(const char *refname
,
1589 const unsigned char *old_sha1
,
1590 int flags
, int *type_p
)
1593 const char *orig_refname
= refname
;
1594 struct ref_lock
*lock
;
1597 int mustexist
= (old_sha1
&& !is_null_sha1(old_sha1
));
1600 lock
= xcalloc(1, sizeof(struct ref_lock
));
1603 refname
= resolve_ref_unsafe(refname
, lock
->old_sha1
, mustexist
, &type
);
1604 if (!refname
&& errno
== EISDIR
) {
1605 /* we are trying to lock foo but we used to
1606 * have foo/bar which now does not exist;
1607 * it is normal for the empty directory 'foo'
1610 ref_file
= git_path("%s", orig_refname
);
1611 if (remove_empty_directories(ref_file
)) {
1613 error("there are still refs under '%s'", orig_refname
);
1616 refname
= resolve_ref_unsafe(orig_refname
, lock
->old_sha1
, mustexist
, &type
);
1622 error("unable to resolve reference %s: %s",
1623 orig_refname
, strerror(errno
));
1626 missing
= is_null_sha1(lock
->old_sha1
);
1627 /* When the ref did not exist and we are creating it,
1628 * make sure there is no existing ref that is packed
1629 * whose name begins with our refname, nor a ref whose
1630 * name is a proper prefix of our refname.
1633 !is_refname_available(refname
, NULL
, get_packed_refs(get_ref_cache(NULL
)))) {
1634 last_errno
= ENOTDIR
;
1638 lock
->lk
= xcalloc(1, sizeof(struct lock_file
));
1640 lflags
= LOCK_DIE_ON_ERROR
;
1641 if (flags
& REF_NODEREF
) {
1642 refname
= orig_refname
;
1643 lflags
|= LOCK_NODEREF
;
1645 lock
->ref_name
= xstrdup(refname
);
1646 lock
->orig_ref_name
= xstrdup(orig_refname
);
1647 ref_file
= git_path("%s", refname
);
1649 lock
->force_write
= 1;
1650 if ((flags
& REF_NODEREF
) && (type
& REF_ISSYMREF
))
1651 lock
->force_write
= 1;
1653 if (safe_create_leading_directories(ref_file
)) {
1655 error("unable to create directory for %s", ref_file
);
1659 lock
->lock_fd
= hold_lock_file_for_update(lock
->lk
, ref_file
, lflags
);
1660 return old_sha1
? verify_lock(lock
, old_sha1
, mustexist
) : lock
;
1668 struct ref_lock
*lock_ref_sha1(const char *refname
, const unsigned char *old_sha1
)
1670 char refpath
[PATH_MAX
];
1671 if (check_refname_format(refname
, 0))
1673 strcpy(refpath
, mkpath("refs/%s", refname
));
1674 return lock_ref_sha1_basic(refpath
, old_sha1
, 0, NULL
);
1677 struct ref_lock
*lock_any_ref_for_update(const char *refname
,
1678 const unsigned char *old_sha1
, int flags
)
1680 if (check_refname_format(refname
, REFNAME_ALLOW_ONELEVEL
))
1682 return lock_ref_sha1_basic(refname
, old_sha1
, flags
, NULL
);
1685 struct repack_without_ref_sb
{
1686 const char *refname
;
1690 static int repack_without_ref_fn(const char *refname
, const unsigned char *sha1
,
1691 int flags
, void *cb_data
)
1693 struct repack_without_ref_sb
*data
= cb_data
;
1694 char line
[PATH_MAX
+ 100];
1697 if (!strcmp(data
->refname
, refname
))
1699 len
= snprintf(line
, sizeof(line
), "%s %s\n",
1700 sha1_to_hex(sha1
), refname
);
1701 /* this should not happen but just being defensive */
1702 if (len
> sizeof(line
))
1703 die("too long a refname '%s'", refname
);
1704 write_or_die(data
->fd
, line
, len
);
1708 static struct lock_file packlock
;
1710 static int repack_without_ref(const char *refname
)
1712 struct repack_without_ref_sb data
;
1713 struct ref_dir
*packed
= get_packed_refs(get_ref_cache(NULL
));
1714 if (find_ref(packed
, refname
) == NULL
)
1716 data
.refname
= refname
;
1717 data
.fd
= hold_lock_file_for_update(&packlock
, git_path("packed-refs"), 0);
1719 unable_to_lock_error(git_path("packed-refs"), errno
);
1720 return error("cannot delete '%s' from packed refs", refname
);
1722 do_for_each_ref_in_dir(packed
, 0, "", repack_without_ref_fn
, 0, 0, &data
);
1723 return commit_lock_file(&packlock
);
1726 int delete_ref(const char *refname
, const unsigned char *sha1
, int delopt
)
1728 struct ref_lock
*lock
;
1729 int err
, i
= 0, ret
= 0, flag
= 0;
1731 lock
= lock_ref_sha1_basic(refname
, sha1
, 0, &flag
);
1734 if (!(flag
& REF_ISPACKED
) || flag
& REF_ISSYMREF
) {
1738 if (!(delopt
& REF_NODEREF
)) {
1739 i
= strlen(lock
->lk
->filename
) - 5; /* .lock */
1740 lock
->lk
->filename
[i
] = 0;
1741 path
= lock
->lk
->filename
;
1743 path
= git_path("%s", refname
);
1745 err
= unlink_or_warn(path
);
1746 if (err
&& errno
!= ENOENT
)
1749 if (!(delopt
& REF_NODEREF
))
1750 lock
->lk
->filename
[i
] = '.';
1752 /* removing the loose one could have resurrected an earlier
1753 * packed one. Also, if it was not loose we need to repack
1756 ret
|= repack_without_ref(refname
);
1758 unlink_or_warn(git_path("logs/%s", lock
->ref_name
));
1759 invalidate_ref_cache(NULL
);
1765 * People using contrib's git-new-workdir have .git/logs/refs ->
1766 * /some/other/path/.git/logs/refs, and that may live on another device.
1768 * IOW, to avoid cross device rename errors, the temporary renamed log must
1769 * live into logs/refs.
1771 #define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
1773 int rename_ref(const char *oldrefname
, const char *newrefname
, const char *logmsg
)
1775 unsigned char sha1
[20], orig_sha1
[20];
1776 int flag
= 0, logmoved
= 0;
1777 struct ref_lock
*lock
;
1778 struct stat loginfo
;
1779 int log
= !lstat(git_path("logs/%s", oldrefname
), &loginfo
);
1780 const char *symref
= NULL
;
1781 struct ref_cache
*refs
= get_ref_cache(NULL
);
1783 if (log
&& S_ISLNK(loginfo
.st_mode
))
1784 return error("reflog for %s is a symlink", oldrefname
);
1786 symref
= resolve_ref_unsafe(oldrefname
, orig_sha1
, 1, &flag
);
1787 if (flag
& REF_ISSYMREF
)
1788 return error("refname %s is a symbolic ref, renaming it is not supported",
1791 return error("refname %s not found", oldrefname
);
1793 if (!is_refname_available(newrefname
, oldrefname
, get_packed_refs(refs
)))
1796 if (!is_refname_available(newrefname
, oldrefname
, get_loose_refs(refs
)))
1799 if (log
&& rename(git_path("logs/%s", oldrefname
), git_path(TMP_RENAMED_LOG
)))
1800 return error("unable to move logfile logs/%s to "TMP_RENAMED_LOG
": %s",
1801 oldrefname
, strerror(errno
));
1803 if (delete_ref(oldrefname
, orig_sha1
, REF_NODEREF
)) {
1804 error("unable to delete old %s", oldrefname
);
1808 if (!read_ref_full(newrefname
, sha1
, 1, &flag
) &&
1809 delete_ref(newrefname
, sha1
, REF_NODEREF
)) {
1810 if (errno
==EISDIR
) {
1811 if (remove_empty_directories(git_path("%s", newrefname
))) {
1812 error("Directory not empty: %s", newrefname
);
1816 error("unable to delete existing %s", newrefname
);
1821 if (log
&& safe_create_leading_directories(git_path("logs/%s", newrefname
))) {
1822 error("unable to create directory for %s", newrefname
);
1827 if (log
&& rename(git_path(TMP_RENAMED_LOG
), git_path("logs/%s", newrefname
))) {
1828 if (errno
==EISDIR
|| errno
==ENOTDIR
) {
1830 * rename(a, b) when b is an existing
1831 * directory ought to result in ISDIR, but
1832 * Solaris 5.8 gives ENOTDIR. Sheesh.
1834 if (remove_empty_directories(git_path("logs/%s", newrefname
))) {
1835 error("Directory not empty: logs/%s", newrefname
);
1840 error("unable to move logfile "TMP_RENAMED_LOG
" to logs/%s: %s",
1841 newrefname
, strerror(errno
));
1847 lock
= lock_ref_sha1_basic(newrefname
, NULL
, 0, NULL
);
1849 error("unable to lock %s for update", newrefname
);
1852 lock
->force_write
= 1;
1853 hashcpy(lock
->old_sha1
, orig_sha1
);
1854 if (write_ref_sha1(lock
, orig_sha1
, logmsg
)) {
1855 error("unable to write current sha1 into %s", newrefname
);
1862 lock
= lock_ref_sha1_basic(oldrefname
, NULL
, 0, NULL
);
1864 error("unable to lock %s for rollback", oldrefname
);
1868 lock
->force_write
= 1;
1869 flag
= log_all_ref_updates
;
1870 log_all_ref_updates
= 0;
1871 if (write_ref_sha1(lock
, orig_sha1
, NULL
))
1872 error("unable to write current sha1 into %s", oldrefname
);
1873 log_all_ref_updates
= flag
;
1876 if (logmoved
&& rename(git_path("logs/%s", newrefname
), git_path("logs/%s", oldrefname
)))
1877 error("unable to restore logfile %s from %s: %s",
1878 oldrefname
, newrefname
, strerror(errno
));
1879 if (!logmoved
&& log
&&
1880 rename(git_path(TMP_RENAMED_LOG
), git_path("logs/%s", oldrefname
)))
1881 error("unable to restore logfile %s from "TMP_RENAMED_LOG
": %s",
1882 oldrefname
, strerror(errno
));
1887 int close_ref(struct ref_lock
*lock
)
1889 if (close_lock_file(lock
->lk
))
1895 int commit_ref(struct ref_lock
*lock
)
1897 if (commit_lock_file(lock
->lk
))
1903 void unlock_ref(struct ref_lock
*lock
)
1905 /* Do not free lock->lk -- atexit() still looks at them */
1907 rollback_lock_file(lock
->lk
);
1908 free(lock
->ref_name
);
1909 free(lock
->orig_ref_name
);
1914 * copy the reflog message msg to buf, which has been allocated sufficiently
1915 * large, while cleaning up the whitespaces. Especially, convert LF to space,
1916 * because reflog file is one line per entry.
1918 static int copy_msg(char *buf
, const char *msg
)
1925 while ((c
= *msg
++)) {
1926 if (wasspace
&& isspace(c
))
1928 wasspace
= isspace(c
);
1933 while (buf
< cp
&& isspace(cp
[-1]))
1939 int log_ref_setup(const char *refname
, char *logfile
, int bufsize
)
1941 int logfd
, oflags
= O_APPEND
| O_WRONLY
;
1943 git_snpath(logfile
, bufsize
, "logs/%s", refname
);
1944 if (log_all_ref_updates
&&
1945 (!prefixcmp(refname
, "refs/heads/") ||
1946 !prefixcmp(refname
, "refs/remotes/") ||
1947 !prefixcmp(refname
, "refs/notes/") ||
1948 !strcmp(refname
, "HEAD"))) {
1949 if (safe_create_leading_directories(logfile
) < 0)
1950 return error("unable to create directory for %s",
1955 logfd
= open(logfile
, oflags
, 0666);
1957 if (!(oflags
& O_CREAT
) && errno
== ENOENT
)
1960 if ((oflags
& O_CREAT
) && errno
== EISDIR
) {
1961 if (remove_empty_directories(logfile
)) {
1962 return error("There are still logs under '%s'",
1965 logfd
= open(logfile
, oflags
, 0666);
1969 return error("Unable to append to %s: %s",
1970 logfile
, strerror(errno
));
1973 adjust_shared_perm(logfile
);
1978 static int log_ref_write(const char *refname
, const unsigned char *old_sha1
,
1979 const unsigned char *new_sha1
, const char *msg
)
1981 int logfd
, result
, written
, oflags
= O_APPEND
| O_WRONLY
;
1982 unsigned maxlen
, len
;
1984 char log_file
[PATH_MAX
];
1986 const char *committer
;
1988 if (log_all_ref_updates
< 0)
1989 log_all_ref_updates
= !is_bare_repository();
1991 result
= log_ref_setup(refname
, log_file
, sizeof(log_file
));
1995 logfd
= open(log_file
, oflags
);
1998 msglen
= msg
? strlen(msg
) : 0;
1999 committer
= git_committer_info(0);
2000 maxlen
= strlen(committer
) + msglen
+ 100;
2001 logrec
= xmalloc(maxlen
);
2002 len
= sprintf(logrec
, "%s %s %s\n",
2003 sha1_to_hex(old_sha1
),
2004 sha1_to_hex(new_sha1
),
2007 len
+= copy_msg(logrec
+ len
- 1, msg
) - 1;
2008 written
= len
<= maxlen
? write_in_full(logfd
, logrec
, len
) : -1;
2010 if (close(logfd
) != 0 || written
!= len
)
2011 return error("Unable to append to %s", log_file
);
2015 static int is_branch(const char *refname
)
2017 return !strcmp(refname
, "HEAD") || !prefixcmp(refname
, "refs/heads/");
2020 int write_ref_sha1(struct ref_lock
*lock
,
2021 const unsigned char *sha1
, const char *logmsg
)
2023 static char term
= '\n';
2028 if (!lock
->force_write
&& !hashcmp(lock
->old_sha1
, sha1
)) {
2032 o
= parse_object(sha1
);
2034 error("Trying to write ref %s with nonexistent object %s",
2035 lock
->ref_name
, sha1_to_hex(sha1
));
2039 if (o
->type
!= OBJ_COMMIT
&& is_branch(lock
->ref_name
)) {
2040 error("Trying to write non-commit object %s to branch %s",
2041 sha1_to_hex(sha1
), lock
->ref_name
);
2045 if (write_in_full(lock
->lock_fd
, sha1_to_hex(sha1
), 40) != 40 ||
2046 write_in_full(lock
->lock_fd
, &term
, 1) != 1
2047 || close_ref(lock
) < 0) {
2048 error("Couldn't write %s", lock
->lk
->filename
);
2052 clear_loose_ref_cache(get_ref_cache(NULL
));
2053 if (log_ref_write(lock
->ref_name
, lock
->old_sha1
, sha1
, logmsg
) < 0 ||
2054 (strcmp(lock
->ref_name
, lock
->orig_ref_name
) &&
2055 log_ref_write(lock
->orig_ref_name
, lock
->old_sha1
, sha1
, logmsg
) < 0)) {
2059 if (strcmp(lock
->orig_ref_name
, "HEAD") != 0) {
2061 * Special hack: If a branch is updated directly and HEAD
2062 * points to it (may happen on the remote side of a push
2063 * for example) then logically the HEAD reflog should be
2065 * A generic solution implies reverse symref information,
2066 * but finding all symrefs pointing to the given branch
2067 * would be rather costly for this rare event (the direct
2068 * update of a branch) to be worth it. So let's cheat and
2069 * check with HEAD only which should cover 99% of all usage
2070 * scenarios (even 100% of the default ones).
2072 unsigned char head_sha1
[20];
2074 const char *head_ref
;
2075 head_ref
= resolve_ref_unsafe("HEAD", head_sha1
, 1, &head_flag
);
2076 if (head_ref
&& (head_flag
& REF_ISSYMREF
) &&
2077 !strcmp(head_ref
, lock
->ref_name
))
2078 log_ref_write("HEAD", lock
->old_sha1
, sha1
, logmsg
);
2080 if (commit_ref(lock
)) {
2081 error("Couldn't set %s", lock
->ref_name
);
2089 int create_symref(const char *ref_target
, const char *refs_heads_master
,
2092 const char *lockpath
;
2094 int fd
, len
, written
;
2095 char *git_HEAD
= git_pathdup("%s", ref_target
);
2096 unsigned char old_sha1
[20], new_sha1
[20];
2098 if (logmsg
&& read_ref(ref_target
, old_sha1
))
2101 if (safe_create_leading_directories(git_HEAD
) < 0)
2102 return error("unable to create directory for %s", git_HEAD
);
2104 #ifndef NO_SYMLINK_HEAD
2105 if (prefer_symlink_refs
) {
2107 if (!symlink(refs_heads_master
, git_HEAD
))
2109 fprintf(stderr
, "no symlink - falling back to symbolic ref\n");
2113 len
= snprintf(ref
, sizeof(ref
), "ref: %s\n", refs_heads_master
);
2114 if (sizeof(ref
) <= len
) {
2115 error("refname too long: %s", refs_heads_master
);
2116 goto error_free_return
;
2118 lockpath
= mkpath("%s.lock", git_HEAD
);
2119 fd
= open(lockpath
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
2121 error("Unable to open %s for writing", lockpath
);
2122 goto error_free_return
;
2124 written
= write_in_full(fd
, ref
, len
);
2125 if (close(fd
) != 0 || written
!= len
) {
2126 error("Unable to write to %s", lockpath
);
2127 goto error_unlink_return
;
2129 if (rename(lockpath
, git_HEAD
) < 0) {
2130 error("Unable to create %s", git_HEAD
);
2131 goto error_unlink_return
;
2133 if (adjust_shared_perm(git_HEAD
)) {
2134 error("Unable to fix permissions on %s", lockpath
);
2135 error_unlink_return
:
2136 unlink_or_warn(lockpath
);
2142 #ifndef NO_SYMLINK_HEAD
2145 if (logmsg
&& !read_ref(refs_heads_master
, new_sha1
))
2146 log_ref_write(ref_target
, old_sha1
, new_sha1
, logmsg
);
2152 static char *ref_msg(const char *line
, const char *endp
)
2156 ep
= memchr(line
, '\n', endp
- line
);
2159 return xmemdupz(line
, ep
- line
);
2162 int read_ref_at(const char *refname
, unsigned long at_time
, int cnt
,
2163 unsigned char *sha1
, char **msg
,
2164 unsigned long *cutoff_time
, int *cutoff_tz
, int *cutoff_cnt
)
2166 const char *logfile
, *logdata
, *logend
, *rec
, *lastgt
, *lastrec
;
2168 int logfd
, tz
, reccnt
= 0;
2171 unsigned char logged_sha1
[20];
2175 logfile
= git_path("logs/%s", refname
);
2176 logfd
= open(logfile
, O_RDONLY
, 0);
2178 die_errno("Unable to read log '%s'", logfile
);
2181 die("Log %s is empty.", logfile
);
2182 mapsz
= xsize_t(st
.st_size
);
2183 log_mapped
= xmmap(NULL
, mapsz
, PROT_READ
, MAP_PRIVATE
, logfd
, 0);
2184 logdata
= log_mapped
;
2188 rec
= logend
= logdata
+ st
.st_size
;
2189 while (logdata
< rec
) {
2191 if (logdata
< rec
&& *(rec
-1) == '\n')
2194 while (logdata
< rec
&& *(rec
-1) != '\n') {
2200 die("Log %s is corrupt.", logfile
);
2201 date
= strtoul(lastgt
+ 1, &tz_c
, 10);
2202 if (date
<= at_time
|| cnt
== 0) {
2203 tz
= strtoul(tz_c
, NULL
, 10);
2205 *msg
= ref_msg(rec
, logend
);
2207 *cutoff_time
= date
;
2211 *cutoff_cnt
= reccnt
- 1;
2213 if (get_sha1_hex(lastrec
, logged_sha1
))
2214 die("Log %s is corrupt.", logfile
);
2215 if (get_sha1_hex(rec
+ 41, sha1
))
2216 die("Log %s is corrupt.", logfile
);
2217 if (hashcmp(logged_sha1
, sha1
)) {
2218 warning("Log %s has gap after %s.",
2219 logfile
, show_date(date
, tz
, DATE_RFC2822
));
2222 else if (date
== at_time
) {
2223 if (get_sha1_hex(rec
+ 41, sha1
))
2224 die("Log %s is corrupt.", logfile
);
2227 if (get_sha1_hex(rec
+ 41, logged_sha1
))
2228 die("Log %s is corrupt.", logfile
);
2229 if (hashcmp(logged_sha1
, sha1
)) {
2230 warning("Log %s unexpectedly ended on %s.",
2231 logfile
, show_date(date
, tz
, DATE_RFC2822
));
2234 munmap(log_mapped
, mapsz
);
2243 while (rec
< logend
&& *rec
!= '>' && *rec
!= '\n')
2245 if (rec
== logend
|| *rec
== '\n')
2246 die("Log %s is corrupt.", logfile
);
2247 date
= strtoul(rec
+ 1, &tz_c
, 10);
2248 tz
= strtoul(tz_c
, NULL
, 10);
2249 if (get_sha1_hex(logdata
, sha1
))
2250 die("Log %s is corrupt.", logfile
);
2251 if (is_null_sha1(sha1
)) {
2252 if (get_sha1_hex(logdata
+ 41, sha1
))
2253 die("Log %s is corrupt.", logfile
);
2256 *msg
= ref_msg(logdata
, logend
);
2257 munmap(log_mapped
, mapsz
);
2260 *cutoff_time
= date
;
2264 *cutoff_cnt
= reccnt
;
2268 int for_each_recent_reflog_ent(const char *refname
, each_reflog_ent_fn fn
, long ofs
, void *cb_data
)
2270 const char *logfile
;
2272 struct strbuf sb
= STRBUF_INIT
;
2275 logfile
= git_path("logs/%s", refname
);
2276 logfp
= fopen(logfile
, "r");
2281 struct stat statbuf
;
2282 if (fstat(fileno(logfp
), &statbuf
) ||
2283 statbuf
.st_size
< ofs
||
2284 fseek(logfp
, -ofs
, SEEK_END
) ||
2285 strbuf_getwholeline(&sb
, logfp
, '\n')) {
2287 strbuf_release(&sb
);
2292 while (!strbuf_getwholeline(&sb
, logfp
, '\n')) {
2293 unsigned char osha1
[20], nsha1
[20];
2294 char *email_end
, *message
;
2295 unsigned long timestamp
;
2298 /* old SP new SP name <email> SP time TAB msg LF */
2299 if (sb
.len
< 83 || sb
.buf
[sb
.len
- 1] != '\n' ||
2300 get_sha1_hex(sb
.buf
, osha1
) || sb
.buf
[40] != ' ' ||
2301 get_sha1_hex(sb
.buf
+ 41, nsha1
) || sb
.buf
[81] != ' ' ||
2302 !(email_end
= strchr(sb
.buf
+ 82, '>')) ||
2303 email_end
[1] != ' ' ||
2304 !(timestamp
= strtoul(email_end
+ 2, &message
, 10)) ||
2305 !message
|| message
[0] != ' ' ||
2306 (message
[1] != '+' && message
[1] != '-') ||
2307 !isdigit(message
[2]) || !isdigit(message
[3]) ||
2308 !isdigit(message
[4]) || !isdigit(message
[5]))
2309 continue; /* corrupt? */
2310 email_end
[1] = '\0';
2311 tz
= strtol(message
+ 1, NULL
, 10);
2312 if (message
[6] != '\t')
2316 ret
= fn(osha1
, nsha1
, sb
.buf
+ 82, timestamp
, tz
, message
,
2322 strbuf_release(&sb
);
2326 int for_each_reflog_ent(const char *refname
, each_reflog_ent_fn fn
, void *cb_data
)
2328 return for_each_recent_reflog_ent(refname
, fn
, 0, cb_data
);
2332 * Call fn for each reflog in the namespace indicated by name. name
2333 * must be empty or end with '/'. Name will be used as a scratch
2334 * space, but its contents will be restored before return.
2336 static int do_for_each_reflog(struct strbuf
*name
, each_ref_fn fn
, void *cb_data
)
2338 DIR *d
= opendir(git_path("logs/%s", name
->buf
));
2341 int oldlen
= name
->len
;
2344 return name
->len
? errno
: 0;
2346 while ((de
= readdir(d
)) != NULL
) {
2349 if (de
->d_name
[0] == '.')
2351 if (has_extension(de
->d_name
, ".lock"))
2353 strbuf_addstr(name
, de
->d_name
);
2354 if (stat(git_path("logs/%s", name
->buf
), &st
) < 0) {
2355 ; /* silently ignore */
2357 if (S_ISDIR(st
.st_mode
)) {
2358 strbuf_addch(name
, '/');
2359 retval
= do_for_each_reflog(name
, fn
, cb_data
);
2361 unsigned char sha1
[20];
2362 if (read_ref_full(name
->buf
, sha1
, 0, NULL
))
2363 retval
= error("bad ref for %s", name
->buf
);
2365 retval
= fn(name
->buf
, sha1
, 0, cb_data
);
2370 strbuf_setlen(name
, oldlen
);
2376 int for_each_reflog(each_ref_fn fn
, void *cb_data
)
2380 strbuf_init(&name
, PATH_MAX
);
2381 retval
= do_for_each_reflog(&name
, fn
, cb_data
);
2382 strbuf_release(&name
);
2386 int update_ref(const char *action
, const char *refname
,
2387 const unsigned char *sha1
, const unsigned char *oldval
,
2388 int flags
, enum action_on_err onerr
)
2390 static struct ref_lock
*lock
;
2391 lock
= lock_any_ref_for_update(refname
, oldval
, flags
);
2393 const char *str
= "Cannot lock the ref '%s'.";
2395 case MSG_ON_ERR
: error(str
, refname
); break;
2396 case DIE_ON_ERR
: die(str
, refname
); break;
2397 case QUIET_ON_ERR
: break;
2401 if (write_ref_sha1(lock
, sha1
, action
) < 0) {
2402 const char *str
= "Cannot update the ref '%s'.";
2404 case MSG_ON_ERR
: error(str
, refname
); break;
2405 case DIE_ON_ERR
: die(str
, refname
); break;
2406 case QUIET_ON_ERR
: break;
2413 struct ref
*find_ref_by_name(const struct ref
*list
, const char *name
)
2415 for ( ; list
; list
= list
->next
)
2416 if (!strcmp(list
->name
, name
))
2417 return (struct ref
*)list
;
2422 * generate a format suitable for scanf from a ref_rev_parse_rules
2423 * rule, that is replace the "%.*s" spec with a "%s" spec
2425 static void gen_scanf_fmt(char *scanf_fmt
, const char *rule
)
2429 spec
= strstr(rule
, "%.*s");
2430 if (!spec
|| strstr(spec
+ 4, "%.*s"))
2431 die("invalid rule in ref_rev_parse_rules: %s", rule
);
2433 /* copy all until spec */
2434 strncpy(scanf_fmt
, rule
, spec
- rule
);
2435 scanf_fmt
[spec
- rule
] = '\0';
2437 strcat(scanf_fmt
, "%s");
2438 /* copy remaining rule */
2439 strcat(scanf_fmt
, spec
+ 4);
2444 char *shorten_unambiguous_ref(const char *refname
, int strict
)
2447 static char **scanf_fmts
;
2448 static int nr_rules
;
2451 /* pre generate scanf formats from ref_rev_parse_rules[] */
2453 size_t total_len
= 0;
2455 /* the rule list is NULL terminated, count them first */
2456 for (; ref_rev_parse_rules
[nr_rules
]; nr_rules
++)
2457 /* no +1 because strlen("%s") < strlen("%.*s") */
2458 total_len
+= strlen(ref_rev_parse_rules
[nr_rules
]);
2460 scanf_fmts
= xmalloc(nr_rules
* sizeof(char *) + total_len
);
2463 for (i
= 0; i
< nr_rules
; i
++) {
2464 scanf_fmts
[i
] = (char *)&scanf_fmts
[nr_rules
]
2466 gen_scanf_fmt(scanf_fmts
[i
], ref_rev_parse_rules
[i
]);
2467 total_len
+= strlen(ref_rev_parse_rules
[i
]);
2471 /* bail out if there are no rules */
2473 return xstrdup(refname
);
2475 /* buffer for scanf result, at most refname must fit */
2476 short_name
= xstrdup(refname
);
2478 /* skip first rule, it will always match */
2479 for (i
= nr_rules
- 1; i
> 0 ; --i
) {
2481 int rules_to_fail
= i
;
2484 if (1 != sscanf(refname
, scanf_fmts
[i
], short_name
))
2487 short_name_len
= strlen(short_name
);
2490 * in strict mode, all (except the matched one) rules
2491 * must fail to resolve to a valid non-ambiguous ref
2494 rules_to_fail
= nr_rules
;
2497 * check if the short name resolves to a valid ref,
2498 * but use only rules prior to the matched one
2500 for (j
= 0; j
< rules_to_fail
; j
++) {
2501 const char *rule
= ref_rev_parse_rules
[j
];
2502 char refname
[PATH_MAX
];
2504 /* skip matched rule */
2509 * the short name is ambiguous, if it resolves
2510 * (with this previous rule) to a valid ref
2511 * read_ref() returns 0 on success
2513 mksnpath(refname
, sizeof(refname
),
2514 rule
, short_name_len
, short_name
);
2515 if (ref_exists(refname
))
2520 * short name is non-ambiguous if all previous rules
2521 * haven't resolved to a valid ref
2523 if (j
== rules_to_fail
)
2528 return xstrdup(refname
);