pretty_print_commit(): pass commit object instead of commit->buffer.
[git/jrn.git] / diff-tree.c
blob44bc2381c9cd4ab6e10e8bd8b519b564a1b9014b
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 read_stdin = 0;
11 static const char *header = NULL;
12 static const char *header_prefix = "";
13 static enum cmit_fmt commit_format = CMIT_FMT_RAW;
15 static struct diff_options diff_options;
17 static int call_diff_flush(void)
19 diffcore_std(&diff_options);
20 if (diff_queue_is_empty()) {
21 int saved_fmt = diff_options.output_format;
22 diff_options.output_format = DIFF_FORMAT_NO_OUTPUT;
23 diff_flush(&diff_options);
24 diff_options.output_format = saved_fmt;
25 return 0;
27 if (header) {
28 if (!no_commit_id)
29 printf("%s%c", header, diff_options.line_termination);
30 header = NULL;
32 diff_flush(&diff_options);
33 return 1;
36 static int diff_tree_sha1_top(const unsigned char *old,
37 const unsigned char *new, const char *base)
39 int ret;
41 ret = diff_tree_sha1(old, new, base, &diff_options);
42 call_diff_flush();
43 return ret;
46 static int diff_root_tree(const unsigned char *new, const char *base)
48 int retval;
49 void *tree;
50 struct tree_desc empty, real;
52 tree = read_object_with_reference(new, "tree", &real.size, NULL);
53 if (!tree)
54 die("unable to read root tree (%s)", sha1_to_hex(new));
55 real.buf = tree;
57 empty.buf = "";
58 empty.size = 0;
59 retval = diff_tree(&empty, &real, base, &diff_options);
60 free(tree);
61 call_diff_flush();
62 return retval;
65 static const char *generate_header(const unsigned char *commit_sha1,
66 const unsigned char *parent_sha1,
67 const struct commit *commit)
69 static char this_header[16384];
70 int offset;
71 unsigned long len;
72 int abbrev = diff_options.abbrev;
73 const char *msg = commit->buffer;
75 if (!verbose_header)
76 return sha1_to_hex(commit_sha1);
78 len = strlen(msg);
80 offset = sprintf(this_header, "%s%s ",
81 header_prefix,
82 diff_unique_abbrev(commit_sha1, abbrev));
83 offset += sprintf(this_header + offset, "(from %s)\n",
84 parent_sha1 ?
85 diff_unique_abbrev(parent_sha1, abbrev) : "root");
86 offset += pretty_print_commit(commit_format, commit, len,
87 this_header + offset,
88 sizeof(this_header) - offset, abbrev);
89 return this_header;
92 static int diff_tree_commit(const unsigned char *commit_sha1)
94 struct commit *commit;
95 struct commit_list *parents;
96 char name[50];
97 unsigned char sha1[20];
99 sprintf(name, "%s^0", sha1_to_hex(commit_sha1));
100 if (get_sha1(name, sha1))
101 return -1;
102 name[40] = 0;
103 commit = lookup_commit(sha1);
105 /* Root commit? */
106 if (show_root_diff && !commit->parents) {
107 header = generate_header(sha1, NULL, commit);
108 diff_root_tree(commit_sha1, "");
111 /* More than one parent? */
112 if (ignore_merges && commit->parents && commit->parents->next)
113 return 0;
115 for (parents = commit->parents; parents; parents = parents->next) {
116 struct commit *parent = parents->item;
117 header = generate_header(sha1, parent->object.sha1, commit);
118 diff_tree_sha1_top(parent->object.sha1, commit_sha1, "");
119 if (!header && verbose_header) {
120 header_prefix = "\ndiff-tree ";
122 * Don't print multiple merge entries if we
123 * don't print the diffs.
127 return 0;
130 static int diff_tree_stdin(char *line)
132 int len = strlen(line);
133 unsigned char commit[20], parent[20];
134 static char this_header[1000];
135 int abbrev = diff_options.abbrev;
137 if (!len || line[len-1] != '\n')
138 return -1;
139 line[len-1] = 0;
140 if (get_sha1_hex(line, commit))
141 return -1;
142 if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
143 line[40] = 0;
144 line[81] = 0;
145 sprintf(this_header, "%s (from %s)\n",
146 diff_unique_abbrev(commit, abbrev),
147 diff_unique_abbrev(parent, abbrev));
148 header = this_header;
149 return diff_tree_sha1_top(parent, commit, "");
151 line[40] = 0;
152 return diff_tree_commit(commit);
155 static const char diff_tree_usage[] =
156 "git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] [-r] [--root] "
157 "[<common diff options>] <tree-ish> [<tree-ish>] [<path>...]\n"
158 " -r diff recursively\n"
159 " --root include the initial commit as diff against /dev/null\n"
160 COMMON_DIFF_OPTIONS_HELP;
162 int main(int argc, const char **argv)
164 int nr_sha1;
165 char line[1000];
166 unsigned char sha1[2][20];
167 const char *prefix = setup_git_directory();
169 git_config(git_diff_config);
170 nr_sha1 = 0;
171 diff_setup(&diff_options);
173 for (;;) {
174 int diff_opt_cnt;
175 const char *arg;
177 argv++;
178 argc--;
179 arg = *argv;
180 if (!arg)
181 break;
183 if (*arg != '-') {
184 if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
185 nr_sha1++;
186 continue;
188 break;
191 diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
192 if (diff_opt_cnt < 0)
193 usage(diff_tree_usage);
194 else if (diff_opt_cnt) {
195 argv += diff_opt_cnt - 1;
196 argc -= diff_opt_cnt - 1;
197 continue;
201 if (!strcmp(arg, "--")) {
202 argv++;
203 argc--;
204 break;
206 if (!strcmp(arg, "-r")) {
207 diff_options.recursive = 1;
208 continue;
210 if (!strcmp(arg, "-t")) {
211 diff_options.recursive = 1;
212 diff_options.tree_in_recursive = 1;
213 continue;
215 if (!strcmp(arg, "-m")) {
216 ignore_merges = 0;
217 continue;
219 if (!strcmp(arg, "-v")) {
220 verbose_header = 1;
221 header_prefix = "diff-tree ";
222 continue;
224 if (!strncmp(arg, "--pretty", 8)) {
225 verbose_header = 1;
226 header_prefix = "diff-tree ";
227 commit_format = get_commit_format(arg+8);
228 continue;
230 if (!strcmp(arg, "--stdin")) {
231 read_stdin = 1;
232 continue;
234 if (!strcmp(arg, "--root")) {
235 show_root_diff = 1;
236 continue;
238 if (!strcmp(arg, "--no-commit-id")) {
239 no_commit_id = 1;
240 continue;
242 usage(diff_tree_usage);
244 if (diff_options.output_format == DIFF_FORMAT_PATCH)
245 diff_options.recursive = 1;
247 diff_tree_setup_paths(get_pathspec(prefix, argv));
248 diff_setup_done(&diff_options);
250 switch (nr_sha1) {
251 case 0:
252 if (!read_stdin)
253 usage(diff_tree_usage);
254 break;
255 case 1:
256 diff_tree_commit(sha1[0]);
257 break;
258 case 2:
259 diff_tree_sha1_top(sha1[0], sha1[1], "");
260 break;
263 if (!read_stdin)
264 return 0;
266 if (diff_options.detect_rename)
267 diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
268 DIFF_SETUP_USE_CACHE);
269 while (fgets(line, sizeof(line), stdin))
270 diff_tree_stdin(line);
272 return 0;