Code

Implemented basic support for converting the date of the perforce change to the git...
[git.git] / contrib / fast-import / p4-fast-export.py
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 #
17 import os, string, sys, time
19 if 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)
29 prefix = sys.argv[1]
30 changeRange = ""
31 try:
32     atIdx = prefix.index("@")
33     changeRange = prefix[atIdx:]
34     prefix = prefix[0:atIdx]
35 except ValueError:
36     changeRange = ""
38 if not prefix.endswith("/"):
39     prefix += "/"
41 def describe(change):
42     output = os.popen("p4 describe %s" % change).readlines()
44     firstLine = output[0]
46     splitted = firstLine.split(" ")
47     author = splitted[3]
48     author = author[:author.find("@")]
49     tm = time.strptime(splitted[5] + " " + splitted[6] + time.tzname[0], "%Y/%m/%d %H:%M:%S %Z")
50     epoch = int(time.mktime(tm))
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 [], [], [], [], []
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 [], [], [], [], []
66     log = output[2:filesSection - 1]
68     lines = output[filesSection + 2:differencesSection - 1]
70     changed = []
71     removed = []
73     for line in lines:
74         # chop off "... " and trailing newline
75         line = line[4:len(line) - 1]
77         lastSpace = line.rfind(" ")
78         if lastSpace == -1:
79             sys.stderr.write("trouble parsing line %s, skipping!\n" % line)
80             continue
82         operation = line[lastSpace + 1:]
83         path = line[:lastSpace]
85         if operation == "delete":
86             removed.append(path)
87         else:
88             changed.append(path)
90     return author, log, epoch, changed, removed
92 def p4cat(path):
93     return os.popen("p4 print -q \"%s\"" % path).read()
95 def stripRevision(path):
96     hashPos = path.rindex("#")
97     return path[:hashPos]
99 def 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]
111         users[key] = name + " " + email
113     return users
116 users = getUserMap()
118 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
120 changes = []
121 for line in output:
122     changeNum = line.split(" ")[1]
123     changes.append(changeNum)
125 changes.reverse()
127 sys.stderr.write("\n")
129 cnt = 1
130 for change in changes:
131     [ author, log, epoch, changedFiles, removedFiles ] = describe(change)
132     sys.stderr.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
133     cnt = cnt + 1
134 #    sys.stderr.write("%s\n" % log)
135 #    sys.stderr.write("%s\n" % changedFiles)
136 #    sys.stderr.write("%s\n" % removedFiles)
138     print "commit refs/heads/master"
139     if author in users:
140         print "committer %s %s +0000" % (users[author], epoch)
141     else:
142         print "committer %s <a@b> %s +0000" % (author, epoch)
143     print "data <<EOT"
144     for l in log:
145         print l[:len(l) - 1]
146     print "EOT"
148     print ""
150     for f in changedFiles:
151         if not f.startswith(prefix):
152             sys.stderr.write("\nchanged files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
153             continue
154         relpath = f[len(prefix):]
155         print "M 644 inline %s" % stripRevision(relpath)
156         data = p4cat(f)
157         print "data %s" % len(data)
158         sys.stdout.write(data)
159         print ""
161     for f in removedFiles:
162         if not f.startswith(prefix):
163             sys.stderr.write("\ndeleted files: ignoring path %s outside of %s in change %s\n" % (f, prefix, change))
164             continue
165         relpath = f[len(prefix):]
166         print "D %s" % stripRevision(relpath)
168     print ""
170 sys.stderr.write("\n")