status: support --no-ahead-behind in long format

Teach long (normal) status format to respect the --no-ahead-behind parameter and skip the possibly expensive ahead/behind computation between the branch and the upstream. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Jan 9, 2018 at 18:50 UTC f39a757dd93488103dde76e992a75edf2d772b62
5 files changed +46 -8
builtin/checkout.c
+1 -1
@@ -610,7 +610,7 @@ static void report_tracking(struct branch_info *new)
610 struct strbuf sb = STRBUF_INIT;
611 struct branch *branch = branch_get(new->name);
612
613 - if (!format_tracking_info(branch, &sb))
613 + if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL))
614 return;
615 fputs(sb.buf, stdout);
616 strbuf_release(&sb);
remote.c
+13 -5
@@ -2096,15 +2096,16 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
2096 /*
2097 * Return true when there is anything to report, otherwise false.
2098 */
2099 -int format_tracking_info(struct branch *branch, struct strbuf *sb)
2099 +int format_tracking_info(struct branch *branch, struct strbuf *sb,
2100 + enum ahead_behind_flags abf)
2101 {
2101 - int ours, theirs;
2102 + int ours, theirs, sti;
2103 const char *full_base;
2104 char *base;
2105 int upstream_is_gone = 0;
2106
2106 - if (stat_tracking_info(branch, &ours, &theirs, &full_base,
2107 - AHEAD_BEHIND_FULL) < 0) {
2107 + sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
2108 + if (sti < 0) {
2109 if (!full_base)
2110 return 0;
2111 upstream_is_gone = 1;
@@ -2118,10 +2119,17 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
2119 if (advice_status_hints)
2120 strbuf_addstr(sb,
2121 _(" (use \"git branch --unset-upstream\" to fixup)\n"));
2121 - } else if (!ours && !theirs) {
2122 + } else if (!sti) {
2123 strbuf_addf(sb,
2124 _("Your branch is up to date with '%s'.\n"),
2125 base);
2126 + } else if (abf == AHEAD_BEHIND_QUICK) {
2127 + strbuf_addf(sb,
2128 + _("Your branch and '%s' refer to different commits.\n"),
2129 + base);
2130 + if (advice_status_hints)
2131 + strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
2132 + "git status --ahead-behind");
2133 } else if (!theirs) {
2134 strbuf_addf(sb,
2135 Q_("Your branch is ahead of '%s' by %d commit.\n",
remote.h
+2 -1
@@ -267,7 +267,8 @@ enum ahead_behind_flags {
267 /* Reporting of tracking info */
268 int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
269 const char **upstream_name, enum ahead_behind_flags abf);
270 -int format_tracking_info(struct branch *branch, struct strbuf *sb);
270 +int format_tracking_info(struct branch *branch, struct strbuf *sb,
271 + enum ahead_behind_flags abf);
272
273 struct ref *get_local_heads(void);
274 /*
t/t6040-tracking-info.sh
+29
@@ -159,6 +159,35 @@ test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' '
159 test_i18ncmp expect actual
160 '
161
162 +cat >expect <<\EOF
163 +On branch b1
164 +Your branch and 'origin/master' have diverged,
165 +and have 1 and 1 different commits each, respectively.
166 +EOF
167 +
168 +test_expect_success 'status --long --branch' '
169 + (
170 + cd test &&
171 + git checkout b1 >/dev/null &&
172 + git status --long -b | head -3
173 + ) >actual &&
174 + test_i18ncmp expect actual
175 +'
176 +
177 +cat >expect <<\EOF
178 +On branch b1
179 +Your branch and 'origin/master' refer to different commits.
180 +EOF
181 +
182 +test_expect_success 'status --long --branch --no-ahead-behind' '
183 + (
184 + cd test &&
185 + git checkout b1 >/dev/null &&
186 + git status --long -b --no-ahead-behind | head -2
187 + ) >actual &&
188 + test_i18ncmp expect actual
189 +'
190 +
191 cat >expect <<\EOF
192 ## b5...brokenbase [gone]
193 EOF
wt-status.c
+1 -1
@@ -1011,7 +1011,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
1011 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1012 return;
1013 branch = branch_get(branch_name);
1014 - if (!format_tracking_info(branch, &sb))
1014 + if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1015 return;
1016
1017 i = 0;