[PATCH] Diffcore updates.
[git.git] / diff-helper.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include <limits.h>
5 #include "cache.h"
6 #include "strbuf.h"
7 #include "diff.h"
8
9 static int detect_rename = 0;
10 static int diff_score_opt = 0;
11 static const char *pickaxe = NULL;
12 static int diff_output_style = DIFF_FORMAT_PATCH;
13 static int line_termination = '\n';
14 static int inter_name_termination = '\t';
15
16 static int parse_diff_raw(char *buf1, char *buf2, char *buf3)
17 {
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;
23
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');
33                 cp++;
34         }
35         if (*cp++ != ' ')
36                 return -1;
37         if (get_sha1_hex(cp, old_sha1))
38                 return -1;
39         cp += 40;
40         if (*cp++ != ' ')
41                 return -1;
42         if (get_sha1_hex(cp, new_sha1))
43                 return -1;
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);
56         return 0;
57 }
58
59 static const char *diff_helper_usage =
60         "git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
61
62 int main(int ac, const char **av) {
63         struct strbuf sb1, sb2, sb3;
64         int reverse_diff = 0;
65
66         strbuf_init(&sb1);
67         strbuf_init(&sb2);
68         strbuf_init(&sb3);
69
70         while (1 < ac && av[1][0] == '-') {
71                 if (av[1][1] == 'R')
72                         reverse_diff = 1;
73                 else if (av[1][1] == 'z')
74                         line_termination = inter_name_termination = 0;
75                 else if (av[1][1] == 'p') /* hidden from the help */
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;
79                 else if (av[1][1] == 'M') {
80                         detect_rename = DIFF_DETECT_RENAME;
81                         diff_score_opt = diff_scoreopt_parse(av[1]);
82                 }
83                 else if (av[1][1] == 'C') {
84                         detect_rename = DIFF_DETECT_COPY;
85                         diff_score_opt = diff_scoreopt_parse(av[1]);
86                 }
87                 else if (av[1][1] == 'S') {
88                         pickaxe = av[1] + 2;
89                 }
90                 else
91                         usage(diff_helper_usage);
92                 ac--; av++;
93         }
94         /* the remaining parameters are paths patterns */
95
96         diff_setup(reverse_diff);
97         while (1) {
98                 int status;
99                 read_line(&sb1, stdin, line_termination);
100                 if (sb1.eof)
101                         break;
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);
122                 if (status) {
123                 unrecognized:
124                         diff_flush(diff_output_style);
125                         printf("%s%c", sb1.buf, line_termination);
126                 }
127         }
128         if (detect_rename)
129                 diffcore_rename(detect_rename, diff_score_opt);
130         diffcore_prune();
131         if (pickaxe)
132                 diffcore_pickaxe(pickaxe);
133         if (ac)
134                 diffcore_pathspec(av + 1);
135         diff_flush(diff_output_style);
136         return 0;
137 }