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!)
11 # - emulate p4's delete behavior: if a directory becomes empty delete it. continue
12 # with parent dir until non-empty dir is found.
14 import os
, string
, sys
, time
15 import marshal
, popen2
, getopt
20 committedChanges
= Set()
21 branch
= "refs/heads/master"
22 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
23 detectBranches
= False
25 if len(globalPrefix
) != 0:
26 globalPrefix
= globalPrefix
[:-1]
29 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches", "changesfile=", "silent" ])
30 except getopt
.GetoptError
:
31 print "fixme, syntax error"
36 branch
= "refs/heads/" + a
37 elif o
== "--detect-branches":
39 elif o
== "--changesfile":
44 if len(args
) == 0 and len(globalPrefix
) != 0:
45 print "[using previously specified depot path %s]" % globalPrefix
47 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
49 print " %s //depot/my/project/ -- to import the current head"
50 print " %s //depot/my/project/@all -- to import everything"
51 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
53 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
57 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
58 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
60 globalPrefix
= args
[0]
69 if globalPrefix
.find("@") != -1:
70 atIdx
= globalPrefix
.index("@")
71 changeRange
= globalPrefix
[atIdx
:]
72 if changeRange
== "@all":
74 elif changeRange
.find(",") == -1:
75 revision
= changeRange
77 globalPrefix
= globalPrefix
[0:atIdx
]
78 elif globalPrefix
.find("#") != -1:
79 hashIdx
= globalPrefix
.index("#")
80 revision
= globalPrefix
[hashIdx
:]
81 globalPrefix
= globalPrefix
[0:hashIdx
]
82 elif len(previousDepotPath
) == 0:
85 if globalPrefix
.endswith("..."):
86 globalPrefix
= globalPrefix
[:-3]
88 if not globalPrefix
.endswith("/"):
92 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
96 entry
= marshal
.load(pipe
)
104 list = p4CmdList(cmd
)
110 def extractFilesFromCommit(commit
):
113 while commit
.has_key("depotFile%s" % fnum
):
114 path
= commit
["depotFile%s" % fnum
]
115 if not path
.startswith(globalPrefix
):
117 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, globalPrefix
, change
)
123 file["rev"] = commit
["rev%s" % fnum
]
124 file["action"] = commit
["action%s" % fnum
]
125 file["type"] = commit
["type%s" % fnum
]
130 def isSubPathOf(first
, second
):
131 if not first
.startswith(second
):
135 return first
[len(second
)] == "/"
137 def branchesForCommit(files
):
142 relativePath
= file["path"][len(globalPrefix
):]
143 # strip off the filename
144 relativePath
= relativePath
[0:relativePath
.rfind("/")]
146 # if len(branches) == 0:
147 # branches.add(relativePath)
148 # knownBranches.add(relativePath)
151 ###### this needs more testing :)
153 for branch
in branches
:
154 if relativePath
== branch
:
157 # if relativePath.startswith(branch):
158 if isSubPathOf(relativePath
, branch
):
161 # if branch.startswith(relativePath):
162 if isSubPathOf(branch
, relativePath
):
163 branches
.remove(branch
)
169 for branch
in knownBranches
:
170 #if relativePath.startswith(branch):
171 if isSubPathOf(relativePath
, branch
):
172 if len(branches
) == 0:
173 relativePath
= branch
181 branches
.add(relativePath
)
182 knownBranches
.add(relativePath
)
186 def commit(details
, files
, branch
, branchPrefix
):
190 global committedChanges
192 epoch
= details
["time"]
193 author
= details
["user"]
195 gitStream
.write("commit %s\n" % branch
)
196 gitStream
.write("mark :%s\n" % details
["change"])
197 committedChanges
.add(int(details
["change"]))
200 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
202 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
204 gitStream
.write("committer %s\n" % committer
)
206 gitStream
.write("data <<EOT\n")
207 gitStream
.write(details
["desc"])
208 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
209 gitStream
.write("EOT\n\n")
211 if len(initialParent
) > 0:
212 gitStream
.write("from %s\n" % initialParent
)
215 #mergedBranches = Set()
219 if lastChange
== 0 or not detectBranches
:
222 if not path
.startswith(branchPrefix
):
224 action
= file["action"]
225 if action
!= "integrate" and action
!= "branch":
228 depotPath
= path
+ "#" + rev
230 log
= p4CmdList("filelog \"%s\"" % depotPath
)
232 print "eek! I got confused by the filelog of %s" % depotPath
236 if log
["action0"] != action
:
237 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
240 branchAction
= log
["how0,0"]
241 # if branchAction == "branch into" or branchAction == "ignored":
242 # continue # ignore for branching
244 if not branchAction
.endswith(" from"):
245 continue # ignore for branching
246 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
249 source
= log
["file0,0"]
250 if source
.startswith(branchPrefix
):
253 lastSourceRev
= log
["erev0,0"]
255 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
256 if len(sourceLog
) != 1:
257 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
259 sourceLog
= sourceLog
[0]
261 change
= int(sourceLog
["change0"])
264 # relPath = source[len(globalPrefix):]
266 # for branch in knownBranches:
267 # if relPath.startswith(branch) and branch not in mergedBranches:
268 # gitStream.write("merge refs/heads/%s\n" % branch)
269 # mergedBranches.add(branch)
273 if merge
in committedChanges
:
274 gitStream
.write("merge :%s\n" % merge
)
278 if not path
.startswith(branchPrefix
):
280 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path
, branchPrefix
, details
["change"])
283 depotPath
= path
+ "#" + rev
284 relPath
= path
[len(branchPrefix
):]
285 action
= file["action"]
287 if action
== "delete":
288 gitStream
.write("D %s\n" % relPath
)
291 if file["type"].startswith("x"):
294 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
296 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
297 gitStream
.write("data %s\n" % len(data
))
298 gitStream
.write(data
)
299 gitStream
.write("\n")
301 gitStream
.write("\n")
303 lastChange
= int(details
["change"])
308 for output
in p4CmdList("users"):
309 if not output
.has_key("User"):
311 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
316 if len(changeRange
) == 0:
318 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
320 if output
.endswith("\n"):
322 tagIdx
= output
.index(" tags/p4/")
323 caretIdx
= output
.find("^")
327 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
328 changeRange
= "@%s,#head" % rev
329 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
330 initialTag
= "p4/%s" % (int(rev
) - 1)
334 sys
.stderr
.write("\n")
336 tz
= - time
.timezone
/ 36
337 tzsign
= ("%s" % tz
)[0]
338 if tzsign
!= '+' and tzsign
!= '-':
339 tz
= "+" + ("%s" % tz
)
341 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
343 if len(revision
) > 0:
344 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
346 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
347 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
348 details
["change"] = revision
352 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
353 change
= int(info
["change"])
354 if change
> newestRevision
:
355 newestRevision
= change
357 if info
["action"] == "delete":
360 for prop
in [ "depotFile", "rev", "action", "type" ]:
361 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
363 fileCnt
= fileCnt
+ 1
365 details
["change"] = newestRevision
368 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
370 print gitError
.read()
375 if len(changesFile
) > 0:
376 output
= open(changesFile
).readlines()
379 changeSet
.add(int(line
))
381 for change
in changeSet
:
382 changes
.append(change
)
386 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
389 changeNum
= line
.split(" ")[1]
390 changes
.append(changeNum
)
394 if len(changes
) == 0:
396 print "no changes to import!"
400 for change
in changes
:
401 description
= p4Cmd("describe %s" % change
)
404 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
409 files
= extractFilesFromCommit(description
)
411 for branch
in branchesForCommit(files
):
412 knownBranches
.add(branch
)
413 branchPrefix
= globalPrefix
+ branch
+ "/"
414 branch
= "refs/heads/" + branch
415 commit(description
, files
, branch
, branchPrefix
)
417 commit(description
, files
, branch
, globalPrefix
)
419 # print gitError.read()
425 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
426 gitStream
.write("from %s\n\n" % branch
);
433 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
434 if len(initialTag
) > 0:
435 os
.popen("git tag -d %s" % initialTag
).read()