55import re
66import shutil
77import tempfile
8- from typing import Any , Dict , List , Optional , Pattern , Tuple
8+ from typing import Any , Dict , List , Optional , Pattern , Tuple , cast
99
1010from async_lru import alru_cache as lru_cache
1111
@@ -151,20 +151,34 @@ async def get_editor() -> str:
151151 ret = os .environ .get ("GIT_EDITOR" , os .environ .get ("EDITOR" , "nano" ))
152152 return ret
153153
154+ async def get_gpg_sign () -> bool :
155+ # commit-tree (plumbing) ignores commit.gpgSign, so read it and pass -S ourselves.
156+ val = await git_ctx .git_stdout (
157+ "config" , "--type=bool" , "--get" , "commit.gpgSign" , raiseonerror = False
158+ )
159+ return val .strip () == "true"
160+
161+ # asyncio.gather is only precisely typed up to 6 awaitables; beyond that
162+ # mypy infers each result as object, so annotate the tuple ourselves.
154163 (
155164 repo_root ,
156165 git_dir ,
157166 actual_version ,
158167 email ,
159168 editor ,
160169 main_exists ,
161- ) = await asyncio .gather (
162- git_ctx .git_stdout ("rev-parse" , "--show-toplevel" ),
163- git_ctx .git_stdout ("rev-parse" , "--path-format=absolute" , "--git-dir" ),
164- git_ctx .git_stdout ("--version" ),
165- get_email (),
166- get_editor (),
167- git_ctx .is_branch_or_commit (f"{ remote_name } /{ main_branch } " ),
170+ gpg_sign ,
171+ ) = cast (
172+ Tuple [str , str , str , str , str , bool , bool ],
173+ await asyncio .gather (
174+ git_ctx .git_stdout ("rev-parse" , "--show-toplevel" ),
175+ git_ctx .git_stdout ("rev-parse" , "--path-format=absolute" , "--git-dir" ),
176+ git_ctx .git_stdout ("--version" ),
177+ get_email (),
178+ get_editor (),
179+ git_ctx .is_branch_or_commit (f"{ remote_name } /{ main_branch } " ),
180+ get_gpg_sign (),
181+ ),
168182 )
169183
170184 if git_version :
@@ -184,6 +198,7 @@ async def get_editor() -> str:
184198 git_ctx .email = email .lower ()
185199 git_ctx .author = git_ctx .email .split ("@" )[0 ]
186200 git_ctx .editor = editor
201+ git_ctx .gpg_sign = gpg_sign
187202 if not main_exists :
188203 if main_branch in COMMON_MAIN_BRANCHES :
189204 git_ctx .main_branch = COMMON_MAIN_BRANCHES [1 - COMMON_MAIN_BRANCHES .index (main_branch )]
@@ -225,6 +240,9 @@ class Git:
225240 author : str
226241 editor : str
227242
243+ # Whether to GPG/SSH sign commits revup creates, from git config commit.gpgSign
244+ gpg_sign : bool
245+
228246 def __init__ (
229247 self ,
230248 sh : shell .Shell ,
@@ -239,6 +257,7 @@ def __init__(
239257 self .remote_name = remote_name
240258 self .keep_temp = keep_temp
241259 self .main_branch = main_branch
260+ self .gpg_sign = False
242261 self .base_branch_globs = base_branch_globs .strip ().splitlines ()
243262 self .temp_dir = tempfile .TemporaryDirectory ( # pylint: disable=consider-using-with
244263 prefix = "revup_"
@@ -560,6 +579,8 @@ async def commit_tree(self, commit_info: CommitHeader) -> GitCommitHash:
560579 "-m" ,
561580 commit_info .commit_msg ,
562581 ]
582+ if self .gpg_sign :
583+ commit_tree_args .append ("-S" )
563584 for p in commit_info .parents :
564585 commit_tree_args .extend (["-p" , p ])
565586 ret = await self .git_stdout (* commit_tree_args , env = git_env )
0 commit comments