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