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 atIdx
= prefix
.index("@")
33 changeRange
= prefix
[atIdx
:]
34 prefix
= prefix
[0:atIdx
]
38 if prefix
.endswith("..."):
41 if not prefix
.endswith("/"):
45 pipe
= os
.popen("p4 -G %s" % cmd
, "rb")
49 entry
= marshal
.load(pipe
)
67 epoch
= details
["time"]
68 author
= details
["user"]
70 gitStream
.write("commit %s\n" % branch
)
73 committer
= "%s %s %s" % (users
[author
], epoch
, tz
)
75 committer
= "%s <a@b> %s %s" % (author
, epoch
, tz
)
77 gitStream
.write("committer %s\n" % committer
)
79 gitStream
.write("data <<EOT\n")
80 gitStream
.write(details
["desc"])
81 gitStream
.write("\n[ imported from %s; change %s ]\n" % (prefix
, change
))
82 gitStream
.write("EOT\n\n")
84 if len(initialParent
) > 0:
85 gitStream
.write("merge %s\n" % initialParent
)
89 while details
.has_key("depotFile%s" % fnum
):
90 path
= details
["depotFile%s" % fnum
]
91 if not path
.startswith(prefix
):
92 print "\nchanged files: ignoring path %s outside of %s in change %s" % (path
, prefix
, change
)
96 rev
= details
["rev%s" % fnum
]
97 depotPath
= path
+ "#" + rev
98 relPath
= path
[len(prefix
):]
99 action
= details
["action%s" % fnum
]
101 if action
== "delete":
102 gitStream
.write("D %s\n" % relPath
)
105 if details
["type%s" % fnum
].startswith("x"):
108 data
= os
.popen("p4 print -q \"%s\"" % depotPath
, "rb").read()
110 gitStream
.write("M %s inline %s\n" % (mode
, relPath
))
111 gitStream
.write("data %s\n" % len(data
))
112 gitStream
.write(data
)
113 gitStream
.write("\n")
117 gitStream
.write("\n")
119 gitStream
.write("tag p4/%s\n" % change
)
120 gitStream
.write("from %s\n" % branch
);
121 gitStream
.write("tagger %s\n" % committer
);
122 gitStream
.write("data 0\n\n")
128 for output
in p4CmdList("users"):
129 if not output
.has_key("User"):
131 users
[output
["User"]] = output
["FullName"] + " <" + output
["Email"] + ">"
136 if len(changeRange
) == 0:
138 sout
, sin
, serr
= popen2
.popen3("git-name-rev --tags `git-rev-parse %s`" % branch
)
140 tagIdx
= output
.index(" tags/p4/")
141 caretIdx
= output
.index("^")
142 revision
= int(output
[tagIdx
+ 9 : caretIdx
]) + 1
143 changeRange
= "@%s,#head" % revision
144 initialParent
= os
.popen("git-rev-parse %s" % branch
).read()[:-1]
148 output
= os
.popen("p4 changes %s...%s" % (prefix
, changeRange
)).readlines()
152 changeNum
= line
.split(" ")[1]
153 changes
.append(changeNum
)
157 if len(changes
) == 0:
158 print "no changes to import!"
161 sys
.stderr
.write("\n")
163 tz
= - time
.timezone
/ 36
165 gitOutput
, gitStream
, gitError
= popen2
.popen3("git-fast-import")
168 for change
in changes
:
169 description
= p4Cmd("describe %s" % change
)
171 sys
.stdout
.write("\rimporting revision %s (%s%%)" % (change
, cnt
* 100 / len(changes
)))