-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitr.py
executable file
·260 lines (208 loc) · 7.05 KB
/
gitr.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
#!/usr/bin/python
import sys
import platform
import re
import os
import subprocess
import collections
import pprint
# Todo:
# * allow user defined groups of modules driven by .gitrecurse config files
# * submodule removal / relocation via .gitrecurse
# * commit (depth-first commit generation)
verbose = False
#
# Helpers
#
def die( err ):
print( err )
exit( 1 )
def failed( code ):
if platform.system() == 'Windows':
return code != 0
else:
return code >> 8 != 0 # http://docs.python.org/3.3/library/os.html#os.system
rootPath = os.getcwd()
prevPath = []
def currentPath():
return os.path.relpath( os.getcwd(), rootPath )
def pushPath( path ):
prevPath.append( os.path.abspath( os.getcwd() ) )
os.chdir( os.path.abspath( path ) )
def popPath():
path = prevPath.pop()
os.chdir( path )
def run( cmd, exitOnFailure = False ):
if verbose:
print( "\n[" + os.path.relpath( os.getcwd(), rootPath ) + "]: " + cmd )
code = os.system( cmd )
if exitOnFailure:
if failed( code ):
print( cmd + " returned " + str( code ) + ", exiting with that code" )
exit( code )
return code
def runAndCapture( cmd, exitOnFailure = False ):
if verbose:
print( "\n[" + os.path.relpath( os.getcwd(), rootPath ) + "]: " + cmd )
proc = subprocess.Popen( cmd, shell=True, stdout=subprocess.PIPE )
output = []
for line in proc.stdout:
output.append( line.decode("utf-8") );
proc.wait() # sets .returncode
resultTuple = collections.namedtuple('Result', ['code', 'output'])
result = resultTuple( proc.returncode, output )
if exitOnFailure:
if failed( result.code ):
print( cmd + " returned " + str( result.code ) + ", exiting with that code" )
exit( result.code )
return result
def runRecursive( cmd, exitOnFailure = False ):
code = run( cmd, exitOnFailure )
if failed( code ):
return code
cmd = "git submodule"
result = runAndCapture( cmd, exitOnFailure )
if failed( result.code ):
return result.code
for line in result.output:
submodule = parseSubmoduleStatus( line )
if submodule[ "path" ][:2] != "..":
pushPath( submodule[ "path" ] )
code = runRecursive( cmd, exitOnFailure )
popPath()
if failed( code ):
return code
def parseSubmoduleStatus( status ):
match = re.match( "([\+\- ])(\w+) (.*) \(.*\)", status )
if not match: # sometimes we don't get a parenthetical at the end of the line
match = re.match( "([\+\- ])(\w+) (.*)", status )
if match:
result = {}
result[ 'commit' ] = match.group( 2 )
result[ 'dirty' ] = True if match.group( 1 ) == "+" else False
result[ 'new' ] = True if match.group( 1 ) == "-" else False
result[ 'path' ] = match.group( 3 )
return result
else:
die( "Unable to parse submodule status: " + status )
def getBranch( result ):
for line in result.output:
match = re.match( "\* (.*)", line )
if match:
branch = match.group(1)
if branch == "(no branch)":
return None
else:
match = re.match( "\(detached from .+\)", branch)
if match:
return None
match = re.match( "\(HEAD detached at .+\)", branch)
if match:
return None
match = re.match( "\(HEAD detached from .+\)", branch)
if match:
return None
return branch
return None
#
# Tasks (callable from the frontend)
#
def do():
cmd = ""
for arg in sys.argv[2:]:
if ' ' in arg:
cmd += " \"" + arg + "\"";
else:
cmd += " " + arg
return runRecursive( cmd.strip(), exitOnFailure = True )
def fetch():
return run( "git fetch --recurse-submodules=yes", exitOnFailure = True )
def headless( isRoot = True ):
if not isRoot:
result = runAndCapture( "git rev-parse HEAD", exitOnFailure = True )
run( "git checkout -q " + result.output[0], exitOnFailure = True ) # `git checkout <commit>` won't touch workspace files, see git docs
result = runAndCapture( "git submodule", exitOnFailure = True )
for line in result.output:
submodule = parseSubmoduleStatus( line )
if submodule[ "path" ][:2] != "..":
pushPath( submodule[ "path" ] )
headless( False )
popPath()
return 0
def pull( newCommit = None ):
if getBranch( runAndCapture( "git branch", exitOnFailure = True ) ) != None:
print( "Pulling " + currentPath() )
run( "git pull", exitOnFailure = True )
else:
if newCommit:
result = runAndCapture( "git rev-parse HEAD", exitOnFailure = True )
if result.output[0].strip() != newCommit.strip():
print( "Fetching tags for " + currentPath() )
run( "git fetch --tags", exitOnFailure = True )
print( "Checking out " + currentPath() + " to " + newCommit )
run( "git reset -q --hard " + newCommit, exitOnFailure = True )
result = runAndCapture( "git submodule status", exitOnFailure = True )
if len( result.output ):
for line in result.output:
submodule = parseSubmoduleStatus( line )
if submodule[ "path" ][:2] != "..":
if submodule[ "new" ]:
run( "git submodule update --init --recursive -- " + submodule[ "path" ], exitOnFailure = True )
else:
result = runAndCapture( "git diff -- " + submodule[ "path" ], exitOnFailure = True )
newSubmoduleCommit = None
for line in result.output:
match = re.match( "-Subproject commit (.*)", line )
if match:
newSubmoduleCommit = match.group(1)
pushPath( submodule[ "path" ] )
pull( newSubmoduleCommit )
popPath()
return 0
def push():
result = runAndCapture( "git submodule status", exitOnFailure = True )
if len( result.output ):
for line in result.output:
submodule = parseSubmoduleStatus( line )
if submodule[ "path" ][:2] != "..":
pushPath( submodule[ "path" ] )
push()
popPath()
if getBranch( runAndCapture( "git branch", exitOnFailure = True ) ) != None:
print( "Pushing " + currentPath() )
run( "git push", exitOnFailure = True )
return 0
def status():
branch = getBranch( runAndCapture( "git branch", exitOnFailure = True ) )
if branch:
print( currentPath() + " is on branch " + branch )
else:
print( currentPath() + " is headless" )
result = runAndCapture( "git submodule", exitOnFailure = True )
for line in result.output:
submodule = parseSubmoduleStatus( line )
if submodule[ "path" ][:2] != "..":
pushPath( submodule[ "path" ] )
status()
popPath()
return 0
def update():
return run( "git submodule update --init --recursive", exitOnFailure = True )
task = None
if len( sys.argv ) > 1:
if sys.argv[1] in locals():
task = locals()[ sys.argv[1] ];
else:
print( "\nUnknown command: " + sys.argv[1] + "\n" )
if task:
exit( task() )
else:
print( "Usage: " + sys.argv[0] + " <command> [arguments]" )
print( "\n Commands:" )
print( " do -- execute a command in the current and submodule workspaces" )
print( " fetch -- shorthand for `git fetch --recurse-submodules=yes`" )
print( " headless -- change all submodules to not follow any branch (headless checkout of HEAD)" )
print( " pull -- fetch and merge such that submodules following branches pull to the latest branch HEAD" )
print( " push -- push submodules following branches to their respective remote" )
print( " status -- enhanced status information about submodules")
print( " update -- shorthand for `git submodule update --init --recursive`" )