contrib / fast-import / p4-fast-export.pyon commit Instead of parsing the output of "p4 users" use the python objects of "p4 -G users". (a39811b)
   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 not prefix.endswith("/"):
  39    prefix += "/"
  40
  41def p4CmdList(cmd):
  42    pipe = os.popen("p4 -G %s" % cmd, "rb")
  43    result = []
  44    try:
  45        while True:
  46            entry = marshal.load(pipe)
  47            result.append(entry)
  48    except EOFError:
  49        pass
  50    pipe.close()
  51    return result
  52
  53def p4Cmd(cmd):
  54    list = p4CmdList(cmd)
  55    result = {}
  56    for entry in list:
  57        result.update(entry)
  58    return result;
  59
  60def describe(change):
  61    describeOutput = p4Cmd("describe %s" % change)
  62
  63    author = describeOutput["user"]
  64    epoch = describeOutput["time"]
  65
  66    log = describeOutput["desc"]
  67
  68    changed = []
  69    removed = []
  70
  71    i = 0
  72    while describeOutput.has_key("depotFile%s" % i):
  73        path = describeOutput["depotFile%s" % i]
  74        rev = describeOutput["rev%s" % i]
  75        action = describeOutput["action%s" % i]
  76        path = path + "#" + rev
  77
  78        if action == "delete":
  79            removed.append(path)
  80        else:
  81            changed.append(path)
  82
  83        i = i + 1
  84
  85    return author, log, epoch, changed, removed
  86
  87def p4Stat(path):
  88    output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
  89    fileSize = 0
  90    mode = 644
  91    for line in output:
  92        if line.startswith("... headType x"):
  93            mode = 755
  94        elif line.startswith("... fileSize "):
  95            fileSize = long(line[12:])
  96    return mode, fileSize
  97
  98def stripRevision(path):
  99    hashPos = path.rindex("#")
 100    return path[:hashPos]
 101
 102def getUserMap():
 103    users = {}
 104
 105    for output in p4CmdList("users"):
 106        if not output.has_key("User"):
 107            continue
 108        users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
 109    return users
 110
 111users = getUserMap()
 112
 113output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
 114
 115changes = []
 116for line in output:
 117    changeNum = line.split(" ")[1]
 118    changes.append(changeNum)
 119
 120changes.reverse()
 121
 122sys.stderr.write("\n")
 123
 124tz = - time.timezone / 36
 125
 126gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
 127
 128cnt = 1
 129for change in changes:
 130    [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
 131    sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 132    cnt = cnt + 1
 133
 134    gitStream.write("commit refs/heads/master\n")
 135    if author in users:
 136        gitStream.write("committer %s %s %s\n" % (users[author], epoch, tz))
 137    else:
 138        gitStream.write("committer %s <a@b> %s %s\n" % (author, epoch, tz))
 139    gitStream.write("data <<EOT\n")
 140    gitStream.write(log)
 141    gitStream.write("EOT\n\n")
 142
 143    for f in changedFiles:
 144        if not f.startswith(prefix):
 145            sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 146            continue
 147        relpath = f[len(prefix):]
 148
 149        [mode, fileSize] = p4Stat(f)
 150
 151        gitStream.write("M %s inline %s\n" % (mode, stripRevision(relpath)))
 152        gitStream.write("data %s\n" % fileSize)
 153        gitStream.write(os.popen("p4 print -q \"%s\"" % f).read())
 154        gitStream.write("\n")
 155
 156    for f in removedFiles:
 157        if not f.startswith(prefix):
 158            sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 159            continue
 160        relpath = f[len(prefix):]
 161        gitStream.write("D %s\n" % stripRevision(relpath))
 162
 163    gitStream.write("\n")
 164
 165gitStream.close()
 166gitOutput.close()
 167gitError.close()
 168
 169print ""