Skip to content

Commit eea9c1e

Browse files
Denton-Lgitster
authored andcommitted
tag: advise on nested tags
Robert Dailey reported confusion on the mailing list about a nested tag which was most likely created by mistake. Jeff King noted that this isn't a very common case and creating a tag-to-a-tag can be a user-error. Suggest that it may be a mistake with an advice message when creating such a tag. Those who do want to create a tag that point at another tag regularly can turn it off with the usual advice mechanism. Reported-by: Robert Dailey <[email protected]> Helped-by: Jeff King <[email protected]> Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Denton Liu <[email protected]> [jc: fixed test style and tweaked the log message] Signed-off-by: Junio C Hamano <[email protected]>
1 parent 01dc801 commit eea9c1e

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

Documentation/config/advice.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,6 @@ advice.*::
9090
waitingForEditor::
9191
Print a message to the terminal whenever Git is waiting for
9292
editor input from the user.
93+
nestedTag::
94+
Advice shown if a user attempts to recursively tag a tag object.
9395
--

advice.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int advice_ignored_hook = 1;
2626
int advice_waiting_for_editor = 1;
2727
int advice_graft_file_deprecated = 1;
2828
int advice_checkout_ambiguous_remote_branch_name = 1;
29+
int advice_nested_tag = 1;
2930

3031
static int advice_use_color = -1;
3132
static char advice_colors[][COLOR_MAXLEN] = {
@@ -81,6 +82,7 @@ static struct {
8182
{ "waitingForEditor", &advice_waiting_for_editor },
8283
{ "graftFileDeprecated", &advice_graft_file_deprecated },
8384
{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
85+
{ "nestedTag", &advice_nested_tag },
8486

8587
/* make this an alias for backward compatibility */
8688
{ "pushNonFastForward", &advice_push_update_rejected }

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern int advice_ignored_hook;
2626
extern int advice_waiting_for_editor;
2727
extern int advice_graft_file_deprecated;
2828
extern int advice_checkout_ambiguous_remote_branch_name;
29+
extern int advice_nested_tag;
2930

3031
int git_default_advice_config(const char *var, const char *value);
3132
__attribute__((format (printf, 1, 2)))

builtin/tag.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,14 @@ struct create_tag_options {
206206
} cleanup_mode;
207207
};
208208

209-
static void create_tag(const struct object_id *object, const char *tag,
209+
static const char message_advice_nested_tag[] =
210+
N_("You have created a nested tag. The object referred to by your new is\n"
211+
"already a tag. If you meant to tag the object that it points to, use:\n"
212+
"\n"
213+
"\tgit tag -f %s %s^{}");
214+
215+
static void create_tag(const struct object_id *object, const char *object_ref,
216+
const char *tag,
210217
struct strbuf *buf, struct create_tag_options *opt,
211218
struct object_id *prev, struct object_id *result)
212219
{
@@ -218,6 +225,9 @@ static void create_tag(const struct object_id *object, const char *tag,
218225
if (type <= OBJ_NONE)
219226
die(_("bad object type."));
220227

228+
if (type == OBJ_TAG && advice_nested_tag)
229+
advise(_(message_advice_nested_tag), tag, object_ref);
230+
221231
strbuf_addf(&header,
222232
"object %s\n"
223233
"type %s\n"
@@ -551,7 +561,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
551561
if (create_tag_object) {
552562
if (force_sign_annotate && !annotate)
553563
opt.sign = 1;
554-
create_tag(&object, tag, &buf, &opt, &prev, &object);
564+
create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
555565
}
556566

557567
transaction = ref_transaction_begin(&err);

t/t7004-tag.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,17 @@ test_expect_success '--points-at finds annotated tags of tags' '
17001700
test_cmp expect actual
17011701
'
17021702

1703+
test_expect_success 'recursive tagging should give advice' '
1704+
sed -e "s/|$//" <<-EOF >expect &&
1705+
hint: You have created a nested tag. The object referred to by your new is
1706+
hint: already a tag. If you meant to tag the object that it points to, use:
1707+
hint: |
1708+
hint: git tag -f nested annotated-v4.0^{}
1709+
EOF
1710+
git tag -m nested nested annotated-v4.0 2>actual &&
1711+
test_i18ncmp expect actual
1712+
'
1713+
17031714
test_expect_success 'multiple --points-at are OR-ed together' '
17041715
cat >expect <<-\EOF &&
17051716
v2.0

0 commit comments

Comments
 (0)