Initial support for importing a directory from Perforce at a specified revision.
Use p4 files //depot/path/...@revision to determine the state of the project and create a "fake" git commit from it. Signed-off-by: Simon Hausmann <hausmann@kde.org>
Simon Hausmann committed
Feb 1, 2007 at 18:19 UTC
6d48d12f5d34a2acb6d8b56bfe3c26d4db92b1a5
1 file changed
+65
-29
contrib/fast-import/p4-fast-export.py
+65
-29
@@ -25,15 +25,21 @@ if len(sys.argv) != 2:
25
branch = "refs/heads/p4"
26
prefix = sys.argv[1]
27
changeRange = ""
28
+revision = ""
29
users = {}
30
initialParent = ""
31
31
-try:
32
+if prefix.find("@") != -1:
33
atIdx = prefix.index("@")
34
changeRange = prefix[atIdx:]
35
+ if changeRange.find(",") == -1:
36
+ revision = changeRange
37
+ changeRange = ""
38
prefix = prefix[0:atIdx]
35
-except ValueError:
36
- changeRange = ""
39
+elif prefix.find("#") != -1:
40
+ hashIdx = prefix.index("#")
41
+ revision = prefix[hashIdx:]
42
+ prefix = prefix[0:hashIdx]
43
44
if prefix.endswith("..."):
45
prefix = prefix[:-3]
@@ -78,7 +84,7 @@ def commit(details):
84
85
gitStream.write("data <<EOT\n")
86
gitStream.write(details["desc"])
81
- gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, change))
87
+ gitStream.write("\n[ imported from %s; change %s ]\n" % (prefix, details["change"]))
88
gitStream.write("EOT\n\n")
89
90
if len(initialParent) > 0:
@@ -116,7 +122,7 @@ def commit(details):
122
123
gitStream.write("\n")
124
119
- gitStream.write("tag p4/%s\n" % change)
125
+ gitStream.write("tag p4/%s\n" % details["change"])
126
gitStream.write("from %s\n" % branch);
127
gitStream.write("tagger %s\n" % committer);
128
gitStream.write("data 0\n\n")
@@ -139,43 +145,73 @@ if len(changeRange) == 0:
145
output = sout.read()
146
tagIdx = output.index(" tags/p4/")
147
caretIdx = output.index("^")
142
- revision = int(output[tagIdx + 9 : caretIdx]) + 1
143
- changeRange = "@%s,#head" % revision
148
+ rev = int(output[tagIdx + 9 : caretIdx]) + 1
149
+ changeRange = "@%s,#head" % rev
150
initialParent = os.popen("git-rev-parse %s" % branch).read()[:-1]
151
except:
152
pass
153
148
-output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
154
+sys.stderr.write("\n")
155
150
-changes = []
151
-for line in output:
152
- changeNum = line.split(" ")[1]
153
- changes.append(changeNum)
156
+tz = - time.timezone / 36
157
155
-changes.reverse()
158
+if len(revision) > 0:
159
+ print "Doing initial import of %s from revision %s" % (prefix, revision)
160
157
-if len(changes) == 0:
158
- print "no changes to import!"
159
- sys.exit(1)
161
+ details = { "user" : "git perforce import user", "time" : int(time.time()) }
162
+ details["desc"] = "Initial import of %s from the state at revision %s" % (prefix, revision)
163
+ details["change"] = revision
164
+ newestRevision = 0
165
161
-sys.stderr.write("\n")
166
+ fileCnt = 0
167
+ for info in p4CmdList("files %s...%s" % (prefix, revision)):
168
+ if info["action"] == "delete":
169
+ continue
170
+ for prop in [ "depotFile", "rev", "action", "type" ]:
171
+ details["%s%s" % (prop, fileCnt)] = info[prop]
172
163
-tz = - time.timezone / 36
173
+ change = info["change"]
174
+ if change > newestRevision:
175
+ newestRevision = change
176
+
177
+ fileCnt = fileCnt + 1
178
+
179
+ details["change"] = newestRevision
180
+
181
+ gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
182
+ commit(details)
183
+
184
+ gitStream.close()
185
+ gitOutput.close()
186
+ gitError.close()
187
+else:
188
+ output = os.popen("p4 changes %s...%s" % (prefix, changeRange)).readlines()
189
+
190
+ changes = []
191
+ for line in output:
192
+ changeNum = line.split(" ")[1]
193
+ changes.append(changeNum)
194
+
195
+ changes.reverse()
196
+
197
+ if len(changes) == 0:
198
+ print "no changes to import!"
199
+ sys.exit(1)
200
165
-gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
201
+ gitOutput, gitStream, gitError = popen2.popen3("git-fast-import")
202
167
-cnt = 1
168
-for change in changes:
169
- description = p4Cmd("describe %s" % change)
203
+ cnt = 1
204
+ for change in changes:
205
+ description = p4Cmd("describe %s" % change)
206
171
- sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
172
- sys.stdout.flush()
173
- cnt = cnt + 1
207
+ sys.stdout.write("\rimporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
208
+ sys.stdout.flush()
209
+ cnt = cnt + 1
210
175
- commit(description)
211
+ commit(description)
212
177
-gitStream.close()
178
-gitOutput.close()
179
-gitError.close()
213
+ gitStream.close()
214
+ gitOutput.close()
215
+ gitError.close()
216
217
print ""