Add --no-ahead-behind to status
v5 cover Add --no-ahead-behind to status
From: Jeff Hostetler <[email protected]>
This is version 5 of my patch series to avoid expensive ahead/behind calculations in status. This version completely removes the config setting and is just the --[no-]ahead-behind command line argument.
Internally (inside MSFT) we have had further discussions in this area and identified 2 somewhat independent needs:
[1] The first is to just be able to turn off the a/b calculation when the invoker does not need the result at all.
[2] The second is to greatly speed up or limit the a/b calculation on extremely large repositories.
In earlier versions of this patch series, there were several discussions of a limited mode that would look for no more than n commits or milliseconds. Also discussed were some problems that such limiting will have and it was decided to be not worth the effort.
We have been discussing some ways to speed up the calculation on the client and have tentatively scheduled this shortly. Hopefully, this will eliminate the performance problems and reduce the likelyhood that anyone would need to set a config setting to change the default behavior (in either porcelain or non-porcelain) formats.
So with that in mind, this version elimates the config setting so that we have don't to carry forward a soon-to-be-obsolete setting.
All that remains in this version is the command line argument to turn on/off the a/b calculation.
Jeff Hostetler (4):
stat_tracking_info: return +1 when branches not equal
status: add --[no-]ahead-behind to status and commit for V2 format.
status: update short status to respect --no-ahead-behind
status: support --no-ahead-behind in long format
Documentation/git-status.txt | 5 ++++ builtin/checkout.c | 2 +- builtin/commit.c | 7 +++++ ref-filter.c | 8 +++--- remote.c | 50 +++++++++++++++++++++++------------ remote.h | 12 +++++++-- t/t6040-tracking-info.sh | 42 ++++++++++++++++++++++++++++++ t/t7064-wtstatus-pv2.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++ wt-status.c | 41 ++++++++++++++++++++--------- wt-status.h | 2 ++ 10 files changed, 196 insertions(+), 35 deletions(-)
Signature · 1 line
2.9.3
PATCH v5 1/4 stat_tracking_info: return +1 when branches not equal
From: Jeff Hostetler <[email protected]>
Extend stat_tracking_info() to return +1 when branches are not equal and to take a new "enum ahead_behind_flags" argument to allow skipping the (possibly expensive) ahead/behind computation.
This will be used in the next commit to allow "git status" to avoid full ahead/behind calculations for performance reasons.
--- ref-filter.c | 8 ++++---- remote.c | 34 +++++++++++++++++++++------------- remote.h | 8 +++++++- wt-status.c | 6 ++++-- 4 files changed, 36 insertions(+), 20 deletions(-)
ref-filter.c +4 -4
@@ -1238,8 +1238,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,if (atom->u.remote_ref.option ==RR_REF ) *s =show_ref (&atom->u.remote_ref.refname, refname);else if (atom->u.remote_ref.option ==RR_TRACK ) { -if (stat_tracking_info (branch, &num_ours, - &num_theirs,NULL )) { +if (stat_tracking_info (branch, &num_ours, &num_theirs, +NULL ,AHEAD_BEHIND_FULL ) <0 ) { *s =xstrdup (msgs.gone); }else if (!num_ours && !num_theirs) *s ="" ;
@@ -1256,8 +1256,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,free ((void *)to_free); } }else if (atom->u.remote_ref.option ==RR_TRACKSHORT ) { -if (stat_tracking_info (branch, &num_ours, - &num_theirs,NULL )) +if (stat_tracking_info (branch, &num_ours, &num_theirs, +NULL ,AHEAD_BEHIND_FULL ) <0 )return ;if (!num_ours && !num_theirs)
remote.c +21 -13
@@ -1977,16 +1977,23 @@ int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) }/* -* Compare a branch with its upstream, and save their differences (number -* of commits) in *num_ours and *num_theirs. The name of the upstream branch -* (or NULL if no upstream is defined) is returned via *upstream_name, if it -* is not itself NULL. +* Lookup the upstream branch for the given branch and if present, optionally +* compute the commit ahead/behind values for the pair. +* +* If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the +* counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip +* the (potentially expensive) a/b computation (*num_ours and *num_theirs are +* set to zero). +* +* The name of the upstream branch (or NULL if no upstream is defined) is +* returned via *upstream_name, if it is not itself NULL. * * Returns -1 if num_ours and num_theirs could not be filled in (e.g., no -* upstream defined, or ref does not exist), 0 otherwise. +* upstream defined, or ref does not exist). Returns 0 if the commits are +* identical. Returns 1 if commits are different. */ int stat_tracking_info (struct branch *branch,int *num_ours,int *num_theirs, -const char **upstream_name) +const char **upstream_name,enum ahead_behind_flags abf) {struct object_id oid;struct commit *ours, *theirs;
@@ -2014,11 +2021,13 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,if (!ours)return -1 ; + *num_theirs = *num_ours =0 ; +/* are we the same? */ -if (theirs == ours) { - *num_theirs = *num_ours =0 ; +if (theirs == ours)return 0 ; - } +if (abf ==AHEAD_BEHIND_QUICK ) +return 1 ;/* Run "rev-list --left-right ours...theirs" internally... */ argv_array_push (&argv,"" );/* ignored */
@@ -2034,8 +2043,6 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,die ("revision walk setup failed" );/* ... and count the commits on each side. */ - *num_ours =0 ; - *num_theirs =0 ;while (1 ) {struct commit *c =get_revision (&revs);if (!c)
@@ -2051,7 +2058,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,clear_commit_marks (theirs,ALL_REV_FLAGS );argv_array_clear (&argv); -return 0 ; +return 1 ; }/*
@@ -2064,7 +2071,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)char *base;int upstream_is_gone =0 ; -if (stat_tracking_info (branch, &ours, &theirs, &full_base) <0 ) { +if (stat_tracking_info (branch, &ours, &theirs, &full_base, +AHEAD_BEHIND_FULL ) <0 ) {if (!full_base)return 0 ; upstream_is_gone =1 ;
remote.h +7 -1
@@ -255,9 +255,15 @@ enum match_refs_flags { MATCH_REFS_FOLLOW_TAGS = (1 << 3) }; +/* Flags for --ahead-behind option. */ +enum ahead_behind_flags { +AHEAD_BEHIND_QUICK =0 ,/* just eq/neq reporting */ +AHEAD_BEHIND_FULL =1 ,/* traditional a/b reporting */ +}; +/* Reporting of tracking info */ int stat_tracking_info (struct branch *branch,int *num_ours,int *num_theirs, -const char **upstream_name); +const char **upstream_name,enum ahead_behind_flags abf);int format_tracking_info (struct branch *branch,struct strbuf *sb);struct ref *get_local_heads (void );
wt-status.c +4 -2
@@ -1791,7 +1791,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)color_fprintf (s->fp, branch_color_local,"%s" , branch_name); -if (stat_tracking_info (branch, &num_ours, &num_theirs, &base) <0 ) { +if (stat_tracking_info (branch, &num_ours, &num_theirs, &base, +AHEAD_BEHIND_FULL ) <0 ) {if (!base) goto conclude;
@@ -1928,7 +1929,8 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)/* Lookup stats on the upstream tracking branch, if set. */ branch =branch_get (branch_name); base =NULL ; - ab_info = (stat_tracking_info (branch, &nr_ahead, &nr_behind, &base) ==0 ); + ab_info = (stat_tracking_info (branch, &nr_ahead, &nr_behind, + &base,AHEAD_BEHIND_FULL ) >=0 );if (base) { base =shorten_unambiguous_ref (base,0 );fprintf (s->fp,"# branch.upstream %s%c" , base, eol);
Signature · 1 line
2.9.3
PATCH v5 2/4 status: add --[no-]ahead-behind to status and commit for V2 format.
In reply to Jeff HostetlerFrom: Jeff Hostetler <[email protected]>
Teach "git status" and "git commit" to accept "--no-ahead-behind" and "--ahead-behind" arguments to request quick or full ahead/behind reporting.
When "--no-ahead-behind" is given, the existing porcelain V2 line "branch.ab +x -y" is replaced with a new "branch.ab +? -?" line. This indicates that the branch and its upstream are or are not equal without the expense of computing the full ahead/behind values.
--- Documentation/git-status.txt | 5 ++++ builtin/commit.c | 7 +++++ remote.c | 2 ++ remote.h | 5 ++-- t/t7064-wtstatus-pv2.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++ wt-status.c | 30 ++++++++++++++------- wt-status.h | 2 ++ 7 files changed, 102 insertions(+), 11 deletions(-)
Documentation/git-status.txt +5 -0
@@ -111,6 +111,11 @@ configuration variable documented in linkgit:git-config[1]. without options are equivalent to 'always' and 'never' respectively. +--ahead-behind:: +--no-ahead-behind:: + Display or do not display detailed ahead/behind counts for the + branch relative to its upstream branch. Defaults to true. + <pathspec>...:: See the 'pathspec' entry in linkgit:gitglossary[7].
builtin/commit.c +7 -0
@@ -1137,6 +1137,9 @@ static void finalize_deferred_config(struct wt_status *s) s->show_branch = status_deferred_config.show_branch;if (s->show_branch <0 ) s->show_branch =0 ; + +if (s->ahead_behind_flags ==AHEAD_BEHIND_UNSPECIFIED ) + s->ahead_behind_flags =AHEAD_BEHIND_FULL ; } static int parse_and_validate_options(int argc, const char *argv[],
@@ -1351,6 +1354,8 @@ int cmd_status(int argc, const char **argv, const char *prefix) N_("show branch information")),OPT_BOOL (0 ,"show-stash" , &s.show_stash,N_ ("show stash information" )), +OPT_BOOL (0 ,"ahead-behind" , &s.ahead_behind_flags, +N_ ("compute full ahead/behind values" )), { OPTION_CALLBACK, 0, "porcelain", &status_format,N_ ("version" ),N_ ("machine-readable output" ), PARSE_OPT_OPTARG, opt_parse_porcelain },
@@ -1628,6 +1633,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)OPT_SET_INT (0 ,"short" , &status_format,N_ ("show status concisely" ),STATUS_FORMAT_SHORT ),OPT_BOOL (0 ,"branch" , &s.show_branch,N_ ("show branch information" )), +OPT_BOOL (0 ,"ahead-behind" , &s.ahead_behind_flags, +N_ ("compute full ahead/behind values" )),OPT_SET_INT (0 ,"porcelain" , &status_format,N_ ("machine-readable output" ),STATUS_FORMAT_PORCELAIN ), OPT_SET_INT(0, "long", &status_format,
remote.c +2 -0
@@ -2028,6 +2028,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,return 0 ;if (abf ==AHEAD_BEHIND_QUICK )return 1 ; +if (abf !=AHEAD_BEHIND_FULL ) +BUG ("stat_tracking_info: invalid abf '%d'" , abf);/* Run "rev-list --left-right ours...theirs" internally... */ argv_array_push (&argv,"" );/* ignored */
remote.h +3 -2
@@ -257,8 +257,9 @@ enum match_refs_flags {/* Flags for --ahead-behind option. */ enum ahead_behind_flags { -AHEAD_BEHIND_QUICK =0 ,/* just eq/neq reporting */ -AHEAD_BEHIND_FULL =1 ,/* traditional a/b reporting */ +AHEAD_BEHIND_UNSPECIFIED =-1 , +AHEAD_BEHIND_QUICK =0 ,/* just eq/neq reporting */ +AHEAD_BEHIND_FULL =1 ,/* traditional a/b reporting */ };/* Reporting of tracking info */
t/t7064-wtstatus-pv2.sh +62 -0
68 lines not on this page · raw message
wt-status.c +21 -9
@@ -136,6 +136,7 @@ void wt_status_prepare(struct wt_status *s) s->ignored.strdup_strings =1 ; s->show_branch =-1 ;/* unspecified */ s->show_stash =0 ; + s->ahead_behind_flags =AHEAD_BEHIND_UNSPECIFIED ; s->display_comment_prefix =0 ; }
@@ -1878,18 +1879,19 @@ static void wt_porcelain_print(struct wt_status *s)* * <upstream> ::= the upstream branch name, when set. * -* <ahead> ::= integer ahead value, when upstream set -* and the commit is present (not gone). -* -* <behind> ::= integer behind value, when upstream set -* and commit is present. +* <ahead> ::= integer ahead value or '?'. * +* <behind> ::= integer behind value or '?'. * * The end-of-line is defined by the -z flag. * * <eol> ::= NUL when -z, * LF when NOT -z. * +* When an upstream is set and present, the 'branch.ab' line will +* be printed with the ahead/behind counts for the branch and the +* upstream. When AHEAD_BEHIND_QUICK is requested and the branches +* are different, '?' will be substituted for the actual count. */ static void wt_porcelain_v2_print_tracking (struct wt_status *s) {
@@ -1929,15 +1931,25 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)/* Lookup stats on the upstream tracking branch, if set. */ branch =branch_get (branch_name); base =NULL ; - ab_info = (stat_tracking_info (branch, &nr_ahead, &nr_behind, - &base,AHEAD_BEHIND_FULL ) >=0 ); + ab_info =stat_tracking_info (branch, &nr_ahead, &nr_behind, + &base, s->ahead_behind_flags);if (base) { base =shorten_unambiguous_ref (base,0 );fprintf (s->fp,"# branch.upstream %s%c" , base, eol);free ((char *)base); -if (ab_info) -fprintf (s->fp,"# branch.ab +%d -%d%c" , nr_ahead, nr_behind, eol); +if (ab_info >0 ) { +/* different */ +if (nr_ahead || nr_behind) +fprintf (s->fp,"# branch.ab +%d -%d%c" , + nr_ahead, nr_behind, eol); +else +fprintf (s->fp,"# branch.ab +? -?%c" , + eol); + }else if (!ab_info) { +/* same */ +fprintf (s->fp,"# branch.ab +0 -0%c" , eol); + } } }
wt-status.h +2 -0
@@ -5,6 +5,7 @@#include "string-list.h" #include "color.h" #include "pathspec.h" +#include "remote.h" struct worktree ;
@@ -80,6 +81,7 @@ struct wt_status {int show_branch;int show_stash;int hints; +enum ahead_behind_flags ahead_behind_flags;enum wt_status_format status_format;unsigned char sha1_commit[GIT_MAX_RAWSZ ];/* when not Initial */
Signature · 1 line
2.9.3
PATCH v5 3/4 status: update short status to respect --no-ahead-behind
In reply to Jeff HostetlerFrom: Jeff Hostetler <[email protected]>
Teach "git status --short --branch" to respect "--no-ahead-behind" parameter to skip computing ahead/behind counts for the branch and its upstream and just report '[different]'.
--- t/t6040-tracking-info.sh | 13 +++++++++++++ wt-status.c | 11 +++++++---- 2 files changed, 20 insertions(+), 4 deletions(-)
t/t6040-tracking-info.sh +13 -0
@@ -147,6 +147,19 @@ test_expect_success 'status -s -b (diverged from upstream)' ' ' cat >expect <<\EOF +## b1...origin/master [different] +EOF + +test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' ' + ( + cd test && + git checkout b1 >/dev/null && + git status -s -b --no-ahead-behind | head -1 + ) >actual && + test_i18ncmp expect actual +' + +cat >expect <<\EOF ## b5...brokenbase [gone] EOF
wt-status.c +7 -4
@@ -1766,7 +1766,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)const char *base;char *short_base;const char *branch_name; -int num_ours, num_theirs; +int num_ours, num_theirs, sti;int upstream_is_gone =0 ;color_fprintf (s->fp,color (WT_STATUS_HEADER , s),"## " );
@@ -1792,8 +1792,9 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)color_fprintf (s->fp, branch_color_local,"%s" , branch_name); -if (stat_tracking_info (branch, &num_ours, &num_theirs, &base, -AHEAD_BEHIND_FULL ) <0 ) { + sti =stat_tracking_info (branch, &num_ours, &num_theirs, &base, + s->ahead_behind_flags); +if (sti <0 ) {if (!base) goto conclude;
@@ -1805,12 +1806,14 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)color_fprintf (s->fp, branch_color_remote,"%s" , short_base);free (short_base); -if (!upstream_is_gone && !num_ours && !num_theirs) +if (!upstream_is_gone && !sti) goto conclude;color_fprintf (s->fp, header_color," [" );if (upstream_is_gone) {color_fprintf (s->fp, header_color,LABEL (N_ ("gone" ))); + }else if (s->ahead_behind_flags ==AHEAD_BEHIND_QUICK ) { +color_fprintf (s->fp, header_color,LABEL (N_ ("different" ))); }else if (!num_ours) {color_fprintf (s->fp, header_color,LABEL (N_ ("behind " )));color_fprintf (s->fp, branch_color_remote,"%d" , num_theirs);
Signature · 1 line
2.9.3
PATCH v5 4/4 status: support --no-ahead-behind in long format
In reply to Jeff HostetlerFrom: Jeff Hostetler <[email protected]>
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.
--- builtin/checkout.c | 2 +- remote.c | 18 +++++++++++++----- remote.h | 3 ++- t/t6040-tracking-info.sh | 29 +++++++++++++++++++++++++++++ wt-status.c | 2 +- 5 files changed, 46 insertions(+), 8 deletions(-)
builtin/checkout.c +1 -1
@@ -605,7 +605,7 @@ static void report_tracking(struct branch_info *new)struct strbuf sb =STRBUF_INIT ;struct branch *branch =branch_get (new->name); -if (!format_tracking_info (branch, &sb)) +if (!format_tracking_info (branch, &sb,AHEAD_BEHIND_FULL ))return ;fputs (sb.buf, stdout);strbuf_release (&sb);
remote.c +13 -5
@@ -2066,15 +2066,16 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,/* * Return true when there is anything to report, otherwise false. */ -int format_tracking_info (struct branch *branch,struct strbuf *sb) +int format_tracking_info (struct branch *branch,struct strbuf *sb, +enum ahead_behind_flags abf) { -int ours, theirs; +int ours, theirs, sti;const char *full_base;char *base;int upstream_is_gone =0 ; -if (stat_tracking_info (branch, &ours, &theirs, &full_base, -AHEAD_BEHIND_FULL ) <0 ) { + sti =stat_tracking_info (branch, &ours, &theirs, &full_base, abf); +if (sti <0 ) {if (!full_base)return 0 ; upstream_is_gone =1 ;
@@ -2088,10 +2089,17 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb)if (advice_status_hints)strbuf_addstr (sb,_ (" (use \"git branch --unset-upstream\" to fixup)\n" )); - }else if (!ours && !theirs) { + }else if (!sti) {strbuf_addf (sb,_ ("Your branch is up to date with '%s'.\n" ), base); + }else if (abf ==AHEAD_BEHIND_QUICK ) { +strbuf_addf (sb, +_ ("Your branch and '%s' refer to different commits.\n" ), + base); +if (advice_status_hints) +strbuf_addf (sb,_ (" (use \"%s\" for details)\n" ), +"git status --ahead-behind" ); }else if (!theirs) { strbuf_addf(sb, Q_("Your branch is ahead of '%s' by %d commit.\n",
remote.h +2 -1
@@ -265,7 +265,8 @@ enum ahead_behind_flags {/* Reporting of tracking info */ int stat_tracking_info (struct branch *branch,int *num_ours,int *num_theirs,const char **upstream_name,enum ahead_behind_flags abf); -int format_tracking_info (struct branch *branch,struct strbuf *sb); +int format_tracking_info (struct branch *branch,struct strbuf *sb, +enum ahead_behind_flags abf);struct ref *get_local_heads (void );/*
t/t6040-tracking-info.sh +29 -0
@@ -160,6 +160,35 @@ test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' ' ' cat >expect <<\EOF +On branch b1 +Your branch and 'origin/master' have diverged, +and have 1 and 1 different commits each, respectively. +EOF + +test_expect_success 'status --long --branch' ' + ( + cd test && + git checkout b1 >/dev/null && + git status --long -b | head -3 + ) >actual && + test_i18ncmp expect actual +' + +cat >expect <<\EOF +On branch b1 +Your branch and 'origin/master' refer to different commits. +EOF + +test_expect_success 'status --long --branch --no-ahead-behind' ' + ( + cd test && + git checkout b1 >/dev/null && + git status --long -b --no-ahead-behind | head -2 + ) >actual && + test_i18ncmp expect actual +' + +cat >expect <<\EOF ## b5...brokenbase [gone] EOF
wt-status.c +1 -1
@@ -1006,7 +1006,7 @@ static void wt_longstatus_print_tracking(struct wt_status *s)if (!skip_prefix (s->branch,"refs/heads/" , &branch_name))return ; branch =branch_get (branch_name); -if (!format_tracking_info (branch, &sb)) +if (!format_tracking_info (branch, &sb, s->ahead_behind_flags))return ; i =0 ;
Signature · 1 line
2.9.3