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

[PATCH v10 3/8] remote set-head: refactor for readability

From
Bence Ferdinandy <bence@ferdinandy.com>
Date
Oct 21, 2024, 13:37 UTC
Message-ID
<20241021134354.705636-4-bence@ferdinandy.com>
In-Reply-To
<20241021134354.705636-1-bence@ferdinandy.com>
Rename buf and buf2 to something more explanatory.

Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store in a new refs variable. Although this change probably offers some performance benefits, the main purpose is to shorten the line lengths of function calls using this variable.

Signed-off-by: Bence Ferdinandy <bence@ferdinandy.com>
---
Notes:
    v5: new patch (split from the next patch as a preparatory step)
    
    v6: no change
    
    v7: - change commit prefix to be more in line with project standards
    
    v8: no change
    
    v9: - further improve readability by renaming buf, and buf2 consistently
          with how patch 6 was already done
    
    v10: no change
 builtin/remote.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/builtin/remote.c b/builtin/remote.c
index 76670ddd8b..1d68c5b2ba 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
 static int set_head(int argc, const char **argv, const char *prefix)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
-	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
+	struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
 	char *head_name = NULL;
+	struct ref_store *refs = get_main_ref_store(the_repository);
 
 	struct option options[] = {
 		OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options,
 			     builtin_remote_sethead_usage, 0);
 	if (argc)
-		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
+		strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
 
 	if (!opt_a && !opt_d && argc == 2) {
 		head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
 			head_name = xstrdup(states.heads.items[0].string);
 		free_remote_ref_states(&states);
 	} else if (opt_d && !opt_a && argc == 1) {
-		if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
-			result |= error(_("Could not delete %s"), buf.buf);
+		if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
+			result |= error(_("Could not delete %s"), b_head.buf);
 	} else
 		usage_with_options(builtin_remote_sethead_usage, options);
 
 	if (head_name) {
-		strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
+		strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
 		/* make sure it's valid */
-		if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
-			result |= error(_("Not a valid ref: %s"), buf2.buf);
-		else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
-			result |= error(_("Could not setup %s"), buf.buf);
+		if (!refs_ref_exists(refs, b_remote_head.buf))
+			result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
+		else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
+			result |= error(_("Could not setup %s"), b_head.buf);
 		else if (opt_a)
 			printf("%s/HEAD set to %s\n", argv[0], head_name);
 		free(head_name);
 	}
 
-	strbuf_release(&buf);
-	strbuf_release(&buf2);
+	strbuf_release(&b_head);
+	strbuf_release(&b_remote_head);
 	return result;
 }
 
-- 
2.47.0.94.g8861098b6d
Previous: Bence FerdinandyNext: Bence Ferdinandy
Message 4 of 15 in “set-head/fetch remote/HEAD updates”
  1. 0/8 set-head/fetch remote/HEAD updatesBence Ferdinandy, Oct 21, 2024
  2. 1/8 t/t5505-remote: set default branch to mainBence Ferdinandy, Oct 21, 2024
  3. 2/8 refs: atomically record overwritten ref in update_symrefBence Ferdinandy, Oct 21, 2024
  4. 3/8 remote set-head: refactor for readabilityBence Ferdinandy, Oct 21, 2024
  5. 4/8 remote set-head: better output for --autoBence Ferdinandy, Oct 21, 2024
  6. 5/8 refs: add TRANSACTION_CREATE_EXISTS errorBence Ferdinandy, Oct 21, 2024
  7. 6/8 refs: add create_only option to refs_update_symref_extendedBence Ferdinandy, Oct 21, 2024
  8. 8/8 fetch set_head: handle mirrored bare repositoriesBence Ferdinandy, Oct 21, 2024
  9. 7/8 fetch: set remote/HEAD if it does not existBence Ferdinandy, Oct 21, 2024
  10. Taylor BlauOct 21, 2024
  11. Bence FerdinandyOct 21, 2024
  12. Taylor BlauOct 22, 2024
  13. Bence FerdinandyOct 22, 2024
  14. Taylor BlauOct 22, 2024
  15. Bence FerdinandyOct 22, 2024

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.