5 # Author: Simon Hausmann <hausmann@kde.org>
6 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
9 # - support integrations (at least p4i)
10 # - support p4 submit (hah!)
12 import os
, string
, sys
, time
13 import marshal
, popen2
, getopt
16 committedChanges
= set()
17 branch
= "refs/heads/master"
18 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
19 detectBranches
= False
21 if len(globalPrefix
) != 0:
22 globalPrefix
= globalPrefix
[:-1]
25 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
26 except getopt
.GetoptError
:
27 print "fixme, syntax error"
32 branch
= "refs/heads/" + a
33 elif o
== "--detect-branches":
35 elif o
== "--changesfile":
38 if len(args
) == 0 and len(globalPrefix
) != 0:
39 print "[using previously specified depot path %s]" % globalPrefix
41 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
43 print " %s //depot/my/project/ -- to import the current head"
44 print " %s //depot/my/project/@all -- to import everything"
45 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
47 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
51 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
52 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
54 globalPrefix
= args
[0]
63 if globalPrefix
.find("@") != -1:
64 atIdx
= globalPrefix
.index("@")
65 changeRange
= globalPrefix
[atIdx
:]
66 if changeRange
== "@all":
68 elif changeRange
.find(",") == -1:
69 revision
= changeRange
71 globalPrefix
= globalPrefix
[0:atIdx
]
72 elif globalPrefix
.find("#") != -1:
73 hashIdx
= globalPrefix
.index("#")
74 revision
= globalPrefix
[hashIdx
:]
75 globalPrefix
= globalPrefix
[0:hashIdx
]
76 elif len(previousDepotPath
) == 0:
79 if globalPrefix
.endswith("..."):
80 globalPrefix
= globalPrefix
[:-3]
82 if not globalPrefix
.endswith("/"):
86 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
90 entry
= marshal
.load(pipe
)
104 def extractFilesFromCommit(commit
):
107 while commit
.has_key("depotFile%s" % fnum
):
108 path
= commit
["depotFile%s" % fnum
]
109 if not path
.startswith(globalPrefix
):
110 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, globalPrefix
, change
)
116 file["rev"] = commit
["rev%s" % fnum
]
117 file["action"] = commit
["action%s" % fnum
]
118 file["type"] = commit
["type%s" % fnum
]
123 def isSubPathOf(first
, second
):
124 if not first
.startswith(second
):
128 return first
[len(second
)] == "/"
130 def branchesForCommit(files
):
135 relativePath
= file["path"][len(globalPrefix
):]
136 # strip off the filename
137 relativePath
= relativePath
[0:relativePath
.rfind("/")]
139 # if len(branches) == 0:
140 # branches.add(relativePath)
141 # knownBranches.add(relativePath)
144 ###### this needs more testing :)
146 for branch
in branches
:
147 if relativePath
== branch
:
150 # if relativePath.startswith(branch):
151 if isSubPathOf(relativePath
, branch
):
154 # if branch.startswith(relativePath):
155 if isSubPathOf(branch
, relativePath
):
156 branches
.remove(branch
)
162 for branch
in knownBranches
:
163 #if relativePath.startswith(branch):
164 if isSubPathOf(relativePath
, branch
):
165 if len(branches
) == 0:
166 relativePath
= branch
174 branches
.add(relativePath
)
175 knownBranches
.add(relativePath
)
179 def commit(details
, files
, branch
, branchPrefix
):
183 global committedChanges
185 epoch
= details
["time"]
186 author
= details
["user"]
188 gitStream
.write("commit %s\n" % branch
)
189 gitStream
.write("mark :%s\n" % details
["change"])
190 committedChanges
.add(int(details
["change"]))
193 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
195 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
197 gitStream
.write("committer %s\n" % committer
)
199 gitStream
.write("data <<EOT\n")
200 gitStream
.write(details
["desc"])
201 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
202 gitStream
.write("EOT\n\n")
204 if len(initialParent
) > 0:
205 gitStream
.write("from %s\n" % initialParent
)
208 #mergedBranches = set()
215 if not path
.startswith(branchPrefix
):
217 action
= file["action"]
218 if action
!= "integrate" and action
!= "branch":
221 depotPath
= path
+ "#" + rev
223 log
= p4CmdList("filelog \"%s\"" % depotPath
)
225 print "eek! I got confused by the filelog of %s" % depotPath
229 if log
["action0"] != action
:
230 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
233 branchAction
= log
["how0,0"]
234 # if branchAction == "branch into" or branchAction == "ignored":
235 # continue # ignore for branching
237 if not branchAction
.endswith(" from"):
238 continue # ignore for branching
239 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
242 source
= log
["file0,0"]
243 if source
.startswith(branchPrefix
):
246 lastSourceRev
= log
["erev0,0"]
248 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
249 if len(sourceLog
) != 1:
250 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
252 sourceLog
= sourceLog
[0]
254 change
= int(sourceLog
["change0"])
257 # relPath = source[len(globalPrefix):]
259 # for branch in knownBranches:
260 # if relPath.startswith(branch) and branch not in mergedBranches:
261 # gitStream.write("merge refs/heads/%s\n" % branch)
262 # mergedBranches.add(branch)
266 if merge
in committedChanges
:
267 gitStream
.write("merge :%s\n" % merge
)
271 if not path
.startswith(branchPrefix
):
272 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path
, branchPrefix
, details
["change"])
275 depotPath
= path
+ "#" + rev
276 relPath
= path
[len(branchPrefix
):]
277 action
= file["action"]
279 if action
== "delete":
280 gitStream
.write("D %s\n" % relPath
)
283 if file["type"].startswith("x"):
286 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
288 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
289 gitStream
.write("data %s\n" % len(data
))
290 gitStream
.write(data
)
291 gitStream
.write("\n")
293 gitStream
.write("\n")
295 lastChange
= int(details
["change"])
300 for output
in p4CmdList("users"):
301 if not output
.has_key("User"):
303 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
308 if len(changeRange
) == 0:
310 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
312 if output
.endswith("\n"):
314 tagIdx
= output
.index(" tags/p4/")
315 caretIdx
= output
.find("^")
319 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
320 changeRange
= "@%s,#head" % rev
321 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
322 initialTag
= "p4/%s" % (int(rev
) - 1)
326 sys
.stderr
.write("\n")
328 tz
= - time
.timezone
/ 36
329 tzsign
= ("%s" % tz
)[0]
330 if tzsign
!= '+' and tzsign
!= '-':
331 tz
= "+" + ("%s" % tz
)
333 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
335 if len(revision
) > 0:
336 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
338 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
339 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
340 details
["change"] = revision
344 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
345 change
= int(info
["change"])
346 if change
> newestRevision
:
347 newestRevision
= change
349 if info
["action"] == "delete":
352 for prop
in [ "depotFile", "rev", "action", "type" ]:
353 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
355 fileCnt
= fileCnt
+ 1
357 details
["change"] = newestRevision
360 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
362 print gitError
.read()
367 if len(changesFile
) > 0:
368 output
= open(changesFile
).readlines()
371 changeSet
.add(int(line
))
373 for change
in changeSet
:
374 changes
.append(change
)
378 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
381 changeNum
= line
.split(" ")[1]
382 changes
.append(changeNum
)
386 if len(changes
) == 0:
387 print "no changes to import!"
391 for change
in changes
:
392 description
= p4Cmd("describe %s" % change
)
394 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
399 files
= extractFilesFromCommit(description
)
401 for branch
in branchesForCommit(files
):
402 knownBranches
.add(branch
)
403 branchPrefix
= globalPrefix
+ branch
+ "/"
404 branch
= "refs/heads/" + branch
405 commit(description
, files
, branch
, branchPrefix
)
407 commit(description
, files
, branch
, globalPrefix
)
409 # print gitError.read()
414 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
415 gitStream
.write("from %s\n\n" % branch
);
422 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
423 if len(initialTag
) > 0:
424 os
.popen("git tag -d %s" % initialTag
).read()