describe: fix --contains when a tag is given as input

"git describe" takes a commit and gives it a name based on tags in its neighbourhood. The command does take a commit-ish but when given a tag that points at a commit, it should dereference the tag before computing the name for the commit. As the whole processing is internally delegated to name-rev, if we unwrap tags down to the underlying commit when invoking name-rev, it will make the name-rev issue an error message based on the unwrapped object name (i.e. either 40-hex object name, or "$tag^0") that is different from what the end-user gave to the command when the commit cannot be described. Introduce an internal option --peel-tag to the name-rev to tell it to unwrap a tag in its input from the command line. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 18, 2013 at 14:46 UTC adfc1857bdb090786fd9d22c1acec39371c76048
3 files changed +30 -2
builtin/describe.c
+2 -1
@@ -446,7 +446,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
446 struct argv_array args;
447
448 argv_array_init(&args);
449 - argv_array_pushl(&args, "name-rev", "--name-only", "--no-undefined",
449 + argv_array_pushl(&args, "name-rev",
450 + "--peel-tag", "--name-only", "--no-undefined",
451 NULL);
452 if (always)
453 argv_array_push(&args, "--always");
builtin/name-rev.c
+16 -1
@@ -307,7 +307,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
307 int cmd_name_rev(int argc, const char **argv, const char *prefix)
308 {
309 struct object_array revs = OBJECT_ARRAY_INIT;
310 - int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
310 + int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
311 struct name_ref_data data = { 0, 0, NULL };
312 struct option opts[] = {
313 OPT_BOOLEAN(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
@@ -320,6 +320,12 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
320 OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")),
321 OPT_BOOLEAN(0, "always", &always,
322 N_("show abbreviated commit object as fallback")),
323 + {
324 + /* A Hidden OPT_BOOL */
325 + OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
326 + N_("dereference tags in the input (internal use)"),
327 + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
328 + },
329 OPT_END(),
330 };
331
@@ -361,6 +367,15 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
367 if (cutoff > commit->date)
368 cutoff = commit->date;
369 }
370 +
371 + if (peel_tag) {
372 + if (!commit) {
373 + fprintf(stderr, "Could not get commit for %s. Skipping.\n",
374 + *argv);
375 + continue;
376 + }
377 + object = (struct object *)commit;
378 + }
379 add_object_array(object, *argv, &revs);
380 }
381
t/t6120-describe.sh
+12
@@ -186,4 +186,16 @@ test_expect_success 'name-rev with exact tags' '
186 test_cmp expect actual
187 '
188
189 +test_expect_success 'describe --contains with the exact tags' '
190 + echo "A^0" >expect &&
191 + tag_object=$(git rev-parse refs/tags/A) &&
192 + git describe --contains $tag_object >actual &&
193 + test_cmp expect actual &&
194 +
195 + echo "A^0" >expect &&
196 + tagged_commit=$(git rev-parse "refs/tags/A^0") &&
197 + git describe --contains $tagged_commit >actual &&
198 + test_cmp expect actual
199 +'
200 +
201 test_done