pretty_print_commit: add different formats
[git/jnareb-git.git] / diffcore-break.c
blob8cbb99fb41bd7a22028757e0e57842365c52622e
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "delta.h"
8 #include "count-delta.h"
10 static int should_break(struct diff_filespec *src,
11 struct diff_filespec *dst,
12 int break_score,
13 int *merge_score_p)
15 /* dst is recorded as a modification of src. Are they so
16 * different that we are better off recording this as a pair
17 * of delete and create?
19 * There are two criteria used in this algorithm. For the
20 * purposes of helping later rename/copy, we take both delete
21 * and insert into account and estimate the amount of "edit".
22 * If the edit is very large, we break this pair so that
23 * rename/copy can pick the pieces up to match with other
24 * files.
26 * On the other hand, we would want to ignore inserts for the
27 * pure "complete rewrite" detection. As long as most of the
28 * existing contents were removed from the file, it is a
29 * complete rewrite, and if sizable chunk from the original
30 * still remains in the result, it is not a rewrite. It does
31 * not matter how much or how little new material is added to
32 * the file.
34 * The score we leave for such a broken filepair uses the
35 * latter definition so that later clean-up stage can find the
36 * pieces that should not have been broken according to the
37 * latter definition after rename/copy runs, and merge the
38 * broken pair that have a score lower than given criteria
39 * back together. The break operation itself happens
40 * according to the former definition.
42 * The minimum_edit parameter tells us when to break (the
43 * amount of "edit" required for us to consider breaking the
44 * pair). We leave the amount of deletion in *merge_score_p
45 * when we return.
47 * The value we return is 1 if we want the pair to be broken,
48 * or 0 if we do not.
50 void *delta;
51 unsigned long delta_size, base_size, src_copied, literal_added;
52 int to_break = 0;
54 *merge_score_p = 0; /* assume no deletion --- "do not break"
55 * is the default.
58 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
59 return 0; /* leave symlink rename alone */
61 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
62 return 0; /* error but caught downstream */
64 delta_size = ((src->size < dst->size) ?
65 (dst->size - src->size) : (src->size - dst->size));
67 /* Notice that we use max of src and dst as the base size,
68 * unlike rename similarity detection. This is so that we do
69 * not mistake a large addition as a complete rewrite.
71 base_size = ((src->size < dst->size) ? dst->size : src->size);
73 delta = diff_delta(src->data, src->size,
74 dst->data, dst->size,
75 &delta_size);
77 /* Estimate the edit size by interpreting delta. */
78 if (count_delta(delta, delta_size,
79 &src_copied, &literal_added)) {
80 free(delta);
81 return 0; /* we cannot tell */
83 free(delta);
85 /* Compute merge-score, which is "how much is removed
86 * from the source material". The clean-up stage will
87 * merge the surviving pair together if the score is
88 * less than the minimum, after rename/copy runs.
90 if (src->size <= src_copied)
91 delta_size = 0; /* avoid wrapping around */
92 else
93 delta_size = src->size - src_copied;
94 *merge_score_p = delta_size * MAX_SCORE / src->size;
96 /* Extent of damage, which counts both inserts and
97 * deletes.
99 if (src->size + literal_added <= src_copied)
100 delta_size = 0; /* avoid wrapping around */
101 else
102 delta_size = (src->size - src_copied) + literal_added;
104 /* We break if the edit exceeds the minimum.
105 * i.e. (break_score / MAX_SCORE < delta_size / base_size)
107 if (break_score * base_size < delta_size * MAX_SCORE)
108 to_break = 1;
110 return to_break;
113 void diffcore_break(int break_score)
115 struct diff_queue_struct *q = &diff_queued_diff;
116 struct diff_queue_struct outq;
118 /* When the filepair has this much edit (insert and delete),
119 * it is first considered to be a rewrite and broken into a
120 * create and delete filepair. This is to help breaking a
121 * file that had too much new stuff added, possibly from
122 * moving contents from another file, so that rename/copy can
123 * match it with the other file.
125 * int break_score; we reuse incoming parameter for this.
128 /* After a pair is broken according to break_score and
129 * subjected to rename/copy, both of them may survive intact,
130 * due to lack of suitable rename/copy peer. Or, the caller
131 * may be calling us without using rename/copy. When that
132 * happens, we merge the broken pieces back into one
133 * modification together if the pair did not have more than
134 * this much delete. For this computation, we do not take
135 * insert into account at all. If you start from a 100-line
136 * file and delete 97 lines of it, it does not matter if you
137 * add 27 lines to it to make a new 30-line file or if you add
138 * 997 lines to it to make a 1000-line file. Either way what
139 * you did was a rewrite of 97%. On the other hand, if you
140 * delete 3 lines, keeping 97 lines intact, it does not matter
141 * if you add 3 lines to it to make a new 100-line file or if
142 * you add 903 lines to it to make a new 1000-line file.
143 * Either way you did a lot of additions and not a rewrite.
144 * This merge happens to catch the latter case. A merge_score
145 * of 80% would be a good default value (a broken pair that
146 * has score lower than merge_score will be merged back
147 * together).
149 int merge_score;
150 int i;
152 /* See comment on DEFAULT_BREAK_SCORE and
153 * DEFAULT_MERGE_SCORE in diffcore.h
155 merge_score = (break_score >> 16) & 0xFFFF;
156 break_score = (break_score & 0xFFFF);
158 if (!break_score)
159 break_score = DEFAULT_BREAK_SCORE;
160 if (!merge_score)
161 merge_score = DEFAULT_MERGE_SCORE;
163 outq.nr = outq.alloc = 0;
164 outq.queue = NULL;
166 for (i = 0; i < q->nr; i++) {
167 struct diff_filepair *p = q->queue[i];
168 int score;
170 /* We deal only with in-place edit of non directory.
171 * We do not break anything else.
173 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) &&
174 !S_ISDIR(p->one->mode) && !S_ISDIR(p->two->mode) &&
175 !strcmp(p->one->path, p->two->path)) {
176 if (should_break(p->one, p->two,
177 break_score, &score)) {
178 /* Split this into delete and create */
179 struct diff_filespec *null_one, *null_two;
180 struct diff_filepair *dp;
182 /* Set score to 0 for the pair that
183 * needs to be merged back together
184 * should they survive rename/copy.
185 * Also we do not want to break very
186 * small files.
188 if ((score < merge_score) ||
189 (p->one->size < MINIMUM_BREAK_SIZE))
190 score = 0;
192 /* deletion of one */
193 null_one = alloc_filespec(p->one->path);
194 dp = diff_queue(&outq, p->one, null_one);
195 dp->score = score;
196 dp->broken_pair = 1;
198 /* creation of two */
199 null_two = alloc_filespec(p->two->path);
200 dp = diff_queue(&outq, null_two, p->two);
201 dp->score = score;
202 dp->broken_pair = 1;
204 free(p); /* not diff_free_filepair(), we are
205 * reusing one and two here.
207 continue;
210 diff_q(&outq, p);
212 free(q->queue);
213 *q = outq;
215 return;
218 static void merge_broken(struct diff_filepair *p,
219 struct diff_filepair *pp,
220 struct diff_queue_struct *outq)
222 /* p and pp are broken pairs we want to merge */
223 struct diff_filepair *c = p, *d = pp;
224 if (DIFF_FILE_VALID(p->one)) {
225 /* this must be a delete half */
226 d = p; c = pp;
228 /* Sanity check */
229 if (!DIFF_FILE_VALID(d->one))
230 die("internal error in merge #1");
231 if (DIFF_FILE_VALID(d->two))
232 die("internal error in merge #2");
233 if (DIFF_FILE_VALID(c->one))
234 die("internal error in merge #3");
235 if (!DIFF_FILE_VALID(c->two))
236 die("internal error in merge #4");
238 diff_queue(outq, d->one, c->two);
239 diff_free_filespec_data(d->two);
240 diff_free_filespec_data(c->one);
241 free(d);
242 free(c);
245 void diffcore_merge_broken(void)
247 struct diff_queue_struct *q = &diff_queued_diff;
248 struct diff_queue_struct outq;
249 int i, j;
251 outq.nr = outq.alloc = 0;
252 outq.queue = NULL;
254 for (i = 0; i < q->nr; i++) {
255 struct diff_filepair *p = q->queue[i];
256 if (!p)
257 /* we already merged this with its peer */
258 continue;
259 else if (p->broken_pair &&
260 p->score == 0 &&
261 !strcmp(p->one->path, p->two->path)) {
262 /* If the peer also survived rename/copy, then
263 * we merge them back together.
265 for (j = i + 1; j < q->nr; j++) {
266 struct diff_filepair *pp = q->queue[j];
267 if (pp->broken_pair &&
268 p->score == 0 &&
269 !strcmp(pp->one->path, pp->two->path) &&
270 !strcmp(p->one->path, pp->two->path)) {
271 /* Peer survived. Merge them */
272 merge_broken(p, pp, &outq);
273 q->queue[j] = NULL;
274 break;
277 if (q->nr <= j)
278 /* The peer did not survive, so we keep
279 * it in the output.
281 diff_q(&outq, p);
283 else
284 diff_q(&outq, p);
286 free(q->queue);
287 *q = outq;
289 return;