tag: add the option to force signing of annotated tags

The `tag.forcesignannotated` configuration variable makes "git tag" that would implicitly create an annotated tag to instead create a signed tag. For example $ git tag -m "This is a message" tag-with-message $ git tag -F message-file tag-with-message would create a signed tag if the configuration variable is in effect. To override this from the command line, the user can explicitly ask for an annotated tag, like so: $ git tag -a -m "This is a message" tag-with-message $ git tag -a -F message-file tag-with-message Creation of a light-weight tag, i.e. $ git tag lightweight is not affected. Signed-off-by: Laurent Arnoud <laurent@spkdev.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Laurent Arnoud committed Mar 22, 2016 at 21:41 UTC 61c2fe0c2984a7785e2ac803ce33e23663295b24
3 files changed +60 -6
Documentation/config.txt
+5
@@ -2729,6 +2729,11 @@ submodule.<name>.ignore::
2729 "--ignore-submodules" option. The 'git submodule' commands are not
2730 affected by this setting.
2731
2732 +tag.forceSignAnnotated::
2733 + A boolean to specify whether annotated tags created should be GPG signed.
2734 + If `--annotate` is specified on the command line, it takes
2735 + precedence over this option.
2736 +
2737 tag.sort::
2738 This variable controls the sort ordering of tags when displayed by
2739 linkgit:git-tag[1]. Without the "--sort=<value>" option provided, the
builtin/tag.c
+14 -6
@@ -29,6 +29,7 @@ static const char * const git_tag_usage[] = {
29 };
30
31 static unsigned int colopts;
32 +static int force_sign_annotate;
33
34 static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
35 {
@@ -166,6 +167,11 @@ static int git_tag_config(const char *var, const char *value, void *cb)
167 status = git_gpg_config(var, value, cb);
168 if (status)
169 return status;
170 + if (!strcmp(var, "tag.forcesignannotated")) {
171 + force_sign_annotate = git_config_bool(var, value);
172 + return 0;
173 + }
174 +
175 if (starts_with(var, "column."))
176 return git_column_config(var, value, "tag", &colopts);
177 return git_default_config(var, value, cb);
@@ -327,7 +333,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
333 char *cleanup_arg = NULL;
334 int create_reflog = 0;
335 int annotate = 0, force = 0;
330 - int cmdmode = 0;
336 + int cmdmode = 0, create_tag_object = 0;
337 const char *msgfile = NULL, *keyid = NULL;
338 struct msg_arg msg = { 0, STRBUF_INIT };
339 struct ref_transaction *transaction;
@@ -385,12 +391,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
391 opt.sign = 1;
392 set_signing_key(keyid);
393 }
388 - if (opt.sign)
389 - annotate = 1;
394 + create_tag_object = (opt.sign || annotate || msg.given || msgfile);
395 +
396 if (argc == 0 && !cmdmode)
397 cmdmode = 'l';
398
393 - if ((annotate || msg.given || msgfile || force) && (cmdmode != 0))
399 + if ((create_tag_object || force) && (cmdmode != 0))
400 usage_with_options(git_tag_usage, options);
401
402 finalize_colopts(&colopts, -1);
@@ -431,7 +437,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
437 if (msg.given || msgfile) {
438 if (msg.given && msgfile)
439 die(_("only one -F or -m option is allowed."));
434 - annotate = 1;
440 if (msg.given)
441 strbuf_addbuf(&buf, &(msg.buf));
442 else {
@@ -474,8 +479,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
479 else
480 die(_("Invalid cleanup mode %s"), cleanup_arg);
481
477 - if (annotate)
482 + if (create_tag_object) {
483 + if (force_sign_annotate && !annotate)
484 + opt.sign = 1;
485 create_tag(object, tag, &buf, &opt, prev, object);
486 + }
487
488 transaction = ref_transaction_begin(&err);
489 if (!transaction ||
t/t7004-tag.sh
+41
@@ -775,6 +775,47 @@ test_expect_success GPG '-s implies annotated tag' '
775 test_cmp expect actual
776 '
777
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 +
819 test_expect_success GPG \
820 'trying to create a signed tag with non-existing -F file should fail' '
821 ! test -f nonexistingfile &&