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
)
101 if len(p4CmdList("opened ...")) > 0:
102 die("You have files opened with perforce! Close them before starting the sync.")
105 if len(config
) > 0 and not reset
:
106 die("Cannot start sync. Previous sync config found at %s" % configFile
)
108 #if len(os.popen("git-update-index --refresh").read()) > 0:
109 # die("Your working tree is not clean. Check with git status!")
112 for line
in os
.popen("git-rev-list --no-merges %s..%s" % (origin
, master
)).readlines():
113 commits
.append(line
[:-1])
116 config
["commits"] = commits
118 # print "Cleaning index..."
119 # system("git checkout -f")
121 def prepareLogMessage(template
, message
):
124 for line
in template
.split("\n"):
125 if line
.startswith("#"):
126 result
+= line
+ "\n"
130 for key
in logSubstitutions
.keys():
131 if line
.find(key
) != -1:
132 value
= logSubstitutions
[key
]
133 value
= value
.replace("%log%", message
)
134 if value
!= "@remove@":
135 result
+= line
.replace(key
, value
) + "\n"
140 result
+= line
+ "\n"
146 print "Applying %s" % (os
.popen("git-log --max-count=1 --pretty=oneline %s" % id).read())
147 diff
= os
.popen("git diff-tree -r --name-status \"%s^\" \"%s\"" % (id, id)).readlines()
149 filesToDelete
= set()
152 path
= line
[1:].strip()
154 system("p4 edit %s" % path
)
155 elif modifier
== "A":
157 if path
in filesToDelete
:
158 filesToDelete
.remove(path
)
159 elif modifier
== "D":
160 filesToDelete
.add(path
)
161 if path
in filesToAdd
:
162 filesToAdd
.remove(path
)
164 die("unknown modifier %s for %s" % (modifier
, path
))
166 system("git-diff-files --name-only -z | git-update-index --remove -z --stdin")
167 system("git cherry-pick --no-commit \"%s\"" % id)
168 #system("git format-patch --stdout -k \"%s^\"..\"%s\" | git-am -k" % (id, id))
169 #system("git branch -D tmp")
170 #system("git checkout -f -b tmp \"%s^\"" % id)
173 system("p4 add %s" % f
)
174 for f
in filesToDelete
:
175 system("p4 revert %s" % f
)
176 system("p4 delete %s" % f
)
180 for log
in os
.popen("git-cat-file commit %s" % id).readlines():
186 if len(logMessage
) > 0:
190 template
= os
.popen("p4 change -o").read()
193 submitTemplate
= prepareLogMessage(template
, logMessage
)
194 diff
= os
.popen("p4 diff -du ...").read()
196 for newFile
in filesToAdd
:
197 diff
+= "==== new file ====\n"
198 diff
+= "--- /dev/null\n"
199 diff
+= "+++ %s\n" % newFile
200 f
= open(newFile
, "r")
201 for line
in f
.readlines():
205 pipe
= os
.popen("less", "w")
206 pipe
.write(submitTemplate
+ diff
)
210 while response
== "e":
211 response
= raw_input("Do you want to submit this change (y/e/n)? ")
213 [handle
, fileName
] = tempfile
.mkstemp()
214 tmpFile
= os
.fdopen(handle
, "w+")
215 tmpFile
.write(submitTemplate
)
217 editor
= os
.environ
.get("EDITOR", "vi")
218 system(editor
+ " " + fileName
)
219 tmpFile
= open(fileName
, "r")
220 submitTemplate
= tmpFile
.read()
224 if response
== "y" or response
== "yes":
227 raw_input("Press return to continue...")
229 pipe
= os
.popen("p4 submit -i", "w")
230 pipe
.write(submitTemplate
)
233 print "Not submitting!"
236 fileName
= "submit.txt"
237 file = open(fileName
, "w+")
238 file.write(prepareLogMessage(template
, logMessage
))
240 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName
, fileName
)
244 config
= shelve
.open(configFile
, writeback
=True)
249 commits
= config
.get("commits", [])
251 while len(commits
) > 0:
254 commits
= commits
[1:]
255 config
["commits"] = commits
262 if len(commits
) == 0:
264 print "No changes found to apply between %s and current HEAD" % origin
266 print "All changes applied!"
267 os
.remove(configFile
)