chiark / gitweb /
Revert 'Changed rebasing safety check to look for reachability of
[stgit] / stgit / gitmergeonefile.py
CommitLineData
3659ef88
CM
1"""Performs a 3-way merge for GIT files
2"""
3
4__copyright__ = """
5Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License version 2 as
9published by the Free Software Foundation.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19"""
20
21import sys, os
170f576b 22from stgit import basedir
f7ed76a9 23from stgit.config import config, file_extensions, ConfigOption
27ac2b7e 24from stgit.utils import append_string, out
3659ef88
CM
25
26
27class GitMergeException(Exception):
28 pass
29
30
31#
32# Options
33#
eee7283e
CM
34merger = ConfigOption('stgit', 'merger')
35keeporig = ConfigOption('stgit', 'keeporig')
3659ef88
CM
36
37#
38# Utility functions
39#
40def __str2none(x):
41 if x == '':
42 return None
43 else:
44 return x
45
46def __output(cmd):
47 f = os.popen(cmd, 'r')
48 string = f.readline().rstrip()
49 if f.close():
50 raise GitMergeException, 'Error: failed to execute "%s"' % cmd
51 return string
52
53def __checkout_files(orig_hash, file1_hash, file2_hash,
54 path,
55 orig_mode, file1_mode, file2_mode):
56 """Check out the files passed as arguments
57 """
58 global orig, src1, src2
59
1e075406
CM
60 extensions = file_extensions()
61
3659ef88 62 if orig_hash:
1e075406 63 orig = path + extensions['ancestor']
3659ef88
CM
64 tmp = __output('git-unpack-file %s' % orig_hash)
65 os.chmod(tmp, int(orig_mode, 8))
66 os.renames(tmp, orig)
67 if file1_hash:
1e075406 68 src1 = path + extensions['current']
3659ef88
CM
69 tmp = __output('git-unpack-file %s' % file1_hash)
70 os.chmod(tmp, int(file1_mode, 8))
71 os.renames(tmp, src1)
72 if file2_hash:
1e075406 73 src2 = path + extensions['patched']
3659ef88
CM
74 tmp = __output('git-unpack-file %s' % file2_hash)
75 os.chmod(tmp, int(file2_mode, 8))
76 os.renames(tmp, src2)
77
8d415553
CM
78 if file1_hash and not os.path.exists(path):
79 # the current file might be removed by GIT when it is a new
80 # file added in both branches. Just re-generate it
81 tmp = __output('git-unpack-file %s' % file1_hash)
82 os.chmod(tmp, int(file1_mode, 8))
83 os.renames(tmp, path)
84
3659ef88
CM
85def __remove_files(orig_hash, file1_hash, file2_hash):
86 """Remove any temporary files
87 """
88 if orig_hash:
89 os.remove(orig)
90 if file1_hash:
91 os.remove(src1)
92 if file2_hash:
93 os.remove(src2)
3659ef88 94
3659ef88
CM
95def __conflict(path):
96 """Write the conflict file for the 'path' variable and exit
97 """
170f576b 98 append_string(os.path.join(basedir.get(), 'conflicts'), path)
3659ef88
CM
99
100
f7ed76a9
CM
101def interactive_merge(filename):
102 """Run the interactive merger on the given file. Note that the
103 index should not have any conflicts.
104 """
f7ed76a9
CM
105 extensions = file_extensions()
106
107 ancestor = filename + extensions['ancestor']
108 current = filename + extensions['current']
109 patched = filename + extensions['patched']
110
b6e961f2
CM
111 if os.path.isfile(ancestor):
112 three_way = True
113 files_dict = {'branch1': current,
114 'ancestor': ancestor,
115 'branch2': patched,
116 'output': filename}
117 imerger = config.get('stgit.i3merge')
118 else:
119 three_way = False
120 files_dict = {'branch1': current,
121 'branch2': patched,
122 'output': filename}
123 imerger = config.get('stgit.i2merge')
124
125 if not imerger:
126 raise GitMergeException, 'No interactive merge command configured'
127
128 # check whether we have all the files for the merge
129 for fn in [filename, current, patched]:
f7ed76a9
CM
130 if not os.path.isfile(fn):
131 raise GitMergeException, \
b6e961f2 132 'Cannot run the interactive merge: "%s" missing' % fn
f7ed76a9
CM
133
134 mtime = os.path.getmtime(filename)
135
27ac2b7e
KH
136 out.info('Trying the interactive %s merge'
137 % (three_way and 'three-way' or 'two-way'))
f7ed76a9 138
b6e961f2 139 err = os.system(imerger % files_dict)
f7ed76a9
CM
140 if err != 0:
141 raise GitMergeException, 'The interactive merge failed: %d' % err
142 if not os.path.isfile(filename):
143 raise GitMergeException, 'The "%s" file is missing' % filename
144 if mtime == os.path.getmtime(filename):
145 raise GitMergeException, 'The "%s" file was not modified' % filename
146
147
3659ef88
CM
148#
149# Main algorithm
150#
151def merge(orig_hash, file1_hash, file2_hash,
152 path,
153 orig_mode, file1_mode, file2_mode):
154 """Three-way merge for one file algorithm
155 """
156 __checkout_files(orig_hash, file1_hash, file2_hash,
157 path,
158 orig_mode, file1_mode, file2_mode)
159
160 # file exists in origin
161 if orig_hash:
162 # modified in both
163 if file1_hash and file2_hash:
164 # if modes are the same (git-read-tree probably dealt with it)
165 if file1_hash == file2_hash:
166 if os.system('git-update-index --cacheinfo %s %s %s'
167 % (file1_mode, file1_hash, path)) != 0:
27ac2b7e 168 out.error('git-update-index failed')
3659ef88
CM
169 __conflict(path)
170 return 1
171 if os.system('git-checkout-index -u -f -- %s' % path):
27ac2b7e 172 out.error('git-checkout-index failed')
3659ef88
CM
173 __conflict(path)
174 return 1
175 if file1_mode != file2_mode:
27ac2b7e 176 out.error('File added in both, permissions conflict')
3659ef88
CM
177 __conflict(path)
178 return 1
179 # 3-way merge
180 else:
5b99888b
CM
181 merge_ok = os.system(str(merger) % {'branch1': src1,
182 'ancestor': orig,
183 'branch2': src2,
184 'output': path }) == 0
3659ef88
CM
185
186 if merge_ok:
187 os.system('git-update-index -- %s' % path)
188 __remove_files(orig_hash, file1_hash, file2_hash)
189 return 0
190 else:
27ac2b7e
KH
191 out.error('Three-way merge tool failed for file "%s"'
192 % path)
3659ef88
CM
193 # reset the cache to the first branch
194 os.system('git-update-index --cacheinfo %s %s %s'
195 % (file1_mode, file1_hash, path))
f7ed76a9 196
c73e63b7 197 if config.get('stgit.autoimerge') == 'yes':
f7ed76a9
CM
198 try:
199 interactive_merge(path)
200 except GitMergeException, ex:
201 # interactive merge failed
27ac2b7e 202 out.error(ex)
f7ed76a9
CM
203 if str(keeporig) != 'yes':
204 __remove_files(orig_hash, file1_hash,
205 file2_hash)
206 __conflict(path)
207 return 1
208 # successful interactive merge
0f4eba6a 209 os.system('git-update-index -- %s' % path)
3659ef88 210 __remove_files(orig_hash, file1_hash, file2_hash)
f7ed76a9
CM
211 return 0
212 else:
213 # no interactive merge, just mark it as conflict
214 if str(keeporig) != 'yes':
215 __remove_files(orig_hash, file1_hash, file2_hash)
216 __conflict(path)
217 return 1
218
3659ef88
CM
219 # file deleted in both or deleted in one and unchanged in the other
220 elif not (file1_hash or file2_hash) \
221 or file1_hash == orig_hash or file2_hash == orig_hash:
222 if os.path.exists(path):
223 os.remove(path)
224 __remove_files(orig_hash, file1_hash, file2_hash)
225 return os.system('git-update-index --remove -- %s' % path)
226 # file deleted in one and changed in the other
227 else:
228 # Do something here - we must at least merge the entry in
229 # the cache, instead of leaving it in U(nmerged) state. In
230 # fact, stg resolved does not handle that.
231
232 # Do the same thing cogito does - remove the file in any case.
233 os.system('git-update-index --remove -- %s' % path)
234
235 #if file1_hash:
236 ## file deleted upstream and changed in the patch. The
237 ## patch is probably going to move the changes
238 ## elsewhere.
239
240 #os.system('git-update-index --remove -- %s' % path)
241 #else:
242 ## file deleted in the patch and changed upstream. We
243 ## could re-delete it, but for now leave it there -
244 ## and let the user check if he still wants to remove
245 ## the file.
246
247 ## reset the cache to the first branch
248 #os.system('git-update-index --cacheinfo %s %s %s'
249 # % (file1_mode, file1_hash, path))
250 __conflict(path)
251 return 1
252
253 # file does not exist in origin
254 else:
255 # file added in both
256 if file1_hash and file2_hash:
257 # files are the same
258 if file1_hash == file2_hash:
259 if os.system('git-update-index --add --cacheinfo %s %s %s'
260 % (file1_mode, file1_hash, path)) != 0:
27ac2b7e 261 out.error('git-update-index failed')
3659ef88
CM
262 __conflict(path)
263 return 1
264 if os.system('git-checkout-index -u -f -- %s' % path):
27ac2b7e 265 out.error('git-checkout-index failed')
3659ef88
CM
266 __conflict(path)
267 return 1
268 if file1_mode != file2_mode:
27ac2b7e
KH
269 out.error('File "s" added in both, permissions conflict'
270 % path)
3659ef88
CM
271 __conflict(path)
272 return 1
b6e961f2 273 # files added in both but different
3659ef88 274 else:
27ac2b7e 275 out.error('File "%s" added in branches but different' % path)
b6e961f2
CM
276 # reset the cache to the first branch
277 os.system('git-update-index --cacheinfo %s %s %s'
278 % (file1_mode, file1_hash, path))
279
280 if config.get('stgit.autoimerge') == 'yes':
281 try:
282 interactive_merge(path)
283 except GitMergeException, ex:
284 # interactive merge failed
27ac2b7e 285 out.error(ex)
b6e961f2
CM
286 if str(keeporig) != 'yes':
287 __remove_files(orig_hash, file1_hash,
288 file2_hash)
289 __conflict(path)
290 return 1
291 # successful interactive merge
292 os.system('git-update-index -- %s' % path)
293 __remove_files(orig_hash, file1_hash, file2_hash)
294 return 0
295 else:
296 # no interactive merge, just mark it as conflict
297 if str(keeporig) != 'yes':
298 __remove_files(orig_hash, file1_hash, file2_hash)
299 __conflict(path)
300 return 1
3659ef88
CM
301 # file added in one
302 elif file1_hash or file2_hash:
303 if file1_hash:
304 mode = file1_mode
305 obj = file1_hash
306 else:
307 mode = file2_mode
308 obj = file2_hash
309 if os.system('git-update-index --add --cacheinfo %s %s %s'
310 % (mode, obj, path)) != 0:
27ac2b7e 311 out.error('git-update-index failed')
3659ef88
CM
312 __conflict(path)
313 return 1
314 __remove_files(orig_hash, file1_hash, file2_hash)
315 return os.system('git-checkout-index -u -f -- %s' % path)
316
317 # Unhandled case
27ac2b7e
KH
318 out.error('Unhandled merge conflict: "%s" "%s" "%s" "%s" "%s" "%s" "%s"'
319 % (orig_hash, file1_hash, file2_hash,
320 path,
321 orig_mode, file1_mode, file2_mode))
3659ef88
CM
322 __conflict(path)
323 return 1