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

[PATCH v2 1/2] Clean up builtin-update-ref's option parsing

From
Karl Hasselström <kha@treskal.com>
Date
Jun 2, 2008, 23:34 UTC
Message-ID
<20080602233447.26087.14533.stgit@yoghurt>
In-Reply-To
<20080602233244.26087.51827.stgit@yoghurt>

builtin-update-ref's option parsing was somewhat tricky to follow, especially if the -d option was given. This patch cleans it upp a bit, at the expense of making it a bit longer.

Signed-off-by: Karl Hasselström <kha@treskal.com>
---
 builtin-update-ref.c |   34 +++++++++++++++++++---------------
 1 files changed, 19 insertions(+), 15 deletions(-)
diff --git a/builtin-update-ref.c b/builtin-update-ref.c
index 93c1271..cc8bc5c 100644
--- a/builtin-update-ref.c
+++ b/builtin-update-ref.c
@@ -11,7 +11,7 @@ static const char * const git_update_ref_usage[] = {
 
 int cmd_update_ref(int argc, const char **argv, const char *prefix)
 {
-	const char *refname, *value, *oldval, *msg=NULL;
+	const char *refname, *oldval, *msg=NULL;
 	unsigned char sha1[20], oldsha1[20];
 	int delete = 0, no_deref = 0;
 	struct option options[] = {
@@ -27,25 +27,29 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	if (msg && !*msg)
 		die("Refusing to perform update with empty message.");
 
-	if (argc < 2 || argc > 3)
-		usage_with_options(git_update_ref_usage, options);
-	refname = argv[0];
-	value   = argv[1];
-	oldval  = argv[2];
-
-	if (get_sha1(value, sha1))
-		die("%s: not a valid SHA1", value);
-
 	if (delete) {
-		if (oldval)
+		if (argc != 2)
+			usage_with_options(git_update_ref_usage, options);
+		refname = argv[0];
+		oldval = argv[1];
+	} else {
+                const char *value;
+		if (argc < 2 || argc > 3)
 			usage_with_options(git_update_ref_usage, options);
-		return delete_ref(refname, sha1);
+		refname = argv[0];
+		value = argv[1];
+		oldval = argv[2];
+		if (get_sha1(value, sha1))
+			die("%s: not a valid SHA1", value);
 	}
 
-	hashclr(oldsha1);
+	hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
 	if (oldval && *oldval && get_sha1(oldval, oldsha1))
 		die("%s: not a valid old SHA1", oldval);
 
-	return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
-			  no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
+	if (delete)
+		return delete_ref(refname, oldsha1);
+	else
+		return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
+				  no_deref ? REF_NODEREF : 0, DIE_ON_ERR);
 }
Previous: Karl HasselströmNext: Karl Hasselström
Message 2 of 6 in “Make old sha1 optional with git update-ref -d”
  1. 0/2 Make old sha1 optional with git update-ref -dKarl Hasselström, Jun 2, 2008
  2. 1/2 Clean up builtin-update-ref's option parsingKarl Hasselström, Jun 2, 2008
  3. 2/2 Make old sha1 optional with git update-ref -dKarl Hasselström, Jun 2, 2008
  4. Junio C HamanoJun 3, 2008
  5. Karl HasselströmJun 3, 2008
  6. Junio C HamanoJun 3, 2008

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.