- struct diff_filepair *dp, *p = q->queue[i];
- if (!DIFF_FILE_VALID(p->one)) {
- /* creation or unmerged entries */
- dp = diff_queue(&outq, p->one, p->two);
- dp->rename_rank = 4;
- }
- else if (!DIFF_FILE_VALID(p->two)) {
- /* deletion */
- dp = diff_queue(&outq, p->one, p->two);
- dp->rename_rank = 2;
- }
- else {
- /* modification, or stay as is */
- dp = diff_queue(&outq, p->one, p->two);
- dp->rename_rank = 4;
- }
- free(p);
- }
- debug_queue("done copying original", &outq);
-
- /* Sort outq */
- qsort(outq.queue, outq.nr, sizeof(outq.queue[0]), rank_compare);
-
- debug_queue("done sorting", &outq);
-
- free(q->queue);
- q->nr = q->alloc = 0;
- q->queue = NULL;
-
- /* Copy it out to q, removing duplicates. */
- for (i = 0; i < outq.nr; i++) {
- struct diff_filepair *p = outq.queue[i];
- if (!DIFF_FILE_VALID(p->one)) {
- /* created or unmerged */
- if (p->two->xfrm_flags & RENAME_DST_MATCHED)
- ; /* rename/copy created it already */
- else
- diff_queue(q, p->one, p->two);
- }
- else if (!DIFF_FILE_VALID(p->two)) {
- /* deleted */
- if (p->one->xfrm_flags & RENAME_SRC_GONE)
- ; /* rename/copy deleted it already */
+ struct diff_filepair *p = q->queue[i];
+ struct diff_filepair *pair_to_free = NULL;
+
+ if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
+ /*
+ * Creation
+ *
+ * We would output this create record if it has
+ * not been turned into a rename/copy already.
+ */
+ struct diff_rename_dst *dst =
+ locate_rename_dst(p->two, 0);
+ if (dst && dst->pair) {
+ diff_q(&outq, dst->pair);
+ pair_to_free = p;
+ }