1 """Performs a 3-way merge for GIT files
5 Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 from stgit import basedir
23 from stgit.config import config, file_extensions, ConfigOption
24 from stgit.utils import append_string
25 from stgit.out import *
28 class GitMergeException(Exception):
35 merger = ConfigOption('stgit', 'merger')
36 keeporig = ConfigOption('stgit', 'keeporig')
48 f = os.popen(cmd, 'r')
49 string = f.readline().rstrip()
51 raise GitMergeException, 'Error: failed to execute "%s"' % cmd
54 def __checkout_files(orig_hash, file1_hash, file2_hash,
56 orig_mode, file1_mode, file2_mode):
57 """Check out the files passed as arguments
59 global orig, src1, src2
61 extensions = file_extensions()
64 orig = path + extensions['ancestor']
65 tmp = __output('git-unpack-file %s' % orig_hash)
66 os.chmod(tmp, int(orig_mode, 8))
69 src1 = path + extensions['current']
70 tmp = __output('git-unpack-file %s' % file1_hash)
71 os.chmod(tmp, int(file1_mode, 8))
74 src2 = path + extensions['patched']
75 tmp = __output('git-unpack-file %s' % file2_hash)
76 os.chmod(tmp, int(file2_mode, 8))
79 if file1_hash and not os.path.exists(path):
80 # the current file might be removed by GIT when it is a new
81 # file added in both branches. Just re-generate it
82 tmp = __output('git-unpack-file %s' % file1_hash)
83 os.chmod(tmp, int(file1_mode, 8))
86 def __remove_files(orig_hash, file1_hash, file2_hash):
87 """Remove any temporary files
97 """Write the conflict file for the 'path' variable and exit
99 append_string(os.path.join(basedir.get(), 'conflicts'), path)
102 def interactive_merge(filename):
103 """Run the interactive merger on the given file. Note that the
104 index should not have any conflicts.
106 extensions = file_extensions()
108 ancestor = filename + extensions['ancestor']
109 current = filename + extensions['current']
110 patched = filename + extensions['patched']
112 if os.path.isfile(ancestor):
114 files_dict = {'branch1': current,
115 'ancestor': ancestor,
118 imerger = config.get('stgit.i3merge')
121 files_dict = {'branch1': current,
124 imerger = config.get('stgit.i2merge')
127 raise GitMergeException, 'No interactive merge command configured'
129 # check whether we have all the files for the merge
130 for fn in [filename, current, patched]:
131 if not os.path.isfile(fn):
132 raise GitMergeException, \
133 'Cannot run the interactive merge: "%s" missing' % fn
135 mtime = os.path.getmtime(filename)
137 out.info('Trying the interactive %s merge'
138 % (three_way and 'three-way' or 'two-way'))
140 err = os.system(imerger % files_dict)
142 raise GitMergeException, 'The interactive merge failed: %d' % err
143 if not os.path.isfile(filename):
144 raise GitMergeException, 'The "%s" file is missing' % filename
145 if mtime == os.path.getmtime(filename):
146 raise GitMergeException, 'The "%s" file was not modified' % filename
152 def merge(orig_hash, file1_hash, file2_hash,
154 orig_mode, file1_mode, file2_mode):
155 """Three-way merge for one file algorithm
157 __checkout_files(orig_hash, file1_hash, file2_hash,
159 orig_mode, file1_mode, file2_mode)
161 # file exists in origin
164 if file1_hash and file2_hash:
165 # if modes are the same (git-read-tree probably dealt with it)
166 if file1_hash == file2_hash:
167 if os.system('git-update-index --cacheinfo %s %s %s'
168 % (file1_mode, file1_hash, path)) != 0:
169 out.error('git-update-index failed')
172 if os.system('git-checkout-index -u -f -- %s' % path):
173 out.error('git-checkout-index failed')
176 if file1_mode != file2_mode:
177 out.error('File added in both, permissions conflict')
182 merge_ok = os.system(str(merger) % {'branch1': src1,
185 'output': path }) == 0
188 os.system('git-update-index -- %s' % path)
189 __remove_files(orig_hash, file1_hash, file2_hash)
192 out.error('Three-way merge tool failed for file "%s"'
194 # reset the cache to the first branch
195 os.system('git-update-index --cacheinfo %s %s %s'
196 % (file1_mode, file1_hash, path))
198 if config.get('stgit.autoimerge') == 'yes':
200 interactive_merge(path)
201 except GitMergeException, ex:
202 # interactive merge failed
204 if str(keeporig) != 'yes':
205 __remove_files(orig_hash, file1_hash,
209 # successful interactive merge
210 os.system('git-update-index -- %s' % path)
211 __remove_files(orig_hash, file1_hash, file2_hash)
214 # no interactive merge, just mark it as conflict
215 if str(keeporig) != 'yes':
216 __remove_files(orig_hash, file1_hash, file2_hash)
220 # file deleted in both or deleted in one and unchanged in the other
221 elif not (file1_hash or file2_hash) \
222 or file1_hash == orig_hash or file2_hash == orig_hash:
223 if os.path.exists(path):
225 __remove_files(orig_hash, file1_hash, file2_hash)
226 return os.system('git-update-index --remove -- %s' % path)
227 # file deleted in one and changed in the other
229 # Do something here - we must at least merge the entry in
230 # the cache, instead of leaving it in U(nmerged) state. In
231 # fact, stg resolved does not handle that.
233 # Do the same thing cogito does - remove the file in any case.
234 os.system('git-update-index --remove -- %s' % path)
237 ## file deleted upstream and changed in the patch. The
238 ## patch is probably going to move the changes
241 #os.system('git-update-index --remove -- %s' % path)
243 ## file deleted in the patch and changed upstream. We
244 ## could re-delete it, but for now leave it there -
245 ## and let the user check if he still wants to remove
248 ## reset the cache to the first branch
249 #os.system('git-update-index --cacheinfo %s %s %s'
250 # % (file1_mode, file1_hash, path))
254 # file does not exist in origin
257 if file1_hash and file2_hash:
259 if file1_hash == file2_hash:
260 if os.system('git-update-index --add --cacheinfo %s %s %s'
261 % (file1_mode, file1_hash, path)) != 0:
262 out.error('git-update-index failed')
265 if os.system('git-checkout-index -u -f -- %s' % path):
266 out.error('git-checkout-index failed')
269 if file1_mode != file2_mode:
270 out.error('File "s" added in both, permissions conflict'
274 # files added in both but different
276 out.error('File "%s" added in branches but different' % path)
277 # reset the cache to the first branch
278 os.system('git-update-index --cacheinfo %s %s %s'
279 % (file1_mode, file1_hash, path))
281 if config.get('stgit.autoimerge') == 'yes':
283 interactive_merge(path)
284 except GitMergeException, ex:
285 # interactive merge failed
287 if str(keeporig) != 'yes':
288 __remove_files(orig_hash, file1_hash,
292 # successful interactive merge
293 os.system('git-update-index -- %s' % path)
294 __remove_files(orig_hash, file1_hash, file2_hash)
297 # no interactive merge, just mark it as conflict
298 if str(keeporig) != 'yes':
299 __remove_files(orig_hash, file1_hash, file2_hash)
303 elif file1_hash or file2_hash:
310 if os.system('git-update-index --add --cacheinfo %s %s %s'
311 % (mode, obj, path)) != 0:
312 out.error('git-update-index failed')
315 __remove_files(orig_hash, file1_hash, file2_hash)
316 return os.system('git-checkout-index -u -f -- %s' % path)
319 out.error('Unhandled merge conflict: "%s" "%s" "%s" "%s" "%s" "%s" "%s"'
320 % (orig_hash, file1_hash, file2_hash,
322 orig_mode, file1_mode, file2_mode))