Skip to content

Commit 25c928c

Browse files
committed
[#2] Clean code: extract functions
1 parent 46bfbf2 commit 25c928c

File tree

1 file changed

+77
-26
lines changed

1 file changed

+77
-26
lines changed

src/03_sign_commit_using_the_gitpython_package.py

+77-26
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,64 @@ def git_init(temp_dir):
2121
return repo
2222

2323

24-
def execute_console_command(command):
25-
print(f'Executing command: {command}')
24+
def execute_console_command(command, show_ouput=False):
2625
output = os.popen(command).read()
27-
print(output)
26+
if (show_ouput):
27+
print(output)
2828

2929

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
3276
repo = git_init(temp_dir)
3377

34-
# Create file
78+
# Git config
79+
set_git_glogal_user_config(repo)
80+
81+
# Create new file to commit
3582
filename = "README_SIGNED.md"
3683
file_path = temp_dir + '/' + filename
3784
print(f'Creating file: {file_path}')
@@ -42,33 +89,26 @@ def main(gpg_private_key, passphrase):
4289
index = repo.index
4390
index.add([file_path])
4491

45-
# Needed for commit with signature:
92+
# Write index. Needed for commit with signature:
4693
# https://github.com/gitpython-developers/GitPython/issues/580#issuecomment-282474086
4794
index.write()
4895

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"')
5499

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()
58100

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)
62103

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)
66104

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)
69110

70-
# Print commit info
71-
execute_console_command(f'cd {temp_dir} && git log --show-signature')
111+
print_commit_info(temp_dir)
72112

73113

74114
if __name__ == "__main__":
@@ -78,4 +118,15 @@ def main(gpg_private_key, passphrase):
78118
gpg_private_key = os.getenv('GPG_PRIVATE_KEY').replace(r'\n', '\n')
79119
passphrase = os.environ.get('PASSPHRASE')
80120

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

Comments
 (0)