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 |
d7fade4b | 23 | from stgit.config import config, file_extensions |
3659ef88 CM |
24 | from stgit.utils import append_string |
25 | ||
26 | ||
27 | class GitMergeException(Exception): | |
28 | pass | |
29 | ||
30 | ||
31 | # | |
32 | # Options | |
33 | # | |
34 | try: | |
c19dc3db CM |
35 | merger = config.get('stgit', 'merger') |
36 | keeporig = config.get('stgit', 'keeporig') | |
3659ef88 CM |
37 | except Exception, err: |
38 | raise GitMergeException, 'Configuration error: %s' % err | |
39 | ||
3659ef88 CM |
40 | |
41 | # | |
42 | # Utility functions | |
43 | # | |
44 | def __str2none(x): | |
45 | if x == '': | |
46 | return None | |
47 | else: | |
48 | return x | |
49 | ||
50 | def __output(cmd): | |
51 | f = os.popen(cmd, 'r') | |
52 | string = f.readline().rstrip() | |
53 | if f.close(): | |
54 | raise GitMergeException, 'Error: failed to execute "%s"' % cmd | |
55 | return string | |
56 | ||
57 | def __checkout_files(orig_hash, file1_hash, file2_hash, | |
58 | path, | |
59 | orig_mode, file1_mode, file2_mode): | |
60 | """Check out the files passed as arguments | |
61 | """ | |
62 | global orig, src1, src2 | |
63 | ||
1e075406 CM |
64 | extensions = file_extensions() |
65 | ||
3659ef88 | 66 | if orig_hash: |
1e075406 | 67 | orig = path + extensions['ancestor'] |
3659ef88 CM |
68 | tmp = __output('git-unpack-file %s' % orig_hash) |
69 | os.chmod(tmp, int(orig_mode, 8)) | |
70 | os.renames(tmp, orig) | |
71 | if file1_hash: | |
1e075406 | 72 | src1 = path + extensions['current'] |
3659ef88 CM |
73 | tmp = __output('git-unpack-file %s' % file1_hash) |
74 | os.chmod(tmp, int(file1_mode, 8)) | |
75 | os.renames(tmp, src1) | |
76 | if file2_hash: | |
1e075406 | 77 | src2 = path + extensions['patched'] |
3659ef88 CM |
78 | tmp = __output('git-unpack-file %s' % file2_hash) |
79 | os.chmod(tmp, int(file2_mode, 8)) | |
80 | os.renames(tmp, src2) | |
81 | ||
82 | def __remove_files(orig_hash, file1_hash, file2_hash): | |
83 | """Remove any temporary files | |
84 | """ | |
85 | if orig_hash: | |
86 | os.remove(orig) | |
87 | if file1_hash: | |
88 | os.remove(src1) | |
89 | if file2_hash: | |
90 | os.remove(src2) | |
91 | pass | |
92 | ||
3659ef88 CM |
93 | def __conflict(path): |
94 | """Write the conflict file for the 'path' variable and exit | |
95 | """ | |
170f576b | 96 | append_string(os.path.join(basedir.get(), 'conflicts'), path) |
3659ef88 CM |
97 | |
98 | ||
99 | # | |
100 | # Main algorithm | |
101 | # | |
102 | def merge(orig_hash, file1_hash, file2_hash, | |
103 | path, | |
104 | orig_mode, file1_mode, file2_mode): | |
105 | """Three-way merge for one file algorithm | |
106 | """ | |
107 | __checkout_files(orig_hash, file1_hash, file2_hash, | |
108 | path, | |
109 | orig_mode, file1_mode, file2_mode) | |
110 | ||
111 | # file exists in origin | |
112 | if orig_hash: | |
113 | # modified in both | |
114 | if file1_hash and file2_hash: | |
115 | # if modes are the same (git-read-tree probably dealt with it) | |
116 | if file1_hash == file2_hash: | |
117 | if os.system('git-update-index --cacheinfo %s %s %s' | |
118 | % (file1_mode, file1_hash, path)) != 0: | |
119 | print >> sys.stderr, 'Error: git-update-index failed' | |
120 | __conflict(path) | |
121 | return 1 | |
122 | if os.system('git-checkout-index -u -f -- %s' % path): | |
123 | print >> sys.stderr, 'Error: git-checkout-index failed' | |
124 | __conflict(path) | |
125 | return 1 | |
126 | if file1_mode != file2_mode: | |
127 | print >> sys.stderr, \ | |
128 | 'Error: File added in both, permissions conflict' | |
129 | __conflict(path) | |
130 | return 1 | |
131 | # 3-way merge | |
132 | else: | |
133 | merge_ok = os.system(merger % {'branch1': src1, | |
134 | 'ancestor': orig, | |
135 | 'branch2': src2, | |
136 | 'output': path }) == 0 | |
137 | ||
138 | if merge_ok: | |
139 | os.system('git-update-index -- %s' % path) | |
140 | __remove_files(orig_hash, file1_hash, file2_hash) | |
141 | return 0 | |
142 | else: | |
143 | print >> sys.stderr, \ | |
144 | 'Error: three-way merge tool failed for file "%s"' \ | |
145 | % path | |
146 | # reset the cache to the first branch | |
147 | os.system('git-update-index --cacheinfo %s %s %s' | |
148 | % (file1_mode, file1_hash, path)) | |
149 | if keeporig != 'yes': | |
150 | __remove_files(orig_hash, file1_hash, file2_hash) | |
151 | __conflict(path) | |
152 | return 1 | |
153 | # file deleted in both or deleted in one and unchanged in the other | |
154 | elif not (file1_hash or file2_hash) \ | |
155 | or file1_hash == orig_hash or file2_hash == orig_hash: | |
156 | if os.path.exists(path): | |
157 | os.remove(path) | |
158 | __remove_files(orig_hash, file1_hash, file2_hash) | |
159 | return os.system('git-update-index --remove -- %s' % path) | |
160 | # file deleted in one and changed in the other | |
161 | else: | |
162 | # Do something here - we must at least merge the entry in | |
163 | # the cache, instead of leaving it in U(nmerged) state. In | |
164 | # fact, stg resolved does not handle that. | |
165 | ||
166 | # Do the same thing cogito does - remove the file in any case. | |
167 | os.system('git-update-index --remove -- %s' % path) | |
168 | ||
169 | #if file1_hash: | |
170 | ## file deleted upstream and changed in the patch. The | |
171 | ## patch is probably going to move the changes | |
172 | ## elsewhere. | |
173 | ||
174 | #os.system('git-update-index --remove -- %s' % path) | |
175 | #else: | |
176 | ## file deleted in the patch and changed upstream. We | |
177 | ## could re-delete it, but for now leave it there - | |
178 | ## and let the user check if he still wants to remove | |
179 | ## the file. | |
180 | ||
181 | ## reset the cache to the first branch | |
182 | #os.system('git-update-index --cacheinfo %s %s %s' | |
183 | # % (file1_mode, file1_hash, path)) | |
184 | __conflict(path) | |
185 | return 1 | |
186 | ||
187 | # file does not exist in origin | |
188 | else: | |
189 | # file added in both | |
190 | if file1_hash and file2_hash: | |
191 | # files are the same | |
192 | if file1_hash == file2_hash: | |
193 | if os.system('git-update-index --add --cacheinfo %s %s %s' | |
194 | % (file1_mode, file1_hash, path)) != 0: | |
195 | print >> sys.stderr, 'Error: git-update-index failed' | |
196 | __conflict(path) | |
197 | return 1 | |
198 | if os.system('git-checkout-index -u -f -- %s' % path): | |
199 | print >> sys.stderr, 'Error: git-checkout-index failed' | |
200 | __conflict(path) | |
201 | return 1 | |
202 | if file1_mode != file2_mode: | |
203 | print >> sys.stderr, \ | |
204 | 'Error: File "s" added in both, ' \ | |
205 | 'permissions conflict' % path | |
206 | __conflict(path) | |
207 | return 1 | |
208 | # files are different | |
209 | else: | |
56df21ed CM |
210 | # reset the index to the current file |
211 | os.system('git-update-index -- %s' % path) | |
3659ef88 CM |
212 | print >> sys.stderr, \ |
213 | 'Error: File "%s" added in branches but different' % path | |
214 | __conflict(path) | |
215 | return 1 | |
216 | # file added in one | |
217 | elif file1_hash or file2_hash: | |
218 | if file1_hash: | |
219 | mode = file1_mode | |
220 | obj = file1_hash | |
221 | else: | |
222 | mode = file2_mode | |
223 | obj = file2_hash | |
224 | if os.system('git-update-index --add --cacheinfo %s %s %s' | |
225 | % (mode, obj, path)) != 0: | |
226 | print >> sys.stderr, 'Error: git-update-index failed' | |
227 | __conflict(path) | |
228 | return 1 | |
229 | __remove_files(orig_hash, file1_hash, file2_hash) | |
230 | return os.system('git-checkout-index -u -f -- %s' % path) | |
231 | ||
232 | # Unhandled case | |
233 | print >> sys.stderr, 'Error: Unhandled merge conflict: ' \ | |
234 | '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' \ | |
235 | % (orig_hash, file1_hash, file2_hash, | |
236 | path, | |
237 | orig_mode, file1_mode, file2_mode) | |
238 | __conflict(path) | |
239 | return 1 |