+ if (DIFF_PAIR_UNMERGED(p))
+ run_diff(name, NULL, NULL, NULL, NULL);
+ else
+ run_diff(name, other, p->one, p->two, msg);
+}
+
+int diff_queue_is_empty(void)
+{
+ struct diff_queue_struct *q = &diff_queued_diff;
+ int i;
+ for (i = 0; i < q->nr; i++)
+ if (!diff_unmodified_pair(q->queue[i]))
+ return 0;
+ return 1;
+}
+
+#if DIFF_DEBUG
+void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
+{
+ fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
+ x, one ? : "",
+ s->path,
+ DIFF_FILE_VALID(s) ? "valid" : "invalid",
+ s->mode,
+ s->sha1_valid ? sha1_to_hex(s->sha1) : "");
+ fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
+ x, one ? : "",
+ s->size, s->xfrm_flags);
+}
+
+void diff_debug_filepair(const struct diff_filepair *p, int i)
+{
+ diff_debug_filespec(p->one, i, "one");
+ diff_debug_filespec(p->two, i, "two");
+ fprintf(stderr, "score %d, status %c source_stays %d\n",
+ p->score, p->status ? : '?', p->source_stays);
+}
+
+void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
+{
+ int i;
+ if (msg)
+ fprintf(stderr, "%s\n", msg);
+ fprintf(stderr, "q->nr = %d\n", q->nr);
+ for (i = 0; i < q->nr; i++) {
+ struct diff_filepair *p = q->queue[i];
+ diff_debug_filepair(p, i);
+ }
+}
+#endif
+
+static void diff_resolve_rename_copy(void)
+{
+ int i, j;
+ struct diff_filepair *p, *pp;
+ struct diff_queue_struct *q = &diff_queued_diff;
+
+ 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)) {
+ /* Deleted entry may have been picked up by
+ * another rename-copy entry. So we scan the
+ * queue and if we find one that uses us as the
+ * source we do not say delete for this entry.
+ */
+ for (j = 0; j < q->nr; j++) {
+ pp = q->queue[j];
+ if (!strcmp(p->one->path, pp->one->path) &&
+ DIFF_PAIR_RENAME(pp)) {
+ /* rename/copy are always valid
+ * so we do not say DIFF_FILE_VALID()
+ * on pp->one and pp->two.
+ */
+ p->status = 'X';
+ break;
+ }
+ }
+ if (!p->status)
+ p->status = 'D';
+ }
+ 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 (DIFF_PAIR_RENAME(p)) {
+ if (p->source_stays) {
+ p->status = 'C';
+ continue;
+ }
+ /* See if there is some other filepair that
+ * copies from the same source as us. If so
+ * we are a copy. Otherwise we are a rename.
+ */
+ for (j = i + 1; j < q->nr; j++) {
+ pp = q->queue[j];
+ if (strcmp(pp->one->path, p->one->path))
+ continue; /* not us */
+ if (!DIFF_PAIR_RENAME(pp))
+ continue; /* not a rename/copy */
+ /* pp is a rename/copy from the same source */
+ 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.
+ * should not happen anymore.
+ * p->status = 'X';
+ */
+ die("internal error in diffcore: unmodified entry remains");
+ }
+ 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]);
+ free(q->queue);
+ q->queue = NULL;
+ q->nr = q->alloc = 0;
+}
+
+void diffcore_std(const char **paths,
+ int detect_rename, int rename_score,
+ const char *pickaxe, int pickaxe_opts)
+{
+ if (paths && paths[0])
+ diffcore_pathspec(paths);
+ if (detect_rename)
+ diffcore_rename(detect_rename, rename_score);
+ if (pickaxe)
+ diffcore_pickaxe(pickaxe, pickaxe_opts);
+}
+
+void diff_addremove(int addremove, unsigned mode,
+ const unsigned char *sha1,
+ const char *base, const char *path)
+{
+ char concatpath[PATH_MAX];
+ struct diff_filespec *one, *two;
+
+ /* This may look odd, but it is a preparation for
+ * feeding "there are unchanged files which should
+ * not produce diffs, but when you are doing copy
+ * detection you would need them, so here they are"
+ * entries to the diff-core. They will be prefixed
+ * with something like '=' or '*' (I haven't decided
+ * which but should not make any difference).
+ * Feeding the same new and old to diff_change()
+ * also has the same effect.
+ * Before the final output happens, they are pruned after
+ * merged into rename/copy pairs as appropriate.
+ */
+ if (reverse_diff)
+ addremove = (addremove == '+' ? '-' :
+ addremove == '-' ? '+' : addremove);
+
+ if (!path) path = "";
+ sprintf(concatpath, "%s%s", base, path);
+ one = alloc_filespec(concatpath);
+ two = alloc_filespec(concatpath);
+
+ if (addremove != '+')
+ fill_filespec(one, sha1, mode);
+ if (addremove != '-')
+ fill_filespec(two, sha1, mode);
+
+ diff_queue(&diff_queued_diff, one, two);
+}
+
+void diff_helper_input(unsigned old_mode,
+ unsigned new_mode,
+ const unsigned char *old_sha1,
+ const unsigned char *new_sha1,
+ const char *old_path,
+ int status,
+ int score,
+ const char *new_path)
+{
+ struct diff_filespec *one, *two;
+ struct diff_filepair *dp;
+
+ one = alloc_filespec(old_path);
+ two = alloc_filespec(new_path);
+ if (old_mode)
+ fill_filespec(one, old_sha1, old_mode);
+ if (new_mode)
+ fill_filespec(two, new_sha1, new_mode);
+ dp = diff_queue(&diff_queued_diff, one, two);
+ dp->score = score * MAX_SCORE / 100;
+ dp->status = status;
+}
+
+void diff_change(unsigned old_mode, unsigned new_mode,
+ const unsigned char *old_sha1,
+ const unsigned char *new_sha1,
+ const char *base, const char *path)
+{
+ char concatpath[PATH_MAX];
+ struct diff_filespec *one, *two;
+
+ if (reverse_diff) {
+ unsigned tmp;
+ const unsigned char *tmp_c;
+ tmp = old_mode; old_mode = new_mode; new_mode = tmp;
+ tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
+ }
+ if (!path) path = "";
+ sprintf(concatpath, "%s%s", base, path);
+ one = alloc_filespec(concatpath);
+ two = alloc_filespec(concatpath);
+ fill_filespec(one, old_sha1, old_mode);
+ fill_filespec(two, new_sha1, new_mode);
+
+ diff_queue(&diff_queued_diff, one, two);
+}
+
+void diff_unmerge(const char *path)
+{
+ struct diff_filespec *one, *two;
+ one = alloc_filespec(path);
+ two = alloc_filespec(path);
+ diff_queue(&diff_queued_diff, one, two);