Skip to content

Commit a2595f0

Browse files
committed
Merge branch 'la/tag-force-signing-annotated-tags'
"git tag" can create an annotated tag without explicitly given an "-a" (or "-s") option (i.e. when a tag message is given). A new configuration variable, tag.forceSignAnnotated, can be used to tell the command to create signed tag in such a situation. * la/tag-force-signing-annotated-tags: tag: add the option to force signing of annotated tags
2 parents 01e1d54 + 61c2fe0 commit a2595f0

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

Documentation/config.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,6 +2744,11 @@ submodule.fetchJobs::
27442744
in parallel. A value of 0 will give some reasonable default.
27452745
If unset, it defaults to 1.
27462746

2747+
tag.forceSignAnnotated::
2748+
A boolean to specify whether annotated tags created should be GPG signed.
2749+
If `--annotate` is specified on the command line, it takes
2750+
precedence over this option.
2751+
27472752
tag.sort::
27482753
This variable controls the sort ordering of tags when displayed by
27492754
linkgit:git-tag[1]. Without the "--sort=<value>" option provided, the

builtin/tag.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static const char * const git_tag_usage[] = {
2929
};
3030

3131
static unsigned int colopts;
32+
static int force_sign_annotate;
3233

3334
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
3435
{
@@ -166,6 +167,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
166167
status = git_gpg_config(var, value, cb);
167168
if (status)
168169
return status;
170+
if (!strcmp(var, "tag.forcesignannotated")) {
171+
force_sign_annotate = git_config_bool(var, value);
172+
return 0;
173+
}
174+
169175
if (starts_with(var, "column."))
170176
return git_column_config(var, value, "tag", &colopts);
171177
return git_default_config(var, value, cb);
@@ -327,7 +333,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
327333
char *cleanup_arg = NULL;
328334
int create_reflog = 0;
329335
int annotate = 0, force = 0;
330-
int cmdmode = 0;
336+
int cmdmode = 0, create_tag_object = 0;
331337
const char *msgfile = NULL, *keyid = NULL;
332338
struct msg_arg msg = { 0, STRBUF_INIT };
333339
struct ref_transaction *transaction;
@@ -385,12 +391,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
385391
opt.sign = 1;
386392
set_signing_key(keyid);
387393
}
388-
if (opt.sign)
389-
annotate = 1;
394+
create_tag_object = (opt.sign || annotate || msg.given || msgfile);
395+
390396
if (argc == 0 && !cmdmode)
391397
cmdmode = 'l';
392398

393-
if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
399+
if ((create_tag_object || force) && (cmdmode != 0))
394400
usage_with_options(git_tag_usage, options);
395401

396402
finalize_colopts(&colopts, -1);
@@ -431,7 +437,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
431437
if (msg.given || msgfile) {
432438
if (msg.given && msgfile)
433439
die(_("only one -F or -m option is allowed."));
434-
annotate = 1;
435440
if (msg.given)
436441
strbuf_addbuf(&buf, &(msg.buf));
437442
else {
@@ -474,8 +479,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
474479
else
475480
die(_("Invalid cleanup mode %s"), cleanup_arg);
476481

477-
if (annotate)
482+
if (create_tag_object) {
483+
if (force_sign_annotate && !annotate)
484+
opt.sign = 1;
478485
create_tag(object, tag, &buf, &opt, prev, object);
486+
}
479487

480488
transaction = ref_transaction_begin(&err);
481489
if (!transaction ||

t/t7004-tag.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,47 @@ test_expect_success GPG '-s implies annotated tag' '
775775
test_cmp expect actual
776776
'
777777

778+
get_tag_header forcesignannotated-implied-sign $commit commit $time >expect
779+
echo "A message" >>expect
780+
echo '-----BEGIN PGP SIGNATURE-----' >>expect
781+
test_expect_success GPG \
782+
'git tag -s implied if configured with tag.forcesignannotated' \
783+
'test_config tag.forcesignannotated true &&
784+
git tag -m "A message" forcesignannotated-implied-sign &&
785+
get_tag_msg forcesignannotated-implied-sign >actual &&
786+
test_cmp expect actual
787+
'
788+
789+
test_expect_success GPG \
790+
'lightweight with no message when configured with tag.forcesignannotated' \
791+
'test_config tag.forcesignannotated true &&
792+
git tag forcesignannotated-lightweight &&
793+
tag_exists forcesignannotated-lightweight &&
794+
test_must_fail git tag -v forcesignannotated-no-message
795+
'
796+
797+
get_tag_header forcesignannotated-annotate $commit commit $time >expect
798+
echo "A message" >>expect
799+
test_expect_success GPG \
800+
'git tag -a disable configured tag.forcesignannotated' \
801+
'test_config tag.forcesignannotated true &&
802+
git tag -a -m "A message" forcesignannotated-annotate &&
803+
get_tag_msg forcesignannotated-annotate >actual &&
804+
test_cmp expect actual &&
805+
test_must_fail git tag -v forcesignannotated-annotate
806+
'
807+
808+
get_tag_header forcesignannotated-disabled $commit commit $time >expect
809+
echo "A message" >>expect
810+
echo '-----BEGIN PGP SIGNATURE-----' >>expect
811+
test_expect_success GPG \
812+
'git tag --sign enable GPG sign' \
813+
'test_config tag.forcesignannotated false &&
814+
git tag --sign -m "A message" forcesignannotated-disabled &&
815+
get_tag_msg forcesignannotated-disabled >actual &&
816+
test_cmp expect actual
817+
'
818+
778819
test_expect_success GPG \
779820
'trying to create a signed tag with non-existing -F file should fail' '
780821
! test -f nonexistingfile &&

0 commit comments

Comments
 (0)