]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/diff-tree.c
diff- and log- family: handle "git cmd -h" early
[thirdparty/git.git] / builtin / diff-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5 #include "builtin.h"
6 #include "submodule.h"
7
8 static struct rev_info log_tree_opt;
9
10 static int diff_tree_commit_sha1(const struct object_id *oid)
11 {
12 struct commit *commit = lookup_commit_reference(oid->hash);
13 if (!commit)
14 return -1;
15 return log_tree_commit(&log_tree_opt, commit);
16 }
17
18 /* Diff one or more commits. */
19 static int stdin_diff_commit(struct commit *commit, const char *p)
20 {
21 struct object_id oid;
22 struct commit_list **pptr = NULL;
23
24 /* Graft the fake parents locally to the commit */
25 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
26 struct commit *parent = lookup_commit(oid.hash);
27 if (!pptr) {
28 /* Free the real parent list */
29 free_commit_list(commit->parents);
30 commit->parents = NULL;
31 pptr = &(commit->parents);
32 }
33 if (parent) {
34 pptr = &commit_list_insert(parent, pptr)->next;
35 }
36 }
37 return log_tree_commit(&log_tree_opt, commit);
38 }
39
40 /* Diff two trees. */
41 static int stdin_diff_trees(struct tree *tree1, const char *p)
42 {
43 struct object_id oid;
44 struct tree *tree2;
45 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
46 return error("Need exactly two trees, separated by a space");
47 tree2 = lookup_tree(oid.hash);
48 if (!tree2 || parse_tree(tree2))
49 return -1;
50 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
51 oid_to_hex(&tree2->object.oid));
52 diff_tree_sha1(tree1->object.oid.hash, tree2->object.oid.hash,
53 "", &log_tree_opt.diffopt);
54 log_tree_diff_flush(&log_tree_opt);
55 return 0;
56 }
57
58 static int diff_tree_stdin(char *line)
59 {
60 int len = strlen(line);
61 struct object_id oid;
62 struct object *obj;
63 const char *p;
64
65 if (!len || line[len-1] != '\n')
66 return -1;
67 line[len-1] = 0;
68 if (parse_oid_hex(line, &oid, &p))
69 return -1;
70 obj = parse_object(oid.hash);
71 if (!obj)
72 return -1;
73 if (obj->type == OBJ_COMMIT)
74 return stdin_diff_commit((struct commit *)obj, p);
75 if (obj->type == OBJ_TREE)
76 return stdin_diff_trees((struct tree *)obj, p);
77 error("Object %s is a %s, not a commit or tree",
78 oid_to_hex(&oid), typename(obj->type));
79 return -1;
80 }
81
82 static const char diff_tree_usage[] =
83 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
84 "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
85 " -r diff recursively\n"
86 " --root include the initial commit as diff against /dev/null\n"
87 COMMON_DIFF_OPTIONS_HELP;
88
89 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
90 {
91 if (!rev->diffopt.output_format) {
92 if (rev->dense_combined_merges)
93 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
94 else
95 rev->diffopt.output_format = DIFF_FORMAT_RAW;
96 }
97 }
98
99 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
100 {
101 int nr_sha1;
102 char line[1000];
103 struct object *tree1, *tree2;
104 static struct rev_info *opt = &log_tree_opt;
105 struct setup_revision_opt s_r_opt;
106 int read_stdin = 0;
107
108 if (argc == 2 && !strcmp(argv[1], "-h"))
109 usage(diff_tree_usage);
110
111 init_revisions(opt, prefix);
112 gitmodules_config();
113 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
114 opt->abbrev = 0;
115 opt->diff = 1;
116 opt->disable_stdin = 1;
117 memset(&s_r_opt, 0, sizeof(s_r_opt));
118 s_r_opt.tweak = diff_tree_tweak_rev;
119
120 precompose_argv(argc, argv);
121 argc = setup_revisions(argc, argv, opt, &s_r_opt);
122
123 while (--argc > 0) {
124 const char *arg = *++argv;
125
126 if (!strcmp(arg, "--stdin")) {
127 read_stdin = 1;
128 continue;
129 }
130 usage(diff_tree_usage);
131 }
132
133 /*
134 * NOTE! We expect "a ^b" to be equal to "a..b", so we
135 * reverse the order of the objects if the second one
136 * is marked UNINTERESTING.
137 */
138 nr_sha1 = opt->pending.nr;
139 switch (nr_sha1) {
140 case 0:
141 if (!read_stdin)
142 usage(diff_tree_usage);
143 break;
144 case 1:
145 tree1 = opt->pending.objects[0].item;
146 diff_tree_commit_sha1(&tree1->oid);
147 break;
148 case 2:
149 tree1 = opt->pending.objects[0].item;
150 tree2 = opt->pending.objects[1].item;
151 if (tree2->flags & UNINTERESTING) {
152 SWAP(tree2, tree1);
153 }
154 diff_tree_sha1(tree1->oid.hash,
155 tree2->oid.hash,
156 "", &opt->diffopt);
157 log_tree_diff_flush(opt);
158 break;
159 }
160
161 if (read_stdin) {
162 int saved_nrl = 0;
163 int saved_dcctc = 0;
164
165 if (opt->diffopt.detect_rename)
166 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
167 DIFF_SETUP_USE_CACHE);
168 while (fgets(line, sizeof(line), stdin)) {
169 struct object_id oid;
170
171 if (get_oid_hex(line, &oid)) {
172 fputs(line, stdout);
173 fflush(stdout);
174 }
175 else {
176 diff_tree_stdin(line);
177 if (saved_nrl < opt->diffopt.needed_rename_limit)
178 saved_nrl = opt->diffopt.needed_rename_limit;
179 if (opt->diffopt.degraded_cc_to_c)
180 saved_dcctc = 1;
181 }
182 }
183 opt->diffopt.degraded_cc_to_c = saved_dcctc;
184 opt->diffopt.needed_rename_limit = saved_nrl;
185 }
186
187 return diff_result_code(&opt->diffopt, 0);
188 }