gitmergeonefile.py should use git.get_base_dir()
[stgit] / gitmergeonefile.py
CommitLineData
41a6d859
CM
1#!/usr/bin/env python
2"""Performs a 3-way merge for GIT files
3"""
4
5__copyright__ = """
6Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com>
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License version 2 as
10published by the Free Software Foundation.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20"""
21
22import sys, os
7ab9ac47
CM
23
24# Try to detect where it is run from and set prefix and the search path.
25# It is assumed that the user installed StGIT using the --prefix= option
26prefix, bin = os.path.split(sys.path[0])
27
28if bin == 'bin' and prefix != sys.prefix:
29 major, minor = sys.version_info[0:2]
9c96f9f2
CM
30 local_path = [os.path.join(prefix, 'lib', 'python'),
31 os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor)),
32 os.path.join(prefix, 'lib', 'python%s.%s' % (major, minor),
33 'site-packages')]
34 sys.path = local_path + sys.path
7ab9ac47 35
41a6d859
CM
36from stgit.config import config
37from stgit.utils import append_string
a622d90b 38from stgit.git import get_base_dir
41a6d859
CM
39
40
41#
42# Options
43#
44try:
45 merger = config.get('gitmergeonefile', 'merger')
46except Exception, err:
47 print >> sys.stderr, 'Configuration error: %s' % err
48 sys.exit(1)
49
50if config.has_option('gitmergeonefile', 'keeporig'):
51 keeporig = config.get('gitmergeonefile', 'keeporig')
52else:
53 keeporig = 'yes'
54
55
56#
41a6d859
CM
57# Utility functions
58#
59def __str2none(x):
60 if x == '':
61 return None
62 else:
63 return x
64
65def __output(cmd):
66 f = os.popen(cmd, 'r')
67 string = f.readline().strip()
68 if f.close():
69 print >> sys.stderr, 'Error: failed to execute "%s"' % cmd
70 sys.exit(1)
71 return string
72
73def __checkout_files():
74 """Check out the files passed as arguments
75 """
76 global orig, src1, src2
77
78 if orig_hash:
79 orig = '%s.older' % path
80 tmp = __output('git-unpack-file %s' % orig_hash)
81 os.chmod(tmp, int(orig_mode, 8))
8c737ba4 82 os.renames(tmp, orig)
41a6d859
CM
83 if file1_hash:
84 src1 = '%s.local' % path
85 tmp = __output('git-unpack-file %s' % file1_hash)
86 os.chmod(tmp, int(file1_mode, 8))
8c737ba4 87 os.renames(tmp, src1)
41a6d859
CM
88 if file2_hash:
89 src2 = '%s.remote' % path
90 tmp = __output('git-unpack-file %s' % file2_hash)
91 os.chmod(tmp, int(file2_mode, 8))
8c737ba4 92 os.renames(tmp, src2)
41a6d859
CM
93
94def __remove_files():
95 """Remove any temporary files
96 """
97 if orig_hash:
98 os.remove(orig)
99 if file1_hash:
100 os.remove(src1)
101 if file2_hash:
102 os.remove(src2)
103 pass
104
105def __conflict():
106 """Write the conflict file for the 'path' variable and exit
107 """
a622d90b 108 append_string(os.path.join(get_base_dir(), 'conflicts'), path)
41a6d859
CM
109 sys.exit(1)
110
111
112# $1 - original file SHA1 (or empty)
113# $2 - file in branch1 SHA1 (or empty)
114# $3 - file in branch2 SHA1 (or empty)
115# $4 - pathname in repository
388f63b6 116# $5 - original file mode (or empty)
41a6d859
CM
117# $6 - file in branch1 mode (or empty)
118# $7 - file in branch2 mode (or empty)
8c737ba4
CM
119#
120#print 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
121# % tuple(sys.argv[1:8])
41a6d859
CM
122orig_hash, file1_hash, file2_hash, path, orig_mode, file1_mode, file2_mode = \
123 [__str2none(x) for x in sys.argv[1:8]]
124
125
126#
127# Main algorithm
128#
129__checkout_files()
130
131# file exists in origin
132if orig_hash:
133 # modified in both
134 if file1_hash and file2_hash:
388f63b6 135 # if modes are the same (git-read-tree probably dealt with it)
41a6d859 136 if file1_hash == file2_hash:
7c09df84 137 if os.system('git-update-index --cacheinfo %s %s %s'
41a6d859 138 % (file1_mode, file1_hash, path)) != 0:
7c09df84 139 print >> sys.stderr, 'Error: git-update-index failed'
41a6d859 140 __conflict()
7c09df84
JH
141 if os.system('git-checkout-index -u -f -- %s' % path):
142 print >> sys.stderr, 'Error: git-checkout-index failed'
41a6d859
CM
143 __conflict()
144 if file1_mode != file2_mode:
145 print >> sys.stderr, \
146 'Error: File added in both, permissions conflict'
147 __conflict()
148 # 3-way merge
149 else:
150 merge_ok = os.system(merger % {'branch1': src1,
151 'ancestor': orig,
152 'branch2': src2,
153 'output': path }) == 0
154
155 if merge_ok:
7c09df84 156 os.system('git-update-index -- %s' % path)
41a6d859
CM
157 __remove_files()
158 sys.exit(0)
159 else:
160 print >> sys.stderr, \
161 'Error: three-way merge tool failed for file "%s"' % path
162 # reset the cache to the first branch
7c09df84 163 os.system('git-update-index --cacheinfo %s %s %s'
41a6d859
CM
164 % (file1_mode, file1_hash, path))
165 if keeporig != 'yes':
166 __remove_files()
167 __conflict()
168 # file deleted in both or deleted in one and unchanged in the other
169 elif not (file1_hash or file2_hash) \
170 or file1_hash == orig_hash or file2_hash == orig_hash:
171 if os.path.exists(path):
172 os.remove(path)
173 __remove_files()
7c09df84 174 sys.exit(os.system('git-update-index --remove -- %s' % path))
1780280c
PBG
175 # file deleted in one and changed in the other
176 else:
177 # Do something here - we must at least merge the entry in the cache,
178 # instead of leaving it in U(nmerged) state. In fact, stg resolved
179 # does not handle that.
180
181 # Do the same thing cogito does - remove the file in any case.
182 os.system('git-update-index --remove -- %s' % path)
183
184 #if file1_hash:
185 ## file deleted upstream and changed in the patch. The patch is
186 ## probably going to move the changes elsewhere.
187
188 #os.system('git-update-index --remove -- %s' % path)
189 #else:
190 ## file deleted in the patch and changed upstream. We could re-delete
191 ## it, but for now leave it there - and let the user check if he
192 ## still wants to remove the file.
193
194 ## reset the cache to the first branch
195 #os.system('git-update-index --cacheinfo %s %s %s'
196 #% (file1_mode, file1_hash, path))
197 __conflict()
198
41a6d859
CM
199# file does not exist in origin
200else:
201 # file added in both
202 if file1_hash and file2_hash:
203 # files are the same
204 if file1_hash == file2_hash:
7c09df84 205 if os.system('git-update-index --add --cacheinfo %s %s %s'
41a6d859 206 % (file1_mode, file1_hash, path)) != 0:
7c09df84 207 print >> sys.stderr, 'Error: git-update-index failed'
41a6d859 208 __conflict()
7c09df84
JH
209 if os.system('git-checkout-index -u -f -- %s' % path):
210 print >> sys.stderr, 'Error: git-checkout-index failed'
41a6d859
CM
211 __conflict()
212 if file1_mode != file2_mode:
213 print >> sys.stderr, \
214 'Error: File "s" added in both, permissions conflict' \
215 % path
216 __conflict()
217 # files are different
218 else:
219 print >> sys.stderr, \
220 'Error: File "%s" added in branches but different' % path
221 __conflict()
222 # file added in one
223 elif file1_hash or file2_hash:
224 if file1_hash:
225 mode = file1_mode
226 obj = file1_hash
227 else:
228 mode = file2_mode
229 obj = file2_hash
7c09df84 230 if os.system('git-update-index --add --cacheinfo %s %s %s'
41a6d859 231 % (mode, obj, path)) != 0:
7c09df84 232 print >> sys.stderr, 'Error: git-update-index failed'
41a6d859
CM
233 __conflict()
234 __remove_files()
7c09df84 235 sys.exit(os.system('git-checkout-index -u -f -- %s' % path))
41a6d859 236
388f63b6
PR
237# Unhandled case
238print >> sys.stderr, 'Error: Unhandled merge conflict'
8c737ba4 239print >> sys.stderr, 'gitmergeonefile.py "%s" "%s" "%s" "%s" "%s" "%s" "%s"' \
41a6d859
CM
240 % tuple(sys.argv[1:8])
241__conflict()