Skip to content

Commit b6160d9

Browse files
Raphael Kubo da Costagitster
authored andcommitted
for-each-ref: always check stat_tracking_info()'s return value
The code handling %(upstream:track) and %(upstream:trackshort) assumed that it always had a valid branch that had been sanitized earlier in populate_value(), and thus did not check the return value of the call to stat_tracking_info(). While there is indeed some sanitization code that basically corresponds to stat_tracking_info() returning 0 (no base branch set), the function can also return -1 when the base branch did exist but has since then been deleted. In this case, num_ours and num_theirs had undefined values and a call to `git for-each-ref --format="%(upstream:track)"` could print spurious values such as [behind -111794512] [ahead 38881640, behind 5103867] even for repositories with one single commit. Verify stat_tracking_info()'s return value and do not print anything if it returns -1. This behavior also matches the documentation ("has no effect if the ref does not have tracking information associated with it"). Helped-by: Eric Sunshine <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Raphael Kubo da Costa <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a8c2b6 commit b6160d9

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

builtin/for-each-ref.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,10 @@ static void populate_value(struct refinfo *ref)
728728
starts_with(name, "upstream")) {
729729
char buf[40];
730730

731-
stat_tracking_info(branch, &num_ours, &num_theirs);
731+
if (stat_tracking_info(branch, &num_ours,
732+
&num_theirs) != 1)
733+
continue;
734+
732735
if (!num_ours && !num_theirs)
733736
v->s = "";
734737
else if (!num_ours) {
@@ -746,7 +749,11 @@ static void populate_value(struct refinfo *ref)
746749
} else if (!strcmp(formatp, "trackshort") &&
747750
starts_with(name, "upstream")) {
748751
assert(branch);
749-
stat_tracking_info(branch, &num_ours, &num_theirs);
752+
753+
if (stat_tracking_info(branch, &num_ours,
754+
&num_theirs) != 1)
755+
continue;
756+
750757
if (!num_ours && !num_theirs)
751758
v->s = "=";
752759
else if (!num_ours)

t/t6300-for-each-ref.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,19 @@ test_expect_success 'Check that :track[short] cannot be used with other atoms' '
334334
test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
335335
'
336336

337+
test_expect_success 'Check that :track[short] works when upstream is invalid' '
338+
cat >expected <<-\EOF &&
339+
340+
341+
EOF
342+
test_when_finished "git config branch.master.merge refs/heads/master" &&
343+
git config branch.master.merge refs/heads/does-not-exist &&
344+
git for-each-ref \
345+
--format="%(upstream:track)$LF%(upstream:trackshort)" \
346+
refs/heads >actual &&
347+
test_cmp expected actual
348+
'
349+
337350
cat >expected <<EOF
338351
$(git rev-parse --short HEAD)
339352
EOF

0 commit comments

Comments
 (0)