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
9static int detect_rename = 0;
10static int diff_score_opt = 0;
11static int generate_patch = 1;
12static const char *pickaxe = NULL;
13
14static int parse_oneside_change(const char *cp, int *mode,
15 unsigned char *sha1, char *path)
16{
17 int ch, m;
18
19 m = 0;
20 while ((ch = *cp) && '0' <= ch && ch <= '7') {
21 m = (m << 3) | (ch - '0');
22 cp++;
23 }
24 *mode = m;
25 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
26 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
27 return -1;
28 cp += 6;
29 if (get_sha1_hex(cp, sha1))
30 return -1;
31 cp += 40;
32 if ((*cp != '\t') && *cp != ' ')
33 return -1;
34 strcpy(path, ++cp);
35 return 0;
36}
37
38static int parse_diff_raw_output(const char *buf)
39{
40 char path[PATH_MAX];
41 unsigned char old_sha1[20], new_sha1[20];
42 const char *cp = buf;
43 int ch, old_mode, new_mode;
44
45 switch (*cp++) {
46 case 'U':
47 diff_unmerge(cp + 1);
48 break;
49 case '+':
50 if (parse_oneside_change(cp, &new_mode, new_sha1, path))
51 return -1;
52 diff_addremove('+', new_mode, new_sha1, path, NULL);
53 break;
54 case '-':
55 if (parse_oneside_change(cp, &old_mode, old_sha1, path))
56 return -1;
57 diff_addremove('-', old_mode, old_sha1, path, NULL);
58 break;
59 case '*':
60 old_mode = new_mode = 0;
61 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
62 old_mode = (old_mode << 3) | (ch - '0');
63 cp++;
64 }
65 if (strncmp(cp, "->", 2))
66 return -1;
67 cp += 2;
68 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
69 new_mode = (new_mode << 3) | (ch - '0');
70 cp++;
71 }
72 if (strncmp(cp, "\tblob\t", 6) && strncmp(cp, " blob ", 6) &&
73 strncmp(cp, "\ttree\t", 6) && strncmp(cp, " tree ", 6))
74 return -1;
75 cp += 6;
76 if (get_sha1_hex(cp, old_sha1))
77 return -1;
78 cp += 40;
79 if (strncmp(cp, "->", 2))
80 return -1;
81 cp += 2;
82 if (get_sha1_hex(cp, new_sha1))
83 return -1;
84 cp += 40;
85 if ((*cp != '\t') && *cp != ' ')
86 return -1;
87 strcpy(path, ++cp);
88 diff_change(old_mode, new_mode, old_sha1, new_sha1, path, NULL);
89 break;
90 default:
91 return -1;
92 }
93 return 0;
94}
95
96static const char *diff_helper_usage =
97 "git-diff-helper [-z] [-R] [-M] [-C] [-S<string>] paths...";
98
99int main(int ac, const char **av) {
100 struct strbuf sb;
101 int reverse = 0;
102 int line_termination = '\n';
103
104 strbuf_init(&sb);
105
106 while (1 < ac && av[1][0] == '-') {
107 if (av[1][1] == 'R')
108 reverse = 1;
109 else if (av[1][1] == 'z')
110 line_termination = 0;
111 else if (av[1][1] == 'p') /* hidden from the help */
112 generate_patch = 0;
113 else if (av[1][1] == 'M') {
114 detect_rename = 1;
115 diff_score_opt = diff_scoreopt_parse(av[1]);
116 }
117 else if (av[1][1] == 'C') {
118 detect_rename = 2;
119 diff_score_opt = diff_scoreopt_parse(av[1]);
120 }
121 else if (av[1][1] == 'S') {
122 pickaxe = av[1] + 2;
123 }
124 else
125 usage(diff_helper_usage);
126 ac--; av++;
127 }
128 /* the remaining parameters are paths patterns */
129
130 diff_setup(detect_rename, diff_score_opt, pickaxe,
131 reverse, (generate_patch ? -1 : line_termination),
132 av+1, ac-1);
133
134 while (1) {
135 int status;
136 read_line(&sb, stdin, line_termination);
137 if (sb.eof)
138 break;
139 status = parse_diff_raw_output(sb.buf);
140 if (status) {
141 diff_flush();
142 printf("%s%c", sb.buf, line_termination);
143 }
144 }
145
146 diff_flush();
147 return 0;
148}