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
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
65 orig = '%s.older' % path
66 tmp = __output('git-unpack-file %s' % orig_hash)
67 os.chmod(tmp, int(orig_mode, 8))
70 src1 = '%s.local' % path
71 tmp = __output('git-unpack-file %s' % file1_hash)
72 os.chmod(tmp, int(file1_mode, 8))
75 src2 = '%s.remote' % path
76 tmp = __output('git-unpack-file %s' % file2_hash)
77 os.chmod(tmp, int(file2_mode, 8))
80 def __remove_files(orig_hash, file1_hash, file2_hash):
81 """Remove any temporary files
92 """Write the conflict file for the 'path' variable and exit
94 append_string(os.path.join(basedir.get(), 'conflicts'), path)
100 def merge(orig_hash, file1_hash, file2_hash,
102 orig_mode, file1_mode, file2_mode):
103 """Three-way merge for one file algorithm
105 __checkout_files(orig_hash, file1_hash, file2_hash,
107 orig_mode, file1_mode, file2_mode)
109 # file exists in origin
112 if file1_hash and file2_hash:
113 # if modes are the same (git-read-tree probably dealt with it)
114 if file1_hash == file2_hash:
115 if os.system('git-update-index --cacheinfo %s %s %s'
116 % (file1_mode, file1_hash, path)) != 0:
117 print >> sys.stderr, 'Error: git-update-index failed'
120 if os.system('git-checkout-index -u -f -- %s' % path):
121 print >> sys.stderr, 'Error: git-checkout-index failed'
124 if file1_mode != file2_mode:
125 print >> sys.stderr, \
126 'Error: File added in both, permissions conflict'
131 merge_ok = os.system(merger % {'branch1': src1,
134 'output': path }) == 0
137 os.system('git-update-index -- %s' % path)
138 __remove_files(orig_hash, file1_hash, file2_hash)
141 print >> sys.stderr, \
142 'Error: three-way merge tool failed for file "%s"' \
144 # reset the cache to the first branch
145 os.system('git-update-index --cacheinfo %s %s %s'
146 % (file1_mode, file1_hash, path))
147 if keeporig != 'yes':
148 __remove_files(orig_hash, file1_hash, file2_hash)
151 # file deleted in both or deleted in one and unchanged in the other
152 elif not (file1_hash or file2_hash) \
153 or file1_hash == orig_hash or file2_hash == orig_hash:
154 if os.path.exists(path):
156 __remove_files(orig_hash, file1_hash, file2_hash)
157 return os.system('git-update-index --remove -- %s' % path)
158 # file deleted in one and changed in the other
160 # Do something here - we must at least merge the entry in
161 # the cache, instead of leaving it in U(nmerged) state. In
162 # fact, stg resolved does not handle that.
164 # Do the same thing cogito does - remove the file in any case.
165 os.system('git-update-index --remove -- %s' % path)
168 ## file deleted upstream and changed in the patch. The
169 ## patch is probably going to move the changes
172 #os.system('git-update-index --remove -- %s' % path)
174 ## file deleted in the patch and changed upstream. We
175 ## could re-delete it, but for now leave it there -
176 ## and let the user check if he still wants to remove
179 ## reset the cache to the first branch
180 #os.system('git-update-index --cacheinfo %s %s %s'
181 # % (file1_mode, file1_hash, path))
185 # file does not exist in origin
188 if file1_hash and file2_hash:
190 if file1_hash == file2_hash:
191 if os.system('git-update-index --add --cacheinfo %s %s %s'
192 % (file1_mode, file1_hash, path)) != 0:
193 print >> sys.stderr, 'Error: git-update-index failed'
196 if os.system('git-checkout-index -u -f -- %s' % path):
197 print >> sys.stderr, 'Error: git-checkout-index failed'
200 if file1_mode != file2_mode:
201 print >> sys.stderr, \
202 'Error: File "s" added in both, ' \
203 'permissions conflict' % path
206 # files are different
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)