Web lists-archives.com

Re: [PATCH 1/2] branch: introduce --current display option




Welcome to the git mailing list!

On Tue, Oct 9, 2018 at 11:31 AM Daniels Umanovskis
<daniels@xxxxxxxxxxxxx> wrote:
>
> When called with --current, git branch will print the current
> branch name and terminate. It will print HEAD in detached-head state.

How does it play with worktrees? (I would expect it to just work as expected:
each worktree would print its current branch, but a test might help?)

> Rationale: finding out the current branch is useful interactively,
> but especially in scripting. git branch --list prints many branches,
> and prepends the current one with an asterisk, meaning sed or other
> filtering is necessary to just get the current branch.
> git rev-parse --abbrev-ref HEAD is the current way to achieve this
> output, but that is not intuitive or easy to understand.

Git used to have (and still has) the approach of dividing its commands
into high level ("porcelain") commands and low level ("plumbing") commands,
with the porcelain facing the user and plumbing being good for scripting.

This patch proposes a radically different approach, which is convenience
of use.

As a scripter you'd need to find out if "branch --current" is stable in its API
or if you'd rather parse it out of the branch list, which adds a subtle burden
to the scripter, as it is convenient to leave out that part and just use what is
there. :-)

> +static void print_current_branch_name()
> +{
> +       struct strbuf out = STRBUF_INIT;
> +       const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
> +       char *shortname = shorten_unambiguous_ref(refname, 0);


> +       strbuf_addf(&out, _("%s"), shortname);
> +       fwrite(out.buf, 1, out.len, stdout);

Why do we need to add the shortname to a strbuf?
(and using _( ) that denotes the string should be translated?)
I would think we could just

    puts(shortname)

here and leave out all the strbuf out ?


> @@ -620,6 +633,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
>                 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
>                 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
>                 OPT_BOOL('l', "list", &list, N_("list branch names")),
> +               OPT_BOOL(0, "current", &current, N_("show current branch name")),

(Gah, we're not using OPT_MODE here to select for one out of many.)

Later we have it in code via

    if (!!delete + !!rename + !!new_upstream +
        list + unset_upstream > 1)
            usage_with_options(builtin_branch_usage, options);

and I would think we'd want to add in a "+ !!current" there, too.
Then we'd get the usage options when giving --current in combination
with say --move