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
.config
import config
23 from stgit
.utils
import append_string
26 class GitMergeException(Exception):
34 merger
= config
.get('gitmergeonefile', 'merger')
35 except Exception, err
:
36 raise GitMergeException
, 'Configuration error: %s' % err
38 if config
.has_option('gitmergeonefile', 'keeporig'):
39 keeporig
= config
.get('gitmergeonefile', 'keeporig')
54 f
= os
.popen(cmd
, 'r')
55 string
= f
.readline().rstrip()
57 raise GitMergeException
, 'Error: failed to execute "%s"' % cmd
60 def __checkout_files(orig_hash
, file1_hash
, file2_hash
,
62 orig_mode
, file1_mode
, file2_mode
):
63 """Check out the files passed as arguments
65 global orig
, src1
, src2
68 orig
= '%s.older' % path
69 tmp
= __output('git-unpack-file %s' % orig_hash
)
70 os
.chmod(tmp
, int(orig_mode
, 8))
73 src1
= '%s.local' % path
74 tmp
= __output('git-unpack-file %s' % file
1_hash
)
75 os
.chmod(tmp
, int(file1_mode
, 8))
78 src2
= '%s.remote' % path
79 tmp
= __output('git-unpack-file %s' % file
2_hash
)
80 os
.chmod(tmp
, int(file2_mode
, 8))
83 def __remove_files(orig_hash
, file1_hash
, file2_hash
):
84 """Remove any temporary files
94 # GIT_DIR value cached
98 """Write the conflict file for the 'path' variable and exit
103 if 'GIT_DIR' in os
.environ
:
104 __base_dir
= os
.environ
['GIT_DIR']
106 __base_dir
= __output('git-rev-parse --git-dir')
108 append_string(os
.path
.join(__base_dir
, 'conflicts'), path
)
114 def merge(orig_hash
, file1_hash
, file2_hash
,
116 orig_mode
, file1_mode
, file2_mode
):
117 """Three-way merge for one file algorithm
119 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
121 orig_mode
, file1_mode
, file2_mode
)
123 # file exists in origin
126 if file1_hash
and file2_hash
:
127 # if modes are the same (git-read-tree probably dealt with it)
128 if file1_hash
== file2_hash
:
129 if os
.system('git-update-index --cacheinfo %s %s %s'
130 %
(file1_mode
, file1_hash
, path
)) != 0:
131 print >> sys
.stderr
, 'Error: git-update-index failed'
134 if os
.system('git-checkout-index -u -f -- %s' % path
):
135 print >> sys
.stderr
, 'Error: git-checkout-index failed'
138 if file1_mode
!= file2_mode
:
139 print >> sys
.stderr
, \
140 'Error: File added in both, permissions conflict'
145 merge_ok
= os
.system(merger %
{'branch1': src1
,
148 'output': path
}) == 0
151 os
.system('git-update-index -- %s' % path
)
152 __remove_files(orig_hash
, file1_hash
, file2_hash
)
155 print >> sys
.stderr
, \
156 'Error: three-way merge tool failed for file "%s"' \
158 # reset the cache to the first branch
159 os
.system('git-update-index --cacheinfo %s %s %s'
160 %
(file1_mode
, file1_hash
, path
))
161 if keeporig
!= 'yes':
162 __remove_files(orig_hash
, file1_hash
, file2_hash
)
165 # file deleted in both or deleted in one and unchanged in the other
166 elif not (file1_hash
or file2_hash
) \
167 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
168 if os
.path
.exists(path
):
170 __remove_files(orig_hash
, file1_hash
, file2_hash
)
171 return os
.system('git-update-index --remove -- %s' % path
)
172 # file deleted in one and changed in the other
174 # Do something here - we must at least merge the entry in
175 # the cache, instead of leaving it in U(nmerged) state. In
176 # fact, stg resolved does not handle that.
178 # Do the same thing cogito does - remove the file in any case.
179 os
.system('git-update-index --remove -- %s' % path
)
182 ## file deleted upstream and changed in the patch. The
183 ## patch is probably going to move the changes
186 #os.system('git-update-index --remove -- %s' % path)
188 ## file deleted in the patch and changed upstream. We
189 ## could re-delete it, but for now leave it there -
190 ## and let the user check if he still wants to remove
193 ## reset the cache to the first branch
194 #os.system('git-update-index --cacheinfo %s %s %s'
195 # % (file1_mode, file1_hash, path))
199 # file does not exist in origin
202 if file1_hash
and file2_hash
:
204 if file1_hash
== file2_hash
:
205 if os
.system('git-update-index --add --cacheinfo %s %s %s'
206 %
(file1_mode
, file1_hash
, path
)) != 0:
207 print >> sys
.stderr
, 'Error: git-update-index failed'
210 if os
.system('git-checkout-index -u -f -- %s' % path
):
211 print >> sys
.stderr
, 'Error: git-checkout-index failed'
214 if file1_mode
!= file2_mode
:
215 print >> sys
.stderr
, \
216 'Error: File "s" added in both, ' \
217 'permissions conflict' % path
220 # files are different
222 print >> sys
.stderr
, \
223 'Error: File "%s" added in branches but different' % path
227 elif file1_hash
or file2_hash
:
234 if os
.system('git-update-index --add --cacheinfo %s %s %s'
235 %
(mode
, obj
, path
)) != 0:
236 print >> sys
.stderr
, 'Error: git-update-index failed'
239 __remove_files(orig_hash
, file1_hash
, file2_hash
)
240 return os
.system('git-checkout-index -u -f -- %s' % path
)
243 print >> sys
.stderr
, 'Error: Unhandled merge conflict: ' \
244 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
245 %
(orig_hash
, file1_hash
, file2_hash
,
247 orig_mode
, file1_mode
, file2_mode
)