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