Skip to content

Commit a198562

Browse files
committed
Merge branch 'dl/warn-tagging-a-tag'
"git tag" learned to give an advice suggesting it might be a mistake when creating an annotated or signed tag that points at another tag. * dl/warn-tagging-a-tag: tag: advise on nested tags tag: fix formatting
2 parents 96379f0 + eea9c1e commit a198562

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
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: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
#include "ref-filter.h"
2323

2424
static const char * const git_tag_usage[] = {
25-
N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
25+
N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"
26+
"\t\t<tagname> [<head>]"),
2627
N_("git tag -d <tagname>..."),
27-
N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]"
28-
"\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
28+
N_("git tag -l [-n[<num>]] [--contains <commit>] [--no-contains <commit>] [--points-at <object>]\n"
29+
"\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
2930
N_("git tag -v [--format=<format>] <tagname>..."),
3031
NULL
3132
};
@@ -205,7 +206,14 @@ struct create_tag_options {
205206
} cleanup_mode;
206207
};
207208

208-
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,
209217
struct strbuf *buf, struct create_tag_options *opt,
210218
struct object_id *prev, struct object_id *result)
211219
{
@@ -215,7 +223,10 @@ static void create_tag(const struct object_id *object, const char *tag,
215223

216224
type = oid_object_info(the_repository, object, NULL);
217225
if (type <= OBJ_NONE)
218-
die(_("bad object type."));
226+
die(_("bad object type."));
227+
228+
if (type == OBJ_TAG && advice_nested_tag)
229+
advise(_(message_advice_nested_tag), tag, object_ref);
219230

220231
strbuf_addf(&header,
221232
"object %s\n"
@@ -549,7 +560,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
549560
if (create_tag_object) {
550561
if (force_sign_annotate && !annotate)
551562
opt.sign = 1;
552-
create_tag(&object, tag, &buf, &opt, &prev, &object);
563+
create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
553564
}
554565

555566
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)