in git-p4.py [0:0]
def run(self, args):
if self.importIntoRemotes:
self.refPrefix = "refs/remotes/p4/"
else:
self.refPrefix = "refs/heads/p4/"
self.sync_origin_only()
branch_arg_given = bool(self.branch)
if len(self.branch) == 0:
self.branch = self.refPrefix + "master"
if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
system(["git", "update-ref", self.branch, "refs/heads/p4"])
system(["git", "branch", "-D", "p4"])
# accept either the command-line option, or the configuration variable
if self.useClientSpec:
# will use this after clone to set the variable
self.useClientSpec_from_options = True
else:
if gitConfigBool("git-p4.useclientspec"):
self.useClientSpec = True
if self.useClientSpec:
self.clientSpecDirs = getClientSpec()
# TODO: should always look at previous commits,
# merge with previous imports, if possible.
if args == []:
if self.hasOrigin:
createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
# branches holds mapping from branch name to sha1
branches = p4BranchesInGit(self.importIntoRemotes)
# restrict to just this one, disabling detect-branches
if branch_arg_given:
short = shortP4Ref(self.branch, self.importIntoRemotes)
if short in branches:
self.p4BranchesInGit = [short]
elif self.branch.startswith('refs/') and \
branchExists(self.branch) and \
'[git-p4:' in extractLogMessageFromGitCommit(self.branch):
self.p4BranchesInGit = [self.branch]
else:
self.p4BranchesInGit = branches.keys()
if len(self.p4BranchesInGit) > 1:
if not self.silent:
print("Importing from/into multiple branches")
self.detectBranches = True
for branch in branches.keys():
self.initialParents[self.refPrefix + branch] = \
branches[branch]
if self.verbose:
print("branches: %s" % self.p4BranchesInGit)
p4Change = 0
for branch in self.p4BranchesInGit:
logMsg = extractLogMessageFromGitCommit(fullP4Ref(branch,
self.importIntoRemotes))
settings = extractSettingsGitLog(logMsg)
self.readOptions(settings)
if 'depot-paths' in settings and 'change' in settings:
change = int(settings['change']) + 1
p4Change = max(p4Change, change)
depotPaths = sorted(settings['depot-paths'])
if self.previousDepotPaths == []:
self.previousDepotPaths = depotPaths
else:
paths = []
for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
prev_list = prev.split("/")
cur_list = cur.split("/")
for i in range(0, min(len(cur_list), len(prev_list))):
if cur_list[i] != prev_list[i]:
i = i - 1
break
paths.append("/".join(cur_list[:i + 1]))
self.previousDepotPaths = paths
if p4Change > 0:
self.depotPaths = sorted(self.previousDepotPaths)
self.changeRange = "@%s,#head" % p4Change
if not self.silent and not self.detectBranches:
print("Performing incremental import into %s git branch" % self.branch)
self.branch = fullP4Ref(self.branch, self.importIntoRemotes)
if len(args) == 0 and self.depotPaths:
if not self.silent:
print("Depot paths: %s" % ' '.join(self.depotPaths))
else:
if self.depotPaths and self.depotPaths != args:
print("previous import used depot path %s and now %s was specified. "
"This doesn't work!" % (' '.join(self.depotPaths),
' '.join(args)))
sys.exit(1)
self.depotPaths = sorted(args)
revision = ""
self.users = {}
# Make sure no revision specifiers are used when --changesfile
# is specified.
bad_changesfile = False
if len(self.changesFile) > 0:
for p in self.depotPaths:
if p.find("@") >= 0 or p.find("#") >= 0:
bad_changesfile = True
break
if bad_changesfile:
die("Option --changesfile is incompatible with revision specifiers")
newPaths = []
for p in self.depotPaths:
if p.find("@") != -1:
atIdx = p.index("@")
self.changeRange = p[atIdx:]
if self.changeRange == "@all":
self.changeRange = ""
elif ',' not in self.changeRange:
revision = self.changeRange
self.changeRange = ""
p = p[:atIdx]
elif p.find("#") != -1:
hashIdx = p.index("#")
revision = p[hashIdx:]
p = p[:hashIdx]
elif self.previousDepotPaths == []:
# pay attention to changesfile, if given, else import
# the entire p4 tree at the head revision
if len(self.changesFile) == 0:
revision = "#head"
p = re.sub(r"\.\.\.$", "", p)
if not p.endswith("/"):
p += "/"
newPaths.append(p)
self.depotPaths = newPaths
# --detect-branches may change this for each branch
self.branchPrefixes = self.depotPaths
self.loadUserMapFromCache()
self.labels = {}
if self.detectLabels:
self.getLabels()
if self.detectBranches:
# FIXME - what's a P4 projectName ?
self.projectName = self.guessProjectName()
if self.hasOrigin:
self.getBranchMappingFromGitBranches()
else:
self.getBranchMapping()
if self.verbose:
print("p4-git branches: %s" % self.p4BranchesInGit)
print("initial parents: %s" % self.initialParents)
for b in self.p4BranchesInGit:
if b != "master":
# FIXME
b = b[len(self.projectName):]
self.createdBranches.add(b)
p4_check_access()
self.openStreams()
err = None
try:
if revision:
self.importHeadRevision(revision)
else:
self.importRevisions(args, branch_arg_given)
if gitConfigBool("git-p4.importLabels"):
self.importLabels = True
if self.importLabels:
p4Labels = getP4Labels(self.depotPaths)
gitTags = getGitTags()
missingP4Labels = p4Labels - gitTags
self.importP4Labels(self.gitStream, missingP4Labels)
except P4CommandException as e:
err = e
finally:
self.closeStreams()
if err:
die(str(err))
# Cleanup temporary branches created during import
if self.tempBranches != []:
for branch in self.tempBranches:
read_pipe(["git", "update-ref", "-d", branch])
if len(read_pipe(["git", "for-each-ref", self.tempBranchLocation])) > 0:
die("There are unexpected temporary branches")
# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
# a convenient shortcut refname "p4".
if self.importIntoRemotes:
head_ref = self.refPrefix + "HEAD"
if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
system(["git", "symbolic-ref", head_ref, self.branch])
return True