Skip to content

Commit 0605465

Browse files
jeffhostetlerdscho
authored andcommitted
status: add --[no-]ahead-behind to status and commit for V2 format.
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. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c97c42a commit 0605465

File tree

7 files changed

+103
-11
lines changed

7 files changed

+103
-11
lines changed

Documentation/git-status.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ ignored, then the directory is not shown, but all contents are shown.
137137
update it afterwards if any changes were detected. Defaults to
138138
`--lock-index`.
139139

140+
--ahead-behind::
141+
--no-ahead-behind::
142+
EXPERIMENTAL, Display or do not display detailed ahead/behind
143+
counts for the branch relative to its upstream branch. Defaults
144+
to true.
145+
140146
<pathspec>...::
141147
See the 'pathspec' entry in linkgit:gitglossary[7].
142148

builtin/commit.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,9 @@ static void finalize_deferred_config(struct wt_status *s)
11511151
s->show_branch = status_deferred_config.show_branch;
11521152
if (s->show_branch < 0)
11531153
s->show_branch = 0;
1154+
1155+
if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1156+
s->ahead_behind_flags = AHEAD_BEHIND_FULL;
11541157
}
11551158

11561159
static int parse_and_validate_options(int argc, const char *argv[],
@@ -1366,6 +1369,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
13661369
N_("show branch information")),
13671370
OPT_BOOL(0, "show-stash", &s.show_stash,
13681371
N_("show stash information")),
1372+
OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1373+
N_("compute full ahead/behind values (EXPERIMENTAL)")),
13691374
{ OPTION_CALLBACK, 0, "porcelain", &status_format,
13701375
N_("version"), N_("machine-readable output"),
13711376
PARSE_OPT_OPTARG, opt_parse_porcelain },
@@ -1658,6 +1663,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16581663
OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
16591664
STATUS_FORMAT_SHORT),
16601665
OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1666+
OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1667+
N_("compute full ahead/behind values (EXPERIMENTAL)")),
16611668
OPT_SET_INT(0, "porcelain", &status_format,
16621669
N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
16631670
OPT_SET_INT(0, "long", &status_format,

remote.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,8 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
20582058
return 0;
20592059
if (abf == AHEAD_BEHIND_QUICK)
20602060
return 1;
2061+
if (abf != AHEAD_BEHIND_FULL)
2062+
BUG("stat_tracking_info: invalid abf '%d'", abf);
20612063

20622064
/* Run "rev-list --left-right ours...theirs" internally... */
20632065
argv_array_push(&argv, ""); /* ignored */

remote.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ enum match_refs_flags {
259259

260260
/* Flags for --ahead-behind option. */
261261
enum ahead_behind_flags {
262-
AHEAD_BEHIND_QUICK = 0, /* just eq/neq reporting */
263-
AHEAD_BEHIND_FULL = 1, /* traditional a/b reporting */
262+
AHEAD_BEHIND_UNSPECIFIED = -1,
263+
AHEAD_BEHIND_QUICK = 0, /* just eq/neq reporting */
264+
AHEAD_BEHIND_FULL = 1, /* traditional a/b reporting */
264265
};
265266

266267
/* Reporting of tracking info */

t/t7064-wtstatus-pv2.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,68 @@ test_expect_success 'verify upstream fields in branch header' '
390390
)
391391
'
392392

393+
test_expect_success 'verify --[no-]ahead-behind with V2 format' '
394+
git checkout master &&
395+
test_when_finished "rm -rf sub_repo" &&
396+
git clone . sub_repo &&
397+
(
398+
## Confirm local master tracks remote master.
399+
cd sub_repo &&
400+
HUF=$(git rev-parse HEAD) &&
401+
402+
# Confirm --no-ahead-behind reports traditional branch.ab with 0/0 for equal branches.
403+
cat >expect <<-EOF &&
404+
# branch.oid $HUF
405+
# branch.head master
406+
# branch.upstream origin/master
407+
# branch.ab +0 -0
408+
EOF
409+
410+
git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
411+
test_cmp expect actual &&
412+
413+
# Confirm --ahead-behind reports traditional branch.ab with 0/0.
414+
cat >expect <<-EOF &&
415+
# branch.oid $HUF
416+
# branch.head master
417+
# branch.upstream origin/master
418+
# branch.ab +0 -0
419+
EOF
420+
421+
git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
422+
test_cmp expect actual &&
423+
424+
## Test non-equal ahead/behind.
425+
echo xyz >file_xyz &&
426+
git add file_xyz &&
427+
git commit -m xyz &&
428+
429+
HUF=$(git rev-parse HEAD) &&
430+
431+
# Confirm --no-ahead-behind reports branch.ab with ?/? for non-equal branches.
432+
cat >expect <<-EOF &&
433+
# branch.oid $HUF
434+
# branch.head master
435+
# branch.upstream origin/master
436+
# branch.ab +? -?
437+
EOF
438+
439+
git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
440+
test_cmp expect actual &&
441+
442+
# Confirm --ahead-behind reports traditional branch.ab with 1/0.
443+
cat >expect <<-EOF &&
444+
# branch.oid $HUF
445+
# branch.head master
446+
# branch.upstream origin/master
447+
# branch.ab +1 -0
448+
EOF
449+
450+
git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
451+
test_cmp expect actual
452+
)
453+
'
454+
393455
test_expect_success 'create and add submodule, submodule appears clean (A. S...)' '
394456
git checkout master &&
395457
git clone . sub_repo &&

wt-status.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ void wt_status_prepare(struct wt_status *s)
136136
s->ignored.strdup_strings = 1;
137137
s->show_branch = -1; /* unspecified */
138138
s->show_stash = 0;
139+
s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
139140
s->display_comment_prefix = 0;
140141
}
141142

@@ -1883,18 +1884,19 @@ static void wt_porcelain_print(struct wt_status *s)
18831884
*
18841885
* <upstream> ::= the upstream branch name, when set.
18851886
*
1886-
* <ahead> ::= integer ahead value, when upstream set
1887-
* and the commit is present (not gone).
1888-
*
1889-
* <behind> ::= integer behind value, when upstream set
1890-
* and commit is present.
1887+
* <ahead> ::= integer ahead value or '?'.
18911888
*
1889+
* <behind> ::= integer behind value or '?'.
18921890
*
18931891
* The end-of-line is defined by the -z flag.
18941892
*
18951893
* <eol> ::= NUL when -z,
18961894
* LF when NOT -z.
18971895
*
1896+
* When an upstream is set and present, the 'branch.ab' line will
1897+
* be printed with the ahead/behind counts for the branch and the
1898+
* upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1899+
* are different, '?' will be substituted for the actual count.
18981900
*/
18991901
static void wt_porcelain_v2_print_tracking(struct wt_status *s)
19001902
{
@@ -1934,15 +1936,25 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
19341936
/* Lookup stats on the upstream tracking branch, if set. */
19351937
branch = branch_get(branch_name);
19361938
base = NULL;
1937-
ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind,
1938-
&base, AHEAD_BEHIND_FULL) >= 0);
1939+
ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1940+
&base, s->ahead_behind_flags);
19391941
if (base) {
19401942
base = shorten_unambiguous_ref(base, 0);
19411943
fprintf(s->fp, "# branch.upstream %s%c", base, eol);
19421944
free((char *)base);
19431945

1944-
if (ab_info)
1945-
fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
1946+
if (ab_info > 0) {
1947+
/* different */
1948+
if (nr_ahead || nr_behind)
1949+
fprintf(s->fp, "# branch.ab +%d -%d%c",
1950+
nr_ahead, nr_behind, eol);
1951+
else
1952+
fprintf(s->fp, "# branch.ab +? -?%c",
1953+
eol);
1954+
} else if (!ab_info) {
1955+
/* same */
1956+
fprintf(s->fp, "# branch.ab +0 -0%c", eol);
1957+
}
19461958
}
19471959
}
19481960

wt-status.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "string-list.h"
66
#include "color.h"
77
#include "pathspec.h"
8+
#include "remote.h"
89

910
struct worktree;
1011

@@ -86,6 +87,7 @@ struct wt_status {
8687
int show_branch;
8788
int show_stash;
8889
int hints;
90+
enum ahead_behind_flags ahead_behind_flags;
8991

9092
enum wt_status_format status_format;
9193
unsigned char sha1_commit[GIT_MAX_RAWSZ]; /* when not Initial */

0 commit comments

Comments
 (0)