Skip to content

Commit 0ecb1fc

Browse files
umanovskisgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5a0cc8a commit 0ecb1fc

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

Documentation/git-branch.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git branch' [--color[=<when>] | --no-color] [-r | -a]
12-
[--list] [-v [--abbrev=<length> | --no-abbrev]]
12+
[--list] [--show-current] [-v [--abbrev=<length> | --no-abbrev]]
1313
[--column[=<options>] | --no-column] [--sort=<key>]
1414
[(--merged | --no-merged) [<commit>]]
1515
[--contains [<commit]] [--no-contains [<commit>]]
@@ -160,6 +160,10 @@ This option is only applicable in non-verbose mode.
160160
branch --list 'maint-*'`, list only the branches that match
161161
the pattern(s).
162162

163+
--show-current::
164+
Print the name of the current branch. In detached HEAD state,
165+
nothing is printed.
166+
163167
-v::
164168
-vv::
165169
--verbose::

builtin/branch.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,21 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
443443
free(to_free);
444444
}
445445

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+
446461
static void reject_rebase_or_bisect_branch(const char *target)
447462
{
448463
struct worktree **worktrees = get_worktrees(0);
@@ -581,6 +596,7 @@ static int edit_branch_description(const char *branch_name)
581596
int cmd_branch(int argc, const char **argv, const char *prefix)
582597
{
583598
int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
599+
int show_current = 0;
584600
int reflog = 0, edit_description = 0;
585601
int quiet = 0, unset_upstream = 0;
586602
const char *new_upstream = NULL;
@@ -620,6 +636,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
620636
OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
621637
OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
622638
OPT_BOOL('l', "list", &list, N_("list branch names")),
639+
OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
623640
OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
624641
OPT_BOOL(0, "edit-description", &edit_description,
625642
N_("edit the description for the branch")),
@@ -662,14 +679,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
662679
argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
663680
0);
664681

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)
666684
list = 1;
667685

668686
if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
669687
filter.no_commit)
670688
list = 1;
671689

672-
if (!!delete + !!rename + !!copy + !!new_upstream +
690+
if (!!delete + !!rename + !!copy + !!new_upstream + !!show_current +
673691
list + unset_upstream > 1)
674692
usage_with_options(builtin_branch_usage, options);
675693

@@ -697,6 +715,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
697715
if (!argc)
698716
die(_("branch name required"));
699717
return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
718+
} else if (show_current) {
719+
print_current_branch_name();
720+
return 0;
700721
} else if (list) {
701722
/* git branch --local also shows HEAD when it is detached */
702723
if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)

t/t3203-branch-output.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,50 @@ test_expect_success 'git branch -v pattern does not show branch summaries' '
100100
test_must_fail git branch -v branch*
101101
'
102102

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+
103147
test_expect_success 'git branch shows detached HEAD properly' '
104148
cat >expect <<EOF &&
105149
* (HEAD detached at $(git rev-parse --short HEAD^0))

0 commit comments

Comments
 (0)