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
24 from stgit
.utils
import append_string
27 class GitMergeException(Exception):
35 merger
= config
.get('stgit', 'merger')
36 keeporig
= config
.get('stgit', 'keeporig')
37 except Exception, err
:
38 raise GitMergeException
, 'Configuration error: %s' % err
51 f
= os
.popen(cmd
, 'r')
52 string
= f
.readline().rstrip()
54 raise GitMergeException
, 'Error: failed to execute "%s"' % cmd
57 def __checkout_files(orig_hash
, file1_hash
, file2_hash
,
59 orig_mode
, file1_mode
, file2_mode
):
60 """Check out the files passed as arguments
62 global orig
, src1
, src2
64 extensions
= file_extensions()
67 orig
= path
+ extensions
['ancestor']
68 tmp
= __output('git-unpack-file %s' % orig_hash
)
69 os
.chmod(tmp
, int(orig_mode
, 8))
72 src1
= path
+ extensions
['current']
73 tmp
= __output('git-unpack-file %s' % file
1_hash
)
74 os
.chmod(tmp
, int(file1_mode
, 8))
77 src2
= path
+ extensions
['patched']
78 tmp
= __output('git-unpack-file %s' % file
2_hash
)
79 os
.chmod(tmp
, int(file2_mode
, 8))
82 def __remove_files(orig_hash
, file1_hash
, file2_hash
):
83 """Remove any temporary files
94 """Write the conflict file for the 'path' variable and exit
96 append_string(os
.path
.join(basedir
.get(), 'conflicts'), path
)
102 def merge(orig_hash
, file1_hash
, file2_hash
,
104 orig_mode
, file1_mode
, file2_mode
):
105 """Three-way merge for one file algorithm
107 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
109 orig_mode
, file1_mode
, file2_mode
)
111 # file exists in origin
114 if file1_hash
and file2_hash
:
115 # if modes are the same (git-read-tree probably dealt with it)
116 if file1_hash
== file2_hash
:
117 if os
.system('git-update-index --cacheinfo %s %s %s'
118 %
(file1_mode
, file1_hash
, path
)) != 0:
119 print >> sys
.stderr
, 'Error: git-update-index failed'
122 if os
.system('git-checkout-index -u -f -- %s' % path
):
123 print >> sys
.stderr
, 'Error: git-checkout-index failed'
126 if file1_mode
!= file2_mode
:
127 print >> sys
.stderr
, \
128 'Error: File added in both, permissions conflict'
133 merge_ok
= os
.system(merger %
{'branch1': src1
,
136 'output': path
}) == 0
139 os
.system('git-update-index -- %s' % path
)
140 __remove_files(orig_hash
, file1_hash
, file2_hash
)
143 print >> sys
.stderr
, \
144 'Error: three-way merge tool failed for file "%s"' \
146 # reset the cache to the first branch
147 os
.system('git-update-index --cacheinfo %s %s %s'
148 %
(file1_mode
, file1_hash
, path
))
149 if keeporig
!= 'yes':
150 __remove_files(orig_hash
, file1_hash
, file2_hash
)
153 # file deleted in both or deleted in one and unchanged in the other
154 elif not (file1_hash
or file2_hash
) \
155 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
156 if os
.path
.exists(path
):
158 __remove_files(orig_hash
, file1_hash
, file2_hash
)
159 return os
.system('git-update-index --remove -- %s' % path
)
160 # file deleted in one and changed in the other
162 # Do something here - we must at least merge the entry in
163 # the cache, instead of leaving it in U(nmerged) state. In
164 # fact, stg resolved does not handle that.
166 # Do the same thing cogito does - remove the file in any case.
167 os
.system('git-update-index --remove -- %s' % path
)
170 ## file deleted upstream and changed in the patch. The
171 ## patch is probably going to move the changes
174 #os.system('git-update-index --remove -- %s' % path)
176 ## file deleted in the patch and changed upstream. We
177 ## could re-delete it, but for now leave it there -
178 ## and let the user check if he still wants to remove
181 ## reset the cache to the first branch
182 #os.system('git-update-index --cacheinfo %s %s %s'
183 # % (file1_mode, file1_hash, path))
187 # file does not exist in origin
190 if file1_hash
and file2_hash
:
192 if file1_hash
== file2_hash
:
193 if os
.system('git-update-index --add --cacheinfo %s %s %s'
194 %
(file1_mode
, file1_hash
, path
)) != 0:
195 print >> sys
.stderr
, 'Error: git-update-index failed'
198 if os
.system('git-checkout-index -u -f -- %s' % path
):
199 print >> sys
.stderr
, 'Error: git-checkout-index failed'
202 if file1_mode
!= file2_mode
:
203 print >> sys
.stderr
, \
204 'Error: File "s" added in both, ' \
205 'permissions conflict' % path
208 # files are different
210 # reset the index to the current file
211 os
.system('git-update-index -- %s' % path
)
212 print >> sys
.stderr
, \
213 'Error: File "%s" added in branches but different' % path
217 elif file1_hash
or file2_hash
:
224 if os
.system('git-update-index --add --cacheinfo %s %s %s'
225 %
(mode
, obj
, path
)) != 0:
226 print >> sys
.stderr
, 'Error: git-update-index failed'
229 __remove_files(orig_hash
, file1_hash
, file2_hash
)
230 return os
.system('git-checkout-index -u -f -- %s' % path
)
233 print >> sys
.stderr
, 'Error: Unhandled merge conflict: ' \
234 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
235 %
(orig_hash
, file1_hash
, file2_hash
,
237 orig_mode
, file1_mode
, file2_mode
)