Skip to content

Commit 118aa4a

Browse files
committed
name-rev: differentiate between tags and commits they point at
"git name-rev --stdin" has been fixed to convert an object name that points at a tag to a refname of the tag. The codepath to handle its command line arguments, however, fed the commit that the tag points at to the underlying naming machinery. With this fix, you will get this: $ git name-rev --refs=tags/\* --name-only $(git rev-parse v1.8.3 v1.8.3^0) v1.8.3 v1.8.3^0 which is the same as what you would get from the fixed "--stdin" variant: $ git rev-parse v1.8.3 v1.8.3^0 | git name-rev --refs=tags/\* --name-only v1.8.3 v1.8.3^0 Signed-off-by: Junio C Hamano <[email protected]>
1 parent 45bc950 commit 118aa4a

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

builtin/name-rev.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
334334

335335
for (; argc; argc--, argv++) {
336336
unsigned char sha1[20];
337-
struct object *o;
337+
struct object *object;
338338
struct commit *commit;
339339

340340
if (get_sha1(*argv, sha1)) {
@@ -343,17 +343,25 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
343343
continue;
344344
}
345345

346-
o = deref_tag(parse_object(sha1), *argv, 0);
347-
if (!o || o->type != OBJ_COMMIT) {
348-
fprintf(stderr, "Could not get commit for %s. Skipping.\n",
346+
commit = NULL;
347+
object = parse_object(sha1);
348+
if (object) {
349+
struct object *peeled = deref_tag(object, *argv, 0);
350+
if (peeled && peeled->type == OBJ_COMMIT)
351+
commit = (struct commit *)peeled;
352+
}
353+
354+
if (!object) {
355+
fprintf(stderr, "Could not get object for %s. Skipping.\n",
349356
*argv);
350357
continue;
351358
}
352359

353-
commit = (struct commit *)o;
354-
if (cutoff > commit->date)
355-
cutoff = commit->date;
356-
add_object_array((struct object *)commit, *argv, &revs);
360+
if (commit) {
361+
if (cutoff > commit->date)
362+
cutoff = commit->date;
363+
}
364+
add_object_array(object, *argv, &revs);
357365
}
358366

359367
if (cutoff)

t/t6120-describe.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,16 @@ check_describe "test2-lightweight-*" --tags --match="test2-*"
174174

175175
check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
176176

177+
test_expect_success 'name-rev with exact tags' '
178+
echo A >expect &&
179+
tag_object=$(git rev-parse refs/tags/A) &&
180+
git name-rev --tags --name-only $tag_object >actual &&
181+
test_cmp expect actual &&
182+
183+
echo "A^0" >expect &&
184+
tagged_commit=$(git rev-parse "refs/tags/A^0") &&
185+
git name-rev --tags --name-only $tagged_commit >actual &&
186+
test_cmp expect actual
187+
'
188+
177189
test_done

0 commit comments

Comments
 (0)