]> git.ipfire.org Git - thirdparty/git.git/blame - diffcore-break.c
git-submodule.sh: shorten submodule SHA-1s using rev-parse
[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
b78ea5fc
NTND
8static int should_break(struct repository *r,
9 struct diff_filespec *src,
eeaa4603
JH
10 struct diff_filespec *dst,
11 int break_score,
12 int *merge_score_p)
f345b0a0
JH
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
eeaa4603 16 * of delete and create?
f345b0a0 17 *
eeaa4603
JH
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.
f345b0a0 48 */
eb3a9dd3 49 unsigned long delta_size, max_size;
6dd4b66f 50 unsigned long src_copied, literal_added, src_removed;
eeaa4603
JH
51
52 *merge_score_p = 0; /* assume no deletion --- "do not break"
53 * is the default.
54 */
f345b0a0 55
b45563a2
JH
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 }
f345b0a0 60
41c9560e 61 if (src->oid_valid && dst->oid_valid &&
4a7e27e9 62 oideq(&src->oid, &dst->oid))
aeecd23a
JH
63 return 0; /* they are the same */
64
b78ea5fc
NTND
65 if (diff_populate_filespec(r, src, 0) ||
66 diff_populate_filespec(r, dst, 0))
f345b0a0
JH
67 return 0; /* error but caught downstream */
68
6dd4b66f
LT
69 max_size = ((src->size > dst->size) ? src->size : dst->size);
70 if (max_size < MINIMUM_BREAK_SIZE)
0532a5e4 71 return 0; /* we do not break too small filepair */
f345b0a0 72
e7b00c57
JK
73 if (!src->size)
74 return 0; /* we do not let empty files get renamed */
75
b78ea5fc 76 if (diffcore_count_changes(r, src, dst,
8282de94 77 &src->cnt_data, &dst->cnt_data,
65416758
JH
78 &src_copied, &literal_added))
79 return 0;
355e76a4 80
4d0f39ce
JH
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
eeaa4603
JH
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 */
dc49cd76 97 *merge_score_p = (int)(src_removed * MAX_SCORE / src->size);
6dd4b66f
LT
98 if (*merge_score_p > break_score)
99 return 1;
4d0f39ce 100
eeaa4603
JH
101 /* Extent of damage, which counts both inserts and
102 * deletes.
103 */
4d0f39ce 104 delta_size = src_removed + literal_added;
6dd4b66f 105 if (delta_size * MAX_SCORE / max_size < break_score)
4d0f39ce
JH
106 return 0;
107
108 /* If you removed a lot without adding new material, that is
109 * not really a rewrite.
eeaa4603 110 */
4d0f39ce
JH
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;
f345b0a0 115
4d0f39ce 116 return 1;
f345b0a0
JH
117}
118
b78ea5fc 119void diffcore_break(struct repository *r, int break_score)
f345b0a0
JH
120{
121 struct diff_queue_struct *q = &diff_queued_diff;
122 struct diff_queue_struct outq;
eeaa4603
JH
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;
f345b0a0
JH
156 int i;
157
eeaa4603
JH
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;
f345b0a0 168
9ca5df90 169 DIFF_QUEUE_CLEAR(&outq);
f345b0a0
JH
170
171 for (i = 0; i < q->nr; i++) {
172 struct diff_filepair *p = q->queue[i];
173 int score;
174
b45563a2
JH
175 /*
176 * We deal only with in-place edit of blobs.
f345b0a0
JH
177 * We do not break anything else.
178 */
179 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
b45563a2
JH
180 object_type(p->one->mode) == OBJ_BLOB &&
181 object_type(p->two->mode) == OBJ_BLOB &&
f345b0a0 182 !strcmp(p->one->path, p->two->path)) {
b78ea5fc 183 if (should_break(r, p->one, p->two,
0532a5e4 184 break_score, &score)) {
f345b0a0
JH
185 /* Split this into delete and create */
186 struct diff_filespec *null_one, *null_two;
187 struct diff_filepair *dp;
188
eeaa4603
JH
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 */
f78c79c5 195 if (score < merge_score)
eeaa4603
JH
196 score = 0;
197
f345b0a0
JH
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
8282de94
JK
210 diff_free_filespec_blob(p->one);
211 diff_free_filespec_blob(p->two);
f345b0a0
JH
212 free(p); /* not diff_free_filepair(), we are
213 * reusing one and two here.
214 */
215 continue;
216 }
217 }
f4f19fb6
JK
218 diff_free_filespec_data(p->one);
219 diff_free_filespec_data(p->two);
f345b0a0
JH
220 diff_q(&outq, p);
221 }
222 free(q->queue);
223 *q = outq;
224
225 return;
226}
eeaa4603
JH
227
228static 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 */
366175ef 233 struct diff_filepair *c = p, *d = pp, *dp;
eeaa4603
JH
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
366175ef
JH
248 dp = diff_queue(outq, d->one, c->two);
249 dp->score = p->score;
6936b585
JH
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++;
19397b45
JH
257 diff_free_filespec_data(d->two);
258 diff_free_filespec_data(c->one);
eeaa4603
JH
259 free(d);
260 free(c);
261}
262
263void 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
9ca5df90 269 DIFF_QUEUE_CLEAR(&outq);
eeaa4603
JH
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 &&
eeaa4603
JH
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 &&
eeaa4603
JH
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 break;
290 }
291 }
292 if (q->nr <= j)
293 /* The peer did not survive, so we keep
294 * it in the output.
295 */
296 diff_q(&outq, p);
297 }
298 else
299 diff_q(&outq, p);
300 }
301 free(q->queue);
302 *q = outq;
303
304 return;
305}