Skip to content

Commit c105654

Browse files
jeffhostetlerderrickstolee
authored andcommitted
status: warn when a/b calculation takes too long
The ahead/behind calculation in 'git status' can be slow in some cases. Users may not realize that there are ways to avoid this computation, especially if they are not using the information. Add a warning that appears if this calculation takes more than two seconds. The warning can be disabled through the new config setting advice.statusAheadBehind. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 2f0f2a2 commit c105654

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
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

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;

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

@@ -1085,14 +1087,29 @@ static void wt_longstatus_print_tracking(struct wt_status *s)
10851087
struct branch *branch;
10861088
char comment_line_string[3];
10871089
int i;
1090+
uint64_t t_begin = 0;
10881091

10891092
assert(s->branch && !s->is_initial);
10901093
if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
10911094
return;
10921095
branch = branch_get(branch_name);
1096+
1097+
t_begin = getnanotime();
1098+
10931099
if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
10941100
return;
10951101

1102+
if (advice_status_ahead_behind_warning &&
1103+
s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1104+
uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1105+
if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1106+
strbuf_addf(&sb, _("\n"
1107+
"It took %.2f seconds to compute the branch ahead/behind values.\n"
1108+
"You can use '--no-ahead-behind' to avoid this.\n"),
1109+
t_delta_in_ms / 1000.0);
1110+
}
1111+
}
1112+
10961113
i = 0;
10971114
if (s->display_comment_prefix) {
10981115
comment_line_string[i++] = comment_line_char;

0 commit comments

Comments
 (0)