5 # Author: Simon Hausmann <hausmann@kde.org>
6 # Copyright: 2007 Simon Hausmann <hausmann@kde.org>
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
11 import os
, string
, shelve
, stat
12 import getopt
, sys
, marshal
, tempfile
15 cmd
= "p4 -G %s" % cmd
16 pipe
= os
.popen(cmd
, "rb")
21 entry
= marshal
.load(pipe
)
37 opts
, args
= getopt
.getopt(sys
.argv
[1:], "", [ "continue", "git-dir=", "origin=", "reset", "master=",
38 "submit-log-subst=", "log-substitutions=", "interactive",
40 except getopt
.GetoptError
:
41 print "fixme, syntax error"
45 logSubstitutions
["<enter description here>"] = "%log%"
46 logSubstitutions
["\tDetails:"] = "\tDetails: %log%"
47 gitdir
= os
.environ
.get("GIT_DIR", "")
62 elif o
== "--continue":
67 elif o
== "--submit-log-subst":
69 value
= a
.split("%")[1]
70 logSubstitutions
[key
] = value
71 elif o
== "--log-substitutions":
72 for line
in open(a
, "r").readlines():
73 tokens
= line
[:-1].split("=")
74 logSubstitutions
[tokens
[0]] = tokens
[1]
75 elif o
== "--interactive":
77 elif o
== "--dry-run":
83 os
.environ
["GIT_DIR"] = gitdir
85 configFile
= gitdir
+ "/p4-git-sync.cfg"
92 sys
.stderr
.write(msg
+ "\n")
96 if os
.system(cmd
) != 0:
97 die("command failed: %s" % cmd
)
100 if len(p4CmdList("opened ...")) > 0:
101 die("You have files opened with perforce! Close them before starting the sync.")
104 if len(config
) > 0 and not reset
:
105 die("Cannot start sync. Previous sync config found at %s" % configFile
)
107 #if len(os.popen("git-update-index --refresh").read()) > 0:
108 # die("Your working tree is not clean. Check with git status!")
111 for line
in os
.popen("git-rev-list --no-merges %s..%s" % (origin
, master
)).readlines():
112 commits
.append(line
[:-1])
115 config
["commits"] = commits
117 print "Creating temporary p4-sync branch from %s ..." % origin
118 system("git checkout -f -b p4-sync %s" % origin
)
120 # print "Cleaning index..."
121 # system("git checkout -f")
123 def prepareLogMessage(template
, message
):
126 for line
in template
.split("\n"):
127 if line
.startswith("#"):
128 result
+= line
+ "\n"
132 for key
in logSubstitutions
.keys():
133 if line
.find(key
) != -1:
134 value
= logSubstitutions
[key
]
135 value
= value
.replace("%log%", message
)
136 if value
!= "@remove@":
137 result
+= line
.replace(key
, value
) + "\n"
142 result
+= line
+ "\n"
148 print "Applying %s" % (os
.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
149 diff
= os
.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
151 filesToDelete
= set()
154 path
= line
[1:].strip()
156 system("p4 edit %s" % path
)
157 elif modifier
== "A":
159 if path
in filesToDelete
:
160 filesToDelete
.remove(path
)
161 elif modifier
== "D":
162 filesToDelete
.add(path
)
163 if path
in filesToAdd
:
164 filesToAdd
.remove(path
)
166 die("unknown modifier %s for %s" % (modifier
, path
))
168 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
169 system("git cherry-pick --no-commit \"%s\"" % id)
170 #system("git format-patch --stdout -k \"%s^\"..\"%s\" | git-am -k" % (id, id))
171 #system("git branch -D tmp")
172 #system("git checkout -f -b tmp \"%s^\"" % id)
175 system("p4 add %s" % f
)
176 for f
in filesToDelete
:
177 system("p4 revert %s" % f
)
178 system("p4 delete %s" % f
)
182 for log
in os
.popen("git-cat-file commit %s" % id).readlines():
188 if len(logMessage
) > 0:
192 template
= os
.popen("p4 change -o").read()
195 submitTemplate
= prepareLogMessage(template
, logMessage
)
196 diff
= os
.popen("p4 diff -du ...").read()
198 for newFile
in filesToAdd
:
199 diff
+= "==== new file ====\n"
200 diff
+= "--- /dev/null\n"
201 diff
+= "+++ %s\n" % newFile
202 f
= open(newFile
, "r")
203 for line
in f
.readlines():
207 pipe
= os
.popen("less", "w")
208 pipe
.write(submitTemplate
+ diff
)
212 while response
== "e":
213 response
= raw_input("Do you want to submit this change (y/e/n)? ")
215 [handle
, fileName
] = tempfile
.mkstemp()
216 tmpFile
= os
.fdopen(handle
, "w+")
217 tmpFile
.write(submitTemplate
)
219 editor
= os
.environ
.get("EDITOR", "vi")
220 system(editor
+ " " + fileName
)
221 tmpFile
= open(fileName
, "r")
222 submitTemplate
= tmpFile
.read()
226 if response
== "y" or response
== "yes":
229 raw_input("Press return to continue...")
231 pipe
= os
.popen("p4 submit -i", "w")
232 pipe
.write(submitTemplate
)
235 print "Not submitting!"
238 fileName
= "submit.txt"
239 file = open(fileName
, "w+")
240 file.write(prepareLogMessage(template
, logMessage
))
242 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName
, fileName
)
246 config
= shelve
.open(configFile
, writeback
=True)
251 commits
= config
.get("commits", [])
253 while len(commits
) > 0:
256 commits
= commits
[1:]
257 config
["commits"] = commits
264 if len(commits
) == 0:
266 print "No changes found to apply between %s and current HEAD" % origin
268 print "All changes applied!"
269 print "Deleting temporary p4-sync branch and going back to %s" % master
270 system("git checkout %s" % master
)
271 system("git branch -D p4-sync")
272 print "Cleaning out your perforce checkout by doing p4 edit ... ; p4 revert -a ..."
273 system("p4 edit ...")
274 system("p4 revert -a ...")
275 os
.remove(configFile
)