61b9e67e54b4bdb3808ed8c02eb668a0cb0db1cf
   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
  15branch = "refs/heads/master"
  16prefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
  17if len(prefix) != 0:
  18    prefix = prefix[:-1]
  19
  20try:
  21    opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ])
  22except getopt.GetoptError:
  23    print "fixme, syntax error"
  24    sys.exit(1)
  25
  26for o, a in opts:
  27    if o == "--branch":
  28        branch = "refs/heads/" + a
  29
  30if len(args) == 0 and len(prefix) != 0:
  31    print "[using previously specified depot path %s]" % prefix
  32elif len(args) != 1:
  33    print "usage: %s //depot/path[@revRange]" % sys.argv[0]
  34    print "\n    example:"
  35    print "    %s //depot/my/project/ -- to import the current head"
  36    print "    %s //depot/my/project/@all -- to import everything"
  37    print "    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
  38    print ""
  39    print "    (a ... is not needed in the path p4 specification, it's added implicitly)"
  40    print ""
  41    sys.exit(1)
  42else:
  43    if len(prefix) != 0 and prefix != args[0]:
  44        print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix, args[0])
  45        sys.exit(1)
  46    prefix = args[0]
  47
  48changeRange = ""
  49revision = ""
  50users = {}
  51initialParent = ""
  52lastChange = ""
  53initialTag = ""
  54
  55if prefix.find("@") != -1:
  56    atIdx = prefix.index("@")
  57    changeRange = prefix[atIdx:]
  58    if changeRange == "@all":
  59        changeRange = ""
  60    elif changeRange.find(",") == -1:
  61        revision = changeRange
  62        changeRange = ""
  63    prefix = prefix[0:atIdx]
  64elif prefix.find("#") != -1:
  65    hashIdx = prefix.index("#")
  66    revision = prefix[hashIdx:]
  67    prefix = prefix[0:hashIdx]
  68elif len(previousDepotPath) == 0:
  69    revision = "#head"
  70
  71if prefix.endswith("..."):
  72    prefix = prefix[:-3]
  73
  74if not prefix.endswith("/"):
  75    prefix += "/"
  76
  77def p4CmdList(cmd):
  78    pipe = os.popen("p4 -G %s" % cmd, "rb")
  79    result = []
  80    try:
  81        while True:
  82            entry = marshal.load(pipe)
  83            result.append(entry)
  84    except EOFError:
  85        pass
  86    pipe.close()
  87    return result
  88
  89def p4Cmd(cmd):
  90    list = p4CmdList(cmd)
  91    result = {}
  92    for entry in list:
  93        result.update(entry)
  94    return result;
  95
  96def commit(details):
  97    global initialParent
  98    global users
  99    global lastChange
 100
 101    epoch = details["time"]
 102    author = details["user"]
 103
 104    gitStream.write("commit %s\n" % branch)
 105    committer = ""
 106    if author in users:
 107        committer = "%s %s %s" % (users[author], epoch, tz)
 108    else:
 109        committer = "%s <a@b> %s %s" % (author, epoch, tz)
 110
 111    gitStream.write("committer %s\n" % committer)
 112
 113    gitStream.write("data <<EOT\n")
 114    gitStream.write(details["desc"])
 115    gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
 116    gitStream.write("EOT\n\n")
 117
 118    if len(initialParent) > 0:
 119        gitStream.write("from %s\n" % initialParent)
 120        initialParent = ""
 121
 122    fnum = 0
 123    while details.has_key("depotFile%s" % fnum):
 124        path = details["depotFile%s" % fnum]
 125        if not path.startswith(prefix):
 126            print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
 127            fnum = fnum + 1
 128            continue
 129
 130        rev = details["rev%s" % fnum]
 131        depotPath = path + "#" + rev
 132        relPath = path[len(prefix):]
 133        action = details["action%s" % fnum]
 134
 135        if action == "delete":
 136            gitStream.write("D %s\n" % relPath)
 137        else:
 138            mode = 644
 139            if details["type%s" % fnum].startswith("x"):
 140                mode = 755
 141
 142            data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
 143
 144            gitStream.write("M %s inline %s\n" % (mode, relPath))
 145            gitStream.write("data %s\n" % len(data))
 146            gitStream.write(data)
 147            gitStream.write("\n")
 148
 149        fnum = fnum + 1
 150
 151    gitStream.write("\n")
 152
 153    lastChange = details["change"]
 154
 155def getUserMap():
 156    users = {}
 157
 158    for output in p4CmdList("users"):
 159        if not output.has_key("User"):
 160            continue
 161        users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
 162    return users
 163
 164users = getUserMap()
 165
 166if len(changeRange) == 0:
 167    try:
 168        sout, sin, serr = popen2.popen3("git-name-rev --tags `git-rev-parse %s`" % branch)
 169        output = sout.read()
 170        if output.endswith("\n"):
 171            output = output[:-1]
 172        tagIdx = output.index(" tags/p4/")
 173        caretIdx = output.find("^")
 174        endPos = len(output)
 175        if caretIdx != -1:
 176            endPos = caretIdx
 177        rev = int(output[tagIdx + 9 : endPos]) + 1
 178        changeRange = "@%s,#head" % rev
 179        initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
 180        initialTag = "p4/%s" % (int(rev) - 1)
 181    except:
 182        pass
 183
 184sys.stderr.write("\n")
 185
 186tz = - time.timezone / 36
 187tzsign = ("%s" % tz)[0]
 188if tzsign != '+' and tzsign != '-':
 189    tz = "+" + ("%s" % tz)
 190
 191gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
 192
 193if len(revision) > 0:
 194    print "Doing initial import of %s from revision %s" % (prefix, revision)
 195
 196    details = { "user" : "git perforce import user", "time" : int(time.time()) }
 197    details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
 198    details["change"] = revision
 199    newestRevision = 0
 200
 201    fileCnt = 0
 202    for info in p4CmdList("files %s...%s" % (prefix, revision)):
 203        change = int(info["change"])
 204        if change > newestRevision:
 205            newestRevision = change
 206
 207        if info["action"] == "delete":
 208            continue
 209
 210        for prop in [ "depotFile", "rev", "action", "type" ]:
 211            details["%s%s" % (prop, fileCnt)] = info[prop]
 212
 213        fileCnt = fileCnt + 1
 214
 215    details["change"] = newestRevision
 216
 217    try:
 218        commit(details)
 219    except:
 220        print gitError.read()
 221
 222else:
 223    output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
 224
 225    changes = []
 226    for line in output:
 227        changeNum = line.split(" ")[1]
 228        changes.append(changeNum)
 229
 230    changes.reverse()
 231
 232    if len(changes) == 0:
 233        print "no changes to import!"
 234        sys.exit(1)
 235
 236    cnt = 1
 237    for change in changes:
 238        description = p4Cmd("describe %s" % change)
 239
 240        sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 241        sys.stdout.flush()
 242        cnt = cnt + 1
 243
 244        try:
 245            commit(description)
 246        except:
 247            print gitError.read()
 248            sys.exit(1)
 249
 250print ""
 251
 252gitStream.write("reset refs/tags/p4/%s\n" % lastChange)
 253gitStream.write("from %s\n\n" % branch);
 254
 255
 256gitStream.close()
 257gitOutput.close()
 258gitError.close()
 259
 260os.popen("git-repo-config p4.depotpath %s" % prefix).read()
 261if len(initialTag) > 0:
 262    os.popen("git tag -d %s" % initialTag).read()
 263
 264sys.exit(0)