Skip to content

Commit 4f07c45

Browse files
ferdinandybgitster
authored andcommitted
remote set-head: refactor for readability
Make two different readability refactors: Rename strbufs "buf" and "buf2" to something more explanatory. Instead of calling get_main_ref_store(the_repository) multiple times, call it once and store the result 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d842cd1 commit 4f07c45

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

builtin/remote.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,8 +1402,9 @@ static int show(int argc, const char **argv, const char *prefix)
14021402
static int set_head(int argc, const char **argv, const char *prefix)
14031403
{
14041404
int i, opt_a = 0, opt_d = 0, result = 0;
1405-
struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1405+
struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
14061406
char *head_name = NULL;
1407+
struct ref_store *refs = get_main_ref_store(the_repository);
14071408

14081409
struct option options[] = {
14091410
OPT_BOOL('a', "auto", &opt_a,
@@ -1415,7 +1416,7 @@ static int set_head(int argc, const char **argv, const char *prefix)
14151416
argc = parse_options(argc, argv, prefix, options,
14161417
builtin_remote_sethead_usage, 0);
14171418
if (argc)
1418-
strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1419+
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]);
14191420

14201421
if (!opt_a && !opt_d && argc == 2) {
14211422
head_name = xstrdup(argv[1]);
@@ -1434,25 +1435,25 @@ static int set_head(int argc, const char **argv, const char *prefix)
14341435
head_name = xstrdup(states.heads.items[0].string);
14351436
free_remote_ref_states(&states);
14361437
} else if (opt_d && !opt_a && argc == 1) {
1437-
if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
1438-
result |= error(_("Could not delete %s"), buf.buf);
1438+
if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
1439+
result |= error(_("Could not delete %s"), b_head.buf);
14391440
} else
14401441
usage_with_options(builtin_remote_sethead_usage, options);
14411442

14421443
if (head_name) {
1443-
strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1444+
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name);
14441445
/* make sure it's valid */
1445-
if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
1446-
result |= error(_("Not a valid ref: %s"), buf2.buf);
1447-
else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
1448-
result |= error(_("Could not set up %s"), buf.buf);
1446+
if (!refs_ref_exists(refs, b_remote_head.buf))
1447+
result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
1448+
else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
1449+
result |= error(_("Could not set up %s"), b_head.buf);
14491450
else if (opt_a)
14501451
printf("%s/HEAD set to %s\n", argv[0], head_name);
14511452
free(head_name);
14521453
}
14531454

1454-
strbuf_release(&buf);
1455-
strbuf_release(&buf2);
1455+
strbuf_release(&b_head);
1456+
strbuf_release(&b_remote_head);
14561457
return result;
14571458
}
14581459

0 commit comments

Comments
 (0)