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('stgit', 'merger')
35 keeporig
= config
.get('stgit', 'keeporig')
36 except Exception, err
:
37 raise GitMergeException
, 'Configuration error: %s' % err
50 f
= os
.popen(cmd
, 'r')
51 string
= f
.readline().rstrip()
53 raise GitMergeException
, 'Error: failed to execute "%s"' % cmd
56 def __checkout_files(orig_hash
, file1_hash
, file2_hash
,
58 orig_mode
, file1_mode
, file2_mode
):
59 """Check out the files passed as arguments
61 global orig
, src1
, src2
64 orig
= '%s.older' % path
65 tmp
= __output('git-unpack-file %s' % orig_hash
)
66 os
.chmod(tmp
, int(orig_mode
, 8))
69 src1
= '%s.local' % path
70 tmp
= __output('git-unpack-file %s' % file
1_hash
)
71 os
.chmod(tmp
, int(file1_mode
, 8))
74 src2
= '%s.remote' % path
75 tmp
= __output('git-unpack-file %s' % file
2_hash
)
76 os
.chmod(tmp
, int(file2_mode
, 8))
79 def __remove_files(orig_hash
, file1_hash
, file2_hash
):
80 """Remove any temporary files
90 # GIT_DIR value cached
94 """Write the conflict file for the 'path' variable and exit
99 if 'GIT_DIR' in os
.environ
:
100 __base_dir
= os
.environ
['GIT_DIR']
102 __base_dir
= __output('git-rev-parse --git-dir')
104 append_string(os
.path
.join(__base_dir
, 'conflicts'), path
)
110 def merge(orig_hash
, file1_hash
, file2_hash
,
112 orig_mode
, file1_mode
, file2_mode
):
113 """Three-way merge for one file algorithm
115 __checkout_files(orig_hash
, file1_hash
, file2_hash
,
117 orig_mode
, file1_mode
, file2_mode
)
119 # file exists in origin
122 if file1_hash
and file2_hash
:
123 # if modes are the same (git-read-tree probably dealt with it)
124 if file1_hash
== file2_hash
:
125 if os
.system('git-update-index --cacheinfo %s %s %s'
126 %
(file1_mode
, file1_hash
, path
)) != 0:
127 print >> sys
.stderr
, 'Error: git-update-index failed'
130 if os
.system('git-checkout-index -u -f -- %s' % path
):
131 print >> sys
.stderr
, 'Error: git-checkout-index failed'
134 if file1_mode
!= file2_mode
:
135 print >> sys
.stderr
, \
136 'Error: File added in both, permissions conflict'
141 merge_ok
= os
.system(merger %
{'branch1': src1
,
144 'output': path
}) == 0
147 os
.system('git-update-index -- %s' % path
)
148 __remove_files(orig_hash
, file1_hash
, file2_hash
)
151 print >> sys
.stderr
, \
152 'Error: three-way merge tool failed for file "%s"' \
154 # reset the cache to the first branch
155 os
.system('git-update-index --cacheinfo %s %s %s'
156 %
(file1_mode
, file1_hash
, path
))
157 if keeporig
!= 'yes':
158 __remove_files(orig_hash
, file1_hash
, file2_hash
)
161 # file deleted in both or deleted in one and unchanged in the other
162 elif not (file1_hash
or file2_hash
) \
163 or file1_hash
== orig_hash
or file2_hash
== orig_hash
:
164 if os
.path
.exists(path
):
166 __remove_files(orig_hash
, file1_hash
, file2_hash
)
167 return os
.system('git-update-index --remove -- %s' % path
)
168 # file deleted in one and changed in the other
170 # Do something here - we must at least merge the entry in
171 # the cache, instead of leaving it in U(nmerged) state. In
172 # fact, stg resolved does not handle that.
174 # Do the same thing cogito does - remove the file in any case.
175 os
.system('git-update-index --remove -- %s' % path
)
178 ## file deleted upstream and changed in the patch. The
179 ## patch is probably going to move the changes
182 #os.system('git-update-index --remove -- %s' % path)
184 ## file deleted in the patch and changed upstream. We
185 ## could re-delete it, but for now leave it there -
186 ## and let the user check if he still wants to remove
189 ## reset the cache to the first branch
190 #os.system('git-update-index --cacheinfo %s %s %s'
191 # % (file1_mode, file1_hash, path))
195 # file does not exist in origin
198 if file1_hash
and file2_hash
:
200 if file1_hash
== file2_hash
:
201 if os
.system('git-update-index --add --cacheinfo %s %s %s'
202 %
(file1_mode
, file1_hash
, path
)) != 0:
203 print >> sys
.stderr
, 'Error: git-update-index failed'
206 if os
.system('git-checkout-index -u -f -- %s' % path
):
207 print >> sys
.stderr
, 'Error: git-checkout-index failed'
210 if file1_mode
!= file2_mode
:
211 print >> sys
.stderr
, \
212 'Error: File "s" added in both, ' \
213 'permissions conflict' % path
216 # files are different
218 print >> sys
.stderr
, \
219 'Error: File "%s" added in branches but different' % path
223 elif file1_hash
or file2_hash
:
230 if os
.system('git-update-index --add --cacheinfo %s %s %s'
231 %
(mode
, obj
, path
)) != 0:
232 print >> sys
.stderr
, 'Error: git-update-index failed'
235 __remove_files(orig_hash
, file1_hash
, file2_hash
)
236 return os
.system('git-checkout-index -u -f -- %s' % path
)
239 print >> sys
.stderr
, 'Error: Unhandled merge conflict: ' \
240 '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
241 %
(orig_hash
, file1_hash
, file2_hash
,
243 orig_mode
, file1_mode
, file2_mode
)