Lots of fixes done together with Paul
[fast-export.git] / p4-git-sync.py
blob5e307af8c61522e66bddecc62a34136736363d52
1 #!/usr/bin/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 return
101 if len(p4CmdList("opened ...")) > 0:
102 die("You have files opened with perforce! Close them before starting the sync.")
104 def start(config):
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!")
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()
116 config["commits"] = commits
118 # print "Cleaning index..."
119 # system("git checkout -f")
121 def prepareLogMessage(template, message):
122 result = ""
124 for line in template.split("\n"):
125 if line.startswith("#"):
126 result += line + "\n"
127 continue
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
139 if not substituted:
140 result += line + "\n"
142 return result
144 def 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))
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)
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)
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
186 if len(logMessage) > 0:
187 logMessage += "\t"
188 logMessage += log
190 template = os.popen("p4 change -o").read()
192 if interactive:
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():
202 diff += "+" + line
203 f.close()
205 pipe = os.popen("less", "w")
206 pipe.write(submitTemplate + diff)
207 pipe.close()
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)
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)
242 check()
244 config = shelve.open(configFile, writeback=True)
246 if firstTime:
247 start(config)
249 commits = config.get("commits", [])
251 while 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
260 config.close()
262 if 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)