blob: b2d74eb1d1d74c87dd2530aba4638da760913916 [file] [log] [blame]
Linus Torvaldse0965d82005-05-06 17:03:171#include <ctype.h>
Linus Torvalds91740262005-04-09 20:00:542#include "cache.h"
Junio C Hamano3ebfd4a2005-04-27 16:21:003#include "diff.h"
Linus Torvaldse3bc7a32005-06-01 15:34:234#include "commit.h"
Linus Torvalds91740262005-04-09 20:00:545
Linus Torvaldsdc26bd82005-05-19 20:44:296static int show_root_diff = 0;
Linus Torvaldscee99d22005-05-06 18:42:477static int verbose_header = 0;
Linus Torvaldse0965d82005-05-06 17:03:178static int ignore_merges = 1;
Linus Torvalds73134b62005-04-10 21:03:589static int recursive = 0;
Junio C Hamano4cae1a92005-05-25 06:24:2210static int show_tree_entry_in_recursive = 0;
Linus Torvaldse0965d82005-05-06 17:03:1711static int read_stdin = 0;
Junio C Hamano6b5ee132005-09-21 07:00:4712
13static struct diff_options diff_options;
14
Linus Torvaldsf4f21ce2005-05-06 17:56:3515static const char *header = NULL;
Linus Torvaldscee99d22005-05-06 18:42:4716static const char *header_prefix = "";
Linus Torvalds000182e2005-06-05 16:02:0317static enum cmit_fmt commit_format = CMIT_FMT_RAW;
Linus Torvalds91740262005-04-09 20:00:5418
Linus Torvaldsc5b42382005-04-24 05:08:0019// What paths are we interested in?
20static int nr_paths = 0;
Junio C Hamano6b14d7f2005-05-22 17:04:3721static const char **paths = NULL;
Linus Torvaldsc5b42382005-04-24 05:08:0022static int *pathlens = NULL;
23
Linus Torvaldseeb79912005-04-10 22:08:0224static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
Linus Torvalds73134b62005-04-10 21:03:5825
26static void update_tree_entry(void **bufp, unsigned long *sizep)
Linus Torvalds91740262005-04-09 20:00:5427{
28 void *buf = *bufp;
29 unsigned long size = *sizep;
30 int len = strlen(buf) + 1 + 20;
31
32 if (size < len)
Petr Baudis2de381f2005-04-13 09:28:4833 die("corrupt tree file");
Linus Torvalds91740262005-04-09 20:00:5434 *bufp = buf + len;
35 *sizep = size - len;
Linus Torvalds91740262005-04-09 20:00:5436}
37
38static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
39{
40 int len = strlen(tree)+1;
41 const unsigned char *sha1 = tree + len;
42 const char *path = strchr(tree, ' ');
Junio C Hamano67574c42005-06-01 18:38:0743 unsigned int mode;
Linus Torvalds91740262005-04-09 20:00:5444
Junio C Hamano67574c42005-06-01 18:38:0745 if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
Petr Baudis2de381f2005-04-13 09:28:4846 die("corrupt tree file");
Linus Torvalds91740262005-04-09 20:00:5447 *pathp = path+1;
Junio C Hamano67574c42005-06-01 18:38:0748 *modep = DIFF_FILE_CANON_MODE(mode);
Linus Torvalds91740262005-04-09 20:00:5449 return sha1;
50}
51
Linus Torvalds262e82b2005-04-11 04:49:2652static char *malloc_base(const char *base, const char *path, int pathlen)
53{
54 int baselen = strlen(base);
Christopher Li812666c2005-04-26 19:00:5855 char *newbase = xmalloc(baselen + pathlen + 2);
Linus Torvalds262e82b2005-04-11 04:49:2656 memcpy(newbase, base, baselen);
57 memcpy(newbase + baselen, path, pathlen);
58 memcpy(newbase + baselen + pathlen, "/", 2);
59 return newbase;
60}
61
62static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
Linus Torvaldsed1a3682005-05-18 21:07:4263static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
Linus Torvalds262e82b2005-04-11 04:49:2664
65/* A file entry went away or appeared */
Linus Torvalds73134b62005-04-10 21:03:5866static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
Linus Torvalds91740262005-04-09 20:00:5467{
68 unsigned mode;
69 const char *path;
70 const unsigned char *sha1 = extract(tree, size, &path, &mode);
Linus Torvalds262e82b2005-04-11 04:49:2671
72 if (recursive && S_ISDIR(mode)) {
73 char type[20];
74 unsigned long size;
75 char *newbase = malloc_base(base, path, strlen(path));
76 void *tree;
77
78 tree = read_sha1_file(sha1, type, &size);
79 if (!tree || strcmp(type, "tree"))
Petr Baudis2de381f2005-04-13 09:28:4880 die("corrupt tree sha %s", sha1_to_hex(sha1));
Linus Torvalds262e82b2005-04-11 04:49:2681
82 show_tree(prefix, tree, size, newbase);
Junio C Hamano57fe64a2005-05-20 02:00:3683
Linus Torvalds262e82b2005-04-11 04:49:2684 free(tree);
85 free(newbase);
86 return;
87 }
88
Junio C Hamano6b5ee132005-09-21 07:00:4789 diff_addremove(&diff_options, prefix[0], mode, sha1, base, path);
Linus Torvalds91740262005-04-09 20:00:5490}
91
Linus Torvaldseeb79912005-04-10 22:08:0292static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
Linus Torvalds91740262005-04-09 20:00:5493{
94 unsigned mode1, mode2;
95 const char *path1, *path2;
96 const unsigned char *sha1, *sha2;
Linus Torvalds73134b62005-04-10 21:03:5897 int cmp, pathlen1, pathlen2;
Linus Torvalds91740262005-04-09 20:00:5498
99 sha1 = extract(tree1, size1, &path1, &mode1);
100 sha2 = extract(tree2, size2, &path2, &mode2);
101
Linus Torvalds73134b62005-04-10 21:03:58102 pathlen1 = strlen(path1);
103 pathlen2 = strlen(path2);
Linus Torvaldse46091d2005-05-20 16:11:46104 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
Linus Torvalds91740262005-04-09 20:00:54105 if (cmp < 0) {
Linus Torvaldseeb79912005-04-10 22:08:02106 show_file("-", tree1, size1, base);
Linus Torvalds91740262005-04-09 20:00:54107 return -1;
108 }
109 if (cmp > 0) {
Linus Torvaldseeb79912005-04-10 22:08:02110 show_file("+", tree2, size2, base);
Linus Torvalds91740262005-04-09 20:00:54111 return 1;
112 }
Junio C Hamano6b5ee132005-09-21 07:00:47113 if (!diff_options.find_copies_harder &&
114 !memcmp(sha1, sha2, 20) && mode1 == mode2)
Linus Torvalds91740262005-04-09 20:00:54115 return 0;
Linus Torvalds262e82b2005-04-11 04:49:26116
117 /*
118 * If the filemode has changed to/from a directory from/to a regular
Ingo Molnaraebb2672005-04-12 18:36:26119 * file, we need to consider it a remove and an add.
Linus Torvalds262e82b2005-04-11 04:49:26120 */
121 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
122 show_file("-", tree1, size1, base);
123 show_file("+", tree2, size2, base);
124 return 0;
125 }
126
127 if (recursive && S_ISDIR(mode1)) {
Linus Torvaldseeb79912005-04-10 22:08:02128 int retval;
Linus Torvalds262e82b2005-04-11 04:49:26129 char *newbase = malloc_base(base, path1, pathlen1);
Junio C Hamano4cae1a92005-05-25 06:24:22130 if (show_tree_entry_in_recursive)
Junio C Hamano6b5ee132005-09-21 07:00:47131 diff_change(&diff_options, mode1, mode2,
132 sha1, sha2, base, path1);
Linus Torvaldseeb79912005-04-10 22:08:02133 retval = diff_tree_sha1(sha1, sha2, newbase);
134 free(newbase);
135 return retval;
Linus Torvalds73134b62005-04-10 21:03:58136 }
137
Junio C Hamano6b5ee132005-09-21 07:00:47138 diff_change(&diff_options, mode1, mode2, sha1, sha2, base, path1);
Linus Torvalds91740262005-04-09 20:00:54139 return 0;
140}
141
Linus Torvaldsc5b42382005-04-24 05:08:00142static int interesting(void *tree, unsigned long size, const char *base)
143{
144 const char *path;
145 unsigned mode;
146 int i;
147 int baselen, pathlen;
148
149 if (!nr_paths)
150 return 1;
151
152 (void)extract(tree, size, &path, &mode);
153
154 pathlen = strlen(path);
155 baselen = strlen(base);
156
157 for (i=0; i < nr_paths; i++) {
158 const char *match = paths[i];
159 int matchlen = pathlens[i];
160
161 if (baselen >= matchlen) {
162 /* If it doesn't match, move along... */
163 if (strncmp(base, match, matchlen))
164 continue;
165
166 /* The base is a subdirectory of a path which was specified. */
167 return 1;
168 }
169
170 /* Does the base match? */
171 if (strncmp(base, match, baselen))
172 continue;
173
174 match += baselen;
175 matchlen -= baselen;
176
177 if (pathlen > matchlen)
178 continue;
179
Linus Torvaldscb6c8ed2005-05-18 20:50:24180 if (matchlen > pathlen) {
181 if (match[pathlen] != '/')
182 continue;
Linus Torvalds850e82d2005-05-18 21:17:22183 if (!S_ISDIR(mode))
184 continue;
Linus Torvaldscb6c8ed2005-05-18 20:50:24185 }
186
Linus Torvaldsc5b42382005-04-24 05:08:00187 if (strncmp(path, match, pathlen))
188 continue;
189
190 return 1;
191 }
192 return 0; /* No matches */
193}
194
Linus Torvaldsed1a3682005-05-18 21:07:42195/* A whole sub-tree went away or appeared */
196static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
197{
198 while (size) {
Junio C Hamano4727f642005-06-19 20:14:05199 if (interesting(tree, size, base))
Linus Torvaldsed1a3682005-05-18 21:07:42200 show_file(prefix, tree, size, base);
201 update_tree_entry(&tree, &size);
202 }
203}
204
Linus Torvaldseeb79912005-04-10 22:08:02205static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
Linus Torvalds91740262005-04-09 20:00:54206{
207 while (size1 | size2) {
Linus Torvaldsc5b42382005-04-24 05:08:00208 if (nr_paths && size1 && !interesting(tree1, size1, base)) {
209 update_tree_entry(&tree1, &size1);
210 continue;
211 }
212 if (nr_paths && size2 && !interesting(tree2, size2, base)) {
213 update_tree_entry(&tree2, &size2);
214 continue;
215 }
Linus Torvalds91740262005-04-09 20:00:54216 if (!size1) {
Linus Torvaldseeb79912005-04-10 22:08:02217 show_file("+", tree2, size2, base);
Linus Torvalds91740262005-04-09 20:00:54218 update_tree_entry(&tree2, &size2);
219 continue;
220 }
221 if (!size2) {
Linus Torvaldseeb79912005-04-10 22:08:02222 show_file("-", tree1, size1, base);
Linus Torvalds91740262005-04-09 20:00:54223 update_tree_entry(&tree1, &size1);
224 continue;
225 }
Linus Torvaldseeb79912005-04-10 22:08:02226 switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
Linus Torvalds91740262005-04-09 20:00:54227 case -1:
228 update_tree_entry(&tree1, &size1);
229 continue;
230 case 0:
231 update_tree_entry(&tree1, &size1);
232 /* Fallthrough */
233 case 1:
234 update_tree_entry(&tree2, &size2);
235 continue;
236 }
Alexey Nezhdanov667bb592005-05-19 11:17:16237 die("git-diff-tree: internal error");
Linus Torvalds91740262005-04-09 20:00:54238 }
239 return 0;
240}
241
Linus Torvaldseeb79912005-04-10 22:08:02242static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
Linus Torvalds91740262005-04-09 20:00:54243{
Linus Torvalds91740262005-04-09 20:00:54244 void *tree1, *tree2;
245 unsigned long size1, size2;
Linus Torvalds73134b62005-04-10 21:03:58246 int retval;
Linus Torvalds91740262005-04-09 20:00:54247
Linus Torvaldse99d59f2005-05-20 18:46:10248 tree1 = read_object_with_reference(old, "tree", &size1, NULL);
Junio C Hamanoc1fdf2a2005-04-21 01:06:50249 if (!tree1)
Petr Baudis2de381f2005-04-13 09:28:48250 die("unable to read source tree (%s)", sha1_to_hex(old));
Linus Torvaldse99d59f2005-05-20 18:46:10251 tree2 = read_object_with_reference(new, "tree", &size2, NULL);
Junio C Hamanoc1fdf2a2005-04-21 01:06:50252 if (!tree2)
Petr Baudis2de381f2005-04-13 09:28:48253 die("unable to read destination tree (%s)", sha1_to_hex(new));
Linus Torvaldseeb79912005-04-10 22:08:02254 retval = diff_tree(tree1, size1, tree2, size2, base);
Linus Torvalds73134b62005-04-10 21:03:58255 free(tree1);
256 free(tree2);
257 return retval;
258}
259
Junio C Hamano6b5ee132005-09-21 07:00:47260static void call_diff_setup_done(void)
Junio C Hamano38c6f782005-05-22 02:40:36261{
Junio C Hamano6b5ee132005-09-21 07:00:47262 diff_setup_done(&diff_options);
Junio C Hamano38c6f782005-05-22 02:40:36263}
264
Linus Torvalds09d74b32005-05-22 21:33:43265static int call_diff_flush(void)
Junio C Hamano38c6f782005-05-22 02:40:36266{
Junio C Hamano6b5ee132005-09-21 07:00:47267 diffcore_std(&diff_options);
Linus Torvalds9ab55bd2005-05-23 23:37:47268 if (diff_queue_is_empty()) {
Junio C Hamano6b5ee132005-09-21 07:00:47269 int saved_fmt = diff_options.output_format;
270 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
271 diff_flush(&diff_options);
272 diff_options.output_format = saved_fmt;
Linus Torvalds9ab55bd2005-05-23 23:37:47273 return 0;
Junio C Hamano6b14d7f2005-05-22 17:04:37274 }
Junio C Hamano6b14d7f2005-05-22 17:04:37275 if (header) {
Junio C Hamano6b5ee132005-09-21 07:00:47276 printf("%s%c", header, diff_options.line_termination);
Junio C Hamano6b14d7f2005-05-22 17:04:37277 header = NULL;
278 }
Junio C Hamano6b5ee132005-09-21 07:00:47279 diff_flush(&diff_options);
Junio C Hamano6b14d7f2005-05-22 17:04:37280 return 1;
Junio C Hamano38c6f782005-05-22 02:40:36281}
282
Junio C Hamano5c975582005-05-19 10:32:35283static int diff_tree_sha1_top(const unsigned char *old,
284 const unsigned char *new, const char *base)
285{
286 int ret;
Junio C Hamano57fe64a2005-05-20 02:00:36287
Junio C Hamano6b5ee132005-09-21 07:00:47288 call_diff_setup_done();
Junio C Hamano5c975582005-05-19 10:32:35289 ret = diff_tree_sha1(old, new, base);
Junio C Hamano38c6f782005-05-22 02:40:36290 call_diff_flush();
Junio C Hamano5c975582005-05-19 10:32:35291 return ret;
292}
293
Linus Torvaldsdc26bd82005-05-19 20:44:29294static int diff_root_tree(const unsigned char *new, const char *base)
295{
296 int retval;
297 void *tree;
298 unsigned long size;
299
Junio C Hamano6b5ee132005-09-21 07:00:47300 call_diff_setup_done();
Linus Torvaldse99d59f2005-05-20 18:46:10301 tree = read_object_with_reference(new, "tree", &size, NULL);
Linus Torvaldsdc26bd82005-05-19 20:44:29302 if (!tree)
303 die("unable to read root tree (%s)", sha1_to_hex(new));
304 retval = diff_tree("", 0, tree, size, base);
305 free(tree);
Junio C Hamano38c6f782005-05-22 02:40:36306 call_diff_flush();
Linus Torvaldsdc26bd82005-05-19 20:44:29307 return retval;
308}
309
Linus Torvalds18092662005-06-23 20:56:55310static const char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
Linus Torvaldscee99d22005-05-06 18:42:47311{
Linus Torvalds3258c902005-05-21 18:04:19312 static char this_header[16384];
Linus Torvaldscee99d22005-05-06 18:42:47313 int offset;
314
Linus Torvalds18092662005-06-23 20:56:55315 if (!verbose_header)
316 return commit;
Linus Torvaldscee99d22005-05-06 18:42:47317
Linus Torvalds18092662005-06-23 20:56:55318 offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
319 offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
Linus Torvaldscee99d22005-05-06 18:42:47320 return this_header;
321}
322
Linus Torvaldsb11645b2005-05-18 20:06:47323static int diff_tree_commit(const unsigned char *commit, const char *name)
324{
325 unsigned long size, offset;
326 char *buf = read_object_with_reference(commit, "commit", &size, NULL);
327
328 if (!buf)
329 return -1;
330
Linus Torvalds73848892005-05-18 20:43:58331 if (!name) {
332 static char commit_name[60];
333 strcpy(commit_name, sha1_to_hex(commit));
334 name = commit_name;
335 }
336
Linus Torvaldsdc26bd82005-05-19 20:44:29337 /* Root commit? */
338 if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
339 header = generate_header(name, "root", buf, size);
340 diff_root_tree(commit, "");
341 }
342
343 /* More than one parent? */
344 if (ignore_merges) {
345 if (!memcmp(buf + 46 + 48, "parent ", 7))
346 return 0;
347 }
348
Linus Torvaldsb11645b2005-05-18 20:06:47349 offset = 46;
350 while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
351 unsigned char parent[20];
352 if (get_sha1_hex(buf + offset + 7, parent))
353 return -1;
354 header = generate_header(name, sha1_to_hex(parent), buf, size);
Junio C Hamano5c975582005-05-19 10:32:35355 diff_tree_sha1_top(parent, commit, "");
Linus Torvaldsd6db0102005-05-21 22:42:53356 if (!header && verbose_header) {
Linus Torvaldsb11645b2005-05-18 20:06:47357 header_prefix = "\ndiff-tree ";
Linus Torvaldsd6db0102005-05-21 22:42:53358 /*
359 * Don't print multiple merge entries if we
360 * don't print the diffs.
361 */
Linus Torvaldsd6db0102005-05-21 22:42:53362 }
Linus Torvaldsb11645b2005-05-18 20:06:47363 offset += 48;
364 }
Junio C Hamano5098baf2005-09-15 23:13:43365 free(buf);
Linus Torvaldsb11645b2005-05-18 20:06:47366 return 0;
367}
368
Linus Torvaldse0965d82005-05-06 17:03:17369static int diff_tree_stdin(char *line)
370{
371 int len = strlen(line);
372 unsigned char commit[20], parent[20];
Linus Torvaldscee99d22005-05-06 18:42:47373 static char this_header[1000];
Linus Torvaldse0965d82005-05-06 17:03:17374
375 if (!len || line[len-1] != '\n')
376 return -1;
377 line[len-1] = 0;
378 if (get_sha1_hex(line, commit))
379 return -1;
380 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
Linus Torvaldsf4f21ce2005-05-06 17:56:35381 line[40] = 0;
Linus Torvaldse0965d82005-05-06 17:03:17382 line[81] = 0;
Linus Torvaldsf4f21ce2005-05-06 17:56:35383 sprintf(this_header, "%s (from %s)\n", line, line+41);
384 header = this_header;
Junio C Hamano5c975582005-05-19 10:32:35385 return diff_tree_sha1_top(parent, commit, "");
Linus Torvaldse0965d82005-05-06 17:03:17386 }
Linus Torvaldse0965d82005-05-06 17:03:17387 line[40] = 0;
Linus Torvaldsb11645b2005-05-18 20:06:47388 return diff_tree_commit(commit, line);
Linus Torvaldse0965d82005-05-06 17:03:17389}
390
Linus Torvaldsd288a702005-08-17 01:06:34391static int count_paths(const char **paths)
392{
393 int i = 0;
394 while (*paths++)
395 i++;
396 return i;
397}
398
Petr Baudis4d1f1192005-07-29 09:01:26399static const char diff_tree_usage[] =
Junio C Hamanodda2d792005-07-13 19:52:35400"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
401"[<common diff options>] <tree-ish> <tree-ish>"
402COMMON_DIFF_OPTIONS_HELP;
Junio C Hamanoa8db1652005-06-13 00:44:21403
Junio C Hamano6b5ee132005-09-21 07:00:47404int main(int argc, const char **argv)
Linus Torvalds73134b62005-04-10 21:03:58405{
Linus Torvalds0a8365a2005-05-18 20:10:17406 int nr_sha1;
Linus Torvaldse0965d82005-05-06 17:03:17407 char line[1000];
Linus Torvalds0a8365a2005-05-18 20:10:17408 unsigned char sha1[2][20];
Linus Torvaldsd288a702005-08-17 01:06:34409 const char *prefix = setup_git_directory();
Linus Torvalds73134b62005-04-10 21:03:58410
Linus Torvalds0a8365a2005-05-18 20:10:17411 nr_sha1 = 0;
Junio C Hamano6b5ee132005-09-21 07:00:47412 diff_setup(&diff_options);
413
Linus Torvaldsc5b42382005-04-24 05:08:00414 for (;;) {
Junio C Hamano6b5ee132005-09-21 07:00:47415 int diff_opt_cnt;
Junio C Hamano6b14d7f2005-05-22 17:04:37416 const char *arg;
Linus Torvaldsc5b42382005-04-24 05:08:00417
Linus Torvalds73134b62005-04-10 21:03:58418 argv++;
419 argc--;
Linus Torvaldse0965d82005-05-06 17:03:17420 arg = *argv;
Linus Torvalds0a8365a2005-05-18 20:10:17421 if (!arg)
Linus Torvaldse0965d82005-05-06 17:03:17422 break;
423
Linus Torvalds0a8365a2005-05-18 20:10:17424 if (*arg != '-') {
425 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
426 nr_sha1++;
427 continue;
428 }
429 break;
430 }
431
Junio C Hamano6b5ee132005-09-21 07:00:47432 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
433 if (diff_opt_cnt < 0)
434 usage(diff_tree_usage);
435 else if (diff_opt_cnt) {
436 argv += diff_opt_cnt - 1;
437 argc -= diff_opt_cnt - 1;
438 continue;
439 }
440
441
Linus Torvalds0a8365a2005-05-18 20:10:17442 if (!strcmp(arg, "--")) {
Linus Torvaldse0965d82005-05-06 17:03:17443 argv++;
444 argc--;
445 break;
446 }
Linus Torvaldsbf16c712005-04-11 15:37:17447 if (!strcmp(arg, "-r")) {
Linus Torvalds73134b62005-04-10 21:03:58448 recursive = 1;
449 continue;
450 }
Junio C Hamano4cae1a92005-05-25 06:24:22451 if (!strcmp(arg, "-t")) {
452 recursive = show_tree_entry_in_recursive = 1;
453 continue;
454 }
Linus Torvaldse0965d82005-05-06 17:03:17455 if (!strcmp(arg, "-m")) {
456 ignore_merges = 0;
457 continue;
458 }
Linus Torvaldscee99d22005-05-06 18:42:47459 if (!strcmp(arg, "-v")) {
460 verbose_header = 1;
461 header_prefix = "diff-tree ";
462 continue;
463 }
Junio C Hamanoa8db1652005-06-13 00:44:21464 if (!strncmp(arg, "--pretty", 8)) {
465 verbose_header = 1;
Linus Torvaldsba88e542005-06-13 03:34:09466 header_prefix = "diff-tree ";
Junio C Hamanoa8db1652005-06-13 00:44:21467 commit_format = get_commit_format(arg+8);
468 continue;
469 }
Linus Torvaldse0965d82005-05-06 17:03:17470 if (!strcmp(arg, "--stdin")) {
471 read_stdin = 1;
472 continue;
473 }
Linus Torvaldsdc26bd82005-05-19 20:44:29474 if (!strcmp(arg, "--root")) {
475 show_root_diff = 1;
476 continue;
477 }
Junio C Hamanoc5bac172005-04-21 02:49:16478 usage(diff_tree_usage);
Linus Torvalds73134b62005-04-10 21:03:58479 }
Junio C Hamano6b5ee132005-09-21 07:00:47480 if (diff_options.output_format == DIFF_FORMAT_PATCH)
481 recursive = 1;
Linus Torvalds73134b62005-04-10 21:03:58482
Linus Torvaldsd288a702005-08-17 01:06:34483 paths = get_pathspec(prefix, argv);
484 if (paths) {
Linus Torvaldsc5b42382005-04-24 05:08:00485 int i;
486
Linus Torvaldsd288a702005-08-17 01:06:34487 nr_paths = count_paths(paths);
Christopher Li812666c2005-04-26 19:00:58488 pathlens = xmalloc(nr_paths * sizeof(int));
Linus Torvaldsc5b42382005-04-24 05:08:00489 for (i=0; i<nr_paths; i++)
490 pathlens[i] = strlen(paths[i]);
491 }
492
Linus Torvalds0a8365a2005-05-18 20:10:17493 switch (nr_sha1) {
494 case 0:
495 if (!read_stdin)
496 usage(diff_tree_usage);
497 break;
498 case 1:
Linus Torvalds73848892005-05-18 20:43:58499 diff_tree_commit(sha1[0], NULL);
Linus Torvalds0a8365a2005-05-18 20:10:17500 break;
501 case 2:
Junio C Hamano5c975582005-05-19 10:32:35502 diff_tree_sha1_top(sha1[0], sha1[1], "");
Linus Torvalds0a8365a2005-05-18 20:10:17503 break;
504 }
505
Linus Torvaldse0965d82005-05-06 17:03:17506 if (!read_stdin)
Linus Torvalds0a8365a2005-05-18 20:10:17507 return 0;
Linus Torvaldse0965d82005-05-06 17:03:17508
Junio C Hamano6b5ee132005-09-21 07:00:47509 if (diff_options.detect_rename)
510 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
511 DIFF_SETUP_USE_CACHE);
Linus Torvaldse0965d82005-05-06 17:03:17512 while (fgets(line, sizeof(line), stdin))
513 diff_tree_stdin(line);
514
515 return 0;
Linus Torvalds91740262005-04-09 20:00:54516}