Merge branch 'bw/protocol-v2'
[git/debian.git] / builtin / diff-tree.c
blob91ba67070e252fa13a0e32e08a251ca58c7f930b
1 #include "cache.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "commit.h"
5 #include "log-tree.h"
6 #include "builtin.h"
7 #include "submodule.h"
8 #include "repository.h"
10 static struct rev_info log_tree_opt;
12 static int diff_tree_commit_oid(const struct object_id *oid)
14 struct commit *commit = lookup_commit_reference(the_repository, oid);
15 if (!commit)
16 return -1;
17 return log_tree_commit(&log_tree_opt, commit);
20 /* Diff one or more commits. */
21 static int stdin_diff_commit(struct commit *commit, const char *p)
23 struct object_id oid;
24 struct commit_list **pptr = NULL;
26 /* Graft the fake parents locally to the commit */
27 while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
28 struct commit *parent = lookup_commit(the_repository, &oid);
29 if (!pptr) {
30 /* Free the real parent list */
31 free_commit_list(commit->parents);
32 commit->parents = NULL;
33 pptr = &(commit->parents);
35 if (parent) {
36 pptr = &commit_list_insert(parent, pptr)->next;
39 return log_tree_commit(&log_tree_opt, commit);
42 /* Diff two trees. */
43 static int stdin_diff_trees(struct tree *tree1, const char *p)
45 struct object_id oid;
46 struct tree *tree2;
47 if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
48 return error("Need exactly two trees, separated by a space");
49 tree2 = lookup_tree(the_repository, &oid);
50 if (!tree2 || parse_tree(tree2))
51 return -1;
52 printf("%s %s\n", oid_to_hex(&tree1->object.oid),
53 oid_to_hex(&tree2->object.oid));
54 diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
55 "", &log_tree_opt.diffopt);
56 log_tree_diff_flush(&log_tree_opt);
57 return 0;
60 static int diff_tree_stdin(char *line)
62 int len = strlen(line);
63 struct object_id oid;
64 struct object *obj;
65 const char *p;
67 if (!len || line[len-1] != '\n')
68 return -1;
69 line[len-1] = 0;
70 if (parse_oid_hex(line, &oid, &p))
71 return -1;
72 obj = parse_object(the_repository, &oid);
73 if (!obj)
74 return -1;
75 if (obj->type == OBJ_COMMIT)
76 return stdin_diff_commit((struct commit *)obj, p);
77 if (obj->type == OBJ_TREE)
78 return stdin_diff_trees((struct tree *)obj, p);
79 error("Object %s is a %s, not a commit or tree",
80 oid_to_hex(&oid), type_name(obj->type));
81 return -1;
84 static const char diff_tree_usage[] =
85 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
86 "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
87 " -r diff recursively\n"
88 " --root include the initial commit as diff against /dev/null\n"
89 COMMON_DIFF_OPTIONS_HELP;
91 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
93 if (!rev->diffopt.output_format) {
94 if (rev->dense_combined_merges)
95 rev->diffopt.output_format = DIFF_FORMAT_PATCH;
96 else
97 rev->diffopt.output_format = DIFF_FORMAT_RAW;
101 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
103 char line[1000];
104 struct object *tree1, *tree2;
105 static struct rev_info *opt = &log_tree_opt;
106 struct setup_revision_opt s_r_opt;
107 int read_stdin = 0;
109 if (argc == 2 && !strcmp(argv[1], "-h"))
110 usage(diff_tree_usage);
112 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
113 init_revisions(opt, prefix);
114 if (read_cache() < 0)
115 die(_("index file corrupt"));
116 opt->abbrev = 0;
117 opt->diff = 1;
118 opt->disable_stdin = 1;
119 memset(&s_r_opt, 0, sizeof(s_r_opt));
120 s_r_opt.tweak = diff_tree_tweak_rev;
122 precompose_argv(argc, argv);
123 argc = setup_revisions(argc, argv, opt, &s_r_opt);
125 while (--argc > 0) {
126 const char *arg = *++argv;
128 if (!strcmp(arg, "--stdin")) {
129 read_stdin = 1;
130 continue;
132 usage(diff_tree_usage);
136 * NOTE! We expect "a..b" to expand to "^a b" but it is
137 * perfectly valid for revision range parser to yield "b ^a",
138 * which means the same thing. If we get the latter, i.e. the
139 * second one is marked UNINTERESTING, we recover the original
140 * order the user gave, i.e. "a..b", by swapping the trees.
142 switch (opt->pending.nr) {
143 case 0:
144 if (!read_stdin)
145 usage(diff_tree_usage);
146 break;
147 case 1:
148 tree1 = opt->pending.objects[0].item;
149 diff_tree_commit_oid(&tree1->oid);
150 break;
151 case 2:
152 tree1 = opt->pending.objects[0].item;
153 tree2 = opt->pending.objects[1].item;
154 if (tree2->flags & UNINTERESTING) {
155 SWAP(tree2, tree1);
157 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
158 log_tree_diff_flush(opt);
159 break;
162 if (read_stdin) {
163 int saved_nrl = 0;
164 int saved_dcctc = 0;
166 if (opt->diffopt.detect_rename)
167 opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
168 DIFF_SETUP_USE_CACHE);
169 while (fgets(line, sizeof(line), stdin)) {
170 struct object_id oid;
172 if (get_oid_hex(line, &oid)) {
173 fputs(line, stdout);
174 fflush(stdout);
176 else {
177 diff_tree_stdin(line);
178 if (saved_nrl < opt->diffopt.needed_rename_limit)
179 saved_nrl = opt->diffopt.needed_rename_limit;
180 if (opt->diffopt.degraded_cc_to_c)
181 saved_dcctc = 1;
184 opt->diffopt.degraded_cc_to_c = saved_dcctc;
185 opt->diffopt.needed_rename_limit = saved_nrl;
188 return diff_result_code(&opt->diffopt, 0);