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

[PATCH 2/3] match_explicit_lhs: allow a "verify only" mode

From
Jeff King <peff@peff.net>
Date
Mar 5, 2014, 19:03 UTC
Message-ID
<20140305190343.GB11039@sigill.intra.peff.net>
In-Reply-To
<20140305190248.GB31252@sigill.intra.peff.net>

The match_explicit_lhs function has all of the logic necessary to verify the refspecs without actually doing any work. This patch lets callers pass a NULL "match" pointer to indicate they want a "verify only" operation.

For the most part, we just need to avoid writing to the NULL pointer. However, we also have to refactor the try_explicit_object_name sub-function; it indicates success by allocating and returning a new ref. Instead, we give it an "out" parameter for the match and return a numeric status.

Signed-off-by: Jeff King <peff@peff.net>
---
 remote.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/remote.c b/remote.c
index 6aa9dd2..b6586c0 100644
--- a/remote.c
+++ b/remote.c
@@ -1031,11 +1031,13 @@ int count_refspec_match(const char *pattern,
 		}
 	}
 	if (!matched) {
-		*matched_ref = matched_weak;
+		if (matched_ref)
+			*matched_ref = matched_weak;
 		return weak_match;
 	}
 	else {
-		*matched_ref = matched;
+		if (matched_ref)
+			*matched_ref = matched;
 		return match;
 	}
 }
@@ -1055,18 +1057,25 @@ static struct ref *alloc_delete_ref(void)
 	return ref;
 }
 
-static struct ref *try_explicit_object_name(const char *name)
+static int try_explicit_object_name(const char *name,
+				    struct ref **match)
 {
 	unsigned char sha1[20];
-	struct ref *ref;
 
-	if (!*name)
-		return alloc_delete_ref();
+	if (!*name) {
+		if (match)
+			*match = alloc_delete_ref();
+		return 0;
+	}
+
 	if (get_sha1(name, sha1))
-		return NULL;
-	ref = alloc_ref(name);
-	hashcpy(ref->new_sha1, sha1);
-	return ref;
+		return -1;
+
+	if (match) {
+		*match = alloc_ref(name);
+		hashcpy((*match)->new_sha1, sha1);
+	}
+	return 0;
 }
 
 static struct ref *make_linked_ref(const char *name, struct ref ***tail)
@@ -1103,17 +1112,18 @@ static int match_explicit_lhs(struct ref *src,
 {
 	switch (count_refspec_match(rs->src, src, match)) {
 	case 1:
-		*allocated_match = 0;
+		if (allocated_match)
+			*allocated_match = 0;
 		return 0;
 	case 0:
 		/* The source could be in the get_sha1() format
 		 * not a reference name.  :refs/other is a
 		 * way to delete 'other' ref at the remote end.
 		 */
-		*match = try_explicit_object_name(rs->src);
-		if (!*match)
+		if (try_explicit_object_name(rs->src, match) < 0)
 			return error("src refspec %s does not match any.", rs->src);
-		*allocated_match = 1;
+		if (allocated_match)
+			*allocated_match = 1;
 		return 0;
 	default:
 		return error("src refspec %s matches more than one.", rs->src);
-- 
1.8.5.2.500.g8060133
Previous: Jeff KingNext: Jeff King
Message 4 of 7 in “git push first asks for credentials, then checks the branch exists”
  1. DmitryMar 5, 2014
  2. 0/3 push: detect local refspec errors earlyJeff King, Mar 5, 2014
  3. 1/3 match_explicit: hoist refspec lhs checks into their own functionJeff King, Mar 5, 2014
  4. 2/3 match_explicit_lhs: allow a "verify only" modeJeff King, Mar 5, 2014
  5. 3/3 push: detect local refspec errors earlyJeff King, Mar 5, 2014
  6. Junio C HamanoMar 5, 2014
  7. Jeff KingMar 5, 2014

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.