Skip to content

Commit 348108d

Browse files
jeffhostetlerdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b474398 commit 348108d

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ static void report_tracking(struct branch_info *new)
605605
struct strbuf sb = STRBUF_INIT;
606606
struct branch *branch = branch_get(new->name);
607607

608-
if (!format_tracking_info(branch, &sb))
608+
if (!format_tracking_info(branch, &sb, AHEAD_BEHIND_FULL))
609609
return;
610610
fputs(sb.buf, stdout);
611611
strbuf_release(&sb);

remote.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,15 +2066,16 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
20662066
/*
20672067
* Return true when there is anything to report, otherwise false.
20682068
*/
2069-
int format_tracking_info(struct branch *branch, struct strbuf *sb)
2069+
int format_tracking_info(struct branch *branch, struct strbuf *sb,
2070+
enum ahead_behind_flags abf)
20702071
{
2071-
int ours, theirs;
2072+
int ours, theirs, sti;
20722073
const char *full_base;
20732074
char *base;
20742075
int upstream_is_gone = 0;
20752076

2076-
if (stat_tracking_info(branch, &ours, &theirs, &full_base,
2077-
AHEAD_BEHIND_FULL) < 0) {
2077+
sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
2078+
if (sti < 0) {
20782079
if (!full_base)
20792080
return 0;
20802081
upstream_is_gone = 1;
@@ -2088,10 +2089,17 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)
20882089
if (advice_status_hints)
20892090
strbuf_addstr(sb,
20902091
_(" (use \"git branch --unset-upstream\" to fixup)\n"));
2091-
} else if (!ours && !theirs) {
2092+
} else if (!sti) {
20922093
strbuf_addf(sb,
20932094
_("Your branch is up to date with '%s'.\n"),
20942095
base);
2096+
} else if (abf == AHEAD_BEHIND_QUICK) {
2097+
strbuf_addf(sb,
2098+
_("Your branch and '%s' refer to different commits.\n"),
2099+
base);
2100+
if (advice_status_hints)
2101+
strbuf_addf(sb, _(" (use \"%s\" for details)\n"),
2102+
"git status --ahead-behind");
20952103
} else if (!theirs) {
20962104
strbuf_addf(sb,
20972105
Q_("Your branch is ahead of '%s' by %d commit.\n",

remote.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ enum ahead_behind_flags {
265265
/* Reporting of tracking info */
266266
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
267267
const char **upstream_name, enum ahead_behind_flags abf);
268-
int format_tracking_info(struct branch *branch, struct strbuf *sb);
268+
int format_tracking_info(struct branch *branch, struct strbuf *sb,
269+
enum ahead_behind_flags abf);
269270

270271
struct ref *get_local_heads(void);
271272
/*

t/t6040-tracking-info.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' '
159159
test_i18ncmp expect actual
160160
'
161161

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+
162191
cat >expect <<\EOF
163192
## b5...brokenbase [gone]
164193
EOF

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
10061006
if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
10071007
return;
10081008
branch = branch_get(branch_name);
1009-
if (!format_tracking_info(branch, &sb))
1009+
if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
10101010
return;
10111011

10121012
i = 0;

0 commit comments

Comments
 (0)