Skip to content

Commit adfc185

Browse files
committed
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 <[email protected]>
1 parent 118aa4a commit adfc185

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

builtin/describe.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
446446
struct argv_array args;
447447

448448
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",
450451
NULL);
451452
if (always)
452453
argv_array_push(&args, "--always");

builtin/name-rev.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
307307
int cmd_name_rev(int argc, const char **argv, const char *prefix)
308308
{
309309
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;
311311
struct name_ref_data data = { 0, 0, NULL };
312312
struct option opts[] = {
313313
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)
320320
OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")),
321321
OPT_BOOLEAN(0, "always", &always,
322322
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+
},
323329
OPT_END(),
324330
};
325331

@@ -361,6 +367,15 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
361367
if (cutoff > commit->date)
362368
cutoff = commit->date;
363369
}
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+
}
364379
add_object_array(object, *argv, &revs);
365380
}
366381

t/t6120-describe.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,16 @@ test_expect_success 'name-rev with exact tags' '
186186
test_cmp expect actual
187187
'
188188

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+
189201
test_done

0 commit comments

Comments
 (0)