Be nice and use /usr/bin/env python for the git-p4 scripts
[fast-export.git] / p4-git-sync.py
bloba4d126fd463c311d256991aa9dad7fcebebfec66
1 #!/usr/bin/env python
3 # p4-git-sync.py
5 # Author: Simon Hausmann <hausmann@kde.org>
6 # Copyright: 2007 Simon Hausmann <hausmann@kde.org>
7 # 2007 Trolltech ASA
8 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
11 import os, string, shelve, stat
12 import getopt, sys, marshal, tempfile
14 def p4CmdList(cmd):
15 cmd = "p4 -G %s" % cmd
16 pipe = os.popen(cmd, "rb")
18 result = []
19 try:
20 while True:
21 entry = marshal.load(pipe)
22 result.append(entry)
23 except EOFError:
24 pass
25 pipe.close()
27 return result
29 def p4Cmd(cmd):
30 list = p4CmdList(cmd)
31 result = {}
32 for entry in list:
33 result.update(entry)
34 return result;
36 try:
37 opts, args = getopt.getopt(sys.argv[1:], "", [ "continue", "git-dir=", "origin=", "reset", "master=",
38 "submit-log-subst=", "log-substitutions=", "interactive",
39 "dry-run" ])
40 except getopt.GetoptError:
41 print "fixme, syntax error"
42 sys.exit(1)
44 logSubstitutions = {}
45 logSubstitutions["<enter description here>"] = "%log%"
46 logSubstitutions["\tDetails:"] = "\tDetails: %log%"
47 gitdir = os.environ.get("GIT_DIR", "")
48 origin = "origin"
49 master = "master"
50 firstTime = True
51 reset = False
52 interactive = False
53 dryRun = False
55 for o, a in opts:
56 if o == "--git-dir":
57 gitdir = a
58 elif o == "--origin":
59 origin = a
60 elif o == "--master":
61 master = a
62 elif o == "--continue":
63 firstTime = False
64 elif o == "--reset":
65 reset = True
66 firstTime = True
67 elif o == "--submit-log-subst":
68 key = a.split("%")[0]
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":
76 interactive = True
77 elif o == "--dry-run":
78 dryRun = True
80 if len(gitdir) == 0:
81 gitdir = ".git"
82 else:
83 os.environ["GIT_DIR"] = gitdir
85 configFile = gitdir + "/p4-git-sync.cfg"
87 origin = "origin"
88 if len(args) == 1:
89 origin = args[0]
91 def die(msg):
92 sys.stderr.write(msg + "\n")
93 sys.exit(1)
95 def system(cmd):
96 if os.system(cmd) != 0:
97 die("command failed: %s" % cmd)
99 def check():
100 if len(p4CmdList("opened ...")) > 0:
101 die("You have files opened with perforce! Close them before starting the sync.")
103 def start(config):
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!")
110 commits = []
111 for line in os.popen("git-rev-list --no-merges %s..%s" % (origin, master)).readlines():
112 commits.append(line[:-1])
113 commits.reverse()
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):
124 result = ""
126 for line in template.split("\n"):
127 if line.startswith("#"):
128 result += line + "\n"
129 continue
131 substituted = False
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"
138 substituted = True
139 break
141 if not substituted:
142 result += line + "\n"
144 return result
146 def apply(id):
147 global interactive
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()
150 filesToAdd = set()
151 filesToDelete = set()
152 for line in diff:
153 modifier = line[0]
154 path = line[1:].strip()
155 if modifier == "M":
156 system("p4 edit %s" % path)
157 elif modifier == "A":
158 filesToAdd.add(path)
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)
165 else:
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)
174 for f in filesToAdd:
175 system("p4 add %s" % f)
176 for f in filesToDelete:
177 system("p4 revert %s" % f)
178 system("p4 delete %s" % f)
180 logMessage = ""
181 foundTitle = False
182 for log in os.popen("git-cat-file commit %s" % id).readlines():
183 if not foundTitle:
184 if len(log) == 1:
185 foundTitle = 1
186 continue
188 if len(logMessage) > 0:
189 logMessage += "\t"
190 logMessage += log
192 template = os.popen("p4 change -o").read()
194 if interactive:
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():
204 diff += "+" + line
205 f.close()
207 pipe = os.popen("less", "w")
208 pipe.write(submitTemplate + diff)
209 pipe.close()
211 response = "e"
212 while response == "e":
213 response = raw_input("Do you want to submit this change (y/e/n)? ")
214 if response == "e":
215 [handle, fileName] = tempfile.mkstemp()
216 tmpFile = os.fdopen(handle, "w+")
217 tmpFile.write(submitTemplate)
218 tmpFile.close()
219 editor = os.environ.get("EDITOR", "vi")
220 system(editor + " " + fileName)
221 tmpFile = open(fileName, "r")
222 submitTemplate = tmpFile.read()
223 tmpFile.close()
224 os.remove(fileName)
226 if response == "y" or response == "yes":
227 if dryRun:
228 print submitTemplate
229 raw_input("Press return to continue...")
230 else:
231 pipe = os.popen("p4 submit -i", "w")
232 pipe.write(submitTemplate)
233 pipe.close()
234 else:
235 print "Not submitting!"
236 interactive = False
237 else:
238 fileName = "submit.txt"
239 file = open(fileName, "w+")
240 file.write(prepareLogMessage(template, logMessage))
241 file.close()
242 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName)
244 check()
246 config = shelve.open(configFile, writeback=True)
248 if firstTime:
249 start(config)
251 commits = config.get("commits", [])
253 while len(commits) > 0:
254 firstTime = False
255 commit = commits[0]
256 commits = commits[1:]
257 config["commits"] = commits
258 apply(commit)
259 if not interactive:
260 break
262 config.close()
264 if len(commits) == 0:
265 if firstTime:
266 print "No changes found to apply between %s and current HEAD" % origin
267 else:
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)