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.exception import *
23 from stgit import basedir
24 from stgit.config import config, file_extensions, ConfigOption
25 from stgit.utils import append_string
26 from stgit.out import *
27 from stgit.run import *
29 class GitMergeException(StgException):
36 merger = ConfigOption('stgit', 'merger')
37 keeporig = ConfigOption('stgit', 'keeporig')
49 exc = GitMergeException # use a custom exception class on errors
51 def __checkout_files(orig_hash, file1_hash, file2_hash,
53 orig_mode, file1_mode, file2_mode):
54 """Check out the files passed as arguments
56 global orig, src1, src2
58 extensions = file_extensions()
61 orig = path + extensions['ancestor']
62 tmp = MRun('git', 'unpack-file', orig_hash).output_one_line()
63 os.chmod(tmp, int(orig_mode, 8))
66 src1 = path + extensions['current']
67 tmp = MRun('git', 'unpack-file', file1_hash).output_one_line()
68 os.chmod(tmp, int(file1_mode, 8))
71 src2 = path + extensions['patched']
72 tmp = MRun('git', 'unpack-file', file2_hash).output_one_line()
73 os.chmod(tmp, int(file2_mode, 8))
76 if file1_hash and not os.path.exists(path):
77 # the current file might be removed by GIT when it is a new
78 # file added in both branches. Just re-generate it
79 tmp = MRun('git', 'unpack-file', file1_hash).output_one_line()
80 os.chmod(tmp, int(file1_mode, 8))
83 def __remove_files(orig_hash, file1_hash, file2_hash):
84 """Remove any temporary files
94 """Write the conflict file for the 'path' variable and exit
96 append_string(os.path.join(basedir.get(), 'conflicts'), path)
99 def interactive_merge(filename):
100 """Run the interactive merger on the given file. Note that the
101 index should not have any conflicts.
103 extensions = file_extensions()
105 ancestor = filename + extensions['ancestor']
106 current = filename + extensions['current']
107 patched = filename + extensions['patched']
109 if os.path.isfile(ancestor):
111 files_dict = {'branch1': current,
112 'ancestor': ancestor,
115 imerger = config.get('stgit.i3merge')
118 files_dict = {'branch1': current,
121 imerger = config.get('stgit.i2merge')
124 raise GitMergeException, 'No interactive merge command configured'
126 # check whether we have all the files for the merge
127 for fn in [filename, current, patched]:
128 if not os.path.isfile(fn):
129 raise GitMergeException, \
130 'Cannot run the interactive merge: "%s" missing' % fn
132 mtime = os.path.getmtime(filename)
134 out.info('Trying the interactive %s merge'
135 % (three_way and 'three-way' or 'two-way'))
137 err = os.system(imerger % files_dict)
139 raise GitMergeException, 'The interactive merge failed: %d' % err
140 if not os.path.isfile(filename):
141 raise GitMergeException, 'The "%s" file is missing' % filename
142 if mtime == os.path.getmtime(filename):
143 raise GitMergeException, 'The "%s" file was not modified' % filename
149 def merge(orig_hash, file1_hash, file2_hash,
151 orig_mode, file1_mode, file2_mode):
152 """Three-way merge for one file algorithm
154 __checkout_files(orig_hash, file1_hash, file2_hash,
156 orig_mode, file1_mode, file2_mode)
158 # file exists in origin
161 if file1_hash and file2_hash:
162 # if modes are the same (git-read-tree probably dealt with it)
163 if file1_hash == file2_hash:
164 if os.system('git update-index --cacheinfo %s %s %s'
165 % (file1_mode, file1_hash, path)) != 0:
166 out.error('git update-index failed')
169 if os.system('git checkout-index -u -f -- %s' % path):
170 out.error('git checkout-index failed')
173 if file1_mode != file2_mode:
174 out.error('File added in both, permissions conflict')
179 merge_ok = os.system(str(merger) % {'branch1': src1,
182 'output': path }) == 0
185 os.system('git update-index -- %s' % path)
186 __remove_files(orig_hash, file1_hash, file2_hash)
189 out.error('Three-way merge tool failed for file "%s"'
191 # reset the cache to the first branch
192 os.system('git update-index --cacheinfo %s %s %s'
193 % (file1_mode, file1_hash, path))
195 if config.get('stgit.autoimerge') == 'yes':
197 interactive_merge(path)
198 except GitMergeException, ex:
199 # interactive merge failed
201 if str(keeporig) != 'yes':
202 __remove_files(orig_hash, file1_hash,
206 # successful interactive merge
207 os.system('git update-index -- %s' % path)
208 __remove_files(orig_hash, file1_hash, file2_hash)
211 # no interactive merge, just mark it as conflict
212 if str(keeporig) != 'yes':
213 __remove_files(orig_hash, file1_hash, file2_hash)
217 # file deleted in both or deleted in one and unchanged in the other
218 elif not (file1_hash or file2_hash) \
219 or file1_hash == orig_hash or file2_hash == orig_hash:
220 if os.path.exists(path):
222 __remove_files(orig_hash, file1_hash, file2_hash)
223 return os.system('git update-index --remove -- %s' % path)
224 # file deleted in one and changed in the other
226 # Do something here - we must at least merge the entry in
227 # the cache, instead of leaving it in U(nmerged) state. In
228 # fact, stg resolved does not handle that.
230 # Do the same thing cogito does - remove the file in any case.
231 os.system('git update-index --remove -- %s' % path)
234 ## file deleted upstream and changed in the patch. The
235 ## patch is probably going to move the changes
238 #os.system('git update-index --remove -- %s' % path)
240 ## file deleted in the patch and changed upstream. We
241 ## could re-delete it, but for now leave it there -
242 ## and let the user check if he still wants to remove
245 ## reset the cache to the first branch
246 #os.system('git update-index --cacheinfo %s %s %s'
247 # % (file1_mode, file1_hash, path))
251 # file does not exist in origin
254 if file1_hash and file2_hash:
256 if file1_hash == file2_hash:
257 if os.system('git update-index --add --cacheinfo %s %s %s'
258 % (file1_mode, file1_hash, path)) != 0:
259 out.error('git update-index failed')
262 if os.system('git checkout-index -u -f -- %s' % path):
263 out.error('git checkout-index failed')
266 if file1_mode != file2_mode:
267 out.error('File "s" added in both, permissions conflict'
271 # files added in both but different
273 out.error('File "%s" added in branches but different' % path)
274 # reset the cache to the first branch
275 os.system('git update-index --cacheinfo %s %s %s'
276 % (file1_mode, file1_hash, path))
278 if config.get('stgit.autoimerge') == 'yes':
280 interactive_merge(path)
281 except GitMergeException, ex:
282 # interactive merge failed
284 if str(keeporig) != 'yes':
285 __remove_files(orig_hash, file1_hash,
289 # successful interactive merge
290 os.system('git update-index -- %s' % path)
291 __remove_files(orig_hash, file1_hash, file2_hash)
294 # no interactive merge, just mark it as conflict
295 if str(keeporig) != 'yes':
296 __remove_files(orig_hash, file1_hash, file2_hash)
300 elif file1_hash or file2_hash:
307 if os.system('git update-index --add --cacheinfo %s %s %s'
308 % (mode, obj, path)) != 0:
309 out.error('git update-index failed')
312 __remove_files(orig_hash, file1_hash, file2_hash)
313 return os.system('git checkout-index -u -f -- %s' % path)
316 out.error('Unhandled merge conflict: "%s" "%s" "%s" "%s" "%s" "%s" "%s"'
317 % (orig_hash, file1_hash, file2_hash,
319 orig_mode, file1_mode, file2_mode))