[PATCH] Fix "git-rev-list" revision range parsing
[git] / diff-helper.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "strbuf.h"
6 #include "diff.h"
7
8 static const char *pickaxe = NULL;
9 static int pickaxe_opts = 0;
10 static const char *orderfile = NULL;
11 static const char *diff_filter = NULL;
12 static int line_termination = '\n';
13 static int inter_name_termination = '\t';
14
15 static void flush_them(int ac, const char **av)
16 {
17         diffcore_std_no_resolve(av + 1,
18                                 pickaxe, pickaxe_opts,
19                                 orderfile, diff_filter);
20         diff_flush(DIFF_FORMAT_PATCH, '\n');
21 }
22
23 static const char diff_helper_usage[] =
24 "git-diff-helper [-z] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
25
26 int main(int ac, const char **av) {
27         struct strbuf sb;
28         const char *garbage_flush_format;
29
30         strbuf_init(&sb);
31
32         while (1 < ac && av[1][0] == '-') {
33                 if (av[1][1] == 'z')
34                         line_termination = inter_name_termination = 0;
35                 else if (av[1][1] == 'S') {
36                         pickaxe = av[1] + 2;
37                 }
38                 else if (!strcmp(av[1], "--pickaxe-all"))
39                         pickaxe_opts = DIFF_PICKAXE_ALL;
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;
44                 else
45                         usage(diff_helper_usage);
46                 ac--; av++;
47         }
48         garbage_flush_format = (line_termination == 0) ? "%s" : "%s\n";
49
50         /* the remaining parameters are paths patterns */
51
52         diff_setup(0);
53         while (1) {
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)
65                         break;
66                 switch (sb.buf[0]) {
67                 case ':':
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++ != ' ')
76                                 break;
77                         while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
78                                 new_mode = (new_mode << 3) | (ch - '0');
79                                 cp++;
80                         }
81                         if (*cp++ != ' ')
82                                 break;
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("AMCRDU", status))
95                                 break;
96                         two_paths = score = 0;
97                         if (status == DIFF_STATUS_RENAMED ||
98                             status == DIFF_STATUS_COPIED)
99                                 two_paths = 1;
100
101                         /* pick up score if exists */
102                         if (sscanf(cp, "%d", &score) != 1)
103                                 score = 0;
104                         cp = strchr(cp,
105                                     inter_name_termination);
106                         if (!cp)
107                                 break;
108                         if (*cp++ != inter_name_termination)
109                                 break;
110
111                         /* first pathname */
112                         if (!line_termination) {
113                                 read_line(&sb, stdin, line_termination);
114                                 if (sb.eof)
115                                         break;
116                                 strcpy(old_path, sb.buf);
117                         }
118                         else if (!two_paths)
119                                 strcpy(old_path, cp);
120                         else {
121                                 ep = strchr(cp, inter_name_termination);
122                                 if (!ep)
123                                         break;
124                                 strncpy(old_path, cp, ep-cp);
125                                 old_path[ep-cp] = 0;
126                                 cp = ep + 1;
127                         }
128
129                         /* second pathname */
130                         if (!two_paths)
131                                 strcpy(new_path, old_path);
132                         else {
133                                 if (!line_termination) {
134                                         read_line(&sb, stdin,
135                                                   line_termination);
136                                         if (sb.eof)
137                                                 break;
138                                         strcpy(new_path, sb.buf);
139                                 }
140                                 else
141                                         strcpy(new_path, cp);
142                         }
143                         diff_helper_input(old_mode, new_mode,
144                                           old_sha1, new_sha1,
145                                           old_path, status, score,
146                                           new_path);
147                         continue;
148                 }
149                 flush_them(ac, av);
150                 printf(garbage_flush_format, sb.buf);
151         }
152         flush_them(ac, av);
153         return 0;
154 }