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
27 class GitMergeException(Exception):
34 merger = ConfigOption('stgit', 'merger')
35 keeporig = ConfigOption('stgit', 'keeporig')
47 f = os.popen(cmd, 'r')
48 string = f.readline().rstrip()
50 raise GitMergeException, 'Error: failed to execute "%s"' % cmd
53 def __checkout_files(orig_hash, file1_hash, file2_hash,
55 orig_mode, file1_mode, file2_mode):
56 """Check out the files passed as arguments
58 global orig, src1, src2
60 extensions = file_extensions()
63 orig = path + extensions['ancestor']
64 tmp = __output('git-unpack-file %s' % orig_hash)
65 os.chmod(tmp, int(orig_mode, 8))
68 src1 = path + extensions['current']
69 tmp = __output('git-unpack-file %s' % file1_hash)
70 os.chmod(tmp, int(file1_mode, 8))
73 src2 = path + extensions['patched']
74 tmp = __output('git-unpack-file %s' % file2_hash)
75 os.chmod(tmp, int(file2_mode, 8))
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))
85 def __remove_files(orig_hash, file1_hash, file2_hash):
86 """Remove any temporary files
96 """Write the conflict file for the 'path' variable and exit
98 append_string(os.path.join(basedir.get(), 'conflicts'), path)
101 def interactive_merge(filename):
102 """Run the interactive merger on the given file. Note that the
103 index should not have any conflicts.
106 imerger = config.get('stgit', 'imerger')
107 except Exception, err:
108 raise GitMergeException, 'Configuration error: %s' % err
110 extensions = file_extensions()
112 ancestor = filename + extensions['ancestor']
113 current = filename + extensions['current']
114 patched = filename + extensions['patched']
116 # check whether we have all the files for a three-way merge
117 for fn in [filename, ancestor, current, patched]:
118 if not os.path.isfile(fn):
119 raise GitMergeException, \
120 'Cannot run the interactive merger: "%s" missing' % fn
122 mtime = os.path.getmtime(filename)
124 err = os.system(imerger % {'branch1': current,
125 'ancestor': ancestor,
130 raise GitMergeException, 'The interactive merge failed: %d' % err
131 if not os.path.isfile(filename):
132 raise GitMergeException, 'The "%s" file is missing' % filename
133 if mtime == os.path.getmtime(filename):
134 raise GitMergeException, 'The "%s" file was not modified' % filename
140 def merge(orig_hash, file1_hash, file2_hash,
142 orig_mode, file1_mode, file2_mode):
143 """Three-way merge for one file algorithm
145 __checkout_files(orig_hash, file1_hash, file2_hash,
147 orig_mode, file1_mode, file2_mode)
149 # file exists in origin
152 if file1_hash and file2_hash:
153 # if modes are the same (git-read-tree probably dealt with it)
154 if file1_hash == file2_hash:
155 if os.system('git-update-index --cacheinfo %s %s %s'
156 % (file1_mode, file1_hash, path)) != 0:
157 print >> sys.stderr, 'Error: git-update-index failed'
160 if os.system('git-checkout-index -u -f -- %s' % path):
161 print >> sys.stderr, 'Error: git-checkout-index failed'
164 if file1_mode != file2_mode:
165 print >> sys.stderr, \
166 'Error: File added in both, permissions conflict'
171 merge_ok = os.system(str(merger) % {'branch1': src1,
174 'output': path }) == 0
177 os.system('git-update-index -- %s' % path)
178 __remove_files(orig_hash, file1_hash, file2_hash)
181 print >> sys.stderr, \
182 'Error: three-way merge tool failed for file "%s"' \
184 # reset the cache to the first branch
185 os.system('git-update-index --cacheinfo %s %s %s'
186 % (file1_mode, file1_hash, path))
188 if config.get('stgit', 'autoimerge') == 'yes':
189 print >> sys.stderr, \
190 'Trying the interactive merge'
192 interactive_merge(path)
193 except GitMergeException, ex:
194 # interactive merge failed
195 print >> sys.stderr, str(ex)
196 if str(keeporig) != 'yes':
197 __remove_files(orig_hash, file1_hash,
201 # successful interactive merge
202 __remove_files(orig_hash, file1_hash, file2_hash)
205 # no interactive merge, just mark it as conflict
206 if str(keeporig) != 'yes':
207 __remove_files(orig_hash, file1_hash, file2_hash)
211 # file deleted in both or deleted in one and unchanged in the other
212 elif not (file1_hash or file2_hash) \
213 or file1_hash == orig_hash or file2_hash == orig_hash:
214 if os.path.exists(path):
216 __remove_files(orig_hash, file1_hash, file2_hash)
217 return os.system('git-update-index --remove -- %s' % path)
218 # file deleted in one and changed in the other
220 # Do something here - we must at least merge the entry in
221 # the cache, instead of leaving it in U(nmerged) state. In
222 # fact, stg resolved does not handle that.
224 # Do the same thing cogito does - remove the file in any case.
225 os.system('git-update-index --remove -- %s' % path)
228 ## file deleted upstream and changed in the patch. The
229 ## patch is probably going to move the changes
232 #os.system('git-update-index --remove -- %s' % path)
234 ## file deleted in the patch and changed upstream. We
235 ## could re-delete it, but for now leave it there -
236 ## and let the user check if he still wants to remove
239 ## reset the cache to the first branch
240 #os.system('git-update-index --cacheinfo %s %s %s'
241 # % (file1_mode, file1_hash, path))
245 # file does not exist in origin
248 if file1_hash and file2_hash:
250 if file1_hash == file2_hash:
251 if os.system('git-update-index --add --cacheinfo %s %s %s'
252 % (file1_mode, file1_hash, path)) != 0:
253 print >> sys.stderr, 'Error: git-update-index failed'
256 if os.system('git-checkout-index -u -f -- %s' % path):
257 print >> sys.stderr, 'Error: git-checkout-index failed'
260 if file1_mode != file2_mode:
261 print >> sys.stderr, \
262 'Error: File "s" added in both, ' \
263 'permissions conflict' % path
266 # files are different
268 # reset the index to the current file
269 os.system('git-update-index -- %s' % path)
270 print >> sys.stderr, \
271 'Error: File "%s" added in branches but different' % path
275 elif file1_hash or file2_hash:
282 if os.system('git-update-index --add --cacheinfo %s %s %s'
283 % (mode, obj, path)) != 0:
284 print >> sys.stderr, 'Error: git-update-index failed'
287 __remove_files(orig_hash, file1_hash, file2_hash)
288 return os.system('git-checkout-index -u -f -- %s' % path)
291 print >> sys.stderr, 'Error: Unhandled merge conflict: ' \
292 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
293 % (orig_hash, file1_hash, file2_hash,
295 orig_mode, file1_mode, file2_mode)