blob: 1d9e530a84758e34d616690684c4842cd1af4cfc [file] [log] [blame]
Junio C Hamanof345b0a2005-05-30 00:08:37 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Junio C Hamanof345b0a2005-05-30 00:08:37 -07007
Junio C Hamanoeeaa4602005-06-03 01:40:28 -07008static int should_break(struct diff_filespec *src,
9 struct diff_filespec *dst,
10 int break_score,
11 int *merge_score_p)
Junio C Hamanof345b0a2005-05-30 00:08:37 -070012{
13 /* dst is recorded as a modification of src. Are they so
14 * different that we are better off recording this as a pair
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070015 * of delete and create?
Junio C Hamanof345b0a2005-05-30 00:08:37 -070016 *
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070017 * 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.
Junio C Hamanof345b0a2005-05-30 00:08:37 -070047 */
Benjamin Kramereb3a9dd2009-03-07 21:02:10 +010048 unsigned long delta_size, max_size;
Linus Torvalds6dd4b662007-10-20 12:31:31 -070049 unsigned long src_copied, literal_added, src_removed;
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070050
51 *merge_score_p = 0; /* assume no deletion --- "do not break"
52 * is the default.
53 */
Junio C Hamanof345b0a2005-05-30 00:08:37 -070054
Junio C Hamanob45563a2007-11-30 22:22:38 -080055 if (S_ISREG(src->mode) != S_ISREG(dst->mode)) {
56 *merge_score_p = (int)MAX_SCORE;
57 return 1; /* even their types are different */
58 }
Junio C Hamanof345b0a2005-05-30 00:08:37 -070059
Junio C Hamanoaeecd232006-02-28 20:19:47 -080060 if (src->sha1_valid && dst->sha1_valid &&
David Rientjesa89fccd2006-08-17 11:54:57 -070061 !hashcmp(src->sha1, dst->sha1))
Junio C Hamanoaeecd232006-02-28 20:19:47 -080062 return 0; /* they are the same */
63
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070064 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
Junio C Hamanof345b0a2005-05-30 00:08:37 -070065 return 0; /* error but caught downstream */
66
Linus Torvalds6dd4b662007-10-20 12:31:31 -070067 max_size = ((src->size > dst->size) ? src->size : dst->size);
68 if (max_size < MINIMUM_BREAK_SIZE)
Junio C Hamano0532a5e2005-12-12 17:15:55 -080069 return 0; /* we do not break too small filepair */
Junio C Hamanof345b0a2005-05-30 00:08:37 -070070
John Keepinge7b00c52013-04-03 20:24:05 +010071 if (!src->size)
72 return 0; /* we do not let empty files get renamed */
73
Junio C Hamanod8c3d032007-06-28 22:54:37 -070074 if (diffcore_count_changes(src, dst,
Jeff King8282de92009-11-16 11:02:02 -050075 &src->cnt_data, &dst->cnt_data,
Junio C Hamano65416752006-02-28 16:01:36 -080076 0,
77 &src_copied, &literal_added))
78 return 0;
Junio C Hamano355e76a2005-06-03 01:36:03 -070079
Junio C Hamano4d0f39c2006-03-04 01:03:53 -080080 /* sanity */
81 if (src->size < src_copied)
82 src_copied = src->size;
83 if (dst->size < literal_added + src_copied) {
84 if (src_copied < dst->size)
85 literal_added = dst->size - src_copied;
86 else
87 literal_added = 0;
88 }
89 src_removed = src->size - src_copied;
90
Junio C Hamanoeeaa4602005-06-03 01:40:28 -070091 /* Compute merge-score, which is "how much is removed
92 * from the source material". The clean-up stage will
93 * merge the surviving pair together if the score is
94 * less than the minimum, after rename/copy runs.
95 */
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -050096 *merge_score_p = (int)(src_removed * MAX_SCORE / src->size);
Linus Torvalds6dd4b662007-10-20 12:31:31 -070097 if (*merge_score_p > break_score)
98 return 1;
Junio C Hamano4d0f39c2006-03-04 01:03:53 -080099
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700100 /* Extent of damage, which counts both inserts and
101 * deletes.
102 */
Junio C Hamano4d0f39c2006-03-04 01:03:53 -0800103 delta_size = src_removed + literal_added;
Linus Torvalds6dd4b662007-10-20 12:31:31 -0700104 if (delta_size * MAX_SCORE / max_size < break_score)
Junio C Hamano4d0f39c2006-03-04 01:03:53 -0800105 return 0;
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700106
Junio C Hamano4d0f39c2006-03-04 01:03:53 -0800107 /* If you removed a lot without adding new material, that is
108 * not really a rewrite.
109 */
110 if ((src->size * break_score < src_removed * MAX_SCORE) &&
111 (literal_added * 20 < src_removed) &&
112 (literal_added * 20 < src_copied))
113 return 0;
114
115 return 1;
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700116}
117
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700118void diffcore_break(int break_score)
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700119{
120 struct diff_queue_struct *q = &diff_queued_diff;
121 struct diff_queue_struct outq;
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700122
123 /* When the filepair has this much edit (insert and delete),
124 * it is first considered to be a rewrite and broken into a
125 * create and delete filepair. This is to help breaking a
126 * file that had too much new stuff added, possibly from
127 * moving contents from another file, so that rename/copy can
128 * match it with the other file.
129 *
130 * int break_score; we reuse incoming parameter for this.
131 */
132
133 /* After a pair is broken according to break_score and
134 * subjected to rename/copy, both of them may survive intact,
135 * due to lack of suitable rename/copy peer. Or, the caller
136 * may be calling us without using rename/copy. When that
137 * happens, we merge the broken pieces back into one
138 * modification together if the pair did not have more than
139 * this much delete. For this computation, we do not take
140 * insert into account at all. If you start from a 100-line
141 * file and delete 97 lines of it, it does not matter if you
142 * add 27 lines to it to make a new 30-line file or if you add
143 * 997 lines to it to make a 1000-line file. Either way what
144 * you did was a rewrite of 97%. On the other hand, if you
145 * delete 3 lines, keeping 97 lines intact, it does not matter
146 * if you add 3 lines to it to make a new 100-line file or if
147 * you add 903 lines to it to make a new 1000-line file.
148 * Either way you did a lot of additions and not a rewrite.
149 * This merge happens to catch the latter case. A merge_score
150 * of 80% would be a good default value (a broken pair that
151 * has score lower than merge_score will be merged back
152 * together).
153 */
154 int merge_score;
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700155 int i;
156
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700157 /* See comment on DEFAULT_BREAK_SCORE and
158 * DEFAULT_MERGE_SCORE in diffcore.h
159 */
160 merge_score = (break_score >> 16) & 0xFFFF;
161 break_score = (break_score & 0xFFFF);
162
163 if (!break_score)
164 break_score = DEFAULT_BREAK_SCORE;
165 if (!merge_score)
166 merge_score = DEFAULT_MERGE_SCORE;
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700167
Bo Yang9ca5df92010-05-06 21:52:27 -0700168 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700169
170 for (i = 0; i < q->nr; i++) {
171 struct diff_filepair *p = q->queue[i];
172 int score;
173
Junio C Hamanob45563a2007-11-30 22:22:38 -0800174 /*
175 * We deal only with in-place edit of blobs.
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700176 * We do not break anything else.
177 */
178 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
Junio C Hamanob45563a2007-11-30 22:22:38 -0800179 object_type(p->one->mode) == OBJ_BLOB &&
180 object_type(p->two->mode) == OBJ_BLOB &&
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700181 !strcmp(p->one->path, p->two->path)) {
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700182 if (should_break(p->one, p->two,
Junio C Hamano0532a5e2005-12-12 17:15:55 -0800183 break_score, &score)) {
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700184 /* Split this into delete and create */
185 struct diff_filespec *null_one, *null_two;
186 struct diff_filepair *dp;
187
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700188 /* Set score to 0 for the pair that
189 * needs to be merged back together
190 * should they survive rename/copy.
191 * Also we do not want to break very
192 * small files.
193 */
Junio C Hamanof78c79c2005-06-03 23:05:57 -0700194 if (score < merge_score)
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700195 score = 0;
196
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700197 /* deletion of one */
198 null_one = alloc_filespec(p->one->path);
199 dp = diff_queue(&outq, p->one, null_one);
200 dp->score = score;
201 dp->broken_pair = 1;
202
203 /* creation of two */
204 null_two = alloc_filespec(p->two->path);
205 dp = diff_queue(&outq, null_two, p->two);
206 dp->score = score;
207 dp->broken_pair = 1;
208
Jeff King8282de92009-11-16 11:02:02 -0500209 diff_free_filespec_blob(p->one);
210 diff_free_filespec_blob(p->two);
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700211 free(p); /* not diff_free_filepair(), we are
212 * reusing one and two here.
213 */
214 continue;
215 }
216 }
Jeff Kingf4f19fb2009-11-16 10:56:25 -0500217 diff_free_filespec_data(p->one);
218 diff_free_filespec_data(p->two);
Junio C Hamanof345b0a2005-05-30 00:08:37 -0700219 diff_q(&outq, p);
220 }
221 free(q->queue);
222 *q = outq;
223
224 return;
225}
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700226
227static void merge_broken(struct diff_filepair *p,
228 struct diff_filepair *pp,
229 struct diff_queue_struct *outq)
230{
231 /* p and pp are broken pairs we want to merge */
Junio C Hamano366175e2005-06-19 13:17:50 -0700232 struct diff_filepair *c = p, *d = pp, *dp;
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700233 if (DIFF_FILE_VALID(p->one)) {
234 /* this must be a delete half */
235 d = p; c = pp;
236 }
237 /* Sanity check */
238 if (!DIFF_FILE_VALID(d->one))
239 die("internal error in merge #1");
240 if (DIFF_FILE_VALID(d->two))
241 die("internal error in merge #2");
242 if (DIFF_FILE_VALID(c->one))
243 die("internal error in merge #3");
244 if (!DIFF_FILE_VALID(c->two))
245 die("internal error in merge #4");
246
Junio C Hamano366175e2005-06-19 13:17:50 -0700247 dp = diff_queue(outq, d->one, c->two);
248 dp->score = p->score;
Junio C Hamano19397b42005-09-14 14:06:50 -0700249 diff_free_filespec_data(d->two);
250 diff_free_filespec_data(c->one);
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700251 free(d);
252 free(c);
253}
254
255void diffcore_merge_broken(void)
256{
257 struct diff_queue_struct *q = &diff_queued_diff;
258 struct diff_queue_struct outq;
259 int i, j;
260
Bo Yang9ca5df92010-05-06 21:52:27 -0700261 DIFF_QUEUE_CLEAR(&outq);
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700262
263 for (i = 0; i < q->nr; i++) {
264 struct diff_filepair *p = q->queue[i];
265 if (!p)
266 /* we already merged this with its peer */
267 continue;
268 else if (p->broken_pair &&
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700269 !strcmp(p->one->path, p->two->path)) {
270 /* If the peer also survived rename/copy, then
271 * we merge them back together.
272 */
273 for (j = i + 1; j < q->nr; j++) {
274 struct diff_filepair *pp = q->queue[j];
275 if (pp->broken_pair &&
Junio C Hamanoeeaa4602005-06-03 01:40:28 -0700276 !strcmp(pp->one->path, pp->two->path) &&
277 !strcmp(p->one->path, pp->two->path)) {
278 /* Peer survived. Merge them */
279 merge_broken(p, pp, &outq);
280 q->queue[j] = NULL;
281 break;
282 }
283 }
284 if (q->nr <= j)
285 /* The peer did not survive, so we keep
286 * it in the output.
287 */
288 diff_q(&outq, p);
289 }
290 else
291 diff_q(&outq, p);
292 }
293 free(q->queue);
294 *q = outq;
295
296 return;
297}