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
25 from stgit
.out
import *
26 from stgit
.run
import *
28 class GitMergeException(Exception):
35 merger
= ConfigOption('stgit', 'merger')
36 keeporig
= ConfigOption('stgit', 'keeporig')
48 exc
= GitMergeException
# use a custom exception class on errors
50 def __checkout_files(orig_hash
, file1_hash
, file2_hash
,
52 orig_mode
, file1_mode
, file2_mode
):
53 """Check out the files passed as arguments
55 global orig
, src1
, src2
57 extensions
= file_extensions()
60 orig
= path
+ extensions
['ancestor']
61 tmp
= MRun('git-unpack-file', orig_hash
).output_one_line()
62 os
.chmod(tmp
, int(orig_mode
, 8))
65 src1
= path
+ extensions
['current']
66 tmp
= MRun('git-unpack-file', file1_hash
).output_one_line()
67 os
.chmod(tmp
, int(file1_mode
, 8))
70 src2
= path
+ extensions
['patched']
71 tmp
= MRun('git-unpack-file', file2_hash
).output_one_line()
72 os
.chmod(tmp
, int(file2_mode
, 8))
75 if file1_hash
and not os
.path
.exists(path
):
76 # the current file might be removed by GIT when it is a new
77 # file added in both branches. Just re-generate it
78 tmp
= MRun('git-unpack-file', file1_hash
).output_one_line()
79 os
.chmod(tmp
, int(file1_mode
, 8))
82 def __remove_files(orig_hash
, file1_hash
, file2_hash
):
83 """Remove any temporary files
93 """Write the conflict file for the 'path' variable and exit
95 append_string(os
.path
.join(basedir
.get(), 'conflicts'), path
)
98 def interactive_merge(filename
):
99 """Run the interactive merger on the given file. Note that the
100 index should not have any conflicts.
102 extensions
= file_extensions()
104 ancestor
= filename
+ extensions
['ancestor']
105 current
= filename
+ extensions
['current']
106 patched
= filename
+ extensions
['patched']
108 if os
.path
.isfile(ancestor
):
110 files_dict
= {'branch1': current
,
111 'ancestor': ancestor
,
114 imerger
= config
.get('stgit.i3merge')
117 files_dict
= {'branch1': current
,
120 imerger
= config
.get('stgit.i2merge')
123 raise GitMergeException
, 'No interactive merge command configured'
125 # check whether we have all the files for the merge
126 for fn
in [filename
, current
, patched
]:
127 if not os
.path
.isfile(fn
):
128 raise GitMergeException
, \
129 'Cannot run the interactive merge: "%s" missing' % fn
131 mtime
= os
.path
.getmtime(filename
)
133 out
.info('Trying the interactive %s merge'
134 %
(three_way
and 'three-way' or 'two-way'))
136 err
= os
.system(imerger % files_dict
)
138 raise GitMergeException
, 'The interactive merge failed: %d' % err
139 if not os
.path
.isfile(filename
):
140 raise GitMergeException
, 'The "%s" file is missing' % filename
141 if mtime
== os
.path
.getmtime(filename
):
142 raise GitMergeException
, 'The "%s" file was not modified' % filename
148 def merge(orig_hash
, file1_hash
, file2_hash
,
150 orig_mode
, file1_mode
, file2_mode
):
151 """Three-way merge for one file algorithm
153 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
155 orig_mode
, file1_mode
, file2_mode
)
157 # file exists in origin
160 if file1_hash
and file2_hash
:
161 # if modes are the same (git-read-tree probably dealt with it)
162 if file1_hash
== file2_hash
:
163 if os
.system('git-update-index --cacheinfo %s %s %s'
164 %
(file1_mode
, file1_hash
, path
)) != 0:
165 out
.error('git-update-index failed')
168 if os
.system('git-checkout-index -u -f -- %s' % path
):
169 out
.error('git-checkout-index failed')
172 if file1_mode
!= file2_mode
:
173 out
.error('File added in both, permissions conflict')
178 merge_ok
= os
.system(str(merger
) %
{'branch1': src1
,
181 'output': path
}) == 0
184 os
.system('git-update-index -- %s' % path
)
185 __remove_files(orig_hash
, file1_hash
, file2_hash
)
188 out
.error('Three-way merge tool failed for file "%s"'
190 # reset the cache to the first branch
191 os
.system('git-update-index --cacheinfo %s %s %s'
192 %
(file1_mode
, file1_hash
, path
))
194 if config
.get('stgit.autoimerge') == 'yes':
196 interactive_merge(path
)
197 except GitMergeException
, ex
:
198 # interactive merge failed
200 if str(keeporig
) != 'yes':
201 __remove_files(orig_hash
, file1_hash
,
205 # successful interactive merge
206 os
.system('git-update-index -- %s' % path
)
207 __remove_files(orig_hash
, file1_hash
, file2_hash
)
210 # no interactive merge, just mark it as conflict
211 if str(keeporig
) != 'yes':
212 __remove_files(orig_hash
, file1_hash
, file2_hash
)
216 # file deleted in both or deleted in one and unchanged in the other
217 elif not (file1_hash
or file2_hash
) \
218 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
219 if os
.path
.exists(path
):
221 __remove_files(orig_hash
, file1_hash
, file2_hash
)
222 return os
.system('git-update-index --remove -- %s' % path
)
223 # file deleted in one and changed in the other
225 # Do something here - we must at least merge the entry in
226 # the cache, instead of leaving it in U(nmerged) state. In
227 # fact, stg resolved does not handle that.
229 # Do the same thing cogito does - remove the file in any case.
230 os
.system('git-update-index --remove -- %s' % path
)
233 ## file deleted upstream and changed in the patch. The
234 ## patch is probably going to move the changes
237 #os.system('git-update-index --remove -- %s' % path)
239 ## file deleted in the patch and changed upstream. We
240 ## could re-delete it, but for now leave it there -
241 ## and let the user check if he still wants to remove
244 ## reset the cache to the first branch
245 #os.system('git-update-index --cacheinfo %s %s %s'
246 # % (file1_mode, file1_hash, path))
250 # file does not exist in origin
253 if file1_hash
and file2_hash
:
255 if file1_hash
== file2_hash
:
256 if os
.system('git-update-index --add --cacheinfo %s %s %s'
257 %
(file1_mode
, file1_hash
, path
)) != 0:
258 out
.error('git-update-index failed')
261 if os
.system('git-checkout-index -u -f -- %s' % path
):
262 out
.error('git-checkout-index failed')
265 if file1_mode
!= file2_mode
:
266 out
.error('File "s" added in both, permissions conflict'
270 # files added in both but different
272 out
.error('File "%s" added in branches but different' % path
)
273 # reset the cache to the first branch
274 os
.system('git-update-index --cacheinfo %s %s %s'
275 %
(file1_mode
, file1_hash
, path
))
277 if config
.get('stgit.autoimerge') == 'yes':
279 interactive_merge(path
)
280 except GitMergeException
, ex
:
281 # interactive merge failed
283 if str(keeporig
) != 'yes':
284 __remove_files(orig_hash
, file1_hash
,
288 # successful interactive merge
289 os
.system('git-update-index -- %s' % path
)
290 __remove_files(orig_hash
, file1_hash
, file2_hash
)
293 # no interactive merge, just mark it as conflict
294 if str(keeporig
) != 'yes':
295 __remove_files(orig_hash
, file1_hash
, file2_hash
)
299 elif file1_hash
or file2_hash
:
306 if os
.system('git-update-index --add --cacheinfo %s %s %s'
307 %
(mode
, obj
, path
)) != 0:
308 out
.error('git-update-index failed')
311 __remove_files(orig_hash
, file1_hash
, file2_hash
)
312 return os
.system('git-checkout-index -u -f -- %s' % path
)
315 out
.error('Unhandled merge conflict: "%s" "%s" "%s" "%s" "%s" "%s" "%s"'
316 %
(orig_hash
, file1_hash
, file2_hash
,
318 orig_mode
, file1_mode
, file2_mode
))