git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/4] Fix prefix handling in ref iteration functions

From
Jamey Sharp <jamey@minilop.net>
Date
Jun 7, 2011, 18:21 UTC
Message-ID
<1307470885-4018-2-git-send-email-jamey@minilop.net>
In-Reply-To
<1307470885-4018-1-git-send-email-jamey@minilop.net>
From: Josh Triplett <josh@joshtriplett.org>

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.

Commit by Josh Triplett and Jamey Sharp.
Signed-off-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Jamey Sharp <jamey@minilop.net>
---
 refs.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/refs.c b/refs.c
index e3c0511..858cf92 100644
--- a/refs.c
+++ b/refs.c
@@ -584,7 +584,7 @@ int read_ref(const char *ref, unsigned char *sha1)
 static int do_one_ref(const char *base, each_ref_fn fn, int trim,
 		      int flags, void *cb_data, struct ref_list *entry)
 {
-	if (strncmp(base, entry->name, trim))
+	if (prefixcmp(entry->name, base))
 		return 0;
 
 	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)
 
 int for_each_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(NULL, "refs/", fn, 0, 0, cb_data);
+	return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
 }
 
 int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(submodule, "refs/", fn, 0, 0, cb_data);
+	return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
 }
 
 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)
 
 int for_each_rawref(each_ref_fn fn, void *cb_data)
 {
-	return do_for_each_ref(NULL, "refs/", fn, 0,
+	return do_for_each_ref(NULL, "", fn, 0,
 			       DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
 }
 
-- 
1.7.5.3
Previous: Jamey SharpNext: Jamey Sharp
Message 5 of 17 in “What's cooking in git.git (Jun 2011, #01; Sun, 5)”
  1. Junio C HamanoJun 6, 2011
  2. Johannes SixtJun 6, 2011
  3. Erik Faye-LundJun 6, 2011
  4. Jamey SharpJun 7, 2011
  5. 1/4 Fix prefix handling in ref iteration functionsJamey Sharp, Jun 7, 2011
  6. 2/4 Add infrastructure for ref namespacesJamey Sharp, Jun 7, 2011
  7. Bert WesargJun 7, 2011
  8. Josh TriplettJun 7, 2011
  9. 3/4 Support ref namespaces for remote repositories via upload-pack and receive-packJamey Sharp, Jun 7, 2011
  10. 4/4 Add documentation for ref namespacesJamey Sharp, Jun 7, 2011
  11. Ævar Arnfjörð BjarmasonAug 23, 2011
  12. Junio C HamanoAug 23, 2011
  13. Ævar Arnfjörð BjarmasonAug 24, 2011
  14. Re* [PATCH 4/4] Add documentation for ref namespacesJunio C Hamano, Sep 16, 2011
  15. Jamey SharpSep 16, 2011
  16. Junio C HamanoSep 16, 2011
  17. Junio C HamanoJun 7, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.