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])
55 if prefix
.find("@") != -1:
56 atIdx
= prefix
.index("@")
57 changeRange
= prefix
[atIdx
:]
58 if changeRange
== "@all":
60 elif changeRange
.find(",") == -1:
61 revision
= changeRange
63 prefix
= prefix
[0:atIdx
]
64 elif prefix
.find("#") != -1:
65 hashIdx
= prefix
.index("#")
66 revision
= prefix
[hashIdx
:]
67 prefix
= prefix
[0:hashIdx
]
68 elif len(previousDepotPath
) == 0:
71 if prefix
.endswith("..."):
74 if not prefix
.endswith("/"):
78 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
82 entry
= marshal
.load(pipe
)
101 epoch
= details
["time"]
102 author
= details
["user"]
104 gitStream
.write("commit %s\n" % branch
)
107 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
109 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
111 gitStream
.write("committer %s\n" % committer
)
113 gitStream
.write("data <<EOT\n")
114 gitStream
.write(details
["desc"])
115 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, details
["change"]))
116 gitStream
.write("EOT\n\n")
118 if len(initialParent
) > 0:
119 gitStream
.write("from %s\n" % initialParent
)
123 while details
.has_key("depotFile%s" % fnum
):
124 path
= details
["depotFile%s" % fnum
]
125 if not path
.startswith(prefix
):
126 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
130 rev
= details
["rev%s" % fnum
]
131 depotPath
= path
+ "#" + rev
132 relPath
= path
[len(prefix
):]
133 action
= details
["action%s" % fnum
]
135 if action
== "delete":
136 gitStream
.write("D %s\n" % relPath
)
139 if details
["type%s" % fnum
].startswith("x"):
142 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
144 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
145 gitStream
.write("data %s\n" % len(data
))
146 gitStream
.write(data
)
147 gitStream
.write("\n")
151 gitStream
.write("\n")
153 lastChange
= details
["change"]
158 for output
in p4CmdList("users"):
159 if not output
.has_key("User"):
161 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
166 if len(changeRange
) == 0:
168 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
170 if output
.endswith("\n"):
172 tagIdx
= output
.index(" tags/p4/")
173 caretIdx
= output
.find("^")
177 rev
= int(output
[tagIdx
+ 9 : endPos
]) + 1
178 changeRange
= "@%s,#head" % rev
179 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
180 initialTag
= "p4/%s" % (int(rev
) - 1)
184 sys
.stderr
.write("\n")
186 tz
= - time
.timezone
/ 36
187 tzsign
= ("%s" % tz
)[0]
188 if tzsign
!= '+' and tzsign
!= '-':
189 tz
= "+" + ("%s" % tz
)
191 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
193 if len(revision
) > 0:
194 print "Doing initial import of %s from revision %s" % (prefix
, revision
)
196 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
197 details
["desc"] = "Initial import of %s from the state at revision %s" % (prefix
, revision
)
198 details
["change"] = revision
202 for info
in p4CmdList("files %s...%s" % (prefix
, revision
)):
203 change
= int(info
["change"])
204 if change
> newestRevision
:
205 newestRevision
= change
207 if info
["action"] == "delete":
210 for prop
in [ "depotFile", "rev", "action", "type" ]:
211 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
213 fileCnt
= fileCnt
+ 1
215 details
["change"] = newestRevision
220 print gitError
.read()
223 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
227 changeNum
= line
.split(" ")[1]
228 changes
.append(changeNum
)
232 if len(changes
) == 0:
233 print "no changes to import!"
237 for change
in changes
:
238 description
= p4Cmd("describe %s" % change
)
240 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
247 print gitError
.read()
252 gitStream
.write("reset refs/tags/p4/%s\n" % lastChange
)
253 gitStream
.write("from %s\n\n" % branch
);
260 os
.popen("git-repo-config p4.depotpath %s" % prefix
).read()
261 if len(initialTag
) > 0:
262 os
.popen("git tag -d %s" % initialTag
).read()