forked from MikePearce/Git-Status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_status
executable file
·168 lines (138 loc) · 5.29 KB
/
show_status
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
#!/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!
#
# @author Mike Pearce <mike@mikepearce.net>
# @since 18/05/2010
# Grab some libraries
import sys
import os
import glob
import commands
import subprocess
from optparse import OptionParser
import os.path
# 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("-n", "--num",
dest = "depth",
action = "store",
help = "Number of recursive directories to process",
default = 1
)
parser.add_option("-v", "--verbose",
action = "store_true",
dest = "verbose",
default = False,
help = "Show the full detail of git status"
)
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"
)
# 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)
def find_repos(depth, dir = ".", curdepth=1):
ret = []
names = os.listdir(dir)
for name in names:
temp_dir = os.path.join(dir, name)
if os.path.exists(os.path.join(temp_dir, ".git")):
ret.append(temp_dir)
elif os.path.isdir(temp_dir):
if depth > curdepth:
ret.extend(find_repos(depth, os.path.join(dir, name), curdepth + 1))
return ret
def call_process(cmd):
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stdin) = (p.stdout, p.stdin)
data = stdout.readline()
ret = ""
while data:
data = stdout.readline()
ret += data
stdout.close()
stdin.close()
return ret
#-------------------
# Now, onto the main event!
#-------------------
if __name__ == "__main__":
os.system('clear')
sys.stdout.write('-- Starting git status...\n')
sys.stdout.write('Scanning sub directories of %s\n' %options.dirname)
repos = find_repos(options.depth)
# See whats here
for infile in repos:
#Yay, we found one!
gitted = True
# OK, contains a .git file. Let's descend into it
# and ask git for a status
cur_dir = os.getcwd()
os.chdir(infile)
out = call_process("git status")
# Mini?
if False == options.verbose:
if -1 != out.find('nothing'):
result = ": No Changes"
# Pull from the remote
if False != options.pull:
push = call_process('git pull '+
' '.join(options.remote.split(":"))
)
result = result + " (Pulled) \n" + push
# Push to the remote
if False != options.push:
push = call_process('git push '+
' '.join(options.remote.split(":"))
)
result = result + " (Pushed) \n" + push
# Write to screen
sys.stdout.write("--" + infile.ljust(40) + result +"\n")
else:
sys.stdout.write("--" + infile.ljust(40) +": Changes\n")
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
os.chdir(cur_dir)
if False == gitted:
show_error("Error: None of those sub directories had a .git file.\n")
sys.stdout.write("Done\n")