+int diff_scoreopt_parse(const char *opt)
+{
+ int diglen, num, scale, i;
+ if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
+ return -1; /* that is not a -M nor -C option */
+ diglen = strspn(opt+2, "0123456789");
+ if (diglen == 0 || strlen(opt+2) != diglen)
+ return 0; /* use default */
+ sscanf(opt+2, "%d", &num);
+ for (i = 0, scale = 1; i < diglen; i++)
+ scale *= 10;
+
+ /* user says num divided by scale and we say internally that
+ * is MAX_SCORE * num / scale.
+ */
+ return MAX_SCORE * num / scale;
+}
+
+void diff_setup(int detect_rename_, int minimum_score_,
+ const char *pickaxe_,
+ int reverse_diff_, int diff_raw_output_,
+ const char **pathspec_, int speccnt_)
+{
+ detect_rename = detect_rename_;
+ reverse_diff = reverse_diff_;
+ pathspec = pathspec_;
+ diff_raw_output = diff_raw_output_;
+ speccnt = speccnt_;
+ minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
+ pickaxe = pickaxe_;
+}
+
+static struct diff_queue_struct queued_diff;
+
+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 = 0;
+ dp->orig_order = queue->nr;
+ dp->xfrm_work = 0;
+ 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;
+ return dp;
+}
+
+static const char *git_object_type(unsigned mode)
+{
+ return S_ISDIR(mode) ? "tree" : "blob";
+}
+
+static void diff_flush_raw(struct diff_filepair *p)
+{
+ struct diff_filespec *it;
+ int addremove;
+
+ /* raw output does not have a way to express rename nor copy */
+ if (strcmp(p->one->path, p->two->path))
+ return;
+
+ if (p->one->file_valid && p->two->file_valid) {
+ char hex[41];
+ strcpy(hex, sha1_to_hex(p->one->sha1));
+ printf("*%06o->%06o %s %s->%s %s%c",
+ p->one->mode, p->two->mode,
+ git_object_type(p->one->mode),
+ hex, sha1_to_hex(p->two->sha1),
+ p->one->path, diff_raw_output);
+ return;
+ }
+
+ if (p->one->file_valid) {
+ it = p->one;
+ addremove = '-';
+ } else {
+ it = p->two;
+ addremove = '+';
+ }
+
+ printf("%c%06o %s %s %s%c",
+ addremove,
+ it->mode, git_object_type(it->mode),
+ sha1_to_hex(it->sha1), it->path, diff_raw_output);
+}
+
+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 ((p->one->file_valid && S_ISDIR(p->one->mode)) ||
+ (p->two->file_valid && S_ISDIR(p->two->mode)))
+ return; /* no tree diffs in patch format */
+
+ run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
+}
+
+static int identical(struct diff_filespec *one, struct diff_filespec *two)
+{
+ /* 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.
+ */
+
+ if (!one->file_valid && !two->file_valid)
+ return 1; /* not interesting */
+
+ /* deletion, addition, mode change and renames are all interesting. */
+ if ((one->file_valid != two->file_valid) || (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_one(struct diff_filepair *p)
+{
+ if (identical(p->one, p->two))
+ return;
+ if (0 <= diff_raw_output)
+ diff_flush_raw(p);
+ else
+ diff_flush_patch(p);
+}
+
+void diff_flush(void)
+{
+ struct diff_queue_struct *q = &queued_diff;
+ int i;
+
+ if (detect_rename)
+ diff_detect_rename(q, detect_rename, minimum_score);
+ if (pickaxe)
+ diff_pickaxe(q, pickaxe);
+ 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;
+}
+