date: make DATE_MODE thread-safe
[git.git] / diffcore-break.c
blob49ba38aa7c0a58433380e560f6d84e0cc084a67f
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "git-compat-util.h"
5 #include "diffcore.h"
6 #include "hash.h"
7 #include "object.h"
8 #include "promisor-remote.h"
10 static int should_break(struct repository *r,
11 struct diff_filespec *src,
12 struct diff_filespec *dst,
13 int break_score,
14 int *merge_score_p)
16 /* dst is recorded as a modification of src. Are they so
17 * different that we are better off recording this as a pair
18 * of delete and create?
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.
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.
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.
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.
48 * The value we return is 1 if we want the pair to be broken,
49 * or 0 if we do not.
51 unsigned long delta_size, max_size;
52 unsigned long src_copied, literal_added, src_removed;
54 struct diff_populate_filespec_options options = { 0 };
56 *merge_score_p = 0; /* assume no deletion --- "do not break"
57 * is the default.
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 */
65 if (src->oid_valid && dst->oid_valid &&
66 oideq(&src->oid, &dst->oid))
67 return 0; /* they are the same */
69 if (r == the_repository && repo_has_promisor_remote(the_repository)) {
70 options.missing_object_cb = diff_queued_diff_prefetch;
71 options.missing_object_data = r;
74 if (diff_populate_filespec(r, src, &options) ||
75 diff_populate_filespec(r, dst, &options))
76 return 0; /* error but caught downstream */
78 max_size = ((src->size > dst->size) ? src->size : dst->size);
79 if (max_size < MINIMUM_BREAK_SIZE)
80 return 0; /* we do not break too small filepair */
82 if (!src->size)
83 return 0; /* we do not let empty files get renamed */
85 if (diffcore_count_changes(r, src, dst,
86 &src->cnt_data, &dst->cnt_data,
87 &src_copied, &literal_added))
88 return 0;
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;
99 src_removed = src->size - src_copied;
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.
106 *merge_score_p = (int)(src_removed * MAX_SCORE / src->size);
107 if (*merge_score_p > break_score)
108 return 1;
110 /* Extent of damage, which counts both inserts and
111 * deletes.
113 delta_size = src_removed + literal_added;
114 if (delta_size * MAX_SCORE / max_size < break_score)
115 return 0;
117 /* If you removed a lot without adding new material, that is
118 * not really a rewrite.
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;
125 return 1;
128 void diffcore_break(struct repository *r, int break_score)
130 struct diff_queue_struct *q = &diff_queued_diff;
131 struct diff_queue_struct outq;
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.
140 * int break_score; we reuse incoming parameter for this.
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).
164 int merge_score;
165 int i;
167 /* See comment on DEFAULT_BREAK_SCORE and
168 * DEFAULT_MERGE_SCORE in diffcore.h
170 merge_score = (break_score >> 16) & 0xFFFF;
171 break_score = (break_score & 0xFFFF);
173 if (!break_score)
174 break_score = DEFAULT_BREAK_SCORE;
175 if (!merge_score)
176 merge_score = DEFAULT_MERGE_SCORE;
178 DIFF_QUEUE_CLEAR(&outq);
180 for (i = 0; i < q->nr; i++) {
181 struct diff_filepair *p = q->queue[i];
182 int score;
185 * We deal only with in-place edit of blobs.
186 * We do not break anything else.
188 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
189 object_type(p->one->mode) == OBJ_BLOB &&
190 object_type(p->two->mode) == OBJ_BLOB &&
191 !strcmp(p->one->path, p->two->path)) {
192 if (should_break(r, p->one, p->two,
193 break_score, &score)) {
194 /* Split this into delete and create */
195 struct diff_filespec *null_one, *null_two;
196 struct diff_filepair *dp;
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.
204 if (score < merge_score)
205 score = 0;
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;
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;
219 diff_free_filespec_blob(p->one);
220 diff_free_filespec_blob(p->two);
221 free(p); /* not diff_free_filepair(), we are
222 * reusing one and two here.
224 continue;
227 diff_free_filespec_data(p->one);
228 diff_free_filespec_data(p->two);
229 diff_q(&outq, p);
231 free(q->queue);
232 *q = outq;
234 return;
237 static void merge_broken(struct diff_filepair *p,
238 struct diff_filepair *pp,
239 struct diff_queue_struct *outq)
241 /* p and pp are broken pairs we want to merge */
242 struct diff_filepair *c = p, *d = pp, *dp;
243 if (DIFF_FILE_VALID(p->one)) {
244 /* this must be a delete half */
245 d = p; c = pp;
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");
257 dp = diff_queue(outq, d->one, c->two);
258 dp->score = p->score;
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.
265 d->one->rename_used++;
266 diff_free_filespec_data(d->two);
267 diff_free_filespec_data(c->one);
268 free(d);
269 free(c);
272 void diffcore_merge_broken(void)
274 struct diff_queue_struct *q = &diff_queued_diff;
275 struct diff_queue_struct outq;
276 int i, j;
278 DIFF_QUEUE_CLEAR(&outq);
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 &&
286 !strcmp(p->one->path, p->two->path)) {
287 /* If the peer also survived rename/copy, then
288 * we merge them back together.
290 for (j = i + 1; j < q->nr; j++) {
291 struct diff_filepair *pp = q->queue[j];
292 if (pp->broken_pair &&
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;
298 goto next;
301 /* The peer did not survive, so we keep
302 * it in the output.
304 diff_q(&outq, p);
306 else
307 diff_q(&outq, p);
308 next:;
310 free(q->queue);
311 *q = outq;
313 return;