1#!/usr/bin/python
2#
3# p4-git-sync.py
4#
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>
9#
10
11import os, string, shelve, stat
12import getopt, sys, marshal, tempfile
13
14def p4CmdList(cmd):
15 cmd = "p4 -G %s" % cmd
16 pipe = os.popen(cmd, "rb")
17
18 result = []
19 try:
20 while True:
21 entry = marshal.load(pipe)
22 result.append(entry)
23 except EOFError:
24 pass
25 pipe.close()
26
27 return result
28
29def p4Cmd(cmd):
30 list = p4CmdList(cmd)
31 result = {}
32 for entry in list:
33 result.update(entry)
34 return result;
35
36try:
37 opts, args = getopt.getopt(sys.argv[1:], "", [ "continue", "git-dir=", "origin=", "reset", "master=",
38 "submit-log-subst=", "log-substitutions=", "interactive",
39 "dry-run" ])
40except getopt.GetoptError:
41 print "fixme, syntax error"
42 sys.exit(1)
43
44logSubstitutions = {}
45logSubstitutions["<enter description here>"] = "%log%"
46logSubstitutions["\tDetails:"] = "\tDetails: %log%"
47gitdir = os.environ.get("GIT_DIR", "")
48origin = "origin"
49master = "master"
50firstTime = True
51reset = False
52interactive = False
53dryRun = False
54
55for 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
79
80if len(gitdir) == 0:
81 gitdir = ".git"
82else:
83 os.environ["GIT_DIR"] = gitdir
84
85configFile = gitdir + "/p4-git-sync.cfg"
86
87origin = "origin"
88if len(args) == 1:
89 origin = args[0]
90
91def die(msg):
92 sys.stderr.write(msg + "\n")
93 sys.exit(1)
94
95def system(cmd):
96 if os.system(cmd) != 0:
97 die("command failed: %s" % cmd)
98
99def check():
100 return
101 if len(p4CmdList("opened ...")) > 0:
102 die("You have files opened with perforce! Close them before starting the sync.")
103
104def start(config):
105 if len(config) > 0 and not reset:
106 die("Cannot start sync. Previous sync config found at %s" % configFile)
107
108 #if len(os.popen("git-update-index --refresh").read()) > 0:
109 # die("Your working tree is not clean. Check with git status!")
110
111 commits = []
112 for line in os.popen("git-rev-list --no-merges %s..%s" % (origin, master)).readlines():
113 commits.append(line[:-1])
114 commits.reverse()
115
116 config["commits"] = commits
117
118# print "Cleaning index..."
119# system("git checkout -f")
120
121def prepareLogMessage(template, message):
122 result = ""
123
124 for line in template.split("\n"):
125 if line.startswith("#"):
126 result += line + "\n"
127 continue
128
129 substituted = False
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"
136 substituted = True
137 break
138
139 if not substituted:
140 result += line + "\n"
141
142 return result
143
144def apply(id):
145 global interactive
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()
148 filesToAdd = set()
149 filesToDelete = set()
150 for line in diff:
151 modifier = line[0]
152 path = line[1:].strip()
153 if modifier == "M":
154 system("p4 edit %s" % path)
155 elif modifier == "A":
156 filesToAdd.add(path)
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)
163 else:
164 die("unknown modifier %s for %s" % (modifier, path))
165
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)
171
172 for f in filesToAdd:
173 system("p4 add %s" % f)
174 for f in filesToDelete:
175 system("p4 revert %s" % f)
176 system("p4 delete %s" % f)
177
178 logMessage = ""
179 foundTitle = False
180 for log in os.popen("git-cat-file commit %s" % id).readlines():
181 if not foundTitle:
182 if len(log) == 1:
183 foundTitle = 1
184 continue
185
186 if len(logMessage) > 0:
187 logMessage += "\t"
188 logMessage += log
189
190 template = os.popen("p4 change -o").read()
191
192 if interactive:
193 submitTemplate = prepareLogMessage(template, logMessage)
194 diff = os.popen("p4 diff -du ...").read()
195
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():
202 diff += "+" + line
203 f.close()
204
205 pipe = os.popen("less", "w")
206 pipe.write(submitTemplate + diff)
207 pipe.close()
208
209 response = "e"
210 while response == "e":
211 response = raw_input("Do you want to submit this change (y/e/n)? ")
212 if response == "e":
213 [handle, fileName] = tempfile.mkstemp()
214 tmpFile = os.fdopen(handle, "w+")
215 tmpFile.write(submitTemplate)
216 tmpFile.close()
217 editor = os.environ.get("EDITOR", "vi")
218 system(editor + " " + fileName)
219 tmpFile = open(fileName, "r")
220 submitTemplate = tmpFile.read()
221 tmpFile.close()
222 os.remove(fileName)
223
224 if response == "y" or response == "yes":
225 if dryRun:
226 print submitTemplate
227 raw_input("Press return to continue...")
228 else:
229 pipe = os.popen("p4 submit -i", "w")
230 pipe.write(submitTemplate)
231 pipe.close()
232 else:
233 print "Not submitting!"
234 interactive = False
235 else:
236 fileName = "submit.txt"
237 file = open(fileName, "w+")
238 file.write(prepareLogMessage(template, logMessage))
239 file.close()
240 print "Perforce submit template written as %s. Please review/edit and then use p4 submit -i < %s to submit directly!" % (fileName, fileName)
241
242check()
243
244config = shelve.open(configFile, writeback=True)
245
246if firstTime:
247 start(config)
248
249commits = config.get("commits", [])
250
251while len(commits) > 0:
252 firstTime = False
253 commit = commits[0]
254 commits = commits[1:]
255 config["commits"] = commits
256 apply(commit)
257 if not interactive:
258 break
259
260config.close()
261
262if len(commits) == 0:
263 if firstTime:
264 print "No changes found to apply between %s and current HEAD" % origin
265 else:
266 print "All changes applied!"
267 os.remove(configFile)
268