Skip to content

Commit a6d7f6f

Browse files
committed
Configure signature for a repository
If there is `gpg` program available, Elegant Git will try to configure signing for the Git objects during execution of `acquire-repository` command. Now `read` addon supports values with spaces also. #11
1 parent 94ecdc7 commit a6d7f6f

5 files changed

+94
-5
lines changed

docs/commands.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ https://elegant-git.bees-hive.org/en/latest/configuration/
9898
usage: git elegant acquire-repository
9999
```
100100

101-
Applies the "basics", "standards", and "aliases" configurations to the current
102-
Git repository using `git config --local`.
101+
Applies the "basics", "standards", "aliases", and "signature" configurations
102+
to the current Git repository using `git config --local`.
103103

104104
To find out what will be configured, please visit
105105
https://elegant-git.bees-hive.org/en/latest/configuration/

docs/configuration.md

+14
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ significantly improve user experience.
5252

5353
The configuration is a call of `git config "alias.<command>" "elegant <command>"` (`i`) for each Elegant
5454
Git command.
55+
56+
# Signature
57+
This configuration aims to say Git how to sign commits, tags, and other objects you create. It starts after
58+
all other configurations. In the beginning, all available signing keys will be shown. Then, you need to choose
59+
the key that will be used to make signatures. If the key is provided, the configuration triggers, otherwise,
60+
it does not apply. The signing configuration consists of
61+
62+
1. setting `user.signingkey` (`l`) to a provided value
63+
2. setting `gpg.program` (`l`) to a full path of `gpg` program
64+
3. setting `commit.gpgsign` (`l`) to `true`
65+
4. setting `tag.forceSignAnnotated` (`l`) to `true`
66+
5. setting `tag.gpgSign` (`l`) to `true`
67+
68+
For now, only `gpg` is supported. If you need other tools, please [create a new feature request](https://github.com/bees-hive/elegant-git/issues/new/choose).

libexec/git-elegant-acquire-repository

+27-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ MESSAGE
1515

1616
command-description() {
1717
cat<<MESSAGE
18-
Applies the "basics", "standards", and "aliases" configurations to the current
19-
Git repository using \`git config --local\`.
18+
Applies the "basics", "standards", "aliases", and "signature" configurations
19+
to the current Git repository using \`git config --local\`.
2020
2121
To find out what will be configured, please visit
2222
${__site}/en/latest/configuration/
@@ -42,4 +42,29 @@ default() {
4242
aliases-removing --local
4343
aliases-configuration --local $(git elegant show-commands)
4444
fi
45+
type -p gpg >/dev/null 2>&1 || return 0
46+
info-box "Configuring signature..."
47+
local listkeys="gpg --list-secret-keys --keyid-format long $(git config --local user.email)"
48+
command-text ${listkeys}
49+
${listkeys}
50+
info-text "From the list of GPG keys above, copy the GPG key ID you'd like to use."
51+
info-text "It will be"
52+
info-text " 3AA5C34371567BD2"
53+
info-text "for the output like this"
54+
info-text " sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]"
55+
info-text " A330C91F8EC4BC7AECFA63E03AA5C34371567BD2"
56+
info-text " uid Hubot"
57+
info-text ""
58+
info-text "If you don't want to configure signature, just hit Enter button."
59+
question-text "Please pass a key that has to sign objects of the current repository: "
60+
read key
61+
if [[ -n ${key} ]] ; then
62+
git-verbose config --local user.signingkey ${key}
63+
git-verbose config --local gpg.program $(type -p gpg)
64+
git-verbose config --local commit.gpgsign true
65+
git-verbose config --local tag.forceSignAnnotated true
66+
git-verbose config --local tag.gpgSign true
67+
else
68+
info-text "The signature is not configured as the empty key is provided."
69+
fi
4570
}

tests/addons-read.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ read() {
3636
if [[ -f "${answers_directory}/${next_read}" ]]; then
3737
value=$(cat ${answers_directory}/${next_read})
3838
fi
39-
eval "export ${1}=${value}"
39+
eval "export ${1}=\"${value}\""
4040
echo ""
4141
}
4242

tests/git-elegant-acquire-repository.bats

+50
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load addons-common
44
load addons-read
55
load addons-fake
66
load addons-repo
7+
load addons-read
78

89
setup() {
910
repo-new
@@ -12,6 +13,7 @@ setup() {
1213
teardown() {
1314
fake-clean
1415
repo-clean
16+
read-clean
1517
}
1618

1719
@test "'acquire-repository': all configurations work as expected" {
@@ -107,3 +109,51 @@ teardown() {
107109
[[ "${lines[@]}" =~ "1 Elegant Git aliases were removed." ]]
108110
[[ ! "${lines[@]}" =~ "==>> git config --local alias.acquire-repository elegant acquire-repository" ]]
109111
}
112+
113+
@test "'acquire-repository': configures a signature if GPG key is provided" {
114+
read-answer "The User"
115+
read-answer "the@email"
116+
read-answer "someeditor"
117+
read-answer "thekey"
118+
fake-pass "gpg --list-secret-keys --keyid-format long the@email" "some dummy keys"
119+
check git-elegant acquire-repository
120+
[[ ${status} -eq 0 ]]
121+
[[ ${lines[@]} =~ "some dummy keys" ]]
122+
[[ ${lines[@]} =~ "==>> git config --local user.signingkey thekey" ]]
123+
[[ ${lines[@]} =~ "==>> git config --local gpg.program /tmp/elegant-git-fakes/gpg" ]]
124+
[[ ${lines[@]} =~ "==>> git config --local commit.gpgsign true" ]]
125+
[[ ${lines[@]} =~ "==>> git config --local tag.forceSignAnnotated true" ]]
126+
[[ ${lines[@]} =~ "==>> git config --local tag.gpgSign true" ]]
127+
}
128+
129+
@test "'acquire-repository': does not configure a signature if GPG key is not provided" {
130+
read-answer "The User"
131+
read-answer "the@email"
132+
read-answer "someeditor"
133+
read-answer ""
134+
fake-pass "gpg --list-secret-keys --keyid-format long the@email" "some keys"
135+
check git-elegant acquire-repository
136+
[[ ${status} -eq 0 ]]
137+
[[ ! ${lines[@]} =~ "==>> git config --local user.signingkey thekey" ]]
138+
[[ ! ${lines[@]} =~ "==>> git config --local gpg.program /tmp/elegant-git-fakes/gpg" ]]
139+
[[ ! ${lines[@]} =~ "==>> git config --local commit.gpgsign true" ]]
140+
[[ ! ${lines[@]} =~ "==>> git config --local tag.forceSignAnnotated true" ]]
141+
[[ ! ${lines[@]} =~ "==>> git config --local tag.gpgSign true" ]]
142+
[[ ${lines[@]} =~ "The signature is not configured as the empty key is provided." ]]
143+
}
144+
145+
@test "'acquire-repository': does not configure a signature if 'gpg' program is absent" {
146+
read-answer "The User"
147+
read-answer "the@email"
148+
read-answer "someeditor"
149+
read-answer ""
150+
check git-elegant acquire-repository
151+
[[ ${status} -eq 0 ]]
152+
[[ ! ${lines[@]} =~ "Configuring signature..." ]]
153+
[[ ! ${lines[@]} =~ "==>> git config --local user.signingkey thekey" ]]
154+
[[ ! ${lines[@]} =~ "==>> git config --local gpg.program /tmp/elegant-git-fakes/gpg" ]]
155+
[[ ! ${lines[@]} =~ "==>> git config --local commit.gpgsign true" ]]
156+
[[ ! ${lines[@]} =~ "==>> git config --local tag.forceSignAnnotated true" ]]
157+
[[ ! ${lines[@]} =~ "==>> git config --local tag.gpgSign true" ]]
158+
[[ ! ${lines[@]} =~ "The signature is not configured as the empty key is provided." ]]
159+
}

0 commit comments

Comments
 (0)