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 <bence@ferdinandy.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
d842cd1301
commit
4f07c45e25
@ -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)
|
static int set_head(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
int i, opt_a = 0, opt_d = 0, result = 0;
|
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;
|
char *head_name = NULL;
|
||||||
|
struct ref_store *refs = get_main_ref_store(the_repository);
|
||||||
|
|
||||||
struct option options[] = {
|
struct option options[] = {
|
||||||
OPT_BOOL('a', "auto", &opt_a,
|
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,
|
argc = parse_options(argc, argv, prefix, options,
|
||||||
builtin_remote_sethead_usage, 0);
|
builtin_remote_sethead_usage, 0);
|
||||||
if (argc)
|
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) {
|
if (!opt_a && !opt_d && argc == 2) {
|
||||||
head_name = xstrdup(argv[1]);
|
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);
|
head_name = xstrdup(states.heads.items[0].string);
|
||||||
free_remote_ref_states(&states);
|
free_remote_ref_states(&states);
|
||||||
} else if (opt_d && !opt_a && argc == 1) {
|
} 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))
|
if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF))
|
||||||
result |= error(_("Could not delete %s"), buf.buf);
|
result |= error(_("Could not delete %s"), b_head.buf);
|
||||||
} else
|
} else
|
||||||
usage_with_options(builtin_remote_sethead_usage, options);
|
usage_with_options(builtin_remote_sethead_usage, options);
|
||||||
|
|
||||||
if (head_name) {
|
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 */
|
/* make sure it's valid */
|
||||||
if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
|
if (!refs_ref_exists(refs, b_remote_head.buf))
|
||||||
result |= error(_("Not a valid ref: %s"), buf2.buf);
|
result |= error(_("Not a valid ref: %s"), b_remote_head.buf);
|
||||||
else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
|
else if (refs_update_symref(refs, b_head.buf, b_remote_head.buf, "remote set-head"))
|
||||||
result |= error(_("Could not set up %s"), buf.buf);
|
result |= error(_("Could not set up %s"), b_head.buf);
|
||||||
else if (opt_a)
|
else if (opt_a)
|
||||||
printf("%s/HEAD set to %s\n", argv[0], head_name);
|
printf("%s/HEAD set to %s\n", argv[0], head_name);
|
||||||
free(head_name);
|
free(head_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
strbuf_release(&buf);
|
strbuf_release(&b_head);
|
||||||
strbuf_release(&buf2);
|
strbuf_release(&b_remote_head);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user