+void diff_setup(int reverse_diff_)
+{
+ reverse_diff = reverse_diff_;
+}
+
+struct diff_queue_struct diff_queued_diff;
+
+void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
+{
+ if (queue->alloc <= queue->nr) {
+ queue->alloc = alloc_nr(queue->alloc);
+ queue->queue = xrealloc(queue->queue,
+ sizeof(dp) * queue->alloc);
+ }
+ queue->queue[queue->nr++] = dp;
+}
+
+struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
+ struct diff_filespec *one,
+ struct diff_filespec *two)
+{
+ struct diff_filepair *dp = xmalloc(sizeof(*dp));
+ dp->one = one;
+ dp->two = two;
+ dp->xfrm_msg = NULL;
+ dp->orig_order = queue->nr;
+ dp->xfrm_work = 0;
+ diff_q(queue, dp);
+ return dp;
+}
+
+static void diff_flush_raw(struct diff_filepair *p)
+{
+ if (DIFF_PAIR_UNMERGED(p)) {
+ printf("U %s%c", p->one->path, line_termination);
+ return;
+ }
+ printf(":%06o %06o %s ",
+ p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
+ printf("%s%c%s%c%s%c",
+ sha1_to_hex(p->two->sha1), inter_name_termination,
+ p->one->path, inter_name_termination,
+ p->two->path, line_termination);
+}
+
+static void diff_flush_patch(struct diff_filepair *p)
+{
+ const char *name, *other;
+
+ name = p->one->path;
+ other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+ if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
+ (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
+ return; /* no tree diffs in patch format */
+
+ if (DIFF_PAIR_UNMERGED(p))
+ run_external_diff(name, NULL, NULL, NULL, NULL);
+ else
+ run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
+}
+
+static int uninteresting(struct diff_filepair *p)
+{
+ /* This function is written stricter than necessary to support
+ * the currently implemented transformers, but the idea is to
+ * let transformers to produce diff_filepairs any way they want,
+ * and filter and clean them up here before producing the output.
+ */
+ struct diff_filespec *one, *two;
+
+ if (DIFF_PAIR_UNMERGED(p))
+ return 0; /* unmerged is interesting */
+
+ one = p->one;
+ two = p->two;
+
+ /* deletion, addition, mode change and renames are all interesting. */
+ if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
+ (one->mode != two->mode) ||
+ strcmp(one->path, two->path))
+ return 0;
+
+ /* both are valid and point at the same path. that is, we are
+ * dealing with a change.
+ */
+ if (one->sha1_valid && two->sha1_valid &&
+ !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
+ return 1; /* no change */
+ if (!one->sha1_valid && !two->sha1_valid)
+ return 1; /* both look at the same file on the filesystem. */
+ return 0;
+}
+
+void diffcore_prune(void)
+{
+ /*
+ * Although rename/copy detection wants to have "no-change"
+ * entries fed into them, the downstream do not need to see
+ * them. This function removes such entries.
+ *
+ * The applications that use rename/copy should:
+ *
+ * (1) feed change and "no-change" entries via diff_queue().
+ * (2) call diffcore_rename, and any other future diffcore_xxx
+ * that would benefit by still having "no-change" entries.
+ * (3) call diffcore_prune
+ * (4) call other diffcore_xxx that do not need to see
+ * "no-change" entries.
+ */
+ struct diff_queue_struct *q = &diff_queued_diff;
+ struct diff_queue_struct outq;
+ int i;
+
+ outq.queue = NULL;
+ outq.nr = outq.alloc = 0;
+
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ if (!uninteresting(p))
+ diff_q(&outq, p);
+ else
+ diff_free_filepair(p);
+ }
+ free(q->queue);
+ *q = outq;
+ return;
+}
+
+static void diff_flush_one(struct diff_filepair *p)
+{
+ if (uninteresting(p))
+ return;
+ if (generate_patch)
+ diff_flush_patch(p);
+ else
+ diff_flush_raw(p);
+}
+
+int diff_queue_is_empty(void)
+{
+ struct diff_queue_struct *q = &diff_queued_diff;
+ int i;
+
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ if (!uninteresting(p))
+ return 0;
+ }
+ return 1;
+}
+
+void diff_flush(int diff_output_style)
+{
+ struct diff_queue_struct *q = &diff_queued_diff;
+ int i;
+
+ generate_patch = 0;
+ switch (diff_output_style) {
+ case DIFF_FORMAT_HUMAN:
+ line_termination = '\n';
+ inter_name_termination = '\t';
+ break;
+ case DIFF_FORMAT_MACHINE:
+ line_termination = inter_name_termination = 0;
+ break;
+ case DIFF_FORMAT_PATCH:
+ generate_patch = 1;
+ break;
+ }
+ for (i = 0; i < q->nr; i++)
+ diff_flush_one(q->queue[i]);
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ diff_free_filespec_data(p->one);
+ diff_free_filespec_data(p->two);
+ free(p->xfrm_msg);
+ free(p);
+ }
+ free(q->queue);
+ q->queue = NULL;
+ q->nr = q->alloc = 0;
+}
+