git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/4] branch: add '--show-description' option
@ 2018-01-01 22:54 SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 1/4] branch: '--edit-description' is incompatible with other options SZEDER Gábor
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-01 22:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

'git branch' has an option to edit a branch's description, but lacks the
option to show that description.  The last patch adds the option to do
just that.  The first three patches are while-at-it minor cleanups and
fixes in the area.


SZEDER Gábor (4):
  branch: '--edit-description' is incompatible with other options
  t3200: fix a misindented line
  t3200: restore branch after "unborn --edit-description" test
  branch: add '--show-description' option

 Documentation/git-branch.txt           |  6 +++++-
 builtin/branch.c                       | 39 +++++++++++++++++++++++++++++++---
 contrib/completion/git-completion.bash |  4 ++--
 t/t3200-branch.sh                      | 26 ++++++++++++++++++++++-
 4 files changed, 68 insertions(+), 7 deletions(-)

-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/4] branch: '--edit-description' is incompatible with other options
  2018-01-01 22:54 [PATCH 0/4] branch: add '--show-description' option SZEDER Gábor
@ 2018-01-01 22:54 ` SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 2/4] t3200: fix a misindented line SZEDER Gábor
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-01 22:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

'--edit-description' is incompatible with 'git branch's other options,
but the check for conflicting options doesn't look for this option.
As a result 'git branch' doesn't error out showing usage when
'--edit-description' is used in combination with other options, but
e.g. 'git branch --list --edit-description' lists all branches and
doesn't edit any branch descriptions, while 'git branch
--edit-description --list' edits the current branch's description but
doesn't list the branches.

Look for '--edit-description', too, when looking for conflicting
options.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 builtin/branch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 8dcc2ed05..32531aa44 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -662,7 +662,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		list = 1;
 
 	if (!!delete + !!rename + !!copy + !!new_upstream +
-	    list + unset_upstream > 1)
+	    list + unset_upstream + edit_description > 1)
 		usage_with_options(builtin_branch_usage, options);
 
 	if (filter.abbrev == -1)
-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/4] t3200: fix a misindented line
  2018-01-01 22:54 [PATCH 0/4] branch: add '--show-description' option SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 1/4] branch: '--edit-description' is incompatible with other options SZEDER Gábor
@ 2018-01-01 22:54 ` SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 3/4] t3200: restore branch after "unborn --edit-description" test SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 4/4] branch: add '--show-description' option SZEDER Gábor
  3 siblings, 0 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-01 22:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 t/t3200-branch.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 503a88d02..e9f55ac04 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1215,7 +1215,7 @@ test_expect_success 'use --edit-description' '
 		echo "New contents" >"$1"
 	EOF
 	EDITOR=./editor git branch --edit-description &&
-		write_script editor <<-\EOF &&
+	write_script editor <<-\EOF &&
 		git stripspace -s <"$1" >"EDITOR_OUTPUT"
 	EOF
 	EDITOR=./editor git branch --edit-description &&
-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/4] t3200: restore branch after "unborn --edit-description" test
  2018-01-01 22:54 [PATCH 0/4] branch: add '--show-description' option SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 1/4] branch: '--edit-description' is incompatible with other options SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 2/4] t3200: fix a misindented line SZEDER Gábor
@ 2018-01-01 22:54 ` SZEDER Gábor
  2018-01-01 22:54 ` [PATCH 4/4] branch: add '--show-description' option SZEDER Gábor
  3 siblings, 0 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-01 22:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

The test 'refuse --edit-description on unborn branch for now' switches
to an orphan branch and leaves the test repository in that state.
This might badly influence the subsequent test expecting 'git branch
--merged' to error out, by hiding a potential regression with an error
triggered by the unborn branch.  Furthermore, a new test that will be
added later in this series failed first because of this.

Use test_when_finished to switch back to the master branch at the end
of the test in question.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 t/t3200-branch.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index e9f55ac04..427ad490d 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1234,6 +1234,7 @@ test_expect_success 'refuse --edit-description on unborn branch for now' '
 	write_script editor <<-\EOF &&
 		echo "New contents" >"$1"
 	EOF
+	test_when_finished git checkout master &&
 	git checkout --orphan unborn &&
 	test_must_fail env EDITOR=./editor git branch --edit-description
 '
-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 4/4] branch: add '--show-description' option
  2018-01-01 22:54 [PATCH 0/4] branch: add '--show-description' option SZEDER Gábor
                   ` (2 preceding siblings ...)
  2018-01-01 22:54 ` [PATCH 3/4] t3200: restore branch after "unborn --edit-description" test SZEDER Gábor
@ 2018-01-01 22:54 ` SZEDER Gábor
  2018-01-02  5:17   ` Eric Sunshine
  2018-01-02  9:32   ` Johannes Sixt
  3 siblings, 2 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-01 22:54 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, SZEDER Gábor

'git branch' has an option to edit a branch's description, but lacks
the option to show that description.

Therefore, add a new '--show-description' option to do just that, as a
more user-friendly alternative to 'git config --get
branch.$branchname.description':

  - it's shorter to type (both in the number of characters and the
    number of TABs if using completion),
  - works on the current branch without explicitly naming it,
  - hides the implementation detail that branch descriptions are
    stored in the config file, and
  - errors out with a proper error message when the given branch
    doesn't exist (but exits quietly with an error code when the
    branch does exit but has no description, just like the 'git config'
    query does).

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 Documentation/git-branch.txt           |  6 +++++-
 builtin/branch.c                       | 39 +++++++++++++++++++++++++++++++---
 contrib/completion/git-completion.bash |  4 ++--
 t/t3200-branch.sh                      | 23 ++++++++++++++++++++
 4 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index b3084c99c..e05c9e193 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -20,7 +20,7 @@ SYNOPSIS
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
 'git branch' (-c | -C) [<oldbranch>] <newbranch>
 'git branch' (-d | -D) [-r] <branchname>...
-'git branch' --edit-description [<branchname>]
+'git branch' (--edit-description | --show-description) [<branchname>]
 
 DESCRIPTION
 -----------
@@ -226,6 +226,10 @@ start-point is either a local or remote-tracking branch.
 	`request-pull`, and `merge` (if enabled)). Multi-line explanations
 	may be used.
 
+--show-description::
+	Show the description of the branch previously set using
+	`--edit-description`.
+
 --contains [<commit>]::
 	Only list branches which contain the specified commit (HEAD
 	if not specified). Implies `--list`.
diff --git a/builtin/branch.c b/builtin/branch.c
index 32531aa44..f2f6614e2 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -573,7 +573,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 reflog = 0, edit_description = 0;
+	int reflog = 0, edit_description = 0, show_description = 0;
 	int quiet = 0, unset_upstream = 0;
 	const char *new_upstream = NULL;
 	enum branch_track track;
@@ -615,6 +615,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
 		OPT_BOOL(0, "edit-description", &edit_description,
 			 N_("edit the description for the branch")),
+		OPT_BOOL(0, "show-description", &show_description,
+			 N_("show the description for the branch")),
 		OPT__FORCE(&force, N_("force creation, move/rename, deletion")),
 		OPT_MERGED(&filter, N_("print only branches that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -654,7 +656,9 @@ 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 && !show_description &&
+	    !new_upstream && !unset_upstream && argc == 0)
 		list = 1;
 
 	if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
@@ -662,7 +666,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		list = 1;
 
 	if (!!delete + !!rename + !!copy + !!new_upstream +
-	    list + unset_upstream + edit_description > 1)
+	    list + unset_upstream + edit_description + show_description > 1)
 		usage_with_options(builtin_branch_usage, options);
 
 	if (filter.abbrev == -1)
@@ -737,6 +741,35 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 		if (edit_branch_description(branch_name))
 			return 1;
+	} else if (show_description) {
+		const char *branch_name;
+		struct strbuf buf = STRBUF_INIT;
+		char *description = NULL;
+
+		if (!argc) {
+			if (filter.detached)
+				die(_("cannot show description on detached HEAD"));
+			branch_name = head;
+		} else if (argc == 1)
+			branch_name = argv[0];
+		else
+			die(_("cannot show description of more than one branch"));
+
+		strbuf_addf(&buf, "refs/heads/%s", branch_name);
+		if (!ref_exists(buf.buf)) {
+			strbuf_release(&buf);
+			return error(_("no branch named '%s'"), branch_name);
+		}
+		strbuf_reset(&buf);
+
+		strbuf_addf(&buf, "branch.%s.description", branch_name);
+		if (git_config_get_string(buf.buf, &description)) {
+			strbuf_release(&buf);
+			return 1;
+		}
+		printf("%s", description);
+		strbuf_release(&buf);
+		free(description);
 	} else if (copy) {
 		if (!argc)
 			die(_("branch name required"));
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3683c772c..0fcc13ea0 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1203,8 +1203,8 @@ _git_branch ()
 		__gitcomp "
 			--color --no-color --verbose --abbrev= --no-abbrev
 			--track --no-track --contains --no-contains --merged --no-merged
-			--set-upstream-to= --edit-description --list
-			--unset-upstream --delete --move --copy --remotes
+			--set-upstream-to= --edit-description --show-description
+			--list --unset-upstream --delete --move --copy --remotes
 			--column --no-column --sort= --points-at
 			"
 		;;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 427ad490d..53adccc5e 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1239,6 +1239,29 @@ test_expect_success 'refuse --edit-description on unborn branch for now' '
 	test_must_fail env EDITOR=./editor git branch --edit-description
 '
 
+test_expect_success '--show-description' '
+	echo "New contents" >expect &&
+	git branch --show-description >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--show-description on detached HEAD' '
+	git checkout --detach &&
+	test_when_finished git checkout master &&
+	test_must_fail git branch --show-description
+'
+
+test_expect_success '--show-description with no description errors quietly' '
+	git config --unset branch.master.description &&
+	test_must_fail git branch --show-description >actual 2>actual.err &&
+	test_must_be_empty actual &&
+	test_must_be_empty actual.err
+'
+
+test_expect_success '--show-description on non-existing branch' '
+	test_must_fail git branch --show-description no-such-branch
+'
+
 test_expect_success '--merged catches invalid object names' '
 	test_must_fail git branch --merged 0000000000000000000000000000000000000000
 '
-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/4] branch: add '--show-description' option
  2018-01-01 22:54 ` [PATCH 4/4] branch: add '--show-description' option SZEDER Gábor
@ 2018-01-02  5:17   ` Eric Sunshine
  2018-01-02 10:39     ` SZEDER Gábor
  2018-01-02  9:32   ` Johannes Sixt
  1 sibling, 1 reply; 10+ messages in thread
From: Eric Sunshine @ 2018-01-02  5:17 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Junio C Hamano, Git List

On Mon, Jan 1, 2018 at 5:54 PM, SZEDER Gábor <szeder.dev@gmail.com> wrote:
> 'git branch' has an option to edit a branch's description, but lacks
> the option to show that description.
>
> Therefore, add a new '--show-description' option to do just that, as a
> more user-friendly alternative to 'git config --get
> branch.$branchname.description':
>   [...]
>   - errors out with a proper error message when the given branch
>     doesn't exist (but exits quietly with an error code when the
>     branch does exit but has no description, just like the 'git config'

s/exit/exist/

>     query does).
>
> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
> ---
> diff --git a/builtin/branch.c b/builtin/branch.c
> @@ -737,6 +741,35 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
> +       } else if (show_description) {
> +               [...]
> +               if (!argc) {
> +                       if (filter.detached)
> +                               die(_("cannot show description on detached HEAD"));
> +                       branch_name = head;
> +               } else if (argc == 1)
> +                       branch_name = argv[0];
> +               else
> +                       die(_("cannot show description of more than one branch"));

Aside from paralleling the single branch accepted by
--edit-description, why this limitation? (Just curious; I don't feel
strongly one way or the other.)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/4] branch: add '--show-description' option
  2018-01-01 22:54 ` [PATCH 4/4] branch: add '--show-description' option SZEDER Gábor
  2018-01-02  5:17   ` Eric Sunshine
@ 2018-01-02  9:32   ` Johannes Sixt
  2018-01-02 10:41     ` SZEDER Gábor
  1 sibling, 1 reply; 10+ messages in thread
From: Johannes Sixt @ 2018-01-02  9:32 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Junio C Hamano, git

Am 01.01.2018 um 23:54 schrieb SZEDER Gábor:
> 'git branch' has an option to edit a branch's description, but lacks
> the option to show that description.
> 
> Therefore, add a new '--show-description' option to do just that, as a
> more user-friendly alternative to 'git config --get
> branch.$branchname.description':
> 
>    - it's shorter to type (both in the number of characters and the
>      number of TABs if using completion),
>    - works on the current branch without explicitly naming it,
>    - hides the implementation detail that branch descriptions are
>      stored in the config file, and
>    - errors out with a proper error message when the given branch
>      doesn't exist (but exits quietly with an error code when the
>      branch does exit but has no description, just like the 'git config'
>      query does).

> +test_expect_success '--show-description with no description errors quietly' '
> +	git config --unset branch.master.description &&
> +	test_must_fail git branch --show-description >actual 2>actual.err &&
> +	test_must_be_empty actual &&
> +	test_must_be_empty actual.err
> +'

Checking the exact contents of stderr typically fails when tests are run 
under -x. Perhaps

	test_i18ngrep ! "fatal: " actual.err &&"
	test_i18ngrep ! "error: " actual.err &&
	test_i18ngrep ! "warning: " actual.err

Which makes me wonder: Why would --show-description have to error out 
silently? This is not 'git config' after all.

-- Hannes

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/4] branch: add '--show-description' option
  2018-01-02  5:17   ` Eric Sunshine
@ 2018-01-02 10:39     ` SZEDER Gábor
  0 siblings, 0 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-02 10:39 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Junio C Hamano, Git List

On Tue, Jan 2, 2018 at 6:17 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Mon, Jan 1, 2018 at 5:54 PM, SZEDER Gábor <szeder.dev@gmail.com> wrote:

> s/exit/exist/

Thanks.

>>     query does).
>>
>> Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
>> ---
>> diff --git a/builtin/branch.c b/builtin/branch.c
>> @@ -737,6 +741,35 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
>> +       } else if (show_description) {
>> +               [...]
>> +               if (!argc) {
>> +                       if (filter.detached)
>> +                               die(_("cannot show description on detached HEAD"));
>> +                       branch_name = head;
>> +               } else if (argc == 1)
>> +                       branch_name = argv[0];
>> +               else
>> +                       die(_("cannot show description of more than one branch"));
>
> Aside from paralleling the single branch accepted by
> --edit-description, why this limitation? (Just curious; I don't feel
> strongly one way or the other.)

It's not just '--edit-description', most other options won't accept
multiple branches either.  As far as I can tell only deleting branches
can deal with multiple branches in one go.

Furthermore, branch descriptions are likely more lines long, so we
can't just dump them one after the other, but we we would have to
separate the descriptions of different branches in the output.
Considering that 'git branch' mostly works only with a single branch at
a time, I didn't feel the need to do so.

Gábor

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/4] branch: add '--show-description' option
  2018-01-02  9:32   ` Johannes Sixt
@ 2018-01-02 10:41     ` SZEDER Gábor
  2018-01-02 19:25       ` SZEDER Gábor
  0 siblings, 1 reply; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-02 10:41 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Junio C Hamano, Git mailing list

On Tue, Jan 2, 2018 at 10:32 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 01.01.2018 um 23:54 schrieb SZEDER Gábor:

>>    - errors out with a proper error message when the given branch
>>      doesn't exist (but exits quietly with an error code when the
>>      branch does exit but has no description, just like the 'git config'
>>      query does).
>
>
>> +test_expect_success '--show-description with no description errors
>> quietly' '
>> +       git config --unset branch.master.description &&
>> +       test_must_fail git branch --show-description >actual 2>actual.err
>> &&
>> +       test_must_be_empty actual &&
>> +       test_must_be_empty actual.err
>> +'
>
>
> Checking the exact contents of stderr typically fails when tests are run
> under -x. Perhaps
>
>         test_i18ngrep ! "fatal: " actual.err &&"
>         test_i18ngrep ! "error: " actual.err &&
>         test_i18ngrep ! "warning: " actual.err
>
> Which makes me wonder: Why would --show-description have to error out
> silently? This is not 'git config' after all.

I figured it would be beneficial if it were a drop-in replacement for
the original 'git config' query.

I don't have a strong opinion about this, and certainly wouldn't mind
adding an error message instead.

Gábor

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 4/4] branch: add '--show-description' option
  2018-01-02 10:41     ` SZEDER Gábor
@ 2018-01-02 19:25       ` SZEDER Gábor
  0 siblings, 0 replies; 10+ messages in thread
From: SZEDER Gábor @ 2018-01-02 19:25 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: SZEDER Gábor, Junio C Hamano, Git mailing list


On Tue, Jan 2, 2018 at 10:32 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> > Which makes me wonder: Why would --show-description have to error out
> > silently? This is not 'git config' after all.

> I don't have a strong opinion about this, and certainly wouldn't mind
> adding an error message instead.

And it would look like this:

  -- >8 --

Subject: [PATCH 4/4] branch: add '--show-description' option

'git branch' has an option to edit a branch's description, but lacks
the option to show that description.

Therefore, add a new '--show-description' option to do just that, as a
more user-friendly alternative to 'git config --get
branch.$branchname.description':

  - it's shorter to type (both in the number of characters and the
    number of TABs if using completion),
  - works on the current branch without explicitly naming it,
  - hides the implementation detail that branch descriptions are
    stored in the config file, and
  - errors out with a proper error message when the given branch
    doesn't exist or has no description.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 Documentation/git-branch.txt           |  6 +++++-
 builtin/branch.c                       | 39 +++++++++++++++++++++++++++++++---
 contrib/completion/git-completion.bash |  4 ++--
 t/t3200-branch.sh                      | 21 ++++++++++++++++++
 4 files changed, 64 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index b3084c99c..e05c9e193 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -20,7 +20,7 @@ SYNOPSIS
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
 'git branch' (-c | -C) [<oldbranch>] <newbranch>
 'git branch' (-d | -D) [-r] <branchname>...
-'git branch' --edit-description [<branchname>]
+'git branch' (--edit-description | --show-description) [<branchname>]
 
 DESCRIPTION
 -----------
@@ -226,6 +226,10 @@ start-point is either a local or remote-tracking branch.
 	`request-pull`, and `merge` (if enabled)). Multi-line explanations
 	may be used.
 
+--show-description::
+	Show the description of the branch previously set using
+	`--edit-description`.
+
 --contains [<commit>]::
 	Only list branches which contain the specified commit (HEAD
 	if not specified). Implies `--list`.
diff --git a/builtin/branch.c b/builtin/branch.c
index 32531aa44..748a1a575 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -573,7 +573,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 reflog = 0, edit_description = 0;
+	int reflog = 0, edit_description = 0, show_description = 0;
 	int quiet = 0, unset_upstream = 0;
 	const char *new_upstream = NULL;
 	enum branch_track track;
@@ -615,6 +615,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
 		OPT_BOOL(0, "edit-description", &edit_description,
 			 N_("edit the description for the branch")),
+		OPT_BOOL(0, "show-description", &show_description,
+			 N_("show the description for the branch")),
 		OPT__FORCE(&force, N_("force creation, move/rename, deletion")),
 		OPT_MERGED(&filter, N_("print only branches that are merged")),
 		OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
@@ -654,7 +656,9 @@ 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 && !show_description &&
+	    !new_upstream && !unset_upstream && argc == 0)
 		list = 1;
 
 	if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
@@ -662,7 +666,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		list = 1;
 
 	if (!!delete + !!rename + !!copy + !!new_upstream +
-	    list + unset_upstream + edit_description > 1)
+	    list + unset_upstream + edit_description + show_description > 1)
 		usage_with_options(builtin_branch_usage, options);
 
 	if (filter.abbrev == -1)
@@ -737,6 +741,35 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 
 		if (edit_branch_description(branch_name))
 			return 1;
+	} else if (show_description) {
+		const char *branch_name;
+		struct strbuf buf = STRBUF_INIT;
+		char *description = NULL;
+
+		if (!argc) {
+			if (filter.detached)
+				die(_("cannot show description on detached HEAD"));
+			branch_name = head;
+		} else if (argc == 1)
+			branch_name = argv[0];
+		else
+			die(_("cannot show description of more than one branch"));
+
+		strbuf_addf(&buf, "refs/heads/%s", branch_name);
+		if (!ref_exists(buf.buf)) {
+			strbuf_release(&buf);
+			return error(_("no branch named '%s'"), branch_name);
+		}
+		strbuf_reset(&buf);
+
+		strbuf_addf(&buf, "branch.%s.description", branch_name);
+		if (git_config_get_string(buf.buf, &description)) {
+			strbuf_release(&buf);
+			return error(_("no description for branch '%s'"), branch_name);
+		}
+		printf("%s", description);
+		strbuf_release(&buf);
+		free(description);
 	} else if (copy) {
 		if (!argc)
 			die(_("branch name required"));
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3683c772c..0fcc13ea0 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1203,8 +1203,8 @@ _git_branch ()
 		__gitcomp "
 			--color --no-color --verbose --abbrev= --no-abbrev
 			--track --no-track --contains --no-contains --merged --no-merged
-			--set-upstream-to= --edit-description --list
-			--unset-upstream --delete --move --copy --remotes
+			--set-upstream-to= --edit-description --show-description
+			--list --unset-upstream --delete --move --copy --remotes
 			--column --no-column --sort= --points-at
 			"
 		;;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 427ad490d..54e8dcbe2 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1239,6 +1239,27 @@ test_expect_success 'refuse --edit-description on unborn branch for now' '
 	test_must_fail env EDITOR=./editor git branch --edit-description
 '
 
+test_expect_success '--show-description' '
+	echo "New contents" >expect &&
+	git branch --show-description >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '--show-description on detached HEAD' '
+	git checkout --detach &&
+	test_when_finished git checkout master &&
+	test_must_fail git branch --show-description
+'
+
+test_expect_success '--show-description with no description' '
+	git config --unset branch.master.description &&
+	test_must_fail git branch --show-description master
+'
+
+test_expect_success '--show-description on non-existing branch' '
+	test_must_fail git branch --show-description no-such-branch
+'
+
 test_expect_success '--merged catches invalid object names' '
 	test_must_fail git branch --merged 0000000000000000000000000000000000000000
 '
-- 
2.16.0.rc0.67.g3a46dbca7


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-01-02 19:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-01 22:54 [PATCH 0/4] branch: add '--show-description' option SZEDER Gábor
2018-01-01 22:54 ` [PATCH 1/4] branch: '--edit-description' is incompatible with other options SZEDER Gábor
2018-01-01 22:54 ` [PATCH 2/4] t3200: fix a misindented line SZEDER Gábor
2018-01-01 22:54 ` [PATCH 3/4] t3200: restore branch after "unborn --edit-description" test SZEDER Gábor
2018-01-01 22:54 ` [PATCH 4/4] branch: add '--show-description' option SZEDER Gábor
2018-01-02  5:17   ` Eric Sunshine
2018-01-02 10:39     ` SZEDER Gábor
2018-01-02  9:32   ` Johannes Sixt
2018-01-02 10:41     ` SZEDER Gábor
2018-01-02 19:25       ` SZEDER Gábor

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).