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
17 committedChanges
= Set()
18 branch
= "refs/heads/master"
19 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
20 detectBranches
= False
22 if len(globalPrefix
) != 0:
23 globalPrefix
= globalPrefix
[:-1]
26 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
27 except getopt
.GetoptError
:
28 print "fixme, syntax error"
33 branch
= "refs/heads/" + a
34 elif o
== "--detect-branches":
36 elif o
== "--changesfile":
39 if len(args
) == 0 and len(globalPrefix
) != 0:
40 print "[using previously specified depot path %s]" % globalPrefix
42 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
44 print " %s //depot/my/project/ -- to import the current head"
45 print " %s //depot/my/project/@all -- to import everything"
46 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
48 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
52 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
53 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
55 globalPrefix
= args
[0]
64 if globalPrefix
.find("@") != -1:
65 atIdx
= globalPrefix
.index("@")
66 changeRange
= globalPrefix
[atIdx
:]
67 if changeRange
== "@all":
69 elif changeRange
.find(",") == -1:
70 revision
= changeRange
72 globalPrefix
= globalPrefix
[0:atIdx
]
73 elif globalPrefix
.find("#") != -1:
74 hashIdx
= globalPrefix
.index("#")
75 revision
= globalPrefix
[hashIdx
:]
76 globalPrefix
= globalPrefix
[0:hashIdx
]
77 elif len(previousDepotPath
) == 0:
80 if globalPrefix
.endswith("..."):
81 globalPrefix
= globalPrefix
[:-3]
83 if not globalPrefix
.endswith("/"):
87 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
91 entry
= marshal
.load(pipe
)
105 def extractFilesFromCommit(commit
):
108 while commit
.has_key("depotFile%s" % fnum
):
109 path
= commit
["depotFile%s" % fnum
]
110 if not path
.startswith(globalPrefix
):
111 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, globalPrefix
, change
)
117 file["rev"] = commit
["rev%s" % fnum
]
118 file["action"] = commit
["action%s" % fnum
]
119 file["type"] = commit
["type%s" % fnum
]
124 def isSubPathOf(first
, second
):
125 if not first
.startswith(second
):
129 return first
[len(second
)] == "/"
131 def branchesForCommit(files
):
136 relativePath
= file["path"][len(globalPrefix
):]
137 # strip off the filename
138 relativePath
= relativePath
[0:relativePath
.rfind("/")]
140 # if len(branches) == 0:
141 # branches.add(relativePath)
142 # knownBranches.add(relativePath)
145 ###### this needs more testing :)
147 for branch
in branches
:
148 if relativePath
== branch
:
151 # if relativePath.startswith(branch):
152 if isSubPathOf(relativePath
, branch
):
155 # if branch.startswith(relativePath):
156 if isSubPathOf(branch
, relativePath
):
157 branches
.remove(branch
)
163 for branch
in knownBranches
:
164 #if relativePath.startswith(branch):
165 if isSubPathOf(relativePath
, branch
):
166 if len(branches
) == 0:
167 relativePath
= branch
175 branches
.add(relativePath
)
176 knownBranches
.add(relativePath
)
180 def commit(details
, files
, branch
, branchPrefix
):
184 global committedChanges
186 epoch
= details
["time"]
187 author
= details
["user"]
189 gitStream
.write("commit %s\n" % branch
)
190 gitStream
.write("mark :%s\n" % details
["change"])
191 committedChanges
.add(int(details
["change"]))
194 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
196 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
198 gitStream
.write("committer %s\n" % committer
)
200 gitStream
.write("data <<EOT\n")
201 gitStream
.write(details
["desc"])
202 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
203 gitStream
.write("EOT\n\n")
205 if len(initialParent
) > 0:
206 gitStream
.write("from %s\n" % initialParent
)
209 #mergedBranches = Set()
216 if not path
.startswith(branchPrefix
):
218 action
= file["action"]
219 if action
!= "integrate" and action
!= "branch":
222 depotPath
= path
+ "#" + rev
224 log
= p4CmdList("filelog \"%s\"" % depotPath
)
226 print "eek! I got confused by the filelog of %s" % depotPath
230 if log
["action0"] != action
:
231 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
234 branchAction
= log
["how0,0"]
235 # if branchAction == "branch into" or branchAction == "ignored":
236 # continue # ignore for branching
238 if not branchAction
.endswith(" from"):
239 continue # ignore for branching
240 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
243 source
= log
["file0,0"]
244 if source
.startswith(branchPrefix
):
247 lastSourceRev
= log
["erev0,0"]
249 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
250 if len(sourceLog
) != 1:
251 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
253 sourceLog
= sourceLog
[0]
255 change
= int(sourceLog
["change0"])
258 # relPath = source[len(globalPrefix):]
260 # for branch in knownBranches:
261 # if relPath.startswith(branch) and branch not in mergedBranches:
262 # gitStream.write("merge refs/heads/%s\n" % branch)
263 # mergedBranches.add(branch)
267 if merge
in committedChanges
:
268 gitStream
.write("merge :%s\n" % merge
)
272 if not path
.startswith(branchPrefix
):
273 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path
, branchPrefix
, details
["change"])
276 depotPath
= path
+ "#" + rev
277 relPath
= path
[len(branchPrefix
):]
278 action
= file["action"]
280 if action
== "delete":
281 gitStream
.write("D %s\n" % relPath
)
284 if file["type"].startswith("x"):
287 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
289 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
290 gitStream
.write("data %s\n" % len(data
))
291 gitStream
.write(data
)
292 gitStream
.write("\n")
294 gitStream
.write("\n")
296 lastChange
= int(details
["change"])
301 for output
in p4CmdList("users"):
302 if not output
.has_key("User"):
304 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
309 if len(changeRange
) == 0:
311 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
313 if output
.endswith("\n"):
315 tagIdx
= output
.index(" tags/p4/")
316 caretIdx
= output
.find("^")
320 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
321 changeRange
= "@%s,#head" % rev
322 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
323 initialTag
= "p4/%s" % (int(rev
) - 1)
327 sys
.stderr
.write("\n")
329 tz
= - time
.timezone
/ 36
330 tzsign
= ("%s" % tz
)[0]
331 if tzsign
!= '+' and tzsign
!= '-':
332 tz
= "+" + ("%s" % tz
)
334 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
336 if len(revision
) > 0:
337 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
339 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
340 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
341 details
["change"] = revision
345 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
346 change
= int(info
["change"])
347 if change
> newestRevision
:
348 newestRevision
= change
350 if info
["action"] == "delete":
353 for prop
in [ "depotFile", "rev", "action", "type" ]:
354 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
356 fileCnt
= fileCnt
+ 1
358 details
["change"] = newestRevision
361 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
363 print gitError
.read()
368 if len(changesFile
) > 0:
369 output
= open(changesFile
).readlines()
372 changeSet
.add(int(line
))
374 for change
in changeSet
:
375 changes
.append(change
)
379 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
382 changeNum
= line
.split(" ")[1]
383 changes
.append(changeNum
)
387 if len(changes
) == 0:
388 print "no changes to import!"
392 for change
in changes
:
393 description
= p4Cmd("describe %s" % change
)
395 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
400 files
= extractFilesFromCommit(description
)
402 for branch
in branchesForCommit(files
):
403 knownBranches
.add(branch
)
404 branchPrefix
= globalPrefix
+ branch
+ "/"
405 branch
= "refs/heads/" + branch
406 commit(description
, files
, branch
, branchPrefix
)
408 commit(description
, files
, branch
, globalPrefix
)
410 # print gitError.read()
415 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
416 gitStream
.write("from %s\n\n" % branch
);
423 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
424 if len(initialTag
) > 0:
425 os
.popen("git tag -d %s" % initialTag
).read()