058b6ac4dec8d09939c5887df88381859396baa4
[stgit] / stgit / gitmergeonefile.py
1 """Performs a 3-way merge for GIT files
2 """
3
4 __copyright__ = """
5 Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
6
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.
10
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.
15
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
19 """
20
21 import sys, os
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 *
28
29 class GitMergeException(StgException):
30 pass
31
32
33 #
34 # Options
35 #
36 merger = ConfigOption('stgit', 'merger')
37 keeporig = ConfigOption('stgit', 'keeporig')
38
39 #
40 # Utility functions
41 #
42 def __str2none(x):
43 if x == '':
44 return None
45 else:
46 return x
47
48 class MRun(Run):
49 exc = GitMergeException # use a custom exception class on errors
50
51 def __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
58 extensions = file_extensions()
59
60 if orig_hash:
61 orig = path + extensions['ancestor']
62 tmp = MRun('git-unpack-file', orig_hash).output_one_line()
63 os.chmod(tmp, int(orig_mode, 8))
64 os.renames(tmp, orig)
65 if file1_hash:
66 src1 = path + extensions['current']
67 tmp = MRun('git-unpack-file', file1_hash).output_one_line()
68 os.chmod(tmp, int(file1_mode, 8))
69 os.renames(tmp, src1)
70 if file2_hash:
71 src2 = path + extensions['patched']
72 tmp = MRun('git-unpack-file', file2_hash).output_one_line()
73 os.chmod(tmp, int(file2_mode, 8))
74 os.renames(tmp, src2)
75
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))
81 os.renames(tmp, path)
82
83 def __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)
92
93 def __conflict(path):
94 """Write the conflict file for the 'path' variable and exit
95 """
96 append_string(os.path.join(basedir.get(), 'conflicts'), path)
97
98
99 def interactive_merge(filename):
100 """Run the interactive merger on the given file. Note that the
101 index should not have any conflicts.
102 """
103 extensions = file_extensions()
104
105 ancestor = filename + extensions['ancestor']
106 current = filename + extensions['current']
107 patched = filename + extensions['patched']
108
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]:
128 if not os.path.isfile(fn):
129 raise GitMergeException, \
130 'Cannot run the interactive merge: "%s" missing' % fn
131
132 mtime = os.path.getmtime(filename)
133
134 out.info('Trying the interactive %s merge'
135 % (three_way and 'three-way' or 'two-way'))
136
137 err = os.system(imerger % files_dict)
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
146 #
147 # Main algorithm
148 #
149 def 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:
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')
167 __conflict(path)
168 return 1
169 if os.system('git-checkout-index -u -f -- %s' % path):
170 out.error('git-checkout-index failed')
171 __conflict(path)
172 return 1
173 if file1_mode != file2_mode:
174 out.error('File added in both, permissions conflict')
175 __conflict(path)
176 return 1
177 # 3-way merge
178 else:
179 merge_ok = os.system(str(merger) % {'branch1': src1,
180 'ancestor': orig,
181 'branch2': src2,
182 'output': path }) == 0
183
184 if merge_ok:
185 os.system('git-update-index -- %s' % path)
186 __remove_files(orig_hash, file1_hash, file2_hash)
187 return 0
188 else:
189 out.error('Three-way merge tool failed for file "%s"'
190 % path)
191 # reset the cache to the first branch
192 os.system('git-update-index --cacheinfo %s %s %s'
193 % (file1_mode, file1_hash, path))
194
195 if config.get('stgit.autoimerge') == 'yes':
196 try:
197 interactive_merge(path)
198 except GitMergeException, ex:
199 # interactive merge failed
200 out.error(str(ex))
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
207 os.system('git-update-index -- %s' % path)
208 __remove_files(orig_hash, file1_hash, file2_hash)
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
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)
223 return os.system('git-update-index --remove -- %s' % path)
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.
231 os.system('git-update-index --remove -- %s' % path)
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
238 #os.system('git-update-index --remove -- %s' % path)
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
246 #os.system('git-update-index --cacheinfo %s %s %s'
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:
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')
260 __conflict(path)
261 return 1
262 if os.system('git-checkout-index -u -f -- %s' % path):
263 out.error('git-checkout-index failed')
264 __conflict(path)
265 return 1
266 if file1_mode != file2_mode:
267 out.error('File "s" added in both, permissions conflict'
268 % path)
269 __conflict(path)
270 return 1
271 # files added in both but different
272 else:
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))
277
278 if config.get('stgit.autoimerge') == 'yes':
279 try:
280 interactive_merge(path)
281 except GitMergeException, ex:
282 # interactive merge failed
283 out.error(str(ex))
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
290 os.system('git-update-index -- %s' % path)
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
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
307 if os.system('git-update-index --add --cacheinfo %s %s %s'
308 % (mode, obj, path)) != 0:
309 out.error('git-update-index failed')
310 __conflict(path)
311 return 1
312 __remove_files(orig_hash, file1_hash, file2_hash)
313 return os.system('git-checkout-index -u -f -- %s' % path)
314
315 # Unhandled case
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))
320 __conflict(path)
321 return 1