]> git.ipfire.org Git - thirdparty/git.git/blame - diff-tree.c
diff --stat: do not do its own three-dashes.
[thirdparty/git.git] / diff-tree.c
CommitLineData
9174026c 1#include "cache.h"
3ebfd4aa 2#include "diff.h"
e3bc7a3b 3#include "commit.h"
5f1c3f07 4#include "log-tree.h"
9174026c 5
5f1c3f07 6static struct log_tree_opt log_tree_opt;
b11645be 7
45392a64
JH
8static int diff_tree_commit_sha1(const unsigned char *sha1)
9{
10 struct commit *commit = lookup_commit_reference(sha1);
11 if (!commit)
12 return -1;
5f1c3f07 13 return log_tree_commit(&log_tree_opt, commit);
45392a64
JH
14}
15
b11645be
LT
16static int diff_tree_stdin(char *line)
17{
18 int len = strlen(line);
45392a64
JH
19 unsigned char sha1[20];
20 struct commit *commit;
b11645be
LT
21
22 if (!len || line[len-1] != '\n')
23 return -1;
24 line[len-1] = 0;
45392a64
JH
25 if (get_sha1_hex(line, sha1))
26 return -1;
27 commit = lookup_commit(sha1);
28 if (!commit || parse_commit(commit))
b11645be 29 return -1;
45392a64
JH
30 if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) {
31 /* Graft the fake parents locally to the commit */
32 int pos = 41;
33 struct commit_list **pptr, *parents;
34
35 /* Free the real parent list */
36 for (parents = commit->parents; parents; ) {
37 struct commit_list *tmp = parents->next;
38 free(parents);
39 parents = tmp;
40 }
41 commit->parents = NULL;
42 pptr = &(commit->parents);
43 while (line[pos] && !get_sha1_hex(line + pos, sha1)) {
44 struct commit *parent = lookup_commit(sha1);
45 if (parent) {
46 pptr = &commit_list_insert(parent, pptr)->next;
47 }
48 pos += 41;
49 }
b11645be 50 }
5f1c3f07 51 return log_tree_commit(&log_tree_opt, commit);
e0965d83
LT
52}
53
4d1f1190 54static const char diff_tree_usage[] =
d8f4790e 55"git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
50b8e355
CS
56"[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
57" -r diff recursively\n"
58" --root include the initial commit as diff against /dev/null\n"
dda2d79a 59COMMON_DIFF_OPTIONS_HELP;
a8db165e 60
6b5ee137 61int main(int argc, const char **argv)
73134b6d 62{
0a8365a1 63 int nr_sha1;
e0965d83 64 char line[1000];
0a8365a1 65 unsigned char sha1[2][20];
d288a700 66 const char *prefix = setup_git_directory();
5f1c3f07
JH
67 static struct log_tree_opt *opt = &log_tree_opt;
68 int read_stdin = 0;
73134b6d 69
9ce392f4 70 git_config(git_diff_config);
0a8365a1 71 nr_sha1 = 0;
5f1c3f07 72 init_log_tree_opt(opt);
6b5ee137 73
c5b42386 74 for (;;) {
5f1c3f07 75 int opt_cnt;
6b14d7fa 76 const char *arg;
c5b42386 77
e0965d83
LT
78 argv++;
79 argc--;
80 arg = *argv;
0a8365a1 81 if (!arg)
c5b42386
LT
82 break;
83
0a8365a1
LT
84 if (*arg != '-') {
85 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
86 nr_sha1++;
87 continue;
88 }
89 break;
90 }
91
5f1c3f07
JH
92 opt_cnt = log_tree_opt_parse(opt, argv, argc);
93 if (opt_cnt < 0)
6b5ee137 94 usage(diff_tree_usage);
5f1c3f07
JH
95 else if (opt_cnt) {
96 argv += opt_cnt - 1;
97 argc -= opt_cnt - 1;
6b5ee137
JH
98 continue;
99 }
100
0a8365a1 101 if (!strcmp(arg, "--")) {
e0965d83
LT
102 argv++;
103 argc--;
104 break;
105 }
e0965d83
LT
106 if (!strcmp(arg, "--stdin")) {
107 read_stdin = 1;
108 continue;
109 }
c5bac17a 110 usage(diff_tree_usage);
73134b6d
LT
111 }
112
5f1c3f07
JH
113 if (opt->combine_merges)
114 opt->ignore_merges = 0;
ee638024
LT
115
116 /* We can only do dense combined merges with diff output */
5f1c3f07
JH
117 if (opt->dense_combined_merges)
118 opt->diffopt.output_format = DIFF_FORMAT_PATCH;
af3feefa 119
5f1c3f07
JH
120 if (opt->diffopt.output_format == DIFF_FORMAT_PATCH)
121 opt->diffopt.recursive = 1;
deb989b5 122
5069b1cf 123 diff_tree_setup_paths(get_pathspec(prefix, argv), &opt->diffopt);
5f1c3f07 124 diff_setup_done(&opt->diffopt);
c5b42386 125
0a8365a1
LT
126 switch (nr_sha1) {
127 case 0:
128 if (!read_stdin)
129 usage(diff_tree_usage);
130 break;
131 case 1:
45392a64 132 diff_tree_commit_sha1(sha1[0]);
0a8365a1
LT
133 break;
134 case 2:
5f1c3f07
JH
135 diff_tree_sha1(sha1[0], sha1[1], "", &opt->diffopt);
136 log_tree_diff_flush(opt);
0a8365a1
LT
137 break;
138 }
139
e0965d83 140 if (!read_stdin)
0a8365a1 141 return 0;
e0965d83 142
5f1c3f07
JH
143 if (opt->diffopt.detect_rename)
144 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
6b5ee137 145 DIFF_SETUP_USE_CACHE);
e0965d83
LT
146 while (fgets(line, sizeof(line), stdin))
147 diff_tree_stdin(line);
148
149 return 0;
9174026c 150}