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' % 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 def __remove_files(orig_hash
, file1_hash
, file2_hash
):
79 """Remove any temporary files
90 """Write the conflict file for the 'path' variable and exit
92 append_string(os
.path
.join(basedir
.get(), 'conflicts'), path
)
98 def merge(orig_hash
, file1_hash
, file2_hash
,
100 orig_mode
, file1_mode
, file2_mode
):
101 """Three-way merge for one file algorithm
103 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
105 orig_mode
, file1_mode
, file2_mode
)
107 # file exists in origin
110 if file1_hash
and file2_hash
:
111 # if modes are the same (git-read-tree probably dealt with it)
112 if file1_hash
== file2_hash
:
113 if os
.system('git-update-index --cacheinfo %s %s %s'
114 %
(file1_mode
, file1_hash
, path
)) != 0:
115 print >> sys
.stderr
, 'Error: git-update-index failed'
118 if os
.system('git-checkout-index -u -f -- %s' % path
):
119 print >> sys
.stderr
, 'Error: git-checkout-index failed'
122 if file1_mode
!= file2_mode
:
123 print >> sys
.stderr
, \
124 'Error: File added in both, permissions conflict'
129 merge_ok
= os
.system(str(merger
) %
{'branch1': src1
,
132 'output': path
}) == 0
135 os
.system('git-update-index -- %s' % path
)
136 __remove_files(orig_hash
, file1_hash
, file2_hash
)
139 print >> sys
.stderr
, \
140 'Error: three-way merge tool failed for file "%s"' \
142 # reset the cache to the first branch
143 os
.system('git-update-index --cacheinfo %s %s %s'
144 %
(file1_mode
, file1_hash
, path
))
145 if str(keeporig
) != 'yes':
146 __remove_files(orig_hash
, file1_hash
, file2_hash
)
149 # file deleted in both or deleted in one and unchanged in the other
150 elif not (file1_hash
or file2_hash
) \
151 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
152 if os
.path
.exists(path
):
154 __remove_files(orig_hash
, file1_hash
, file2_hash
)
155 return os
.system('git-update-index --remove -- %s' % path
)
156 # file deleted in one and changed in the other
158 # Do something here - we must at least merge the entry in
159 # the cache, instead of leaving it in U(nmerged) state. In
160 # fact, stg resolved does not handle that.
162 # Do the same thing cogito does - remove the file in any case.
163 os
.system('git-update-index --remove -- %s' % path
)
166 ## file deleted upstream and changed in the patch. The
167 ## patch is probably going to move the changes
170 #os.system('git-update-index --remove -- %s' % path)
172 ## file deleted in the patch and changed upstream. We
173 ## could re-delete it, but for now leave it there -
174 ## and let the user check if he still wants to remove
177 ## reset the cache to the first branch
178 #os.system('git-update-index --cacheinfo %s %s %s'
179 # % (file1_mode, file1_hash, path))
183 # file does not exist in origin
186 if file1_hash
and file2_hash
:
188 if file1_hash
== file2_hash
:
189 if os
.system('git-update-index --add --cacheinfo %s %s %s'
190 %
(file1_mode
, file1_hash
, path
)) != 0:
191 print >> sys
.stderr
, 'Error: git-update-index failed'
194 if os
.system('git-checkout-index -u -f -- %s' % path
):
195 print >> sys
.stderr
, 'Error: git-checkout-index failed'
198 if file1_mode
!= file2_mode
:
199 print >> sys
.stderr
, \
200 'Error: File "s" added in both, ' \
201 'permissions conflict' % path
204 # files are different
206 # reset the index to the current file
207 os
.system('git-update-index -- %s' % path
)
208 print >> sys
.stderr
, \
209 'Error: File "%s" added in branches but different' % path
213 elif file1_hash
or file2_hash
:
220 if os
.system('git-update-index --add --cacheinfo %s %s %s'
221 %
(mode
, obj
, path
)) != 0:
222 print >> sys
.stderr
, 'Error: git-update-index failed'
225 __remove_files(orig_hash
, file1_hash
, file2_hash
)
226 return os
.system('git-checkout-index -u -f -- %s' % path
)
229 print >> sys
.stderr
, 'Error: Unhandled merge conflict: ' \
230 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
231 %
(orig_hash
, file1_hash
, file2_hash
,
233 orig_mode
, file1_mode
, file2_mode
)