2 * Copyright (C) 2005 Junio C Hamano
9 struct diff_rename_pool {
10 struct diff_filespec **s;
14 static void diff_rename_pool_clear(struct diff_rename_pool *pool)
16 pool->s = NULL; pool->nr = pool->alloc = 0;
19 static void diff_rename_pool_add(struct diff_rename_pool *pool,
20 struct diff_filespec *s)
23 return; /* rename/copy patch for tree does not make sense. */
25 if (pool->alloc <= pool->nr) {
26 pool->alloc = alloc_nr(pool->alloc);
27 pool->s = xrealloc(pool->s,
28 sizeof(*(pool->s)) * pool->alloc);
30 pool->s[pool->nr] = s;
34 static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)
36 if (src->sha1_valid && dst->sha1_valid &&
37 !memcmp(src->sha1, dst->sha1, 20))
39 if (diff_populate_filespec(src) || diff_populate_filespec(dst))
40 /* this is an error but will be caught downstream */
42 if (src->size == dst->size &&
43 !memcmp(src->data, dst->data, src->size))
49 struct diff_filespec *src;
50 struct diff_filespec *dst;
55 static int estimate_similarity(struct diff_filespec *src,
56 struct diff_filespec *dst,
59 /* src points at a file that existed in the original tree (or
60 * optionally a file in the destination tree) and dst points
61 * at a newly created file. They may be quite similar, in which
62 * case we want to say src is renamed to dst or src is copied into
63 * dst, and then some edit has been applied to dst.
65 * Compare them and return how similar they are, representing
66 * the score as an integer between 0 and 10000, except
67 * where they match exactly it is considered better than anything
71 unsigned long delta_size;
74 delta_size = ((src->size < dst->size) ?
75 (dst->size - src->size) : (src->size - dst->size));
77 /* We would not consider rename followed by more than
78 * minimum_score/MAX_SCORE edits; that is, delta_size must be smaller
79 * than (src->size + dst->size)/2 * minimum_score/MAX_SCORE,
83 if ((src->size+dst->size)*minimum_score < delta_size*MAX_SCORE*2)
86 delta = diff_delta(src->data, src->size,
91 /* This "delta" is really xdiff with adler32 and all the
92 * overheads but it is a quick and dirty approximation.
94 * Now we will give some score to it. 100% edit gets
95 * 0 points and 0% edit gets MAX_SCORE points. That is, every
96 * 1/MAX_SCORE edit gets 1 point penalty. The amount of penalty is:
98 * (delta_size * 2 / (src->size + dst->size)) * MAX_SCORE
101 score = MAX_SCORE-(MAX_SCORE*2*delta_size/(src->size+dst->size));
102 if (score < 0) return 0;
103 if (MAX_SCORE < score) return MAX_SCORE;
107 static void record_rename_pair(struct diff_queue_struct *outq,
108 struct diff_filespec *src,
109 struct diff_filespec *dst,
113 /* The rank is used to sort the final output, because there
114 * are certain dependencies.
116 * - rank #0 depends on deleted ones.
117 * - rank #1 depends on kept files before they are modified.
118 * - rank #2 depends on kept files after they are modified;
119 * currently not used.
121 * Therefore, the final output order should be:
123 * 1. rank #0 rename/copy diffs.
124 * 2. deletions in the original.
125 * 3. rank #1 rename/copy diffs.
126 * 4. additions and modifications in the original.
127 * 5. rank #2 rename/copy diffs; currently not used.
129 * To achieve this sort order, we give xform_work the number
132 struct diff_file_pair *dp = diff_queue(outq, src, dst);
133 dp->xfrm_work = (rank * 2 + 1) | (score<<RENAME_SCORE_SHIFT);
134 dst->xfrm_flags |= RENAME_DST_MATCHED;
138 static void debug_filespec(struct diff_filespec *s, int x, const char *one)
140 fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
143 s->file_valid ? "valid" : "invalid",
145 s->sha1_valid ? sha1_to_hex(s->sha1) : "");
146 fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
148 s->size, s->xfrm_flags);
151 static void debug_filepair(const struct diff_file_pair *p, int i)
153 debug_filespec(p->one, i, "one");
154 debug_filespec(p->two, i, "two");
155 fprintf(stderr, "pair flags %d, orig order %d, score %d\n",
156 (p->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1)),
158 (p->xfrm_work >> RENAME_SCORE_SHIFT));
161 static void debug_queue(const char *msg, struct diff_queue_struct *q)
165 fprintf(stderr, "%s\n", msg);
166 fprintf(stderr, "q->nr = %d\n", q->nr);
167 for (i = 0; i < q->nr; i++) {
168 struct diff_file_pair *p = q->queue[i];
169 debug_filepair(p, i);
173 #define debug_queue(a,b) do { ; /*nothing*/ } while(0)
177 * We sort the outstanding diff entries according to the rank (see
178 * comment at the beginning of record_rename_pair) and tiebreak with
179 * the order in the original input.
181 static int rank_compare(const void *a_, const void *b_)
183 const struct diff_file_pair *a = *(const struct diff_file_pair **)a_;
184 const struct diff_file_pair *b = *(const struct diff_file_pair **)b_;
185 int a_rank = a->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1);
186 int b_rank = b->xfrm_work & ((1<<RENAME_SCORE_SHIFT) - 1);
188 if (a_rank != b_rank)
189 return a_rank - b_rank;
190 return a->orig_order - b->orig_order;
194 * We sort the rename similarity matrix with the score, in descending
195 * order (more similar first).
197 static int score_compare(const void *a_, const void *b_)
199 const struct diff_score *a = a_, *b = b_;
200 return b->score - a->score;
203 static int needs_to_stay(struct diff_queue_struct *q, int i,
204 struct diff_filespec *it)
206 /* If it will be used in later entry (either stay or used
207 * as the source of rename/copy), we need to copy, not rename.
210 struct diff_file_pair *p = q->queue[i++];
211 if (!p->two->file_valid)
212 continue; /* removed is fine */
213 if (strcmp(p->one->path, it->path))
214 continue; /* not relevant */
216 /* p has its src set to *it and it is not a delete;
217 * it will be used for in-place change or rename/copy,
218 * so we cannot rename it out.
225 void diff_detect_rename(struct diff_queue_struct *q,
229 struct diff_queue_struct outq;
230 struct diff_rename_pool created, deleted, stay;
231 struct diff_rename_pool *(srcs[2]);
232 struct diff_score *mx;
234 int num_create, num_src, dst_cnt, src_cnt;
237 outq.nr = outq.alloc = 0;
239 diff_rename_pool_clear(&created);
240 diff_rename_pool_clear(&deleted);
241 diff_rename_pool_clear(&stay);
247 * (1) make sure we properly ignore but pass trees.
249 * (2) make sure we do right thing on the same path deleted
250 * and created in the same patch.
253 for (i = 0; i < q->nr; i++) {
254 struct diff_file_pair *p = q->queue[i];
255 if (!p->one->file_valid)
256 if (!p->two->file_valid)
257 continue; /* ignore nonsense */
259 diff_rename_pool_add(&created, p->two);
260 else if (!p->two->file_valid)
261 diff_rename_pool_add(&deleted, p->one);
262 else if (1 < detect_rename) /* find copy, too */
263 diff_rename_pool_add(&stay, p->one);
266 goto cleanup; /* nothing to do */
268 /* We really want to cull the candidates list early
269 * with cheap tests in order to avoid doing deltas.
271 * With the current callers, we should not have already
272 * matched entries at this point, but it is nonetheless
273 * checked for sanity.
275 for (i = 0; i < created.nr; i++) {
276 if (created.s[i]->xfrm_flags & RENAME_DST_MATCHED)
277 continue; /* we have matched exactly already */
278 for (h = 0; h < sizeof(srcs)/sizeof(srcs[0]); h++) {
279 struct diff_rename_pool *p = srcs[h];
280 for (j = 0; j < p->nr; j++) {
281 if (!is_exact_match(p->s[j], created.s[i]))
283 record_rename_pair(&outq,
284 p->s[j], created.s[i], h,
286 break; /* we are done with this entry */
290 debug_queue("done detecting exact", &outq);
292 /* Have we run out the created file pool? If so we can avoid
293 * doing the delta matrix altogether.
295 if (outq.nr == created.nr)
298 num_create = (created.nr - outq.nr);
299 num_src = deleted.nr + stay.nr;
300 mx = xmalloc(sizeof(*mx) * num_create * num_src);
301 for (dst_cnt = i = 0; i < created.nr; i++) {
302 int base = dst_cnt * num_src;
303 if (created.s[i]->xfrm_flags & RENAME_DST_MATCHED)
304 continue; /* dealt with exact match already. */
305 for (src_cnt = h = 0; h < sizeof(srcs)/sizeof(srcs[0]); h++) {
306 struct diff_rename_pool *p = srcs[h];
307 for (j = 0; j < p->nr; j++, src_cnt++) {
308 struct diff_score *m = &mx[base + src_cnt];
310 m->dst = created.s[i];
311 m->score = estimate_similarity(m->src, m->dst,
318 /* cost matrix sorted by most to least similar pair */
319 qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
320 for (i = 0; i < num_create * num_src; i++) {
321 if (mx[i].dst->xfrm_flags & RENAME_DST_MATCHED)
322 continue; /* alreayd done, either exact or fuzzy. */
323 if (mx[i].score < minimum_score)
325 record_rename_pair(&outq,
326 mx[i].src, mx[i].dst, mx[i].rank,
330 debug_queue("done detecting fuzzy", &outq);
333 /* At this point, we have found some renames and copies and they
334 * are kept in outq. The original list is still in *q.
336 * Scan the original list and move them into the outq; we will sort
337 * outq and swap it into the queue supplied to pass that to
338 * downstream, so we assign the sort keys in this loop.
340 * See comments at the top of record_rename_pair for numbers used
341 * to assign xfrm_work.
343 * Note that we have not annotated the diff_file_pair with any comment
344 * so there is nothing other than p to free.
346 for (i = 0; i < q->nr; i++) {
347 struct diff_file_pair *dp, *p = q->queue[i];
348 if (!p->one->file_valid) {
349 if (p->two->file_valid) {
351 dp = diff_queue(&outq, p->one, p->two);
354 /* otherwise it is a nonsense; just ignore it */
356 else if (!p->two->file_valid) {
358 dp = diff_queue(&outq, p->one, p->two);
362 /* modification, or stay as is */
363 dp = diff_queue(&outq, p->one, p->two);
368 debug_queue("done copying original", &outq);
371 qsort(outq.queue, outq.nr, sizeof(outq.queue[0]), rank_compare);
373 debug_queue("done sorting", &outq);
376 q->nr = q->alloc = 0;
379 /* Copy it out to q, removing duplicates. */
380 for (i = 0; i < outq.nr; i++) {
381 struct diff_file_pair *p = outq.queue[i];
382 if (!p->one->file_valid) {
384 if (p->two->xfrm_flags & RENAME_DST_MATCHED)
385 ; /* rename/copy created it already */
387 diff_queue(q, p->one, p->two);
389 else if (!p->two->file_valid) {
391 if (p->one->xfrm_flags & RENAME_SRC_GONE)
392 ; /* rename/copy deleted it already */
394 diff_queue(q, p->one, p->two);
396 else if (strcmp(p->one->path, p->two->path)) {
398 struct diff_file_pair *dp =
399 diff_queue(q, p->one, p->two);
400 int msglen = (strlen(p->one->path) +
401 strlen(p->two->path) + 100);
402 int score = (p->xfrm_work >> RENAME_SCORE_SHIFT);
403 dp->xfrm_msg = xmalloc(msglen);
405 /* if we have a later entry that is a rename/copy
406 * that depends on p->one, then we copy here.
407 * otherwise we rename it.
409 if (needs_to_stay(&outq, i+1, p->one)) {
411 sprintf(dp->xfrm_msg,
412 "similarity index %d%%\n"
415 (int)(0.5 + score * 100 / MAX_SCORE),
416 p->one->path, p->two->path);
419 /* rename it, and mark it as gone. */
420 p->one->xfrm_flags |= RENAME_SRC_GONE;
421 sprintf(dp->xfrm_msg,
422 "similarity index %d%%\n"
425 (int)(0.5 + score * 100 / MAX_SCORE),
426 p->one->path, p->two->path);
430 /* otherwise it is a modified (or stayed) entry */
431 diff_queue(q, p->one, p->two);
436 debug_queue("done collapsing", q);