Skip to content
Newer
Older
100644 291 lines (259 sloc) 8.58 KB
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
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
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
10 static int should_break(struct diff_filespec *src,
11 struct diff_filespec *dst,
12 int break_score,
13 int *merge_score_p)
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
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
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
17 * of delete and create?
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
18 *
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
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.
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
49 */
50 void *delta;
355e76a [PATCH] Tweak count-delta interface
Junio C Hamano authored
51 unsigned long delta_size, base_size, src_copied, literal_added;
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
52 int to_break = 0;
53
54 *merge_score_p = 0; /* assume no deletion --- "do not break"
55 * is the default.
56 */
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
57
58 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
59 return 0; /* leave symlink rename alone */
60
aeecd23 diffcore-break: micro-optimize by avoiding delta between identical fi…
Junio C Hamano authored
61 if (src->sha1_valid && dst->sha1_valid &&
62 !memcmp(src->sha1, dst->sha1, 20))
63 return 0; /* they are the same */
64
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
65 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
66 return 0; /* error but caught downstream */
67
f78c79c [PATCH] diffcore-break.c: various fixes.
Junio C Hamano authored
68 base_size = ((src->size < dst->size) ? src->size : dst->size);
0532a5e diffcore-break: do not break too small filepair.
Junio C Hamano authored
69 if (base_size < MINIMUM_BREAK_SIZE)
70 return 0; /* we do not break too small filepair */
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
71
72 delta = diff_delta(src->data, src->size,
73 dst->data, dst->size,
3c84974 [PATCH] Fixlets on top of Nico's clean-up.
Junio C Hamano authored
74 &delta_size, 0);
d28c8af diffcore-break.c: check diff_delta() return value.
Junio C Hamano authored
75 if (!delta)
76 return 0; /* error but caught downstream */
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
77
78 /* Estimate the edit size by interpreting delta. */
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
79 if (count_delta(delta, delta_size,
80 &src_copied, &literal_added)) {
355e76a [PATCH] Tweak count-delta interface
Junio C Hamano authored
81 free(delta);
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
82 return 0; /* we cannot tell */
355e76a [PATCH] Tweak count-delta interface
Junio C Hamano authored
83 }
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
84 free(delta);
355e76a [PATCH] Tweak count-delta interface
Junio C Hamano authored
85
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
86 /* Compute merge-score, which is "how much is removed
87 * from the source material". The clean-up stage will
88 * merge the surviving pair together if the score is
89 * less than the minimum, after rename/copy runs.
90 */
91 if (src->size <= src_copied)
f78c79c [PATCH] diffcore-break.c: various fixes.
Junio C Hamano authored
92 ; /* all copied, nothing removed */
93 else {
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
94 delta_size = src->size - src_copied;
f78c79c [PATCH] diffcore-break.c: various fixes.
Junio C Hamano authored
95 *merge_score_p = delta_size * MAX_SCORE / src->size;
96 }
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
97
98 /* Extent of damage, which counts both inserts and
99 * deletes.
100 */
101 if (src->size + literal_added <= src_copied)
102 delta_size = 0; /* avoid wrapping around */
355e76a [PATCH] Tweak count-delta interface
Junio C Hamano authored
103 else
104 delta_size = (src->size - src_copied) + literal_added;
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
105
106 /* We break if the edit exceeds the minimum.
107 * i.e. (break_score / MAX_SCORE < delta_size / base_size)
108 */
109 if (break_score * base_size < delta_size * MAX_SCORE)
110 to_break = 1;
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
111
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
112 return to_break;
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
113 }
114
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
115 void diffcore_break(int break_score)
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
116 {
117 struct diff_queue_struct *q = &diff_queued_diff;
118 struct diff_queue_struct outq;
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
119
120 /* When the filepair has this much edit (insert and delete),
121 * it is first considered to be a rewrite and broken into a
122 * create and delete filepair. This is to help breaking a
123 * file that had too much new stuff added, possibly from
124 * moving contents from another file, so that rename/copy can
125 * match it with the other file.
126 *
127 * int break_score; we reuse incoming parameter for this.
128 */
129
130 /* After a pair is broken according to break_score and
131 * subjected to rename/copy, both of them may survive intact,
132 * due to lack of suitable rename/copy peer. Or, the caller
133 * may be calling us without using rename/copy. When that
134 * happens, we merge the broken pieces back into one
135 * modification together if the pair did not have more than
136 * this much delete. For this computation, we do not take
137 * insert into account at all. If you start from a 100-line
138 * file and delete 97 lines of it, it does not matter if you
139 * add 27 lines to it to make a new 30-line file or if you add
140 * 997 lines to it to make a 1000-line file. Either way what
141 * you did was a rewrite of 97%. On the other hand, if you
142 * delete 3 lines, keeping 97 lines intact, it does not matter
143 * if you add 3 lines to it to make a new 100-line file or if
144 * you add 903 lines to it to make a new 1000-line file.
145 * Either way you did a lot of additions and not a rewrite.
146 * This merge happens to catch the latter case. A merge_score
147 * of 80% would be a good default value (a broken pair that
148 * has score lower than merge_score will be merged back
149 * together).
150 */
151 int merge_score;
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
152 int i;
153
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
154 /* See comment on DEFAULT_BREAK_SCORE and
155 * DEFAULT_MERGE_SCORE in diffcore.h
156 */
157 merge_score = (break_score >> 16) & 0xFFFF;
158 break_score = (break_score & 0xFFFF);
159
160 if (!break_score)
161 break_score = DEFAULT_BREAK_SCORE;
162 if (!merge_score)
163 merge_score = DEFAULT_MERGE_SCORE;
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
164
165 outq.nr = outq.alloc = 0;
166 outq.queue = NULL;
167
168 for (i = 0; i < q->nr; i++) {
169 struct diff_filepair *p = q->queue[i];
170 int score;
171
172 /* We deal only with in-place edit of non directory.
173 * We do not break anything else.
174 */
175 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
176 !S_ISDIR(p->one->mode) && !S_ISDIR(p->two->mode) &&
177 !strcmp(p->one->path, p->two->path)) {
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
178 if (should_break(p->one, p->two,
0532a5e diffcore-break: do not break too small filepair.
Junio C Hamano authored
179 break_score, &score)) {
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
180 /* Split this into delete and create */
181 struct diff_filespec *null_one, *null_two;
182 struct diff_filepair *dp;
183
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
184 /* Set score to 0 for the pair that
185 * needs to be merged back together
186 * should they survive rename/copy.
187 * Also we do not want to break very
188 * small files.
189 */
f78c79c [PATCH] diffcore-break.c: various fixes.
Junio C Hamano authored
190 if (score < merge_score)
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
191 score = 0;
192
f345b0a [PATCH] Add -B flag to diff-* brothers.
Junio C Hamano authored
193 /* deletion of one */
194 null_one = alloc_filespec(p->one->path);
195 dp = diff_queue(&outq, p->one, null_one);
196 dp->score = score;
197 dp->broken_pair = 1;
198
199 /* creation of two */
200 null_two = alloc_filespec(p->two->path);
201 dp = diff_queue(&outq, null_two, p->two);
202 dp->score = score;
203 dp->broken_pair = 1;
204
205 free(p); /* not diff_free_filepair(), we are
206 * reusing one and two here.
207 */
208 continue;
209 }
210 }
211 diff_q(&outq, p);
212 }
213 free(q->queue);
214 *q = outq;
215
216 return;
217 }
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
218
219 static void merge_broken(struct diff_filepair *p,
220 struct diff_filepair *pp,
221 struct diff_queue_struct *outq)
222 {
223 /* p and pp are broken pairs we want to merge */
366175e [PATCH] Rework -B output.
Junio C Hamano authored
224 struct diff_filepair *c = p, *d = pp, *dp;
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
225 if (DIFF_FILE_VALID(p->one)) {
226 /* this must be a delete half */
227 d = p; c = pp;
228 }
229 /* Sanity check */
230 if (!DIFF_FILE_VALID(d->one))
231 die("internal error in merge #1");
232 if (DIFF_FILE_VALID(d->two))
233 die("internal error in merge #2");
234 if (DIFF_FILE_VALID(c->one))
235 die("internal error in merge #3");
236 if (!DIFF_FILE_VALID(c->two))
237 die("internal error in merge #4");
238
366175e [PATCH] Rework -B output.
Junio C Hamano authored
239 dp = diff_queue(outq, d->one, c->two);
240 dp->score = p->score;
19397b4 Revert "[PATCH] plug memory leak in diff.c::diff_free_filepair()"
Junio C Hamano authored
241 diff_free_filespec_data(d->two);
242 diff_free_filespec_data(c->one);
eeaa460 [PATCH] diff: Update -B heuristics.
Junio C Hamano authored
243 free(d);
244 free(c);
245 }
246
247 void diffcore_merge_broken(void)
248 {
249 struct diff_queue_struct *q = &diff_queued_diff;
250 struct diff_queue_struct outq;
251 int i, j;
252
253 outq.nr = outq.alloc = 0;
254 outq.queue = NULL;
255
256 for (i = 0; i < q->nr; i++) {
257 struct diff_filepair *p = q->queue[i];
258 if (!p)
259 /* we already merged this with its peer */
260 continue;
261 else if (p->broken_pair &&
262 !strcmp(p->one->path, p->two->path)) {
263 /* If the peer also survived rename/copy, then
264 * we merge them back together.
265 */
266 for (j = i + 1; j < q->nr; j++) {
267 struct diff_filepair *pp = q->queue[j];
268 if (pp->broken_pair &&
269 !strcmp(pp->one->path, pp->two->path) &&
270 !strcmp(p->one->path, pp->two->path)) {
271 /* Peer survived. Merge them */
272 merge_broken(p, pp, &outq);
273 q->queue[j] = NULL;
274 break;
275 }
276 }
277 if (q->nr <= j)
278 /* The peer did not survive, so we keep
279 * it in the output.
280 */
281 diff_q(&outq, p);
282 }
283 else
284 diff_q(&outq, p);
285 }
286 free(q->queue);
287 *q = outq;
288
289 return;
290 }
Something went wrong with that request. Please try again.