Skip to content

Commit c646d09

Browse files
Damien Robertgitster
authored andcommitted
ref-filter: use correct branch for %(push:track)
In ref-filter.c, when processing the atom %(push:track), the ahead/behind values are computed using `stat_tracking_info` which refers to the upstream branch. Fix that by introducing a new flag `for_push` in `stat_tracking_info` in remote.c, which does the same thing but for the push branch. Update the few callers of `stat_tracking_info` to handle this flag. This ensure that whenever we use this function in the future, we are careful to specify is this should apply to the upstream or the push branch. This bug was not detected in t/t6300-for-each-ref.sh because in the test for push:track, both the upstream and the push branches were behind by 1 from the local branch. Change the test so that the upstream branch is behind by 1 while the push branch is ahead by 1. This allows us to test that %(push:track) refers to the correct branch. This changes the expected value of some following tests (by introducing new references), so update them too. Signed-off-by: Damien Robert <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeb582a commit c646d09

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

ref-filter.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
13881388
*s = show_ref(&atom->u.remote_ref.refname, refname);
13891389
else if (atom->u.remote_ref.option == RR_TRACK) {
13901390
if (stat_tracking_info(branch, &num_ours, &num_theirs,
1391-
NULL, AHEAD_BEHIND_FULL) < 0) {
1391+
NULL, atom->u.remote_ref.push,
1392+
AHEAD_BEHIND_FULL) < 0) {
13921393
*s = xstrdup(msgs.gone);
13931394
} else if (!num_ours && !num_theirs)
13941395
*s = xstrdup("");
@@ -1406,7 +1407,8 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
14061407
}
14071408
} else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
14081409
if (stat_tracking_info(branch, &num_ours, &num_theirs,
1409-
NULL, AHEAD_BEHIND_FULL) < 0) {
1410+
NULL, atom->u.remote_ref.push,
1411+
AHEAD_BEHIND_FULL) < 0) {
14101412
*s = xstrdup("");
14111413
return;
14121414
}

remote.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,45 +1880,35 @@ int resolve_remote_symref(struct ref *ref, struct ref *list)
18801880
}
18811881

18821882
/*
1883-
* Lookup the upstream branch for the given branch and if present, optionally
1884-
* compute the commit ahead/behind values for the pair.
1883+
* Compute the commit ahead/behind values for the pair branch_name, base.
18851884
*
18861885
* If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
18871886
* counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
18881887
* the (potentially expensive) a/b computation (*num_ours and *num_theirs are
18891888
* set to zero).
18901889
*
1891-
* The name of the upstream branch (or NULL if no upstream is defined) is
1892-
* returned via *upstream_name, if it is not itself NULL.
1893-
*
1894-
* Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
1895-
* upstream defined, or ref does not exist). Returns 0 if the commits are
1896-
* identical. Returns 1 if commits are different.
1890+
* Returns -1 if num_ours and num_theirs could not be filled in (e.g., ref
1891+
* does not exist). Returns 0 if the commits are identical. Returns 1 if
1892+
* commits are different.
18971893
*/
1898-
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
1899-
const char **upstream_name, enum ahead_behind_flags abf)
1894+
1895+
static int stat_branch_pair(const char *branch_name, const char *base,
1896+
int *num_ours, int *num_theirs,
1897+
enum ahead_behind_flags abf)
19001898
{
19011899
struct object_id oid;
19021900
struct commit *ours, *theirs;
19031901
struct rev_info revs;
1904-
const char *base;
19051902
struct argv_array argv = ARGV_ARRAY_INIT;
19061903

1907-
/* Cannot stat unless we are marked to build on top of somebody else. */
1908-
base = branch_get_upstream(branch, NULL);
1909-
if (upstream_name)
1910-
*upstream_name = base;
1911-
if (!base)
1912-
return -1;
1913-
19141904
/* Cannot stat if what we used to build on no longer exists */
19151905
if (read_ref(base, &oid))
19161906
return -1;
19171907
theirs = lookup_commit_reference(the_repository, &oid);
19181908
if (!theirs)
19191909
return -1;
19201910

1921-
if (read_ref(branch->refname, &oid))
1911+
if (read_ref(branch_name, &oid))
19221912
return -1;
19231913
ours = lookup_commit_reference(the_repository, &oid);
19241914
if (!ours)
@@ -1932,7 +1922,7 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
19321922
if (abf == AHEAD_BEHIND_QUICK)
19331923
return 1;
19341924
if (abf != AHEAD_BEHIND_FULL)
1935-
BUG("stat_tracking_info: invalid abf '%d'", abf);
1925+
BUG("stat_branch_pair: invalid abf '%d'", abf);
19361926

19371927
/* Run "rev-list --left-right ours...theirs" internally... */
19381928
argv_array_push(&argv, ""); /* ignored */
@@ -1966,6 +1956,42 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
19661956
return 1;
19671957
}
19681958

1959+
/*
1960+
* Lookup the tracking branch for the given branch and if present, optionally
1961+
* compute the commit ahead/behind values for the pair.
1962+
*
1963+
* If for_push is true, the tracking branch refers to the push branch,
1964+
* otherwise it refers to the upstream branch.
1965+
*
1966+
* The name of the tracking branch (or NULL if it is not defined) is
1967+
* returned via *tracking_name, if it is not itself NULL.
1968+
*
1969+
* If abf is AHEAD_BEHIND_FULL, compute the full ahead/behind and return the
1970+
* counts in *num_ours and *num_theirs. If abf is AHEAD_BEHIND_QUICK, skip
1971+
* the (potentially expensive) a/b computation (*num_ours and *num_theirs are
1972+
* set to zero).
1973+
*
1974+
* Returns -1 if num_ours and num_theirs could not be filled in (e.g., no
1975+
* upstream defined, or ref does not exist). Returns 0 if the commits are
1976+
* identical. Returns 1 if commits are different.
1977+
*/
1978+
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
1979+
const char **tracking_name, int for_push,
1980+
enum ahead_behind_flags abf)
1981+
{
1982+
const char *base;
1983+
1984+
/* Cannot stat unless we are marked to build on top of somebody else. */
1985+
base = for_push ? branch_get_push(branch, NULL) :
1986+
branch_get_upstream(branch, NULL);
1987+
if (tracking_name)
1988+
*tracking_name = base;
1989+
if (!base)
1990+
return -1;
1991+
1992+
return stat_branch_pair(branch->refname, base, num_ours, num_theirs, abf);
1993+
}
1994+
19691995
/*
19701996
* Return true when there is anything to report, otherwise false.
19711997
*/
@@ -1977,7 +2003,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb,
19772003
char *base;
19782004
int upstream_is_gone = 0;
19792005

1980-
sti = stat_tracking_info(branch, &ours, &theirs, &full_base, abf);
2006+
sti = stat_tracking_info(branch, &ours, &theirs, &full_base, 0, abf);
19812007
if (sti < 0) {
19822008
if (!full_base)
19832009
return 0;

remote.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ enum ahead_behind_flags {
253253

254254
/* Reporting of tracking info */
255255
int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs,
256-
const char **upstream_name, enum ahead_behind_flags abf);
256+
const char **upstream_name, int for_push,
257+
enum ahead_behind_flags abf);
257258
int format_tracking_info(struct branch *branch, struct strbuf *sb,
258259
enum ahead_behind_flags abf);
259260

t/t6300-for-each-ref.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,15 @@ test_atom head upstream:track '[ahead 1]'
392392
test_atom head upstream:trackshort '>'
393393
test_atom head upstream:track,nobracket 'ahead 1'
394394
test_atom head upstream:nobracket,track 'ahead 1'
395-
test_atom head push:track '[ahead 1]'
396-
test_atom head push:trackshort '>'
395+
396+
test_expect_success 'setup for push:track[short]' '
397+
test_commit third &&
398+
git update-ref refs/remotes/myfork/master master &&
399+
git reset master~1
400+
'
401+
402+
test_atom head push:track '[behind 1]'
403+
test_atom head push:trackshort '<'
397404

398405
test_expect_success 'Check that :track[short] cannot be used with other atoms' '
399406
test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
@@ -420,8 +427,10 @@ test_expect_success 'Check for invalid refname format' '
420427
test_expect_success 'set up color tests' '
421428
cat >expected.color <<-EOF &&
422429
$(git rev-parse --short refs/heads/master) <GREEN>master<RESET>
430+
$(git rev-parse --short refs/remotes/myfork/master) <GREEN>myfork/master<RESET>
423431
$(git rev-parse --short refs/remotes/origin/master) <GREEN>origin/master<RESET>
424432
$(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
433+
$(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
425434
$(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
426435
EOF
427436
sed "s/<[^>]*>//g" <expected.color >expected.bare &&

wt-status.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
18401840
color_fprintf(s->fp, branch_color_local, "%s", branch_name);
18411841

18421842
sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
1843-
s->ahead_behind_flags);
1843+
0, s->ahead_behind_flags);
18441844
if (sti < 0) {
18451845
if (!base)
18461846
goto conclude;
@@ -1979,7 +1979,7 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s)
19791979
branch = branch_get(branch_name);
19801980
base = NULL;
19811981
ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1982-
&base, s->ahead_behind_flags);
1982+
&base, 0, s->ahead_behind_flags);
19831983
if (base) {
19841984
base = shorten_unambiguous_ref(base, 0);
19851985
fprintf(s->fp, "# branch.upstream %s%c", base, eol);

0 commit comments

Comments
 (0)