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
15 branch
= "refs/heads/p4"
16 prefix
= previousDepotPath
= os
.popen("git-repo-config --get p4.depotpath").read()
20 if len(sys
.argv
) == 1 and len(prefix
) != 0:
21 print "[using previously specified depot path %s]" % prefix
22 elif len(sys
.argv
) != 2:
23 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
25 print " %s //depot/my/project/ -- to import the current head"
26 print " %s //depot/my/project/@all -- to import everything"
27 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
29 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
33 if len(prefix
) != 0 and prefix
!= sys
.argv
[1]:
34 print "previous import used depot path %s and now %s was specified. this doesn't work!" % (prefix
, sys
.argv
[1])
43 if prefix
.find("@") != -1:
44 atIdx
= prefix
.index("@")
45 changeRange
= prefix
[atIdx
:]
46 if changeRange
== "@all":
48 elif changeRange
.find(",") == -1:
49 revision
= changeRange
51 prefix
= prefix
[0:atIdx
]
52 elif prefix
.find("#") != -1:
53 hashIdx
= prefix
.index("#")
54 revision
= prefix
[hashIdx
:]
55 prefix
= prefix
[0:hashIdx
]
56 elif len(previousDepotPath
) == 0:
59 if prefix
.endswith("..."):
62 if not prefix
.endswith("/"):
66 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
70 entry
= marshal
.load(pipe
)
88 epoch
= details
["time"]
89 author
= details
["user"]
91 gitStream
.write("commit %s\n" % branch
)
94 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
96 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
98 gitStream
.write("committer %s\n" % committer
)
100 gitStream
.write("data <<EOT\n")
101 gitStream
.write(details
["desc"])
102 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, details
["change"]))
103 gitStream
.write("EOT\n\n")
105 if len(initialParent
) > 0:
106 gitStream
.write("from %s\n" % initialParent
)
110 while details
.has_key("depotFile%s" % fnum
):
111 path
= details
["depotFile%s" % fnum
]
112 if not path
.startswith(prefix
):
113 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
117 rev
= details
["rev%s" % fnum
]
118 depotPath
= path
+ "#" + rev
119 relPath
= path
[len(prefix
):]
120 action
= details
["action%s" % fnum
]
122 if action
== "delete":
123 gitStream
.write("D %s\n" % relPath
)
126 if details
["type%s" % fnum
].startswith("x"):
129 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
131 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
132 gitStream
.write("data %s\n" % len(data
))
133 gitStream
.write(data
)
134 gitStream
.write("\n")
138 gitStream
.write("\n")
140 gitStream
.write("tag p4/%s\n" % details
["change"])
141 gitStream
.write("from %s\n" % branch
);
142 gitStream
.write("tagger %s\n" % committer
);
143 gitStream
.write("data 0\n\n")
149 for output
in p4CmdList("users"):
150 if not output
.has_key("User"):
152 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
157 if len(changeRange
) == 0:
159 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
161 tagIdx
= output
.index(" tags/p4/")
162 caretIdx
= output
.index("^")
163 rev
= int(output
[tagIdx
+ 9 : caretIdx
]) + 1
164 changeRange
= "@%s,#head" % rev
165 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
169 sys
.stderr
.write("\n")
171 tz
= - time
.timezone
/ 36
172 tzsign
= ("%s" % tz
)[0]
173 if tzsign
!= '+' and tzsign
!= '-':
174 tz
= "+" + ("%s" % tz
)
176 if len(revision
) > 0:
177 print "Doing initial import of %s from revision %s" % (prefix
, revision
)
179 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
180 details
["desc"] = "Initial import of %s from the state at revision %s" % (prefix
, revision
)
181 details
["change"] = revision
185 for info
in p4CmdList("files %s...%s" % (prefix
, revision
)):
186 change
= int(info
["change"])
187 if change
> newestRevision
:
188 newestRevision
= change
190 if info
["action"] == "delete":
193 for prop
in [ "depotFile", "rev", "action", "type" ]:
194 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
196 fileCnt
= fileCnt
+ 1
198 details
["change"] = newestRevision
200 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
204 print gitError
.read()
210 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
214 changeNum
= line
.split(" ")[1]
215 changes
.append(changeNum
)
219 if len(changes
) == 0:
220 print "no changes to import!"
223 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
226 for change
in changes
:
227 description
= p4Cmd("describe %s" % change
)
229 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))
241 os
.popen("git-repo-config p4.depotpath %s" % prefix
).read()