Commit | Line | Data |
---|---|---|
3659ef88 CM |
1 | """Performs a 3-way merge for GIT files |
2 | """ | |
3 | ||
4 | __copyright__ = """ | |
5 | Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com> | |
6 | ||
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. | |
10 | ||
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. | |
15 | ||
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 | |
19 | """ | |
20 | ||
21 | import sys, os | |
170f576b | 22 | from stgit import basedir |
f7ed76a9 | 23 | from stgit.config import config, file_extensions, ConfigOption |
3659ef88 CM |
24 | from stgit.utils import append_string |
25 | ||
26 | ||
27 | class GitMergeException(Exception): | |
28 | pass | |
29 | ||
30 | ||
31 | # | |
32 | # Options | |
33 | # | |
eee7283e CM |
34 | merger = ConfigOption('stgit', 'merger') |
35 | keeporig = ConfigOption('stgit', 'keeporig') | |
3659ef88 CM |
36 | |
37 | # | |
38 | # Utility functions | |
39 | # | |
40 | def __str2none(x): | |
41 | if x == '': | |
42 | return None | |
43 | else: | |
44 | return x | |
45 | ||
46 | def __output(cmd): | |
47 | f = os.popen(cmd, 'r') | |
48 | string = f.readline().rstrip() | |
49 | if f.close(): | |
50 | raise GitMergeException, 'Error: failed to execute "%s"' % cmd | |
51 | return string | |
52 | ||
53 | def __checkout_files(orig_hash, file1_hash, file2_hash, | |
54 | path, | |
55 | orig_mode, file1_mode, file2_mode): | |
56 | """Check out the files passed as arguments | |
57 | """ | |
58 | global orig, src1, src2 | |
59 | ||
1e075406 CM |
60 | extensions = file_extensions() |
61 | ||
3659ef88 | 62 | if orig_hash: |
1e075406 | 63 | orig = path + extensions['ancestor'] |
3659ef88 CM |
64 | tmp = __output('git-unpack-file %s' % orig_hash) |
65 | os.chmod(tmp, int(orig_mode, 8)) | |
66 | os.renames(tmp, orig) | |
67 | if file1_hash: | |
1e075406 | 68 | src1 = path + extensions['current'] |
3659ef88 CM |
69 | tmp = __output('git-unpack-file %s' % file1_hash) |
70 | os.chmod(tmp, int(file1_mode, 8)) | |
71 | os.renames(tmp, src1) | |
72 | if file2_hash: | |
1e075406 | 73 | src2 = path + extensions['patched'] |
3659ef88 CM |
74 | tmp = __output('git-unpack-file %s' % file2_hash) |
75 | os.chmod(tmp, int(file2_mode, 8)) | |
76 | os.renames(tmp, src2) | |
77 | ||
8d415553 CM |
78 | if file1_hash and not os.path.exists(path): |
79 | # the current file might be removed by GIT when it is a new | |
80 | # file added in both branches. Just re-generate it | |
81 | tmp = __output('git-unpack-file %s' % file1_hash) | |
82 | os.chmod(tmp, int(file1_mode, 8)) | |
83 | os.renames(tmp, path) | |
84 | ||
3659ef88 CM |
85 | def __remove_files(orig_hash, file1_hash, file2_hash): |
86 | """Remove any temporary files | |
87 | """ | |
88 | if orig_hash: | |
89 | os.remove(orig) | |
90 | if file1_hash: | |
91 | os.remove(src1) | |
92 | if file2_hash: | |
93 | os.remove(src2) | |
3659ef88 | 94 | |
3659ef88 CM |
95 | def __conflict(path): |
96 | """Write the conflict file for the 'path' variable and exit | |
97 | """ | |
170f576b | 98 | append_string(os.path.join(basedir.get(), 'conflicts'), path) |
3659ef88 CM |
99 | |
100 | ||
f7ed76a9 CM |
101 | def interactive_merge(filename): |
102 | """Run the interactive merger on the given file. Note that the | |
103 | index should not have any conflicts. | |
104 | """ | |
105 | try: | |
106 | imerger = config.get('stgit', 'imerger') | |
107 | except Exception, err: | |
108 | raise GitMergeException, 'Configuration error: %s' % err | |
109 | ||
110 | extensions = file_extensions() | |
111 | ||
112 | ancestor = filename + extensions['ancestor'] | |
113 | current = filename + extensions['current'] | |
114 | patched = filename + extensions['patched'] | |
115 | ||
116 | # check whether we have all the files for a three-way merge | |
117 | for fn in [filename, ancestor, current, patched]: | |
118 | if not os.path.isfile(fn): | |
119 | raise GitMergeException, \ | |
120 | 'Cannot run the interactive merger: "%s" missing' % fn | |
121 | ||
122 | mtime = os.path.getmtime(filename) | |
123 | ||
124 | err = os.system(imerger % {'branch1': current, | |
125 | 'ancestor': ancestor, | |
126 | 'branch2': patched, | |
127 | 'output': filename}) | |
128 | ||
129 | if err != 0: | |
130 | raise GitMergeException, 'The interactive merge failed: %d' % err | |
131 | if not os.path.isfile(filename): | |
132 | raise GitMergeException, 'The "%s" file is missing' % filename | |
133 | if mtime == os.path.getmtime(filename): | |
134 | raise GitMergeException, 'The "%s" file was not modified' % filename | |
135 | ||
136 | ||
3659ef88 CM |
137 | # |
138 | # Main algorithm | |
139 | # | |
140 | def merge(orig_hash, file1_hash, file2_hash, | |
141 | path, | |
142 | orig_mode, file1_mode, file2_mode): | |
143 | """Three-way merge for one file algorithm | |
144 | """ | |
145 | __checkout_files(orig_hash, file1_hash, file2_hash, | |
146 | path, | |
147 | orig_mode, file1_mode, file2_mode) | |
148 | ||
149 | # file exists in origin | |
150 | if orig_hash: | |
151 | # modified in both | |
152 | if file1_hash and file2_hash: | |
153 | # if modes are the same (git-read-tree probably dealt with it) | |
154 | if file1_hash == file2_hash: | |
155 | if os.system('git-update-index --cacheinfo %s %s %s' | |
156 | % (file1_mode, file1_hash, path)) != 0: | |
157 | print >> sys.stderr, 'Error: git-update-index failed' | |
158 | __conflict(path) | |
159 | return 1 | |
160 | if os.system('git-checkout-index -u -f -- %s' % path): | |
161 | print >> sys.stderr, 'Error: git-checkout-index failed' | |
162 | __conflict(path) | |
163 | return 1 | |
164 | if file1_mode != file2_mode: | |
165 | print >> sys.stderr, \ | |
166 | 'Error: File added in both, permissions conflict' | |
167 | __conflict(path) | |
168 | return 1 | |
169 | # 3-way merge | |
170 | else: | |
5b99888b CM |
171 | merge_ok = os.system(str(merger) % {'branch1': src1, |
172 | 'ancestor': orig, | |
173 | 'branch2': src2, | |
174 | 'output': path }) == 0 | |
3659ef88 CM |
175 | |
176 | if merge_ok: | |
177 | os.system('git-update-index -- %s' % path) | |
178 | __remove_files(orig_hash, file1_hash, file2_hash) | |
179 | return 0 | |
180 | else: | |
181 | print >> sys.stderr, \ | |
182 | 'Error: three-way merge tool failed for file "%s"' \ | |
183 | % path | |
184 | # reset the cache to the first branch | |
185 | os.system('git-update-index --cacheinfo %s %s %s' | |
186 | % (file1_mode, file1_hash, path)) | |
f7ed76a9 CM |
187 | |
188 | if config.get('stgit', 'autoimerge') == 'yes': | |
189 | print >> sys.stderr, \ | |
190 | 'Trying the interactive merge' | |
191 | try: | |
192 | interactive_merge(path) | |
193 | except GitMergeException, ex: | |
194 | # interactive merge failed | |
195 | print >> sys.stderr, str(ex) | |
196 | if str(keeporig) != 'yes': | |
197 | __remove_files(orig_hash, file1_hash, | |
198 | file2_hash) | |
199 | __conflict(path) | |
200 | return 1 | |
201 | # successful interactive merge | |
0f4eba6a | 202 | os.system('git-update-index -- %s' % path) |
3659ef88 | 203 | __remove_files(orig_hash, file1_hash, file2_hash) |
f7ed76a9 CM |
204 | return 0 |
205 | else: | |
206 | # no interactive merge, just mark it as conflict | |
207 | if str(keeporig) != 'yes': | |
208 | __remove_files(orig_hash, file1_hash, file2_hash) | |
209 | __conflict(path) | |
210 | return 1 | |
211 | ||
3659ef88 CM |
212 | # file deleted in both or deleted in one and unchanged in the other |
213 | elif not (file1_hash or file2_hash) \ | |
214 | or file1_hash == orig_hash or file2_hash == orig_hash: | |
215 | if os.path.exists(path): | |
216 | os.remove(path) | |
217 | __remove_files(orig_hash, file1_hash, file2_hash) | |
218 | return os.system('git-update-index --remove -- %s' % path) | |
219 | # file deleted in one and changed in the other | |
220 | else: | |
221 | # Do something here - we must at least merge the entry in | |
222 | # the cache, instead of leaving it in U(nmerged) state. In | |
223 | # fact, stg resolved does not handle that. | |
224 | ||
225 | # Do the same thing cogito does - remove the file in any case. | |
226 | os.system('git-update-index --remove -- %s' % path) | |
227 | ||
228 | #if file1_hash: | |
229 | ## file deleted upstream and changed in the patch. The | |
230 | ## patch is probably going to move the changes | |
231 | ## elsewhere. | |
232 | ||
233 | #os.system('git-update-index --remove -- %s' % path) | |
234 | #else: | |
235 | ## file deleted in the patch and changed upstream. We | |
236 | ## could re-delete it, but for now leave it there - | |
237 | ## and let the user check if he still wants to remove | |
238 | ## the file. | |
239 | ||
240 | ## reset the cache to the first branch | |
241 | #os.system('git-update-index --cacheinfo %s %s %s' | |
242 | # % (file1_mode, file1_hash, path)) | |
243 | __conflict(path) | |
244 | return 1 | |
245 | ||
246 | # file does not exist in origin | |
247 | else: | |
248 | # file added in both | |
249 | if file1_hash and file2_hash: | |
250 | # files are the same | |
251 | if file1_hash == file2_hash: | |
252 | if os.system('git-update-index --add --cacheinfo %s %s %s' | |
253 | % (file1_mode, file1_hash, path)) != 0: | |
254 | print >> sys.stderr, 'Error: git-update-index failed' | |
255 | __conflict(path) | |
256 | return 1 | |
257 | if os.system('git-checkout-index -u -f -- %s' % path): | |
258 | print >> sys.stderr, 'Error: git-checkout-index failed' | |
259 | __conflict(path) | |
260 | return 1 | |
261 | if file1_mode != file2_mode: | |
262 | print >> sys.stderr, \ | |
263 | 'Error: File "s" added in both, ' \ | |
264 | 'permissions conflict' % path | |
265 | __conflict(path) | |
266 | return 1 | |
267 | # files are different | |
268 | else: | |
56df21ed CM |
269 | # reset the index to the current file |
270 | os.system('git-update-index -- %s' % path) | |
3659ef88 CM |
271 | print >> sys.stderr, \ |
272 | 'Error: File "%s" added in branches but different' % path | |
273 | __conflict(path) | |
274 | return 1 | |
275 | # file added in one | |
276 | elif file1_hash or file2_hash: | |
277 | if file1_hash: | |
278 | mode = file1_mode | |
279 | obj = file1_hash | |
280 | else: | |
281 | mode = file2_mode | |
282 | obj = file2_hash | |
283 | if os.system('git-update-index --add --cacheinfo %s %s %s' | |
284 | % (mode, obj, path)) != 0: | |
285 | print >> sys.stderr, 'Error: git-update-index failed' | |
286 | __conflict(path) | |
287 | return 1 | |
288 | __remove_files(orig_hash, file1_hash, file2_hash) | |
289 | return os.system('git-checkout-index -u -f -- %s' % path) | |
290 | ||
291 | # Unhandled case | |
292 | print >> sys.stderr, 'Error: Unhandled merge conflict: ' \ | |
293 | '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \ | |
294 | % (orig_hash, file1_hash, file2_hash, | |
295 | path, | |
296 | orig_mode, file1_mode, file2_mode) | |
297 | __conflict(path) | |
298 | return 1 |