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
15 branch
= "refs/heads/master"
16 prefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
21 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "branch=" ])
22 except getopt
.GetoptError
:
23 print "fixme, syntax error"
28 branch
= "refs/heads/" + a
30 if len(args
) == 0 and len(prefix
) != 0:
31 print "[using previously specified depot path %s]" % prefix
33 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
35 print " %s //depot/my/project/ -- to import the current head"
36 print " %s //depot/my/project/@all -- to import everything"
37 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
39 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
43 if len(prefix
) != 0 and prefix
!= args
[0]:
44 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix
, args
[0])
56 if prefix
.find("@") != -1:
57 atIdx
= prefix
.index("@")
58 changeRange
= prefix
[atIdx
:]
59 if changeRange
== "@all":
61 elif changeRange
.find(",") == -1:
62 revision
= changeRange
64 prefix
= prefix
[0:atIdx
]
65 elif prefix
.find("#") != -1:
66 hashIdx
= prefix
.index("#")
67 revision
= prefix
[hashIdx
:]
68 prefix
= prefix
[0:hashIdx
]
69 elif len(previousDepotPath
) == 0:
72 if prefix
.endswith("..."):
75 if not prefix
.endswith("/"):
79 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
83 entry
= marshal
.load(pipe
)
103 epoch
= details
["time"]
104 author
= details
["user"]
106 gitStream
.write("commit %s\n" % branch
)
109 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
111 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
113 gitStream
.write("committer %s\n" % committer
)
115 gitStream
.write("data <<EOT\n")
116 gitStream
.write(details
["desc"])
117 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, details
["change"]))
118 gitStream
.write("EOT\n\n")
120 if len(initialParent
) > 0:
121 gitStream
.write("from %s\n" % initialParent
)
125 while details
.has_key("depotFile%s" % fnum
):
126 path
= details
["depotFile%s" % fnum
]
127 if not path
.startswith(prefix
):
128 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
132 rev
= details
["rev%s" % fnum
]
133 depotPath
= path
+ "#" + rev
134 relPath
= path
[len(prefix
):]
135 action
= details
["action%s" % fnum
]
137 if action
== "delete":
138 gitStream
.write("D %s\n" % relPath
)
141 if details
["type%s" % fnum
].startswith("x"):
144 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
146 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
147 gitStream
.write("data %s\n" % len(data
))
148 gitStream
.write(data
)
149 gitStream
.write("\n")
153 gitStream
.write("\n")
155 lastChange
= details
["change"]
156 lastCommitter
= committer
161 for output
in p4CmdList("users"):
162 if not output
.has_key("User"):
164 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
169 if len(changeRange
) == 0:
171 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
173 if output
.endswith("\n"):
175 tagIdx
= output
.index(" tags/p4/")
176 caretIdx
= output
.find("^")
180 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
181 changeRange
= "@%s,#head" % rev
182 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
183 initialTag
= "p4/%s" % (int(rev
) - 1)
187 sys
.stderr
.write("\n")
189 tz
= - time
.timezone
/ 36
190 tzsign
= ("%s" % tz
)[0]
191 if tzsign
!= '+' and tzsign
!= '-':
192 tz
= "+" + ("%s" % tz
)
194 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
196 if len(revision
) > 0:
197 print "Doing initial import of %s from revision %s" % (prefix
, revision
)
199 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
200 details
["desc"] = "Initial import of %s from the state at revision %s" % (prefix
, revision
)
201 details
["change"] = revision
205 for info
in p4CmdList("files %s...%s" % (prefix
, revision
)):
206 change
= int(info
["change"])
207 if change
> newestRevision
:
208 newestRevision
= change
210 if info
["action"] == "delete":
213 for prop
in [ "depotFile", "rev", "action", "type" ]:
214 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
216 fileCnt
= fileCnt
+ 1
218 details
["change"] = newestRevision
223 print gitError
.read()
226 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
230 changeNum
= line
.split(" ")[1]
231 changes
.append(changeNum
)
235 if len(changes
) == 0:
236 print "no changes to import!"
240 for change
in changes
:
241 description
= p4Cmd("describe %s" % change
)
243 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
250 print gitError
.read()
255 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
256 gitStream
.write("from %s\n\n" % branch
);
263 os
.popen("git-repo-config p4.depotpath %s" % prefix
).read()
264 if len(initialTag
) > 0:
265 os
.popen("git tag -d %s" % initialTag
).read()