@matheusbsbs / matheus-bagliano / commits / 5f1c3f07b7
Fork 0

log-tree: separate major part of diff-tree.

This separates out the part that deals with one-commit diff-tree (and --stdin form) into a separate log-tree module. There are two goals with this. The more important one is to be able to make this part available to "git log --diff", so that we can have a native "git whatchanged" command. Another is to simplify the commit log generation part simpler. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 9, 2006 at 01:11 UTC 5f1c3f07b7f4a8436c7773b8e7a04998190c125e
4 files changed +224 -204
Makefile
+2 -2
@@ -194,12 +194,12 @@ LIB_H = \
194 blob.h cache.h commit.h csum-file.h delta.h \
195 diff.h object.h pack.h pkt-line.h quote.h refs.h \
196 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
197 - tree-walk.h
197 + tree-walk.h log-tree.h
198
199 DIFF_OBJS = \
200 diff.o diffcore-break.o diffcore-order.o diffcore-pathspec.o \
201 diffcore-pickaxe.o diffcore-rename.o tree-diff.o combine-diff.o \
202 - diffcore-delta.o
202 + diffcore-delta.o log-tree.o
203
204 LIB_OBJS = \
205 blob.o commit.o connect.o csum-file.o \
diff-tree.c
+24 -202
@@ -1,152 +1,16 @@
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 +#include "log-tree.h"
5
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 combine_merges = 0;
10 -static int dense_combined_merges = 0;
11 -static int read_stdin = 0;
12 -static int always_show_header = 0;
13 -
14 -static const char *header = NULL;
15 -static const char *header_prefix = "";
16 -static enum cmit_fmt commit_format = CMIT_FMT_RAW;
17 -
18 -static struct diff_options diff_options;
19 -
20 -static int call_diff_flush(void)
21 -{
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;
29 - }
30 - if (header) {
31 - if (!no_commit_id)
32 - printf("%s%c", header, diff_options.line_termination);
33 - header = NULL;
34 - }
35 - diff_flush(&diff_options);
36 - return 1;
37 -}
38 -
39 -static int diff_tree_sha1_top(const unsigned char *old,
40 - const unsigned char *new, const char *base)
41 -{
42 - int ret;
43 -
44 - ret = diff_tree_sha1(old, new, base, &diff_options);
45 - call_diff_flush();
46 - return ret;
47 -}
48 -
49 -static int diff_root_tree(const unsigned char *new, const char *base)
50 -{
51 - int retval;
52 - void *tree;
53 - struct tree_desc empty, real;
54 -
55 - tree = read_object_with_reference(new, tree_type, &real.size, NULL);
56 - if (!tree)
57 - die("unable to read root tree (%s)", sha1_to_hex(new));
58 - real.buf = tree;
59 -
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;
66 -}
67 -
68 -static const char *generate_header(const unsigned char *commit_sha1,
69 - const unsigned char *parent_sha1,
70 - const struct commit *commit)
71 -{
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;
77 -
78 - if (!verbose_header)
79 - return sha1_to_hex(commit_sha1);
80 -
81 - len = strlen(msg);
82 -
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 - if (always_show_header) {
97 - puts(this_header);
98 - return NULL;
99 - }
100 - return this_header;
101 -}
102 -
103 -static int diff_tree_commit(struct commit *commit)
104 -{
105 - struct commit_list *parents;
106 - unsigned const char *sha1 = commit->object.sha1;
107 -
108 - /* Root commit? */
109 - if (show_root_diff && !commit->parents) {
110 - header = generate_header(sha1, NULL, commit);
111 - diff_root_tree(sha1, "");
112 - }
113 -
114 - /* More than one parent? */
115 - if (commit->parents && commit->parents->next) {
116 - if (ignore_merges)
117 - return 0;
118 - else if (combine_merges) {
119 - header = generate_header(sha1, sha1, commit);
120 - header = diff_tree_combined_merge(sha1, header,
121 - dense_combined_merges,
122 - &diff_options);
123 - if (!header && verbose_header)
124 - header_prefix = "\ndiff-tree ";
125 - return 0;
126 - }
127 - }
128 -
129 - for (parents = commit->parents; parents; parents = parents->next) {
130 - struct commit *parent = parents->item;
131 - header = generate_header(sha1, parent->object.sha1, commit);
132 - diff_tree_sha1_top(parent->object.sha1, sha1, "");
133 - if (!header && verbose_header) {
134 - header_prefix = "\ndiff-tree ";
135 - /*
136 - * Don't print multiple merge entries if we
137 - * don't print the diffs.
138 - */
139 - }
140 - }
141 - return 0;
142 -}
6 +static struct log_tree_opt log_tree_opt;
7
8 static int diff_tree_commit_sha1(const unsigned char *sha1)
9 {
10 struct commit *commit = lookup_commit_reference(sha1);
11 if (!commit)
12 return -1;
149 - return diff_tree_commit(commit);
13 + return log_tree_commit(&log_tree_opt, commit);
14 }
15
16 static int diff_tree_stdin(char *line)
@@ -184,7 +48,7 @@ static int diff_tree_stdin(char *line)
48 pos += 41;
49 }
50 }
187 - return diff_tree_commit(commit);
51 + return log_tree_commit(&log_tree_opt, commit);
52 }
53
54 static const char diff_tree_usage[] =
@@ -200,13 +64,15 @@ int main(int argc, const char **argv)
64 char line[1000];
65 unsigned char sha1[2][20];
66 const char *prefix = setup_git_directory();
67 + static struct log_tree_opt *opt = &log_tree_opt;
68 + int read_stdin = 0;
69
70 git_config(git_diff_config);
71 nr_sha1 = 0;
206 - diff_setup(&diff_options);
72 + init_log_tree_opt(opt);
73
74 for (;;) {
209 - int diff_opt_cnt;
75 + int opt_cnt;
76 const char *arg;
77
78 argv++;
@@ -223,84 +89,39 @@ int main(int argc, const char **argv)
89 break;
90 }
91
226 - diff_opt_cnt = diff_opt_parse(&diff_options, argv, argc);
227 - if (diff_opt_cnt < 0)
92 + opt_cnt = log_tree_opt_parse(opt, argv, argc);
93 + if (opt_cnt < 0)
94 usage(diff_tree_usage);
229 - else if (diff_opt_cnt) {
230 - argv += diff_opt_cnt - 1;
231 - argc -= diff_opt_cnt - 1;
95 + else if (opt_cnt) {
96 + argv += opt_cnt - 1;
97 + argc -= opt_cnt - 1;
98 continue;
99 }
100
235 -
101 if (!strcmp(arg, "--")) {
102 argv++;
103 argc--;
104 break;
105 }
241 - if (!strcmp(arg, "-r")) {
242 - diff_options.recursive = 1;
243 - continue;
244 - }
245 - if (!strcmp(arg, "-t")) {
246 - diff_options.recursive = 1;
247 - diff_options.tree_in_recursive = 1;
248 - continue;
249 - }
250 - if (!strcmp(arg, "-m")) {
251 - ignore_merges = 0;
252 - continue;
253 - }
254 - if (!strcmp(arg, "-c")) {
255 - combine_merges = 1;
256 - continue;
257 - }
258 - if (!strcmp(arg, "--cc")) {
259 - dense_combined_merges = combine_merges = 1;
260 - continue;
261 - }
262 - if (!strcmp(arg, "-v")) {
263 - verbose_header = 1;
264 - header_prefix = "diff-tree ";
265 - continue;
266 - }
267 - if (!strncmp(arg, "--pretty", 8)) {
268 - verbose_header = 1;
269 - header_prefix = "diff-tree ";
270 - commit_format = get_commit_format(arg+8);
271 - continue;
272 - }
106 if (!strcmp(arg, "--stdin")) {
107 read_stdin = 1;
108 continue;
109 }
277 - if (!strcmp(arg, "--root")) {
278 - show_root_diff = 1;
279 - continue;
280 - }
281 - if (!strcmp(arg, "--no-commit-id")) {
282 - no_commit_id = 1;
283 - continue;
284 - }
285 - if (!strcmp(arg, "--always")) {
286 - always_show_header = 1;
287 - continue;
288 - }
110 usage(diff_tree_usage);
111 }
112
292 - if (combine_merges)
293 - ignore_merges = 0;
113 + if (opt->combine_merges)
114 + opt->ignore_merges = 0;
115
116 /* We can only do dense combined merges with diff output */
296 - if (dense_combined_merges)
297 - diff_options.output_format = DIFF_FORMAT_PATCH;
117 + if (opt->dense_combined_merges)
118 + opt->diffopt.output_format = DIFF_FORMAT_PATCH;
119
299 - if (diff_options.output_format == DIFF_FORMAT_PATCH)
300 - diff_options.recursive = 1;
120 + if (opt->diffopt.output_format == DIFF_FORMAT_PATCH)
121 + opt->diffopt.recursive = 1;
122
123 diff_tree_setup_paths(get_pathspec(prefix, argv));
303 - diff_setup_done(&diff_options);
124 + diff_setup_done(&opt->diffopt);
125
126 switch (nr_sha1) {
127 case 0:
@@ -311,15 +132,16 @@ int main(int argc, const char **argv)
132 diff_tree_commit_sha1(sha1[0]);
133 break;
134 case 2:
314 - diff_tree_sha1_top(sha1[0], sha1[1], "");
135 + diff_tree_sha1(sha1[0], sha1[1], "", &opt->diffopt);
136 + log_tree_diff_flush(opt);
137 break;
138 }
139
140 if (!read_stdin)
141 return 0;
142
321 - if (diff_options.detect_rename)
322 - diff_options.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
143 + if (opt->diffopt.detect_rename)
144 + opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
145 DIFF_SETUP_USE_CACHE);
146 while (fgets(line, sizeof(line), stdin))
147 diff_tree_stdin(line);
log-tree.c new
+175
@@ -0,0 +1,175 @@
1 +#include "cache.h"
2 +#include "diff.h"
3 +#include "commit.h"
4 +#include "log-tree.h"
5 +
6 +void init_log_tree_opt(struct log_tree_opt *opt)
7 +{
8 + memset(opt, 0, sizeof *opt);
9 + opt->ignore_merges = 1;
10 + opt->header_prefix = "";
11 + opt->commit_format = CMIT_FMT_RAW;
12 + diff_setup(&opt->diffopt);
13 +}
14 +
15 +int log_tree_opt_parse(struct log_tree_opt *opt, const char **av, int ac)
16 +{
17 + const char *arg;
18 + int cnt = diff_opt_parse(&opt->diffopt, av, ac);
19 + if (0 < cnt)
20 + return cnt;
21 + arg = *av;
22 + if (!strcmp(arg, "-r"))
23 + opt->diffopt.recursive = 1;
24 + else if (!strcmp(arg, "-t")) {
25 + opt->diffopt.recursive = 1;
26 + opt->diffopt.tree_in_recursive = 1;
27 + }
28 + else if (!strcmp(arg, "-m"))
29 + opt->ignore_merges = 0;
30 + else if (!strcmp(arg, "-c"))
31 + opt->combine_merges = 1;
32 + else if (!strcmp(arg, "--cc")) {
33 + opt->dense_combined_merges = 1;
34 + opt->combine_merges = 1;
35 + }
36 + else if (!strcmp(arg, "-v")) {
37 + opt->verbose_header = 1;
38 + opt->header_prefix = "diff-tree ";
39 + }
40 + else if (!strncmp(arg, "--pretty", 8)) {
41 + opt->verbose_header = 1;
42 + opt->header_prefix = "diff-tree ";
43 + opt->commit_format = get_commit_format(arg+8);
44 + }
45 + else if (!strcmp(arg, "--root"))
46 + opt->show_root_diff = 1;
47 + else if (!strcmp(arg, "--no-commit-id"))
48 + opt->no_commit_id = 1;
49 + else if (!strcmp(arg, "--always"))
50 + opt->always_show_header = 1;
51 + else
52 + return 0;
53 + return 1;
54 +}
55 +
56 +int log_tree_diff_flush(struct log_tree_opt *opt)
57 +{
58 + diffcore_std(&opt->diffopt);
59 + if (diff_queue_is_empty()) {
60 + int saved_fmt = opt->diffopt.output_format;
61 + opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
62 + diff_flush(&opt->diffopt);
63 + opt->diffopt.output_format = saved_fmt;
64 + return 0;
65 + }
66 + if (opt->header) {
67 + if (!opt->no_commit_id)
68 + printf("%s%c", opt->header,
69 + opt->diffopt.line_termination);
70 + opt->header = NULL;
71 + }
72 + diff_flush(&opt->diffopt);
73 + return 1;
74 +}
75 +
76 +static int diff_root_tree(struct log_tree_opt *opt,
77 + const unsigned char *new, const char *base)
78 +{
79 + int retval;
80 + void *tree;
81 + struct tree_desc empty, real;
82 +
83 + tree = read_object_with_reference(new, tree_type, &real.size, NULL);
84 + if (!tree)
85 + die("unable to read root tree (%s)", sha1_to_hex(new));
86 + real.buf = tree;
87 +
88 + empty.buf = "";
89 + empty.size = 0;
90 + retval = diff_tree(&empty, &real, base, &opt->diffopt);
91 + free(tree);
92 + log_tree_diff_flush(opt);
93 + return retval;
94 +}
95 +
96 +static const char *generate_header(struct log_tree_opt *opt,
97 + const unsigned char *commit_sha1,
98 + const unsigned char *parent_sha1,
99 + const struct commit *commit)
100 +{
101 + static char this_header[16384];
102 + int offset;
103 + unsigned long len;
104 + int abbrev = opt->diffopt.abbrev;
105 + const char *msg = commit->buffer;
106 +
107 + if (!opt->verbose_header)
108 + return sha1_to_hex(commit_sha1);
109 +
110 + len = strlen(msg);
111 +
112 + offset = sprintf(this_header, "%s%s ",
113 + opt->header_prefix,
114 + diff_unique_abbrev(commit_sha1, abbrev));
115 + if (commit_sha1 != parent_sha1)
116 + offset += sprintf(this_header + offset, "(from %s)\n",
117 + parent_sha1
118 + ? diff_unique_abbrev(parent_sha1, abbrev)
119 + : "root");
120 + else
121 + offset += sprintf(this_header + offset, "(from parents)\n");
122 + offset += pretty_print_commit(opt->commit_format, commit, len,
123 + this_header + offset,
124 + sizeof(this_header) - offset, abbrev);
125 + if (opt->always_show_header) {
126 + puts(this_header);
127 + return NULL;
128 + }
129 + return this_header;
130 +}
131 +
132 +static int do_diff_combined(struct log_tree_opt *opt, struct commit *commit)
133 +{
134 + unsigned const char *sha1 = commit->object.sha1;
135 +
136 + opt->header = generate_header(opt, sha1, sha1, commit);
137 + opt->header = diff_tree_combined_merge(sha1, opt->header,
138 + opt->dense_combined_merges,
139 + &opt->diffopt);
140 + if (!opt->header && opt->verbose_header)
141 + opt->header_prefix = "\ndiff-tree ";
142 + return 0;
143 +}
144 +
145 +int log_tree_commit(struct log_tree_opt *opt, struct commit *commit)
146 +{
147 + struct commit_list *parents;
148 + unsigned const char *sha1 = commit->object.sha1;
149 +
150 + /* Root commit? */
151 + if (opt->show_root_diff && !commit->parents) {
152 + opt->header = generate_header(opt, sha1, NULL, commit);
153 + diff_root_tree(opt, sha1, "");
154 + }
155 +
156 + /* More than one parent? */
157 + if (commit->parents && commit->parents->next) {
158 + if (opt->ignore_merges)
159 + return 0;
160 + else if (opt->combine_merges)
161 + return do_diff_combined(opt, commit);
162 + }
163 +
164 + for (parents = commit->parents; parents; parents = parents->next) {
165 + struct commit *parent = parents->item;
166 + unsigned const char *psha1 = parent->object.sha1;
167 + opt->header = generate_header(opt, sha1, psha1, commit);
168 + diff_tree_sha1(psha1, sha1, "", &opt->diffopt);
169 + log_tree_diff_flush(opt);
170 +
171 + if (!opt->header && opt->verbose_header)
172 + opt->header_prefix = "\ndiff-tree ";
173 + }
174 + return 0;
175 +}
log-tree.h new
+23
@@ -0,0 +1,23 @@
1 +#ifndef LOG_TREE_H
2 +#define LOG_TREE_H
3 +
4 +struct log_tree_opt {
5 + struct diff_options diffopt;
6 + int show_root_diff;
7 + int no_commit_id;
8 + int verbose_header;
9 + int ignore_merges;
10 + int combine_merges;
11 + int dense_combined_merges;
12 + int always_show_header;
13 + const char *header_prefix;
14 + const char *header;
15 + enum cmit_fmt commit_format;
16 +};
17 +
18 +void init_log_tree_opt(struct log_tree_opt *);
19 +int log_tree_diff_flush(struct log_tree_opt *);
20 +int log_tree_commit(struct log_tree_opt *, struct commit *);
21 +int log_tree_opt_parse(struct log_tree_opt *, const char **, int);
22 +
23 +#endif