]>
Commit | Line | Data |
---|---|---|
9174026c | 1 | #include "cache.h" |
3ebfd4aa | 2 | #include "diff.h" |
e3bc7a3b | 3 | #include "commit.h" |
5f1c3f07 | 4 | #include "log-tree.h" |
e8cc9cd9 | 5 | #include "builtin.h" |
302ad7a9 | 6 | #include "submodule.h" |
9174026c | 7 | |
cd2bdc53 | 8 | static struct rev_info log_tree_opt; |
b11645be | 9 | |
45392a64 JH |
10 | static int diff_tree_commit_sha1(const unsigned char *sha1) |
11 | { | |
12 | struct commit *commit = lookup_commit_reference(sha1); | |
13 | if (!commit) | |
14 | return -1; | |
5f1c3f07 | 15 | return log_tree_commit(&log_tree_opt, commit); |
45392a64 JH |
16 | } |
17 | ||
a57114c8 KW |
18 | /* Diff one or more commits. */ |
19 | static int stdin_diff_commit(struct commit *commit, char *line, int len) | |
b11645be | 20 | { |
45392a64 | 21 | unsigned char sha1[20]; |
45392a64 JH |
22 | if (isspace(line[40]) && !get_sha1_hex(line+41, sha1)) { |
23 | /* Graft the fake parents locally to the commit */ | |
24 | int pos = 41; | |
25 | struct commit_list **pptr, *parents; | |
26 | ||
27 | /* Free the real parent list */ | |
28 | for (parents = commit->parents; parents; ) { | |
29 | struct commit_list *tmp = parents->next; | |
30 | free(parents); | |
31 | parents = tmp; | |
32 | } | |
33 | commit->parents = NULL; | |
34 | pptr = &(commit->parents); | |
35 | while (line[pos] && !get_sha1_hex(line + pos, sha1)) { | |
36 | struct commit *parent = lookup_commit(sha1); | |
37 | if (parent) { | |
38 | pptr = &commit_list_insert(parent, pptr)->next; | |
39 | } | |
40 | pos += 41; | |
41 | } | |
b11645be | 42 | } |
5f1c3f07 | 43 | return log_tree_commit(&log_tree_opt, commit); |
e0965d83 LT |
44 | } |
45 | ||
140b378d KW |
46 | /* Diff two trees. */ |
47 | static int stdin_diff_trees(struct tree *tree1, char *line, int len) | |
48 | { | |
49 | unsigned char sha1[20]; | |
50 | struct tree *tree2; | |
51 | if (len != 82 || !isspace(line[40]) || get_sha1_hex(line + 41, sha1)) | |
52 | return error("Need exactly two trees, separated by a space"); | |
53 | tree2 = lookup_tree(sha1); | |
54 | if (!tree2 || parse_tree(tree2)) | |
55 | return -1; | |
56 | printf("%s %s\n", sha1_to_hex(tree1->object.sha1), | |
57 | sha1_to_hex(tree2->object.sha1)); | |
58 | diff_tree_sha1(tree1->object.sha1, tree2->object.sha1, | |
59 | "", &log_tree_opt.diffopt); | |
60 | log_tree_diff_flush(&log_tree_opt); | |
61 | return 0; | |
62 | } | |
63 | ||
a57114c8 KW |
64 | static int diff_tree_stdin(char *line) |
65 | { | |
66 | int len = strlen(line); | |
67 | unsigned char sha1[20]; | |
140b378d | 68 | struct object *obj; |
a57114c8 KW |
69 | |
70 | if (!len || line[len-1] != '\n') | |
71 | return -1; | |
72 | line[len-1] = 0; | |
73 | if (get_sha1_hex(line, sha1)) | |
74 | return -1; | |
628b06d7 JH |
75 | obj = lookup_unknown_object(sha1); |
76 | if (!obj || !obj->parsed) | |
77 | obj = parse_object(sha1); | |
140b378d | 78 | if (!obj) |
a57114c8 | 79 | return -1; |
140b378d KW |
80 | if (obj->type == OBJ_COMMIT) |
81 | return stdin_diff_commit((struct commit *)obj, line, len); | |
82 | if (obj->type == OBJ_TREE) | |
83 | return stdin_diff_trees((struct tree *)obj, line, len); | |
84 | error("Object %s is a %s, not a commit or tree", | |
85 | sha1_to_hex(sha1), typename(obj->type)); | |
86 | return -1; | |
a57114c8 KW |
87 | } |
88 | ||
4d1f1190 | 89 | static const char diff_tree_usage[] = |
1b1dd23f | 90 | "git diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] " |
50b8e355 CS |
91 | "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n" |
92 | " -r diff recursively\n" | |
93 | " --root include the initial commit as diff against /dev/null\n" | |
dda2d79a | 94 | COMMON_DIFF_OPTIONS_HELP; |
a8db165e | 95 | |
b4490059 JH |
96 | static void diff_tree_tweak_rev(struct rev_info *rev, struct setup_revision_opt *opt) |
97 | { | |
98 | if (!rev->diffopt.output_format) { | |
99 | if (rev->dense_combined_merges) | |
100 | rev->diffopt.output_format = DIFF_FORMAT_PATCH; | |
101 | else | |
102 | rev->diffopt.output_format = DIFF_FORMAT_RAW; | |
103 | } | |
104 | } | |
105 | ||
a633fca0 | 106 | int cmd_diff_tree(int argc, const char **argv, const char *prefix) |
73134b6d | 107 | { |
0a8365a1 | 108 | int nr_sha1; |
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; |
5f1c3f07 | 113 | int read_stdin = 0; |
73134b6d | 114 | |
a633fca0 | 115 | init_revisions(opt, prefix); |
302ad7a9 | 116 | gitmodules_config(); |
ef90d6d4 | 117 | git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ |
8e8f9987 | 118 | opt->abbrev = 0; |
91539833 | 119 | opt->diff = 1; |
8b3dce56 | 120 | opt->disable_stdin = 1; |
b4490059 JH |
121 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
122 | s_r_opt.tweak = diff_tree_tweak_rev; | |
123 | argc = setup_revisions(argc, argv, opt, &s_r_opt); | |
6b5ee137 | 124 | |
cd2bdc53 LT |
125 | while (--argc > 0) { |
126 | const char *arg = *++argv; | |
c5b42386 | 127 | |
e0965d83 LT |
128 | if (!strcmp(arg, "--stdin")) { |
129 | read_stdin = 1; | |
130 | continue; | |
131 | } | |
c5bac17a | 132 | usage(diff_tree_usage); |
73134b6d LT |
133 | } |
134 | ||
cd2bdc53 | 135 | /* |
1f1e895f LT |
136 | * NOTE! We expect "a ^b" to be equal to "a..b", so we |
137 | * reverse the order of the objects if the second one | |
138 | * is marked UNINTERESTING. | |
cd2bdc53 | 139 | */ |
1f1e895f | 140 | nr_sha1 = opt->pending.nr; |
0a8365a1 LT |
141 | switch (nr_sha1) { |
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; |
cd2bdc53 | 148 | diff_tree_commit_sha1(tree1->sha1); |
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) { | |
154 | struct object *tmp = tree2; | |
155 | tree2 = tree1; | |
156 | tree1 = tmp; | |
157 | } | |
cd2bdc53 LT |
158 | diff_tree_sha1(tree1->sha1, |
159 | tree2->sha1, | |
160 | "", &opt->diffopt); | |
5f1c3f07 | 161 | log_tree_diff_flush(opt); |
0a8365a1 LT |
162 | break; |
163 | } | |
164 | ||
62c64895 | 165 | if (read_stdin) { |
f31027c9 JH |
166 | int saved_nrl = 0; |
167 | int saved_dcctc = 0; | |
168 | ||
62c64895 WC |
169 | if (opt->diffopt.detect_rename) |
170 | opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE | | |
171 | DIFF_SETUP_USE_CACHE); | |
172 | while (fgets(line, sizeof(line), stdin)) { | |
173 | unsigned char sha1[20]; | |
e0965d83 | 174 | |
62c64895 WC |
175 | if (get_sha1_hex(line, sha1)) { |
176 | fputs(line, stdout); | |
177 | fflush(stdout); | |
178 | } | |
f31027c9 | 179 | else { |
62c64895 | 180 | diff_tree_stdin(line); |
f31027c9 JH |
181 | if (saved_nrl < opt->diffopt.needed_rename_limit) |
182 | saved_nrl = opt->diffopt.needed_rename_limit; | |
183 | if (opt->diffopt.degraded_cc_to_c) | |
184 | saved_dcctc = 1; | |
185 | } | |
e0c97ca6 | 186 | } |
f31027c9 JH |
187 | opt->diffopt.degraded_cc_to_c = saved_dcctc; |
188 | opt->diffopt.needed_rename_limit = saved_nrl; | |
e0c97ca6 | 189 | } |
da31b358 JH |
190 | |
191 | return diff_result_code(&opt->diffopt, 0); | |
9174026c | 192 | } |