]>
Commit | Line | Data |
---|---|---|
5f1c3f07 JH |
1 | #include "cache.h" |
2 | #include "diff.h" | |
3 | #include "commit.h" | |
4 | #include "log-tree.h" | |
5 | ||
91539833 LT |
6 | void show_log(struct rev_info *opt, struct log_info *log, const char *sep) |
7 | { | |
8 | static char this_header[16384]; | |
9 | struct commit *commit = log->commit, *parent = log->parent; | |
10 | int abbrev = opt->diffopt.abbrev; | |
11 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40; | |
a4d34e2d | 12 | const char *extra; |
91539833 LT |
13 | int len; |
14 | ||
15 | opt->loginfo = NULL; | |
16 | if (!opt->verbose_header) { | |
17 | puts(sha1_to_hex(commit->object.sha1)); | |
18 | return; | |
19 | } | |
20 | ||
21 | /* | |
a4d34e2d LT |
22 | * The "oneline" format has several special cases: |
23 | * - The pretty-printed commit lacks a newline at the end | |
24 | * of the buffer, but we do want to make sure that we | |
25 | * have a newline there. If the separator isn't already | |
26 | * a newline, add an extra one. | |
27 | * - unlike other log messages, the one-line format does | |
28 | * not have an empty line between entries. | |
91539833 | 29 | */ |
a4d34e2d LT |
30 | extra = ""; |
31 | if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE) | |
32 | extra = "\n"; | |
91539833 LT |
33 | if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE) |
34 | putchar('\n'); | |
35 | opt->shown_one = 1; | |
36 | ||
37 | /* | |
38 | * Print header line of header.. | |
39 | */ | |
3eefc189 JH |
40 | |
41 | if (opt->commit_format == CMIT_FMT_EMAIL) | |
42 | printf("From %s Thu Apr 7 15:13:13 2005\n", | |
43 | sha1_to_hex(commit->object.sha1)); | |
44 | else { | |
45 | printf("%s%s", | |
46 | opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ", | |
47 | diff_unique_abbrev(commit->object.sha1, abbrev_commit)); | |
48 | if (parent) | |
49 | printf(" (from %s)", | |
50 | diff_unique_abbrev(parent->object.sha1, | |
51 | abbrev_commit)); | |
52 | putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n'); | |
53 | } | |
91539833 LT |
54 | |
55 | /* | |
56 | * And then the pretty-printed message itself | |
57 | */ | |
58 | len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev); | |
a4d34e2d | 59 | printf("%s%s%s", this_header, extra, sep); |
91539833 LT |
60 | } |
61 | ||
cd2bdc53 | 62 | int log_tree_diff_flush(struct rev_info *opt) |
5f1c3f07 JH |
63 | { |
64 | diffcore_std(&opt->diffopt); | |
91539833 | 65 | |
5f1c3f07 JH |
66 | if (diff_queue_is_empty()) { |
67 | int saved_fmt = opt->diffopt.output_format; | |
68 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; | |
69 | diff_flush(&opt->diffopt); | |
70 | opt->diffopt.output_format = saved_fmt; | |
71 | return 0; | |
72 | } | |
91539833 LT |
73 | |
74 | if (opt->loginfo && !opt->no_commit_id) | |
eab144ac | 75 | show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n"); |
5f1c3f07 JH |
76 | diff_flush(&opt->diffopt); |
77 | return 1; | |
78 | } | |
79 | ||
cd2bdc53 | 80 | static int diff_root_tree(struct rev_info *opt, |
5f1c3f07 JH |
81 | const unsigned char *new, const char *base) |
82 | { | |
83 | int retval; | |
84 | void *tree; | |
85 | struct tree_desc empty, real; | |
86 | ||
87 | tree = read_object_with_reference(new, tree_type, &real.size, NULL); | |
88 | if (!tree) | |
89 | die("unable to read root tree (%s)", sha1_to_hex(new)); | |
90 | real.buf = tree; | |
91 | ||
92 | empty.buf = ""; | |
93 | empty.size = 0; | |
94 | retval = diff_tree(&empty, &real, base, &opt->diffopt); | |
95 | free(tree); | |
96 | log_tree_diff_flush(opt); | |
97 | return retval; | |
98 | } | |
99 | ||
cd2bdc53 | 100 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
5f1c3f07 JH |
101 | { |
102 | unsigned const char *sha1 = commit->object.sha1; | |
103 | ||
91539833 LT |
104 | diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt); |
105 | return !opt->loginfo; | |
5f1c3f07 JH |
106 | } |
107 | ||
91539833 LT |
108 | /* |
109 | * Show the diff of a commit. | |
110 | * | |
111 | * Return true if we printed any log info messages | |
112 | */ | |
113 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) | |
5f1c3f07 | 114 | { |
91539833 | 115 | int showed_log; |
5f1c3f07 JH |
116 | struct commit_list *parents; |
117 | unsigned const char *sha1 = commit->object.sha1; | |
118 | ||
91539833 LT |
119 | if (!opt->diff) |
120 | return 0; | |
121 | ||
5f1c3f07 | 122 | /* Root commit? */ |
91539833 LT |
123 | parents = commit->parents; |
124 | if (!parents) { | |
125 | if (opt->show_root_diff) | |
126 | diff_root_tree(opt, sha1, ""); | |
127 | return !opt->loginfo; | |
5f1c3f07 JH |
128 | } |
129 | ||
130 | /* More than one parent? */ | |
91539833 | 131 | if (parents && parents->next) { |
5f1c3f07 JH |
132 | if (opt->ignore_merges) |
133 | return 0; | |
134 | else if (opt->combine_merges) | |
135 | return do_diff_combined(opt, commit); | |
91539833 LT |
136 | |
137 | /* If we show individual diffs, show the parent info */ | |
138 | log->parent = parents->item; | |
5f1c3f07 JH |
139 | } |
140 | ||
91539833 LT |
141 | showed_log = 0; |
142 | for (;;) { | |
5f1c3f07 | 143 | struct commit *parent = parents->item; |
5f1c3f07 | 144 | |
91539833 LT |
145 | diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt); |
146 | log_tree_diff_flush(opt); | |
147 | ||
148 | showed_log |= !opt->loginfo; | |
149 | ||
150 | /* Set up the log info for the next parent, if any.. */ | |
151 | parents = parents->next; | |
152 | if (!parents) | |
153 | break; | |
154 | log->parent = parents->item; | |
155 | opt->loginfo = log; | |
156 | } | |
157 | return showed_log; | |
158 | } | |
159 | ||
160 | int log_tree_commit(struct rev_info *opt, struct commit *commit) | |
161 | { | |
162 | struct log_info log; | |
3eefc189 | 163 | int shown; |
91539833 LT |
164 | |
165 | log.commit = commit; | |
166 | log.parent = NULL; | |
167 | opt->loginfo = &log; | |
168 | ||
3eefc189 JH |
169 | shown = log_tree_diff(opt, commit, &log); |
170 | if (!shown && opt->loginfo && opt->always_show_header) { | |
91539833 LT |
171 | log.parent = NULL; |
172 | show_log(opt, opt->loginfo, ""); | |
3eefc189 | 173 | shown = 1; |
5f1c3f07 | 174 | } |
91539833 | 175 | opt->loginfo = NULL; |
3eefc189 | 176 | return shown; |
5f1c3f07 | 177 | } |