forked from MikePearce/Git-Status
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshow_status
More file actions
executable file
·268 lines (228 loc) · 9.08 KB
/
show_status
File metadata and controls
executable file
·268 lines (228 loc) · 9.08 KB
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
#!/usr/bin/python
# @desc Tired of having to go into each sub dir to find out whether or
# not you did a git commit? Tire no more, just use this!
#
# @usage ./showstatus [location]
# @author Mike Pearce <mike@mikepearce.net>
# @since 18/05/2010
# Grab some libraries
import sys
import os
import glob
import commands
from optparse import OptionParser
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
# Setup some stuff
dirname = './'
gitted = False
mini = True
parser = OptionParser(description="\
Show Status is awesome. If you tell it a directory to look in, it'll scan \
through all the sub dirs looking for a .git directory. When it finds one \
it'll look to see if there are any changes and let you know. \
It can also push and pull to/from a remote location (like github.com) \
(but only if there are no changes.) \
Contact mike@mikepearce.net for any support.")
parser.add_option("-d", "--dir",
dest = "dirname",
action = "store",
help = "The directory to parse sub dirs from",
default = os.path.abspath("./")+"/"
)
parser.add_option("-v", "--verbose",
action = "store_true",
dest = "verbose",
default = False,
help = "Show the full detail of git status"
)
parser.add_option("-a", "--align",
action = "store",
dest = "align",
default = 40,
type = "int",
help = "Repo name align (space padding)"
)
parser.add_option("-r", "--remote",
action = "store",
dest = "remote",
default = "",
help = "Set the remote name (remotename:branchname)"
)
parser.add_option("--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_option("-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_option("-c", "--clear",
action = "store_true",
dest = "clear",
default = False,
help = "Clear screen on startup"
)
parser.add_option("-C", "--count-dirty",
action = "store_true",
dest = "count",
default = False,
help = "Only display a count of not-clean repos"
)
parser.add_option("-q", "--quiet",
action = "store_true",
dest = "quiet",
default = False,
help = "Skip startup info"
)
parser.add_option("-H", "--hide-clean",
action = "store_true",
dest = "hide_clean",
default = False,
help = "Hide clean repos"
)
parser.add_option("-R", "--relative",
action = "store_true",
dest = "relative",
default = False,
help = "Print relative paths"
)
parser.add_option("-n", "--no-colors",
action = "store_false",
dest = "colors",
default = True,
help = "Disable ANSI color output"
)
parser.add_option("-b", "--branch",
action = "store",
dest = "branch",
default = "master",
help = "Warn if not on this branch"
)
# Now, parse the args
(options, args) = parser.parse_args()
#-------------------
def show_error(error="Undefined Error!"):
#-------------------
"""Writes an error to stderr"""
sys.stderr.write(error)
sys.exit(1)
#-------------------
# Now, onto the main event!
#-------------------
if __name__ == "__main__":
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)
bc = bcolors()
if not options.colors:
bc.disable()
dirties = 0
# See whats here
for infile in glob.glob( os.path.join(options.dirname, '*') ):
#is there a .git file
if os.path.exists( os.path.join(infile, ".git") ):
#Yay, we found one!
gitted = True
# OK, contains a .git file. Let's descend into it
# and ask git for a status
out = commands.getoutput('cd '+ infile + '; LC_ALL=C git status')
# Mini?
if not options.verbose:
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:
messages.append(bc.WARNING + "Not on branch %s" % options.branch + bc.ENDC)
can_pull = False
clean = False
if 'nothing to commit (working directory clean)' in out:
messages.append(bc.OKBLUE + "No Changes" + bc.ENDC)
can_push = True
elif 'nothing added to commit but untracked files present' in out:
messages.append(bc.WARNING + "Untracked files" + bc.ENDC)
can_push = True
clean = False
else:
messages.append(bc.FAIL + "Changes" + bc.ENDC)
can_pull = False
clean = False
if 'Your branch is ahead of' in out:
messages.append(bc.FAIL + "Unpushed commits" + bc.ENDC)
can_pull = False
clean = False
else:
can_push = False
if clean:
if options.hide_clean:
continue
messages = [bc.OKGREEN + "Clean" + bc.ENDC]
else:
dirties += 1
if can_push and options.push:
# Push to the remote
push = commands.getoutput(
'cd '+ infile +
'; LC_ALL=C git push '+
' '.join(options.remote.split(":"))
)
if "[rejected]" in push:
messages.append(bc.FAIL + "Push rejected" + bc.ENDC)
else:
messages.append(bc.OKBLUE + "Pushed OK" + bc.ENDC)
if can_pull and options.pull:
# Pull from the remote
pull = commands.getoutput(
'cd '+ infile +
'; LC_ALL=C git pull '+
' '.join(options.remote.split(":"))
)
if "Already up-to-date" in pull:
messages.append("Pulled nothing")
elif "CONFLICT" in pull:
messages.append(bc.FAIL + "Pull conflict" + bc.ENDC)
elif "fatal: " in pull:
messages.append(bc.FAIL + "Pull fatal" + bc.ENDC)
else:
messages.append("Pulled")
# Write to screen
if not options.count:
sys.stdout.write("--" + infile.ljust(options.align) + ": ")
sys.stdout.write(", ".join(messages) + "\n")
sys.stdout.flush()
else:
#Print some repo details
sys.stdout.write("\n---------------- "+ infile +" -----------------\n")
sys.stdout.write(out)
sys.stdout.write("\n---------------- "+ infile +" -----------------\n")
# Come out of the dir and into the next
commands.getoutput('cd ../')
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")