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