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