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 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)
104 def merge(orig_hash, file1_hash, file2_hash,
106 orig_mode, file1_mode, file2_mode):
107 """Three-way merge for one file algorithm
109 __checkout_files(orig_hash, file1_hash, file2_hash,
111 orig_mode, file1_mode, file2_mode)
113 # file exists in origin
116 if file1_hash and file2_hash:
117 # if modes are the same (git-read-tree probably dealt with it)
118 if file1_hash == file2_hash:
119 if os.system('git-update-index --cacheinfo %s %s %s'
120 % (file1_mode, file1_hash, path)) != 0:
121 print >> sys.stderr, 'Error: git-update-index failed'
124 if os.system('git-checkout-index -u -f -- %s' % path):
125 print >> sys.stderr, 'Error: git-checkout-index failed'
128 if file1_mode != file2_mode:
129 print >> sys.stderr, \
130 'Error: File added in both, permissions conflict'
135 merge_ok = os.system(str(merger) % {'branch1': src1,
138 'output': path }) == 0
141 os.system('git-update-index -- %s' % path)
142 __remove_files(orig_hash, file1_hash, file2_hash)
145 print >> sys.stderr, \
146 'Error: three-way merge tool failed for file "%s"' \
148 # reset the cache to the first branch
149 os.system('git-update-index --cacheinfo %s %s %s'
150 % (file1_mode, file1_hash, path))
151 if str(keeporig) != 'yes':
152 __remove_files(orig_hash, file1_hash, file2_hash)
155 # file deleted in both or deleted in one and unchanged in the other
156 elif not (file1_hash or file2_hash) \
157 or file1_hash == orig_hash or file2_hash == orig_hash:
158 if os.path.exists(path):
160 __remove_files(orig_hash, file1_hash, file2_hash)
161 return os.system('git-update-index --remove -- %s' % path)
162 # file deleted in one and changed in the other
164 # Do something here - we must at least merge the entry in
165 # the cache, instead of leaving it in U(nmerged) state. In
166 # fact, stg resolved does not handle that.
168 # Do the same thing cogito does - remove the file in any case.
169 os.system('git-update-index --remove -- %s' % path)
172 ## file deleted upstream and changed in the patch. The
173 ## patch is probably going to move the changes
176 #os.system('git-update-index --remove -- %s' % path)
178 ## file deleted in the patch and changed upstream. We
179 ## could re-delete it, but for now leave it there -
180 ## and let the user check if he still wants to remove
183 ## reset the cache to the first branch
184 #os.system('git-update-index --cacheinfo %s %s %s'
185 # % (file1_mode, file1_hash, path))
189 # file does not exist in origin
192 if file1_hash and file2_hash:
194 if file1_hash == file2_hash:
195 if os.system('git-update-index --add --cacheinfo %s %s %s'
196 % (file1_mode, file1_hash, path)) != 0:
197 print >> sys.stderr, 'Error: git-update-index failed'
200 if os.system('git-checkout-index -u -f -- %s' % path):
201 print >> sys.stderr, 'Error: git-checkout-index failed'
204 if file1_mode != file2_mode:
205 print >> sys.stderr, \
206 'Error: File "s" added in both, ' \
207 'permissions conflict' % path
210 # files are different
212 # reset the index to the current file
213 os.system('git-update-index -- %s' % path)
214 print >> sys.stderr, \
215 'Error: File "%s" added in branches but different' % path
219 elif file1_hash or file2_hash:
226 if os.system('git-update-index --add --cacheinfo %s %s %s'
227 % (mode, obj, path)) != 0:
228 print >> sys.stderr, 'Error: git-update-index failed'
231 __remove_files(orig_hash, file1_hash, file2_hash)
232 return os.system('git-checkout-index -u -f -- %s' % path)
235 print >> sys.stderr, 'Error: Unhandled merge conflict: ' \
236 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
237 % (orig_hash, file1_hash, file2_hash,
239 orig_mode, file1_mode, file2_mode)