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
19 committedChanges
= Set()
20 branch
= "refs/heads/master"
21 globalPrefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
22 detectBranches
= False
24 if len(globalPrefix
) != 0:
25 globalPrefix
= globalPrefix
[:-1]
28 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=", "detect-branches", "changesfile=" ])
29 except getopt
.GetoptError
:
30 print "fixme, syntax error"
35 branch
= "refs/heads/" + a
36 elif o
== "--detect-branches":
38 elif o
== "--changesfile":
41 if len(args
) == 0 and len(globalPrefix
) != 0:
42 print "[using previously specified depot path %s]" % globalPrefix
44 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
46 print " %s //depot/my/project/ -- to import the current head"
47 print " %s //depot/my/project/@all -- to import everything"
48 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
50 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
54 if len(globalPrefix
) != 0 and globalPrefix
!= args
[0]:
55 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (globalPrefix
, args
[0])
57 globalPrefix
= args
[0]
66 if globalPrefix
.find("@") != -1:
67 atIdx
= globalPrefix
.index("@")
68 changeRange
= globalPrefix
[atIdx
:]
69 if changeRange
== "@all":
71 elif changeRange
.find(",") == -1:
72 revision
= changeRange
74 globalPrefix
= globalPrefix
[0:atIdx
]
75 elif globalPrefix
.find("#") != -1:
76 hashIdx
= globalPrefix
.index("#")
77 revision
= globalPrefix
[hashIdx
:]
78 globalPrefix
= globalPrefix
[0:hashIdx
]
79 elif len(previousDepotPath
) == 0:
82 if globalPrefix
.endswith("..."):
83 globalPrefix
= globalPrefix
[:-3]
85 if not globalPrefix
.endswith("/"):
89 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
93 entry
= marshal
.load(pipe
)
101 list = p4CmdList(cmd
)
107 def extractFilesFromCommit(commit
):
110 while commit
.has_key("depotFile%s" % fnum
):
111 path
= commit
["depotFile%s" % fnum
]
112 if not path
.startswith(globalPrefix
):
113 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, globalPrefix
, change
)
119 file["rev"] = commit
["rev%s" % fnum
]
120 file["action"] = commit
["action%s" % fnum
]
121 file["type"] = commit
["type%s" % fnum
]
126 def isSubPathOf(first
, second
):
127 if not first
.startswith(second
):
131 return first
[len(second
)] == "/"
133 def branchesForCommit(files
):
138 relativePath
= file["path"][len(globalPrefix
):]
139 # strip off the filename
140 relativePath
= relativePath
[0:relativePath
.rfind("/")]
142 # if len(branches) == 0:
143 # branches.add(relativePath)
144 # knownBranches.add(relativePath)
147 ###### this needs more testing :)
149 for branch
in branches
:
150 if relativePath
== branch
:
153 # if relativePath.startswith(branch):
154 if isSubPathOf(relativePath
, branch
):
157 # if branch.startswith(relativePath):
158 if isSubPathOf(branch
, relativePath
):
159 branches
.remove(branch
)
165 for branch
in knownBranches
:
166 #if relativePath.startswith(branch):
167 if isSubPathOf(relativePath
, branch
):
168 if len(branches
) == 0:
169 relativePath
= branch
177 branches
.add(relativePath
)
178 knownBranches
.add(relativePath
)
182 def commit(details
, files
, branch
, branchPrefix
):
186 global committedChanges
188 epoch
= details
["time"]
189 author
= details
["user"]
191 gitStream
.write("commit %s\n" % branch
)
192 gitStream
.write("mark :%s\n" % details
["change"])
193 committedChanges
.add(int(details
["change"]))
196 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
198 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
200 gitStream
.write("committer %s\n" % committer
)
202 gitStream
.write("data <<EOT\n")
203 gitStream
.write(details
["desc"])
204 gitStream
.write("\n[ imported from %s; change %s ]\n" % (branchPrefix
, details
["change"]))
205 gitStream
.write("EOT\n\n")
207 if len(initialParent
) > 0:
208 gitStream
.write("from %s\n" % initialParent
)
211 #mergedBranches = Set()
215 if lastChange
== 0 or not detectBranches
:
218 if not path
.startswith(branchPrefix
):
220 action
= file["action"]
221 if action
!= "integrate" and action
!= "branch":
224 depotPath
= path
+ "#" + rev
226 log
= p4CmdList("filelog \"%s\"" % depotPath
)
228 print "eek! I got confused by the filelog of %s" % depotPath
232 if log
["action0"] != action
:
233 print "eek! wrong action in filelog for %s : found %s, expected %s" % (depotPath
, log
["action0"], action
)
236 branchAction
= log
["how0,0"]
237 # if branchAction == "branch into" or branchAction == "ignored":
238 # continue # ignore for branching
240 if not branchAction
.endswith(" from"):
241 continue # ignore for branching
242 # print "eek! file %s was not branched from but instead: %s" % (depotPath, branchAction)
245 source
= log
["file0,0"]
246 if source
.startswith(branchPrefix
):
249 lastSourceRev
= log
["erev0,0"]
251 sourceLog
= p4CmdList("filelog -m 1 \"%s%s\"" % (source
, lastSourceRev
))
252 if len(sourceLog
) != 1:
253 print "eek! I got confused by the source filelog of %s%s" % (source
, lastSourceRev
)
255 sourceLog
= sourceLog
[0]
257 change
= int(sourceLog
["change0"])
260 # relPath = source[len(globalPrefix):]
262 # for branch in knownBranches:
263 # if relPath.startswith(branch) and branch not in mergedBranches:
264 # gitStream.write("merge refs/heads/%s\n" % branch)
265 # mergedBranches.add(branch)
269 if merge
in committedChanges
:
270 gitStream
.write("merge :%s\n" % merge
)
274 if not path
.startswith(branchPrefix
):
275 print "\nchanged files: ignoring path %s outside of branch prefix %s in change %s" % (path
, branchPrefix
, details
["change"])
278 depotPath
= path
+ "#" + rev
279 relPath
= path
[len(branchPrefix
):]
280 action
= file["action"]
282 if action
== "delete":
283 gitStream
.write("D %s\n" % relPath
)
286 if file["type"].startswith("x"):
289 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
291 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
292 gitStream
.write("data %s\n" % len(data
))
293 gitStream
.write(data
)
294 gitStream
.write("\n")
296 gitStream
.write("\n")
298 lastChange
= int(details
["change"])
303 for output
in p4CmdList("users"):
304 if not output
.has_key("User"):
306 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
311 if len(changeRange
) == 0:
313 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
315 if output
.endswith("\n"):
317 tagIdx
= output
.index(" tags/p4/")
318 caretIdx
= output
.find("^")
322 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
323 changeRange
= "@%s,#head" % rev
324 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
325 initialTag
= "p4/%s" % (int(rev
) - 1)
329 sys
.stderr
.write("\n")
331 tz
= - time
.timezone
/ 36
332 tzsign
= ("%s" % tz
)[0]
333 if tzsign
!= '+' and tzsign
!= '-':
334 tz
= "+" + ("%s" % tz
)
336 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
338 if len(revision
) > 0:
339 print "Doing initial import of %s from revision %s" % (globalPrefix
, revision
)
341 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
342 details
["desc"] = "Initial import of %s from the state at revision %s" % (globalPrefix
, revision
)
343 details
["change"] = revision
347 for info
in p4CmdList("files %s...%s" % (globalPrefix
, revision
)):
348 change
= int(info
["change"])
349 if change
> newestRevision
:
350 newestRevision
= change
352 if info
["action"] == "delete":
355 for prop
in [ "depotFile", "rev", "action", "type" ]:
356 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
358 fileCnt
= fileCnt
+ 1
360 details
["change"] = newestRevision
363 commit(details
, extractFilesFromCommit(details
), branch
, globalPrefix
)
365 print gitError
.read()
370 if len(changesFile
) > 0:
371 output
= open(changesFile
).readlines()
374 changeSet
.add(int(line
))
376 for change
in changeSet
:
377 changes
.append(change
)
381 output
= os
.popen("p4 changes %s...%s" % (globalPrefix
, changeRange
)).readlines()
384 changeNum
= line
.split(" ")[1]
385 changes
.append(changeNum
)
389 if len(changes
) == 0:
390 print "no changes to import!"
394 for change
in changes
:
395 description
= p4Cmd("describe %s" % change
)
397 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
402 files
= extractFilesFromCommit(description
)
404 for branch
in branchesForCommit(files
):
405 knownBranches
.add(branch
)
406 branchPrefix
= globalPrefix
+ branch
+ "/"
407 branch
= "refs/heads/" + branch
408 commit(description
, files
, branch
, branchPrefix
)
410 commit(description
, files
, branch
, globalPrefix
)
412 # print gitError.read()
417 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
418 gitStream
.write("from %s\n\n" % branch
);
425 os
.popen("git-repo-config p4.depotpath %s" % globalPrefix
).read()
426 if len(initialTag
) > 0:
427 os
.popen("git tag -d %s" % initialTag
).read()