git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] contrib/subtree: Add -S/--gpg-sign option

From
Patrik Weiskircher <patrik@pspdfkit.com>
Date
May 28, 2025, 13:01 UTC
Message-ID
<20250528130116.21534-2-patrik@pspdfkit.com>
In-Reply-To
<20250528130116.21534-1-patrik@pspdfkit.com>
If set, forwards the options to commit-tree and merge.
Signed-off-by: Patrik Weiskircher <patrik@pspdfkit.com>
---
 contrib/subtree/git-subtree.adoc | 20 +++++++++----
 contrib/subtree/git-subtree.sh   | 50 +++++++++++++++++++++++---------
 2 files changed, 51 insertions(+), 19 deletions(-)
diff --git a/contrib/subtree/git-subtree.adoc b/contrib/subtree/git-subtree.adoc
index 004abf415b..f550be1a86 100644
--- a/contrib/subtree/git-subtree.adoc
+++ b/contrib/subtree/git-subtree.adoc
@@ -9,14 +9,14 @@ git-subtree - Merge subtrees together and split repository into subtrees
 SYNOPSIS
 --------
 [verse]
-'git subtree' [<options>] -P <prefix> add <local-commit>
-'git subtree' [<options>] -P <prefix> add <repository> <remote-ref>
-'git subtree' [<options>] -P <prefix> merge <local-commit> [<repository>]
-'git subtree' [<options>] -P <prefix> split [<local-commit>]
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] add <local-commit>
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] add <repository> <remote-ref>
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] merge <local-commit> [<repository>]
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] split [<local-commit>]
 
 [verse]
-'git subtree' [<options>] -P <prefix> pull <repository> <remote-ref>
-'git subtree' [<options>] -P <prefix> push <repository> <refspec>
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] pull <repository> <remote-ref>
+'git subtree' [<options>] -P <prefix> [-S[<keyid>]] push <repository> <refspec>
 
 DESCRIPTION
 -----------
@@ -149,6 +149,14 @@ OPTIONS FOR ALL COMMANDS
 	want to manipulate.  This option is mandatory
 	for all commands.
 
+-S[<keyid>]::
+--gpg-sign[=<keyid>]::
+--no-gpg-sign::
+	GPG-sign commits. The `keyid` argument is optional and
+	defaults to the committer identity; if specified, it must be
+	stuck to the option without a space. `--no-gpg-sign` is useful to
+	countermand a `--gpg-sign` option given earlier on the command line.
+
 OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull', 'split --rejoin', AND 'push --rejoin')
 -----------------------------------------------------------------------------------
 These options for 'add' and 'merge' may also be given to 'pull' (which
diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
index 15ae86db1b..b98a708c10 100755
--- a/contrib/subtree/git-subtree.sh
+++ b/contrib/subtree/git-subtree.sh
@@ -26,12 +26,12 @@ then
 fi
 
 OPTS_SPEC="\
-git subtree add   --prefix=<prefix> <commit>
-git subtree add   --prefix=<prefix> <repository> <ref>
-git subtree merge --prefix=<prefix> <commit>
-git subtree split --prefix=<prefix> [<commit>]
-git subtree pull  --prefix=<prefix> <repository> <ref>
-git subtree push  --prefix=<prefix> <repository> <refspec>
+git subtree add   --prefix=<prefix> [-S[<keyid>]] <commit>
+git subtree add   --prefix=<prefix> [-S[<keyid>]] <repository> <ref>
+git subtree merge --prefix=<prefix> [-S[<keyid>]] <commit>
+git subtree split --prefix=<prefix> [-S[<keyid>]] [<commit>]
+git subtree pull  --prefix=<prefix> [-S[<keyid>]] <repository> <ref>
+git subtree push  --prefix=<prefix> [-S[<keyid>]] <repository> <refspec>
 --
 h,help!       show the help
 q,quiet!      quiet
@@ -46,6 +46,7 @@ rejoin        merge the new branch back into HEAD
  options for 'add' and 'merge' (also: 'pull', 'split --rejoin', and 'push --rejoin')
 squash        merge subtree changes as a single commit
 m,message!=   use the given message as the commit message for the merge commit
+S,gpg-sign?key-id   GPG-sign commits. The keyid argument is optional and defaults to the committer identity; if specified, it must be stuck to the option without a space.
 "
 
 indent=0
@@ -102,6 +103,20 @@ assert () {
 	fi
 }
 
+# Usage: gpg_sign_opt
+# Returns the GPG signing option for git commit-tree
+gpg_sign_opt () {
+	if test "${arg_gpg_sign+set}" = "set"
+	then
+		if test -n "$arg_gpg_sign"
+		then
+			printf " -S%s" "$arg_gpg_sign"
+		else
+			printf " -S"
+		fi
+	fi
+}
+
 # Usage: die_incompatible_opt OPTION COMMAND
 die_incompatible_opt () {
 	assert test "$#" = 2
@@ -240,6 +255,15 @@ main () {
 			test -n "$allow_addmerge" || die_incompatible_opt "$opt" "$arg_command"
 			arg_addmerge_squash=
 			;;
+		-S)
+			if test $# -gt 0 && test "${1#-}" = "$1"
+			then
+				arg_gpg_sign="$1"
+				shift
+			else
+				arg_gpg_sign=""
+			fi
+			;;
 		--)
 			break
 			;;
@@ -537,7 +561,7 @@ copy_commit () {
 			printf "%s" "$arg_split_annotate"
 			cat
 		) |
-		git commit-tree "$2" $3  # reads the rest of stdin
+		git commit-tree "$2" $(gpg_sign_opt) $3  # reads the rest of stdin
 	) || die "fatal: can't copy commit $1"
 }
 
@@ -683,10 +707,10 @@ new_squash_commit () {
 	if test -n "$old"
 	then
 		squash_msg "$dir" "$oldsub" "$newsub" |
-		git commit-tree "$tree" -p "$old" || exit $?
+		git commit-tree "$tree" $(gpg_sign_opt) -p "$old" || exit $?
 	else
 		squash_msg "$dir" "" "$newsub" |
-		git commit-tree "$tree" || exit $?
+		git commit-tree "$tree" $(gpg_sign_opt) || exit $?
 	fi
 }
 
@@ -925,11 +949,11 @@ cmd_add_commit () {
 	then
 		rev=$(new_squash_commit "" "" "$rev") || exit $?
 		commit=$(add_squashed_msg "$rev" "$dir" |
-			git commit-tree "$tree" $headp -p "$rev") || exit $?
+			git commit-tree "$tree" $(gpg_sign_opt) $headp -p "$rev") || exit $?
 	else
 		revp=$(peel_committish "$rev") || exit $?
 		commit=$(add_msg "$dir" $headrev "$rev" |
-			git commit-tree "$tree" $headp -p "$revp") || exit $?
+			git commit-tree "$tree" $(gpg_sign_opt) $headp -p "$revp") || exit $?
 	fi
 	git reset "$commit" || exit $?
 
@@ -1080,9 +1104,9 @@ cmd_merge () {
 	if test -n "$arg_addmerge_message"
 	then
 		git merge --no-ff -Xsubtree="$arg_prefix" \
-			--message="$arg_addmerge_message" "$rev"
+			--message="$arg_addmerge_message" $(gpg_sign_opt) "$rev"
 	else
-		git merge --no-ff -Xsubtree="$arg_prefix" $rev
+		git merge --no-ff -Xsubtree="$arg_prefix" $(gpg_sign_opt) "$rev"
 	fi
 }
 
-- 
2.49.0
Previous: Patrik WeiskircherNext: Junio C Hamano
Message 2 of 8 in “contrib/subtree: Add -S/--gpg-sign option”
  1. 0/2 contrib/subtree: Add -S/--gpg-sign optionPatrik Weiskircher, May 28, 2025
  2. 1/2 contrib/subtree: Add -S/--gpg-sign optionPatrik Weiskircher, May 28, 2025
  3. Junio C HamanoMay 29, 2025
  4. 2/2 contrib/subtree: Add tests for -S/--gpg-signPatrik Weiskircher, May 28, 2025
  5. Junio C HamanoMay 29, 2025
  6. Patrik WeiskircherMay 30, 2025
  7. Junio C HamanoMay 30, 2025
  8. Patrik WeiskircherJun 2, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.