status: add optional stash count information
authorLiam Beguin <liambeguin@gmail.com>
Sat, 17 Jun 2017 22:30:51 +0000 (18:30 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 19 Jun 2017 05:17:47 +0000 (22:17 -0700)
Introduce '--show-stash' and its configuration option 'status.showStash'
to allow git-status to show information about currently stashed entries.

Signed-off-by: Liam Beguin <liambeguin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config.txt
Documentation/git-status.txt
builtin/commit.c
t/t7508-status.sh
wt-status.c
wt-status.h
index 23b807065d92585c5b0118e9f3e3fc283ee18d24..e83b0f6415743d840d4efbe635bbe3f8ee18ada5 100644 (file)
@@ -2992,6 +2992,11 @@ status.displayCommentPrefix::
        behavior of linkgit:git-status[1] in Git 1.8.4 and previous.
        Defaults to false.
 
+status.showStash::
+       If set to true, linkgit:git-status[1] will display the number of
+       entries currently stashed away.
+       Defaults to false.
+
 status.showUntrackedFiles::
        By default, linkgit:git-status[1] and linkgit:git-commit[1] show
        files which are not currently tracked by Git. Directories which
index d70abc6afe3aa40aaaab2e055ee75d8ee22cdb69..d47f198f15cd4c767b03c14580b816bf750e9ee1 100644 (file)
@@ -32,6 +32,9 @@ OPTIONS
 --branch::
        Show the branch and tracking info even in short-format.
 
+--show-stash::
+       Show the number of entries currently stashed away.
+
 --porcelain[=<version>]::
        Give the output in an easy-to-parse format for scripts.
        This is similar to the short output, but will remain stable
index ef52457effc1f519a53c105efcb24d5e19048f6c..c089fb87e363cdb4ae63a65aebdf71207f804e1b 100644 (file)
@@ -1295,6 +1295,10 @@ static int git_status_config(const char *k, const char *v, void *cb)
                status_deferred_config.show_branch = git_config_bool(k, v);
                return 0;
        }
+       if (!strcmp(k, "status.showstash")) {
+               s->show_stash = git_config_bool(k, v);
+               return 0;
+       }
        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
                s->use_color = git_config_colorbool(k, v);
                return 0;
@@ -1343,6 +1347,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
                            N_("show status concisely"), STATUS_FORMAT_SHORT),
                OPT_BOOL('b', "branch", &s.show_branch,
                         N_("show branch information")),
+               OPT_BOOL(0, "show-stash", &s.show_stash,
+                        N_("show stash information")),
                { OPTION_CALLBACK, 0, "porcelain", &status_format,
                  N_("version"), N_("machine-readable output"),
                  PARSE_OPT_OPTARG, opt_parse_porcelain },
index 79427840a4faed765c06c9c9dcf35e4ec3c43c43..7121a550c7ce789d5bcd2bb9a5d8c595e7baaa6a 100755 (executable)
@@ -1608,4 +1608,36 @@ test_expect_success 'git commit -m will commit a staged but ignored submodule' '
        git config -f .gitmodules  --remove-section submodule.subname
 '
 
+test_expect_success 'show stash info with "--show-stash"' '
+       git reset --hard &&
+       git stash clear &&
+       echo 1 >file &&
+       git add file &&
+       git stash &&
+       git status >expected_default &&
+       git status --show-stash >expected_with_stash &&
+       test_i18ngrep "^Your stash currently has 1 entry$" expected_with_stash
+'
+
+test_expect_success 'no stash info with "--show-stash --no-show-stash"' '
+       git status --show-stash --no-show-stash >expected_without_stash &&
+       test_cmp expected_default expected_without_stash
+'
+
+test_expect_success '"status.showStash=false" weaker than "--show-stash"' '
+       git -c status.showStash=false status --show-stash >actual &&
+       test_cmp expected_with_stash actual
+'
+
+test_expect_success '"status.showStash=true" weaker than "--no-show-stash"' '
+       git -c status.showStash=true status --no-show-stash >actual &&
+       test_cmp expected_without_stash actual
+'
+
+test_expect_success 'no additionnal info if no stash entries' '
+       git stash clear &&
+       git -c status.showStash=true status >actual &&
+       test_cmp expected_without_stash actual
+'
+
 test_done
index bf651f16fae83ffe30676790685799e8f2280082..7992a73902ae5c3fa70d8937faec4ce1e0a4f78b 100644 (file)
@@ -137,6 +137,7 @@ void wt_status_prepare(struct wt_status *s)
        s->untracked.strdup_strings = 1;
        s->ignored.strdup_strings = 1;
        s->show_branch = -1;  /* unspecified */
+       s->show_stash = 0;
        s->display_comment_prefix = 0;
 }
 
@@ -801,6 +802,27 @@ static void wt_longstatus_print_changed(struct wt_status *s)
        wt_longstatus_print_trailer(s);
 }
 
+static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
+                           const char *email, timestamp_t timestamp, int tz,
+                           const char *message, void *cb_data)
+{
+       int *c = cb_data;
+       (*c)++;
+       return 0;
+}
+
+static void wt_longstatus_print_stash_summary(struct wt_status *s)
+{
+       int stash_count = 0;
+
+       for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
+       if (stash_count > 0)
+               status_printf_ln(s, GIT_COLOR_NORMAL,
+                                Q_("Your stash currently has %d entry",
+                                   "Your stash currently has %d entries", stash_count),
+                                stash_count);
+}
+
 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
 {
        struct child_process sm_summary = CHILD_PROCESS_INIT;
@@ -1642,6 +1664,8 @@ static void wt_longstatus_print(struct wt_status *s)
                } else
                        printf(_("nothing to commit, working tree clean\n"));
        }
+       if(s->show_stash)
+               wt_longstatus_print_stash_summary(s);
 }
 
 static void wt_shortstatus_unmerged(struct string_list_item *it,
index 8a3864783b039d92454cfda1c2ef2457e737cddd..d8ae2e590dafb87b92f5e3a4b183f0ccdeb4c359 100644 (file)
@@ -77,6 +77,7 @@ struct wt_status {
        unsigned colopts;
        int null_termination;
        int show_branch;
+       int show_stash;
        int hints;
 
        enum wt_status_format status_format;