]>
Commit | Line | Data |
---|---|---|
9174026c | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
3ebfd4aa | 3 | #include "diff.h" |
e3bc7a3b | 4 | #include "commit.h" |
5f1c3f07 | 5 | #include "log-tree.h" |
e8cc9cd9 | 6 | #include "builtin.h" |
302ad7a9 | 7 | #include "submodule.h" |
9174026c | 8 | |
cd2bdc53 | 9 | static struct rev_info log_tree_opt; |
b11645be | 10 | |
315f49f2 | 11 | static int diff_tree_commit_oid(const struct object_id *oid) |
45392a64 | 12 | { |
bc83266a | 13 | struct commit *commit = lookup_commit_reference(oid); |
45392a64 JH |
14 | if (!commit) |
15 | return -1; | |
5f1c3f07 | 16 | return log_tree_commit(&log_tree_opt, commit); |
45392a64 JH |
17 | } |
18 | ||
a57114c8 | 19 | /* Diff one or more commits. */ |
5f5e936d | 20 | static int stdin_diff_commit(struct commit *commit, const char *p) |
b11645be | 21 | { |
5f5e936d | 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)) { | |
bc83266a | 27 | struct commit *parent = lookup_commit(&oid); |
5f5e936d | 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; | |
45392a64 | 36 | } |
b11645be | 37 | } |
5f1c3f07 | 38 | return log_tree_commit(&log_tree_opt, commit); |
e0965d83 LT |
39 | } |
40 | ||
140b378d | 41 | /* Diff two trees. */ |
5f5e936d | 42 | static int stdin_diff_trees(struct tree *tree1, const char *p) |
140b378d | 43 | { |
5f5e936d | 44 | struct object_id oid; |
140b378d | 45 | struct tree *tree2; |
5f5e936d | 46 | if (!isspace(*p++) || parse_oid_hex(p, &oid, &p) || *p) |
140b378d | 47 | return error("Need exactly two trees, separated by a space"); |
740ee055 | 48 | tree2 = lookup_tree(&oid); |
140b378d KW |
49 | if (!tree2 || parse_tree(tree2)) |
50 | return -1; | |
f2fd0760 | 51 | printf("%s %s\n", oid_to_hex(&tree1->object.oid), |
52 | oid_to_hex(&tree2->object.oid)); | |
66f414f8 BW |
53 | diff_tree_oid(&tree1->object.oid, &tree2->object.oid, |
54 | "", &log_tree_opt.diffopt); | |
140b378d KW |
55 | log_tree_diff_flush(&log_tree_opt); |
56 | return 0; | |
57 | } | |
58 | ||
a57114c8 KW |
59 | static int diff_tree_stdin(char *line) |
60 | { | |
61 | int len = strlen(line); | |
5f5e936d | 62 | struct object_id oid; |
140b378d | 63 | struct object *obj; |
5f5e936d | 64 | const char *p; |
a57114c8 KW |
65 | |
66 | if (!len || line[len-1] != '\n') | |
67 | return -1; | |
68 | line[len-1] = 0; | |
5f5e936d | 69 | if (parse_oid_hex(line, &oid, &p)) |
a57114c8 | 70 | return -1; |
c251c83d | 71 | obj = parse_object(&oid); |
140b378d | 72 | if (!obj) |
a57114c8 | 73 | return -1; |
140b378d | 74 | if (obj->type == OBJ_COMMIT) |
5f5e936d | 75 | return stdin_diff_commit((struct commit *)obj, p); |
140b378d | 76 | if (obj->type == OBJ_TREE) |
5f5e936d | 77 | return stdin_diff_trees((struct tree *)obj, p); |
140b378d | 78 | error("Object %s is a %s, not a commit or tree", |
5f5e936d | 79 | oid_to_hex(&oid), typename(obj->type)); |
140b378d | 80 | return -1; |
a57114c8 KW |
81 | } |
82 | ||
4d1f1190 | 83 | static const char diff_tree_usage[] = |
1b1dd23f | 84 | "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " |
9c9b4f2f | 85 | "[<common-diff-options>] <tree-ish> [<tree-ish>] [<path>...]\n" |
50b8e355 CS |
86 | " -r diff recursively\n" |
87 | " --root include the initial commit as diff against /dev/null\n" | |
dda2d79a | 88 | COMMON_DIFF_OPTIONS_HELP; |
a8db165e | 89 | |
b4490059 JH |
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 | ||
a633fca0 | 100 | int cmd_diff_tree(int argc, const char **argv, const char *prefix) |
73134b6d | 101 | { |
e0965d83 | 102 | char line[1000]; |
cd2bdc53 LT |
103 | struct object *tree1, *tree2; |
104 | static struct rev_info *opt = &log_tree_opt; | |
b4490059 | 105 | struct setup_revision_opt s_r_opt; |
5f1c3f07 | 106 | int read_stdin = 0; |
73134b6d | 107 | |
5a88f97c JH |
108 | if (argc == 2 && !strcmp(argv[1], "-h")) |
109 | usage(diff_tree_usage); | |
110 | ||
37590ce3 | 111 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
a633fca0 | 112 | init_revisions(opt, prefix); |
fd66bcc3 BW |
113 | if (read_cache() < 0) |
114 | die(_("index file corrupt")); | |
8e8f9987 | 115 | opt->abbrev = 0; |
91539833 | 116 | opt->diff = 1; |
8b3dce56 | 117 | opt->disable_stdin = 1; |
b4490059 JH |
118 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
119 | s_r_opt.tweak = diff_tree_tweak_rev; | |
90a78b83 AR |
120 | |
121 | precompose_argv(argc, argv); | |
b4490059 | 122 | argc = setup_revisions(argc, argv, opt, &s_r_opt); |
6b5ee137 | 123 | |
cd2bdc53 LT |
124 | while (--argc > 0) { |
125 | const char *arg = *++argv; | |
c5b42386 | 126 | |
e0965d83 LT |
127 | if (!strcmp(arg, "--stdin")) { |
128 | read_stdin = 1; | |
129 | continue; | |
130 | } | |
c5bac17a | 131 | usage(diff_tree_usage); |
73134b6d LT |
132 | } |
133 | ||
cd2bdc53 | 134 | /* |
8ba74bfd JH |
135 | * NOTE! We expect "a..b" to expand to "^a b" but it is |
136 | * perfectly valid for revision range parser to yield "b ^a", | |
137 | * which means the same thing. If we get the latter, i.e. the | |
138 | * second one is marked UNINTERESTING, we recover the original | |
139 | * order the user gave, i.e. "a..b", by swapping the trees. | |
cd2bdc53 | 140 | */ |
315f49f2 | 141 | switch (opt->pending.nr) { |
0a8365a1 LT |
142 | case 0: |
143 | if (!read_stdin) | |
144 | usage(diff_tree_usage); | |
145 | break; | |
146 | case 1: | |
1f1e895f | 147 | tree1 = opt->pending.objects[0].item; |
315f49f2 | 148 | diff_tree_commit_oid(&tree1->oid); |
0a8365a1 LT |
149 | break; |
150 | case 2: | |
1f1e895f LT |
151 | tree1 = opt->pending.objects[0].item; |
152 | tree2 = opt->pending.objects[1].item; | |
153 | if (tree2->flags & UNINTERESTING) { | |
35d803bc | 154 | SWAP(tree2, tree1); |
1f1e895f | 155 | } |
66f414f8 | 156 | diff_tree_oid(&tree1->oid, &tree2->oid, "", &opt->diffopt); |
5f1c3f07 | 157 | log_tree_diff_flush(opt); |
0a8365a1 LT |
158 | break; |
159 | } | |
160 | ||
62c64895 | 161 | if (read_stdin) { |
f31027c9 JH |
162 | int saved_nrl = 0; |
163 | int saved_dcctc = 0; | |
164 | ||
62c64895 WC |
165 | if (opt->diffopt.detect_rename) |
166 | opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE | | |
167 | DIFF_SETUP_USE_CACHE); | |
168 | while (fgets(line, sizeof(line), stdin)) { | |
5f5e936d | 169 | struct object_id oid; |
e0965d83 | 170 | |
5f5e936d | 171 | if (get_oid_hex(line, &oid)) { |
62c64895 WC |
172 | fputs(line, stdout); |
173 | fflush(stdout); | |
174 | } | |
f31027c9 | 175 | else { |
62c64895 | 176 | diff_tree_stdin(line); |
f31027c9 JH |
177 | if (saved_nrl < opt->diffopt.needed_rename_limit) |
178 | saved_nrl = opt->diffopt.needed_rename_limit; | |
179 | if (opt->diffopt.degraded_cc_to_c) | |
180 | saved_dcctc = 1; | |
181 | } | |
e0c97ca6 | 182 | } |
f31027c9 JH |
183 | opt->diffopt.degraded_cc_to_c = saved_dcctc; |
184 | opt->diffopt.needed_rename_limit = saved_nrl; | |
e0c97ca6 | 185 | } |
da31b358 JH |
186 | |
187 | return diff_result_code(&opt->diffopt, 0); | |
9174026c | 188 | } |