[PATCH] Diffcore updates.
[git/git.git] / diff-helper.c
CommitLineData
be3cfa85
JH
1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
a1df57ab 4#include <limits.h>
d1df5743
JH
5#include "cache.h"
6#include "strbuf.h"
7#include "diff.h"
8
915838c3 9static int detect_rename = 0;
57fe64a4 10static int diff_score_opt = 0;
057c7d30 11static const char *pickaxe = NULL;
81e50eab
JH
12static int diff_output_style = DIFF_FORMAT_PATCH;
13static int line_termination = '\n';
14static int inter_name_termination = '\t';
915838c3 15
81e50eab 16static int parse_diff_raw(char *buf1, char *buf2, char *buf3)
be3cfa85 17{
81e50eab
JH
18 char old_path[PATH_MAX];
19 unsigned char old_sha1[20], new_sha1[20];
20 char *ep;
21 char *cp = buf1;
22 int ch, old_mode, new_mode;
be3cfa85 23
81e50eab
JH
24 old_mode = new_mode = 0;
25 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
26 old_mode = (old_mode << 3) | (ch - '0');
27 cp++;
28 }
29 if (*cp++ != ' ')
30 return -1;
31 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
32 new_mode = (new_mode << 3) | (ch - '0');
be3cfa85
JH
33 cp++;
34 }
81e50eab 35 if (*cp++ != ' ')
d1df5743 36 return -1;
81e50eab 37 if (get_sha1_hex(cp, old_sha1))
d1df5743
JH
38 return -1;
39 cp += 40;
81e50eab 40 if (*cp++ != ' ')
d1df5743 41 return -1;
81e50eab 42 if (get_sha1_hex(cp, new_sha1))
91a6eaa0 43 return -1;
81e50eab
JH
44 cp += 40;
45 if (*cp++ != inter_name_termination)
46 return -1;
47 if (buf2)
48 cp = buf2;
49 ep = strchr(cp, inter_name_termination);
50 if (!ep)
51 return -1;
52 *ep++ = 0;
53 strcpy(old_path, cp);
54 diff_guif(old_mode, new_mode, old_sha1, new_sha1,
55 old_path, buf3 ? buf3 : ep);
d1df5743
JH
56 return 0;
57}
58
902b92e0 59static const char *diff_helper_usage =
52e95789 60 "git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
d1df5743 61
91a6eaa0 62int main(int ac, const char **av) {
81e50eab
JH
63 struct strbuf sb1, sb2, sb3;
64 int reverse_diff = 0;
d1df5743 65
81e50eab
JH
66 strbuf_init(&sb1);
67 strbuf_init(&sb2);
68 strbuf_init(&sb3);
d1df5743
JH
69
70 while (1 < ac && av[1][0] == '-') {
71 if (av[1][1] == 'R')
81e50eab 72 reverse_diff = 1;
d1df5743 73 else if (av[1][1] == 'z')
81e50eab 74 line_termination = inter_name_termination = 0;
427dcb4b 75 else if (av[1][1] == 'p') /* hidden from the help */
81e50eab
JH
76 diff_output_style = DIFF_FORMAT_HUMAN;
77 else if (av[1][1] == 'P') /* hidden from the help */
78 diff_output_style = DIFF_FORMAT_MACHINE;
57fe64a4 79 else if (av[1][1] == 'M') {
6b14d7fa 80 detect_rename = DIFF_DETECT_RENAME;
57fe64a4
JH
81 diff_score_opt = diff_scoreopt_parse(av[1]);
82 }
427dcb4b 83 else if (av[1][1] == 'C') {
6b14d7fa 84 detect_rename = DIFF_DETECT_COPY;
427dcb4b
JH
85 diff_score_opt = diff_scoreopt_parse(av[1]);
86 }
52e95789
JH
87 else if (av[1][1] == 'S') {
88 pickaxe = av[1] + 2;
89 }
d1df5743 90 else
902b92e0 91 usage(diff_helper_usage);
d1df5743
JH
92 ac--; av++;
93 }
94 /* the remaining parameters are paths patterns */
95
6b14d7fa 96 diff_setup(reverse_diff);
d1df5743 97 while (1) {
0b32ff0d 98 int status;
81e50eab
JH
99 read_line(&sb1, stdin, line_termination);
100 if (sb1.eof)
d1df5743 101 break;
81e50eab
JH
102 switch (sb1.buf[0]) {
103 case 'U':
104 diff_unmerge(sb1.buf + 2);
105 continue;
106 case ':':
107 break;
108 default:
109 goto unrecognized;
110 }
111 if (!line_termination) {
112 read_line(&sb2, stdin, line_termination);
113 if (sb2.eof)
114 break;
115 read_line(&sb3, stdin, line_termination);
116 if (sb3.eof)
117 break;
118 status = parse_diff_raw(sb1.buf+1, sb2.buf, sb3.buf);
119 }
120 else
121 status = parse_diff_raw(sb1.buf+1, NULL, NULL);
e11b29c5 122 if (status) {
81e50eab 123 unrecognized:
6b14d7fa 124 diff_flush(diff_output_style);
81e50eab 125 printf("%s%c", sb1.buf, line_termination);
e11b29c5 126 }
d1df5743 127 }
38c6f780 128 if (detect_rename)
6b14d7fa
JH
129 diffcore_rename(detect_rename, diff_score_opt);
130 diffcore_prune();
38c6f780 131 if (pickaxe)
6b14d7fa
JH
132 diffcore_pickaxe(pickaxe);
133 if (ac)
134 diffcore_pathspec(av + 1);
135 diff_flush(diff_output_style);
d1df5743
JH
136 return 0;
137}