-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
243 lines (210 loc) · 8.56 KB
/
commands.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from colors import Colors
from pathlib import Path
from auto import find_identity
import git
import configparser
identities_file_path = str(Path.home() / '.git_identities')
command_name = None
def set_command_name(name):
global command_name
command_name = name
# subparsers 'command' functions
def list_identities(args):
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
identity_counter = 0
for identity in identities.sections():
if identity[:9] == 'identity.':
identity_counter += 1
identity_obj = identities[identity]
line = ' '
git_config_user = git.config_get('user', 'name')
git_config_email = git.config_get('user', 'email')
if identity_obj['name'] == git_config_user[0] and \
identity_obj['email'] == git_config_email[0]:
line = '* '
if git_config_user[1] or git_config_email[1]:
line += Colors.yellow
else:
line += Colors.green
line += identity[9:] + Colors.default
if args.verbose:
line += " (%s <%s>)" % (identity_obj['name'], identity_obj['email'])
print(line)
if identity_counter == 0:
print(Colors.red + 'There are no saved identities.' + Colors.default)
print('\nUse\n %s add ...\nto add a new identity.' % command_name)
return 0
def show_identity(args):
git_config_user = git.config_get('user', 'name')
git_config_email = git.config_get('user', 'email')
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
identity = args.identity
if identity is None:
for identity_entry in identities.sections():
identity_entry_obj = identities[identity_entry]
if identity_entry_obj['name'] == git_config_user[0] and \
identity_entry_obj['email'] == git_config_email[0]:
identity = identity_entry[9:]
break
if identity is None:
print('[?] %s <%s>' % (git_config_user[0], git_config_email[0]))
print(Colors.red +
'The identity in the current context is not known to %s.' % command_name +
Colors.default)
print('You can add the current identity with\n %s add --current' % command_name)
return
identity_obj = identities['identity.' + identity]
print('[%s] %s <%s>' % (identity, identity_obj['name'], identity_obj['email']))
print('\nKeywords:')
i = 1
while True:
key = 'keyword' + str(i)
if key not in identity_obj:
break
print(' - ' + identity_obj[key])
i += 1
if i == 1:
print(' (none)')
print('\nPaths:')
i = 1
while True:
key = 'path' + str(i)
if key not in identity_obj:
break
print(' - ' + identity_obj[key])
i += 1
if i == 1:
print(' (none)')
if identity_obj['name'] == git_config_user[0] and \
identity_obj['email'] == git_config_email[0]:
line = '\nThis is the ' + Colors.green + 'default' + Colors.default + ' identity in the current context.'
if git_config_user[1] or git_config_email[1]:
line += '\nIt is set ' + Colors.yellow + 'locally' + Colors.default + ' in the current repository.'
else:
line += '\nIt is set globally.'
print(line)
return 0
def add_identity(args):
if not args.identity.isidentifier():
print(Colors.red + 'The specified identity ID is not a valid identifier.' + Colors.default)
return 1
name = None
email = None
if args.current:
git_config_user = git.config_get('user', 'name')
git_config_email = git.config_get('user', 'email')
name = git_config_user[0]
email = git_config_email[0]
else:
name = args.name
email = args.email
if name is None or email is None:
print(Colors.red + 'error: the following arguments are required: name, email or -c' + Colors.default)
return 2
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
if not args.force and identities.has_section('identity.' + args.identity):
print(Colors.red + 'The specified identity ID already exists.' + Colors.default)
print('To replace the existing identity, specify --force.')
return 3
identities['identity.' + args.identity] = {
'name': name,
'email': email
}
with open(identities_file_path, 'w') as identities_file:
identities.write(identities_file)
return 0
def remove_identity(args):
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
if not identities.has_section('identity.' + args.identity):
print(Colors.red + "The specified identity ID doesn't exist." + Colors.default)
return 1
identities.remove_section('identity.' + args.identity)
with open(identities_file_path, 'w') as identities_file:
identities.write(identities_file)
return 0
def update_identity(args):
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
identity = 'identity.' + args.identity
if not identities.has_section(identity):
print(Colors.red + "The specified identity ID doesn't exist." + Colors.default)
return 1
if args.name is not None:
identities.set(identity, 'name', args.name)
if args.email is not None:
identities.set(identity, 'email', args.email)
if args.keywords is not None:
i = 1
for keyword in args.keywords:
identities.set(identity, 'keyword' + str(i), keyword)
i += 1
while identities.has_option(identity, 'keyword' + str(i)):
identities.remove_option(identity, 'keyword' + str(i))
i += 1
if args.paths is not None:
i = 1
for path in args.paths:
identities.set(identity, 'path' + str(i), path)
i += 1
while identities.has_option(identity, 'path' + str(i)):
identities.remove_option(identity, 'path' + str(i))
i += 1
with open(identities_file_path, 'w') as identities_file:
identities.write(identities_file)
return 0
def apply_identity(args):
identities = configparser.ConfigParser()
identities.read(identities_file_path, encoding='utf_8')
if args.auto:
result = find_identity(Path.cwd(), identities)
if result.identity_key is None:
print(Colors.red + "No automatic identity was found." + Colors.default)
return 1
line = 'Selected ' + Colors.bold + result.identity_key + Colors.default + ' based on '
if result.keyword is not None:
line += 'keyword "%s".' % result.keyword
elif result.path is not None:
line += 'path "%s" with weakness %s.' % (result.path, result.weakness)
print(line)
identity_obj = result.identity
else:
if args.identity is None:
print(Colors.red + 'error: the following arguments are required: identity or -a' + Colors.default)
return 2
else:
if not identities.has_section('identity.' + args.identity):
print(Colors.red + "The specified identity ID doesn't exist." + Colors.default)
return 1
else:
identity_obj = identities['identity.' + args.identity]
if not args.local:
git_config_user = git.config_get('user', 'name')
git_config_email = git.config_get('user', 'email')
if git_config_user[1] or git_config_email[1]:
print(Colors.yellow + 'Warning:' + Colors.default +
' the current repo has a local identity config and will not use the new value.')
result_n = git.config_set('user', 'name', identity_obj['name'], local=args.local)
result_e = git.config_set('user', 'email', identity_obj['email'], local=args.local)
if not result_e and not result_n:
return 11
elif not result_e: # and resultN
return 3
elif not result_n: # and resultE
return 5
else: # both True
return 0
def unset_identity(args):
result_n = git.config_unset('user', 'name')
result_e = git.config_unset('user', 'email')
if not result_e and not result_n:
return 5
elif not result_e: # and resultN
return 2
elif not result_n: # and resultE
return 3
else: # both True
return 0