Make the "struct tree_desc" operations available to others
[git/haiku.git] / diff-tree.c
blob6593a6920adcdb81fafad94ba2cf96431e6ceec9
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
5 static int show_root_diff = 0;
6 static int no_commit_id = 0;
7 static int verbose_header = 0;
8 static int ignore_merges = 1;
9 static int show_empty_combined = 0;
10 static int combine_merges = 0;
11 static int dense_combined_merges = 0;
12 static int read_stdin = 0;
14 static const char *header = NULL;
15 static const char *header_prefix = "";
16 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
18 static struct diff_options diff_options;
20 static int call_diff_flush(void)
22 diffcore_std(&diff_options);
23 if (diff_queue_is_empty()) {
24 int saved_fmt = diff_options.output_format;
25 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
26 diff_flush(&diff_options);
27 diff_options.output_format = saved_fmt;
28 return 0;
30 if (header) {
31 if (!no_commit_id)
32 printf("%s%c", header, diff_options.line_termination);
33 header = NULL;
35 diff_flush(&diff_options);
36 return 1;
39 static int diff_tree_sha1_top(const unsigned char *old,
40 const unsigned char *new, const char *base)
42 int ret;
44 ret = diff_tree_sha1(old, new, base, &diff_options);
45 call_diff_flush();
46 return ret;
49 static int diff_root_tree(const unsigned char *new, const char *base)
51 int retval;
52 void *tree;
53 struct tree_desc empty, real;
55 tree = read_object_with_reference(new, "tree", &real.size, NULL);
56 if (!tree)
57 die("unable to read root tree (%s)", sha1_to_hex(new));
58 real.buf = tree;
60 empty.buf = "";
61 empty.size = 0;
62 retval = diff_tree(&empty, &real, base, &diff_options);
63 free(tree);
64 call_diff_flush();
65 return retval;
68 static const char *generate_header(const unsigned char *commit_sha1,
69 const unsigned char *parent_sha1,
70 const struct commit *commit)
72 static char this_header[16384];
73 int offset;
74 unsigned long len;
75 int abbrev = diff_options.abbrev;
76 const char *msg = commit->buffer;
78 if (!verbose_header)
79 return sha1_to_hex(commit_sha1);
81 len = strlen(msg);
83 offset = sprintf(this_header, "%s%s ",
84 header_prefix,
85 diff_unique_abbrev(commit_sha1, abbrev));
86 if (commit_sha1 != parent_sha1)
87 offset += sprintf(this_header + offset, "(from %s)\n",
88 parent_sha1
89 ? diff_unique_abbrev(parent_sha1, abbrev)
90 : "root");
91 else
92 offset += sprintf(this_header + offset, "(from parents)\n");
93 offset += pretty_print_commit(commit_format, commit, len,
94 this_header + offset,
95 sizeof(this_header) - offset, abbrev);
96 return this_header;
99 static int diff_tree_commit(const unsigned char *commit_sha1)
101 struct commit *commit;
102 struct commit_list *parents;
103 char name[50];
104 unsigned char sha1[20];
106 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
107 if (get_sha1(name, sha1))
108 return -1;
109 name[40] = 0;
110 commit = lookup_commit(sha1);
112 /* Root commit? */
113 if (show_root_diff && !commit->parents) {
114 header = generate_header(sha1, NULL, commit);
115 diff_root_tree(commit_sha1, "");
118 /* More than one parent? */
119 if (commit->parents && commit->parents->next) {
120 if (ignore_merges)
121 return 0;
122 else if (combine_merges) {
123 header = generate_header(sha1, sha1, commit);
124 return diff_tree_combined_merge(sha1, header,
125 show_empty_combined,
126 dense_combined_merges);
130 for (parents = commit->parents; parents; parents = parents->next) {
131 struct commit *parent = parents->item;
132 header = generate_header(sha1, parent->object.sha1, commit);
133 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
134 if (!header && verbose_header) {
135 header_prefix = "\ndiff-tree ";
137 * Don't print multiple merge entries if we
138 * don't print the diffs.
142 return 0;
145 static int diff_tree_stdin(char *line)
147 int len = strlen(line);
148 unsigned char commit[20], parent[20];
149 static char this_header[1000];
150 int abbrev = diff_options.abbrev;
152 if (!len || line[len-1] != '\n')
153 return -1;
154 line[len-1] = 0;
155 if (get_sha1_hex(line, commit))
156 return -1;
157 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
158 line[40] = 0;
159 line[81] = 0;
160 sprintf(this_header, "%s (from %s)\n",
161 diff_unique_abbrev(commit, abbrev),
162 diff_unique_abbrev(parent, abbrev));
163 header = this_header;
164 return diff_tree_sha1_top(parent, commit, "");
166 line[40] = 0;
167 return diff_tree_commit(commit);
170 static const char diff_tree_usage[] =
171 "git-diff-tree [--stdin] [-m] [-c] [--cc] [-s] [-v] [--pretty] [-t] [-r] [--root] "
172 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
173 " -r diff recursively\n"
174 " --root include the initial commit as diff against /dev/null\n"
175 COMMON_DIFF_OPTIONS_HELP;
177 int main(int argc, const char **argv)
179 int nr_sha1;
180 char line[1000];
181 unsigned char sha1[2][20];
182 const char *prefix = setup_git_directory();
184 git_config(git_diff_config);
185 nr_sha1 = 0;
186 diff_setup(&diff_options);
188 for (;;) {
189 int diff_opt_cnt;
190 const char *arg;
192 argv++;
193 argc--;
194 arg = *argv;
195 if (!arg)
196 break;
198 if (*arg != '-') {
199 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
200 nr_sha1++;
201 continue;
203 break;
206 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
207 if (diff_opt_cnt < 0)
208 usage(diff_tree_usage);
209 else if (diff_opt_cnt) {
210 argv += diff_opt_cnt - 1;
211 argc -= diff_opt_cnt - 1;
212 continue;
216 if (!strcmp(arg, "--")) {
217 argv++;
218 argc--;
219 break;
221 if (!strcmp(arg, "-r")) {
222 diff_options.recursive = 1;
223 continue;
225 if (!strcmp(arg, "-t")) {
226 diff_options.recursive = 1;
227 diff_options.tree_in_recursive = 1;
228 continue;
230 if (!strcmp(arg, "-m")) {
231 ignore_merges = 0;
232 continue;
234 if (!strcmp(arg, "-c")) {
235 combine_merges = 1;
236 continue;
238 if (!strcmp(arg, "--cc")) {
239 dense_combined_merges = combine_merges = 1;
240 continue;
242 if (!strcmp(arg, "-v")) {
243 verbose_header = 1;
244 header_prefix = "diff-tree ";
245 continue;
247 if (!strncmp(arg, "--pretty", 8)) {
248 verbose_header = 1;
249 header_prefix = "diff-tree ";
250 commit_format = get_commit_format(arg+8);
251 continue;
253 if (!strcmp(arg, "--stdin")) {
254 read_stdin = 1;
255 continue;
257 if (!strcmp(arg, "--root")) {
258 show_root_diff = 1;
259 continue;
261 if (!strcmp(arg, "--no-commit-id")) {
262 no_commit_id = 1;
263 continue;
265 usage(diff_tree_usage);
267 if (diff_options.output_format == DIFF_FORMAT_PATCH)
268 diff_options.recursive = 1;
270 if (combine_merges) {
271 diff_options.output_format = DIFF_FORMAT_PATCH;
272 show_empty_combined = !ignore_merges;
273 ignore_merges = 0;
276 diff_tree_setup_paths(get_pathspec(prefix, argv));
277 diff_setup_done(&diff_options);
279 switch (nr_sha1) {
280 case 0:
281 if (!read_stdin)
282 usage(diff_tree_usage);
283 break;
284 case 1:
285 diff_tree_commit(sha1[0]);
286 break;
287 case 2:
288 diff_tree_sha1_top(sha1[0], sha1[1], "");
289 break;
292 if (!read_stdin)
293 return 0;
295 if (diff_options.detect_rename)
296 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
297 DIFF_SETUP_USE_CACHE);
298 while (fgets(line, sizeof(line), stdin))
299 diff_tree_stdin(line);
301 return 0;