[PATCH] Adjust diff-helper to diff-tree -v -z changes.
[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 int line_termination = '\n';
11 static int inter_name_termination = '\t';
12
13 static const char *diff_helper_usage =
14         "git-diff-helper [-z] [-S<string>] paths...";
15
16 int 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                                 sscanf(cp, "%d", &score);
86                                 if (line_termination) {
87                                         cp = strchr(cp,
88                                                     inter_name_termination);
89                                         if (!cp)
90                                                 break;
91                                 }
92                         }
93
94                         if (*cp++ != inter_name_termination)
95                                 break;
96
97                         /* first pathname */
98                         if (!line_termination) {
99                                 read_line(&sb, stdin, line_termination);
100                                 if (sb.eof)
101                                         break;
102                                 strcpy(old_path, sb.buf);
103                         }
104                         else if (!two_paths)
105                                 strcpy(old_path, cp);
106                         else {
107                                 ep = strchr(cp, inter_name_termination);
108                                 if (!ep)
109                                         break;
110                                 strncpy(old_path, cp, ep-cp);
111                                 old_path[ep-cp] = 0;
112                                 cp = ep + 1;
113                         }
114
115                         /* second pathname */
116                         if (!two_paths)
117                                 strcpy(new_path, old_path);
118                         else {
119                                 if (!line_termination) {
120                                         read_line(&sb, stdin,
121                                                   line_termination);
122                                         if (sb.eof)
123                                                 break;
124                                         strcpy(new_path, sb.buf);
125                                 }
126                                 else
127                                         strcpy(new_path, cp);
128                         }
129                         diff_helper_input(old_mode, new_mode,
130                                           old_sha1, new_sha1,
131                                           old_path, status, score,
132                                           new_path);
133                         continue;
134                 }
135                 if (1 < ac)
136                         diffcore_pathspec(av + 1);
137                 if (pickaxe)
138                         diffcore_pickaxe(pickaxe, pickaxe_opts);
139                 diff_flush(DIFF_FORMAT_PATCH, 0);
140                 printf(garbage_flush_format, sb.buf);
141         }
142         if (1 < ac)
143                 diffcore_pathspec(av + 1);
144         if (pickaxe)
145                 diffcore_pickaxe(pickaxe, pickaxe_opts);
146         diff_flush(DIFF_FORMAT_PATCH, 0);
147         return 0;
148 }