branch: introduce --show-current display option

When called with --show-current, git branch will print the current branch name and terminate. Only the actual name gets printed, without refs/heads. In detached HEAD state, nothing is output. Intended both for scripting and interactive/informative use. Unlike git branch --list, no filtering is needed to just get the branch name. Signed-off-by: Daniels Umanovskis <daniels@umanovskis.se> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniels Umanovskis committed Oct 25, 2018 at 21:04 UTC 0ecb1fc7269e15be890870937b8b639c987abd08
3 files changed +72 -3
Documentation/git-branch.txt
+5 -1
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git branch' [--color[=<when>] | --no-color] [-r | -a]
12 - [--list] [-v [--abbrev=<length> | --no-abbrev]]
12 + [--list] [--show-current] [-v [--abbrev=<length> | --no-abbrev]]
13 [--column[=<options>] | --no-column] [--sort=<key>]
14 [(--merged | --no-merged) [<commit>]]
15 [--contains [<commit]] [--no-contains [<commit>]]
@@ -160,6 +160,10 @@ This option is only applicable in non-verbose mode.
160 branch --list 'maint-*'`, list only the branches that match
161 the pattern(s).
162
163 +--show-current::
164 + Print the name of the current branch. In detached HEAD state,
165 + nothing is printed.
166 +
167 -v::
168 -vv::
169 --verbose::
builtin/branch.c
+23 -2
@@ -443,6 +443,21 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
443 free(to_free);
444 }
445
446 +static void print_current_branch_name(void)
447 +{
448 + int flags;
449 + const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
450 + const char *shortname;
451 + if (!refname)
452 + die(_("could not resolve HEAD"));
453 + else if (!(flags & REF_ISSYMREF))
454 + return;
455 + else if (skip_prefix(refname, "refs/heads/", &shortname))
456 + puts(shortname);
457 + else
458 + die(_("HEAD (%s) points outside of refs/heads/"), refname);
459 +}
460 +
461 static void reject_rebase_or_bisect_branch(const char *target)
462 {
463 struct worktree **worktrees = get_worktrees(0);
@@ -581,6 +596,7 @@ static int edit_branch_description(const char *branch_name)
596 int cmd_branch(int argc, const char **argv, const char *prefix)
597 {
598 int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
599 + int show_current = 0;
600 int reflog = 0, edit_description = 0;
601 int quiet = 0, unset_upstream = 0;
602 const char *new_upstream = NULL;
@@ -620,6 +636,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
636 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
637 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
638 OPT_BOOL('l', "list", &list, N_("list branch names")),
639 + OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
640 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
641 OPT_BOOL(0, "edit-description", &edit_description,
642 N_("edit the description for the branch")),
@@ -662,14 +679,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
679 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
680 0);
681
665 - if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
682 + if (!delete && !rename && !copy && !edit_description && !new_upstream &&
683 + !show_current && !unset_upstream && argc == 0)
684 list = 1;
685
686 if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
687 filter.no_commit)
688 list = 1;
689
672 - if (!!delete + !!rename + !!copy + !!new_upstream +
690 + if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
691 list + unset_upstream > 1)
692 usage_with_options(builtin_branch_usage, options);
693
@@ -697,6 +715,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
715 if (!argc)
716 die(_("branch name required"));
717 return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
718 + } else if (show_current) {
719 + print_current_branch_name();
720 + return 0;
721 } else if (list) {
722 /* git branch --local also shows HEAD when it is detached */
723 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
t/t3203-branch-output.sh
+44
@@ -100,6 +100,50 @@ test_expect_success 'git branch -v pattern does not show branch summaries' '
100 test_must_fail git branch -v branch*
101 '
102
103 +test_expect_success 'git branch `--show-current` shows current branch' '
104 + cat >expect <<-\EOF &&
105 + branch-two
106 + EOF
107 + git checkout branch-two &&
108 + git branch --show-current >actual &&
109 + test_cmp expect actual
110 +'
111 +
112 +test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
113 + git checkout HEAD^0 &&
114 + git branch --show-current >actual &&
115 + test_must_be_empty actual
116 +'
117 +
118 +test_expect_success 'git branch `--show-current` works properly when tag exists' '
119 + cat >expect <<-\EOF &&
120 + branch-and-tag-name
121 + EOF
122 + test_when_finished "
123 + git checkout branch-one
124 + git branch -D branch-and-tag-name
125 + " &&
126 + git checkout -b branch-and-tag-name &&
127 + test_when_finished "git tag -d branch-and-tag-name" &&
128 + git tag branch-and-tag-name &&
129 + git branch --show-current >actual &&
130 + test_cmp expect actual
131 +'
132 +
133 +test_expect_success 'git branch `--show-current` works properly with worktrees' '
134 + cat >expect <<-\EOF &&
135 + branch-one
136 + branch-two
137 + EOF
138 + git checkout branch-one &&
139 + git worktree add worktree branch-two &&
140 + {
141 + git branch --show-current &&
142 + git -C worktree branch --show-current
143 + } >actual &&
144 + test_cmp expect actual
145 +'
146 +
147 test_expect_success 'git branch shows detached HEAD properly' '
148 cat >expect <<EOF &&
149 * (HEAD detached at $(git rev-parse --short HEAD^0))