+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->score = 0;
+ dp->orig_order = queue->nr;
+ dp->rename_rank = 0;
+ diff_q(queue, dp);
+ return dp;
+}
+
+static void diff_flush_raw(struct diff_filepair *p,
+ int line_termination,
+ int inter_name_termination)
+{
+ int two_paths;
+ char status[10];
+ switch (p->status) {
+ case 'C': case 'R':
+ two_paths = 1;
+ sprintf(status, "%c%1d", p->status, p->score);
+ break;
+ default:
+ two_paths = 0;
+ status[0] = p->status;
+ status[1] = 0;
+ break;
+ }
+ printf(":%06o %06o %s ",
+ p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
+ printf("%s %s%c%s",
+ sha1_to_hex(p->two->sha1),
+ status,
+ inter_name_termination,
+ p->one->path);
+ if (two_paths)
+ printf("%c%s", inter_name_termination, p->two->path);
+ putchar(line_termination);
+}
+
+int diff_unmodified_pair(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;
+}
+
+static void diff_flush_patch(struct diff_filepair *p)
+{
+ const char *name, *other;
+ char msg_[PATH_MAX*2+200], *msg;
+
+ if (diff_unmodified_pair(p))
+ return;
+
+ 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 */
+
+ switch (p->status) {
+ case 'C':
+ sprintf(msg_,
+ "similarity index %d%%\n"
+ "copy from %s\n"
+ "copy to %s\n",
+ (int)(0.5 + p->score * 100.0/MAX_SCORE),
+ p->one->path, p->two->path);
+ msg = msg_;
+ break;
+ case 'R':
+ sprintf(msg_,
+ "similarity index %d%%\n"
+ "rename old %s\n"
+ "rename new %s\n",
+ (int)(0.5 + p->score * 100.0/MAX_SCORE),
+ p->one->path, p->two->path);
+ msg = msg_;
+ break;
+ default:
+ msg = NULL;
+ }
+
+ if (DIFF_PAIR_UNMERGED(p))
+ run_external_diff(name, NULL, NULL, NULL, NULL);
+ else
+ run_external_diff(name, other, p->one, p->two, msg);
+}
+
+int diff_needs_to_stay(struct diff_queue_struct *q, int i,
+ struct diff_filespec *it)
+{
+ /* If it will be used in later entry (either stay or used
+ * as the source of rename/copy), we need to copy, not rename.
+ */
+ while (i < q->nr) {
+ struct diff_filepair *p = q->queue[i++];
+ if (!DIFF_FILE_VALID(p->two))
+ continue; /* removed is fine */
+ if (strcmp(p->one->path, it->path))
+ continue; /* not relevant */
+
+ /* p has its src set to *it and it is not a delete;
+ * it will be used for in-place change, rename/copy,
+ * or just stays there. We cannot rename it out.
+ */
+ return 1;
+ }
+ return 0;
+}
+
+int diff_queue_is_empty(void)
+{
+ struct diff_queue_struct *q = &diff_queued_diff;
+ return q->nr == 0;
+}
+
+static void diff_resolve_rename_copy(void)
+{
+ int i;
+ struct diff_queue_struct *q = &diff_queued_diff;
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ p->status = 0;
+ 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)) {
+ /* maybe earlier one said 'R', meaning
+ * it will take it, in which case we do
+ * not need to keep 'D'.
+ */
+ int j;
+ for (j = 0; j < i; j++) {
+ struct diff_filepair *pp = q->queue[j];
+ if (pp->status == 'R' &&
+ !strcmp(pp->one->path, p->one->path))
+ break;
+ }
+ if (j < i)
+ continue;
+ p->status = 'D';
+ }
+ else if (strcmp(p->one->path, p->two->path)) {
+ /* This is rename or copy. Which one is it? */
+ if (diff_needs_to_stay(q, i+1, p->one))
+ p->status = 'C';
+ else
+ p->status = 'R';
+ }
+ else if (memcmp(p->one->sha1, p->two->sha1, 20))
+ p->status = 'M';
+ else {
+ /* we do not need this one */
+ p->status = 0;
+ }
+ }
+}
+
+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 (p->status == 0)
+ continue;
+ 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++) {
+ struct diff_filepair *p = q->queue[i];
+ diff_free_filespec_data(p->one);
+ diff_free_filespec_data(p->two);
+ free(p);
+ }
+ free(q->queue);
+ q->queue = NULL;
+ q->nr = q->alloc = 0;
+}
+