Code

Instead of parsing the output of "p4 users" use the python objects of "p4 -G users".
[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:
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 #
16 import os, string, sys, time
17 import marshal, popen2
19 if 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)
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 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
53 def p4Cmd(cmd):
54     list = p4CmdList(cmd)
55     result = {}
56     for entry in list:
57         result.update(entry)
58     return result;
60 def describe(change):
61     describeOutput = p4Cmd("describe %s" % change)
63     author = describeOutput["user"]
64     epoch = describeOutput["time"]
66     log = describeOutput["desc"]
68     changed = []
69     removed = []
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
78         if action == "delete":
79             removed.append(path)
80         else:
81             changed.append(path)
83         i = i + 1
85     return author, log, epoch, changed, removed
87 def 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
98 def stripRevision(path):
99     hashPos = path.rindex("#")
100     return path[:hashPos]
102 def getUserMap():
103     users = {}
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
111 users = getUserMap()
113 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
115 changes = []
116 for line in output:
117     changeNum = line.split(" ")[1]
118     changes.append(changeNum)
120 changes.reverse()
122 sys.stderr.write("\n")
124 tz = - time.timezone / 36
126 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
128 cnt = 1
129 for 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
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")
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):]
149         [mode, fileSize] = p4Stat(f)
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")
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))
163     gitStream.write("\n")
165 gitStream.close()
166 gitOutput.close()
167 gitError.close()
169 print ""