sha1_file.c: do not die failing to malloc in unpack_compressed_entry
[git/git.git] / diffcore-rename.c
CommitLineData
427dcb4b
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
f79d9c58 7#include "hashmap.h"
3ac942d4 8#include "progress.h"
427dcb4b 9
25d5ea41
JH
10/* Table of rename/copy destinations */
11
12static struct diff_rename_dst {
13 struct diff_filespec *two;
14 struct diff_filepair *pair;
15} *rename_dst;
16static int rename_dst_nr, rename_dst_alloc;
427dcb4b 17
25d5ea41
JH
18static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19 int insert_ok)
427dcb4b 20{
25d5ea41
JH
21 int first, last;
22
23 first = 0;
24 last = rename_dst_nr;
25 while (last > first) {
26 int next = (last + first) >> 1;
27 struct diff_rename_dst *dst = &(rename_dst[next]);
28 int cmp = strcmp(two->path, dst->two->path);
29 if (!cmp)
30 return dst;
31 if (cmp < 0) {
32 last = next;
33 continue;
34 }
35 first = next+1;
36 }
37 /* not found */
38 if (!insert_ok)
39 return NULL;
40 /* insert to make it at "first" */
337ce247 41 ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
25d5ea41
JH
42 rename_dst_nr++;
43 if (first < rename_dst_nr)
44 memmove(rename_dst + first + 1, rename_dst + first,
45 (rename_dst_nr - first - 1) * sizeof(*rename_dst));
5098bafb 46 rename_dst[first].two = alloc_filespec(two->path);
e5450100 47 fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
25d5ea41
JH
48 rename_dst[first].pair = NULL;
49 return &(rename_dst[first]);
427dcb4b
JH
50}
51
15d061b4 52/* Table of rename/copy src files */
25d5ea41 53static struct diff_rename_src {
e88d6bc6 54 struct diff_filepair *p;
fc580719 55 unsigned short score; /* to remember the break score */
25d5ea41
JH
56} *rename_src;
57static int rename_src_nr, rename_src_alloc;
427dcb4b 58
e88d6bc6 59static struct diff_rename_src *register_rename_src(struct diff_filepair *p)
25d5ea41
JH
60{
61 int first, last;
e88d6bc6
JH
62 struct diff_filespec *one = p->one;
63 unsigned short score = p->score;
25d5ea41
JH
64
65 first = 0;
66 last = rename_src_nr;
67 while (last > first) {
68 int next = (last + first) >> 1;
69 struct diff_rename_src *src = &(rename_src[next]);
e88d6bc6 70 int cmp = strcmp(one->path, src->p->one->path);
25d5ea41
JH
71 if (!cmp)
72 return src;
73 if (cmp < 0) {
74 last = next;
75 continue;
76 }
77 first = next+1;
78 }
15d061b4 79
25d5ea41 80 /* insert to make it at "first" */
337ce247 81 ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc);
25d5ea41
JH
82 rename_src_nr++;
83 if (first < rename_src_nr)
84 memmove(rename_src + first + 1, rename_src + first,
85 (rename_src_nr - first - 1) * sizeof(*rename_src));
e88d6bc6 86 rename_src[first].p = p;
fc580719 87 rename_src[first].score = score;
25d5ea41 88 return &(rename_src[first]);
427dcb4b
JH
89}
90
0ce39643
JS
91static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
92{
93 int src_len = strlen(src->path), dst_len = strlen(dst->path);
94 while (src_len && dst_len) {
95 char c1 = src->path[--src_len];
96 char c2 = dst->path[--dst_len];
97 if (c1 != c2)
98 return 0;
99 if (c1 == '/')
100 return 1;
101 }
102 return (!src_len || src->path[src_len - 1] == '/') &&
103 (!dst_len || dst->path[dst_len - 1] == '/');
104}
105
427dcb4b 106struct diff_score {
25d5ea41
JH
107 int src; /* index in rename_src */
108 int dst; /* index in rename_dst */
6d24ad97
JH
109 unsigned short score;
110 short name_score;
427dcb4b
JH
111};
112
113static int estimate_similarity(struct diff_filespec *src,
114 struct diff_filespec *dst,
115 int minimum_score)
116{
117 /* src points at a file that existed in the original tree (or
118 * optionally a file in the destination tree) and dst points
119 * at a newly created file. They may be quite similar, in which
120 * case we want to say src is renamed to dst or src is copied into
121 * dst, and then some edit has been applied to dst.
122 *
123 * Compare them and return how similar they are, representing
f0c6b2a2
JH
124 * the score as an integer between 0 and MAX_SCORE.
125 *
126 * When there is an exact match, it is considered a better
127 * match than anything else; the destination does not even
128 * call into this function in that case.
427dcb4b 129 */
90bd932c 130 unsigned long max_size, delta_size, base_size, src_copied, literal_added;
75c660ac 131 unsigned long delta_limit;
427dcb4b
JH
132 int score;
133
60896c7b
JH
134 /* We deal only with regular files. Symlink renames are handled
135 * only when they are exact matches --- in other words, no edits
136 * after renaming.
137 */
138 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
139 return 0;
140
81ac051d
LT
141 /*
142 * Need to check that source and destination sizes are
143 * filled in before comparing them.
144 *
145 * If we already have "cnt_data" filled in, we know it's
146 * all good (avoid checking the size for zero, as that
147 * is a possible size - we really should have a flag to
148 * say whether the size is valid or not!)
149 */
885c716f 150 if (!src->cnt_data && diff_populate_filespec(src, 1))
81ac051d 151 return 0;
885c716f 152 if (!dst->cnt_data && diff_populate_filespec(dst, 1))
81ac051d
LT
153 return 0;
154
90bd932c 155 max_size = ((src->size > dst->size) ? src->size : dst->size);
58b103f5 156 base_size = ((src->size < dst->size) ? src->size : dst->size);
90bd932c 157 delta_size = max_size - base_size;
427dcb4b 158
58b103f5
JH
159 /* We would not consider edits that change the file size so
160 * drastically. delta_size must be smaller than
cd1870ed 161 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
f0c6b2a2 162 *
58b103f5
JH
163 * Note that base_size == 0 case is handled here already
164 * and the final score computation below would not have a
165 * divide-by-zero issue.
427dcb4b 166 */
3a4d6769 167 if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
427dcb4b
JH
168 return 0;
169
885c716f
BS
170 if (!src->cnt_data && diff_populate_filespec(src, 0))
171 return 0;
172 if (!dst->cnt_data && diff_populate_filespec(dst, 0))
173 return 0;
174
dc49cd76
SP
175 delta_limit = (unsigned long)
176 (base_size * (MAX_SCORE-minimum_score) / MAX_SCORE);
d8c3d03a 177 if (diffcore_count_changes(src, dst,
c06c7966 178 &src->cnt_data, &dst->cnt_data,
65416758
JH
179 delta_limit,
180 &src_copied, &literal_added))
85976974 181 return 0;
355e76a4 182
1706306a
JH
183 /* How similar are they?
184 * what percentage of material in dst are from source?
427dcb4b 185 */
90bd932c 186 if (!dst->size)
1706306a 187 score = 0; /* should not happen */
cfc0aef1 188 else
dc49cd76 189 score = (int)(src_copied * MAX_SCORE / max_size);
427dcb4b
JH
190 return score;
191}
192
5098bafb 193static void record_rename_pair(int dst_index, int src_index, int score)
427dcb4b 194{
9fb88419 195 struct diff_filespec *src, *dst;
25d5ea41 196 struct diff_filepair *dp;
f7c1512a 197
25d5ea41
JH
198 if (rename_dst[dst_index].pair)
199 die("internal error: dst already matched.");
427dcb4b 200
e88d6bc6 201 src = rename_src[src_index].p->one;
64479711 202 src->rename_used++;
9fb88419 203 src->count++;
427dcb4b 204
25d5ea41 205 dst = rename_dst[dst_index].two;
9fb88419 206 dst->count++;
427dcb4b 207
9fb88419 208 dp = diff_queue(NULL, src, dst);
ef677686 209 dp->renamed_pair = 1;
fc580719
JH
210 if (!strcmp(src->path, dst->path))
211 dp->score = rename_src[src_index].score;
212 else
213 dp->score = score;
25d5ea41 214 rename_dst[dst_index].pair = dp;
427dcb4b
JH
215}
216
217/*
218 * We sort the rename similarity matrix with the score, in descending
15d061b4 219 * order (the most similar first).
427dcb4b
JH
220 */
221static int score_compare(const void *a_, const void *b_)
222{
223 const struct diff_score *a = a_, *b = b_;
cfc0aef1 224
6d24ad97
JH
225 /* sink the unused ones to the bottom */
226 if (a->dst < 0)
227 return (0 <= b->dst);
228 else if (b->dst < 0)
229 return -1;
230
cfc0aef1
RS
231 if (a->score == b->score)
232 return b->name_score - a->name_score;
233
427dcb4b
JH
234 return b->score - a->score;
235}
236
9027f53c 237struct file_similarity {
f79d9c58 238 struct hashmap_entry entry;
7c85f8ac 239 int index;
9027f53c 240 struct diff_filespec *filespec;
9027f53c
LT
241};
242
48f6407f
KB
243static unsigned int hash_filespec(struct diff_filespec *filespec)
244{
48f6407f
KB
245 if (!filespec->sha1_valid) {
246 if (diff_populate_filespec(filespec, 0))
247 return 0;
248 hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
249 }
039dc71a 250 return sha1hash(filespec->sha1);
48f6407f
KB
251}
252
f79d9c58 253static int find_identical_files(struct hashmap *srcs,
7c85f8ac 254 int dst_index,
11f944dd 255 struct diff_options *options)
9027f53c
LT
256{
257 int renames = 0;
258
7c85f8ac 259 struct diff_filespec *target = rename_dst[dst_index].two;
ab73a9d1 260 struct file_similarity *p, *best = NULL;
48f6407f
KB
261 int i = 100, best_score = -1;
262
263 /*
7c85f8ac 264 * Find the best source match for specified destination.
48f6407f 265 */
ab73a9d1
KB
266 p = hashmap_get_from_hash(srcs, hash_filespec(target), NULL);
267 for (; p; p = hashmap_get_next(srcs, p)) {
48f6407f
KB
268 int score;
269 struct diff_filespec *source = p->filespec;
270
271 /* False hash collision? */
272 if (hashcmp(source->sha1, target->sha1))
273 continue;
274 /* Non-regular files? If so, the modes must match! */
275 if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) {
276 if (source->mode != target->mode)
0940e5f2 277 continue;
9027f53c 278 }
48f6407f
KB
279 /* Give higher scores to sources that haven't been used already */
280 score = !source->rename_used;
281 if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY)
282 continue;
283 score += basename_same(source, target);
284 if (score > best_score) {
285 best = p;
286 best_score = score;
287 if (score == 2)
288 break;
9027f53c 289 }
48f6407f
KB
290
291 /* Too many identical alternatives? Pick one */
292 if (!--i)
293 break;
294 }
295 if (best) {
7c85f8ac 296 record_rename_pair(dst_index, best->index, MAX_SCORE);
48f6407f
KB
297 renames++;
298 }
9027f53c
LT
299 return renames;
300}
301
f79d9c58 302static void insert_file_table(struct hashmap *table, int index, struct diff_filespec *filespec)
9027f53c 303{
9027f53c
LT
304 struct file_similarity *entry = xmalloc(sizeof(*entry));
305
9027f53c
LT
306 entry->index = index;
307 entry->filespec = filespec;
9027f53c 308
f79d9c58
KB
309 hashmap_entry_init(entry, hash_filespec(filespec));
310 hashmap_add(table, entry);
9027f53c
LT
311}
312
cb1491b6
LT
313/*
314 * Find exact renames first.
315 *
316 * The first round matches up the up-to-date entries,
317 * and then during the second round we try to match
318 * cache-dirty entries as well.
cb1491b6 319 */
11f944dd 320static int find_exact_renames(struct diff_options *options)
cb1491b6 321{
7c85f8ac 322 int i, renames = 0;
f79d9c58 323 struct hashmap file_table;
cb1491b6 324
7c85f8ac 325 /* Add all sources to the hash table */
f79d9c58 326 hashmap_init(&file_table, NULL, rename_src_nr);
9027f53c 327 for (i = 0; i < rename_src_nr; i++)
7c85f8ac 328 insert_file_table(&file_table, i, rename_src[i].p->one);
9027f53c 329
7c85f8ac 330 /* Walk the destinations and find best source match */
9027f53c 331 for (i = 0; i < rename_dst_nr; i++)
7c85f8ac 332 renames += find_identical_files(&file_table, i, options);
9027f53c 333
f79d9c58
KB
334 /* Free the hash data structure and entries */
335 hashmap_free(&file_table, 1);
9027f53c 336
7c85f8ac 337 return renames;
cb1491b6
LT
338}
339
6d24ad97
JH
340#define NUM_CANDIDATE_PER_DST 4
341static void record_if_better(struct diff_score m[], struct diff_score *o)
342{
343 int i, worst;
344
345 /* find the worst one */
346 worst = 0;
347 for (i = 1; i < NUM_CANDIDATE_PER_DST; i++)
348 if (score_compare(&m[i], &m[worst]) > 0)
349 worst = i;
350
351 /* is it better than the worst one? */
352 if (score_compare(&m[worst], o) > 0)
353 m[worst] = *o;
354}
355
f31027c9
JH
356/*
357 * Returns:
358 * 0 if we are under the limit;
359 * 1 if we need to disable inexact rename detection;
360 * 2 if we would be under the limit if we were given -C instead of -C -C.
361 */
9d8a5a50
JH
362static int too_many_rename_candidates(int num_create,
363 struct diff_options *options)
364{
365 int rename_limit = options->rename_limit;
366 int num_src = rename_src_nr;
f31027c9 367 int i;
9d8a5a50
JH
368
369 options->needed_rename_limit = 0;
370
371 /*
372 * This basically does a test for the rename matrix not
373 * growing larger than a "rename_limit" square matrix, ie:
374 *
375 * num_create * num_src > rename_limit * rename_limit
376 *
377 * but handles the potential overflow case specially (and we
378 * assume at least 32-bit integers)
379 */
380 if (rename_limit <= 0 || rename_limit > 32767)
381 rename_limit = 32767;
382 if ((num_create <= rename_limit || num_src <= rename_limit) &&
383 (num_create * num_src <= rename_limit * rename_limit))
384 return 0;
385
386 options->needed_rename_limit =
387 num_src > num_create ? num_src : num_create;
f31027c9
JH
388
389 /* Are we running under -C -C? */
390 if (!DIFF_OPT_TST(options, FIND_COPIES_HARDER))
391 return 1;
392
393 /* Would we bust the limit if we were running under -C? */
394 for (num_src = i = 0; i < rename_src_nr; i++) {
395 if (diff_unmodified_pair(rename_src[i].p))
396 continue;
397 num_src++;
398 }
399 if ((num_create <= rename_limit || num_src <= rename_limit) &&
400 (num_create * num_src <= rename_limit * rename_limit))
401 return 2;
9d8a5a50
JH
402 return 1;
403}
404
0940e5f2
LT
405static int find_renames(struct diff_score *mx, int dst_cnt, int minimum_score, int copies)
406{
407 int count = 0, i;
408
409 for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) {
410 struct diff_rename_dst *dst;
411
412 if ((mx[i].dst < 0) ||
413 (mx[i].score < minimum_score))
414 break; /* there is no more usable pair. */
415 dst = &rename_dst[mx[i].dst];
416 if (dst->pair)
417 continue; /* already done, either exact or fuzzy. */
e88d6bc6 418 if (!copies && rename_src[mx[i].src].p->one->rename_used)
0940e5f2
LT
419 continue;
420 record_rename_pair(mx[i].dst, mx[i].src, mx[i].score);
421 count++;
422 }
423 return count;
424}
425
8082d8d3 426void diffcore_rename(struct diff_options *options)
427dcb4b 427{
8082d8d3
JH
428 int detect_rename = options->detect_rename;
429 int minimum_score = options->rename_score;
38c6f780 430 struct diff_queue_struct *q = &diff_queued_diff;
5098bafb 431 struct diff_queue_struct outq;
427dcb4b 432 struct diff_score *mx;
f31027c9 433 int i, j, rename_count, skip_unmodified = 0;
dabdbee1 434 int num_create, dst_cnt;
3ac942d4 435 struct progress *progress = NULL;
427dcb4b 436
26dee0ad 437 if (!minimum_score)
f345b0a0 438 minimum_score = DEFAULT_RENAME_SCORE;
427dcb4b 439
427dcb4b 440 for (i = 0; i < q->nr; i++) {
52e95789 441 struct diff_filepair *p = q->queue[i];
2f3f8b21 442 if (!DIFF_FILE_VALID(p->one)) {
81e50eab 443 if (!DIFF_FILE_VALID(p->two))
60896c7b 444 continue; /* unmerged */
2f3f8b21
JH
445 else if (options->single_follow &&
446 strcmp(options->single_follow, p->two->path))
447 continue; /* not interested */
90d43b07
JK
448 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
449 is_empty_blob_sha1(p->two->sha1))
450 continue;
427dcb4b 451 else
25d5ea41 452 locate_rename_dst(p->two, 1);
2f3f8b21 453 }
90d43b07
JK
454 else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
455 is_empty_blob_sha1(p->one->sha1))
456 continue;
d7c9bf22 457 else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) {
64479711
LT
458 /*
459 * If the source is a broken "delete", and
2210100a
JH
460 * they did not really want to get broken,
461 * that means the source actually stays.
64479711
LT
462 * So we increment the "rename_used" score
463 * by one, to indicate ourselves as a user
464 */
465 if (p->broken_pair && !p->score)
466 p->one->rename_used++;
e88d6bc6 467 register_rename_src(p);
64479711
LT
468 }
469 else if (detect_rename == DIFF_DETECT_COPY) {
470 /*
471 * Increment the "rename_used" score by
472 * one, to indicate ourselves as a user.
2210100a 473 */
64479711 474 p->one->rename_used++;
e88d6bc6 475 register_rename_src(p);
2210100a 476 }
427dcb4b 477 }
0024a549 478 if (rename_dst_nr == 0 || rename_src_nr == 0)
427dcb4b
JH
479 goto cleanup; /* nothing to do */
480
17559a64
LT
481 /*
482 * We really want to cull the candidates list early
483 * with cheap tests in order to avoid doing deltas.
484 */
11f944dd 485 rename_count = find_exact_renames(options);
17559a64 486
42899ac8
LT
487 /* Did we only want exact renames? */
488 if (minimum_score == MAX_SCORE)
489 goto cleanup;
490
491 /*
492 * Calculate how many renames are left (but all the source
493 * files still remain as options for rename/copies!)
494 */
495 num_create = (rename_dst_nr - rename_count);
42899ac8
LT
496
497 /* All done? */
498 if (!num_create)
499 goto cleanup;
500
f31027c9
JH
501 switch (too_many_rename_candidates(num_create, options)) {
502 case 1:
0024a549 503 goto cleanup;
f31027c9
JH
504 case 2:
505 options->degraded_cc_to_c = 1;
506 skip_unmodified = 1;
507 break;
508 default:
509 break;
510 }
0024a549 511
3ac942d4
JK
512 if (options->show_rename_progress) {
513 progress = start_progress_delay(
754dbc43 514 _("Performing inexact rename detection"),
3ac942d4
JK
515 rename_dst_nr * rename_src_nr, 50, 1);
516 }
517
6d24ad97 518 mx = xcalloc(num_create * NUM_CANDIDATE_PER_DST, sizeof(*mx));
25d5ea41 519 for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
25d5ea41 520 struct diff_filespec *two = rename_dst[i].two;
6d24ad97
JH
521 struct diff_score *m;
522
25d5ea41 523 if (rename_dst[i].pair)
427dcb4b 524 continue; /* dealt with exact match already. */
6d24ad97
JH
525
526 m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST];
527 for (j = 0; j < NUM_CANDIDATE_PER_DST; j++)
528 m[j].dst = -1;
529
25d5ea41 530 for (j = 0; j < rename_src_nr; j++) {
e88d6bc6 531 struct diff_filespec *one = rename_src[j].p->one;
6d24ad97 532 struct diff_score this_src;
f31027c9
JH
533
534 if (skip_unmodified &&
535 diff_unmodified_pair(rename_src[j].p))
536 continue;
537
6d24ad97
JH
538 this_src.score = estimate_similarity(one, two,
539 minimum_score);
540 this_src.name_score = basename_same(one, two);
541 this_src.dst = i;
542 this_src.src = j;
543 record_if_better(m, &this_src);
809809bb
JH
544 /*
545 * Once we run estimate_similarity,
546 * We do not need the text anymore.
547 */
8ae92e63 548 diff_free_filespec_blob(one);
809809bb 549 diff_free_filespec_blob(two);
427dcb4b
JH
550 }
551 dst_cnt++;
3ac942d4 552 display_progress(progress, (i+1)*rename_src_nr);
427dcb4b 553 }
3ac942d4 554 stop_progress(&progress);
6d24ad97 555
427dcb4b 556 /* cost matrix sorted by most to least similar pair */
6d24ad97
JH
557 qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
558
0940e5f2
LT
559 rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
560 if (detect_rename == DIFF_DETECT_COPY)
561 rename_count += find_renames(mx, dst_cnt, minimum_score, 1);
427dcb4b 562 free(mx);
427dcb4b 563
15d061b4 564 cleanup:
427dcb4b 565 /* At this point, we have found some renames and copies and they
5098bafb 566 * are recorded in rename_dst. The original list is still in *q.
427dcb4b 567 */
9ca5df90 568 DIFF_QUEUE_CLEAR(&outq);
427dcb4b 569 for (i = 0; i < q->nr; i++) {
25d5ea41 570 struct diff_filepair *p = q->queue[i];
25d5ea41
JH
571 struct diff_filepair *pair_to_free = NULL;
572
d7c9bf22
MZ
573 if (DIFF_PAIR_UNMERGED(p)) {
574 diff_q(&outq, p);
575 }
576 else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
2cd68882
JH
577 /*
578 * Creation
579 *
580 * We would output this create record if it has
581 * not been turned into a rename/copy already.
582 */
583 struct diff_rename_dst *dst =
584 locate_rename_dst(p->two, 0);
585 if (dst && dst->pair) {
25d5ea41
JH
586 diff_q(&outq, dst->pair);
587 pair_to_free = p;
588 }
589 else
2cd68882
JH
590 /* no matching rename/copy source, so
591 * record this as a creation.
25d5ea41
JH
592 */
593 diff_q(&outq, p);
427dcb4b 594 }
2cd68882
JH
595 else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) {
596 /*
597 * Deletion
598 *
f345b0a0
JH
599 * We would output this delete record if:
600 *
601 * (1) this is a broken delete and the counterpart
602 * broken create remains in the output; or
5098bafb
JH
603 * (2) this is not a broken delete, and rename_dst
604 * does not have a rename/copy to move p->one->path
605 * out of existence.
f345b0a0
JH
606 *
607 * Otherwise, the counterpart broken create
608 * has been turned into a rename-edit; or
609 * delete did not have a matching create to
610 * begin with.
2cd68882 611 */
f345b0a0
JH
612 if (DIFF_PAIR_BROKEN(p)) {
613 /* broken delete */
614 struct diff_rename_dst *dst =
615 locate_rename_dst(p->one, 0);
616 if (dst && dst->pair)
617 /* counterpart is now rename/copy */
618 pair_to_free = p;
619 }
620 else {
64479711 621 if (p->one->rename_used)
f345b0a0
JH
622 /* this path remains */
623 pair_to_free = p;
624 }
2cd68882
JH
625
626 if (pair_to_free)
627 ;
628 else
629 diff_q(&outq, p);
630 }
25d5ea41 631 else if (!diff_unmodified_pair(p))
15d061b4 632 /* all the usual ones need to be kept */
25d5ea41 633 diff_q(&outq, p);
15d061b4
JH
634 else
635 /* no need to keep unmodified pairs */
636 pair_to_free = p;
637
226406f6
JH
638 if (pair_to_free)
639 diff_free_filepair(pair_to_free);
427dcb4b 640 }
25d5ea41 641 diff_debug_queue("done copying original", &outq);
427dcb4b 642
25d5ea41
JH
643 free(q->queue);
644 *q = outq;
645 diff_debug_queue("done collapsing", q);
427dcb4b 646
9fb88419
LT
647 for (i = 0; i < rename_dst_nr; i++)
648 free_filespec(rename_dst[i].two);
5098bafb 649
25d5ea41
JH
650 free(rename_dst);
651 rename_dst = NULL;
652 rename_dst_nr = rename_dst_alloc = 0;
653 free(rename_src);
654 rename_src = NULL;
655 rename_src_nr = rename_src_alloc = 0;
427dcb4b
JH
656 return;
657}