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/p4"
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])
53 if prefix
.find("@") != -1:
54 atIdx
= prefix
.index("@")
55 changeRange
= prefix
[atIdx
:]
56 if changeRange
== "@all":
58 elif changeRange
.find(",") == -1:
59 revision
= changeRange
61 prefix
= prefix
[0:atIdx
]
62 elif prefix
.find("#") != -1:
63 hashIdx
= prefix
.index("#")
64 revision
= prefix
[hashIdx
:]
65 prefix
= prefix
[0:hashIdx
]
66 elif len(previousDepotPath
) == 0:
69 if prefix
.endswith("..."):
72 if not prefix
.endswith("/"):
76 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
80 entry
= marshal
.load(pipe
)
98 epoch
= details
["time"]
99 author
= details
["user"]
101 gitStream
.write("commit %s\n" % branch
)
104 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
106 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
108 gitStream
.write("committer %s\n" % committer
)
110 gitStream
.write("data <<EOT\n")
111 gitStream
.write(details
["desc"])
112 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, details
["change"]))
113 gitStream
.write("EOT\n\n")
115 if len(initialParent
) > 0:
116 gitStream
.write("from %s\n" % initialParent
)
120 while details
.has_key("depotFile%s" % fnum
):
121 path
= details
["depotFile%s" % fnum
]
122 if not path
.startswith(prefix
):
123 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
127 rev
= details
["rev%s" % fnum
]
128 depotPath
= path
+ "#" + rev
129 relPath
= path
[len(prefix
):]
130 action
= details
["action%s" % fnum
]
132 if action
== "delete":
133 gitStream
.write("D %s\n" % relPath
)
136 if details
["type%s" % fnum
].startswith("x"):
139 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
141 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
142 gitStream
.write("data %s\n" % len(data
))
143 gitStream
.write(data
)
144 gitStream
.write("\n")
148 gitStream
.write("\n")
150 gitStream
.write("tag p4/%s\n" % details
["change"])
151 gitStream
.write("from %s\n" % branch
);
152 gitStream
.write("tagger %s\n" % committer
);
153 gitStream
.write("data 0\n\n")
159 for output
in p4CmdList("users"):
160 if not output
.has_key("User"):
162 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
167 if len(changeRange
) == 0:
169 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
171 tagIdx
= output
.index(" tags/p4/")
172 caretIdx
= output
.index("^")
173 rev
= int(output
[tagIdx
+ 9 : caretIdx
]) + 1
174 changeRange
= "@%s,#head" % rev
175 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
179 sys
.stderr
.write("\n")
181 tz
= - time
.timezone
/ 36
182 tzsign
= ("%s" % tz
)[0]
183 if tzsign
!= '+' and tzsign
!= '-':
184 tz
= "+" + ("%s" % tz
)
186 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
188 if len(revision
) > 0:
189 print "Doing initial import of %s from revision %s" % (prefix
, revision
)
191 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
192 details
["desc"] = "Initial import of %s from the state at revision %s" % (prefix
, revision
)
193 details
["change"] = revision
197 for info
in p4CmdList("files %s...%s" % (prefix
, revision
)):
198 change
= int(info
["change"])
199 if change
> newestRevision
:
200 newestRevision
= change
202 if info
["action"] == "delete":
205 for prop
in [ "depotFile", "rev", "action", "type" ]:
206 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
208 fileCnt
= fileCnt
+ 1
210 details
["change"] = newestRevision
215 print gitError
.read()
218 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
222 changeNum
= line
.split(" ")[1]
223 changes
.append(changeNum
)
227 if len(changes
) == 0:
228 print "no changes to import!"
232 for change
in changes
:
233 description
= p4Cmd("describe %s" % change
)
235 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
242 print gitError
.read()
251 os
.popen("git-repo-config p4.depotpath %s" % prefix
).read()