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 if len(sys
.argv
) != 2:
16 print "usage: %s //depot/path[@revRange]" % sys
.argv
[0]
18 print " %s //depot/my/project/ -- to import everything"
19 print " %s //depot/my/project/@1,6 -- to import only from revision 1 to 6"
21 print " (a ... is not needed in the path p4 specification, it's added implicitly)"
25 branch
= "refs/heads/p4"
32 if prefix
.find("@") != -1:
33 atIdx
= prefix
.index("@")
34 changeRange
= prefix
[atIdx
:]
35 if changeRange
.find(",") == -1:
36 revision
= changeRange
38 prefix
= prefix
[0:atIdx
]
39 elif prefix
.find("#") != -1:
40 hashIdx
= prefix
.index("#")
41 revision
= prefix
[hashIdx
:]
42 prefix
= prefix
[0:hashIdx
]
44 if prefix
.endswith("..."):
47 if not prefix
.endswith("/"):
51 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
55 entry
= marshal
.load(pipe
)
73 epoch
= details
["time"]
74 author
= details
["user"]
76 gitStream
.write("commit %s\n" % branch
)
79 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
81 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
83 gitStream
.write("committer %s\n" % committer
)
85 gitStream
.write("data <<EOT\n")
86 gitStream
.write(details
["desc"])
87 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, details
["change"]))
88 gitStream
.write("EOT\n\n")
90 if len(initialParent
) > 0:
91 gitStream
.write("merge %s\n" % initialParent
)
95 while details
.has_key("depotFile%s" % fnum
):
96 path
= details
["depotFile%s" % fnum
]
97 if not path
.startswith(prefix
):
98 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
102 rev
= details
["rev%s" % fnum
]
103 depotPath
= path
+ "#" + rev
104 relPath
= path
[len(prefix
):]
105 action
= details
["action%s" % fnum
]
107 if action
== "delete":
108 gitStream
.write("D %s\n" % relPath
)
111 if details
["type%s" % fnum
].startswith("x"):
114 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
116 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
117 gitStream
.write("data %s\n" % len(data
))
118 gitStream
.write(data
)
119 gitStream
.write("\n")
123 gitStream
.write("\n")
125 gitStream
.write("tag p4/%s\n" % details
["change"])
126 gitStream
.write("from %s\n" % branch
);
127 gitStream
.write("tagger %s\n" % committer
);
128 gitStream
.write("data 0\n\n")
134 for output
in p4CmdList("users"):
135 if not output
.has_key("User"):
137 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
142 if len(changeRange
) == 0:
144 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
146 tagIdx
= output
.index(" tags/p4/")
147 caretIdx
= output
.index("^")
148 rev
= int(output
[tagIdx
+ 9 : caretIdx
]) + 1
149 changeRange
= "@%s,#head" % rev
150 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
154 sys
.stderr
.write("\n")
156 tz
= - time
.timezone
/ 36
158 if len(revision
) > 0:
159 print "Doing initial import of %s from revision %s" % (prefix
, revision
)
161 details
= { "user" : "git perforce import user", "time" : int(time
.time()) }
162 details
["desc"] = "Initial import of %s from the state at revision %s" % (prefix
, revision
)
163 details
["change"] = revision
167 for info
in p4CmdList("files %s...%s" % (prefix
, revision
)):
168 if info
["action"] == "delete":
170 for prop
in [ "depotFile", "rev", "action", "type" ]:
171 details
["%s%s" % (prop
, fileCnt
)] = info
[prop
]
173 change
= info
["change"]
174 if change
> newestRevision
:
175 newestRevision
= change
177 fileCnt
= fileCnt
+ 1
179 details
["change"] = newestRevision
181 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
188 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
192 changeNum
= line
.split(" ")[1]
193 changes
.append(changeNum
)
197 if len(changes
) == 0:
198 print "no changes to import!"
201 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
204 for change
in changes
:
205 description
= p4Cmd("describe %s" % change
)
207 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))