From: Bence Ferdinandy <bence@ferdinandy.com>
To: git@vger.kernel.org
Cc: "Fővárosi Vízművek Zrt." <noreply@vizmuvek.hu>,
"Bence Ferdinandy" <bence@ferdinandy.com>
Subject: [PATCH v10 3/8] remote set-head: refactor for readability
Date: Mon, 21 Oct 2024 15:37:00 +0200 [thread overview]
Message-ID: <20241021134354.705636-4-bence@ferdinandy.com> (raw)
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
next prev parent reply other threads:[~2024-10-21 13:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <1088915169.629942.1729445083543@FVRT-HAMMYAS-P.vizmuvek.hu>
2024-10-21 13:36 ` [PATCH v10 0/8] set-head/fetch remote/HEAD updates Bence Ferdinandy
2024-10-21 13:36 ` [PATCH v10 1/8] t/t5505-remote: set default branch to main Bence Ferdinandy
2024-10-21 13:36 ` [PATCH v10 2/8] refs: atomically record overwritten ref in update_symref Bence Ferdinandy
2024-10-21 13:37 ` Bence Ferdinandy [this message]
2024-10-21 13:37 ` [PATCH v10 4/8] remote set-head: better output for --auto Bence Ferdinandy
2024-10-21 13:37 ` [PATCH v10 5/8] refs: add TRANSACTION_CREATE_EXISTS error Bence Ferdinandy
2024-10-21 13:37 ` [PATCH v10 6/8] refs: add create_only option to refs_update_symref_extended Bence Ferdinandy
2024-10-21 13:37 ` [PATCH v10 7/8] fetch: set remote/HEAD if it does not exist Bence Ferdinandy
2024-10-21 13:37 ` [PATCH v10 8/8] fetch set_head: handle mirrored bare repositories Bence Ferdinandy
2024-10-21 20:43 ` [PATCH v10 0/8] set-head/fetch remote/HEAD updates Taylor Blau
2024-10-21 21:06 ` Bence Ferdinandy
2024-10-22 17:39 ` Taylor Blau
2024-10-22 18:33 ` Bence Ferdinandy
2024-10-22 19:04 ` Taylor Blau
2024-10-22 19:09 ` Bence Ferdinandy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241021134354.705636-4-bence@ferdinandy.com \
--to=bence@ferdinandy.com \
--cc=git@vger.kernel.org \
--cc=noreply@vizmuvek.hu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.