Skip to content

Commit 8bad3de

Browse files
committed
Merge branch 'jk/list-tag-2.7-regression'
"git tag" started listing a tag "foo" as "tags/foo" when a branch named "foo" exists in the same repository; remove this unnecessary disambiguation, which is a regression introduced in v2.7.0. * jk/list-tag-2.7-regression: tag: do not show ambiguous tag names as "tags/foo" t6300: use test_atom for some un-modern tests
2 parents 6d579a0 + 0571979 commit 8bad3de

File tree

7 files changed

+72
-56
lines changed

7 files changed

+72
-56
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ refname::
9292
The name of the ref (the part after $GIT_DIR/).
9393
For a non-ambiguous short name of the ref append `:short`.
9494
The option core.warnAmbiguousRefs is used to select the strict
95-
abbreviation mode.
95+
abbreviation mode. If `strip=<N>` is appended, strips `<N>`
96+
slash-separated path components from the front of the refname
97+
(e.g., `%(refname:strip=2)` turns `refs/tags/foo` into `foo`.
98+
`<N>` must be a positive integer. If a displayed ref has fewer
99+
components than `<N>`, the command aborts with an error.
96100

97101
objecttype::
98102
The type of the object (`blob`, `tree`, `commit`, `tag`).

Documentation/git-tag.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ This option is only applicable when listing tags without annotation lines.
163163
A string that interpolates `%(fieldname)` from the object
164164
pointed at by a ref being shown. The format is the same as
165165
that of linkgit:git-for-each-ref[1]. When unspecified,
166-
defaults to `%(refname:short)`.
166+
defaults to `%(refname:strip=2)`.
167167

168168
--[no-]merged [<commit>]::
169169
Only list tags whose tips are reachable, or not reachable

builtin/tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
4444
if (!format) {
4545
if (filter->lines) {
4646
to_free = xstrfmt("%s %%(contents:lines=%d)",
47-
"%(align:15)%(refname:short)%(end)",
47+
"%(align:15)%(refname:strip=2)%(end)",
4848
filter->lines);
4949
format = to_free;
5050
} else
51-
format = "%(refname:short)";
51+
format = "%(refname:strip=2)";
5252
}
5353

5454
verify_ref_format(format);

ref-filter.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,29 @@ static inline char *copy_advance(char *dst, const char *src)
763763
return dst;
764764
}
765765

766+
static const char *strip_ref_components(const char *refname, const char *nr_arg)
767+
{
768+
char *end;
769+
long nr = strtol(nr_arg, &end, 10);
770+
long remaining = nr;
771+
const char *start = refname;
772+
773+
if (nr < 1 || *end != '\0')
774+
die(":strip= requires a positive integer argument");
775+
776+
while (remaining) {
777+
switch (*start++) {
778+
case '\0':
779+
die("ref '%s' does not have %ld components to :strip",
780+
refname, nr);
781+
case '/':
782+
remaining--;
783+
break;
784+
}
785+
}
786+
return start;
787+
}
788+
766789
/*
767790
* Parse the object referred by ref, and grab needed value.
768791
*/
@@ -909,11 +932,14 @@ static void populate_value(struct ref_array_item *ref)
909932
formatp = strchr(name, ':');
910933
if (formatp) {
911934
int num_ours, num_theirs;
935+
const char *arg;
912936

913937
formatp++;
914938
if (!strcmp(formatp, "short"))
915939
refname = shorten_unambiguous_ref(refname,
916940
warn_ambiguous_refs);
941+
else if (skip_prefix(formatp, "strip=", &arg))
942+
refname = strip_ref_components(refname, arg);
917943
else if (!strcmp(formatp, "track") &&
918944
(starts_with(name, "upstream") ||
919945
starts_with(name, "push"))) {

t/t3203-branch-output.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,12 @@ test_expect_success 'git branch --points-at option' '
176176
test_cmp expect actual
177177
'
178178

179+
test_expect_success 'ambiguous branch/tag not marked' '
180+
git tag ambiguous &&
181+
git branch ambiguous &&
182+
echo " ambiguous" >expect &&
183+
git branch --list ambiguous >actual &&
184+
test_cmp expect actual
185+
'
186+
179187
test_done

t/t6300-for-each-ref.sh

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ test_atom() {
4949
}
5050

5151
test_atom head refname refs/heads/master
52+
test_atom head refname:short master
53+
test_atom head refname:strip=1 heads/master
54+
test_atom head refname:strip=2 master
5255
test_atom head upstream refs/remotes/origin/master
56+
test_atom head upstream:short origin/master
5357
test_atom head push refs/remotes/myfork/master
58+
test_atom head push:short myfork/master
5459
test_atom head objecttype commit
5560
test_atom head objectsize 171
5661
test_atom head objectname $(git rev-parse refs/heads/master)
62+
test_atom head objectname:short $(git rev-parse --short refs/heads/master)
5763
test_atom head tree $(git rev-parse refs/heads/master^{tree})
5864
test_atom head parent ''
5965
test_atom head numparent 0
@@ -86,11 +92,13 @@ test_atom head contents 'Initial
8692
test_atom head HEAD '*'
8793

8894
test_atom tag refname refs/tags/testtag
95+
test_atom tag refname:short testtag
8996
test_atom tag upstream ''
9097
test_atom tag push ''
9198
test_atom tag objecttype tag
9299
test_atom tag objectsize 154
93100
test_atom tag objectname $(git rev-parse refs/tags/testtag)
101+
test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
94102
test_atom tag tree ''
95103
test_atom tag parent ''
96104
test_atom tag numparent ''
@@ -126,6 +134,16 @@ test_expect_success 'Check invalid atoms names are errors' '
126134
test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
127135
'
128136

137+
test_expect_success 'arguments to :strip must be positive integers' '
138+
test_must_fail git for-each-ref --format="%(refname:strip=0)" &&
139+
test_must_fail git for-each-ref --format="%(refname:strip=-1)" &&
140+
test_must_fail git for-each-ref --format="%(refname:strip=foo)"
141+
'
142+
143+
test_expect_success 'stripping refnames too far gives an error' '
144+
test_must_fail git for-each-ref --format="%(refname:strip=3)"
145+
'
146+
129147
test_expect_success 'Check format specifiers are ignored in naming date atoms' '
130148
git for-each-ref --format="%(authordate)" refs/heads &&
131149
git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
@@ -338,47 +356,14 @@ for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
338356
"
339357
done
340358

341-
cat >expected <<\EOF
342-
master
343-
testtag
344-
EOF
345-
346-
test_expect_success 'Check short refname format' '
347-
(git for-each-ref --format="%(refname:short)" refs/heads &&
348-
git for-each-ref --format="%(refname:short)" refs/tags) >actual &&
349-
test_cmp expected actual
350-
'
351-
352-
cat >expected <<EOF
353-
origin/master
354-
EOF
355-
356-
test_expect_success 'Check short upstream format' '
357-
git for-each-ref --format="%(upstream:short)" refs/heads >actual &&
358-
test_cmp expected actual
359-
'
360-
361359
test_expect_success 'setup for upstream:track[short]' '
362360
test_commit two
363361
'
364362

365-
cat >expected <<EOF
366-
[ahead 1]
367-
EOF
368-
369-
test_expect_success 'Check upstream:track format' '
370-
git for-each-ref --format="%(upstream:track)" refs/heads >actual &&
371-
test_cmp expected actual
372-
'
373-
374-
cat >expected <<EOF
375-
>
376-
EOF
377-
378-
test_expect_success 'Check upstream:trackshort format' '
379-
git for-each-ref --format="%(upstream:trackshort)" refs/heads >actual &&
380-
test_cmp expected actual
381-
'
363+
test_atom head upstream:track '[ahead 1]'
364+
test_atom head upstream:trackshort '>'
365+
test_atom head push:track '[ahead 1]'
366+
test_atom head push:trackshort '>'
382367

383368
test_expect_success 'Check that :track[short] cannot be used with other atoms' '
384369
test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
@@ -398,21 +383,6 @@ test_expect_success 'Check that :track[short] works when upstream is invalid' '
398383
test_cmp expected actual
399384
'
400385

401-
test_expect_success '%(push) supports tracking specifiers, too' '
402-
echo "[ahead 1]" >expected &&
403-
git for-each-ref --format="%(push:track)" refs/heads >actual &&
404-
test_cmp expected actual
405-
'
406-
407-
cat >expected <<EOF
408-
$(git rev-parse --short HEAD)
409-
EOF
410-
411-
test_expect_success 'Check short objectname format' '
412-
git for-each-ref --format="%(objectname:short)" refs/heads >actual &&
413-
test_cmp expected actual
414-
'
415-
416386
test_expect_success 'Check for invalid refname format' '
417387
test_must_fail git for-each-ref --format="%(refname:INVALID)"
418388
'

t/t7004-tag.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,4 +1558,12 @@ test_expect_success '--no-merged show unmerged tags' '
15581558
test_cmp expect actual
15591559
'
15601560

1561+
test_expect_success 'ambiguous branch/tags not marked' '
1562+
git tag ambiguous &&
1563+
git branch ambiguous &&
1564+
echo ambiguous >expect &&
1565+
git tag -l ambiguous >actual &&
1566+
test_cmp expect actual
1567+
'
1568+
15611569
test_done

0 commit comments

Comments
 (0)