Remove uneeded #include
[git/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
5
c8c893c6
LT
6static void show_parents(struct commit *commit, int abbrev)
7{
8 struct commit_list *p;
9 for (p = commit->parents; p ; p = p->next) {
10 struct commit *parent = p->item;
11 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
12 }
13}
14
cf2251b6
JH
15static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
16{
17 int signoff_len = strlen(signoff);
18 static const char signed_off_by[] = "Signed-off-by: ";
19 char *cp = buf;
20
21 /* Do we have enough space to add it? */
c35f4c37 22 if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 3)
cf2251b6
JH
23 return at;
24
25 /* First see if we already have the sign-off by the signer */
26 while (1) {
27 cp = strstr(cp, signed_off_by);
28 if (!cp)
29 break;
30 cp += strlen(signed_off_by);
31 if ((cp + signoff_len < buf + at) &&
32 !strncmp(cp, signoff, signoff_len) &&
33 isspace(cp[signoff_len]))
34 return at; /* we already have him */
35 }
36
c35f4c37
FBH
37 /* Does the last line already end with "^[-A-Za-z]+: [^@]+@"?
38 * If not, add a blank line to separate the message from
39 * the run of Signed-off-by: and Acked-by: lines.
40 */
41 {
42 char ch;
43 int seen_colon, seen_at, seen_name, seen_head, not_signoff;
44 seen_colon = 0;
45 seen_at = 0;
46 seen_name = 0;
47 seen_head = 0;
48 not_signoff = 0;
49 cp = buf + at;
50 while (buf <= --cp && (ch = *cp) == '\n')
51 ;
52 while (!not_signoff && buf <= cp && (ch = *cp--) != '\n') {
53 if (!seen_at) {
54 if (ch == '@')
55 seen_at = 1;
56 continue;
57 }
58 if (!seen_colon) {
59 if (ch == '@')
60 not_signoff = 1;
61 else if (ch == ':')
62 seen_colon = 1;
63 else
64 seen_name = 1;
65 continue;
66 }
67 if (('A' <= ch && ch <= 'Z') ||
68 ('a' <= ch && ch <= 'z') ||
69 ch == '-') {
70 seen_head = 1;
71 continue;
72 }
73 not_signoff = 1;
74 }
75 if (not_signoff || !seen_head || !seen_name)
76 buf[at++] = '\n';
77 }
78
cf2251b6
JH
79 strcpy(buf + at, signed_off_by);
80 at += strlen(signed_off_by);
81 strcpy(buf + at, signoff);
82 at += signoff_len;
83 buf[at++] = '\n';
84 buf[at] = 0;
85 return at;
86}
87
39bc9a6c 88void show_log(struct rev_info *opt, const char *sep)
91539833
LT
89{
90 static char this_header[16384];
39bc9a6c 91 struct log_info *log = opt->loginfo;
91539833
LT
92 struct commit *commit = log->commit, *parent = log->parent;
93 int abbrev = opt->diffopt.abbrev;
94 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
a4d34e2d 95 const char *extra;
91539833 96 int len;
20ff0680 97 const char *subject = NULL, *extra_headers = opt->extra_headers;
91539833
LT
98
99 opt->loginfo = NULL;
100 if (!opt->verbose_header) {
c8c893c6
LT
101 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
102 if (opt->parents)
103 show_parents(commit, abbrev_commit);
1dcb6922 104 putchar(opt->diffopt.line_termination);
91539833
LT
105 return;
106 }
107
108 /*
a4d34e2d
LT
109 * The "oneline" format has several special cases:
110 * - The pretty-printed commit lacks a newline at the end
111 * of the buffer, but we do want to make sure that we
112 * have a newline there. If the separator isn't already
113 * a newline, add an extra one.
114 * - unlike other log messages, the one-line format does
115 * not have an empty line between entries.
91539833 116 */
a4d34e2d
LT
117 extra = "";
118 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
119 extra = "\n";
91539833
LT
120 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
121 putchar('\n');
122 opt->shown_one = 1;
123
124 /*
125 * Print header line of header..
126 */
3eefc189 127
596524b3 128 if (opt->commit_format == CMIT_FMT_EMAIL) {
698ce6f8 129 char *sha1 = sha1_to_hex(commit->object.sha1);
596524b3
JS
130 if (opt->total > 0) {
131 static char buffer[64];
132 snprintf(buffer, sizeof(buffer),
133 "Subject: [PATCH %d/%d] ",
134 opt->nr, opt->total);
135 subject = buffer;
8ac80a57 136 } else if (opt->total == 0)
596524b3 137 subject = "Subject: [PATCH] ";
8ac80a57
JS
138 else
139 subject = "Subject: ";
140
698ce6f8 141 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
d1566f78
JT
142 if (opt->message_id)
143 printf("Message-Id: <%s>\n", opt->message_id);
144 if (opt->ref_message_id)
145 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
146 opt->ref_message_id, opt->ref_message_id);
698ce6f8
JS
147 if (opt->mime_boundary) {
148 static char subject_buffer[1024];
149 static char buffer[1024];
150 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
20ff0680 151 "%s"
698ce6f8
JS
152 "MIME-Version: 1.0\n"
153 "Content-Type: multipart/mixed;\n"
154 " boundary=\"%s%s\"\n"
155 "\n"
156 "This is a multi-part message in MIME "
157 "format.\n"
158 "--%s%s\n"
159 "Content-Type: text/plain; "
160 "charset=UTF-8; format=fixed\n"
161 "Content-Transfer-Encoding: 8bit\n\n",
20ff0680 162 extra_headers ? extra_headers : "",
698ce6f8
JS
163 mime_boundary_leader, opt->mime_boundary,
164 mime_boundary_leader, opt->mime_boundary);
20ff0680 165 extra_headers = subject_buffer;
698ce6f8
JS
166
167 snprintf(buffer, sizeof(buffer) - 1,
168 "--%s%s\n"
169 "Content-Type: text/x-patch;\n"
170 " name=\"%s.diff\"\n"
171 "Content-Transfer-Encoding: 8bit\n"
172 "Content-Disposition: inline;\n"
173 " filename=\"%s.diff\"\n\n",
174 mime_boundary_leader, opt->mime_boundary,
175 sha1, sha1);
176 opt->diffopt.stat_sep = buffer;
177 }
596524b3 178 } else {
ce436973
JK
179 printf("%s%s%s",
180 diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
3eefc189
JH
181 opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
182 diff_unique_abbrev(commit->object.sha1, abbrev_commit));
c66b6c06
JH
183 if (opt->parents)
184 show_parents(commit, abbrev_commit);
73f0a157 185 if (parent)
3eefc189
JH
186 printf(" (from %s)",
187 diff_unique_abbrev(parent->object.sha1,
188 abbrev_commit));
ce436973
JK
189 printf("%s",
190 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
e557667e 191 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
3eefc189 192 }
91539833
LT
193
194 /*
195 * And then the pretty-printed message itself
196 */
3dfb9278
JF
197 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
198 sizeof(this_header), abbrev, subject,
199 extra_headers, opt->relative_date);
cf2251b6
JH
200
201 if (opt->add_signoff)
202 len = append_signoff(this_header, sizeof(this_header), len,
203 opt->add_signoff);
a4d34e2d 204 printf("%s%s%s", this_header, extra, sep);
91539833
LT
205}
206
cd2bdc53 207int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
208{
209 diffcore_std(&opt->diffopt);
91539833 210
5f1c3f07
JH
211 if (diff_queue_is_empty()) {
212 int saved_fmt = opt->diffopt.output_format;
213 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
214 diff_flush(&opt->diffopt);
215 opt->diffopt.output_format = saved_fmt;
216 return 0;
217 }
91539833 218
3969cf7d
JH
219 if (opt->loginfo && !opt->no_commit_id) {
220 /* When showing a verbose header (i.e. log message),
221 * and not in --pretty=oneline format, we would want
222 * an extra newline between the end of log and the
223 * output for readability.
224 */
39bc9a6c 225 show_log(opt, opt->diffopt.msg_sep);
3969cf7d
JH
226 if (opt->verbose_header &&
227 opt->commit_format != CMIT_FMT_ONELINE) {
228 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
229 if ((pch & opt->diffopt.output_format) == pch)
230 printf("---%c", opt->diffopt.line_termination);
231 else
232 putchar(opt->diffopt.line_termination);
233 }
234 }
5f1c3f07
JH
235 diff_flush(&opt->diffopt);
236 return 1;
237}
238
cd2bdc53 239static int diff_root_tree(struct rev_info *opt,
5f1c3f07
JH
240 const unsigned char *new, const char *base)
241{
242 int retval;
243 void *tree;
244 struct tree_desc empty, real;
245
246 tree = read_object_with_reference(new, tree_type, &real.size, NULL);
247 if (!tree)
248 die("unable to read root tree (%s)", sha1_to_hex(new));
249 real.buf = tree;
250
251 empty.buf = "";
252 empty.size = 0;
253 retval = diff_tree(&empty, &real, base, &opt->diffopt);
254 free(tree);
255 log_tree_diff_flush(opt);
256 return retval;
257}
258
cd2bdc53 259static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
260{
261 unsigned const char *sha1 = commit->object.sha1;
262
91539833
LT
263 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
264 return !opt->loginfo;
5f1c3f07
JH
265}
266
91539833
LT
267/*
268 * Show the diff of a commit.
269 *
270 * Return true if we printed any log info messages
271 */
272static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 273{
91539833 274 int showed_log;
5f1c3f07
JH
275 struct commit_list *parents;
276 unsigned const char *sha1 = commit->object.sha1;
277
91539833
LT
278 if (!opt->diff)
279 return 0;
280
5f1c3f07 281 /* Root commit? */
91539833
LT
282 parents = commit->parents;
283 if (!parents) {
284 if (opt->show_root_diff)
285 diff_root_tree(opt, sha1, "");
286 return !opt->loginfo;
5f1c3f07
JH
287 }
288
289 /* More than one parent? */
91539833 290 if (parents && parents->next) {
5f1c3f07
JH
291 if (opt->ignore_merges)
292 return 0;
293 else if (opt->combine_merges)
294 return do_diff_combined(opt, commit);
91539833
LT
295
296 /* If we show individual diffs, show the parent info */
297 log->parent = parents->item;
5f1c3f07
JH
298 }
299
91539833
LT
300 showed_log = 0;
301 for (;;) {
5f1c3f07 302 struct commit *parent = parents->item;
5f1c3f07 303
91539833
LT
304 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
305 log_tree_diff_flush(opt);
306
307 showed_log |= !opt->loginfo;
308
309 /* Set up the log info for the next parent, if any.. */
310 parents = parents->next;
311 if (!parents)
312 break;
313 log->parent = parents->item;
314 opt->loginfo = log;
315 }
316 return showed_log;
317}
318
319int log_tree_commit(struct rev_info *opt, struct commit *commit)
320{
321 struct log_info log;
3eefc189 322 int shown;
91539833
LT
323
324 log.commit = commit;
325 log.parent = NULL;
326 opt->loginfo = &log;
327
3eefc189
JH
328 shown = log_tree_diff(opt, commit, &log);
329 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 330 log.parent = NULL;
39bc9a6c 331 show_log(opt, "");
3eefc189 332 shown = 1;
5f1c3f07 333 }
91539833 334 opt->loginfo = NULL;
3eefc189 335 return shown;
5f1c3f07 336}