@@ -21,17 +21,64 @@ def git_init(temp_dir):
21
21
return repo
22
22
23
23
24
- def execute_console_command (command ):
25
- print (f'Executing command: { command } ' )
24
+ def execute_console_command (command , show_ouput = False ):
26
25
output = os .popen (command ).read ()
27
- print (output )
26
+ if (show_ouput ):
27
+ print (output )
28
28
29
29
30
- def main (gpg_private_key , passphrase ):
31
- temp_dir = create_temp_dir ()
30
+ def import_gpg_private_key (gpg_private_key , passphrase ):
31
+ """
32
+ Import PGP key into the the local keyring
33
+ """
34
+ gpg = gnupg .GPG (gnupghome = '/root/.gnupg' , verbose = False , use_agent = True )
35
+ gpg .import_keys (gpg_private_key , passphrase = passphrase )
36
+
37
+
38
+ def preset_passphrase (keygrip , passphrase ):
39
+ """
40
+ Preset passphrase using gpg-connect-agent in order to avoid prompting the user for it.
41
+ """
42
+ hex_passphrase = passphrase .encode ('utf-8' ).hex ().upper ()
43
+ preset_passphrase_command = f'gpg-connect-agent \' PRESET_PASSPHRASE { keygrip } -1 { hex_passphrase } \' /bye'
44
+ execute_console_command (preset_passphrase_command )
45
+
46
+
47
+ def set_git_glogal_user_config (repo ):
48
+ """
49
+ This configuration prevents from having this git error:
50
+
51
+ stderr: 'Committer identity unknown
52
+
53
+ *** Please tell me who you are.
54
+
55
+ Run
56
+
57
+ git config --global user.email "you@example.com"
58
+ git config --global user.name "Your Name"
59
+
60
+ to set your account's default identity.
61
+ Omit --global to set the identity only in this repository.
62
+
63
+ fatal: unable to auto-detect email address (got 'root@b37fb619ac5a.(none)')'
64
+ """
65
+ repo .config_writer ().set_value ("user" , "name" , "A committer" ).release ()
66
+ repo .config_writer ().set_value ("user" , "email" , "committer@example.com" ).release ()
67
+
68
+
69
+ def set_gpg_configuration (gpg_private_key , passphrase , keygrip ):
70
+ import_gpg_private_key (gpg_private_key , passphrase )
71
+ preset_passphrase (keygrip , passphrase )
72
+
73
+
74
+ def create_signed_commit (temp_dir , signingkey ):
75
+ # Initialize the Git repo
32
76
repo = git_init (temp_dir )
33
77
34
- # Create file
78
+ # Git config
79
+ set_git_glogal_user_config (repo )
80
+
81
+ # Create new file to commit
35
82
filename = "README_SIGNED.md"
36
83
file_path = temp_dir + '/' + filename
37
84
print (f'Creating file: { file_path } ' )
@@ -42,33 +89,26 @@ def main(gpg_private_key, passphrase):
42
89
index = repo .index
43
90
index .add ([file_path ])
44
91
45
- # Needed for commit with signature:
92
+ # Write index. Needed for commit with signature:
46
93
# https://github.com/gitpython-developers/GitPython/issues/580#issuecomment-282474086
47
94
index .write ()
48
95
49
- # TODO: get from console, gnupg package or env var
50
- signingkey = '27304EDD6079B81C'
51
- # Of master key 88966A5B8C01BD04F3DA440427304EDD6079B81C
52
- keygrip = '449972AC9FF11BCABEED8A7AE834C4349CC4DBFF'
53
- hex_passphrase = passphrase .encode ('utf-8' ).hex ().upper ()
96
+ # Signed commit
97
+ repo .git .commit (
98
+ '-S' , f'--gpg-sign={ signingkey } ' , '-m' , '"my signed commit"' )
54
99
55
- # Set global Git user
56
- repo .config_writer ().set_value ("user" , "name" , "A committer" ).release ()
57
- repo .config_writer ().set_value ("user" , "email" , "committer@example.com" ).release ()
58
100
59
- # Import private key
60
- gpg = gnupg .GPG (gnupghome = '/root/.gnupg' , verbose = False , use_agent = True )
61
- gpg .import_keys (gpg_private_key , passphrase = passphrase )
101
+ def print_commit_info (temp_dir ):
102
+ execute_console_command (f'cd { temp_dir } && git log --show-signature' , True )
62
103
63
- # Preset passphrase using gpg-connect-agent:
64
- preset_passphrase_command = f'gpg-connect-agent \' PRESET_PASSPHRASE { keygrip } -1 { hex_passphrase } \' /bye'
65
- execute_console_command (preset_passphrase_command )
66
104
67
- repo .git .commit ('-S' , f'--gpg-sign={ signingkey } ' , '-m' , '"my signed commit"' ,
68
- author = '"A committer <committer@example.com>"' )
105
+ def main (temp_dir , gpg_private_key , passphrase , signingkey , keygrip ):
106
+
107
+ set_gpg_configuration (gpg_private_key , passphrase , keygrip )
108
+
109
+ create_signed_commit (temp_dir , signingkey )
69
110
70
- # Print commit info
71
- execute_console_command (f'cd { temp_dir } && git log --show-signature' )
111
+ print_commit_info (temp_dir )
72
112
73
113
74
114
if __name__ == "__main__" :
@@ -78,4 +118,15 @@ def main(gpg_private_key, passphrase):
78
118
gpg_private_key = os .getenv ('GPG_PRIVATE_KEY' ).replace (r'\n' , '\n ' )
79
119
passphrase = os .environ .get ('PASSPHRASE' )
80
120
81
- main (gpg_private_key , passphrase )
121
+ # TODO: get signingkey and keygrip from private key
122
+
123
+ signingkey = '27304EDD6079B81C'
124
+
125
+ # Of previous signingkey [master] key 88966A5B8C01BD04F3DA440427304EDD6079B81C
126
+ # It has to be the keygrip of the key you are using to sign commits.
127
+ keygrip = '449972AC9FF11BCABEED8A7AE834C4349CC4DBFF'
128
+
129
+ # Create temp dir for the example
130
+ temp_dir = create_temp_dir ()
131
+
132
+ main (temp_dir , gpg_private_key , passphrase , signingkey , keygrip )
0 commit comments