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