contrib / fast-import / p4-fast-export.pyon commit Added p4 delete behavioural emulation as todo item. (9c9fec3)
   1#!/usr/bin/python
   2#
   3# p4-fast-export.py
   4#
   5# Author: Simon Hausmann <hausmann@kde.org>
   6# License: MIT <http://www.opensource.org/licenses/mit-license.php>
   7#
   8# TODO:
   9#       - support integrations (at least p4i)
  10#       - support p4 submit (hah!)
  11#       - emulate p4's delete behavior: if a directory becomes empty delete it. continue
  12#         with parent dir until non-empty dir is found.
  13#
  14import os, string, sys, time
  15import marshal, popen2, getopt
  16from sets import Set;
  17
  18knownBranches = Set()
  19committedChanges = Set()
  20branch = "refs/heads/master"
  21globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
  22detectBranches = False
  23changesFile = ""
  24if len(globalPrefix) != 0:
  25    globalPrefix = globalPrefix[:-1]
  26
  27try:
  28    opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
  29except getopt.GetoptError:
  30    print "fixme, syntax error"
  31    sys.exit(1)
  32
  33for o, a in opts:
  34    if o == "--branch":
  35        branch = "refs/heads/" + a
  36    elif o == "--detect-branches":
  37        detectBranches = True
  38    elif o == "--changesfile":
  39        changesFile = a
  40
  41if len(args) == 0 and len(globalPrefix) != 0:
  42    print "[using previously specified depot path %s]" % globalPrefix
  43elif len(args) != 1:
  44    print "usage: %s //depot/path[@revRange]" % sys.argv[0]
  45    print "\n    example:"
  46    print "    %s //depot/my/project/ -- to import the current head"
  47    print "    %s //depot/my/project/@all -- to import everything"
  48    print "    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
  49    print ""
  50    print "    (a ... is not needed in the path p4 specification, it's added implicitly)"
  51    print ""
  52    sys.exit(1)
  53else:
  54    if len(globalPrefix) != 0 and globalPrefix != args[0]:
  55        print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix, args[0])
  56        sys.exit(1)
  57    globalPrefix = args[0]
  58
  59changeRange = ""
  60revision = ""
  61users = {}
  62initialParent = ""
  63lastChange = 0
  64initialTag = ""
  65
  66if globalPrefix.find("@") != -1:
  67    atIdx = globalPrefix.index("@")
  68    changeRange = globalPrefix[atIdx:]
  69    if changeRange == "@all":
  70        changeRange = ""
  71    elif changeRange.find(",") == -1:
  72        revision = changeRange
  73        changeRange = ""
  74    globalPrefix = globalPrefix[0:atIdx]
  75elif globalPrefix.find("#") != -1:
  76    hashIdx = globalPrefix.index("#")
  77    revision = globalPrefix[hashIdx:]
  78    globalPrefix = globalPrefix[0:hashIdx]
  79elif len(previousDepotPath) == 0:
  80    revision = "#head"
  81
  82if globalPrefix.endswith("..."):
  83    globalPrefix = globalPrefix[:-3]
  84
  85if not globalPrefix.endswith("/"):
  86    globalPrefix += "/"
  87
  88def p4CmdList(cmd):
  89    pipe = os.popen("p4 -G %s" % cmd, "rb")
  90    result = []
  91    try:
  92        while True:
  93            entry = marshal.load(pipe)
  94            result.append(entry)
  95    except EOFError:
  96        pass
  97    pipe.close()
  98    return result
  99
 100def p4Cmd(cmd):
 101    list = p4CmdList(cmd)
 102    result = {}
 103    for entry in list:
 104        result.update(entry)
 105    return result;
 106
 107def extractFilesFromCommit(commit):
 108    files = []
 109    fnum = 0
 110    while commit.has_key("depotFile%s" % fnum):
 111        path =  commit["depotFile%s" % fnum]
 112        if not path.startswith(globalPrefix):
 113            print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
 114            fnum = fnum + 1
 115            continue
 116
 117        file = {}
 118        file["path"] = path
 119        file["rev"] = commit["rev%s" % fnum]
 120        file["action"] = commit["action%s" % fnum]
 121        file["type"] = commit["type%s" % fnum]
 122        files.append(file)
 123        fnum = fnum + 1
 124    return files
 125
 126def isSubPathOf(first, second):
 127    if not first.startswith(second):
 128        return False
 129    if first == second:
 130        return True
 131    return first[len(second)] == "/"
 132
 133def branchesForCommit(files):
 134    global knownBranches
 135    branches = Set()
 136
 137    for file in files:
 138        relativePath = file["path"][len(globalPrefix):]
 139        # strip off the filename
 140        relativePath = relativePath[0:relativePath.rfind("/")]
 141
 142#        if len(branches) == 0:
 143#            branches.add(relativePath)
 144#            knownBranches.add(relativePath)
 145#            continue
 146
 147        ###### this needs more testing :)
 148        knownBranch = False
 149        for branch in branches:
 150            if relativePath == branch:
 151                knownBranch = True
 152                break
 153#            if relativePath.startswith(branch):
 154            if isSubPathOf(relativePath, branch):
 155                knownBranch = True
 156                break
 157#            if branch.startswith(relativePath):
 158            if isSubPathOf(branch, relativePath):
 159                branches.remove(branch)
 160                break
 161
 162        if knownBranch:
 163            continue
 164
 165        for branch in knownBranches:
 166            #if relativePath.startswith(branch):
 167            if isSubPathOf(relativePath, branch):
 168                if len(branches) == 0:
 169                    relativePath = branch
 170                else:
 171                    knownBranch = True
 172                break
 173
 174        if knownBranch:
 175            continue
 176
 177        branches.add(relativePath)
 178        knownBranches.add(relativePath)
 179
 180    return branches
 181
 182def commit(details, files, branch, branchPrefix):
 183    global initialParent
 184    global users
 185    global lastChange
 186    global committedChanges
 187
 188    epoch = details["time"]
 189    author = details["user"]
 190
 191    gitStream.write("commit %s\n" % branch)
 192    gitStream.write("mark :%s\n" % details["change"])
 193    committedChanges.add(int(details["change"]))
 194    committer = ""
 195    if author in users:
 196        committer = "%s %s %s" % (users[author], epoch, tz)
 197    else:
 198        committer = "%s <a@b> %s %s" % (author, epoch, tz)
 199
 200    gitStream.write("committer %s\n" % committer)
 201
 202    gitStream.write("data <<EOT\n")
 203    gitStream.write(details["desc"])
 204    gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
 205    gitStream.write("EOT\n\n")
 206
 207    if len(initialParent) > 0:
 208        gitStream.write("from %s\n" % initialParent)
 209        initialParent = ""
 210
 211    #mergedBranches = Set()
 212    merges = Set()
 213
 214    for file in files:
 215        if lastChange == 0 or not detectBranches:
 216            continue
 217        path = file["path"]
 218        if not path.startswith(branchPrefix):
 219            continue
 220        action = file["action"]
 221        if action != "integrate" and action != "branch":
 222            continue
 223        rev = file["rev"]
 224        depotPath = path + "#" + rev
 225
 226        log = p4CmdList("filelog \"%s\"" % depotPath)
 227        if len(log) != 1:
 228            print "eek! I got confused by the filelog of %s" % depotPath
 229            sys.exit(1);
 230
 231        log = log[0]
 232        if log["action0"] != action:
 233            print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath, log["action0"], action)
 234            sys.exit(1);
 235
 236        branchAction = log["how0,0"]
 237#        if branchAction == "branch into" or branchAction == "ignored":
 238#            continue # ignore for branching
 239
 240        if not branchAction.endswith(" from"):
 241            continue # ignore for branching
 242#            print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
 243#            sys.exit(1);
 244
 245        source = log["file0,0"]
 246        if source.startswith(branchPrefix):
 247            continue
 248
 249        lastSourceRev = log["erev0,0"]
 250
 251        sourceLog = p4CmdList("filelog -m 1 \"%s%s\"" % (source, lastSourceRev))
 252        if len(sourceLog) != 1:
 253            print "eek! I got confused by the source filelog of %s%s" % (source, lastSourceRev)
 254            sys.exit(1);
 255        sourceLog = sourceLog[0]
 256
 257        change = int(sourceLog["change0"])
 258        merges.add(change)
 259
 260#        relPath = source[len(globalPrefix):]
 261#
 262#        for branch in knownBranches:
 263#            if relPath.startswith(branch) and branch not in mergedBranches:
 264#                gitStream.write("merge refs/heads/%s\n" % branch)
 265#                mergedBranches.add(branch)
 266#                break
 267
 268    for merge in merges:
 269        if merge in committedChanges:
 270            gitStream.write("merge :%s\n" % merge)
 271
 272    for file in files:
 273        path = file["path"]
 274        if not path.startswith(branchPrefix):
 275            print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
 276            continue
 277        rev = file["rev"]
 278        depotPath = path + "#" + rev
 279        relPath = path[len(branchPrefix):]
 280        action = file["action"]
 281
 282        if action == "delete":
 283            gitStream.write("D %s\n" % relPath)
 284        else:
 285            mode = 644
 286            if file["type"].startswith("x"):
 287                mode = 755
 288
 289            data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
 290
 291            gitStream.write("M %s inline %s\n" % (mode, relPath))
 292            gitStream.write("data %s\n" % len(data))
 293            gitStream.write(data)
 294            gitStream.write("\n")
 295
 296    gitStream.write("\n")
 297
 298    lastChange = int(details["change"])
 299
 300def getUserMap():
 301    users = {}
 302
 303    for output in p4CmdList("users"):
 304        if not output.has_key("User"):
 305            continue
 306        users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
 307    return users
 308
 309users = getUserMap()
 310
 311if len(changeRange) == 0:
 312    try:
 313        sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
 314        output = sout.read()
 315        if output.endswith("\n"):
 316            output = output[:-1]
 317        tagIdx = output.index(" tags/p4/")
 318        caretIdx = output.find("^")
 319        endPos = len(output)
 320        if caretIdx != -1:
 321            endPos = caretIdx
 322        rev = int(output[tagIdx + 9 : endPos]) + 1
 323        changeRange = "@%s,#head" % rev
 324        initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
 325        initialTag = "p4/%s" % (int(rev) - 1)
 326    except:
 327        pass
 328
 329sys.stderr.write("\n")
 330
 331tz = - time.timezone / 36
 332tzsign = ("%s" % tz)[0]
 333if tzsign != '+' and tzsign != '-':
 334    tz = "+" + ("%s" % tz)
 335
 336gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
 337
 338if len(revision) > 0:
 339    print "Doing initial import of %s from revision %s" % (globalPrefix, revision)
 340
 341    details = { "user" : "git perforce import user", "time" : int(time.time()) }
 342    details["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix, revision)
 343    details["change"] = revision
 344    newestRevision = 0
 345
 346    fileCnt = 0
 347    for info in p4CmdList("files %s...%s" % (globalPrefix, revision)):
 348        change = int(info["change"])
 349        if change > newestRevision:
 350            newestRevision = change
 351
 352        if info["action"] == "delete":
 353            continue
 354
 355        for prop in [ "depotFile", "rev", "action", "type" ]:
 356            details["%s%s" % (prop, fileCnt)] = info[prop]
 357
 358        fileCnt = fileCnt + 1
 359
 360    details["change"] = newestRevision
 361
 362    try:
 363        commit(details, extractFilesFromCommit(details), branch, globalPrefix)
 364    except:
 365        print gitError.read()
 366
 367else:
 368    changes = []
 369
 370    if len(changesFile) > 0:
 371        output = open(changesFile).readlines()
 372        changeSet = Set()
 373        for line in output:
 374            changeSet.add(int(line))
 375
 376        for change in changeSet:
 377            changes.append(change)
 378
 379        changes.sort()
 380    else:
 381        output = os.popen("p4 changes %s...%s" % (globalPrefix, changeRange)).readlines()
 382
 383        for line in output:
 384            changeNum = line.split(" ")[1]
 385            changes.append(changeNum)
 386
 387        changes.reverse()
 388
 389    if len(changes) == 0:
 390        print "no changes to import!"
 391        sys.exit(1)
 392
 393    cnt = 1
 394    for change in changes:
 395        description = p4Cmd("describe %s" % change)
 396
 397        sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 398        sys.stdout.flush()
 399        cnt = cnt + 1
 400
 401#        try:
 402        files = extractFilesFromCommit(description)
 403        if detectBranches:
 404            for branch in branchesForCommit(files):
 405                knownBranches.add(branch)
 406                branchPrefix = globalPrefix + branch + "/"
 407                branch = "refs/heads/" + branch
 408                commit(description, files, branch, branchPrefix)
 409        else:
 410            commit(description, files, branch, globalPrefix)
 411#        except:
 412#            print gitError.read()
 413#            sys.exit(1)
 414
 415print ""
 416
 417gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
 418gitStream.write("from %s\n\n" % branch);
 419
 420
 421gitStream.close()
 422gitOutput.close()
 423gitError.close()
 424
 425os.popen("git-repo-config p4.depotpath %s" % globalPrefix).read()
 426if len(initialTag) > 0:
 427    os.popen("git tag -d %s" % initialTag).read()
 428
 429sys.exit(0)