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

[PATCH 10/12] names_conflict(): new function, extracted from is_refname_available()

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Oct 19, 2011, 21:44 UTC
Message-ID
<1319060692-27216-11-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1319060692-27216-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>

This costs an extra strlen() in the loop, but even that small price will be clawed back in the next patch.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   43 +++++++++++++++++++++++++++++++------------
 1 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/refs.c b/refs.c
index 7c2bcab..ada691b 100644
--- a/refs.c
+++ b/refs.c
@@ -1088,6 +1088,30 @@ static int remove_empty_directories(const char *file)
 }
 
 /*
+ * Return true iff refname1 and refname2 conflict with each other.
+ * Two reference names conflict if one of them exactly matches the
+ * leading components of the other; e.g., "foo/bar" conflicts with
+ * both "foo" and with "foo/bar/baz" but not with "foo/bar" or
+ * "foo/barbados".
+ */
+static int names_conflict(const char *refname1, const char *refname2)
+{
+	int len1 = strlen(refname1);
+	int len2 = strlen(refname2);
+	int cmplen;
+	const char *lead;
+
+	if (len1 < len2) {
+		cmplen = len1;
+		lead = refname2;
+	} else {
+		cmplen = len2;
+		lead = refname1;
+	}
+	return !strncmp(refname1, refname2, cmplen) && lead[cmplen] == '/';
+}
+
+/*
  * Return true iff a reference named refname could be created without
  * conflicting with the name of an existing reference.  If oldrefname
  * is non-NULL, ignore potential conflicts with oldrefname (e.g.,
@@ -1097,20 +1121,15 @@ static int remove_empty_directories(const char *file)
 static int is_refname_available(const char *refname, const char *oldrefname,
 				struct ref_array *array)
 {
-	int i, namlen = strlen(refname); /* e.g. 'foo/bar' */
+	int i;
 	for (i = 0; i < array->nr; i++ ) {
 		struct ref_entry *entry = array->refs[i];
-		/* entry->name could be 'foo' or 'foo/bar/baz' */
-		if (!oldrefname || strcmp(oldrefname, entry->name)) {
-			int len = strlen(entry->name);
-			int cmplen = (namlen < len) ? namlen : len;
-			const char *lead = (namlen < len) ? entry->name : refname;
-			if (!strncmp(refname, entry->name, cmplen) &&
-			    lead[cmplen] == '/') {
-				error("'%s' exists; cannot create '%s'",
-				      entry->name, refname);
-				return 0;
-			}
+		if (oldrefname && !strcmp(oldrefname, entry->name))
+			continue;
+		if (names_conflict(refname, entry->name)) {
+			error("'%s' exists; cannot create '%s'",
+			      entry->name, refname);
+			return 0;
 		}
 	}
 	return 1;
-- 
1.7.7
Previous: mhagger@alum.mit.eduNext: mhagger@alum.mit.edu
Message 13 of 18 in “Use refs API more consistently”
  1. 00/12 Use refs API more consistentlymhagger@alum.mit.edu, Oct 19, 2011
  2. 01/12 Rename another local variable name -> refnamemhagger@alum.mit.edu, Oct 19, 2011
  3. 02/12 repack_without_ref(): remove temporarymhagger@alum.mit.edu, Oct 19, 2011
  4. 03/12 parse_ref_line(): add a check that the refname is properly formattedmhagger@alum.mit.edu, Oct 19, 2011
  5. 04/12 create_ref_entry(): extract function from add_ref()mhagger@alum.mit.edu, Oct 19, 2011
  6. 05/12 add_ref(): take a (struct ref_entry *) parametermhagger@alum.mit.edu, Oct 19, 2011
  7. 06/12 do_for_each_ref(): correctly terminate while processesing extra_refsmhagger@alum.mit.edu, Oct 19, 2011
  8. 07/12 do_for_each_ref_in_array(): new functionmhagger@alum.mit.edu, Oct 19, 2011
  9. 08/12 do_for_each_ref_in_arrays(): new functionmhagger@alum.mit.edu, Oct 19, 2011
  10. Junio C HamanoOct 19, 2011
  11. Michael HaggertyOct 20, 2011
  12. 09/12 repack_without_ref(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Oct 19, 2011
  13. 10/12 names_conflict(): new function, extracted from is_refname_available()mhagger@alum.mit.edu, Oct 19, 2011
  14. 11/12 names_conflict(): simplify implementationmhagger@alum.mit.edu, Oct 19, 2011
  15. 12/12 is_refname_available(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Oct 19, 2011
  16. Junio C HamanoOct 20, 2011
  17. Michael HaggertyOct 20, 2011
  18. Michael HaggertyOct 24, 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.