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