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
, base_size
;
74 delta_size
= ((src
->size
< dst
->size
) ?
75 (dst
->size
- src
->size
) : (src
->size
- dst
->size
));
76 base_size
= ((src
->size
< dst
->size
) ? src
->size
: dst
->size
);
78 /* We would not consider edits that change the file size so
79 * drastically. delta_size must be smaller than
80 * minimum_score/MAX_SCORE * min(src->size, dst->size).
81 * Note that base_size == 0 case is handled here already
82 * and the final score computation below would not have a
83 * divide-by-zero issue.
85 if (base_size
* minimum_score
< delta_size
* MAX_SCORE
)
88 delta
= diff_delta(src
->data
, src
->size
,
92 * We currently punt here, but we may later end up parsing the
93 * delta to really assess the extent of damage. A big consecutive
94 * remove would produce small delta_size that affects quite a
95 * big portion of the file.
100 * Now we will give some score to it. 100% edit gets 0 points
101 * and 0% edit gets MAX_SCORE points.
103 score
= MAX_SCORE
- (MAX_SCORE
* delta_size
/ base_size
);
104 if (score
< 0) return 0;
105 if (MAX_SCORE
< score
) return MAX_SCORE
;
109 static void record_rename_pair(struct diff_queue_struct
*outq
,
110 struct diff_filespec
*src
,
111 struct diff_filespec
*dst
,
115 /* The rank is used to sort the final output, because there
116 * are certain dependencies.
118 * - rank #0 depends on deleted ones.
119 * - rank #1 depends on kept files before they are modified.
120 * - rank #2 depends on kept files after they are modified;
121 * currently not used.
123 * Therefore, the final output order should be:
125 * 1. rank #0 rename/copy diffs.
126 * 2. deletions in the original.
127 * 3. rank #1 rename/copy diffs.
128 * 4. additions and modifications in the original.
129 * 5. rank #2 rename/copy diffs; currently not used.
131 * To achieve this sort order, we give xform_work the number
134 struct diff_filepair
*dp
= diff_queue(outq
, src
, dst
);
135 dp
->xfrm_work
= (rank
* 2 + 1) | (score
<<RENAME_SCORE_SHIFT
);
136 dst
->xfrm_flags
|= RENAME_DST_MATCHED
;
140 static void debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
142 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
145 s
->file_valid
? "valid" : "invalid",
147 s
->sha1_valid
? sha1_to_hex(s
->sha1
) : "");
148 fprintf(stderr
, "queue[%d] %s size %lu flags %d\n",
150 s
->size
, s
->xfrm_flags
);
153 static void debug_filepair(const struct diff_filepair
*p
, int i
)
155 debug_filespec(p
->one
, i
, "one");
156 debug_filespec(p
->two
, i
, "two");
157 fprintf(stderr
, "pair flags %d, orig order %d, score %d\n",
158 (p
->xfrm_work
& ((1<<RENAME_SCORE_SHIFT
) - 1)),
160 (p
->xfrm_work
>> RENAME_SCORE_SHIFT
));
163 static void debug_queue(const char *msg
, struct diff_queue_struct
*q
)
167 fprintf(stderr
, "%s\n", msg
);
168 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
169 for (i
= 0; i
< q
->nr
; i
++) {
170 struct diff_filepair
*p
= q
->queue
[i
];
171 debug_filepair(p
, i
);
175 #define debug_queue(a,b) do { ; /*nothing*/ } while(0)
179 * We sort the outstanding diff entries according to the rank (see
180 * comment at the beginning of record_rename_pair) and tiebreak with
181 * the order in the original input.
183 static int rank_compare(const void *a_
, const void *b_
)
185 const struct diff_filepair
*a
= *(const struct diff_filepair
**)a_
;
186 const struct diff_filepair
*b
= *(const struct diff_filepair
**)b_
;
187 int a_rank
= a
->xfrm_work
& ((1<<RENAME_SCORE_SHIFT
) - 1);
188 int b_rank
= b
->xfrm_work
& ((1<<RENAME_SCORE_SHIFT
) - 1);
190 if (a_rank
!= b_rank
)
191 return a_rank
- b_rank
;
192 return a
->orig_order
- b
->orig_order
;
196 * We sort the rename similarity matrix with the score, in descending
197 * order (more similar first).
199 static int score_compare(const void *a_
, const void *b_
)
201 const struct diff_score
*a
= a_
, *b
= b_
;
202 return b
->score
- a
->score
;
205 static int needs_to_stay(struct diff_queue_struct
*q
, int i
,
206 struct diff_filespec
*it
)
208 /* If it will be used in later entry (either stay or used
209 * as the source of rename/copy), we need to copy, not rename.
212 struct diff_filepair
*p
= q
->queue
[i
++];
213 if (!p
->two
->file_valid
)
214 continue; /* removed is fine */
215 if (strcmp(p
->one
->path
, it
->path
))
216 continue; /* not relevant */
218 /* p has its src set to *it and it is not a delete;
219 * it will be used for in-place change or rename/copy,
220 * so we cannot rename it out.
227 void diff_detect_rename(struct diff_queue_struct
*q
,
231 struct diff_queue_struct outq
;
232 struct diff_rename_pool created
, deleted
, stay
;
233 struct diff_rename_pool
*(srcs
[2]);
234 struct diff_score
*mx
;
236 int num_create
, num_src
, dst_cnt
, src_cnt
;
239 outq
.nr
= outq
.alloc
= 0;
241 diff_rename_pool_clear(&created
);
242 diff_rename_pool_clear(&deleted
);
243 diff_rename_pool_clear(&stay
);
248 for (i
= 0; i
< q
->nr
; i
++) {
249 struct diff_filepair
*p
= q
->queue
[i
];
250 if (!p
->one
->file_valid
)
251 if (!p
->two
->file_valid
)
252 continue; /* ignore nonsense */
254 diff_rename_pool_add(&created
, p
->two
);
255 else if (!p
->two
->file_valid
)
256 diff_rename_pool_add(&deleted
, p
->one
);
257 else if (1 < detect_rename
) /* find copy, too */
258 diff_rename_pool_add(&stay
, p
->one
);
261 goto cleanup
; /* nothing to do */
263 /* We really want to cull the candidates list early
264 * with cheap tests in order to avoid doing deltas.
266 * With the current callers, we should not have already
267 * matched entries at this point, but it is nonetheless
268 * checked for sanity.
270 for (i
= 0; i
< created
.nr
; i
++) {
271 if (created
.s
[i
]->xfrm_flags
& RENAME_DST_MATCHED
)
272 continue; /* we have matched exactly already */
273 for (h
= 0; h
< sizeof(srcs
)/sizeof(srcs
[0]); h
++) {
274 struct diff_rename_pool
*p
= srcs
[h
];
275 for (j
= 0; j
< p
->nr
; j
++) {
276 if (!is_exact_match(p
->s
[j
], created
.s
[i
]))
278 record_rename_pair(&outq
,
279 p
->s
[j
], created
.s
[i
], h
,
281 break; /* we are done with this entry */
285 debug_queue("done detecting exact", &outq
);
287 /* Have we run out the created file pool? If so we can avoid
288 * doing the delta matrix altogether.
290 if (outq
.nr
== created
.nr
)
293 num_create
= (created
.nr
- outq
.nr
);
294 num_src
= deleted
.nr
+ stay
.nr
;
295 mx
= xmalloc(sizeof(*mx
) * num_create
* num_src
);
296 for (dst_cnt
= i
= 0; i
< created
.nr
; i
++) {
297 int base
= dst_cnt
* num_src
;
298 if (created
.s
[i
]->xfrm_flags
& RENAME_DST_MATCHED
)
299 continue; /* dealt with exact match already. */
300 for (src_cnt
= h
= 0; h
< sizeof(srcs
)/sizeof(srcs
[0]); h
++) {
301 struct diff_rename_pool
*p
= srcs
[h
];
302 for (j
= 0; j
< p
->nr
; j
++, src_cnt
++) {
303 struct diff_score
*m
= &mx
[base
+ src_cnt
];
305 m
->dst
= created
.s
[i
];
306 m
->score
= estimate_similarity(m
->src
, m
->dst
,
313 /* cost matrix sorted by most to least similar pair */
314 qsort(mx
, num_create
* num_src
, sizeof(*mx
), score_compare
);
315 for (i
= 0; i
< num_create
* num_src
; i
++) {
316 if (mx
[i
].dst
->xfrm_flags
& RENAME_DST_MATCHED
)
317 continue; /* alreayd done, either exact or fuzzy. */
318 if (mx
[i
].score
< minimum_score
)
319 break; /* there is not any more diffs applicable. */
320 record_rename_pair(&outq
,
321 mx
[i
].src
, mx
[i
].dst
, mx
[i
].rank
,
325 debug_queue("done detecting fuzzy", &outq
);
328 /* At this point, we have found some renames and copies and they
329 * are kept in outq. The original list is still in *q.
331 * Scan the original list and move them into the outq; we will sort
332 * outq and swap it into the queue supplied to pass that to
333 * downstream, so we assign the sort keys in this loop.
335 * See comments at the top of record_rename_pair for numbers used
336 * to assign xfrm_work.
338 * Note that we have not annotated the diff_filepair with any comment
339 * so there is nothing other than p to free.
341 for (i
= 0; i
< q
->nr
; i
++) {
342 struct diff_filepair
*dp
, *p
= q
->queue
[i
];
343 if (!p
->one
->file_valid
) {
344 if (p
->two
->file_valid
) {
346 dp
= diff_queue(&outq
, p
->one
, p
->two
);
349 /* otherwise it is a nonsense; just ignore it */
351 else if (!p
->two
->file_valid
) {
353 dp
= diff_queue(&outq
, p
->one
, p
->two
);
357 /* modification, or stay as is */
358 dp
= diff_queue(&outq
, p
->one
, p
->two
);
363 debug_queue("done copying original", &outq
);
366 qsort(outq
.queue
, outq
.nr
, sizeof(outq
.queue
[0]), rank_compare
);
368 debug_queue("done sorting", &outq
);
371 q
->nr
= q
->alloc
= 0;
374 /* Copy it out to q, removing duplicates. */
375 for (i
= 0; i
< outq
.nr
; i
++) {
376 struct diff_filepair
*p
= outq
.queue
[i
];
377 if (!p
->one
->file_valid
) {
379 if (p
->two
->xfrm_flags
& RENAME_DST_MATCHED
)
380 ; /* rename/copy created it already */
382 diff_queue(q
, p
->one
, p
->two
);
384 else if (!p
->two
->file_valid
) {
386 if (p
->one
->xfrm_flags
& RENAME_SRC_GONE
)
387 ; /* rename/copy deleted it already */
389 diff_queue(q
, p
->one
, p
->two
);
391 else if (strcmp(p
->one
->path
, p
->two
->path
)) {
393 struct diff_filepair
*dp
=
394 diff_queue(q
, p
->one
, p
->two
);
395 int msglen
= (strlen(p
->one
->path
) +
396 strlen(p
->two
->path
) + 100);
397 int score
= (p
->xfrm_work
>> RENAME_SCORE_SHIFT
);
398 dp
->xfrm_msg
= xmalloc(msglen
);
400 /* if we have a later entry that is a rename/copy
401 * that depends on p->one, then we copy here.
402 * otherwise we rename it.
404 if (needs_to_stay(&outq
, i
+1, p
->one
)) {
406 sprintf(dp
->xfrm_msg
,
407 "similarity index %d%%\n"
410 (int)(0.5 + score
* 100 / MAX_SCORE
),
411 p
->one
->path
, p
->two
->path
);
414 /* rename it, and mark it as gone. */
415 p
->one
->xfrm_flags
|= RENAME_SRC_GONE
;
416 sprintf(dp
->xfrm_msg
,
417 "similarity index %d%%\n"
420 (int)(0.5 + score
* 100 / MAX_SCORE
),
421 p
->one
->path
, p
->two
->path
);
425 /* otherwise it is a modified (or stayed) entry */
426 diff_queue(q
, p
->one
, p
->two
);
431 debug_queue("done collapsing", q
);