Work in progress on detecting branches.
Added a disk-cache p4 output so debugging imports is faster. Added --known-branches commandline option for pre-defining branches. Various other fixes... Signed-off-by: Simon Hausmann <hausmann@kde.org>
Simon Hausmann committed
Mar 8, 2007 at 21:27 UTC
3ef674bd4b2f651d769cd5b464ba26313eab290a
1 file changed
+103
-57
contrib/fast-import/p4-fast-export.py
+103
-57
@@ -11,12 +11,15 @@
11
# - emulate p4's delete behavior: if a directory becomes empty delete it. continue
12
# with parent dir until non-empty dir is found.
13
#
14
-import os, string, sys, time
15
-import marshal, popen2, getopt
14
+import os, string, sys, time, os.path
15
+import marshal, popen2, getopt, sha
16
from sets import Set;
17
18
+cacheDebug = False
19
+
20
silent = False
21
knownBranches = Set()
22
+createdBranches = Set()
23
committedChanges = Set()
24
branch = "refs/heads/master"
25
globalPrefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
@@ -26,7 +29,7 @@ if len(globalPrefix) != 0:
29
globalPrefix = globalPrefix[:-1]
30
31
try:
29
- opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=", "silent" ])
32
+ opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches", "changesfile=", "silent", "known-branches=" ])
33
except getopt.GetoptError:
34
print "fixme, syntax error"
35
sys.exit(1)
@@ -40,6 +43,9 @@ for o, a in opts:
43
changesFile = a
44
elif o == "--silent":
45
silent= True
46
+ elif o == "--known-branches":
47
+ for branch in o.split(","):
48
+ knownBranches.add(branch)
49
50
if len(args) == 0 and len(globalPrefix) != 0:
51
if not silent:
@@ -89,8 +95,37 @@ if globalPrefix.endswith("..."):
95
if not globalPrefix.endswith("/"):
96
globalPrefix += "/"
97
98
+def p4File(depotPath):
99
+ cacheKey = "/tmp/p4cache/data-" + sha.new(depotPath).hexdigest()
100
+
101
+ data = 0
102
+ try:
103
+ if not cacheDebug:
104
+ raise
105
+ data = open(cacheKey, "rb").read()
106
+ except:
107
+ data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
108
+ if cacheDebug:
109
+ open(cacheKey, "wb").write(data)
110
+
111
+ return data
112
+
113
def p4CmdList(cmd):
93
- pipe = os.popen("p4 -G %s" % cmd, "rb")
114
+ fullCmd = "p4 -G %s" % cmd;
115
+
116
+ cacheKey = sha.new(fullCmd).hexdigest()
117
+ cacheKey = "/tmp/p4cache/cmd-" + cacheKey
118
+
119
+ cached = True
120
+ pipe = 0
121
+ try:
122
+ if not cacheDebug:
123
+ raise
124
+ pipe = open(cacheKey, "rb")
125
+ except:
126
+ cached = False
127
+ pipe = os.popen(fullCmd, "rb")
128
+
129
result = []
130
try:
131
while True:
@@ -99,6 +134,13 @@ def p4CmdList(cmd):
134
except EOFError:
135
pass
136
pipe.close()
137
+
138
+ if not cached and cacheDebug:
139
+ pipe = open(cacheKey, "wb")
140
+ for r in result:
141
+ marshal.dump(r, pipe)
142
+ pipe.close()
143
+
144
return result
145
146
def p4Cmd(cmd):
@@ -114,8 +156,8 @@ def extractFilesFromCommit(commit):
156
while commit.has_key("depotFile%s" % fnum):
157
path = commit["depotFile%s" % fnum]
158
if not path.startswith(globalPrefix):
117
- if not silent:
118
- print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
159
+# if not silent:
160
+# print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, globalPrefix, change)
161
fnum = fnum + 1
162
continue
163
@@ -184,41 +226,8 @@ def branchesForCommit(files):
226
227
return branches
228
187
-def commit(details, files, branch, branchPrefix):
188
- global initialParent
189
- global users
190
- global lastChange
191
- global committedChanges
192
-
193
- epoch = details["time"]
194
- author = details["user"]
195
-
196
- gitStream.write("commit %s\n" % branch)
197
- gitStream.write("mark :%s\n" % details["change"])
198
- committedChanges.add(int(details["change"]))
199
- committer = ""
200
- if author in users:
201
- committer = "%s %s %s" % (users[author], epoch, tz)
202
- else:
203
- committer = "%s <a@b> %s %s" % (author, epoch, tz)
204
-
205
- gitStream.write("committer %s\n" % committer)
206
-
207
- gitStream.write("data <<EOT\n")
208
- gitStream.write(details["desc"])
209
- gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
210
- gitStream.write("EOT\n\n")
211
-
212
- if len(initialParent) > 0:
213
- gitStream.write("from %s\n" % initialParent)
214
- initialParent = ""
215
-
216
- #mergedBranches = Set()
217
- merges = Set()
218
-
229
+def findBranchParent(branchPrefix, files):
230
for file in files:
220
- if lastChange == 0 or not detectBranches:
221
- continue
231
path = file["path"]
232
if not path.startswith(branchPrefix):
233
continue
@@ -259,26 +268,51 @@ def commit(details, files, branch, branchPrefix):
268
sys.exit(1);
269
sourceLog = sourceLog[0]
270
262
- change = int(sourceLog["change0"])
263
- merges.add(change)
271
+ relPath = source[len(globalPrefix):]
272
+ # strip off the filename
273
+ relPath = relPath[0:relPath.rfind("/")]
274
265
-# relPath = source[len(globalPrefix):]
266
-#
267
-# for branch in knownBranches:
268
-# if relPath.startswith(branch) and branch not in mergedBranches:
269
-# gitStream.write("merge refs/heads/%s\n" % branch)
270
-# mergedBranches.add(branch)
271
-# break
275
+ for branch in knownBranches:
276
+ if isSubPathOf(relPath, branch):
277
+# print "determined parent branch branch %s due to change in file %s" % (branch, source)
278
+ return "refs/heads/%s" % branch
279
+# else:
280
+# print "%s is not a subpath of branch %s" % (relPath, branch)
281
+
282
+ return ""
283
+
284
+def commit(details, files, branch, branchPrefix, parent):
285
+ global users
286
+ global lastChange
287
+ global committedChanges
288
+
289
+ epoch = details["time"]
290
+ author = details["user"]
291
+
292
+ gitStream.write("commit %s\n" % branch)
293
+ gitStream.write("mark :%s\n" % details["change"])
294
+ committedChanges.add(int(details["change"]))
295
+ committer = ""
296
+ if author in users:
297
+ committer = "%s %s %s" % (users[author], epoch, tz)
298
+ else:
299
+ committer = "%s <a@b> %s %s" % (author, epoch, tz)
300
273
- for merge in merges:
274
- if merge in committedChanges:
275
- gitStream.write("merge :%s\n" % merge)
301
+ gitStream.write("committer %s\n" % committer)
302
+
303
+ gitStream.write("data <<EOT\n")
304
+ gitStream.write(details["desc"])
305
+ gitStream.write("\n[ imported from %s; change %s ]\n" % (branchPrefix, details["change"]))
306
+ gitStream.write("EOT\n\n")
307
+
308
+ if len(parent) > 0:
309
+ gitStream.write("from %s\n" % parent)
310
311
for file in files:
312
path = file["path"]
313
if not path.startswith(branchPrefix):
280
- if not silent:
281
- print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
314
+# if not silent:
315
+# print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path, branchPrefix, details["change"])
316
continue
317
rev = file["rev"]
318
depotPath = path + "#" + rev
@@ -292,7 +326,7 @@ def commit(details, files, branch, branchPrefix):
326
if file["type"].startswith("x"):
327
mode = 755
328
295
- data = os.popen("p4 print -q \"%s\"" % depotPath, "rb").read()
329
+ data = p4File(depotPath)
330
331
gitStream.write("M %s inline %s\n" % (mode, relPath))
332
gitStream.write("data %s\n" % len(data))
@@ -410,10 +444,22 @@ else:
444
for branch in branchesForCommit(files):
445
knownBranches.add(branch)
446
branchPrefix = globalPrefix + branch + "/"
447
+
448
+ parent = ""
449
+ ########### remove cnt!!!
450
+ if branch not in createdBranches and cnt > 2:
451
+ createdBranches.add(branch)
452
+ parent = findBranchParent(branchPrefix, files)
453
+ if parent == branch:
454
+ parent = ""
455
+# elif len(parent) > 0:
456
+# print "%s branched off of %s" % (branch, parent)
457
+
458
branch = "refs/heads/" + branch
414
- commit(description, files, branch, branchPrefix)
459
+ commit(description, files, branch, branchPrefix, parent)
460
else:
416
- commit(description, files, branch, globalPrefix)
461
+ commit(description, files, branch, globalPrefix, initialParent)
462
+ initialParent = ""
463
# except:
464
# print gitError.read()
465
# sys.exit(1)