forked from jooaodanieel/GCommit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcommit
More file actions
executable file
·123 lines (98 loc) · 3.29 KB
/
gcommit
File metadata and controls
executable file
·123 lines (98 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
from __future__ import print_function
import argparse
import os
import sys
import tempfile
from subprocess import call, check_output, CalledProcessError
def format_developer(line):
"""
A team member must be written as
ID="Member Name <member.email@example.com>"
"""
return line.replace("\n", "").split("=")
def check_format(str_array):
return len(str_array) == 2
def read_team():
try:
team = {}
curr_dir = os.getcwd()
team_file = "{}/.gitteam".format(curr_dir)
with open(team_file) as f:
for i, line in enumerate(f):
dev = format_developer(line)
if check_format(dev):
team[dev[0]] = dev[1]
else:
raise SyntaxError("Format error .gitteam:{}".format(i + 1))
return team
except IOError:
raise IOError('Could not find .gitteam file')
def filter_team(team, initials):
filtered = {}
error = None
for mem in initials:
if mem in team:
filtered[mem] = team[mem]
else:
error = "Identifier '{id}' not found".format(id=mem)
break
msg = "Identifiers must match those in .gitteam file"
if error:
raise ValueError("{} - {}".format(msg, error))
if len(filtered) == 0:
raise ValueError(msg)
return filtered
def commit():
call(["git", "commit", "-s"])
def group_commit(team):
initial_message = b"\n"
for d in team:
line = "\nSigned-off-by: {}".format(team[d])
initial_message += line.encode()
try:
editor = check_output(['git', 'config', 'core.editor'])
editor = editor.decode().strip()
except CalledProcessError:
editor = os.environ.get('GIT_EDITOR', 'nano')
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
tf.write(initial_message)
tf.flush()
try:
print(editor, tf.name)
call([editor, tf.name])
except OSError:
print('Error: Editor {} not found'.format(editor))
print("Configure the editor by setting the 'GIT_EDITOR' env variable")
print("Or by setting 'git config --global core.editor {editor}'")
sys.exit()
tf.seek(0)
edited_message = tf.read()
call(["git", "commit", "-m", edited_message])
def main():
"""
Tries to create a multi-dev commit signature
If there's no .gitteam file, then a regular commit is done
If there's an argument-format error, then a ValueError is raised
"""
# Define expected command line arguments, and parse arguments given
parser = argparse.ArgumentParser(
description='GCommit is a git-plugin that allows commits to be signed \
by more than one person -- pair and mob programming \
reality.')
parser.add_argument(
'initials', metavar='[INITIALS]', type=str, nargs='+',
help='The intials of each developer defined in .gitteam')
args = parser.parse_args()
try:
team = read_team()
group = filter_team(team, args.initials)
group_commit(group)
except ValueError as ve:
print(ve)
except OSError:
commit()
except SyntaxError as se:
print(se)
if __name__ == "__main__":
main()