# [PATCH] Introduce --current option to git-branch builtin command.

5 messages from 2012-06-07 to 2012-06-07. Participants: Kenta Murata (村田 賢太), Vincent van Ravesteijn, konglu@minatec.inpg.fr, Nguyen Thai Ngoc Duy, Junio C Hamano.
Thread: https://gitlist.dev/t/30733

## Kenta Murata (村田 賢太), 2012-06-07 07:23

Subject: [PATCH] Introduce --current option to git-branch builtin command.
Message-ID: <CAECJ0BmcxeS1q8bw4hN9jDja2g8GGzNyN_v7jC3ehOW70x6CRA@mail.gmail.com>
URL: https://gitlist.dev/e/CAECJ0BmcxeS1q8bw4hN9jDja2g8GGzNyN_v7jC3ehOW70x6CRA%40mail.gmail.com

```
Introducing --current option to git-branch builtin command.
This option allows us to simply show the current branch name.

---
 builtin/branch.c |   12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 0e060f2..21e4675 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -19,6 +19,7 @@
 #include "column.h"

 static const char * const builtin_branch_usage[] = {
+       "git branch --current",
        "git branch [options] [-r | -a] [--merged | --no-merged]",
        "git branch [options] [-l] [-f] <branchname> [<start-point>]",
        "git branch [options] [-r] (-d | -D) <branchname>...",
@@ -711,7 +712,7 @@ int cmd_branch(int argc, const char **argv, const
char *prefix)
 {
        int delete = 0, rename = 0, force_create = 0, list = 0;
        int verbose = 0, abbrev = -1, detached = 0;
-       int reflog = 0, edit_description = 0;
+       int reflog = 0, edit_description = 0, show_current = 0;
        int quiet = 0;
        enum branch_track track;
        int kinds = REF_LOCAL_BRANCH;
@@ -768,6 +769,7 @@ int cmd_branch(int argc, const char **argv, const
char *prefix)
                        opt_parse_merge_filter, (intptr_t) "HEAD",
                },
                OPT_COLUMN(0, "column", &colopts, "list branches in columns"),
+               OPT_BOOLEAN(0, "current", &show_current, "show current
branch only"),
                OPT_END(),
        };

@@ -794,7 +796,7 @@ int cmd_branch(int argc, const char **argv, const
char *prefix)
        argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
                             0);

-       if (!delete && !rename && !edit_description && argc == 0)
+       if (!delete && !rename && !edit_description && !show_current
&& argc == 0)
                list = 1;

        if (!!delete + !!rename + !!force_create + !!list > 1)
@@ -852,6 +854,12 @@ int cmd_branch(int argc, const char **argv, const
char *prefix)
                        rename_branch(argv[0], argv[1], rename > 1);
                else
                        usage_with_options(builtin_branch_usage, options);
+       } else if (show_current) {
+               const char *branch_name = head;
+               if (detached) {
+                       branch_name = _("(no branch)");
+               }
+               printf("%s\n", branch_name);
        } else if (argc > 0 && argc <= 2) {
                if (kinds != REF_LOCAL_BRANCH)
                        die(_("-a and -r options to 'git branch' do
not make sense with a branch name"));


-- 
Kenta Murata

COOKPAD Inc.
http://cookpad.com

MG Shirokanedai Bld. 5F, 5-12-7, Shirokanedai
Minato, Tokyo, 180-0071 Japan

```

## Vincent van Ravesteijn, 2012-06-07 08:43

Subject: Re: [PATCH] Introduce --current option to git-branch builtin command.
Message-ID: <4FD069A5.1040905@lyx.org>
URL: https://gitlist.dev/e/4FD069A5.1040905%40lyx.org
In-Reply-To: <CAECJ0BmcxeS1q8bw4hN9jDja2g8GGzNyN_v7jC3ehOW70x6CRA@mail.gmail.com>

```
Op 7-6-2012 9:23, Kenta Murata (村田 賢太) schreef:
> Introducing --current option to git-branch builtin command.
> This option allows us to simply show the current branch name.

I don't know whether we would need such an option. To me it feels this 
is a missing option indeed, but at the same time you can use 'git 
status' or 'git branch' to see the current branch. It might be useful in 
case you have lots of branches and lots of untracked files, but in that 
case "git status | head -n1" or "git branch | grep '*'" would do the trick.

>
> ---
>   builtin/branch.c |   12 ++++++++++--
>   1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/branch.c b/builtin/branch.c
> index 0e060f2..21e4675 100644
> --- a/builtin/branch.c
> +++ b/builtin/branch.c
> @@ -19,6 +19,7 @@
>   #include "column.h"
>
>   static const char * const builtin_branch_usage[] = {
> +       "git branch --current",
>          "git branch [options] [-r | -a] [--merged | --no-merged]",
>          "git branch [options] [-l] [-f]<branchname>  [<start-point>]",
>          "git branch [options] [-r] (-d | -D)<branchname>...",

git-branch has four modes: 'list', 'create', 'delete', and 'rename'. 
Isn't --current then just another option to the 'list'-mode?  Then it 
would be  something like:

"git branch [options] [-r | -a] [--current] [--merged | --no-merged]"


> @@ -794,7 +796,7 @@ int cmd_branch(int argc, const char **argv, const
> char *prefix)
>          argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
>                               0);
>
> -       if (!delete&&  !rename&&  !edit_description&&  argc == 0)
> +       if (!delete&&  !rename&&  !edit_description&&  !show_current
> &&  argc == 0)
>                  list = 1;

In line with what I wrote above, show_current should imply 'list'-mode.

>
>          if (!!delete + !!rename + !!force_create + !!list>  1)
> @@ -852,6 +854,12 @@ int cmd_branch(int argc, const char **argv, const
> char *prefix)
>                          rename_branch(argv[0], argv[1], rename>  1);
>                  else
>                          usage_with_options(builtin_branch_usage, options);
> +       } else if (show_current) {
> +               const char *branch_name = head;
> +               if (detached) {
> +                       branch_name = _("(no branch)");
> +               }
> +               printf("%s\n", branch_name);
>          } else if (argc>  0&&  argc<= 2) {
>                  if (kinds != REF_LOCAL_BRANCH)
>                          die(_("-a and -r options to 'git branch' do
> not make sense with a branch name"));

If '--current' implies the 'list'-mode, it would use 'print_ref_list'. 
There already is code in print_ref_list to detect whether a branch is 
the current one, it outputs the branch list in a formatted way, it takes 
care of 'columns', so wouldn't it be better to modify that code to only 
print the current branch ?

Vincent

```

## konglu@minatec.inpg.fr, 2012-06-07 11:18

Subject: Re: [PATCH] Introduce --current option to git-branch builtin command.
Message-ID: <20120607131800.Horde.yryrAXwdC4BP0I3o4goH21A@webmail.minatec.grenoble-inp.fr>
URL: https://gitlist.dev/e/20120607131800.Horde.yryrAXwdC4BP0I3o4goH21A%40webmail.minatec.grenoble-inp.fr
In-Reply-To: <CAECJ0BmcxeS1q8bw4hN9jDja2g8GGzNyN_v7jC3ehOW70x6CRA@mail.gmail.com>

```

"Kenta Murata (?? ??)" <mrkn@cookpad.com> a écrit :

> Introducing --current option to git-branch builtin command.
> This option allows us to simply show the current branch name.

In which case would you need to use this option ? Running 'git
branch' is faster and would do the same thing. Even though you
have a lot of branches as Vincent said (and so the output of
'git branch' would be quite verbose), I do not think that adding
an option just for that is relevant.

Lucien Kong

```

## Nguyen Thai Ngoc Duy, 2012-06-07 11:23

Subject: Re: [PATCH] Introduce --current option to git-branch builtin command.
Message-ID: <CACsJy8Bn2S4UT5ctMBNNB_Wf8J00nGw38F6O0P-jb3dpXEcs9w@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8Bn2S4UT5ctMBNNB_Wf8J00nGw38F6O0P-jb3dpXEcs9w%40mail.gmail.com
In-Reply-To: <CAECJ0BmcxeS1q8bw4hN9jDja2g8GGzNyN_v7jC3ehOW70x6CRA@mail.gmail.com>

```
On Thu, Jun 7, 2012 at 2:23 PM, Kenta Murata (村田 賢太) <mrkn@cookpad.com> wrote:
> Introducing --current option to git-branch builtin command.
> This option allows us to simply show the current branch name.

or just add an alias that does "git symbolic-ref HEAD" (append "|sed
s,^refs/heads,," if you don't like full ref)
-- 
Duy

```

## Junio C Hamano, 2012-06-07 17:11

Subject: Re: [PATCH] Introduce --current option to git-branch builtin command.
Message-ID: <7v4nqnjbcc.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v4nqnjbcc.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4FD069A5.1040905@lyx.org>

```
Vincent van Ravesteijn <vfr@lyx.org> writes:

> git-branch has four modes: 'list', 'create', 'delete', and
> rename'. Isn't --current then just another option to the 'list'-mode?

That is how I would look at it.  We can think of various ways that
"list" can be filtered, and matching by name is one that we have an
implementation for.  "Current-only" could be another one.

> If '--current' implies the 'list'-mode, it would use
> print_ref_list'. There already is code in print_ref_list to detect
> whether a branch is the current one, it outputs the branch list in a
> formatted way, it takes care of 'columns', so wouldn't it be better to
> modify that code to only print the current branch ?

Exactly.

```
