+}
+#endif
+
+static void diff_resolve_rename_copy(void)
+{
+ int i, j;
+ struct diff_filepair *p, *pp;
+ struct diff_queue_struct *q = &diff_queued_diff;
+
+ /* This should not depend on the ordering of things. */
+
+ diff_debug_queue("resolve-rename-copy", q);
+
+ for (i = 0; i < q->nr; i++) {
+ p = q->queue[i];
+ p->status = 0; /* undecided */
+ if (DIFF_PAIR_UNMERGED(p))
+ p->status = 'U';
+ else if (!DIFF_FILE_VALID((p)->one))
+ p->status = 'N';
+ else if (!DIFF_FILE_VALID((p)->two)) {
+ /* Deletion record should be omitted if there
+ * are rename/copy entries using this one as
+ * the source. Then we can say one of them
+ * is a rename and the rest are copies.
+ */
+ p->status = 'D';
+ for (j = 0; j < q->nr; j++) {
+ pp = q->queue[j];
+ if (!strcmp(pp->one->path, p->one->path) &&
+ strcmp(pp->one->path, pp->two->path)) {
+ p->status = 'X';
+ break;
+ }
+ }
+ }
+ else if (DIFF_PAIR_TYPE_CHANGED(p))
+ p->status = 'T';
+
+ /* from this point on, we are dealing with a pair
+ * whose both sides are valid and of the same type, i.e.
+ * either in-place edit or rename/copy edit.
+ */
+ else if (strcmp(p->one->path, p->two->path)) {
+ /* See if there is somebody else anywhere that
+ * will keep the path (either modified or
+ * unmodified). If so, we have to be a copy,
+ * not a rename. In addition, if there is
+ * some other rename or copy that comes later
+ * than us that uses the same source, we
+ * have to be a copy, not a rename.
+ */
+ for (j = 0; j < q->nr; j++) {
+ pp = q->queue[j];
+ if (strcmp(pp->one->path, p->one->path))
+ continue;
+ if (!strcmp(pp->one->path, pp->two->path)) {
+ if (DIFF_FILE_VALID(pp->two)) {
+ /* non-delete */
+ p->status = 'C';
+ break;
+ }
+ continue;
+ }
+ /* pp is a rename/copy ... */
+ if (i < j) {
+ /* ... and comes later than us */
+ p->status = 'C';
+ break;
+ }
+ }
+ if (!p->status)
+ p->status = 'R';
+ }
+ else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
+ p->one->mode != p->two->mode)
+ p->status = 'M';
+ else
+ /* this is a "no-change" entry */
+ p->status = 'X';
+ }
+ diff_debug_queue("resolve-rename-copy done", q);
+}
+
+void diff_flush(int diff_output_style, int resolve_rename_copy)
+{
+ struct diff_queue_struct *q = &diff_queued_diff;
+ int i;
+ int line_termination = '\n';
+ int inter_name_termination = '\t';
+
+ if (diff_output_style == DIFF_FORMAT_MACHINE)
+ line_termination = inter_name_termination = 0;
+ if (resolve_rename_copy)
+ diff_resolve_rename_copy();
+
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
+ (p->status == 'X'))
+ continue;
+ if (p->status == 0)
+ die("internal error in diff-resolve-rename-copy");
+ switch (diff_output_style) {
+ case DIFF_FORMAT_PATCH:
+ diff_flush_patch(p);
+ break;
+ case DIFF_FORMAT_HUMAN:
+ case DIFF_FORMAT_MACHINE:
+ diff_flush_raw(p, line_termination,
+ inter_name_termination);
+ break;
+ }
+ }
+ for (i = 0; i < q->nr; i++)
+ diff_free_filepair(q->queue[i]);