2 """Performs a 3-way merge for GIT files
6 Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 from stgit.config import config
24 from stgit.utils import append_string
31 merger = config.get('gitmergeonefile', 'merger')
32 except Exception, err:
33 print >> sys.stderr, 'Configuration error: %s' % err
36 if config.has_option('gitmergeonefile', 'keeporig'):
37 keeporig = config.get('gitmergeonefile', 'keeporig')
45 if 'GIT_DIR' in os.environ:
46 base_dir = os.environ['GIT_DIR']
61 f = os.popen(cmd, 'r')
62 string = f.readline().strip()
64 print >> sys.stderr, 'Error: failed to execute "%s"' % cmd
68 def __checkout_files():
69 """Check out the files passed as arguments
71 global orig, src1, src2
74 orig = '%s.older' % path
75 tmp = __output('git-unpack-file %s' % orig_hash)
76 os.chmod(tmp, int(orig_mode, 8))
79 src1 = '%s.local' % path
80 tmp = __output('git-unpack-file %s' % file1_hash)
81 os.chmod(tmp, int(file1_mode, 8))
84 src2 = '%s.remote' % path
85 tmp = __output('git-unpack-file %s' % file2_hash)
86 os.chmod(tmp, int(file2_mode, 8))
90 """Remove any temporary files
101 """Write the conflict file for the 'path' variable and exit
103 append_string(os.path.join(base_dir, 'conflicts'), path)
107 # $1 - original file SHA1 (or empty)
108 # $2 - file in branch1 SHA1 (or empty)
109 # $3 - file in branch2 SHA1 (or empty)
110 # $4 - pathname in repository
111 # $5 - orignal file mode (or empty)
112 # $6 - file in branch1 mode (or empty)
113 # $7 - file in branch2 mode (or empty)
114 #print 'gitmerge.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % tuple(sys.argv[1:8])
115 orig_hash, file1_hash, file2_hash, path, orig_mode, file1_mode, file2_mode = \
116 [__str2none(x) for x in sys.argv[1:8]]
124 # file exists in origin
127 if file1_hash and file2_hash:
128 # if modes are the same (git-read-tree probably dealed with it)
129 if file1_hash == file2_hash:
130 if os.system('git-update-cache --cacheinfo %s %s %s'
131 % (file1_mode, file1_hash, path)) != 0:
132 print >> sys.stderr, 'Error: git-update-cache failed'
134 if os.system('git-checkout-cache -u -f -- %s' % path):
135 print >> sys.stderr, 'Error: git-checkout-cache failed'
137 if file1_mode != file2_mode:
138 print >> sys.stderr, \
139 'Error: File added in both, permissions conflict'
143 merge_ok = os.system(merger % {'branch1': src1,
146 'output': path }) == 0
149 os.system('git-update-cache %s' % path)
153 print >> sys.stderr, \
154 'Error: three-way merge tool failed for file "%s"' % path
155 # reset the cache to the first branch
156 os.system('git-update-cache --cacheinfo %s %s %s'
157 % (file1_mode, file1_hash, path))
158 if keeporig != 'yes':
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):
167 sys.exit(os.system('git-update-cache --remove %s' % path))
168 # file does not exist in origin
171 if file1_hash and file2_hash:
173 if file1_hash == file2_hash:
174 if os.system('git-update-cache --add --cacheinfo %s %s %s'
175 % (file1_mode, file1_hash, path)) != 0:
176 print >> sys.stderr, 'Error: git-update-cache failed'
178 if os.system('git-checkout-cache -u -f -- %s' % path):
179 print >> sys.stderr, 'Error: git-checkout-cache failed'
181 if file1_mode != file2_mode:
182 print >> sys.stderr, \
183 'Error: File "s" added in both, permissions conflict' \
186 # files are different
188 print >> sys.stderr, \
189 'Error: File "%s" added in branches but different' % path
192 elif file1_hash or file2_hash:
199 if os.system('git-update-cache --add --cacheinfo %s %s %s'
200 % (mode, obj, path)) != 0:
201 print >> sys.stderr, 'Error: git-update-cache failed'
204 sys.exit(os.system('git-checkout-cache -u -f -- %s' % path))
207 print >> sys.stderr, 'Error: Un-handled merge conflict'
208 print >> sys.stderr, 'gitmerge.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
209 % tuple(sys.argv[1:8])