Merge branch 'tg/refs-allowed-flags' into maint
[git] / builtin / diff-tree.c
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
9 static struct rev_info log_tree_opt;
10
11 static int diff_tree_commit_oid(const struct object_id *oid)
12 {
13         struct commit *commit = lookup_commit_reference(oid);
14         if (!commit)
15                 return -1;
16         return log_tree_commit(&log_tree_opt, commit);
17 }
18
19 /* Diff one or more commits. */
20 static int stdin_diff_commit(struct commit *commit, const char *p)
21 {
22         struct object_id oid;
23         struct commit_list **pptr = NULL;
24
25         /* Graft the fake parents locally to the commit */
26         while (isspace(*p++) && !parse_oid_hex(p, &oid, &p)) {
27                 struct commit *parent = lookup_commit(&oid);
28                 if (!pptr) {
29                         /* Free the real parent list */
30                         free_commit_list(commit->parents);
31                         commit->parents = NULL;
32                         pptr = &(commit->parents);
33                 }
34                 if (parent) {
35                         pptr = &commit_list_insert(parent, pptr)->next;
36                 }
37         }
38         return log_tree_commit(&log_tree_opt, commit);
39 }
40
41 /* Diff two trees. */
42 static int stdin_diff_trees(struct tree *tree1, const char *p)
43 {
44         struct object_id oid;
45         struct tree *tree2;
46         if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p)
47                 return error("Need exactly two trees, separated by a space");
48         tree2 = lookup_tree(&oid);
49         if (!tree2 || parse_tree(tree2))
50                 return -1;
51         printf("%s %s\n", oid_to_hex(&tree1->object.oid),
52                           oid_to_hex(&tree2->object.oid));
53         diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
54                       "", &log_tree_opt.diffopt);
55         log_tree_diff_flush(&log_tree_opt);
56         return 0;
57 }
58
59 static int diff_tree_stdin(char *line)
60 {
61         int len = strlen(line);
62         struct object_id oid;
63         struct object *obj;
64         const char *p;
65
66         if (!len || line[len-1] != '\n')
67                 return -1;
68         line[len-1] = 0;
69         if (parse_oid_hex(line, &oid, &p))
70                 return -1;
71         obj = parse_object(&oid);
72         if (!obj)
73                 return -1;
74         if (obj->type == OBJ_COMMIT)
75                 return stdin_diff_commit((struct commit *)obj, p);
76         if (obj->type == OBJ_TREE)
77                 return stdin_diff_trees((struct tree *)obj, p);
78         error("Object %s is a %s, not a commit or tree",
79               oid_to_hex(&oid), typename(obj->type));
80         return -1;
81 }
82
83 static const char diff_tree_usage[] =
84 "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
85 "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n"
86 "  -r            diff recursively\n"
87 "  --root        include the initial commit as diff against /dev/null\n"
88 COMMON_DIFF_OPTIONS_HELP;
89
90 static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt)
91 {
92         if (!rev->diffopt.output_format) {
93                 if (rev->dense_combined_merges)
94                         rev->diffopt.output_format = DIFF_FORMAT_PATCH;
95                 else
96                         rev->diffopt.output_format = DIFF_FORMAT_RAW;
97         }
98 }
99
100 int cmd_diff_tree(int argc, const char **argv, const char *prefix)
101 {
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         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
112         init_revisions(opt, prefix);
113         gitmodules_config();
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 expand to "^a b" but it is
135          * perfectly valid for revision range parser to yield "b ^a",
136          * which means the same thing. If we get the latter, i.e. the
137          * second one is marked UNINTERESTING, we recover the original
138          * order the user gave, i.e. "a..b", by swapping the trees.
139          */
140         switch (opt->pending.nr) {
141         case 0:
142                 if (!read_stdin)
143                         usage(diff_tree_usage);
144                 break;
145         case 1:
146                 tree1 = opt->pending.objects[0].item;
147                 diff_tree_commit_oid(&tree1->oid);
148                 break;
149         case 2:
150                 tree1 = opt->pending.objects[0].item;
151                 tree2 = opt->pending.objects[1].item;
152                 if (tree2->flags & UNINTERESTING) {
153                         SWAP(tree2, tree1);
154                 }
155                 diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt);
156                 log_tree_diff_flush(opt);
157                 break;
158         }
159
160         if (read_stdin) {
161                 int saved_nrl = 0;
162                 int saved_dcctc = 0;
163
164                 if (opt->diffopt.detect_rename)
165                         opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
166                                                DIFF_SETUP_USE_CACHE);
167                 while (fgets(line, sizeof(line), stdin)) {
168                         struct object_id oid;
169
170                         if (get_oid_hex(line, &oid)) {
171                                 fputs(line, stdout);
172                                 fflush(stdout);
173                         }
174                         else {
175                                 diff_tree_stdin(line);
176                                 if (saved_nrl < opt->diffopt.needed_rename_limit)
177                                         saved_nrl = opt->diffopt.needed_rename_limit;
178                                 if (opt->diffopt.degraded_cc_to_c)
179                                         saved_dcctc = 1;
180                         }
181                 }
182                 opt->diffopt.degraded_cc_to_c = saved_dcctc;
183                 opt->diffopt.needed_rename_limit = saved_nrl;
184         }
185
186         return diff_result_code(&opt->diffopt, 0);
187 }