blob: 63aff6969863f6fb3da94c9866067e1c779831a3 [file] [log] [blame]
Junio C Hamanobe3cfa82005-04-26 09:25:05 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
Junio C Hamanod1df5742005-04-25 18:26:45 -07004#include "cache.h"
5#include "strbuf.h"
6#include "diff.h"
7
Junio C Hamano057c7d32005-05-21 15:02:51 -07008static const char *pickaxe = NULL;
Junio C Hamano367cec12005-05-27 15:55:28 -07009static int pickaxe_opts = 0;
Junio C Hamanoce240672005-06-03 01:36:43 -070010static const char *orderfile = NULL;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070011static const char *diff_filter = NULL;
Junio C Hamano81e50ea2005-05-21 19:42:18 -070012static int line_termination = '\n';
13static int inter_name_termination = '\t';
Junio C Hamano915838c2005-05-17 23:29:49 -070014
Junio C Hamanoce240672005-06-03 01:36:43 -070015static void flush_them(int ac, const char **av)
16{
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070017 diffcore_std_no_resolve(av + 1,
18 pickaxe, pickaxe_opts,
19 orderfile, diff_filter);
20 diff_flush(DIFF_FORMAT_PATCH);
Junio C Hamanoce240672005-06-03 01:36:43 -070021}
22
Junio C Hamano902b92e2005-05-13 18:41:36 -070023static const char *diff_helper_usage =
Junio C Hamano232b75a2005-06-19 13:14:53 -070024"git-diff-helper [-z] [-O<orderfile>] [-S<string>] [--pickaxe-all] [<path>...]";
Junio C Hamanod1df5742005-04-25 18:26:45 -070025
Junio C Hamano91a6eaa2005-04-28 10:13:01 -070026int main(int ac, const char **av) {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070027 struct strbuf sb;
Junio C Hamano2bc25642005-05-27 21:05:38 -070028 const char *garbage_flush_format;
Junio C Hamanod1df5742005-04-25 18:26:45 -070029
Junio C Hamanob6d8f302005-05-23 14:55:33 -070030 strbuf_init(&sb);
Junio C Hamanod1df5742005-04-25 18:26:45 -070031
32 while (1 < ac && av[1][0] == '-') {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070033 if (av[1][1] == 'z')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070034 line_termination = inter_name_termination = 0;
Junio C Hamano52e95782005-05-21 02:40:01 -070035 else if (av[1][1] == 'S') {
36 pickaxe = av[1] + 2;
37 }
Junio C Hamano367cec12005-05-27 15:55:28 -070038 else if (!strcmp(av[1], "--pickaxe-all"))
39 pickaxe_opts = DIFF_PICKAXE_ALL;
Junio C Hamanof2ce9fd2005-06-11 20:57:13 -070040 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;
Junio C Hamanod1df5742005-04-25 18:26:45 -070044 else
Junio C Hamano902b92e2005-05-13 18:41:36 -070045 usage(diff_helper_usage);
Junio C Hamanod1df5742005-04-25 18:26:45 -070046 ac--; av++;
47 }
Junio C Hamano2bc25642005-05-27 21:05:38 -070048 garbage_flush_format = (line_termination == 0) ? "%s" : "%s\n";
49
Junio C Hamanod1df5742005-04-25 18:26:45 -070050 /* the remaining parameters are paths patterns */
51
Junio C Hamanob6d8f302005-05-23 14:55:33 -070052 diff_setup(0);
Junio C Hamanod1df5742005-04-25 18:26:45 -070053 while (1) {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070054 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)
Junio C Hamanod1df5742005-04-25 18:26:45 -070065 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070066 switch (sb.buf[0]) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -070067 case ':':
Junio C Hamanob6d8f302005-05-23 14:55:33 -070068 /* 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++ != ' ')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070076 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070077 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
78 new_mode = (new_mode << 3) | (ch - '0');
79 cp++;
80 }
81 if (*cp++ != ' ')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070082 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070083 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;
Junio C Hamanoddafa7e2005-05-29 16:54:59 -070097 if (status == 'R' || status == 'C')
Junio C Hamanob6d8f302005-05-23 14:55:33 -070098 two_paths = 1;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070099
Junio C Hamanoddafa7e2005-05-29 16:54:59 -0700100 /* 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;
Junio C Hamanob6d8f302005-05-23 14:55:33 -0700107 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;
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700147 }
Junio C Hamanoce240672005-06-03 01:36:43 -0700148 flush_them(ac, av);
Junio C Hamano2bc25642005-05-27 21:05:38 -0700149 printf(garbage_flush_format, sb.buf);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700150 }
Junio C Hamanoce240672005-06-03 01:36:43 -0700151 flush_them(ac, av);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700152 return 0;
153}