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 <gitster@pobox.com>

Junio C Hamano committed Jul 18, 2013 at 14:11 UTC 118aa4acff6afae733c206df7049ea54f7e0b282
2 files changed +28 -8
builtin/name-rev.c
+16 -8
@@ -334,7 +334,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
334
335 for (; argc; argc--, argv++) {
336 unsigned char sha1[20];
337 - struct object *o;
337 + struct object *object;
338 struct commit *commit;
339
340 if (get_sha1(*argv, sha1)) {
@@ -343,17 +343,25 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
343 continue;
344 }
345
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",
356 *argv);
357 continue;
358 }
359
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);
365 }
366
367 if (cutoff)
t/t6120-describe.sh
+12
@@ -174,4 +174,16 @@ check_describe "test2-lightweight-*" --tags --match="test2-*"
174
175 check_describe "test2-lightweight-*" --long --tags --match="test2-*" HEAD^
176
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 +
189 test_done