Skip to content

Commit b3cfc40

Browse files
joshtriplettgitster
authored andcommitted
Fix prefix handling in ref iteration functions
The do_for_each_ref iteration function accepts a prefix and a trim, and checks for the prefix on each ref before passing in that ref; it also supports trimming off part of the ref before passing it. However, do_for_each_ref used trim as the length of the prefix to check, ignoring the actual length of the prefix. Switch to using prefixcmp, checking the entire length of the prefix string, to properly support a trim value different than the length of the prefix. Several callers passed a prefix of "refs/" to filter out everything outside of refs/, but a trim of 0 to avoid trimming off the "refs/"; the trim of 0 meant that the filter of "refs/" no longer applied. Change these callers to pass an empty prefix instead, to avoid changing the existing behavior. Various callers count on this lack of filtering, such as receive-pack which uses add_extra_ref to add alternates as refs named ".have"; adding filtering would break that, causing t5501-fetch-push-alternates.sh to fail. That lack of filtering doesn't currently have any other effect, since the loose ref functions can never supply refs outside of "refs/", and packed-refs will not normally include such refs unless manually edited. Signed-off-by: Josh Triplett <[email protected]> Signed-off-by: Jamey Sharp <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5df3e2b commit b3cfc40

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

refs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ int read_ref(const char *ref, unsigned char *sha1)
584584
static int do_one_ref(const char *base, each_ref_fn fn, int trim,
585585
int flags, void *cb_data, struct ref_list *entry)
586586
{
587-
if (strncmp(base, entry->name, trim))
587+
if (prefixcmp(entry->name, base))
588588
return 0;
589589

590590
if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
@@ -728,12 +728,12 @@ int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
728728

729729
int for_each_ref(each_ref_fn fn, void *cb_data)
730730
{
731-
return do_for_each_ref(NULL, "refs/", fn, 0, 0, cb_data);
731+
return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
732732
}
733733

734734
int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
735735
{
736-
return do_for_each_ref(submodule, "refs/", fn, 0, 0, cb_data);
736+
return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
737737
}
738738

739739
int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
@@ -819,7 +819,7 @@ int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data)
819819

820820
int for_each_rawref(each_ref_fn fn, void *cb_data)
821821
{
822-
return do_for_each_ref(NULL, "refs/", fn, 0,
822+
return do_for_each_ref(NULL, "", fn, 0,
823823
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
824824
}
825825

0 commit comments

Comments
 (0)