-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (72 loc) · 2.6 KB
/
main.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
import sys
import os
import json
from file_helper import read_json
CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
initials = {}
class TestableCLI:
def out(self, message):
print(message)
def run(self, command):
os.system(command)
class Arguments:
def __init__(self, args):
ignore_file_path = args[0]
self.to_push = args[1] == "push"
self.commit_message = args[2]
self.coauthors = (Coauthors.create(args[3]) if len(args) >= 4 else Coauthors.create())
def parse(self):
if self.coauthors.are_not_valid():
return InvalidCoauthorsCommand()
if self.to_push:
return GitPushCommand(GitCommitCommand(self.commit_message, self.coauthors))
return GitCommitCommand(self.commit_message, self.coauthors)
class Coauthors:
@staticmethod
def create(coauthors_arg=None):
if not coauthors_arg:
return NoCoauthors()
coauthor_initials = coauthors_arg.replace('co:', '').split(',')
if not set(coauthor_initials).issubset(set(initials.keys())):
return InvalidCoauthors()
return Coauthors(coauthor_initials)
def __init__(self, coauthor_initials):
self.coauthor_initials = coauthor_initials
def are_not_valid(self):
return False
def string(self):
emails = [initials[key] for key in self.coauthor_initials]
return '\n\n' + '\n'.join(map('Co-authored-by: {0}'.format, emails))
class NoCoauthors:
def are_not_valid(self):
return False
def string(self):
return ''
class InvalidCoauthors:
def are_not_valid(self):
return True
def string(self):
return ''
def process_git_command(args, cli, filename=CURRENT_DIRECTORY + '/initials.json'):
global initials
initials = read_json(filename)
git_command = Arguments(args).parse()
git_command.run(cli)
class InvalidCoauthorsCommand:
def run(self, cli):
cli.out('Invalid co-author initials. Registered initials: ' + ', '.join(initials.keys()))
class GitCommitCommand:
def __init__(self, commit_message, coauthors):
self.commit_message = commit_message
self.coauthors = coauthors
def run(self, cli):
cli.run('git add .')
cli.run('git commit -m "' + self.commit_message + self.coauthors.string() + '"')
class GitPushCommand:
def __init__(self, git_commit):
self.gitCommit = git_commit
def run(self, cli):
self.gitCommit.run(cli)
cli.run('git push origin HEAD')
if __name__ == '__main__':
process_git_command(sys.argv, TestableCLI())