[v3] branch: introduce --show-current display option
diff mbox series

Message ID 20181011222028.20008-1-daniels@umanovskis.se
State New
Headers show
Series
  • [v3] branch: introduce --show-current display option
Related show

Commit Message

Daniels Umanovskis Oct. 11, 2018, 10:20 p.m. UTC
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>
---

Cleaned up per suggestions, explicitly passing flags to clearly
denote intent. If you consider the patch good conceptually, this
implementation should hopefully be good enough to include.

 Documentation/git-branch.txt |  6 +++++-
 builtin/branch.c             | 25 ++++++++++++++++++++--
 t/t3203-branch-output.sh     | 41 ++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 3 deletions(-)

Comments

Junio C Hamano Oct. 11, 2018, 11:15 p.m. UTC | #1
Daniels Umanovskis <daniels@umanovskis.se> writes:

> +static void print_current_branch_name(void)

Thanks for fixing this (I fixed this in the previous round in my
tree but forgot to tell you about it).

> diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
> index ee6787614..8d2020aea 100755
> --- a/t/t3203-branch-output.sh
> +++ b/t/t3203-branch-output.sh
> @@ -100,6 +100,47 @@ test_expect_success 'git branch -v pattern does not show branch summaries' '
>  	test_must_fail git branch -v branch*
>  '
>  
> +test_expect_success 'git branch `--show-current` shows current branch' '
> +	cat >expect <<-\EOF &&
> +	branch-two
> +	EOF
> +	git checkout branch-two &&
> +	git branch --show-current >actual &&
> +	test_cmp expect actual
> +'

OK, that's trivial.  We checkout a branch and make sure show-current
reports the name of that branch.  Good.

> +test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
> +	git checkout HEAD^0 &&
> +	git branch --show-current >actual &&
> +	test_must_be_empty actual
> +'

OK, and at the same time we make sure the command exits with
success.  Good.

> +test_expect_success 'git branch `--show-current` works properly when tag exists' '
> +	cat >expect <<-\EOF &&
> +	branch-and-tag-name
> +	EOF
> +	git checkout -b branch-and-tag-name &&
> +	git tag branch-and-tag-name &&
> +	git branch --show-current >actual &&
> +	git checkout branch-one &&
> +	git branch -d branch-and-tag-name &&
> +	test_cmp expect actual
> +'

It is a bit curious why you remove the branch but not the tag after
this test.  If we are cleaning after ourselves, removing both would
be equally good, if not cleaner.  If having both absolutely harms
later tests but having just one is OK, then any failure in this test
between the time branch-and-tag-name tag gets created and the time
branch-and-tag-name branch gets removed will leave the repository
with both the tag and the branch, which will be the state in which
later tests start, so having "branch -d" at this spot in the sequence
is not a good idea anyway.

So two equally valid choices are to remove "branch -d" and then
either:

 (1) leave both branch and tag after this test in the test
     repository

 (2) use test_when_finished, i.e.

	echo branch-and-tag-name >expect &&
	test_when_finished "git branch -D branch-and-tag-name" &&
	git checkout -b branch-and-tag-name &&
	test_when_finished "git tag -d branch-and-tag-name" &&
	git tag branch-and-tag-name &&
	...

     to arrange them to be cleaned once this test is done.

(1) is only valid if they do not harm later tests.  I guess you
remove the branch because you did not want to touch later tests that
checks output from "git branch --list", in which case you'd want (2).

> +test_expect_success 'git branch `--show-current` works properly with worktrees' '
> +	cat >expect <<-\EOF &&
> +	branch-one
> +	branch-two
> +	EOF
> +	git checkout branch-one &&
> +	git branch --show-current >actual &&
> +	git worktree add worktree branch-two &&
> +	cd worktree &&
> +	git branch --show-current >>../actual &&
> +	cd .. &&
> +	test_cmp expect actual
> +'

Please do *not* cd around without being in a subshell.  If the
second --show-current failed for some reason, "cd .." will not be
executed, and the next and subsequent test will start inside
./worktree subdirectory, which is likely to break the expectations
of them.  Perhaps something like

	git checkout branch-one &&
	git worktree add worktree branch-two &&
	(
		git branch --show-current &&
		cd worktree && git branch --show-current
	) >actual &&
	test_cmp expect actual

or its modern equivalent

	git checkout branch-one &&
	git worktree add worktree branch-two &&
	(
		git branch --show-current &&
		git -C worktree branch --show-current
	) >actual &&
	test_cmp expect actual

Note that the latter _could_ be written without subshell, i.e.

	git branch --show-current >actual &&
	git -C worktree branch --show-current >>actual &&

but I personally tend to prefer with a single redirection into
">actual", as that is easier to later add _more_ commands to
redirect into 'actual' to be inspected without having to worry about
details like repeating ">>actual" or only the first one must be
">actual" (iow, the preference comes mostly from maintainability
concerns).

Thanks.

> +
>  test_expect_success 'git branch shows detached HEAD properly' '
>  	cat >expect <<EOF &&
>  * (HEAD detached at $(git rev-parse --short HEAD^0))
Daniels Umanovskis Oct. 11, 2018, 11:31 p.m. UTC | #2
On 10/12/18 1:15 AM, Junio C Hamano wrote:
> It is a bit curious why you remove the branch but not the tag after
> this test.  [..]
> 
> So two equally valid choices are to remove "branch -d" and then
> either:
> 
>  (1) leave both branch and tag after this test in the test
>      repository
> 
>  (2) use test_when_finished [..]

Thanks for this explanation! You're right, I removed the branch because
it otherwise breaks subsequent tests, while the tag doesn't matter. I'll
go take a look at how test_when_finished can be used.

> Please do *not* cd around without being in a subshell.  

Understood, thanks for explaining this as well.

Patch
diff mbox series

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index bf5316ffa..0babb9b1b 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -9,7 +9,7 @@  SYNOPSIS
 --------
 [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 @@  This option is only applicable in non-verbose mode.
 	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::
diff --git a/builtin/branch.c b/builtin/branch.c
index c396c4153..46f91dc06 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -443,6 +443,21 @@  static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
 	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 @@  static int edit_branch_description(const char *branch_name)
 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;
@@ -620,6 +636,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, "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")),
@@ -662,14 +679,15 @@  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 && !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);
 
@@ -697,6 +715,9 @@  int cmd_branch(int argc, const char **argv, const char *prefix)
 		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)
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index ee6787614..8d2020aea 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -100,6 +100,47 @@  test_expect_success 'git branch -v pattern does not show branch summaries' '
 	test_must_fail git branch -v branch*
 '
 
+test_expect_success 'git branch `--show-current` shows current branch' '
+	cat >expect <<-\EOF &&
+	branch-two
+	EOF
+	git checkout branch-two &&
+	git branch --show-current >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
+	git checkout HEAD^0 &&
+	git branch --show-current >actual &&
+	test_must_be_empty actual
+'
+
+test_expect_success 'git branch `--show-current` works properly when tag exists' '
+	cat >expect <<-\EOF &&
+	branch-and-tag-name
+	EOF
+	git checkout -b branch-and-tag-name &&
+	git tag branch-and-tag-name &&
+	git branch --show-current >actual &&
+	git checkout branch-one &&
+	git branch -d branch-and-tag-name &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git branch `--show-current` works properly with worktrees' '
+	cat >expect <<-\EOF &&
+	branch-one
+	branch-two
+	EOF
+	git checkout branch-one &&
+	git branch --show-current >actual &&
+	git worktree add worktree branch-two &&
+	cd worktree &&
+	git branch --show-current >>../actual &&
+	cd .. &&
+	test_cmp expect actual
+'
+
 test_expect_success 'git branch shows detached HEAD properly' '
 	cat >expect <<EOF &&
 * (HEAD detached at $(git rev-parse --short HEAD^0))