-
Notifications
You must be signed in to change notification settings - Fork 3
/
igitt.py
executable file
·381 lines (312 loc) · 10.1 KB
/
igitt.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#! /usr/bin/env python3
"""Simple helper script for working on multiple git repositories in a specific
directory.
This script is useful if you work with 'mr.developer' sources for project
development or if you want to perform backups of a github account.
Usage
-----
For cloning repositories, use:
igitt clone CONTEXT [PACKAGE]
For updating repositories, use:
igitt pull [PACKAGE]
For backup of context, use:
igitt backup CONTEXT
For checking repository state, use:
igitt st [PACKAGE]
For listing repository branch, use:
igitt b [PACKAGE]
For showing repository diff, use:
igitt diff [PACKAGE]
For committing all repository changes, use:
igitt cia "MESSAGE" [PACKAGE]
For pushing all committed changes, use:
igitt push [PACKAGE]
For discarding all changes, use:
igitt co [package]
Install
-------
Checkout this script and create a symlink in '/usr/local/bin'.
This script requires python2.7 or python2.6 with 'argparse' package installed
"""
import json
import os
import subprocess
import sys
import urllib
from argparse import ArgumentParser
mainparser = ArgumentParser(description="Git helper utilities")
subparsers = mainparser.add_subparsers(help="commands")
def listdir(path="."):
return sorted(os.listdir(path))
def hilite(string, color, bold):
# http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
# http://stackoverflow.com/questions/2330245/python-change-text-color-in-shell
attr = []
if color == "green":
attr.append("32")
elif color == "red":
attr.append("31")
elif color == "blue":
attr.append("34")
if bold:
attr.append("1")
return "\x1b[{}m{}\x1b[0m".format(";".join(attr), string)
def query_repos(context):
org_url = "https://api.github.com/orgs/%s/repos" % context
user_url = "https://api.github.com/users/%s/repos" % context
query = "%s?page=%i&per_page=50"
data = list()
page = 1
while True:
try:
url = query % (org_url, page)
res = urllib.request.urlopen(url)
except urllib.error.URLError:
try:
url = query % (user_url, page)
res = urllib.request.urlopen(url)
except urllib.error.URLError as e:
print(e)
sys.exit(0)
page_data = json.loads(res.read())
res.close()
if not page_data:
break
data += page_data
page += 1
print("Fetched %i repositories for '%s'" % (len(data), context))
return data
def perform_clone(arguments):
base_uri = "[email protected]:%s/%s.git"
context = arguments.context[0]
if arguments.repository:
repos = arguments.repository
else:
repos = [_["name"] for _ in query_repos(args.context)]
for repo in repos:
uri = base_uri % (context, repo)
cmd = ["git", "clone", uri]
subprocess.call(cmd)
sub = subparsers.add_parser("clone", help="Clone from an organisation or a user")
sub.add_argument("context", nargs=1, help="Name of organisation or user")
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to clone, leave empty to clone all",
)
sub.set_defaults(func=perform_clone)
def get_branch():
cmd = "git branch"
p = subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
)
output = p.stdout.readlines()
for line in output:
line = line.decode("utf-8") # python3 returns bytes, python2 doesn't break.
if line.strip().startswith("*"):
return line.strip().strip("*").strip()
def perform_pull(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Perform pull for '%s'" % hilite(child, "blue", True))
cmd = ["git", "pull", "origin", get_branch()]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser("pull", help="Pull distinct or all repositories in folder.")
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to pull, leave empty to pull all",
)
sub.set_defaults(func=perform_pull)
def perform(cmd):
pr = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pr.communicate()
print(stdout)
if pr.returncode != 0:
print("{} failed to perform: exit code {}".format(" ".join(cmd), pr.returncode))
print(stderr)
def perform_backup(arguments):
context = arguments.context[0]
if context not in listdir("."):
os.mkdir(context)
os.chdir(context)
contents = listdir(".")
data = query_repos(context)
base_uri = "[email protected]:%s/%s.git"
for repo in data:
name = repo["name"]
fs_name = "%s.git" % name
if fs_name in contents:
print("Fetching existing local repository '%s'" % fs_name)
os.chdir(fs_name)
perform(["git", "fetch", "origin"])
os.chdir("..")
else:
print("Cloning new repository '%s'" % fs_name)
uri = base_uri % (context, name)
perform(["git", "clone", "--bare", "--mirror", uri])
sub = subparsers.add_parser(
"backup", help="Backup all repositories from an organisation or a user"
)
sub.add_argument("context", nargs=1, help="Name of organisation or user")
sub.set_defaults(func=perform_backup)
def perform_status(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Status for '%s'" % hilite(child, "blue", True))
cmd = ["git", "status"]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser(
"st", help="Status of distinct or all repositories in current folder."
)
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to show, leave empty to show all",
)
sub.set_defaults(func=perform_status)
def perform_b(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Branches for '%s'" % hilite(child, "blue", True))
cmd = ["git", "branch"]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser(
"b", help="Show branches of distinct or all repositories in current folder."
)
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to show, leave empty to show all",
)
sub.set_defaults(func=perform_b)
def perform_diff(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Diff for '%s'" % hilite(child, "blue", True))
cmd = ["git", "diff"]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser(
"diff", help="Show diff of distinct or all repositories in current folder."
)
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to show diff of, leave empty to show all",
)
sub.set_defaults(func=perform_diff)
def perform_cia(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
message = arguments.message[0]
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Commit all changes resources for '%s'" % hilite(child, "blue", True))
cmd = ["git", "cia", "-am", message]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser(
"cia",
help="Commit all changes of distinct or all repositories in current " "folder.",
)
sub.add_argument("message", nargs=1, help="Commit message")
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to commit, leave empty to commit all",
)
sub.set_defaults(func=perform_cia)
def perform_push(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Perform push for '%s'" % hilite(child, "blue", True))
cmd = ["git", "push", "origin", get_branch()]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser("push", help="Push distinct or all repositories in folder.")
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to push, leave empty to push all",
)
sub.set_defaults(func=perform_push)
def perform_co(arguments):
if arguments.repository:
dirnames = arguments.repository
else:
dirnames = listdir(".")
for child in dirnames:
if not os.path.isdir(child):
continue
if ".git" not in listdir(child):
continue
os.chdir(child)
print("Perform checkout for '%s'" % hilite(child, "blue", True))
cmd = ["git", "checkout", "."]
subprocess.call(cmd)
os.chdir("..")
sub = subparsers.add_parser("co", help="Discard all uncommited changes.")
sub.add_argument(
"repository",
nargs="*",
help="Name of repositories to discard, leave empty to discard all",
)
sub.set_defaults(func=perform_co)
if __name__ == "__main__":
args = mainparser.parse_args()
args.func(args)