Started work on p4 branch detection (experimental!).
Signed-off-by: Simon Hausmann <simon@lst.de>
Simon Hausmann committed
Feb 12, 2007 at 21:04 UTC
766887e110898d4ec6f08d19061db323a1ac2b27
1 file changed
+50
-9
contrib/fast-import/p4-fast-export.py
+50
-9
@@ -14,11 +14,12 @@ import marshal, popen2, getopt
14
15
branch = "refs/heads/master"
16
prefix = previousDepotPath = os.popen("git-repo-config --get p4.depotpath").read()
17
+detectBranches = False
18
if len(prefix) != 0:
19
prefix = prefix[:-1]
20
21
try:
21
- opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=" ])
22
+ opts, args = getopt.getopt(sys.argv[1:], "", [ "branch=", "detect-branches" ])
23
except getopt.GetoptError:
24
print "fixme, syntax error"
25
sys.exit(1)
@@ -26,6 +27,8 @@ except getopt.GetoptError:
27
for o, a in opts:
28
if o == "--branch":
29
branch = "refs/heads/" + a
30
+ elif o == "--detect-branches":
31
+ detectBranches = True
32
33
if len(args) == 0 and len(prefix) != 0:
34
print "[using previously specified depot path %s]" % prefix
@@ -97,8 +100,13 @@ def extractFilesFromCommit(commit):
100
files = []
101
fnum = 0
102
while commit.has_key("depotFile%s" % fnum):
103
+ path = commit["depotFile%s" % fnum]
104
+ if not path.startswith(prefix):
105
+ print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
106
+ continue
107
+
108
file = {}
101
- file["path"] = commit["depotFile%s" % fnum]
109
+ file["path"] = path
110
file["rev"] = commit["rev%s" % fnum]
111
file["action"] = commit["action%s" % fnum]
112
file["type"] = commit["type%s" % fnum]
@@ -106,7 +114,37 @@ def extractFilesFromCommit(commit):
114
fnum = fnum + 1
115
return files
116
109
-def commit(details, files, branch):
117
+def branchesForCommit(files):
118
+ branches = set()
119
+
120
+ for file in files:
121
+ relativePath = file["path"][len(prefix):]
122
+ # strip off the filename
123
+ relativePath = relativePath[0:relativePath.rfind("/")]
124
+
125
+ if len(branches) == 0:
126
+ branches.add(relativePath)
127
+ continue
128
+
129
+ ###### this needs more testing :)
130
+ knownBranch = False
131
+ for branch in branches:
132
+ if relativePath == branch:
133
+ knownBranch = True
134
+ break
135
+ if relativePath.startswith(branch):
136
+ knownBranch = True
137
+ break
138
+ if branch.startswith(relativePath):
139
+ branches.remove(branch)
140
+ break
141
+
142
+ if not knownBranch:
143
+ branches.add(relativePath)
144
+
145
+ return branches
146
+
147
+def commit(details, files, branch, prefix):
148
global initialParent
149
global users
150
global lastChange
@@ -134,10 +172,6 @@ def commit(details, files, branch):
172
173
for file in files:
174
path = file["path"]
137
- if not path.startswith(prefix):
138
- print "\nchanged files: ignoring path %s outside of %s in change %s" % (path, prefix, change)
139
- continue
140
-
175
rev = file["rev"]
176
depotPath = path + "#" + rev
177
relPath = path[len(prefix):]
@@ -224,7 +258,7 @@ if len(revision) > 0:
258
details["change"] = newestRevision
259
260
try:
227
- commit(details, extractFilesFromCommit(details), branch)
261
+ commit(details, extractFilesFromCommit(details), branch, prefix)
262
except:
263
print gitError.read()
264
@@ -251,7 +285,14 @@ else:
285
cnt = cnt + 1
286
287
try:
254
- commit(description, extractFilesFromCommit(description), branch)
288
+ files = extractFilesFromCommit(description)
289
+ if detectBranches:
290
+ for branch in branchesForCommit(files):
291
+ branchPrefix = prefix + branch + "/"
292
+ branch = "refs/heads/" + branch
293
+ commit(description, files, branch, branchPrefix)
294
+ else:
295
+ commit(description, files, branch, prefix)
296
except:
297
print gitError.read()
298
sys.exit(1)