Merge branch 'du/branch-show-current'
authorJunio C Hamano <gitster@pobox.com>
Thu, 7 Mar 2019 00:59:53 +0000 (09:59 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Mar 2019 00:59:54 +0000 (09:59 +0900)
"git branch" learned a new subcommand "--show-current".

* du/branch-show-current:
branch: introduce --show-current display option

1  2 
Documentation/git-branch.txt
builtin/branch.c
index 3bd83a7cbdd9d62c9b10057bd4bf3f03d57b1085,0babb9b1be4db9727694e706835434a944e2754e..0cd87ddeff205ac6b5a92260f690e642f16d0875
@@@ -9,7 -9,7 +9,7 @@@ SYNOPSI
  --------
  [verse]
  'git branch' [--color[=<when>] | --no-color] [-r | -a]
-       [--list] [-v [--abbrev=<length> | --no-abbrev]]
+       [--list] [--show-current] [-v [--abbrev=<length> | --no-abbrev]]
        [--column[=<options>] | --no-column] [--sort=<key>]
        [(--merged | --no-merged) [<commit>]]
        [--contains [<commit]] [--no-contains [<commit>]]
@@@ -160,6 -160,10 +160,10 @@@ This option is only applicable in non-v
        branch --list 'maint-*'`, list only the branches that match
        the pattern(s).
  
+ --show-current::
+       Print the name of the current branch. In detached HEAD state,
+       nothing is printed.
  -v::
  -vv::
  --verbose::
@@@ -297,7 -301,7 +301,7 @@@ $ git checkout my2.6.1
  ------------
  +
  <1> This step and the next one could be combined into a single step with
 -"checkout -b my2.6.14 v2.6.14".
 +    "checkout -b my2.6.14 v2.6.14".
  
  Delete an unneeded branch::
  +
@@@ -309,10 -313,10 +313,10 @@@ $ git branch -D tes
  ------------
  +
  <1> Delete the remote-tracking branches "todo", "html" and "man". The next
 -'fetch' or 'pull' will create them again unless you configure them not to.
 -See linkgit:git-fetch[1].
 +    'fetch' or 'pull' will create them again unless you configure them not to.
 +    See linkgit:git-fetch[1].
  <2> Delete the "test" branch even if the "master" branch (or whichever branch
 -is currently checked out) does not have all commits from the test branch.
 +    is currently checked out) does not have all commits from the test branch.
  
  
  NOTES
diff --combined builtin/branch.c
index 1be727209b7ceaead46d0bc5e4b9666fbe7ef7a1,46f91dc06dc64c2b24ae41ac1e57488f118e255e..4c830557306398360e06f0e53166e78a4c590da4
@@@ -443,6 -443,21 +443,21 @@@ static void print_ref_list(struct ref_f
        free(to_free);
  }
  
+ static void print_current_branch_name(void)
+ {
+       int flags;
+       const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
+       const char *shortname;
+       if (!refname)
+               die(_("could not resolve HEAD"));
+       else if (!(flags & REF_ISSYMREF))
+               return;
+       else if (skip_prefix(refname, "refs/heads/", &shortname))
+               puts(shortname);
+       else
+               die(_("HEAD (%s) points outside of refs/heads/"), refname);
+ }
  static void reject_rebase_or_bisect_branch(const char *target)
  {
        struct worktree **worktrees = get_worktrees(0);
@@@ -581,6 -596,7 +596,7 @@@ static int edit_branch_description(cons
  int cmd_branch(int argc, const char **argv, const char *prefix)
  {
        int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
+       int show_current = 0;
        int reflog = 0, edit_description = 0;
        int quiet = 0, unset_upstream = 0;
        const char *new_upstream = NULL;
                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, "show-current", &show_current, N_("show current branch name")),
                OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
                OPT_BOOL(0, "edit-description", &edit_description,
                         N_("edit the description for the branch")),
        argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
                             0);
  
-       if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
+       if (!delete && !rename && !copy && !edit_description && !new_upstream &&
+           !show_current && !unset_upstream && argc == 0)
                list = 1;
  
        if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
            filter.no_commit)
                list = 1;
  
-       if (!!delete + !!rename + !!copy + !!new_upstream +
+       if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
            list + unset_upstream > 1)
                usage_with_options(builtin_branch_usage, options);
  
                if (!argc)
                        die(_("branch name required"));
                return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
+       } else if (show_current) {
+               print_current_branch_name();
+               return 0;
        } else if (list) {
                /*  git branch --local also shows HEAD when it is detached */
                if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
                print_columns(&output, colopts, NULL);
                string_list_clear(&output, 0);
                return 0;
 -      }
 -      else if (edit_description) {
 +      } else if (edit_description) {
                const char *branch_name;
                struct strbuf branch_ref = STRBUF_INIT;
  
                 * create_branch takes care of setting up the tracking
                 * info and making sure new_upstream is correct
                 */
 -              create_branch(branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
 +              create_branch(the_repository, branch->name, new_upstream,
 +                            0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
        } else if (unset_upstream) {
                struct branch *branch = branch_get(argv[0]);
                struct strbuf buf = STRBUF_INIT;
                git_config_set_multivar(buf.buf, NULL, NULL, 1);
                strbuf_release(&buf);
        } else if (argc > 0 && argc <= 2) {
 -              struct branch *branch = branch_get(argv[0]);
 -
 -              if (!branch)
 -                      die(_("no such branch '%s'"), argv[0]);
 -
                if (filter.kind != FILTER_REFS_BRANCHES)
                        die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
  
                if (track == BRANCH_TRACK_OVERRIDE)
                        die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
  
 -              create_branch(argv[0], (argc == 2) ? argv[1] : head,
 +              create_branch(the_repository,
 +                            argv[0], (argc == 2) ? argv[1] : head,
                              force, 0, reflog, quiet, track);
  
        } else