Skip to content

Commit 810273b

Browse files
committed
Merge branch 'nv/commit-gpgsign-config'
Introduce commit.gpgsign configuration variable to force every commit to be GPG signed. The variable cannot be overriden from the command line of some of the commands that create commits except for "git commit" and "git commit-tree", but I am not convinced that it is a good idea to sprinkle support for --no-gpg-sign everywhere, which in turn means that this configuration variable may not be such a good idea. * nv/commit-gpgsign-config: test the commit.gpgsign config option commit-tree: add and document --no-gpg-sign commit-tree: add the commit.gpgsign option to sign all commits
2 parents 5f95c9f + 4b8d14b commit 810273b

File tree

7 files changed

+56
-5
lines changed

7 files changed

+56
-5
lines changed

Documentation/config.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,14 @@ commit.cleanup::
992992
have to remove the help lines that begin with `#` in the commit log
993993
template yourself, if you do this).
994994

995+
commit.gpgsign::
996+
997+
A boolean to specify whether all commits should be GPG signed.
998+
Use of this option when doing operations such as rebase can
999+
result in a large number of commits being signed. It may be
1000+
convenient to use an agent to avoid typing your GPG passphrase
1001+
several times.
1002+
9951003
commit.status::
9961004
A boolean to enable/disable inclusion of status information in the
9971005
commit message template when using an editor to prepare the commit

Documentation/git-commit-tree.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ OPTIONS
5555
from the standard input.
5656

5757
-S[<keyid>]::
58+
--gpg-sign[=<keyid>]::
5859
GPG-sign commit.
5960

61+
--no-gpg-sign::
62+
Countermand `commit.gpgsign` configuration variable that is
63+
set to force each and every commit to be signed.
64+
6065

6166
Commit Information
6267
------------------

Documentation/git-commit.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ configuration variable documented in linkgit:git-config[1].
302302
--gpg-sign[=<keyid>]::
303303
GPG-sign commit.
304304

305+
--no-gpg-sign::
306+
Countermand `commit.gpgsign` configuration variable that is
307+
set to force each and every commit to be signed.
308+
305309
\--::
306310
Do not interpret any more arguments as options.
307311

builtin/commit-tree.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
static const char commit_tree_usage[] = "git commit-tree [(-p <sha1>)...] [-S[<keyid>]] [-m <message>] [-F <file>] <sha1> <changelog";
1414

15+
static const char *sign_commit;
16+
1517
static void new_parent(struct commit *parent, struct commit_list **parents_p)
1618
{
1719
unsigned char *sha1 = parent->object.sha1;
@@ -31,6 +33,10 @@ static int commit_tree_config(const char *var, const char *value, void *cb)
3133
int status = git_gpg_config(var, value, NULL);
3234
if (status)
3335
return status;
36+
if (!strcmp(var, "commit.gpgsign")) {
37+
sign_commit = git_config_bool(var, value) ? "" : NULL;
38+
return 0;
39+
}
3440
return git_default_config(var, value, cb);
3541
}
3642

@@ -41,7 +47,6 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
4147
unsigned char tree_sha1[20];
4248
unsigned char commit_sha1[20];
4349
struct strbuf buffer = STRBUF_INIT;
44-
const char *sign_commit = NULL;
4550

4651
git_config(commit_tree_config, NULL);
4752

@@ -66,6 +71,11 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
6671
continue;
6772
}
6873

74+
if (!strcmp(arg, "--no-gpg-sign")) {
75+
sign_commit = NULL;
76+
continue;
77+
}
78+
6979
if (!strcmp(arg, "-m")) {
7080
if (argc <= ++i)
7181
usage(commit_tree_usage);

builtin/commit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
14061406
}
14071407
if (!strcmp(k, "commit.cleanup"))
14081408
return git_config_string(&cleanup_arg, k, v);
1409+
if (!strcmp(k, "commit.gpgsign")) {
1410+
sign_commit = git_config_bool(k, v) ? "" : NULL;
1411+
return 0;
1412+
}
14091413

14101414
status = git_gpg_config(k, v, NULL);
14111415
if (status)

builtin/merge.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ static int git_merge_config(const char *k, const char *v, void *cb)
597597
} else if (!strcmp(k, "merge.defaulttoupstream")) {
598598
default_to_upstream = git_config_bool(k, v);
599599
return 0;
600+
} else if (!strcmp(k, "commit.gpgsign")) {
601+
sign_commit = git_config_bool(k, v) ? "" : NULL;
602+
return 0;
600603
}
601604

602605
status = fmt_merge_msg_config(k, v, cb);

t/t7510-signed-commit.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ test_description='signed commit tests'
55
. "$TEST_DIRECTORY/lib-gpg.sh"
66

77
test_expect_success GPG 'create signed commits' '
8+
test_when_finished "test_unconfig commit.gpgsign" &&
9+
810
echo 1 >file && git add file &&
911
test_tick && git commit -S -m initial &&
1012
git tag initial &&
@@ -25,12 +27,27 @@ test_expect_success GPG 'create signed commits' '
2527
git tag fourth-unsigned &&
2628
2729
test_tick && git commit --amend -S -m "fourth signed" &&
28-
git tag fourth-signed
30+
git tag fourth-signed &&
31+
32+
git config commit.gpgsign true &&
33+
echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
34+
git tag fifth-signed &&
35+
36+
git config commit.gpgsign false &&
37+
echo 6 >file && test_tick && git commit -a -m "sixth" &&
38+
git tag sixth-unsigned &&
39+
40+
git config commit.gpgsign true &&
41+
echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
42+
git tag seventh-unsigned &&
43+
44+
test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
45+
git tag seventh-signed
2946
'
3047

3148
test_expect_success GPG 'show signatures' '
3249
(
33-
for commit in initial second merge master
50+
for commit in initial second merge fourth-signed fifth-signed sixth-signed master
3451
do
3552
git show --pretty=short --show-signature $commit >actual &&
3653
grep "Good signature from" actual || exit 1
@@ -39,7 +56,7 @@ test_expect_success GPG 'show signatures' '
3956
done
4057
) &&
4158
(
42-
for commit in merge^2 fourth-unsigned
59+
for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
4360
do
4461
git show --pretty=short --show-signature $commit >actual &&
4562
grep "Good signature from" actual && exit 1
@@ -52,7 +69,7 @@ test_expect_success GPG 'show signatures' '
5269
test_expect_success GPG 'detect fudged signature' '
5370
git cat-file commit master >raw &&
5471
55-
sed -e "s/fourth signed/4th forged/" raw >forged1 &&
72+
sed -e "s/seventh/7th forged/" raw >forged1 &&
5673
git hash-object -w -t commit forged1 >forged1.commit &&
5774
git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
5875
grep "BAD signature from" actual1 &&

0 commit comments

Comments
 (0)