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