]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "diff.h" | |
6 | #include "diffcore.h" | |
7 | ||
8 | static int should_break(struct repository *r, | |
9 | struct diff_filespec *src, | |
10 | struct diff_filespec *dst, | |
11 | int break_score, | |
12 | int *merge_score_p) | |
13 | { | |
14 | /* dst is recorded as a modification of src. Are they so | |
15 | * different that we are better off recording this as a pair | |
16 | * of delete and create? | |
17 | * | |
18 | * There are two criteria used in this algorithm. For the | |
19 | * purposes of helping later rename/copy, we take both delete | |
20 | * and insert into account and estimate the amount of "edit". | |
21 | * If the edit is very large, we break this pair so that | |
22 | * rename/copy can pick the pieces up to match with other | |
23 | * files. | |
24 | * | |
25 | * On the other hand, we would want to ignore inserts for the | |
26 | * pure "complete rewrite" detection. As long as most of the | |
27 | * existing contents were removed from the file, it is a | |
28 | * complete rewrite, and if sizable chunk from the original | |
29 | * still remains in the result, it is not a rewrite. It does | |
30 | * not matter how much or how little new material is added to | |
31 | * the file. | |
32 | * | |
33 | * The score we leave for such a broken filepair uses the | |
34 | * latter definition so that later clean-up stage can find the | |
35 | * pieces that should not have been broken according to the | |
36 | * latter definition after rename/copy runs, and merge the | |
37 | * broken pair that have a score lower than given criteria | |
38 | * back together. The break operation itself happens | |
39 | * according to the former definition. | |
40 | * | |
41 | * The minimum_edit parameter tells us when to break (the | |
42 | * amount of "edit" required for us to consider breaking the | |
43 | * pair). We leave the amount of deletion in *merge_score_p | |
44 | * when we return. | |
45 | * | |
46 | * The value we return is 1 if we want the pair to be broken, | |
47 | * or 0 if we do not. | |
48 | */ | |
49 | unsigned long delta_size, max_size; | |
50 | unsigned long src_copied, literal_added, src_removed; | |
51 | ||
52 | *merge_score_p = 0; /* assume no deletion --- "do not break" | |
53 | * is the default. | |
54 | */ | |
55 | ||
56 | if (S_ISREG(src->mode) != S_ISREG(dst->mode)) { | |
57 | *merge_score_p = (int)MAX_SCORE; | |
58 | return 1; /* even their types are different */ | |
59 | } | |
60 | ||
61 | if (src->oid_valid && dst->oid_valid && | |
62 | oideq(&src->oid, &dst->oid)) | |
63 | return 0; /* they are the same */ | |
64 | ||
65 | if (diff_populate_filespec(r, src, 0) || | |
66 | diff_populate_filespec(r, dst, 0)) | |
67 | return 0; /* error but caught downstream */ | |
68 | ||
69 | max_size = ((src->size > dst->size) ? src->size : dst->size); | |
70 | if (max_size < MINIMUM_BREAK_SIZE) | |
71 | return 0; /* we do not break too small filepair */ | |
72 | ||
73 | if (!src->size) | |
74 | return 0; /* we do not let empty files get renamed */ | |
75 | ||
76 | if (diffcore_count_changes(r, src, dst, | |
77 | &src->cnt_data, &dst->cnt_data, | |
78 | &src_copied, &literal_added)) | |
79 | return 0; | |
80 | ||
81 | /* sanity */ | |
82 | if (src->size < src_copied) | |
83 | src_copied = src->size; | |
84 | if (dst->size < literal_added + src_copied) { | |
85 | if (src_copied < dst->size) | |
86 | literal_added = dst->size - src_copied; | |
87 | else | |
88 | literal_added = 0; | |
89 | } | |
90 | src_removed = src->size - src_copied; | |
91 | ||
92 | /* Compute merge-score, which is "how much is removed | |
93 | * from the source material". The clean-up stage will | |
94 | * merge the surviving pair together if the score is | |
95 | * less than the minimum, after rename/copy runs. | |
96 | */ | |
97 | *merge_score_p = (int)(src_removed * MAX_SCORE / src->size); | |
98 | if (*merge_score_p > break_score) | |
99 | return 1; | |
100 | ||
101 | /* Extent of damage, which counts both inserts and | |
102 | * deletes. | |
103 | */ | |
104 | delta_size = src_removed + literal_added; | |
105 | if (delta_size * MAX_SCORE / max_size < break_score) | |
106 | return 0; | |
107 | ||
108 | /* If you removed a lot without adding new material, that is | |
109 | * not really a rewrite. | |
110 | */ | |
111 | if ((src->size * break_score < src_removed * MAX_SCORE) && | |
112 | (literal_added * 20 < src_removed) && | |
113 | (literal_added * 20 < src_copied)) | |
114 | return 0; | |
115 | ||
116 | return 1; | |
117 | } | |
118 | ||
119 | void diffcore_break(struct repository *r, int break_score) | |
120 | { | |
121 | struct diff_queue_struct *q = &diff_queued_diff; | |
122 | struct diff_queue_struct outq; | |
123 | ||
124 | /* When the filepair has this much edit (insert and delete), | |
125 | * it is first considered to be a rewrite and broken into a | |
126 | * create and delete filepair. This is to help breaking a | |
127 | * file that had too much new stuff added, possibly from | |
128 | * moving contents from another file, so that rename/copy can | |
129 | * match it with the other file. | |
130 | * | |
131 | * int break_score; we reuse incoming parameter for this. | |
132 | */ | |
133 | ||
134 | /* After a pair is broken according to break_score and | |
135 | * subjected to rename/copy, both of them may survive intact, | |
136 | * due to lack of suitable rename/copy peer. Or, the caller | |
137 | * may be calling us without using rename/copy. When that | |
138 | * happens, we merge the broken pieces back into one | |
139 | * modification together if the pair did not have more than | |
140 | * this much delete. For this computation, we do not take | |
141 | * insert into account at all. If you start from a 100-line | |
142 | * file and delete 97 lines of it, it does not matter if you | |
143 | * add 27 lines to it to make a new 30-line file or if you add | |
144 | * 997 lines to it to make a 1000-line file. Either way what | |
145 | * you did was a rewrite of 97%. On the other hand, if you | |
146 | * delete 3 lines, keeping 97 lines intact, it does not matter | |
147 | * if you add 3 lines to it to make a new 100-line file or if | |
148 | * you add 903 lines to it to make a new 1000-line file. | |
149 | * Either way you did a lot of additions and not a rewrite. | |
150 | * This merge happens to catch the latter case. A merge_score | |
151 | * of 80% would be a good default value (a broken pair that | |
152 | * has score lower than merge_score will be merged back | |
153 | * together). | |
154 | */ | |
155 | int merge_score; | |
156 | int i; | |
157 | ||
158 | /* See comment on DEFAULT_BREAK_SCORE and | |
159 | * DEFAULT_MERGE_SCORE in diffcore.h | |
160 | */ | |
161 | merge_score = (break_score >> 16) & 0xFFFF; | |
162 | break_score = (break_score & 0xFFFF); | |
163 | ||
164 | if (!break_score) | |
165 | break_score = DEFAULT_BREAK_SCORE; | |
166 | if (!merge_score) | |
167 | merge_score = DEFAULT_MERGE_SCORE; | |
168 | ||
169 | DIFF_QUEUE_CLEAR(&outq); | |
170 | ||
171 | for (i = 0; i < q->nr; i++) { | |
172 | struct diff_filepair *p = q->queue[i]; | |
173 | int score; | |
174 | ||
175 | /* | |
176 | * We deal only with in-place edit of blobs. | |
177 | * We do not break anything else. | |
178 | */ | |
179 | if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) && | |
180 | object_type(p->one->mode) == OBJ_BLOB && | |
181 | object_type(p->two->mode) == OBJ_BLOB && | |
182 | !strcmp(p->one->path, p->two->path)) { | |
183 | if (should_break(r, p->one, p->two, | |
184 | break_score, &score)) { | |
185 | /* Split this into delete and create */ | |
186 | struct diff_filespec *null_one, *null_two; | |
187 | struct diff_filepair *dp; | |
188 | ||
189 | /* Set score to 0 for the pair that | |
190 | * needs to be merged back together | |
191 | * should they survive rename/copy. | |
192 | * Also we do not want to break very | |
193 | * small files. | |
194 | */ | |
195 | if (score < merge_score) | |
196 | score = 0; | |
197 | ||
198 | /* deletion of one */ | |
199 | null_one = alloc_filespec(p->one->path); | |
200 | dp = diff_queue(&outq, p->one, null_one); | |
201 | dp->score = score; | |
202 | dp->broken_pair = 1; | |
203 | ||
204 | /* creation of two */ | |
205 | null_two = alloc_filespec(p->two->path); | |
206 | dp = diff_queue(&outq, null_two, p->two); | |
207 | dp->score = score; | |
208 | dp->broken_pair = 1; | |
209 | ||
210 | diff_free_filespec_blob(p->one); | |
211 | diff_free_filespec_blob(p->two); | |
212 | free(p); /* not diff_free_filepair(), we are | |
213 | * reusing one and two here. | |
214 | */ | |
215 | continue; | |
216 | } | |
217 | } | |
218 | diff_free_filespec_data(p->one); | |
219 | diff_free_filespec_data(p->two); | |
220 | diff_q(&outq, p); | |
221 | } | |
222 | free(q->queue); | |
223 | *q = outq; | |
224 | ||
225 | return; | |
226 | } | |
227 | ||
228 | static void merge_broken(struct diff_filepair *p, | |
229 | struct diff_filepair *pp, | |
230 | struct diff_queue_struct *outq) | |
231 | { | |
232 | /* p and pp are broken pairs we want to merge */ | |
233 | struct diff_filepair *c = p, *d = pp, *dp; | |
234 | if (DIFF_FILE_VALID(p->one)) { | |
235 | /* this must be a delete half */ | |
236 | d = p; c = pp; | |
237 | } | |
238 | /* Sanity check */ | |
239 | if (!DIFF_FILE_VALID(d->one)) | |
240 | die("internal error in merge #1"); | |
241 | if (DIFF_FILE_VALID(d->two)) | |
242 | die("internal error in merge #2"); | |
243 | if (DIFF_FILE_VALID(c->one)) | |
244 | die("internal error in merge #3"); | |
245 | if (!DIFF_FILE_VALID(c->two)) | |
246 | die("internal error in merge #4"); | |
247 | ||
248 | dp = diff_queue(outq, d->one, c->two); | |
249 | dp->score = p->score; | |
250 | /* | |
251 | * We will be one extra user of the same src side of the | |
252 | * broken pair, if it was used as the rename source for other | |
253 | * paths elsewhere. Increment to mark that the path stays | |
254 | * in the resulting tree. | |
255 | */ | |
256 | d->one->rename_used++; | |
257 | diff_free_filespec_data(d->two); | |
258 | diff_free_filespec_data(c->one); | |
259 | free(d); | |
260 | free(c); | |
261 | } | |
262 | ||
263 | void diffcore_merge_broken(void) | |
264 | { | |
265 | struct diff_queue_struct *q = &diff_queued_diff; | |
266 | struct diff_queue_struct outq; | |
267 | int i, j; | |
268 | ||
269 | DIFF_QUEUE_CLEAR(&outq); | |
270 | ||
271 | for (i = 0; i < q->nr; i++) { | |
272 | struct diff_filepair *p = q->queue[i]; | |
273 | if (!p) | |
274 | /* we already merged this with its peer */ | |
275 | continue; | |
276 | else if (p->broken_pair && | |
277 | !strcmp(p->one->path, p->two->path)) { | |
278 | /* If the peer also survived rename/copy, then | |
279 | * we merge them back together. | |
280 | */ | |
281 | for (j = i + 1; j < q->nr; j++) { | |
282 | struct diff_filepair *pp = q->queue[j]; | |
283 | if (pp->broken_pair && | |
284 | !strcmp(pp->one->path, pp->two->path) && | |
285 | !strcmp(p->one->path, pp->two->path)) { | |
286 | /* Peer survived. Merge them */ | |
287 | merge_broken(p, pp, &outq); | |
288 | q->queue[j] = NULL; | |
289 | goto next; | |
290 | } | |
291 | } | |
292 | /* The peer did not survive, so we keep | |
293 | * it in the output. | |
294 | */ | |
295 | diff_q(&outq, p); | |
296 | } | |
297 | else | |
298 | diff_q(&outq, p); | |
299 | next:; | |
300 | } | |
301 | free(q->queue); | |
302 | *q = outq; | |
303 | ||
304 | return; | |
305 | } |