]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/diff-tree.c
Merge branch 'jk/bundle-use-dash-for-stdfiles'
[thirdparty/git.git] / builtin / diff-tree.c
1 #define USE_THE_INDEX_VARIABLE
2 #include "cache.h"
3 #include "config.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "hex.h"
7 #include "log-tree.h"
8 #include "builtin.h"
9 #include "submodule.h"
10 #include "repository.h"
11
12 static struct rev_info log_tree_opt;
13
14 static int diff_tree_commit_oid(const struct object_id *oid)
15 {
16 struct commit *commit = lookup_commit_reference(the_repository, oid);
17 if (!commit)
18 return -1;
19 return log_tree_commit(&log_tree_opt, commit);
20 }
21
22 /* Diff one or more commits. */
23 static int stdin_diff_commit(struct commit *commit, const char *p)
24 {
25 struct object_id oid;
26 struct commit_list **pptr = NULL;
27
28 /* Graft the fake parents locally to the commit */
29 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
30 struct commit *parent = lookup_commit(the_repository, &oid);
31 if (!pptr) {
32 /* Free the real parent list */
33 free_commit_list(commit->parents);
34 commit->parents = NULL;
35 pptr = &(commit->parents);
36 }
37 if (parent) {
38 pptr = &commit_list_insert(parent, pptr)->next;
39 }
40 }
41 return log_tree_commit(&log_tree_opt, commit);
42 }
43
44 /* Diff two trees. */
45 static int stdin_diff_trees(struct tree *tree1, const char *p)
46 {
47 struct object_id oid;
48 struct tree *tree2;
49 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
50 return error("Need exactly two trees, separated by a space");
51 tree2 = lookup_tree(the_repository, &oid);
52 if (!tree2 || parse_tree(tree2))
53 return -1;
54 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
55 oid_to_hex(&tree2->object.oid));
56 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
57 "", &log_tree_opt.diffopt);
58 log_tree_diff_flush(&log_tree_opt);
59 return 0;
60 }
61
62 static int diff_tree_stdin(char *line)
63 {
64 int len = strlen(line);
65 struct object_id oid;
66 struct object *obj;
67 const char *p;
68
69 if (!len || line[len-1] != '\n')
70 return -1;
71 line[len-1] = 0;
72 if (parse_oid_hex(line, &oid, &p))
73 return -1;
74 obj = parse_object(the_repository, &oid);
75 if (!obj)
76 return -1;
77 if (obj->type == OBJ_COMMIT)
78 return stdin_diff_commit((struct commit *)obj, p);
79 if (obj->type == OBJ_TREE)
80 return stdin_diff_trees((struct tree *)obj, p);
81 error("Object %s is a %s, not a commit or tree",
82 oid_to_hex(&oid), type_name(obj->type));
83 return -1;
84 }
85
86 static const char diff_tree_usage[] =
87 "git diff-tree [--stdin] [-m] [-s] [-v] [--no-commit-id] [--pretty]\n"
88 " [-t] [-r] [-c | --cc] [--combined-all-paths] [--root] [--merge-base]\n"
89 " [<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
90 "\n"
91 " -r diff recursively\n"
92 " -c show combined diff for merge commits\n"
93 " --cc show combined diff for merge commits removing uninteresting hunks\n"
94 " --combined-all-paths\n"
95 " show name of file in all parents for combined diffs\n"
96 " --root include the initial commit as diff against /dev/null\n"
97 COMMON_DIFF_OPTIONS_HELP;
98
99 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
100 {
101 if (!rev->diffopt.output_format) {
102 if (rev->dense_combined_merges)
103 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
104 else
105 rev->diffopt.output_format = DIFF_FORMAT_RAW;
106 }
107 }
108
109 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
110 {
111 char line[1000];
112 struct object *tree1, *tree2;
113 static struct rev_info *opt = &log_tree_opt;
114 struct setup_revision_opt s_r_opt;
115 struct userformat_want w;
116 int read_stdin = 0;
117 int merge_base = 0;
118
119 if (argc == 2 && !strcmp(argv[1], "-h"))
120 usage(diff_tree_usage);
121
122 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
123 repo_init_revisions(the_repository, opt, prefix);
124 if (repo_read_index(the_repository) < 0)
125 die(_("index file corrupt"));
126 opt->abbrev = 0;
127 opt->diff = 1;
128 opt->disable_stdin = 1;
129 memset(&s_r_opt, 0, sizeof(s_r_opt));
130 s_r_opt.tweak = diff_tree_tweak_rev;
131
132 prefix = precompose_argv_prefix(argc, argv, prefix);
133 argc = setup_revisions(argc, argv, opt, &s_r_opt);
134
135 memset(&w, 0, sizeof(w));
136 userformat_find_requirements(NULL, &w);
137
138 if (!opt->show_notes_given && w.notes)
139 opt->show_notes = 1;
140 if (opt->show_notes)
141 load_display_notes(&opt->notes_opt);
142
143 while (--argc > 0) {
144 const char *arg = *++argv;
145
146 if (!strcmp(arg, "--stdin")) {
147 read_stdin = 1;
148 continue;
149 }
150 if (!strcmp(arg, "--merge-base")) {
151 merge_base = 1;
152 continue;
153 }
154 usage(diff_tree_usage);
155 }
156
157 if (read_stdin && merge_base)
158 die(_("options '%s' and '%s' cannot be used together"), "--stdin", "--merge-base");
159 if (merge_base && opt->pending.nr != 2)
160 die(_("--merge-base only works with two commits"));
161
162 opt->diffopt.rotate_to_strict = 1;
163
164 /*
165 * NOTE! We expect "a..b" to expand to "^a b" but it is
166 * perfectly valid for revision range parser to yield "b ^a",
167 * which means the same thing. If we get the latter, i.e. the
168 * second one is marked UNINTERESTING, we recover the original
169 * order the user gave, i.e. "a..b", by swapping the trees.
170 */
171 switch (opt->pending.nr) {
172 case 0:
173 if (!read_stdin)
174 usage(diff_tree_usage);
175 break;
176 case 1:
177 tree1 = opt->pending.objects[0].item;
178 diff_tree_commit_oid(&tree1->oid);
179 break;
180 case 2:
181 tree1 = opt->pending.objects[0].item;
182 tree2 = opt->pending.objects[1].item;
183 if (merge_base) {
184 struct object_id oid;
185
186 diff_get_merge_base(opt, &oid);
187 tree1 = lookup_object(the_repository, &oid);
188 } else if (tree2->flags & UNINTERESTING) {
189 SWAP(tree2, tree1);
190 }
191 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
192 log_tree_diff_flush(opt);
193 break;
194 }
195
196 if (read_stdin) {
197 int saved_nrl = 0;
198 int saved_dcctc = 0;
199
200 opt->diffopt.rotate_to_strict = 0;
201 opt->diffopt.no_free = 1;
202 if (opt->diffopt.detect_rename) {
203 if (!the_index.cache)
204 repo_read_index(the_repository);
205 opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE;
206 }
207 while (fgets(line, sizeof(line), stdin)) {
208 struct object_id oid;
209
210 if (get_oid_hex(line, &oid)) {
211 fputs(line, stdout);
212 fflush(stdout);
213 }
214 else {
215 diff_tree_stdin(line);
216 if (saved_nrl < opt->diffopt.needed_rename_limit)
217 saved_nrl = opt->diffopt.needed_rename_limit;
218 if (opt->diffopt.degraded_cc_to_c)
219 saved_dcctc = 1;
220 }
221 }
222 opt->diffopt.degraded_cc_to_c = saved_dcctc;
223 opt->diffopt.needed_rename_limit = saved_nrl;
224 opt->diffopt.no_free = 0;
225 diff_free(&opt->diffopt);
226 }
227
228 return diff_result_code(&opt->diffopt, 0);
229 }