fix permissions
[fast-export/rorcz.git] / p4-fast-export.py
blob588554d67262238ad0259396e372038c7affd077
1 #!/usr/bin/python
3 # p4-fast-export.py
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
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
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 prefix.endswith("..."):
39 prefix = prefix[:-3]
41 if not prefix.endswith("/"):
42 prefix += "/"
44 def p4CmdList(cmd):
45 pipe = os.popen("p4 -G %s" % cmd, "rb")
46 result = []
47 try:
48 while True:
49 entry = marshal.load(pipe)
50 result.append(entry)
51 except EOFError:
52 pass
53 pipe.close()
54 return result
56 def p4Cmd(cmd):
57 list = p4CmdList(cmd)
58 result = {}
59 for entry in list:
60 result.update(entry)
61 return result;
63 def getUserMap():
64 users = {}
66 for output in p4CmdList("users"):
67 if not output.has_key("User"):
68 continue
69 users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
70 return users
72 users = getUserMap()
74 output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
76 changes = []
77 for line in output:
78 changeNum = line.split(" ")[1]
79 changes.append(changeNum)
81 changes.reverse()
83 sys.stderr.write("\n")
85 tz = - time.timezone / 36
87 gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
89 cnt = 1
90 for change in changes:
91 description = p4Cmd("describe %s" % change)
93 sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
94 sys.stdout.flush()
95 cnt = cnt + 1
97 epoch = description["time"]
98 author = description["user"]
100 gitStream.write("commit refs/heads/master\n")
101 committer = ""
102 if author in users:
103 committer = "%s %s %s" % (users[author], epoch, tz)
104 else:
105 committer = "%s <a@b> %s %s" % (author, epoch, tz)
107 gitStream.write("committer %s\n" % committer)
109 gitStream.write("data <<EOT\n")
110 gitStream.write(description["desc"])
111 gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
112 gitStream.write("EOT\n\n")
114 fnum = 0
115 while description.has_key("depotFile%s" % fnum):
116 path = description["depotFile%s" % fnum]
117 if not path.startswith(prefix):
118 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
119 fnum = fnum + 1
120 continue
122 rev = description["rev%s" % fnum]
123 depotPath = path + "#" + rev
124 relPath = path[len(prefix):]
125 action = description["action%s" % fnum]
127 if action == "delete":
128 gitStream.write("D %s\n" % relPath)
129 else:
130 mode = 644
131 if description["type%s" % fnum].startswith("x"):
132 mode = 755
134 data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
136 gitStream.write("M %s inline %s\n" % (mode, relPath))
137 gitStream.write("data %s\n" % len(data))
138 gitStream.write(data)
139 gitStream.write("\n")
141 fnum = fnum + 1
143 gitStream.write("\n")
145 gitStream.write("tag p4/%s\n" % change)
146 gitStream.write("from refs/heads/master\n");
147 gitStream.write("tagger %s\n" % committer);
148 gitStream.write("data 0\n\n")
151 gitStream.close()
152 gitOutput.close()
153 gitError.close()
155 print ""