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' % file
1_hash
)
70 os
.chmod(tmp
, int(file1_mode
, 8))
73 src2
= path
+ extensions
['patched']
74 tmp
= __output('git-unpack-file %s' % file
2_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' % file
1_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.
105 extensions
= file_extensions()
107 ancestor
= filename
+ extensions
['ancestor']
108 current
= filename
+ extensions
['current']
109 patched
= filename
+ extensions
['patched']
111 if os
.path
.isfile(ancestor
):
113 files_dict
= {'branch1': current
,
114 'ancestor': ancestor
,
117 imerger
= config
.get('stgit.i3merge')
120 files_dict
= {'branch1': current
,
123 imerger
= config
.get('stgit.i2merge')
126 raise GitMergeException
, 'No interactive merge command configured'
128 # check whether we have all the files for the merge
129 for fn
in [filename
, current
, patched
]:
130 if not os
.path
.isfile(fn
):
131 raise GitMergeException
, \
132 'Cannot run the interactive merge: "%s" missing' % fn
134 mtime
= os
.path
.getmtime(filename
)
136 print 'Trying the interractive %s merge' % \
137 {True: 'three-way', False: 'two-way'}[three_way
]
139 err
= os
.system(imerger % files_dict
)
141 raise GitMergeException
, 'The interactive merge failed: %d' % err
142 if not os
.path
.isfile(filename
):
143 raise GitMergeException
, 'The "%s" file is missing' % filename
144 if mtime
== os
.path
.getmtime(filename
):
145 raise GitMergeException
, 'The "%s" file was not modified' % filename
151 def merge(orig_hash
, file1_hash
, file2_hash
,
153 orig_mode
, file1_mode
, file2_mode
):
154 """Three-way merge for one file algorithm
156 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
158 orig_mode
, file1_mode
, file2_mode
)
160 # file exists in origin
163 if file1_hash
and file2_hash
:
164 # if modes are the same (git-read-tree probably dealt with it)
165 if file1_hash
== file2_hash
:
166 if os
.system('git-update-index --cacheinfo %s %s %s'
167 %
(file1_mode
, file1_hash
, path
)) != 0:
168 print >> sys
.stderr
, 'Error: git-update-index failed'
171 if os
.system('git-checkout-index -u -f -- %s' % path
):
172 print >> sys
.stderr
, 'Error: git-checkout-index failed'
175 if file1_mode
!= file2_mode
:
176 print >> sys
.stderr
, \
177 'Error: File added in both, permissions conflict'
182 merge_ok
= os
.system(str(merger
) %
{'branch1': src1
,
185 'output': path
}) == 0
188 os
.system('git-update-index -- %s' % path
)
189 __remove_files(orig_hash
, file1_hash
, file2_hash
)
192 print >> sys
.stderr
, \
193 'Error: three-way merge tool failed for file "%s"' \
195 # reset the cache to the first branch
196 os
.system('git-update-index --cacheinfo %s %s %s'
197 %
(file1_mode
, file1_hash
, path
))
199 if config
.get('stgit.autoimerge') == 'yes':
201 interactive_merge(path
)
202 except GitMergeException
, ex
:
203 # interactive merge failed
204 print >> sys
.stderr
, str(ex
)
205 if str(keeporig
) != 'yes':
206 __remove_files(orig_hash
, file1_hash
,
210 # successful interactive merge
211 os
.system('git-update-index -- %s' % path
)
212 __remove_files(orig_hash
, file1_hash
, file2_hash
)
215 # no interactive merge, just mark it as conflict
216 if str(keeporig
) != 'yes':
217 __remove_files(orig_hash
, file1_hash
, file2_hash
)
221 # file deleted in both or deleted in one and unchanged in the other
222 elif not (file1_hash
or file2_hash
) \
223 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
224 if os
.path
.exists(path
):
226 __remove_files(orig_hash
, file1_hash
, file2_hash
)
227 return os
.system('git-update-index --remove -- %s' % path
)
228 # file deleted in one and changed in the other
230 # Do something here - we must at least merge the entry in
231 # the cache, instead of leaving it in U(nmerged) state. In
232 # fact, stg resolved does not handle that.
234 # Do the same thing cogito does - remove the file in any case.
235 os
.system('git-update-index --remove -- %s' % path
)
238 ## file deleted upstream and changed in the patch. The
239 ## patch is probably going to move the changes
242 #os.system('git-update-index --remove -- %s' % path)
244 ## file deleted in the patch and changed upstream. We
245 ## could re-delete it, but for now leave it there -
246 ## and let the user check if he still wants to remove
249 ## reset the cache to the first branch
250 #os.system('git-update-index --cacheinfo %s %s %s'
251 # % (file1_mode, file1_hash, path))
255 # file does not exist in origin
258 if file1_hash
and file2_hash
:
260 if file1_hash
== file2_hash
:
261 if os
.system('git-update-index --add --cacheinfo %s %s %s'
262 %
(file1_mode
, file1_hash
, path
)) != 0:
263 print >> sys
.stderr
, 'Error: git-update-index failed'
266 if os
.system('git-checkout-index -u -f -- %s' % path
):
267 print >> sys
.stderr
, 'Error: git-checkout-index failed'
270 if file1_mode
!= file2_mode
:
271 print >> sys
.stderr
, \
272 'Error: File "s" added in both, ' \
273 'permissions conflict' % path
276 # files added in both but different
278 print >> sys
.stderr
, \
279 'Error: File "%s" added in branches but different' % path
280 # reset the cache to the first branch
281 os
.system('git-update-index --cacheinfo %s %s %s'
282 %
(file1_mode
, file1_hash
, path
))
284 if config
.get('stgit.autoimerge') == 'yes':
286 interactive_merge(path
)
287 except GitMergeException
, ex
:
288 # interactive merge failed
289 print >> sys
.stderr
, str(ex
)
290 if str(keeporig
) != 'yes':
291 __remove_files(orig_hash
, file1_hash
,
295 # successful interactive merge
296 os
.system('git-update-index -- %s' % path
)
297 __remove_files(orig_hash
, file1_hash
, file2_hash
)
300 # no interactive merge, just mark it as conflict
301 if str(keeporig
) != 'yes':
302 __remove_files(orig_hash
, file1_hash
, file2_hash
)
306 elif file1_hash
or file2_hash
:
313 if os
.system('git-update-index --add --cacheinfo %s %s %s'
314 %
(mode
, obj
, path
)) != 0:
315 print >> sys
.stderr
, 'Error: git-update-index failed'
318 __remove_files(orig_hash
, file1_hash
, file2_hash
)
319 return os
.system('git-checkout-index -u -f -- %s' % path
)
322 print >> sys
.stderr
, 'Error: Unhandled merge conflict: ' \
323 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
324 %
(orig_hash
, file1_hash
, file2_hash
,
326 orig_mode
, file1_mode
, file2_mode
)