-
Notifications
You must be signed in to change notification settings - Fork 0
/
gco.py
146 lines (98 loc) · 3.46 KB
/
gco.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python
import argparse
import base64
import os
import subprocess
import tempfile
GIT_CONFIG_KEY = 'coauthor.authors'
class Coauthor(object):
@staticmethod
def parse(line):
author_parts = line.split(':')
return Coauthor(author_parts[0], author_parts[1])
def __init__(self, name, email):
self.name = name
self.email = email
def str(self):
return '{} <{}>'.format(self.name, self.email)
def commit(args):
message = args.message or ''
coauthors = _read_authors()
coauthor_stanza = ''
if len(coauthors) > 0:
coauthor_stanza = '\n\n\n'
coauthor_stanza += '\n'.join(['Co-authored-by: {}'.format(coauthor.str()) for coauthor in coauthors])
path = None
if args.message:
with _tempfile() as tmpfile:
path = tmpfile.name
tmpfile.write(message)
tmpfile.write(coauthor_stanza)
sh('git commit -F {}'.format(path).split(' '))
elif len(coauthors) > 0:
with _tempfile() as tmpfile:
path = tmpfile.name
tmpfile.write(coauthor_stanza)
sh('git commit -t {}'.format(path).split(' '))
else:
sh('git commit'.split(' '))
if path:
os.unlink(path)
def show():
print '\n'.join([ca.str() for ca in _read_authors()])
def clear():
sh('git config --unset-all {}'.format(GIT_CONFIG_KEY).split(' '))
def add():
print 'Add your co-authors (press C-d to stop)'
coauthors = []
try:
while True:
name = raw_input("Co-author's name: ")
email = raw_input("{}'s email: ".format(name))
coauthors.append(Coauthor(name, email))
except EOFError:
pass
if len(coauthors) > 0:
coauthor_serialized = ','.join(['{}:{}'.format(coauthor.name, coauthor.email) for coauthor in coauthors])
sh('git config --replace-all {} {}'.format(GIT_CONFIG_KEY, base64.b64encode(coauthor_serialized)).split(' '))
def shpipe(args, stdout=None, wd=None, stdin=None):
cmd = _sp(args, stdout=stdout, stdin=stdin, wd=wd)
cmd.wait()
def sh(args, wd=None, stdin=None):
cmd = _sp(args, stdout=subprocess.PIPE, wd=wd, stdin=stdin)
return cmd.communicate()[0].strip()
def _sp(args, stdout=None, stdin=None, wd=None):
if not wd:
wd = os.getcwd()
return subprocess.Popen(args, cwd=wd, stdout=stdout, stdin=stdin)
def _read_authors():
try:
config = sh('git config {}'.format(GIT_CONFIG_KEY).split(' '))
authors = base64.b64decode(config)
if len(authors) == 0:
return []
return [Coauthor.parse(line) for line in authors.split(',')]
except subprocess.CalledProcessError:
return []
def _tempfile():
fd, path = tempfile.mkstemp()
return open(path, 'w')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand')
commit_parser = subparsers.add_parser('commit')
commit_parser.add_argument('-m', '--message', help='commit message')
subparsers.add_parser('clear')
subparsers.add_parser('add')
subparsers.add_parser('show')
args = parser.parse_args()
if args.subcommand == 'commit':
commit(args)
elif args.subcommand == 'clear':
clear()
elif args.subcommand == 'show':
show()
elif args.subcommand == 'add':
add()
else:
print 'command not supported: {}. Try again with --help for options'.format(args.subcommand)