contrib / fast-import / p4-fast-export.pyon commit Some fixes to the timezone conversion between the date of a perforce change and the git commit. (d93ed31)
   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:  - support integrations (at least p4i)
   9#       - support incremental imports
  10#       - create tags
  11#       - instead of reading all files into a variable try to pipe from
  12#       - p4 print directly to stdout. need to figure out file size somehow
  13#         though.
  14#       - support p4 submit (hah!)
  15#       - don't hardcode the import to master
  16#
  17import os, string, sys, time
  18
  19if len(sys.argv) != 2:
  20    sys.stderr.write("usage: %s //depot/path[@revRange]\n" % sys.argv[0]);
  21    sys.stderr.write("\n    example:\n");
  22    sys.stderr.write("    %s //depot/my/project/ -- to import everything\n");
  23    sys.stderr.write("    %s //depot/my/project/@1,6 -- to import only from revision 1 to 6\n");
  24    sys.stderr.write("\n");
  25    sys.stderr.write("    (a ... is not needed in the path p4 specification, it's added implicitly)\n");
  26    sys.stderr.write("\n");
  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 describe(change):
  42    output = os.popen("p4 describe %s" % change).readlines()
  43
  44    firstLine = output[0]
  45
  46    splitted = firstLine.split(" ")
  47    author = splitted[3]
  48    author = author[:author.find("@")]
  49    tm = time.strptime(splitted[5] + " " + splitted[6], "%Y/%m/%d %H:%M:%S ")
  50    epoch = int(time.mktime(tm))
  51
  52    filesSection = 0
  53    try:
  54        filesSection = output.index("Affected files ...\n")
  55    except ValueError:
  56        sys.stderr.write("Change %s doesn't seem to affect any files. Weird.\n" % change)
  57        return [], [], [], [], []
  58
  59    differencesSection = 0
  60    try:
  61        differencesSection = output.index("Differences ...\n")
  62    except ValueError:
  63        sys.stderr.write("Change %s doesn't seem to have a differences section. Weird.\n" % change)
  64        return [], [], [], [], []
  65
  66    log = output[2:filesSection - 1]
  67
  68    lines = output[filesSection + 2:differencesSection - 1]
  69
  70    changed = []
  71    removed = []
  72
  73    for line in lines:
  74        # chop off "... " and trailing newline
  75        line = line[4:len(line) - 1]
  76
  77        lastSpace = line.rfind(" ")
  78        if lastSpace == -1:
  79            sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
  80            continue
  81
  82        operation = line[lastSpace + 1:]
  83        path = line[:lastSpace]
  84
  85        if operation == "delete":
  86            removed.append(path)
  87        else:
  88            changed.append(path)
  89
  90    return author, log, epoch, changed, removed
  91
  92def p4cat(path):
  93    return os.popen("p4 print -q \"%s\"" % path).read()
  94
  95def stripRevision(path):
  96    hashPos = path.rindex("#")
  97    return path[:hashPos]
  98
  99def getUserMap():
 100    users = {}
 101    output = os.popen("p4 users")
 102    for line in output:
 103        firstSpace = line.index(" ")
 104        secondSpace = line.index(" ", firstSpace + 1)
 105        key = line[:firstSpace]
 106        email = line[firstSpace + 1:secondSpace]
 107        openParenPos = line.index("(", secondSpace)
 108        closedParenPos = line.index(")", openParenPos)
 109        name = line[openParenPos + 1:closedParenPos]
 110
 111        users[key] = name + " " + email
 112
 113    return users
 114
 115
 116users = getUserMap()
 117
 118output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
 119
 120changes = []
 121for line in output:
 122    changeNum = line.split(" ")[1]
 123    changes.append(changeNum)
 124
 125changes.reverse()
 126
 127sys.stderr.write("\n")
 128
 129tz = - time.timezone / 36
 130
 131cnt = 1
 132for change in changes:
 133    [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
 134    sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
 135    cnt = cnt + 1
 136#    sys.stderr.write("%s\n" % log)
 137#    sys.stderr.write("%s\n" % changedFiles)
 138#    sys.stderr.write("%s\n" % removedFiles)
 139
 140    print "commit refs/heads/master"
 141    if author in users:
 142        print "committer %s %s %s" % (users[author], epoch, tz)
 143    else:
 144        print "committer %s <a@b> %s %s" % (author, epoch, tz)
 145    print "data <<EOT"
 146    for l in log:
 147        print l[:len(l) - 1]
 148    print "EOT"
 149
 150    print ""
 151
 152    for f in changedFiles:
 153        if not f.startswith(prefix):
 154            sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 155            continue
 156        relpath = f[len(prefix):]
 157        print "M 644 inline %s" % stripRevision(relpath)
 158        data = p4cat(f)
 159        print "data %s" % len(data)
 160        sys.stdout.write(data)
 161        print ""
 162
 163    for f in removedFiles:
 164        if not f.startswith(prefix):
 165            sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
 166            continue
 167        relpath = f[len(prefix):]
 168        print "D %s" % stripRevision(relpath)
 169
 170    print ""
 171
 172sys.stderr.write("\n")
 173