abd6da4668f465f042b04d9bac146c6157f3a5ae
   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: - fix date parsing (how hard can it be?)
   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#       - p4 print directly to stdout. need to figure out file size somehow
  14#         though.
  15#       - support p4 submit (hah!)
  16#       - don't hardcode the import to master
  17#
  18import os, string, sys
  19
  20if len(sys.argv) != 2:
  21    sys.stderr.write("usage: %s //depot/path[@revRange]\n" % sys.argv[0]);
  22    sys.stderr.write("\n    example:\n");
  23    sys.stderr.write("    %s //depot/my/project -- to import everything\n");
  24    sys.stderr.write("    %s //depot/my/project@1,6 -- to import only from revision 1 to 6\n");
  25    sys.stderr.write("\n");
  26    sys.exit(1)
  27
  28prefix = sys.argv[1]
  29changeRange = ""
  30try:
  31    atIdx = prefix.index("@")
  32    changeRange = prefix[atIdx:]
  33    prefix = prefix[0:atIdx]
  34except ValueError:
  35    changeRange = ""
  36
  37def describe(change):
  38    output = os.popen("p4 describe %s" % change).readlines()
  39
  40    firstLine = output[0]
  41
  42    author = firstLine.split(" ")[3]
  43    author = author[:author.find("@")]
  44
  45    filesSection = 0
  46    try:
  47        filesSection = output.index("Affected files ...\n")
  48    except ValueError:
  49        sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
  50        return [], [], [], []
  51
  52    differencesSection = 0
  53    try:
  54        differencesSection = output.index("Differences ...\n")
  55    except ValueError:
  56        sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
  57        return [], [], [], []
  58
  59    log = output[2:filesSection - 1]
  60
  61    lines = output[filesSection + 2:differencesSection - 1]
  62
  63    changed = []
  64    removed = []
  65
  66    for line in lines:
  67        # chop off "... " and trailing newline
  68        line = line[4:len(line) - 1]
  69
  70        lastSpace = line.rfind(" ")
  71        if lastSpace == -1:
  72            sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
  73            continue
  74
  75        operation = line[lastSpace + 1:]
  76        path = line[:lastSpace]
  77
  78        if operation == "delete":
  79            removed.append(path)
  80        else:
  81            changed.append(path)
  82
  83    return author, log, changed, removed
  84
  85def p4cat(path):
  86    return os.popen("p4 print -q \"%s\"" % path).read()
  87
  88def stripRevision(path):
  89    hashPos = path.rindex("#")
  90    return path[:hashPos]
  91
  92def getUserMap():
  93    users = {}
  94    output = os.popen("p4 users")
  95    for line in output:
  96        firstSpace = line.index(" ")
  97        secondSpace = line.index(" ", firstSpace + 1)
  98        key = line[:firstSpace]
  99        email = line[firstSpace + 1:secondSpace]
 100        openParenPos = line.index("(", secondSpace)
 101        closedParenPos = line.index(")", openParenPos)
 102        name = line[openParenPos + 1:closedParenPos]
 103
 104        users[key] = name + " " + email
 105
 106    return users
 107
 108
 109users = getUserMap()
 110
 111output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
 112
 113changes = []
 114for line in output:
 115    changeNum = line.split(" ")[1]
 116    changes.append(changeNum)
 117
 118changes.reverse()
 119
 120sys.stderr.write("\n")
 121
 122cnt = 0
 123for change in changes:
 124    [ author, log, changedFiles, removedFiles ] = describe(change)
 125    sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 126    cnt = cnt + 1
 127#    sys.stderr.write("%s\n" % log)
 128#    sys.stderr.write("%s\n" % changedFiles)
 129#    sys.stderr.write("%s\n" % removedFiles)
 130
 131    print "commit refs/heads/master"
 132    if author in users:
 133        print "committer %s 1 2" % users[author]
 134    else:
 135        print "committer %s <a@b> 1 2" % author
 136    print "data <<EOT"
 137    for l in log:
 138        print l[:len(l) - 1]
 139    print "EOT"
 140
 141    print ""
 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        print "M 644 inline %s" % stripRevision(relpath)
 149        data = p4cat(f)
 150        print "data %s" % len(data)
 151        sys.stdout.write(data)
 152        print ""
 153
 154    for f in removedFiles:
 155        if not f.startswith(prefix):
 156            sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 157            continue
 158        relpath = f[len(prefix):]
 159        print "D %s" % stripRevision(relpath)
 160
 161    print ""
 162