Skip to content

Commit 3418622

Browse files
committed
Merge branch 'jh/status-aheadbehind'
"git status" can be told a non-standard default value for the "--[no-]ahead-behind" option with a new configuration variable status.aheadBehind. * jh/status-aheadbehind: status: ignore status.aheadbehind in porcelain formats status: warn when a/b calculation takes too long status: add status.aheadbehind setting
2 parents 2fff509 + fb4db1a commit 3418622

File tree

8 files changed

+88
-1
lines changed

8 files changed

+88
-1
lines changed

Documentation/config/advice.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ advice.*::
3737
we can still suggest that the user push to either
3838
refs/heads/* or refs/tags/* based on the type of the
3939
source object.
40+
statusAheadBehind::
41+
Shown when linkgit:git-status[1] computes the ahead/behind
42+
counts for a local ref compared to its remote tracking ref,
43+
and that calculation takes longer than expected. Will not
44+
appear if `status.aheadBehind` is false or the option
45+
`--no-ahead-behind` is given.
4046
statusHints::
4147
Show directions on how to proceed from the current
4248
state in the output of linkgit:git-status[1], in

Documentation/config/status.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ status.branch::
1212
Set to true to enable --branch by default in linkgit:git-status[1].
1313
The option --no-branch takes precedence over this variable.
1414

15+
status.aheadBehind::
16+
Set to true to enable `--ahead-behind` and false to enable
17+
`--no-ahead-behind` by default in linkgit:git-status[1] for
18+
non-porcelain status formats. Defaults to true.
19+
1520
status.displayCommentPrefix::
1621
If set to true, linkgit:git-status[1] will insert a comment
1722
prefix before each output line (starting with

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int advice_push_needs_force = 1;
1212
int advice_push_unqualified_ref_name = 1;
1313
int advice_status_hints = 1;
1414
int advice_status_u_option = 1;
15+
int advice_status_ahead_behind_warning = 1;
1516
int advice_commit_before_merge = 1;
1617
int advice_reset_quiet_warning = 1;
1718
int advice_resolve_conflict = 1;
@@ -68,6 +69,7 @@ static struct {
6869
{ "pushUnqualifiedRefName", &advice_push_unqualified_ref_name },
6970
{ "statusHints", &advice_status_hints },
7071
{ "statusUoption", &advice_status_u_option },
72+
{ "statusAheadBehindWarning", &advice_status_ahead_behind_warning },
7173
{ "commitBeforeMerge", &advice_commit_before_merge },
7274
{ "resetQuiet", &advice_reset_quiet_warning },
7375
{ "resolveConflict", &advice_resolve_conflict },

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern int advice_push_needs_force;
1212
extern int advice_push_unqualified_ref_name;
1313
extern int advice_status_hints;
1414
extern int advice_status_u_option;
15+
extern int advice_status_ahead_behind_warning;
1516
extern int advice_commit_before_merge;
1617
extern int advice_reset_quiet_warning;
1718
extern int advice_resolve_conflict;

builtin/commit.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,11 @@ static const char *read_commit_message(const char *name)
10781078
static struct status_deferred_config {
10791079
enum wt_status_format status_format;
10801080
int show_branch;
1081+
enum ahead_behind_flags ahead_behind;
10811082
} status_deferred_config = {
10821083
STATUS_FORMAT_UNSPECIFIED,
1083-
-1 /* unspecified */
1084+
-1, /* unspecified */
1085+
AHEAD_BEHIND_UNSPECIFIED,
10841086
};
10851087

10861088
static void finalize_deferred_config(struct wt_status *s)
@@ -1107,6 +1109,17 @@ static void finalize_deferred_config(struct wt_status *s)
11071109
if (s->show_branch < 0)
11081110
s->show_branch = 0;
11091111

1112+
/*
1113+
* If the user did not give a "--[no]-ahead-behind" command
1114+
* line argument *AND* we will print in a human-readable format
1115+
* (short, long etc.) then we inherit from the status.aheadbehind
1116+
* config setting. In all other cases (and porcelain V[12] formats
1117+
* in particular), we inherit _FULL for backwards compatibility.
1118+
*/
1119+
if (use_deferred_config &&
1120+
s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1121+
s->ahead_behind_flags = status_deferred_config.ahead_behind;
1122+
11101123
if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
11111124
s->ahead_behind_flags = AHEAD_BEHIND_FULL;
11121125
}
@@ -1246,6 +1259,10 @@ static int git_status_config(const char *k, const char *v, void *cb)
12461259
status_deferred_config.show_branch = git_config_bool(k, v);
12471260
return 0;
12481261
}
1262+
if (!strcmp(k, "status.aheadbehind")) {
1263+
status_deferred_config.ahead_behind = git_config_bool(k, v);
1264+
return 0;
1265+
}
12491266
if (!strcmp(k, "status.showstash")) {
12501267
s->show_stash = git_config_bool(k, v);
12511268
return 0;

t/t6040-tracking-info.sh

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

162+
cat >expect <<\EOF
163+
## b1...origin/master [different]
164+
EOF
165+
166+
test_expect_success 'status.aheadbehind=false status -s -b (diverged from upstream)' '
167+
(
168+
cd test &&
169+
git checkout b1 >/dev/null &&
170+
git -c status.aheadbehind=false status -s -b | head -1
171+
) >actual &&
172+
test_i18ncmp expect actual
173+
'
174+
162175
cat >expect <<\EOF
163176
On branch b1
164177
Your branch and 'origin/master' have diverged,
@@ -174,6 +187,15 @@ test_expect_success 'status --long --branch' '
174187
test_i18ncmp expect actual
175188
'
176189

190+
test_expect_success 'status --long --branch' '
191+
(
192+
cd test &&
193+
git checkout b1 >/dev/null &&
194+
git -c status.aheadbehind=true status --long -b | head -3
195+
) >actual &&
196+
test_i18ncmp expect actual
197+
'
198+
177199
cat >expect <<\EOF
178200
On branch b1
179201
Your branch and 'origin/master' refer to different commits.
@@ -188,6 +210,15 @@ test_expect_success 'status --long --branch --no-ahead-behind' '
188210
test_i18ncmp expect actual
189211
'
190212

213+
test_expect_success 'status.aheadbehind=false status --long --branch' '
214+
(
215+
cd test &&
216+
git checkout b1 >/dev/null &&
217+
git -c status.aheadbehind=false status --long -b | head -2
218+
) >actual &&
219+
test_i18ncmp expect actual
220+
'
221+
191222
cat >expect <<\EOF
192223
## b5...brokenbase [gone]
193224
EOF

t/t7064-wtstatus-pv2.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ test_expect_success 'verify --[no-]ahead-behind with V2 format' '
445445
EOF
446446
447447
git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
448+
test_cmp expect actual &&
449+
450+
# Confirm that "status.aheadbehind" DOES NOT work on V2 format.
451+
git -c status.aheadbehind=false status --porcelain=v2 --branch --untracked-files=all >actual &&
452+
test_cmp expect actual &&
453+
454+
# Confirm that "status.aheadbehind" DOES NOT work on V2 format.
455+
git -c status.aheadbehind=true status --porcelain=v2 --branch --untracked-files=all >actual &&
448456
test_cmp expect actual
449457
)
450458
'

wt-status.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "lockfile.h"
2020
#include "sequencer.h"
2121

22+
#define AB_DELAY_WARNING_IN_MS (2 * 1000)
23+
2224
static const char cut_line[] =
2325
"------------------------ >8 ------------------------\n";
2426

@@ -1097,14 +1099,29 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
10971099
struct branch *branch;
10981100
char comment_line_string[3];
10991101
int i;
1102+
uint64_t t_begin = 0;
11001103

11011104
assert(s->branch && !s->is_initial);
11021105
if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
11031106
return;
11041107
branch = branch_get(branch_name);
1108+
1109+
t_begin = getnanotime();
1110+
11051111
if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
11061112
return;
11071113

1114+
if (advice_status_ahead_behind_warning &&
1115+
s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1116+
uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1117+
if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1118+
strbuf_addf(&sb, _("\n"
1119+
"It took %.2f seconds to compute the branch ahead/behind values.\n"
1120+
"You can use '--no-ahead-behind' to avoid this.\n"),
1121+
t_delta_in_ms / 1000.0);
1122+
}
1123+
}
1124+
11081125
i = 0;
11091126
if (s->display_comment_prefix) {
11101127
comment_line_string[i++] = comment_line_char;

0 commit comments

Comments
 (0)