Code

Minor code cleanups and ported some p4 interfacing code over to the p4 python mode.
[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 p4Cmd(cmd):
42     pipe = os.popen("p4 -G %s" % cmd, "rb")
43     result = {}
44     try:
45         while True:
46             entry = marshal.load(pipe)
47             result.update(entry)
48     except EOFError:
49         pass
50     pipe.close()
51     return result
53 def describe(change):
54     describeOutput = p4Cmd("describe %s" % change)
56     author = describeOutput["user"]
57     epoch = describeOutput["time"]
59     log = describeOutput["desc"]
61     changed = []
62     removed = []
64     i = 0
65     while describeOutput.has_key("depotFile%s" % i):
66         path = describeOutput["depotFile%s" % i]
67         rev = describeOutput["rev%s" % i]
68         action = describeOutput["action%s" % i]
69         path = path + "#" + rev
71         if action == "delete":
72             removed.append(path)
73         else:
74             changed.append(path)
76         i = i + 1
78     return author, log, epoch, changed, removed
80 def p4Stat(path):
81     output = os.popen("p4 fstat -Ol \"%s\"" % path).readlines()
82     fileSize = 0
83     mode = 644
84     for line in output:
85         if line.startswith("... headType x"):
86             mode = 755
87         elif line.startswith("... fileSize "):
88             fileSize = long(line[12:])
89     return mode, fileSize
91 def stripRevision(path):
92     hashPos = path.rindex("#")
93     return path[:hashPos]
95 def getUserMap():
96     users = {}
97     output = os.popen("p4 users")
98     for line in output:
99         firstSpace = line.index(" ")
100         secondSpace = line.index(" ", firstSpace + 1)
101         key = line[:firstSpace]
102         email = line[firstSpace + 1:secondSpace]
103         openParenPos = line.index("(", secondSpace)
104         closedParenPos = line.index(")", openParenPos)
105         name = line[openParenPos + 1:closedParenPos]
107         users[key] = name + " " + 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 ""