Browse Source

Make it possible to specify the p4 changes to import through a text file (for debugging) and made various improvements to the branch/merge heuristic detection.

Signed-off-by: Simon Hausmann <hausmann@kde.org>
maint
Simon Hausmann 18 years ago
parent
commit
0563a4538a
  1. 96
      contrib/fast-import/p4-fast-export.py

96
contrib/fast-import/p4-fast-export.py

@ -13,14 +13,16 @@ import os, string, sys, time
import marshal, popen2, getopt import marshal, popen2, getopt


knownBranches = set() knownBranches = set()
committedChanges = set()
branch = "refs/heads/master" branch = "refs/heads/master"
globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read() globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
detectBranches = False detectBranches = False
changesFile = ""
if len(globalPrefix) != 0: if len(globalPrefix) != 0:
globalPrefix = globalPrefix[:-1] globalPrefix = globalPrefix[:-1]


try: try:
opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches" ]) opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
except getopt.GetoptError: except getopt.GetoptError:
print "fixme, syntax error" print "fixme, syntax error"
sys.exit(1) sys.exit(1)
@ -30,6 +32,8 @@ for o, a in opts:
branch = "refs/heads/" + a branch = "refs/heads/" + a
elif o == "--detect-branches": elif o == "--detect-branches":
detectBranches = True detectBranches = True
elif o == "--changesfile":
changesFile = a


if len(args) == 0 and len(globalPrefix) != 0: if len(args) == 0 and len(globalPrefix) != 0:
print "[using previously specified depot path %s]" % globalPrefix print "[using previously specified depot path %s]" % globalPrefix
@ -53,7 +57,7 @@ changeRange = ""
revision = "" revision = ""
users = {} users = {}
initialParent = "" initialParent = ""
lastChange = "" lastChange = 0
initialTag = "" initialTag = ""


if globalPrefix.find("@") != -1: if globalPrefix.find("@") != -1:
@ -104,6 +108,7 @@ def extractFilesFromCommit(commit):
path = commit["depotFile%s" % fnum] path = commit["depotFile%s" % fnum]
if not path.startswith(globalPrefix): if not path.startswith(globalPrefix):
print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change) print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
fnum = fnum + 1
continue continue


file = {} file = {}
@ -115,7 +120,15 @@ def extractFilesFromCommit(commit):
fnum = fnum + 1 fnum = fnum + 1
return files return files


def isSubPathOf(first, second):
if not first.startswith(second):
return False
if first == second:
return True
return first[len(second)] == "/"

def branchesForCommit(files): def branchesForCommit(files):
global knownBranches
branches = set() branches = set()


for file in files: for file in files:
@ -123,9 +136,10 @@ def branchesForCommit(files):
# strip off the filename # strip off the filename
relativePath = relativePath[0:relativePath.rfind("/")] relativePath = relativePath[0:relativePath.rfind("/")]


if len(branches) == 0: # if len(branches) == 0:
branches.add(relativePath) # branches.add(relativePath)
continue # knownBranches.add(relativePath)
# continue


###### this needs more testing :) ###### this needs more testing :)
knownBranch = False knownBranch = False
@ -133,15 +147,32 @@ def branchesForCommit(files):
if relativePath == branch: if relativePath == branch:
knownBranch = True knownBranch = True
break break
if relativePath.startswith(branch): # if relativePath.startswith(branch):
if isSubPathOf(relativePath, branch):
knownBranch = True knownBranch = True
break break
if branch.startswith(relativePath): # if branch.startswith(relativePath):
if isSubPathOf(branch, relativePath):
branches.remove(branch) branches.remove(branch)
break break


if not knownBranch: if knownBranch:
continue

for branch in knownBranches:
#if relativePath.startswith(branch):
if isSubPathOf(relativePath, branch):
if len(branches) == 0:
relativePath = branch
else:
knownBranch = True
break

if knownBranch:
continue

branches.add(relativePath) branches.add(relativePath)
knownBranches.add(relativePath)


return branches return branches


@ -149,12 +180,14 @@ def commit(details, files, branch, branchPrefix):
global initialParent global initialParent
global users global users
global lastChange global lastChange
global committedChanges


epoch = details["time"] epoch = details["time"]
author = details["user"] author = details["user"]


gitStream.write("commit %s\n" % branch) gitStream.write("commit %s\n" % branch)
gitStream.write("mark :%s\n" % details["change"]) gitStream.write("mark :%s\n" % details["change"])
committedChanges.add(int(details["change"]))
committer = "" committer = ""
if author in users: if author in users:
committer = "%s %s %s" % (users[author], epoch, tz) committer = "%s %s %s" % (users[author], epoch, tz)
@ -173,9 +206,11 @@ def commit(details, files, branch, branchPrefix):
initialParent = "" initialParent = ""


#mergedBranches = set() #mergedBranches = set()
merge = 0 merges = set()


for file in files: for file in files:
if lastChange == 0:
continue
path = file["path"] path = file["path"]
if not path.startswith(branchPrefix): if not path.startswith(branchPrefix):
continue continue
@ -195,9 +230,14 @@ def commit(details, files, branch, branchPrefix):
print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action) print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
sys.exit(1); sys.exit(1);


if not log["how0,0"].endswith(" from"): branchAction = log["how0,0"]
print "eek! file %s was not branched but instead: %s" % (depotPath, log["how0,0"]) # if branchAction == "branch into" or branchAction == "ignored":
sys.exit(1); # continue # ignore for branching

if not branchAction.endswith(" from"):
continue # ignore for branching
# print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
# sys.exit(1);


source = log["file0,0"] source = log["file0,0"]
if source.startswith(branchPrefix): if source.startswith(branchPrefix):
@ -212,8 +252,7 @@ def commit(details, files, branch, branchPrefix):
sourceLog = sourceLog[0] sourceLog = sourceLog[0]


change = int(sourceLog["change0"]) change = int(sourceLog["change0"])
if change > merge: merges.add(change)
merge = change


# relPath = source[len(globalPrefix):] # relPath = source[len(globalPrefix):]
# #
@ -223,13 +262,14 @@ def commit(details, files, branch, branchPrefix):
# mergedBranches.add(branch) # mergedBranches.add(branch)
# break # break


if merge != 0: for merge in merges:
if merge in committedChanges:
gitStream.write("merge :%s\n" % merge) gitStream.write("merge :%s\n" % merge)


for file in files: for file in files:
path = file["path"] path = file["path"]
if not path.startswith(branchPrefix): if not path.startswith(branchPrefix):
print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, change) print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
continue continue
rev = file["rev"] rev = file["rev"]
depotPath = path + "#" + rev depotPath = path + "#" + rev
@ -252,7 +292,7 @@ def commit(details, files, branch, branchPrefix):


gitStream.write("\n") gitStream.write("\n")


lastChange = details["change"] lastChange = int(details["change"])


def getUserMap(): def getUserMap():
users = {} users = {}
@ -321,10 +361,22 @@ if len(revision) > 0:
except: except:
print gitError.read() print gitError.read()


else:
changes = []

if len(changesFile) > 0:
output = open(changesFile).readlines()
changeSet = set()
for line in output:
changeSet.add(int(line))

for change in changeSet:
changes.append(change)

changes.sort()
else: else:
output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines() output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()


changes = []
for line in output: for line in output:
changeNum = line.split(" ")[1] changeNum = line.split(" ")[1]
changes.append(changeNum) changes.append(changeNum)
@ -343,7 +395,7 @@ else:
sys.stdout.flush() sys.stdout.flush()
cnt = cnt + 1 cnt = cnt + 1


try: # try:
files = extractFilesFromCommit(description) files = extractFilesFromCommit(description)
if detectBranches: if detectBranches:
for branch in branchesForCommit(files): for branch in branchesForCommit(files):
@ -353,9 +405,9 @@ else:
commit(description, files, branch, branchPrefix) commit(description, files, branch, branchPrefix)
else: else:
commit(description, files, branch, globalPrefix) commit(description, files, branch, globalPrefix)
except: # except:
print gitError.read() # print gitError.read()
sys.exit(1) # sys.exit(1)


print "" print ""



Loading…
Cancel
Save