1/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "strbuf.h"
6#include "diff.h"
7
8static const char *pickaxe = NULL;
9static int pickaxe_opts = 0;
10static int line_termination = '\n';
11static int inter_name_termination = '\t';
12
13static const char *diff_helper_usage =
14 "git-diff-helper [-z] [-S<string>] paths...";
15
16int main(int ac, const char **av) {
17 struct strbuf sb;
18 const char *garbage_flush_format;
19
20 strbuf_init(&sb);
21
22 while (1 < ac && av[1][0] == '-') {
23 if (av[1][1] == 'z')
24 line_termination = inter_name_termination = 0;
25 else if (av[1][1] == 'S') {
26 pickaxe = av[1] + 2;
27 }
28 else if (!strcmp(av[1], "--pickaxe-all"))
29 pickaxe_opts = DIFF_PICKAXE_ALL;
30 else
31 usage(diff_helper_usage);
32 ac--; av++;
33 }
34 garbage_flush_format = (line_termination == 0) ? "%s" : "%s\n";
35
36 /* the remaining parameters are paths patterns */
37
38 diff_setup(0);
39 while (1) {
40 unsigned old_mode, new_mode;
41 unsigned char old_sha1[20], new_sha1[20];
42 char old_path[PATH_MAX];
43 int status, score, two_paths;
44 char new_path[PATH_MAX];
45
46 int ch;
47 char *cp, *ep;
48
49 read_line(&sb, stdin, line_termination);
50 if (sb.eof)
51 break;
52 switch (sb.buf[0]) {
53 case ':':
54 /* parse the first part up to the status */
55 cp = sb.buf + 1;
56 old_mode = new_mode = 0;
57 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
58 old_mode = (old_mode << 3) | (ch - '0');
59 cp++;
60 }
61 if (*cp++ != ' ')
62 break;
63 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
64 new_mode = (new_mode << 3) | (ch - '0');
65 cp++;
66 }
67 if (*cp++ != ' ')
68 break;
69 if (get_sha1_hex(cp, old_sha1))
70 break;
71 cp += 40;
72 if (*cp++ != ' ')
73 break;
74 if (get_sha1_hex(cp, new_sha1))
75 break;
76 cp += 40;
77 if (*cp++ != ' ')
78 break;
79 status = *cp++;
80 if (!strchr("MCRNDU", status))
81 break;
82 two_paths = score = 0;
83 if (status == 'R' || status == 'C')
84 two_paths = 1;
85
86 /* pick up score if exists */
87 if (sscanf(cp, "%d", &score) != 1)
88 score = 0;
89 cp = strchr(cp,
90 inter_name_termination);
91 if (!cp)
92 break;
93 if (*cp++ != inter_name_termination)
94 break;
95
96 /* first pathname */
97 if (!line_termination) {
98 read_line(&sb, stdin, line_termination);
99 if (sb.eof)
100 break;
101 strcpy(old_path, sb.buf);
102 }
103 else if (!two_paths)
104 strcpy(old_path, cp);
105 else {
106 ep = strchr(cp, inter_name_termination);
107 if (!ep)
108 break;
109 strncpy(old_path, cp, ep-cp);
110 old_path[ep-cp] = 0;
111 cp = ep + 1;
112 }
113
114 /* second pathname */
115 if (!two_paths)
116 strcpy(new_path, old_path);
117 else {
118 if (!line_termination) {
119 read_line(&sb, stdin,
120 line_termination);
121 if (sb.eof)
122 break;
123 strcpy(new_path, sb.buf);
124 }
125 else
126 strcpy(new_path, cp);
127 }
128 diff_helper_input(old_mode, new_mode,
129 old_sha1, new_sha1,
130 old_path, status, score,
131 new_path);
132 continue;
133 }
134 if (1 < ac)
135 diffcore_pathspec(av + 1);
136 if (pickaxe)
137 diffcore_pickaxe(pickaxe, pickaxe_opts);
138 diff_flush(DIFF_FORMAT_PATCH, 0);
139 printf(garbage_flush_format, sb.buf);
140 }
141 if (1 < ac)
142 diffcore_pathspec(av + 1);
143 if (pickaxe)
144 diffcore_pickaxe(pickaxe, pickaxe_opts);
145 diff_flush(DIFF_FORMAT_PATCH, 0);
146 return 0;
147}