-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit_utils.py
541 lines (495 loc) · 20.8 KB
/
git_utils.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
'''
Utilities for creating GIT bare repos and such'''
import os
import re
import fileinput
import subprocess
import sys
from repo_defaults import *
from svn_utils import *
from version_utils import *
import gc
'''TODO: Fix error msg on svn epics-release:
fatal: Not a git repository (or any parent up to mount point /reg/g/pcds)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).'''
LCLS_TOOLS = '/afs/slac/g/lcls/tools'
if 'TOOLS' in os.environ:
TOOLS_SITE_TOP = os.environ['TOOLS']
else:
TOOLS_SITE_TOP = LCLS_TOOLS
os.environ['TOOLS'] = TOOLS_SITE_TOP
gitModulesTxtFile = os.path.join( TOOLS_SITE_TOP, 'eco_modulelist', 'modulelist.txt' )
def parseGitModulesTxt():
'''Parse the GIT modules txt file and return a dict of packageName -> location'''
if not os.path.isfile( gitModulesTxtFile ):
return {}
package2Location = {}
with open(gitModulesTxtFile, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith('#'):
continue
parts = line.split()
if(len(parts) < 2):
print("Error parsing ", gitModulesTxtFile, "Cannot break", line, "into columns with enough fields using spaces/tabs")
continue
packageName = parts[0]
packageLocation = expandMacros( parts[1], os.environ )
package2Location[packageName] = packageLocation
return package2Location
git_package2Location = parseGitModulesTxt()
def determineGitRoot( ):
'''Get the root folder for GIT repos at SLAC'''
gitRoot = DEF_AFS_GIT_REPOS
# The GIT_REPO_ROOT variable is mainly used when testing eco and is not something that we really expect from the environment.
if "GIT_REPO_ROOT" in os.environ:
gitRoot = os.environ["GIT_REPO_ROOT"]
elif "GIT_TOP" in os.environ:
gitRoot = os.environ["GIT_TOP"]
return gitRoot
def git_call( gitCommand, gitDir=None, debug=False, *args, ** kwargs ):
'''
Run the specified git command via subprocess.call
gitCommand can be a string or a list of strings.
An initial "git" string will be provided if needed.
Returns command status
'''
cmdList = []
if type(gitCommand) is str:
if not gitCommand.startswith( 'git ' ):
cmdList = [ 'git' ]
cmdList += gitCommand.split()
else:
if gitCommand[0] != 'git':
cmdList = [ 'git' ]
cmdList += gitCommand
if gitDir is not None:
cmdList.insert( 1, [ '--git-dir', gitDir ] )
if debug:
print("git_call running: %s" % ' '.join( cmdList ))
callStatus = subprocess.call( cmdList, *args, **kwargs )
if debug:
print("git_call status:", callStatus)
return callStatus
def git_check_call( gitCommand, gitDir=None, debug=False, *args, ** kwargs ):
'''
Run the specified git command via subprocess.check_call
gitCommand can be a string or a list of strings.
An initial "git" string will be provided if needed.
Returns cmd status code
May throw RuntimeError or subprocess.CalledProcessError exceptions
'''
cmdList = []
if isinstance( gitCommand, str ):
if not gitCommand.startswith( 'git ' ):
cmdList = [ 'git' ]
cmdList += gitCommand.split()
else:
if gitCommand[0] != 'git':
cmdList = [ 'git' ]
cmdList += gitCommand
if gitDir is not None:
cmdList.insert( 1, [ '--git-dir', gitDir ] )
if debug:
print("git_check_call running: %s" % ' '.join( cmdList ))
callStatus = subprocess.check_call( cmdList, *args, **kwargs )
if debug:
print("git_check_call status:", callStatus)
return callStatus
def git_check_output( gitCommand, gitDir=None, debug=False, *args, ** kwargs ):
'''
Run the specified git command via subprocess.check_output
gitCommand can be a string or a list of strings.
An initial "git" string will be provided if needed.
Returns cmd output
May throw RuntimeError or subprocess.CalledProcessError exceptions
'''
cmdList = []
if type(gitCommand) is str:
if not gitCommand.startswith( 'git ' ):
cmdList = [ 'git' ]
cmdList += gitCommand.split()
else:
if gitCommand[0] != 'git':
cmdList = [ 'git' ]
cmdList += gitCommand
if gitDir is not None:
cmdList.insert( 1, [ '--git-dir', gitDir ] )
if debug:
print("git_check_output running: %s" % ' '.join( cmdList ))
git_output = subprocess.check_output( cmdList, *args, **kwargs, universal_newlines=True )
if debug:
print(git_output)
return git_output
def gitGetRemoteFile( url, refName, filePath, debug = False ):
'''Fetchs a file from a git repo url.
Returns file as a string or None if not found.'''
fileContents = None
try:
commitSpec = refName + ':' + filePath
cmdList = [ 'git', '--git-dir=%s' % url, 'show', commitSpec ]
if debug:
print( "gitGetRemoteFile: Running %s" % cmdList.join() )
fileContents = subprocess.check_output( cmdList, stderr=subprocess.STDOUT, universal_newlines=True )
except OSError as e:
if debug:
print(e)
pass
except subprocess.CalledProcessError as e:
if debug:
print(e)
pass
return fileContents
def gitGetRemoteTags( url, debug = False, verbose = False ):
'''Fetchs a list of tags from a git repo url.
Returns a dictionary of SHA1 hashes by tagName.'''
tagSpecRegExp = re.compile( r"^(.*)\s+refs/tags/(.*)$" )
tags = {}
try:
if verbose:
print("gitGetRemoteTags running: git ls-remote %s" % url)
statusInfo = subprocess.check_output( [ 'git', 'ls-remote', url ], stderr=subprocess.STDOUT, universal_newlines=True )
for line in statusInfo.splitlines():
if line is None:
break
tagSpecMatch = tagSpecRegExp.search( line )
if not tagSpecMatch:
continue
tags[ tagSpecMatch.group(2) ] = tagSpecMatch.group(1)
except OSError as e:
if debug:
print(e)
pass
except subprocess.CalledProcessError as e:
if debug:
print(e)
pass
if verbose:
print("gitGetRemoteTags: Found %d tags in repo %s" % ( len(tags), url if url else None ))
return tags
def gitGetRemoteTag( url, tag, debug = False, verbose = False ):
'''Fetchs tags from a git repo url and looks for a match w/ the desired tag.
Returns a tuple of ( sha, tag ), ( None, None ) on error.
For a matching git remote, url must be a valid string and tag must be found.'''
tag_sha = None
git_url = None
git_tag = None
url_valid = False
tags = {}
if tag is None:
tag = 'HEAD'
tag_spec = tag
else:
tag_spec = 'refs/tags/%s' % tag
try:
if url:
tags = gitGetRemoteTags( url, debug = debug, verbose = verbose )
if tag in tags:
git_url = url
git_tag = tag
tag_sha = tags[tag]
except OSError as e:
if debug:
print(e)
pass
except subprocess.CalledProcessError as e:
if debug:
print(e)
pass
if verbose:
if git_url:
print("gitGetRemoteTag: Found git_tag %s %7.7s in git_url %s" % ( git_tag, tag_sha, git_url ))
elif url_valid:
print("gitGetRemoteTag: Unable to find tag %s in git url %s" % ( tag, url ))
else:
print("gitGetRemoteTag: Invalid git url %s" % ( url if url else None ))
return ( tag_sha, git_tag )
def gitGetTagSha( tag ):
tagSha = None
try:
# Get the tagSha
cmdList = [ "git", "show-ref", tag ]
gitOutput = subprocess.check_output( cmdList, universal_newlines=True ).splitlines()
if len(gitOutput) == 1:
tagSha = gitOutput[0].split()[0]
except:
pass
return tagSha
def initBareRepo( gitRepoPath, verbose=False ):
if not gitRepoPath.endswith( ".git" ):
raise Exception( "initBareRepo: repo path must end in .git\n%s" % gitRepoPath )
if os.path.exists(gitRepoPath):
raise Exception("Git repo path already exists at " + gitRepoPath)
# Make sure parent folder exists
( parentFolder, module ) = os.path.split( gitRepoPath )
if not os.path.isdir( parentFolder ):
if verbose: print("Pre-creating parent folder for " + gitRepoPath)
os.makedirs( parentFolder, 0o775 )
if verbose: print("Creating a new bare repo in " + gitRepoPath)
subprocess.check_call(["git", "init", "--bare", "--template=%s/templates" % DEF_GIT_MODULES_PATH, gitRepoPath])
if not os.path.exists(gitRepoPath):
raise Exception( "Failed to create git repo at:\n" + gitRepoPath )
def cloneUpstreamRepo( gitUpstreamRepo, tpath, packageName, branch=None, depth=None, verbose=False ):
'''Create a clone of the upstream repo given a destination folder'''
if packageName:
clonedFolder = os.path.join(tpath, packageName)
else:
clonedFolder = tpath
prompt = "Cloning the upstream repo at %s into %s" % ( gitUpstreamRepo, clonedFolder )
gitCommand = "clone --recursive %s %s" % ( gitUpstreamRepo, clonedFolder )
if branch:
gitCommand += " --branch %s" % branch
prompt += " from branch %s" % branch
if gitGetVersionNumber() > 1.08:
gitCommand += " --config advice.detachedHead=false"
#if depth and gitUpstreamRepo.find('://') > 0:
if depth:
gitCommand += " --no-local --depth %d" % depth
#May throw RuntimeError or subprocess.CalledProcessError exceptions
print(prompt)
#print "%s" % prompt
git_check_call( gitCommand, debug=verbose )
return clonedFolder
def createGitIgnore():
gitIgnoreLines = [
"*~",
"O.*/",
"*.log",
"*.swp",
"CVS/",
"bin/",
"db/",
"dbd/",
"html/",
"include/",
"lib/",
"templates/",
"cdCommands",
"envPaths",
"RELEASE_SITE",
"tags"
]
with open(".gitignore", "w") as f:
f.write("\n".join(gitIgnoreLines))
subprocess.check_call(['git', 'add', '.gitignore'])
def gitCommitAndPush( message ):
'''Call git commit and git push'''
subprocess.check_call(['git', 'commit', '-m', message ])
message = 'Initial commit/import from eco. Added a default .gitignore and other defaults.'
subprocess.check_call(['git', 'push' ])
def addPackageToEcoModuleList(packageName, gitUpstreamRepo):
'''Add the package with the given upstream repo to eco's modulelist'''
curDir = os.getcwd()
print("Adding package", packageName, "to eco_modulelist")
tools_dir = os.environ['TOOLS']
if not tools_dir:
print('addPackageToEcoModuleList Error: TOOLS env not defined.')
return
gitModulesTxtFolder = os.path.join(os.environ['TOOLS'], 'eco_modulelist')
os.chdir(gitModulesTxtFolder)
try:
repoCmd = [ 'git', 'status', '--short', '--untracked-files=no' ]
statusInfo = git_check_output( repoCmd, stderr=subprocess.STDOUT )
statusLines = statusInfo.splitlines()
if len(statusLines) > 0:
repoCmd = [ 'git', 'commit', '-a', '-m', '"eco_tools addPackageToEcoModuleList: Committing uncommited changes"' ]
git_check_call( repoCmd, stderr=subprocess.STDOUT )
except:
raise
subprocess.check_call(['git', 'pull', '--rebase'])
with open('modulelist.txt', 'a') as f:
f.write(packageName + "\t\t\t" + gitUpstreamRepo+"\n")
subprocess.check_call(['git', 'add', 'modulelist.txt'])
subprocess.check_call(['git', 'commit', '-m', 'eco added package ' + packageName + ' located at ' + gitUpstreamRepo])
subprocess.check_call(['git', 'pull', '--rebase'])
subprocess.check_call(['git', 'push' ])
os.chdir(curDir)
def createBranchFromTag( tag, branchName ):
'''Checkout the tag and create a branch using the tag as a starting point.'''
subprocess.check_call(['git', 'checkout', '-q', tag])
subprocess.check_call(['git', 'checkout', '-b', branchName])
def gitGetWorkingBranch( debug = False, verbose = False ):
'''See if the current directory is the top of an git working directory.
Returns a 3-tuple of ( url, branch, tag ), ( None, None, None ) on error.
For a valid git working dir, url must be a valid string, branch is the branch name or None if detached,
tag is either None or a tag name if HEAD refers to a tag name.'''
repo_url = None
repo_branch = None
repo_tag = None
try:
repoCmd = [ 'git', 'symbolic-ref', 'HEAD' ]
statusInfo = subprocess.check_output( repoCmd, stderr=subprocess.STDOUT, universal_newlines=True )
statusLines = statusInfo.splitlines()
if len(statusLines) > 0 and statusLines[0].startswith( 'refs/heads/' ):
repo_branch = statusLines[0].split('/')[2]
repoCmd = [ 'git', 'remote', '-v' ]
statusInfo = subprocess.check_output( repoCmd, stderr=subprocess.STDOUT, universal_newlines=True )
statusLines = statusInfo.splitlines()
for line in statusLines:
if line is None:
break
tokens = line.split()
if tokens[0] == 'origin': # Use remote 'origin' if found
repo_url = tokens[1]
break
if tokens[0].find('origin') >= 0: # Backup is last remote containing 'origin'
repo_url = tokens[1]
if repo_url is None: # If all else fails just use first remote
repo_url = tokens[1]
if repo_url:
# Remove any trailing path separator
( repoPath, repoPkg ) = os.path.split( repo_url )
if not repoPkg:
repo_url = repoPath
# See if HEAD corresponds to any tags
statusInfo = subprocess.check_output( [ 'git', 'name-rev', '--name-only', '--tags', 'HEAD' ], stderr=subprocess.STDOUT, universal_newlines=True )
statusLines = statusInfo.splitlines()
if len(statusLines) > 0:
# Just grab the first tag that matches
repo_tag = statusLines[0].split('^')[0]
except OSError as e:
if debug:
print(e)
pass
except subprocess.CalledProcessError as e:
if debug:
print(e)
pass
return ( repo_url, repo_branch, repo_tag )
def determinePathToGitRepo( packagePath, verbose = False ):
'''If the specified package is stored in GIT, then return the URL to the GIT repo. Otherwise, return None'''
# See if the package was listed in $TOOLS/eco_modulelist/modulelist.txt
packageName = os.path.split( packagePath )[-1]
if packageName in git_package2Location:
defRepoPath = git_package2Location[packageName]
if os.path.isdir( defRepoPath ):
if verbose:
print( "determinePathToGitRepo: Found %s in modulelist.txt. repo is %s" % (packagePath, defRepoPath) )
return defRepoPath
gitRoot = determineGitRoot()
if gitRoot != DEF_AFS_GIT_REPOS and gitRoot != DEF_AFS_GIT_REPOS2:
# See if we need to adjust the path from git_package2Location
if defRepoPath.startswith( DEF_AFS_GIT_REPOS ):
defRepoPath = defRepoPath.replace( DEF_AFS_GIT_REPOS, gitRoot )
if defRepoPath.startswith( DEF_AFS_GIT_REPOS2 ):
defRepoPath = defRepoPath.replace( DEF_AFS_GIT_REPOS2, gitRoot )
if verbose:
print( "determinePathToGitRepo: %s repo is %s" % (packagePath, defRepoPath) )
return defRepoPath
# Check under the root of the git repo area for a bare repo w/ the right name
gitRoot = determineGitRoot()
gitPackageDir = packageName + ".git"
gitPackagePath = packagePath + ".git"
if not os.path.isdir( DEF_CVS_ROOT ) and gitRoot:
# Must be offsite, assume gitRoot and an EPICS module path
defRepoPath = os.path.join( gitRoot, 'package/epics/modules', gitPackageDir )
if verbose:
print( "determinePathToGitRepo: Offsite %s repo is %s" % (packagePath, defRepoPath) )
return defRepoPath
for dirPath, dirs, files in os.walk( gitRoot, topdown=True ):
if len( dirs ) == 0:
continue
for dir in dirs[:]:
if dir == 'from-svn' or dir == 'from-cvs':
# Remove from list so we don't search these import directories
dirs.remove( dir )
continue
if os.path.isdir( os.path.join( dirPath, gitPackagePath ) ):
defRepoPath = os.path.join( dirPath, gitPackagePath )
if verbose:
print( "determinePathToGitRepo: os walk %s repo is %s" % (packagePath, defRepoPath) )
return defRepoPath
if dir == gitPackagePath:
defRepoPath = os.path.join( dirPath, dir )
if verbose:
print( "determinePathToGitRepo: oswalk %s repo is %s" % (packagePath, defRepoPath) )
return defRepoPath
if dir.endswith( ".git" ):
# Remove from list so we don't search recursively
dirs.remove( dir )
# Didn't find a match in eco_modulelist or git paths.
# Check for an svn package
(svn_url, svn_path, svn_tag) = svnFindPackageRelease( packagePath, tag = None, verbose=verbose )
if svn_url:
if verbose:
print( "determinePathToGitRepo: svn %s repo is %s" % (packagePath, svn_url) )
return svn_url
if verbose:
print( "determinePathToGitRepo: Unable to determine %s repo" % packagePath )
return None
def gitFindPackageRelease( packageSpec, tag, debug = False, verbose = False ):
(repo_url, repo_tag) = (None, None)
if verbose:
print("gitFindPackageRelease( packageSpec=%s, tag=%s )" % ( packageSpec, tag ))
if tag:
packagePath = packageSpec
else:
(packagePath, tag) = os.path.split( packageSpec )
if tag:
packageName = os.path.split( packagePath )[1]
else:
packageName = packagePath
if verbose:
print("gitFindPackageRelease: packageName=%s, packagePath=%s" % ( packageName, packagePath ))
# See if the package was listed in $TOOLS/eco_modulelist/modulelist.txt
if not packageName in git_package2Location and not 'github.com' in DEF_GIT_REPO_PATH:
for url_root in [ DEF_GIT_MODULES_PATH, DEF_GIT_EXTENSIONS_PATH, DEF_GIT_EPICS_PATH, DEF_GIT_REPO_PATH ]:
if repo_url is not None:
break
for p in [ packageName, packagePath ]:
url_path = '%s/%s.git' % ( url_root, p )
(repo_sha, repo_tag) = gitGetRemoteTag( url_path, tag, verbose=verbose )
if repo_sha is not None:
repo_url = url_path
break
if packageName == packagePath:
break
if verbose:
if repo_url:
print("gitFindPackageRelease found %s/%s: url=%s, tag=%s" % ( packagePath, tag, repo_url, repo_tag ))
else:
print("gitFindPackageRelease Error: Cannot find %s/%s" % (packagePath, tag))
return (repo_url, repo_tag)
def git_get_versionFileName():
'''If git config has a value for ecotools.versionfile,
this routine returns it. If not, returns None'''
versionFileName = None
try:
git_output = git_check_output( "git rev-parse --show-toplevel" ).splitlines()
if len(git_output) >= 1:
# Try reading versionFileName from the new softlink .versionFile
versionFileName = os.readlink( os.path.join( git_output[0], '.versionFile' ) )
except:
pass
# Backup versionFileName is original git config setting ecotools.versionfile
try:
if versionFileName is None:
git_output = git_check_output( "git config --get ecotools.versionfile" ).splitlines()
if len(git_output) >= 1:
versionFileName = git_output[0]
except:
pass
return versionFileName
def gitGetVersion():
'''Run git --version and return the git version as a string
Returns None on error'''
version = None
try:
git_output = git_check_output( "git --version" ).splitlines()
if len(git_output) >= 1:
version = git_output[0].split()[-1]
except:
pass
return version
def gitGetVersionNumber():
'''Run git --version and return the git version as a float.
Each decimal point scales subsequent version components by 0.01
1.2.3.4 => 1.020304
Returns 0.0 on error'''
version = gitGetVersion()
if not version:
return 0.0
return VersionToRelNumber( version )