72e01224bf3d75e697479a59b4c2a7d50bab1c81
   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 incremental imports
  11#       - create tags
  12#       - instead of reading all files into a variable try to pipe from
  13#       - support p4 submit (hah!)
  14#       - don't hardcode the import to master
  15#
  16import os, string, sys, time
  17import marshal, popen2
  18
  19if len(sys.argv) != 2:
  20    print "usage: %s //depot/path[@revRange]" % sys.argv[0]
  21    print "\n    example:"
  22    print "    %s //depot/my/project/ -- to import everything"
  23    print "    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
  24    print ""
  25    print "    (a ... is not needed in the path p4 specification, it's added implicitly)"
  26    print ""
  27    sys.exit(1)
  28
  29prefix = sys.argv[1]
  30changeRange = ""
  31try:
  32    atIdx = prefix.index("@")
  33    changeRange = prefix[atIdx:]
  34    prefix = prefix[0:atIdx]
  35except ValueError:
  36    changeRange = ""
  37
  38if prefix.endswith("..."):
  39    prefix = prefix[:-3]
  40
  41if not prefix.endswith("/"):
  42    prefix += "/"
  43
  44def p4CmdList(cmd):
  45    pipe = os.popen("p4 -G %s" % cmd, "rb")
  46    result = []
  47    try:
  48        while True:
  49            entry = marshal.load(pipe)
  50            result.append(entry)
  51    except EOFError:
  52        pass
  53    pipe.close()
  54    return result
  55
  56def p4Cmd(cmd):
  57    list = p4CmdList(cmd)
  58    result = {}
  59    for entry in list:
  60        result.update(entry)
  61    return result;
  62
  63def getUserMap():
  64    users = {}
  65
  66    for output in p4CmdList("users"):
  67        if not output.has_key("User"):
  68            continue
  69        users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
  70    return users
  71
  72users = getUserMap()
  73
  74output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
  75
  76changes = []
  77for line in output:
  78    changeNum = line.split(" ")[1]
  79    changes.append(changeNum)
  80
  81changes.reverse()
  82
  83sys.stderr.write("\n")
  84
  85tz = - time.timezone / 36
  86
  87gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
  88
  89cnt = 1
  90for change in changes:
  91    description = p4Cmd("describe %s" % change)
  92
  93    sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
  94    cnt = cnt + 1
  95
  96    epoch = description["time"]
  97    author = description["user"]
  98
  99    gitStream.write("commit refs/heads/master\n")
 100    if author in users:
 101        gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
 102    else:
 103        gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
 104    gitStream.write("data <<EOT\n")
 105    gitStream.write(description["desc"])
 106    gitStream.write("EOT\n\n")
 107
 108    fnum = 0
 109    while description.has_key("depotFile%s" % fnum):
 110        path = description["depotFile%s" % fnum]
 111        if not path.startswith(prefix):
 112            print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
 113            fnum = fnum + 1
 114            continue
 115
 116        rev = description["rev%s" % fnum]
 117        depotPath = path + "#" + rev
 118        relPath = path[len(prefix):]
 119        action = description["action%s" % fnum]
 120
 121        if action == "delete":
 122            gitStream.write("D %s\n" % relPath)
 123        else:
 124            mode = 644
 125            if description["type%s" % fnum].startswith("x"):
 126                mode = 755
 127
 128            data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
 129
 130            gitStream.write("M %s inline %s\n" % (mode, relPath))
 131            gitStream.write("data %s\n" % len(data))
 132            gitStream.write(data)
 133            gitStream.write("\n")
 134
 135        fnum = fnum + 1
 136
 137    gitStream.write("\n")
 138
 139gitStream.close()
 140gitOutput.close()
 141gitError.close()
 142
 143print ""