blob: 04e6f2cd72b1d802662eb22fafbb5c5bb97b5f98 [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 Hamano81e50ea2005-05-21 19:42:18 -070011static int line_termination = '\n';
12static int inter_name_termination = '\t';
Junio C Hamano915838c2005-05-17 23:29:49 -070013
Junio C Hamanoce240672005-06-03 01:36:43 -070014static void flush_them(int ac, const char **av)
15{
16 diffcore_std(av + 1,
17 0, 0, /* no renames */
18 pickaxe, pickaxe_opts,
19 -1, /* no breaks */
20 orderfile);
21 diff_flush(DIFF_FORMAT_PATCH, 0);
22}
23
Junio C Hamano902b92e2005-05-13 18:41:36 -070024static const char *diff_helper_usage =
Junio C Hamanoce240672005-06-03 01:36:43 -070025 "git-diff-helper [-z] [-S<string>] [-O<orderfile>] paths...";
Junio C Hamanod1df5742005-04-25 18:26:45 -070026
Junio C Hamano91a6eaa2005-04-28 10:13:01 -070027int main(int ac, const char **av) {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070028 struct strbuf sb;
Junio C Hamano2bc25642005-05-27 21:05:38 -070029 const char *garbage_flush_format;
Junio C Hamanod1df5742005-04-25 18:26:45 -070030
Junio C Hamanob6d8f302005-05-23 14:55:33 -070031 strbuf_init(&sb);
Junio C Hamanod1df5742005-04-25 18:26:45 -070032
33 while (1 < ac && av[1][0] == '-') {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070034 if (av[1][1] == 'z')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070035 line_termination = inter_name_termination = 0;
Junio C Hamano52e95782005-05-21 02:40:01 -070036 else if (av[1][1] == 'S') {
37 pickaxe = av[1] + 2;
38 }
Junio C Hamano367cec12005-05-27 15:55:28 -070039 else if (!strcmp(av[1], "--pickaxe-all"))
40 pickaxe_opts = DIFF_PICKAXE_ALL;
Junio C Hamanod1df5742005-04-25 18:26:45 -070041 else
Junio C Hamano902b92e2005-05-13 18:41:36 -070042 usage(diff_helper_usage);
Junio C Hamanod1df5742005-04-25 18:26:45 -070043 ac--; av++;
44 }
Junio C Hamano2bc25642005-05-27 21:05:38 -070045 garbage_flush_format = (line_termination == 0) ? "%s" : "%s\n";
46
Junio C Hamanod1df5742005-04-25 18:26:45 -070047 /* the remaining parameters are paths patterns */
48
Junio C Hamanob6d8f302005-05-23 14:55:33 -070049 diff_setup(0);
Junio C Hamanod1df5742005-04-25 18:26:45 -070050 while (1) {
Junio C Hamanob6d8f302005-05-23 14:55:33 -070051 unsigned old_mode, new_mode;
52 unsigned char old_sha1[20], new_sha1[20];
53 char old_path[PATH_MAX];
54 int status, score, two_paths;
55 char new_path[PATH_MAX];
56
57 int ch;
58 char *cp, *ep;
59
60 read_line(&sb, stdin, line_termination);
61 if (sb.eof)
Junio C Hamanod1df5742005-04-25 18:26:45 -070062 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070063 switch (sb.buf[0]) {
Junio C Hamano81e50ea2005-05-21 19:42:18 -070064 case ':':
Junio C Hamanob6d8f302005-05-23 14:55:33 -070065 /* parse the first part up to the status */
66 cp = sb.buf + 1;
67 old_mode = new_mode = 0;
68 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
69 old_mode = (old_mode << 3) | (ch - '0');
70 cp++;
71 }
72 if (*cp++ != ' ')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070073 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070074 while ((ch = *cp) && ('0' <= ch && ch <= '7')) {
75 new_mode = (new_mode << 3) | (ch - '0');
76 cp++;
77 }
78 if (*cp++ != ' ')
Junio C Hamano81e50ea2005-05-21 19:42:18 -070079 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070080 if (get_sha1_hex(cp, old_sha1))
81 break;
82 cp += 40;
83 if (*cp++ != ' ')
84 break;
85 if (get_sha1_hex(cp, new_sha1))
86 break;
87 cp += 40;
88 if (*cp++ != ' ')
89 break;
90 status = *cp++;
91 if (!strchr("MCRNDU", status))
92 break;
93 two_paths = score = 0;
Junio C Hamanoddafa7e2005-05-29 16:54:59 -070094 if (status == 'R' || status == 'C')
Junio C Hamanob6d8f302005-05-23 14:55:33 -070095 two_paths = 1;
Junio C Hamanob6d8f302005-05-23 14:55:33 -070096
Junio C Hamanoddafa7e2005-05-29 16:54:59 -070097 /* pick up score if exists */
98 if (sscanf(cp, "%d", &score) != 1)
99 score = 0;
100 cp = strchr(cp,
101 inter_name_termination);
102 if (!cp)
103 break;
Junio C Hamanob6d8f302005-05-23 14:55:33 -0700104 if (*cp++ != inter_name_termination)
105 break;
106
107 /* first pathname */
108 if (!line_termination) {
109 read_line(&sb, stdin, line_termination);
110 if (sb.eof)
111 break;
112 strcpy(old_path, sb.buf);
113 }
114 else if (!two_paths)
115 strcpy(old_path, cp);
116 else {
117 ep = strchr(cp, inter_name_termination);
118 if (!ep)
119 break;
120 strncpy(old_path, cp, ep-cp);
121 old_path[ep-cp] = 0;
122 cp = ep + 1;
123 }
124
125 /* second pathname */
126 if (!two_paths)
127 strcpy(new_path, old_path);
128 else {
129 if (!line_termination) {
130 read_line(&sb, stdin,
131 line_termination);
132 if (sb.eof)
133 break;
134 strcpy(new_path, sb.buf);
135 }
136 else
137 strcpy(new_path, cp);
138 }
139 diff_helper_input(old_mode, new_mode,
140 old_sha1, new_sha1,
141 old_path, status, score,
142 new_path);
143 continue;
Junio C Hamano81e50ea2005-05-21 19:42:18 -0700144 }
Junio C Hamanoce240672005-06-03 01:36:43 -0700145 flush_them(ac, av);
Junio C Hamano2bc25642005-05-27 21:05:38 -0700146 printf(garbage_flush_format, sb.buf);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700147 }
Junio C Hamanoce240672005-06-03 01:36:43 -0700148 flush_them(ac, av);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700149 return 0;
150}