chiark / gitweb /
Execute the 'git ...' rather than 'git-...'
[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
87c93eab 22from stgit.exception import *
170f576b 23from stgit import basedir
f7ed76a9 24from stgit.config import config, file_extensions, ConfigOption
5e888f30
KH
25from stgit.utils import append_string
26from stgit.out import *
f0de3f92 27from stgit.run import *
3659ef88 28
87c93eab 29class GitMergeException(StgException):
3659ef88
CM
30 pass
31
32
33#
34# Options
35#
eee7283e
CM
36merger = ConfigOption('stgit', 'merger')
37keeporig = ConfigOption('stgit', 'keeporig')
3659ef88
CM
38
39#
40# Utility functions
41#
42def __str2none(x):
43 if x == '':
44 return None
45 else:
46 return x
47
f0de3f92
KH
48class MRun(Run):
49 exc = GitMergeException # use a custom exception class on errors
3659ef88
CM
50
51def __checkout_files(orig_hash, file1_hash, file2_hash,
52 path,
53 orig_mode, file1_mode, file2_mode):
54 """Check out the files passed as arguments
55 """
56 global orig, src1, src2
57
1e075406
CM
58 extensions = file_extensions()
59
3659ef88 60 if orig_hash:
1e075406 61 orig = path + extensions['ancestor']
1576d681 62 tmp = MRun('git', 'unpack-file', orig_hash).output_one_line()
3659ef88
CM
63 os.chmod(tmp, int(orig_mode, 8))
64 os.renames(tmp, orig)
65 if file1_hash:
1e075406 66 src1 = path + extensions['current']
1576d681 67 tmp = MRun('git', 'unpack-file', file1_hash).output_one_line()
3659ef88
CM
68 os.chmod(tmp, int(file1_mode, 8))
69 os.renames(tmp, src1)
70 if file2_hash:
1e075406 71 src2 = path + extensions['patched']
1576d681 72 tmp = MRun('git', 'unpack-file', file2_hash).output_one_line()
3659ef88
CM
73 os.chmod(tmp, int(file2_mode, 8))
74 os.renames(tmp, src2)
75
8d415553
CM
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
1576d681 79 tmp = MRun('git', 'unpack-file', file1_hash).output_one_line()
8d415553
CM
80 os.chmod(tmp, int(file1_mode, 8))
81 os.renames(tmp, path)
82
3659ef88
CM
83def __remove_files(orig_hash, file1_hash, file2_hash):
84 """Remove any temporary files
85 """
86 if orig_hash:
87 os.remove(orig)
88 if file1_hash:
89 os.remove(src1)
90 if file2_hash:
91 os.remove(src2)
3659ef88 92
3659ef88
CM
93def __conflict(path):
94 """Write the conflict file for the 'path' variable and exit
95 """
170f576b 96 append_string(os.path.join(basedir.get(), 'conflicts'), path)
3659ef88
CM
97
98
f7ed76a9
CM
99def interactive_merge(filename):
100 """Run the interactive merger on the given file. Note that the
101 index should not have any conflicts.
102 """
f7ed76a9
CM
103 extensions = file_extensions()
104
105 ancestor = filename + extensions['ancestor']
106 current = filename + extensions['current']
107 patched = filename + extensions['patched']
108
b6e961f2
CM
109 if os.path.isfile(ancestor):
110 three_way = True
111 files_dict = {'branch1': current,
112 'ancestor': ancestor,
113 'branch2': patched,
114 'output': filename}
115 imerger = config.get('stgit.i3merge')
116 else:
117 three_way = False
118 files_dict = {'branch1': current,
119 'branch2': patched,
120 'output': filename}
121 imerger = config.get('stgit.i2merge')
122
123 if not imerger:
124 raise GitMergeException, 'No interactive merge command configured'
125
126 # check whether we have all the files for the merge
127 for fn in [filename, current, patched]:
f7ed76a9
CM
128 if not os.path.isfile(fn):
129 raise GitMergeException, \
b6e961f2 130 'Cannot run the interactive merge: "%s" missing' % fn
f7ed76a9
CM
131
132 mtime = os.path.getmtime(filename)
133
27ac2b7e
KH
134 out.info('Trying the interactive %s merge'
135 % (three_way and 'three-way' or 'two-way'))
f7ed76a9 136
b6e961f2 137 err = os.system(imerger % files_dict)
f7ed76a9
CM
138 if err != 0:
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
144
145
3659ef88
CM
146#
147# Main algorithm
148#
149def merge(orig_hash, file1_hash, file2_hash,
150 path,
151 orig_mode, file1_mode, file2_mode):
152 """Three-way merge for one file algorithm
153 """
154 __checkout_files(orig_hash, file1_hash, file2_hash,
155 path,
156 orig_mode, file1_mode, file2_mode)
157
158 # file exists in origin
159 if orig_hash:
160 # modified in both
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:
1576d681 164 if os.system('git update-index --cacheinfo %s %s %s'
3659ef88 165 % (file1_mode, file1_hash, path)) != 0:
1576d681 166 out.error('git update-index failed')
3659ef88
CM
167 __conflict(path)
168 return 1
1576d681
CM
169 if os.system('git checkout-index -u -f -- %s' % path):
170 out.error('git checkout-index failed')
3659ef88
CM
171 __conflict(path)
172 return 1
173 if file1_mode != file2_mode:
27ac2b7e 174 out.error('File added in both, permissions conflict')
3659ef88
CM
175 __conflict(path)
176 return 1
177 # 3-way merge
178 else:
5b99888b
CM
179 merge_ok = os.system(str(merger) % {'branch1': src1,
180 'ancestor': orig,
181 'branch2': src2,
182 'output': path }) == 0
3659ef88
CM
183
184 if merge_ok:
1576d681 185 os.system('git update-index -- %s' % path)
3659ef88
CM
186 __remove_files(orig_hash, file1_hash, file2_hash)
187 return 0
188 else:
27ac2b7e
KH
189 out.error('Three-way merge tool failed for file "%s"'
190 % path)
3659ef88 191 # reset the cache to the first branch
1576d681 192 os.system('git update-index --cacheinfo %s %s %s'
3659ef88 193 % (file1_mode, file1_hash, path))
f7ed76a9 194
c73e63b7 195 if config.get('stgit.autoimerge') == 'yes':
f7ed76a9
CM
196 try:
197 interactive_merge(path)
198 except GitMergeException, ex:
199 # interactive merge failed
f0b5d4dd 200 out.error(str(ex))
f7ed76a9
CM
201 if str(keeporig) != 'yes':
202 __remove_files(orig_hash, file1_hash,
203 file2_hash)
204 __conflict(path)
205 return 1
206 # successful interactive merge
1576d681 207 os.system('git update-index -- %s' % path)
3659ef88 208 __remove_files(orig_hash, file1_hash, file2_hash)
f7ed76a9
CM
209 return 0
210 else:
211 # no interactive merge, just mark it as conflict
212 if str(keeporig) != 'yes':
213 __remove_files(orig_hash, file1_hash, file2_hash)
214 __conflict(path)
215 return 1
216
3659ef88
CM
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):
221 os.remove(path)
222 __remove_files(orig_hash, file1_hash, file2_hash)
1576d681 223 return os.system('git update-index --remove -- %s' % path)
3659ef88
CM
224 # file deleted in one and changed in the other
225 else:
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.
229
230 # Do the same thing cogito does - remove the file in any case.
1576d681 231 os.system('git update-index --remove -- %s' % path)
3659ef88
CM
232
233 #if file1_hash:
234 ## file deleted upstream and changed in the patch. The
235 ## patch is probably going to move the changes
236 ## elsewhere.
237
1576d681 238 #os.system('git update-index --remove -- %s' % path)
3659ef88
CM
239 #else:
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
243 ## the file.
244
245 ## reset the cache to the first branch
1576d681 246 #os.system('git update-index --cacheinfo %s %s %s'
3659ef88
CM
247 # % (file1_mode, file1_hash, path))
248 __conflict(path)
249 return 1
250
251 # file does not exist in origin
252 else:
253 # file added in both
254 if file1_hash and file2_hash:
255 # files are the same
256 if file1_hash == file2_hash:
1576d681 257 if os.system('git update-index --add --cacheinfo %s %s %s'
3659ef88 258 % (file1_mode, file1_hash, path)) != 0:
1576d681 259 out.error('git update-index failed')
3659ef88
CM
260 __conflict(path)
261 return 1
1576d681
CM
262 if os.system('git checkout-index -u -f -- %s' % path):
263 out.error('git checkout-index failed')
3659ef88
CM
264 __conflict(path)
265 return 1
266 if file1_mode != file2_mode:
27ac2b7e
KH
267 out.error('File "s" added in both, permissions conflict'
268 % path)
3659ef88
CM
269 __conflict(path)
270 return 1
b6e961f2 271 # files added in both but different
3659ef88 272 else:
27ac2b7e 273 out.error('File "%s" added in branches but different' % path)
b6e961f2 274 # reset the cache to the first branch
1576d681 275 os.system('git update-index --cacheinfo %s %s %s'
b6e961f2
CM
276 % (file1_mode, file1_hash, path))
277
278 if config.get('stgit.autoimerge') == 'yes':
279 try:
280 interactive_merge(path)
281 except GitMergeException, ex:
282 # interactive merge failed
f0b5d4dd 283 out.error(str(ex))
b6e961f2
CM
284 if str(keeporig) != 'yes':
285 __remove_files(orig_hash, file1_hash,
286 file2_hash)
287 __conflict(path)
288 return 1
289 # successful interactive merge
1576d681 290 os.system('git update-index -- %s' % path)
b6e961f2
CM
291 __remove_files(orig_hash, file1_hash, file2_hash)
292 return 0
293 else:
294 # no interactive merge, just mark it as conflict
295 if str(keeporig) != 'yes':
296 __remove_files(orig_hash, file1_hash, file2_hash)
297 __conflict(path)
298 return 1
3659ef88
CM
299 # file added in one
300 elif file1_hash or file2_hash:
301 if file1_hash:
302 mode = file1_mode
303 obj = file1_hash
304 else:
305 mode = file2_mode
306 obj = file2_hash
1576d681 307 if os.system('git update-index --add --cacheinfo %s %s %s'
3659ef88 308 % (mode, obj, path)) != 0:
1576d681 309 out.error('git update-index failed')
3659ef88
CM
310 __conflict(path)
311 return 1
312 __remove_files(orig_hash, file1_hash, file2_hash)
1576d681 313 return os.system('git checkout-index -u -f -- %s' % path)
3659ef88
CM
314
315 # Unhandled case
27ac2b7e
KH
316 out.error('Unhandled merge conflict: "%s" "%s" "%s" "%s" "%s" "%s" "%s"'
317 % (orig_hash, file1_hash, file2_hash,
318 path,
319 orig_mode, file1_mode, file2_mode))
3659ef88
CM
320 __conflict(path)
321 return 1