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