Skip to content

Commit c8f4a6d

Browse files
committed
contrib/subtree: Add -S/--gpg-sign
Allows signing the commits subtree adds when operating.
1 parent 1f46176 commit c8f4a6d

File tree

3 files changed

+145
-19
lines changed

3 files changed

+145
-19
lines changed

contrib/subtree/git-subtree.adoc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ git-subtree - Merge subtrees together and split repository into subtrees
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git subtree' [<options>] -P <prefix> add <local-commit>
13-
'git subtree' [<options>] -P <prefix> add <repository> <remote-ref>
14-
'git subtree' [<options>] -P <prefix> merge <local-commit> [<repository>]
15-
'git subtree' [<options>] -P <prefix> split [<local-commit>]
12+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] add <local-commit>
13+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] add <repository> <remote-ref>
14+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] merge <local-commit> [<repository>]
15+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] split [<local-commit>]
1616

1717
[verse]
18-
'git subtree' [<options>] -P <prefix> pull <repository> <remote-ref>
19-
'git subtree' [<options>] -P <prefix> push <repository> <refspec>
18+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] pull <repository> <remote-ref>
19+
'git subtree' [<options>] -P <prefix> [-S[<keyid>]] push <repository> <refspec>
2020

2121
DESCRIPTION
2222
-----------
@@ -149,6 +149,13 @@ OPTIONS FOR ALL COMMANDS
149149
want to manipulate. This option is mandatory
150150
for all commands.
151151

152+
-S[<keyid>]::
153+
--gpg-sign[=<keyid>]::
154+
--no-gpg-sign::
155+
GPG-sign commits. The `keyid` argument is optional and
156+
defaults to the committer identity; `--no-gpg-sign` is useful to
157+
countermand a `--gpg-sign` option given earlier on the command line.
158+
152159
OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull', 'split --rejoin', AND 'push --rejoin')
153160
-----------------------------------------------------------------------------------
154161
These options for 'add' and 'merge' may also be given to 'pull' (which

contrib/subtree/git-subtree.sh

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ then
2626
fi
2727

2828
OPTS_SPEC="\
29-
git subtree add --prefix=<prefix> <commit>
30-
git subtree add --prefix=<prefix> <repository> <ref>
31-
git subtree merge --prefix=<prefix> <commit>
32-
git subtree split --prefix=<prefix> [<commit>]
33-
git subtree pull --prefix=<prefix> <repository> <ref>
34-
git subtree push --prefix=<prefix> <repository> <refspec>
29+
git subtree add --prefix=<prefix> [-S[=<key-id>]] <commit>
30+
git subtree add --prefix=<prefix> [-S[=<key-id>]] <repository> <ref>
31+
git subtree merge --prefix=<prefix> [-S[=<key-id>]] <commit>
32+
git subtree split --prefix=<prefix> [-S[=<key-id>]] [<commit>]
33+
git subtree pull --prefix=<prefix> [-S[=<key-id>]] <repository> <ref>
34+
git subtree push --prefix=<prefix> [-S[=<key-id>]] <repository> <refspec>
3535
--
3636
h,help! show the help
3737
q,quiet! quiet
@@ -46,6 +46,7 @@ rejoin merge the new branch back into HEAD
4646
options for 'add' and 'merge' (also: 'pull', 'split --rejoin', and 'push --rejoin')
4747
squash merge subtree changes as a single commit
4848
m,message!= use the given message as the commit message for the merge commit
49+
S,gpg-sign?key-id GPG-sign commits. The keyid argument is optional and defaults to the committer identity
4950
"
5051

5152
indent=0
@@ -168,6 +169,7 @@ main () {
168169
arg_split_annotate=
169170
arg_addmerge_squash=
170171
arg_addmerge_message=
172+
arg_gpg_sign=
171173
while test $# -gt 0
172174
do
173175
opt="$1"
@@ -232,6 +234,9 @@ main () {
232234
test -n "$allow_addmerge" || die_incompatible_opt "$opt" "$arg_command"
233235
arg_addmerge_squash=
234236
;;
237+
--gpg-sign=* | --gpg-sign | --no-gpg-sign)
238+
arg_gpg_sign="$opt"
239+
;;
235240
--)
236241
break
237242
;;
@@ -264,6 +269,7 @@ main () {
264269
debug "quiet: {$arg_quiet}"
265270
debug "dir: {$dir}"
266271
debug "opts: {$*}"
272+
debug "gpg-sign: {$arg_gpg_sign}"
267273
debug
268274

269275
"cmd_$arg_command" "$@"
@@ -529,7 +535,7 @@ copy_commit () {
529535
printf "%s" "$arg_split_annotate"
530536
cat
531537
) |
532-
git commit-tree "$2" $3 # reads the rest of stdin
538+
git commit-tree $arg_gpg_sign "$2" $3 # reads the rest of stdin
533539
) || die "fatal: can't copy commit $1"
534540
}
535541

@@ -675,10 +681,10 @@ new_squash_commit () {
675681
if test -n "$old"
676682
then
677683
squash_msg "$dir" "$oldsub" "$newsub" |
678-
git commit-tree "$tree" -p "$old" || exit $?
684+
git commit-tree $arg_gpg_sign "$tree" -p "$old" || exit $?
679685
else
680686
squash_msg "$dir" "" "$newsub" |
681-
git commit-tree "$tree" || exit $?
687+
git commit-tree $arg_gpg_sign "$tree" || exit $?
682688
fi
683689
}
684690

@@ -917,11 +923,11 @@ cmd_add_commit () {
917923
then
918924
rev=$(new_squash_commit "" "" "$rev") || exit $?
919925
commit=$(add_squashed_msg "$rev" "$dir" |
920-
git commit-tree "$tree" $headp -p "$rev") || exit $?
926+
git commit-tree $arg_gpg_sign "$tree" $headp -p "$rev") || exit $?
921927
else
922928
revp=$(peel_committish "$rev") || exit $?
923929
commit=$(add_msg "$dir" $headrev "$rev" |
924-
git commit-tree "$tree" $headp -p "$revp") || exit $?
930+
git commit-tree $arg_gpg_sign "$tree" $headp -p "$revp") || exit $?
925931
fi
926932
git reset "$commit" || exit $?
927933

@@ -1072,9 +1078,9 @@ cmd_merge () {
10721078
if test -n "$arg_addmerge_message"
10731079
then
10741080
git merge --no-ff -Xsubtree="$arg_prefix" \
1075-
--message="$arg_addmerge_message" "$rev"
1081+
--message="$arg_addmerge_message" $arg_gpg_sign "$rev"
10761082
else
1077-
git merge --no-ff -Xsubtree="$arg_prefix" $rev
1083+
git merge --no-ff -Xsubtree="$arg_prefix" $arg_gpg_sign $rev
10781084
fi
10791085
}
10801086

contrib/subtree/t/t7900-subtree.sh

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and push subcommands of git subtree.
1111

1212
TEST_DIRECTORY=$(pwd)/../../../t
1313
. "$TEST_DIRECTORY"/test-lib.sh
14+
. "$TEST_DIRECTORY"/lib-gpg.sh
1415

1516
# Use our own wrapper around test-lib.sh's test_create_repo, in order
1617
# to set log.date=relative. `git subtree` parses the output of `git
@@ -1563,4 +1564,116 @@ test_expect_success 'subtree descendant check' '
15631564
)
15641565
'
15651566

1567+
test_expect_success GPG 'add subproj with GPG signing using -S flag' '
1568+
subtree_test_create_repo "$test_count" &&
1569+
subtree_test_create_repo "$test_count/sub proj" &&
1570+
test_create_commit "$test_count" main1 &&
1571+
test_create_commit "$test_count/sub proj" sub1 &&
1572+
(
1573+
cd "$test_count" &&
1574+
git fetch ./"sub proj" HEAD &&
1575+
git subtree add --prefix="sub dir" -S FETCH_HEAD &&
1576+
git verify-commit HEAD &&
1577+
test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1578+
)
1579+
'
1580+
1581+
test_expect_success GPG 'add subproj with GPG signing using --gpg-sign flag' '
1582+
subtree_test_create_repo "$test_count" &&
1583+
subtree_test_create_repo "$test_count/sub proj" &&
1584+
test_create_commit "$test_count" main1 &&
1585+
test_create_commit "$test_count/sub proj" sub1 &&
1586+
(
1587+
cd "$test_count" &&
1588+
git fetch ./"sub proj" HEAD &&
1589+
git subtree add --prefix="sub dir" --gpg-sign FETCH_HEAD &&
1590+
git verify-commit HEAD &&
1591+
test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1592+
)
1593+
'
1594+
1595+
test_expect_success GPG 'add subproj with GPG signing using specific key ID' '
1596+
subtree_test_create_repo "$test_count" &&
1597+
subtree_test_create_repo "$test_count/sub proj" &&
1598+
test_create_commit "$test_count" main1 &&
1599+
test_create_commit "$test_count/sub proj" sub1 &&
1600+
(
1601+
cd "$test_count" &&
1602+
git fetch ./"sub proj" HEAD &&
1603+
git subtree add --prefix="sub dir" -S"$GIT_COMMITTER_EMAIL" FETCH_HEAD &&
1604+
git verify-commit HEAD &&
1605+
test "$(last_commit_subject)" = "Add '\''sub dir/'\'' from commit '\''$(git rev-parse FETCH_HEAD)'\''"
1606+
)
1607+
'
1608+
1609+
test_expect_success GPG 'merge with GPG signing' '
1610+
subtree_test_create_repo "$test_count" &&
1611+
subtree_test_create_repo "$test_count/sub proj" &&
1612+
test_create_commit "$test_count" main1 &&
1613+
test_create_commit "$test_count/sub proj" sub1 &&
1614+
(
1615+
cd "$test_count" &&
1616+
git fetch ./"sub proj" HEAD &&
1617+
git subtree add --prefix="sub dir" FETCH_HEAD
1618+
) &&
1619+
test_create_commit "$test_count/sub proj" sub2 &&
1620+
(
1621+
cd "$test_count" &&
1622+
git fetch ./"sub proj" HEAD &&
1623+
git subtree merge --prefix="sub dir" -S FETCH_HEAD &&
1624+
git verify-commit HEAD
1625+
)
1626+
'
1627+
1628+
test_expect_success GPG 'split with GPG signing and --rejoin' '
1629+
subtree_test_create_repo "$test_count" &&
1630+
subtree_test_create_repo "$test_count/sub proj" &&
1631+
test_create_commit "$test_count" main1 &&
1632+
test_create_commit "$test_count/sub proj" sub1 &&
1633+
(
1634+
cd "$test_count" &&
1635+
git fetch ./"sub proj" HEAD &&
1636+
git subtree add --prefix="sub dir" FETCH_HEAD
1637+
) &&
1638+
test_create_commit "$test_count" "sub dir/main-sub1" &&
1639+
(
1640+
cd "$test_count" &&
1641+
git subtree split --prefix="sub dir" --rejoin -S &&
1642+
git verify-commit HEAD
1643+
)
1644+
'
1645+
1646+
test_expect_success GPG 'add with --squash and GPG signing' '
1647+
subtree_test_create_repo "$test_count" &&
1648+
subtree_test_create_repo "$test_count/sub proj" &&
1649+
test_create_commit "$test_count" main1 &&
1650+
test_create_commit "$test_count/sub proj" sub1 &&
1651+
(
1652+
cd "$test_count" &&
1653+
git fetch ./"sub proj" HEAD &&
1654+
git subtree add --prefix="sub dir" --squash -S FETCH_HEAD &&
1655+
git verify-commit HEAD &&
1656+
# With --squash, the commit subject should reference the squash commit (first parent of merge)
1657+
squash_commit=$(git rev-parse HEAD^2) &&
1658+
test "$(last_commit_subject)" = "Merge commit '\''$squash_commit'\'' as '\''sub dir'\''"
1659+
)
1660+
'
1661+
1662+
test_expect_success GPG 'pull with GPG signing' '
1663+
subtree_test_create_repo "$test_count" &&
1664+
subtree_test_create_repo "$test_count/sub proj" &&
1665+
test_create_commit "$test_count" main1 &&
1666+
test_create_commit "$test_count/sub proj" sub1 &&
1667+
(
1668+
cd "$test_count" &&
1669+
git subtree add --prefix="sub dir" ./"sub proj" HEAD
1670+
) &&
1671+
test_create_commit "$test_count/sub proj" sub2 &&
1672+
(
1673+
cd "$test_count" &&
1674+
git subtree pull --prefix="sub dir" -S ./"sub proj" HEAD &&
1675+
git verify-commit HEAD
1676+
)
1677+
'
1678+
15661679
test_done

0 commit comments

Comments
 (0)