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