diff.c: replace a 'strdup' with 'xstrdup'.
[alt-git.git] / diffcore-break.c
blob31cdcfe8bcdae7df65b0387071846299a14bb7be
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
8 static int should_break(struct diff_filespec *src,
9 struct diff_filespec *dst,
10 int break_score,
11 int *merge_score_p)
13 /* dst is recorded as a modification of src. Are they so
14 * different that we are better off recording this as a pair
15 * of delete and create?
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.
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.
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.
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.
45 * The value we return is 1 if we want the pair to be broken,
46 * or 0 if we do not.
48 unsigned long delta_size, base_size, max_size;
49 unsigned long src_copied, literal_added, src_removed;
51 *merge_score_p = 0; /* assume no deletion --- "do not break"
52 * is the default.
55 if (S_ISREG(src->mode) != S_ISREG(dst->mode)) {
56 *merge_score_p = (int)MAX_SCORE;
57 return 1; /* even their types are different */
60 if (src->sha1_valid && dst->sha1_valid &&
61 !hashcmp(src->sha1, dst->sha1))
62 return 0; /* they are the same */
64 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
65 return 0; /* error but caught downstream */
67 base_size = ((src->size < dst->size) ? src->size : dst->size);
68 max_size = ((src->size > dst->size) ? src->size : dst->size);
69 if (max_size < MINIMUM_BREAK_SIZE)
70 return 0; /* we do not break too small filepair */
72 if (diffcore_count_changes(src, dst,
73 NULL, NULL,
75 &src_copied, &literal_added))
76 return 0;
78 /* sanity */
79 if (src->size < src_copied)
80 src_copied = src->size;
81 if (dst->size < literal_added + src_copied) {
82 if (src_copied < dst->size)
83 literal_added = dst->size - src_copied;
84 else
85 literal_added = 0;
87 src_removed = src->size - src_copied;
89 /* Compute merge-score, which is "how much is removed
90 * from the source material". The clean-up stage will
91 * merge the surviving pair together if the score is
92 * less than the minimum, after rename/copy runs.
94 *merge_score_p = (int)(src_removed * MAX_SCORE / src->size);
95 if (*merge_score_p > break_score)
96 return 1;
98 /* Extent of damage, which counts both inserts and
99 * deletes.
101 delta_size = src_removed + literal_added;
102 if (delta_size * MAX_SCORE / max_size < break_score)
103 return 0;
105 /* If you removed a lot without adding new material, that is
106 * not really a rewrite.
108 if ((src->size * break_score < src_removed * MAX_SCORE) &&
109 (literal_added * 20 < src_removed) &&
110 (literal_added * 20 < src_copied))
111 return 0;
113 return 1;
116 void diffcore_break(int break_score)
118 struct diff_queue_struct *q = &diff_queued_diff;
119 struct diff_queue_struct outq;
121 /* When the filepair has this much edit (insert and delete),
122 * it is first considered to be a rewrite and broken into a
123 * create and delete filepair. This is to help breaking a
124 * file that had too much new stuff added, possibly from
125 * moving contents from another file, so that rename/copy can
126 * match it with the other file.
128 * int break_score; we reuse incoming parameter for this.
131 /* After a pair is broken according to break_score and
132 * subjected to rename/copy, both of them may survive intact,
133 * due to lack of suitable rename/copy peer. Or, the caller
134 * may be calling us without using rename/copy. When that
135 * happens, we merge the broken pieces back into one
136 * modification together if the pair did not have more than
137 * this much delete. For this computation, we do not take
138 * insert into account at all. If you start from a 100-line
139 * file and delete 97 lines of it, it does not matter if you
140 * add 27 lines to it to make a new 30-line file or if you add
141 * 997 lines to it to make a 1000-line file. Either way what
142 * you did was a rewrite of 97%. On the other hand, if you
143 * delete 3 lines, keeping 97 lines intact, it does not matter
144 * if you add 3 lines to it to make a new 100-line file or if
145 * you add 903 lines to it to make a new 1000-line file.
146 * Either way you did a lot of additions and not a rewrite.
147 * This merge happens to catch the latter case. A merge_score
148 * of 80% would be a good default value (a broken pair that
149 * has score lower than merge_score will be merged back
150 * together).
152 int merge_score;
153 int i;
155 /* See comment on DEFAULT_BREAK_SCORE and
156 * DEFAULT_MERGE_SCORE in diffcore.h
158 merge_score = (break_score >> 16) & 0xFFFF;
159 break_score = (break_score & 0xFFFF);
161 if (!break_score)
162 break_score = DEFAULT_BREAK_SCORE;
163 if (!merge_score)
164 merge_score = DEFAULT_MERGE_SCORE;
166 outq.nr = outq.alloc = 0;
167 outq.queue = NULL;
169 for (i = 0; i < q->nr; i++) {
170 struct diff_filepair *p = q->queue[i];
171 int score;
174 * We deal only with in-place edit of blobs.
175 * We do not break anything else.
177 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
178 object_type(p->one->mode) == OBJ_BLOB &&
179 object_type(p->two->mode) == OBJ_BLOB &&
180 !strcmp(p->one->path, p->two->path)) {
181 if (should_break(p->one, p->two,
182 break_score, &score)) {
183 /* Split this into delete and create */
184 struct diff_filespec *null_one, *null_two;
185 struct diff_filepair *dp;
187 /* Set score to 0 for the pair that
188 * needs to be merged back together
189 * should they survive rename/copy.
190 * Also we do not want to break very
191 * small files.
193 if (score < merge_score)
194 score = 0;
196 /* deletion of one */
197 null_one = alloc_filespec(p->one->path);
198 dp = diff_queue(&outq, p->one, null_one);
199 dp->score = score;
200 dp->broken_pair = 1;
202 /* creation of two */
203 null_two = alloc_filespec(p->two->path);
204 dp = diff_queue(&outq, null_two, p->two);
205 dp->score = score;
206 dp->broken_pair = 1;
208 free(p); /* not diff_free_filepair(), we are
209 * reusing one and two here.
211 continue;
214 diff_q(&outq, p);
216 free(q->queue);
217 *q = outq;
219 return;
222 static void merge_broken(struct diff_filepair *p,
223 struct diff_filepair *pp,
224 struct diff_queue_struct *outq)
226 /* p and pp are broken pairs we want to merge */
227 struct diff_filepair *c = p, *d = pp, *dp;
228 if (DIFF_FILE_VALID(p->one)) {
229 /* this must be a delete half */
230 d = p; c = pp;
232 /* Sanity check */
233 if (!DIFF_FILE_VALID(d->one))
234 die("internal error in merge #1");
235 if (DIFF_FILE_VALID(d->two))
236 die("internal error in merge #2");
237 if (DIFF_FILE_VALID(c->one))
238 die("internal error in merge #3");
239 if (!DIFF_FILE_VALID(c->two))
240 die("internal error in merge #4");
242 dp = diff_queue(outq, d->one, c->two);
243 dp->score = p->score;
244 diff_free_filespec_data(d->two);
245 diff_free_filespec_data(c->one);
246 free(d);
247 free(c);
250 void diffcore_merge_broken(void)
252 struct diff_queue_struct *q = &diff_queued_diff;
253 struct diff_queue_struct outq;
254 int i, j;
256 outq.nr = outq.alloc = 0;
257 outq.queue = NULL;
259 for (i = 0; i < q->nr; i++) {
260 struct diff_filepair *p = q->queue[i];
261 if (!p)
262 /* we already merged this with its peer */
263 continue;
264 else if (p->broken_pair &&
265 !strcmp(p->one->path, p->two->path)) {
266 /* If the peer also survived rename/copy, then
267 * we merge them back together.
269 for (j = i + 1; j < q->nr; j++) {
270 struct diff_filepair *pp = q->queue[j];
271 if (pp->broken_pair &&
272 !strcmp(pp->one->path, pp->two->path) &&
273 !strcmp(p->one->path, pp->two->path)) {
274 /* Peer survived. Merge them */
275 merge_broken(p, pp, &outq);
276 q->queue[j] = NULL;
277 break;
280 if (q->nr <= j)
281 /* The peer did not survive, so we keep
282 * it in the output.
284 diff_q(&outq, p);
286 else
287 diff_q(&outq, p);
289 free(q->queue);
290 *q = outq;
292 return;