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