Re: [PATCH v4] branch: support for shortcuts like @{-1}, completed
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Oct 8, 2022, 04:17 UTC
- Message-ID
- <xmqq8rlrc5jp.fsf@gitster.g>
- In-Reply-To
- <2e164aea-7dd8-5018-474a-01643553ea49@gmail.com>
Rubén Justo <rjusto@gmail.com> writes:
Show 33 quoted lines
> } else if (new_upstream) {
> - struct branch *branch = branch_get(argv[0]);
> + struct branch *branch;
> + struct strbuf buf = STRBUF_INIT;
>
> - if (argc > 1)
> + if (!argc)
> + branch = branch_get(NULL);
> + else if (argc == 1) {
> + strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
> + branch = branch_get(buf.buf);
> + } else
> die(_("too many arguments to set new upstream"));
>
> if (!branch) {
> @@ -854,11 +867,17 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
> dwim_and_setup_tracking(the_repository, branch->name,
> new_upstream, BRANCH_TRACK_OVERRIDE,
> quiet);
> + strbuf_release(&buf);
> } else if (unset_upstream) {
> - struct branch *branch = branch_get(argv[0]);
> + struct branch *branch;
> struct strbuf buf = STRBUF_INIT;
>
> - if (argc > 1)
> + if (!argc)
> + branch = branch_get(NULL);
> + else if (argc == 1) {
> + strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
> + branch = branch_get(buf.buf);
> + } else
> die(_("too many arguments to unset upstream"));The above two are a bit repetitious. A helper like
static struct branch *interpret_local(int ac, const char **av)
{
struct branch *branch;
if (!ac) {
branch = branch_get(NULL);
} else if (ac == 1) {
struct strbuf buf = STRBUF_INIT;
strbuf_branchname(&buf, av[0], INTERPRET_BRANCH_LOCAL);
branch = branch_get(buf.buf);
strbuf_release(&buf);
} else {
die(_("too many arguments"));
}
return branch;
}might be useful, and it frees the callers from worrying about temporary allocations.
But the code written is OK as-is.