diff mbox series

[2/2] status: print stash info with --porcelain=v2 --show-stash

Message ID 20211021222532.463375-3-oystwa@gmail.com (mailing list archive)
State New, archived
Headers show
Series Print stash info in the v2 porcelain format | expand

Commit Message

Øystein Walle Oct. 21, 2021, 10:25 p.m. UTC
The v2 porcelain format is very convenient for obtaining a lot of
information about the current state of the repo, but does not contain
any info about the stash. git status already accepts --show-stash but
it's silently ignored when --porcelain=v2 is given.

Let's add a simple line to print the number of stash entries but in a
format similar in style to the rest of the format.

Signed-off-by: Øystein Walle <oystwa@gmail.com>
---
 Documentation/git-status.txt |  8 ++++++++
 t/t7064-wtstatus-pv2.sh      | 15 +++++++++++++++
 wt-status.c                  | 15 +++++++++++++++
 3 files changed, 38 insertions(+)

Comments

Junio C Hamano Oct. 21, 2021, 11:47 p.m. UTC | #1
Øystein Walle <oystwa@gmail.com> writes:

> The v2 porcelain format is very convenient for obtaining a lot of
> information about the current state of the repo, but does not contain
> any info about the stash. git status already accepts --show-stash but
> it's silently ignored when --porcelain=v2 is given.
>
> Let's add a simple line to print the number of stash entries but in a
> format similar in style to the rest of the format.

Who is the primary target audience of this feature?  IDEs that read
from "git status --porcelain"?

Whoever it is, if they bothered enough to pass an extra option
(i.e. "--show-stash") to their "git status" invocation, I wonder if
they want to know at least what "git stash list" would give you at
the same time.  After all, the --porcelain output is all about
machine readability and reducing the number of forks would be what
those people would be interested in, wouldn't it?  I dunno.

Thanks.
Eric Sunshine Oct. 22, 2021, 12:05 a.m. UTC | #2
On Thu, Oct 21, 2021 at 6:25 PM Øystein Walle <oystwa@gmail.com> wrote:
> The v2 porcelain format is very convenient for obtaining a lot of
> information about the current state of the repo, but does not contain
> any info about the stash. git status already accepts --show-stash but
> it's silently ignored when --porcelain=v2 is given.
>
> Let's add a simple line to print the number of stash entries but in a
> format similar in style to the rest of the format.
>
> Signed-off-by: Øystein Walle <oystwa@gmail.com>
> ---
> diff --git a/t/t7064-wtstatus-pv2.sh b/t/t7064-wtstatus-pv2.sh
> @@ -113,6 +113,21 @@ test_expect_success 'after first commit, create unstaged changes' '
> +test_expect_success 'after first commit, stash existing changes' '
> +       cat >expect <<-EOF &&
> +       # branch.oid $H0
> +       # branch.head initial-branch
> +       # stash 2
> +       EOF
> +
> +       test_when_finished "git stash pop && git stash pop" &&

If it's indeed important to clean up the stashes when the test
finishes, then the test_when_finished() invocation should probably be
a bit more robust...

> +       git stash -- file_x &&
> +       git stash &&

... since, as it is now, if an error occurs between these two
git-stash invocations or before them, then there will only be zero or
one stashes, so the double stash-pop by test_when_finished() will
itself errout out. Better, perhaps to do this:

    test_when_finished "git stash pop && git stash pop || :" &&

> +       git status --porcelain=v2 --branch --show-stash --untracked-files=no >actual &&
> +       test_cmp expect actual
> +'
Junio C Hamano Oct. 22, 2021, 12:29 a.m. UTC | #3
Eric Sunshine <sunshine@sunshineco.com> writes:

>> +       test_when_finished "git stash pop && git stash pop" &&
>
> If it's indeed important to clean up the stashes when the test
> finishes, then the test_when_finished() invocation should probably be
> a bit more robust...
>
>> +       git stash -- file_x &&
>> +       git stash &&
>
> ... since, as it is now, if an error occurs between these two
> git-stash invocations or before them, then there will only be zero or
> one stashes, so the double stash-pop by test_when_finished() will
> itself errout out. Better, perhaps to do this:
>
>     test_when_finished "git stash pop && git stash pop || :" &&
>
>> +       git status --porcelain=v2 --branch --show-stash --untracked-files=no >actual &&
>> +       test_cmp expect actual
>> +'

There is no "I do not care what is in the stash right now, just
clear all"?
Eric Sunshine Oct. 22, 2021, 1:19 a.m. UTC | #4
On Thu, Oct 21, 2021 at 8:30 PM Junio C Hamano <gitster@pobox.com> wrote:
> Eric Sunshine <sunshine@sunshineco.com> writes:
> >> +       test_when_finished "git stash pop && git stash pop" &&
> >
> > If it's indeed important to clean up the stashes when the test
> > finishes, then the test_when_finished() invocation should probably be
> > a bit more robust...
> >
> >> +       git stash -- file_x &&
> >> +       git stash &&
> >
> > ... since, as it is now, if an error occurs between these two
> > git-stash invocations or before them, then there will only be zero or
> > one stashes, so the double stash-pop by test_when_finished() will
> > itself errout out. Better, perhaps to do this:
> >
> >     test_when_finished "git stash pop && git stash pop || :" &&
>
> There is no "I do not care what is in the stash right now, just
> clear all"?

Indeed, I wondered that too but was too lazy to look and I had
forgotten about `git stash clear`, which makes this much simpler:

    test_when_finished "git stash clear" &&
diff mbox series

Patch

diff --git a/Documentation/git-status.txt b/Documentation/git-status.txt
index 4a2c3e0408..54a4b29b47 100644
--- a/Documentation/git-status.txt
+++ b/Documentation/git-status.txt
@@ -314,6 +314,14 @@  Line                                     Notes
 ------------------------------------------------------------
 ....
 
+Stash Information
+^^^^^^^^^^^^^^^^^
+
+If `--show-stash` is given, one line is printed showing the number of stash
+entries if non-zero:
+
+    # stash <N>
+
 Changed Tracked Entries
 ^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/t/t7064-wtstatus-pv2.sh b/t/t7064-wtstatus-pv2.sh
index eeb0534163..47fc21d962 100755
--- a/t/t7064-wtstatus-pv2.sh
+++ b/t/t7064-wtstatus-pv2.sh
@@ -113,6 +113,21 @@  test_expect_success 'after first commit, create unstaged changes' '
 	test_cmp expect actual
 '
 
+test_expect_success 'after first commit, stash existing changes' '
+	cat >expect <<-EOF &&
+	# branch.oid $H0
+	# branch.head initial-branch
+	# stash 2
+	EOF
+
+	test_when_finished "git stash pop && git stash pop" &&
+
+	git stash -- file_x &&
+	git stash &&
+	git status --porcelain=v2 --branch --show-stash --untracked-files=no >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'after first commit but omit untracked files and branch' '
 	cat >expect <<-EOF &&
 	1 .M N... 100644 100644 100644 $OID_X $OID_X file_x
diff --git a/wt-status.c b/wt-status.c
index 97230477b2..88c5481e24 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -2182,6 +2182,18 @@  static void wt_porcelain_v2_print_tracking(struct wt_status *s)
 	}
 }
 
+/*
+ * Print the stash count in a porcelain-friendly format
+ */
+static void wt_porcelain_v2_print_stash(struct wt_status *s)
+{
+	int stash_count = count_stash_entries();
+	char eol = s->null_termination ? '\0' : '\n';
+
+	if (stash_count > 0)
+		fprintf(s->fp, "# stash %d%c", stash_count, eol);
+}
+
 /*
  * Convert various submodule status values into a
  * fixed-length string of characters in the buffer provided.
@@ -2443,6 +2455,9 @@  static void wt_porcelain_v2_print(struct wt_status *s)
 	if (s->show_branch)
 		wt_porcelain_v2_print_tracking(s);
 
+	if (s->show_stash)
+		wt_porcelain_v2_print_stash(s);
+
 	for (i = 0; i < s->change.nr; i++) {
 		it = &(s->change.items[i]);
 		d = it->util;