- return 1;
-}
-
-static int parse_num(const char **cp_p)
-{
- unsigned long num, scale;
- int ch, dot;
- const char *cp = *cp_p;
-
- num = 0;
- scale = 1;
- dot = 0;
- for(;;) {
- ch = *cp;
- if ( !dot && ch == '.' ) {
- scale = 1;
- dot = 1;
- } else if ( ch == '%' ) {
- scale = dot ? scale*100 : 100;
- cp++; /* % is always at the end */
- break;
- } else if ( ch >= '0' && ch <= '9' ) {
- if ( scale < 100000 ) {
- scale *= 10;
- num = (num*10) + (ch-'0');
- }
- } else {
- break;
- }
- cp++;
- }
- *cp_p = cp;
-
- /* user says num divided by scale and we say internally that
- * is MAX_SCORE * num / scale.
- */
- return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
-}
-
-int diff_scoreopt_parse(const char *opt)
-{
- int opt1, opt2, cmd;
-
- if (*opt++ != '-')
- return -1;
- cmd = *opt++;
- if (cmd != 'M' && cmd != 'C' && cmd != 'B')
- return -1; /* that is not a -M, -C nor -B option */
-
- opt1 = parse_num(&opt);
- if (cmd != 'B')
- opt2 = 0;
- else {
- if (*opt == 0)
- opt2 = 0;
- else if (*opt != '/')
- return -1; /* we expect -B80/99 or -B80 */
- else {
- opt++;
- opt2 = parse_num(&opt);
- }
- }
- if (*opt != 0)
- return -1;
- return opt1 | (opt2 << 16);
-}
-
-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->status = 0;
- dp->source_stays = 0;
- dp->broken_pair = 0;
- if (queue)
- diff_q(queue, dp);
- return dp;
-}
-
-void diff_free_filepair(struct diff_filepair *p)
-{
- diff_free_filespec_data(p->one);
- diff_free_filespec_data(p->two);
- free(p->one);
- free(p->two);
- free(p);
-}
-
-/* This is different from find_unique_abbrev() in that
- * it stuffs the result with dots for alignment.
- */
-const char *diff_unique_abbrev(const unsigned char *sha1, int len)
-{
- int abblen;
- const char *abbrev;
- if (len == 40)
- return sha1_to_hex(sha1);
-
- abbrev = find_unique_abbrev(sha1, len);
- if (!abbrev)
- return sha1_to_hex(sha1);
- abblen = strlen(abbrev);
- if (abblen < 37) {
- static char hex[41];
- if (len < abblen && abblen <= len + 2)
- sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
- else
- sprintf(hex, "%s...", abbrev);
- return hex;
- }
- return sha1_to_hex(sha1);
-}
-
-static void diff_flush_raw(struct diff_filepair *p,
- int line_termination,
- int inter_name_termination,
- struct diff_options *options,
- int output_format)
-{
- int two_paths;
- char status[10];
- int abbrev = options->abbrev;
- const char *path_one, *path_two;
-
- path_one = p->one->path;
- path_two = p->two->path;
- if (line_termination) {
- path_one = quote_one(path_one);
- path_two = quote_one(path_two);
- }
-
- if (p->score)
- sprintf(status, "%c%03d", p->status,
- (int)(0.5 + p->score * 100.0/MAX_SCORE));
- else {
- status[0] = p->status;
- status[1] = 0;
- }
- switch (p->status) {
- case DIFF_STATUS_COPIED:
- case DIFF_STATUS_RENAMED:
- two_paths = 1;
- break;
- case DIFF_STATUS_ADDED:
- case DIFF_STATUS_DELETED:
- two_paths = 0;
- break;
- default:
- two_paths = 0;
- break;
- }
- if (output_format != DIFF_FORMAT_NAME_STATUS) {
- printf(":%06o %06o %s ",
- p->one->mode, p->two->mode,
- diff_unique_abbrev(p->one->sha1, abbrev));
- printf("%s ",
- diff_unique_abbrev(p->two->sha1, abbrev));
- }
- printf("%s%c%s", status, inter_name_termination, path_one);
- if (two_paths)
- printf("%c%s", inter_name_termination, path_two);
- putchar(line_termination);
- if (path_one != p->one->path)
- free((void*)path_one);
- if (path_two != p->two->path)
- free((void*)path_two);
-}
-
-static void diff_flush_name(struct diff_filepair *p,
- int inter_name_termination,
- int line_termination)
-{
- char *path = p->two->path;
-
- if (line_termination)
- path = quote_one(p->two->path);
- else
- path = p->two->path;
- printf("%s%c", path, line_termination);
- if (p->two->path != path)
- free(path);
-}
-
-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;