forked from mnagel/clustergit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clustergit
executable file
·386 lines (335 loc) · 13.8 KB
/
clustergit
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
382
383
384
385
386
#! /usr/bin/env python
""" run git commands on multiple git clones https://github.com/mnagel/clustergit """
from __future__ import print_function
import re
import sys
import os
from argcomplete import autocomplete
try:
from argparse import ArgumentParser
except Exception:
# python3
from ArgParse import ArgumentParser
try:
import commands
except Exception:
# python3
import subprocess as commands
commands.get_output = commands.check_output
def colorize(color, message):
return "%s%s%s" % (color, message, Colors.ENDC)
def colorize_off(color, message):
for color in [Colors.BOLD, Colors.UNDERLINE, Colors.HEADER,
Colors.OKBLUE, Colors.OKGREEN,
Colors.WARNING, Colors.FAIL, Colors.ENDC]:
message = message.replace(color, '')
return message
class Colors:
BOLD = '\033[1m' # unused
UNDERLINE = '\033[4m'# unused
HEADER = '\033[95m' # unused
OKBLUE = '\033[94m' # write operation succeeded
OKGREEN = '\033[92m' # readonly operation succeeded
WARNING = '\033[93m' # operation succeeded with non-default result
FAIL = '\033[91m' # operation did not succeed
ENDC = '\033[0m' # reset color
def read_arguments():
parser = ArgumentParser(description="""
clustergit will scan through all subdirectories looking for a .git directory.
When it finds one it'll look to see if there are any changes and let you know.
If there are no changes it can also push and pull to/from a remote location.
""".strip())
parser.add_argument("-d", "--dir",
dest = "dirname",
action = "store",
help = "The directory to parse sub dirs from",
default = "."
)
parser.add_argument("-v", "--verbose",
action = "store_true",
dest = "verbose",
default = False,
help = "Show the full detail of git status"
)
parser.add_argument("-a", "--align",
action = "store",
dest = "align",
default = 40,
type = int,
help = "Repo name align (space padding)"
)
parser.add_argument("-r", "--remote",
action = "store",
dest = "remote",
default = "",
help = "Set the remote name (remotename:branchname)"
)
parser.add_argument("--push",
action = "store_true",
dest = "push",
default = False,
help = "Do a 'git push' if you've set a remote with -r it will push to there"
)
parser.add_argument("-p", "--pull",
action = "store_true",
dest = "pull",
default = False,
help = "Do a 'git pull' if you've set a remote with -r it will pull from there"
)
parser.add_argument("--exec", "--execute",
action = "store",
dest = "command",
type = str,
default = "",
help = "Execute a shell command in each repository"
)
parser.add_argument("-c", "--clear",
action = "store_true",
dest = "clear",
default = False,
help = "Clear screen on startup"
)
parser.add_argument("-C", "--count-dirty",
action = "store_true",
dest = "count",
default = False,
help = "Only display a count of not-clean repos"
)
parser.add_argument("-q", "--quiet",
action = "store_true",
dest = "quiet",
default = False,
help = "Skip startup info"
)
parser.add_argument("-H", "--hide-clean",
action = "store_true",
dest = "hide_clean",
default = False,
help = "Hide clean repos"
)
parser.add_argument("-R", "--relative",
action = "store_true",
dest = "relative",
default = False,
help = "Print relative paths"
)
parser.add_argument("-n", "--no-colors",
action = "store_false",
dest = "colors",
default = True,
help = "Disable ANSI color output"
)
parser.add_argument("-b", "--branch",
action = "store",
dest = "branch",
default = "master",
help = "Warn if not on this branch"
)
parser.add_argument("--recursive",
action = "store_true",
dest = "recursive",
default = False,
help = "Recursively search for git repos"
)
parser.add_argument("-e", "--exclude",
action = "append",
dest = "exclude",
default = [],
help = "Regex to exclude directories"
)
parser.add_argument("-B", "--checkout-branch",
action = "store",
dest = "cbranch",
default = [],
help = "Checkout branch"
)
parser.add_argument("--warn-unversioned",
action = "store_true",
dest = "unversioned",
default = False,
help = "Prints a warning if a directory is not under git version control"
)
autocomplete(parser)
options = parser.parse_args()
return options
def show_error(error="Undefined Error!"):
"""Writes an error to stderr"""
sys.stderr.write(error)
sys.exit(1)
def is_excluded(path, options):
for ex in options.exclude:
if re.search(ex, path):
if options.verbose:
print("skipping %s" % (path))
return True
return False
def run(command, options):
if options.verbose:
print("running %s" % (command))
return commands.getoutput(command)
def check(dirname, options):
"""
Check the subdirectories of a single directory.
See if they are versioned in git and display the requested information.
"""
gitted = False
dirties = 0
# See whats here
files = os.listdir(dirname)
files[:] = [f for f in files if not is_excluded(os.path.join(dirname, f), options)]
files.sort()
for infile in files:
infile = os.path.join(dirname, infile)
#is there a .git file
if os.path.exists( os.path.join(infile, ".git") ):
if options.verbose:
sys.stdout.write("\n")
sys.stdout.write("---------------- "+ infile +" -----------------\n")
#Yay, we found one!
gitted = True
# OK, contains a .git file. Let's descend into it
# and ask git for a status
out = run('cd "%s"; LC_ALL=C git status' % infile, options)
if options.verbose:
sys.stdout.write(out + "\n")
if options.relative:
infile = os.path.relpath(infile, options.dirname)
messages = []
clean = True
can_push = False
can_pull = True
if len(options.branch) > 0 and 'On branch ' + options.branch not in out:
branch = out.splitlines()[0].replace("On branch ","")
messages.append(colorize(Colors.WARNING, "On branch %s" % branch))
can_pull = False
clean = False
if re.search(r'nothing to commit.?.?working directory clean.?', out):
messages.append(colorize(Colors.OKBLUE, "No Changes"))
can_push = True
elif 'nothing added to commit but untracked files present' in out:
messages.append(colorize(Colors.WARNING, "Untracked files"))
can_push = True
clean = False
else:
messages.append(colorize(Colors.FAIL, "Changes"))
can_pull = False
clean = False
if 'Your branch is ahead of' in out:
messages.append(colorize(Colors.FAIL, "Unpushed commits"))
can_pull = False
clean = False
else:
can_push = False
if clean:
if not options.hide_clean:
messages = [colorize(Colors.OKGREEN, "Clean")]
else:
messages = []
else:
dirties += 1
if can_push and options.push:
# Push to the remote
push = run(
'cd "%s"; LC_ALL=C git push %s'
% (infile, ' '.join(options.remote.split(":"))), options
)
if options.verbose:
sys.stdout.write(push + "\n")
if re.search(r'\[(remote )?rejected\]', push):
messages.append(colorize(Colors.FAIL, "Push rejected"))
else:
messages.append(colorize(Colors.OKBLUE, "Pushed OK"))
if can_pull and options.pull:
# Pull from the remote
pull = run(
'cd "%s"; LC_ALL=C git pull %s'
% (infile, ' '.join(options.remote.split(":"))), options
)
if options.verbose:
sys.stdout.write(pull + "\n")
if "Already up-to-date" in pull:
if not options.hide_clean:
messages.append(colorize(Colors.OKGREEN, "Pulled nothing"))
elif "CONFLICT" in pull:
messages.append(colorize(Colors.FAIL, "Pull conflict"))
elif "fatal: No remote repository specified." in pull:
messages.append(colorize(Colors.WARNING, "Pull remote not configured"))
elif "fatal: " in pull:
messages.append(colorize(Colors.FAIL, "Pull fatal"))
else:
messages.append(colorize(Colors.OKBLUE, "Pulled"))
if options.command:
cmd = run(
'cd "%s"; LC_ALL=C %s; echo $?'
% (infile, options.command),
options
)
if not options.colors:
cmd = colorize_off('', cmd)
if not options.quiet:
messages.append('\n' + cmd[:-2])
if not cmd[-1] is '0':
show_error(colorize(Colors.FAIL,
"The command exited with status {s} in {r}".format(s=cmd[-1],r=infile) +
"\nThe output was:" + cmd[:-2]))
if options.cbranch:
checkoutbranch = run(
'cd "%s"; LC_ALL=C git checkout %s'
% (infile, options.cbranch), options
)
if options.verbose:
sys.stdout.write(pull + "\n")
if "Already on" in checkoutbranch:
if not options.hide_clean:
messages.append(colorize(Colors.OKGREEN, "No action"))
elif "error: " in checkoutbranch:
messages.append(colorize(Colors.FAIL, "Checkout failed"))
else:
messages.append(colorize(Colors.OKBLUE, "Checkout successful"))
if not options.count and messages:
sys.stdout.write(infile.ljust(options.align) + ": ")
sys.stdout.write(", ".join(messages) + "\n")
sys.stdout.flush()
# Come out of the dir and into the next
run('cd ../', options)
if options.verbose:
sys.stdout.write("---------------- "+ infile +" -----------------\n")
elif options.unversioned and not infile.startswith("./.") and os.path.isdir(infile):
sys.stdout.write(infile.ljust(options.align) + ": ")
sys.stdout.write(colorize(Colors.WARNING, "Not a GIT repository")+"\n")
sys.stdout.flush()
return gitted, dirties
#-------------------
# Now, onto the main event!
#-------------------
def main():
try:
options = read_arguments()
if options.clear:
os.system('clear')
if not options.quiet:
sys.stdout.write('Starting git status...\n')
sys.stdout.write('Scanning sub directories of %s\n' %options.dirname)
if not options.colors:
global colorize
colorize = colorize_off
gitted = False
dirties = 0
for (path, dirs, files) in os.walk(options.dirname, topdown=True):
new_gitted, new_dirties = check(path, options)
gitted = gitted or new_gitted
dirties += new_dirties
if not options.recursive:
break
if not gitted:
show_error("Error: None of those sub directories had a .git file.\n")
if options.count:
sys.stdout.write(str(dirties) + "\n")
if dirties == 0 and options.hide_clean:
sys.stdout.write("All repos clean\n")
if not options.quiet:
sys.stdout.write("Done\n")
except (KeyboardInterrupt, SystemExit):
sys.stdout.write("\n")
if __name__ == '__main__':
main()