]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
git-svn: Same default as cvsimport when using --use-log-author
[thirdparty/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"
8860fd42 5#include "reflog-walk.h"
5f1c3f07 6
ca135e7a
LT
7struct decoration name_decoration = { "object names" };
8
c8c893c6
LT
9static void show_parents(struct commit *commit, int abbrev)
10{
11 struct commit_list *p;
12 for (p = commit->parents; p ; p = p->next) {
13 struct commit *parent = p->item;
14 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
15 }
16}
17
50e62a8e 18void show_decorations(struct commit *commit)
ca135e7a
LT
19{
20 const char *prefix;
21 struct name_decoration *decoration;
22
23 decoration = lookup_decoration(&name_decoration, &commit->object);
24 if (!decoration)
25 return;
26 prefix = " (";
27 while (decoration) {
28 printf("%s%s", prefix, decoration->name);
29 prefix = ", ";
30 decoration = decoration->next;
31 }
32 putchar(')');
33}
34
fc1c75ec
FBH
35/*
36 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37 * Signed-off-by: and Acked-by: lines.
38 */
39static int detect_any_signoff(char *letter, int size)
40{
41 char ch, *cp;
42 int seen_colon = 0;
43 int seen_at = 0;
44 int seen_name = 0;
45 int seen_head = 0;
46
47 cp = letter + size;
48 while (letter <= --cp && (ch = *cp) == '\n')
49 continue;
50
51 while (letter <= cp) {
52 ch = *cp--;
53 if (ch == '\n')
54 break;
55
56 if (!seen_at) {
57 if (ch == '@')
58 seen_at = 1;
59 continue;
60 }
61 if (!seen_colon) {
62 if (ch == '@')
63 return 0;
64 else if (ch == ':')
65 seen_colon = 1;
66 else
67 seen_name = 1;
68 continue;
69 }
70 if (('A' <= ch && ch <= 'Z') ||
71 ('a' <= ch && ch <= 'z') ||
72 ch == '-') {
73 seen_head = 1;
74 continue;
75 }
76 /* no empty last line doesn't match */
77 return 0;
78 }
79 return seen_head && seen_name;
80}
81
674d1727 82static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 83{
cf2251b6 84 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 85 size_t signoff_len = strlen(signoff);
fc1c75ec 86 int has_signoff = 0;
80583c0e 87 char *cp;
cf2251b6 88
674d1727 89 cp = sb->buf;
cf2251b6
JH
90
91 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
92 while ((cp = strstr(cp, signed_off_by))) {
93
94 has_signoff = 1;
95
cf2251b6 96 cp += strlen(signed_off_by);
674d1727 97 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
98 break;
99 if (strncmp(cp, signoff, signoff_len))
100 continue;
101 if (!isspace(cp[signoff_len]))
102 continue;
103 /* we already have him */
674d1727 104 return;
cf2251b6
JH
105 }
106
fc1c75ec 107 if (!has_signoff)
674d1727 108 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
109
110 if (!has_signoff)
674d1727 111 strbuf_addch(sb, '\n');
c35f4c37 112
674d1727
PH
113 strbuf_addstr(sb, signed_off_by);
114 strbuf_add(sb, signoff, signoff_len);
115 strbuf_addch(sb, '\n');
cf2251b6
JH
116}
117
e00de24b
JS
118static unsigned int digits_in_number(unsigned int number)
119{
120 unsigned int i = 10, result = 1;
121 while (i <= number) {
122 i *= 10;
123 result++;
124 }
125 return result;
126}
127
4593fb84
JH
128static int has_non_ascii(const char *s)
129{
130 int ch;
131 if (!s)
132 return 0;
133 while ((ch = *s++) != '\0') {
134 if (non_ascii(ch))
135 return 1;
136 }
137 return 0;
138}
139
b02bd65f 140void log_write_email_headers(struct rev_info *opt, const char *name,
267123b4
JH
141 const char **subject_p,
142 const char **extra_headers_p,
143 int *need_8bit_cte_p)
b02bd65f
DB
144{
145 const char *subject = NULL;
146 const char *extra_headers = opt->extra_headers;
267123b4
JH
147
148 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
149 if (opt->total > 0) {
150 static char buffer[64];
151 snprintf(buffer, sizeof(buffer),
152 "Subject: [%s %0*d/%d] ",
153 opt->subject_prefix,
154 digits_in_number(opt->total),
155 opt->nr, opt->total);
156 subject = buffer;
157 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
158 static char buffer[256];
159 snprintf(buffer, sizeof(buffer),
160 "Subject: [%s] ",
161 opt->subject_prefix);
162 subject = buffer;
163 } else {
164 subject = "Subject: ";
165 }
166
167 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
168 if (opt->message_id)
169 printf("Message-Id: <%s>\n", opt->message_id);
170 if (opt->ref_message_id)
171 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
172 opt->ref_message_id, opt->ref_message_id);
173 if (opt->mime_boundary) {
174 static char subject_buffer[1024];
175 static char buffer[1024];
267123b4 176 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
177 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
178 "%s"
179 "MIME-Version: 1.0\n"
180 "Content-Type: multipart/mixed;"
181 " boundary=\"%s%s\"\n"
182 "\n"
183 "This is a multi-part message in MIME "
184 "format.\n"
185 "--%s%s\n"
186 "Content-Type: text/plain; "
187 "charset=UTF-8; format=fixed\n"
188 "Content-Transfer-Encoding: 8bit\n\n",
189 extra_headers ? extra_headers : "",
190 mime_boundary_leader, opt->mime_boundary,
191 mime_boundary_leader, opt->mime_boundary);
192 extra_headers = subject_buffer;
193
194 snprintf(buffer, sizeof(buffer) - 1,
195 "--%s%s\n"
196 "Content-Type: text/x-patch;"
197 " name=\"%s.diff\"\n"
198 "Content-Transfer-Encoding: 8bit\n"
199 "Content-Disposition: %s;"
200 " filename=\"%s.diff\"\n\n",
201 mime_boundary_leader, opt->mime_boundary,
202 name,
203 opt->no_inline ? "attachment" : "inline",
204 name);
205 opt->diffopt.stat_sep = buffer;
206 }
207 *subject_p = subject;
208 *extra_headers_p = extra_headers;
209}
210
39bc9a6c 211void show_log(struct rev_info *opt, const char *sep)
91539833 212{
674d1727 213 struct strbuf msgbuf;
39bc9a6c 214 struct log_info *log = opt->loginfo;
91539833
LT
215 struct commit *commit = log->commit, *parent = log->parent;
216 int abbrev = opt->diffopt.abbrev;
217 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
a4d34e2d 218 const char *extra;
20ff0680 219 const char *subject = NULL, *extra_headers = opt->extra_headers;
6bf4f1b4 220 int need_8bit_cte = 0;
91539833
LT
221
222 opt->loginfo = NULL;
223 if (!opt->verbose_header) {
3131b713
LT
224 if (commit->object.flags & BOUNDARY)
225 putchar('-');
226 else if (commit->object.flags & UNINTERESTING)
227 putchar('^');
228 else if (opt->left_right) {
229 if (commit->object.flags & SYMMETRIC_LEFT)
74bd9029
JH
230 putchar('<');
231 else
232 putchar('>');
233 }
c8c893c6
LT
234 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
235 if (opt->parents)
236 show_parents(commit, abbrev_commit);
ca135e7a 237 show_decorations(commit);
1dcb6922 238 putchar(opt->diffopt.line_termination);
91539833
LT
239 return;
240 }
241
242 /*
a4d34e2d
LT
243 * The "oneline" format has several special cases:
244 * - The pretty-printed commit lacks a newline at the end
245 * of the buffer, but we do want to make sure that we
246 * have a newline there. If the separator isn't already
247 * a newline, add an extra one.
248 * - unlike other log messages, the one-line format does
249 * not have an empty line between entries.
91539833 250 */
a4d34e2d 251 extra = "";
4da45bef 252 if (*sep != '\n' && opt->use_terminator)
a4d34e2d 253 extra = "\n";
4da45bef 254 if (opt->shown_one && !opt->use_terminator)
abd4e222 255 putchar(opt->diffopt.line_termination);
91539833
LT
256 opt->shown_one = 1;
257
258 /*
259 * Print header line of header..
260 */
3eefc189 261
596524b3 262 if (opt->commit_format == CMIT_FMT_EMAIL) {
b02bd65f 263 log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
267123b4
JH
264 &subject, &extra_headers,
265 &need_8bit_cte);
e52a5de4 266 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 267 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
268 if (opt->commit_format != CMIT_FMT_ONELINE)
269 fputs("commit ", stdout);
e330a406
LT
270 if (commit->object.flags & BOUNDARY)
271 putchar('-');
3131b713
LT
272 else if (commit->object.flags & UNINTERESTING)
273 putchar('^');
e330a406
LT
274 else if (opt->left_right) {
275 if (commit->object.flags & SYMMETRIC_LEFT)
74bd9029
JH
276 putchar('<');
277 else
278 putchar('>');
279 }
280 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
281 stdout);
c66b6c06
JH
282 if (opt->parents)
283 show_parents(commit, abbrev_commit);
73f0a157 284 if (parent)
3eefc189
JH
285 printf(" (from %s)",
286 diff_unique_abbrev(parent->object.sha1,
287 abbrev_commit));
ca135e7a 288 show_decorations(commit);
8f67f8ae 289 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
e557667e 290 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
903b45fe 291 if (opt->reflog_info) {
4d12a471 292 show_reflog_message(opt->reflog_info,
4e244cbc 293 opt->commit_format == CMIT_FMT_ONELINE,
a7b02ccf 294 opt->date_mode);
903b45fe
NP
295 if (opt->commit_format == CMIT_FMT_ONELINE) {
296 printf("%s", sep);
297 return;
298 }
299 }
3eefc189 300 }
91539833 301
3131b713
LT
302 if (!commit->buffer)
303 return;
304
91539833
LT
305 /*
306 * And then the pretty-printed message itself
307 */
674d1727 308 strbuf_init(&msgbuf, 0);
6bf4f1b4
JH
309 if (need_8bit_cte >= 0)
310 need_8bit_cte = has_non_ascii(opt->add_signoff);
674d1727 311 pretty_print_commit(opt->commit_format, commit, &msgbuf,
4593fb84 312 abbrev, subject, extra_headers, opt->date_mode,
6bf4f1b4 313 need_8bit_cte);
cf2251b6
JH
314
315 if (opt->add_signoff)
674d1727 316 append_signoff(&msgbuf, opt->add_signoff);
9fa3465d 317 if (opt->show_log_size)
674d1727 318 printf("log size %i\n", (int)msgbuf.len);
9fa3465d 319
42c8c74c
GS
320 if (msgbuf.len) {
321 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
322 printf("%s%s", extra, sep);
323 }
674d1727 324 strbuf_release(&msgbuf);
91539833
LT
325}
326
cd2bdc53 327int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
328{
329 diffcore_std(&opt->diffopt);
91539833 330
5f1c3f07
JH
331 if (diff_queue_is_empty()) {
332 int saved_fmt = opt->diffopt.output_format;
333 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
334 diff_flush(&opt->diffopt);
335 opt->diffopt.output_format = saved_fmt;
336 return 0;
337 }
91539833 338
3969cf7d
JH
339 if (opt->loginfo && !opt->no_commit_id) {
340 /* When showing a verbose header (i.e. log message),
341 * and not in --pretty=oneline format, we would want
342 * an extra newline between the end of log and the
343 * output for readability.
344 */
39bc9a6c 345 show_log(opt, opt->diffopt.msg_sep);
304b5af6
LT
346 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
347 opt->verbose_header &&
3969cf7d
JH
348 opt->commit_format != CMIT_FMT_ONELINE) {
349 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
350 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
351 printf("---");
352 putchar('\n');
3969cf7d
JH
353 }
354 }
5f1c3f07
JH
355 diff_flush(&opt->diffopt);
356 return 1;
357}
358
cd2bdc53 359static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
360{
361 unsigned const char *sha1 = commit->object.sha1;
362
91539833
LT
363 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
364 return !opt->loginfo;
5f1c3f07
JH
365}
366
91539833
LT
367/*
368 * Show the diff of a commit.
369 *
370 * Return true if we printed any log info messages
371 */
372static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 373{
91539833 374 int showed_log;
5f1c3f07
JH
375 struct commit_list *parents;
376 unsigned const char *sha1 = commit->object.sha1;
377
91539833
LT
378 if (!opt->diff)
379 return 0;
380
5f1c3f07 381 /* Root commit? */
91539833
LT
382 parents = commit->parents;
383 if (!parents) {
2b60356d
RS
384 if (opt->show_root_diff) {
385 diff_root_tree_sha1(sha1, "", &opt->diffopt);
386 log_tree_diff_flush(opt);
387 }
91539833 388 return !opt->loginfo;
5f1c3f07
JH
389 }
390
391 /* More than one parent? */
91539833 392 if (parents && parents->next) {
5f1c3f07
JH
393 if (opt->ignore_merges)
394 return 0;
395 else if (opt->combine_merges)
396 return do_diff_combined(opt, commit);
91539833
LT
397
398 /* If we show individual diffs, show the parent info */
399 log->parent = parents->item;
5f1c3f07
JH
400 }
401
91539833
LT
402 showed_log = 0;
403 for (;;) {
5f1c3f07 404 struct commit *parent = parents->item;
5f1c3f07 405
91539833
LT
406 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
407 log_tree_diff_flush(opt);
408
409 showed_log |= !opt->loginfo;
410
411 /* Set up the log info for the next parent, if any.. */
412 parents = parents->next;
413 if (!parents)
414 break;
415 log->parent = parents->item;
416 opt->loginfo = log;
417 }
418 return showed_log;
419}
420
421int log_tree_commit(struct rev_info *opt, struct commit *commit)
422{
423 struct log_info log;
3eefc189 424 int shown;
91539833
LT
425
426 log.commit = commit;
427 log.parent = NULL;
428 opt->loginfo = &log;
429
3eefc189
JH
430 shown = log_tree_diff(opt, commit, &log);
431 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 432 log.parent = NULL;
39bc9a6c 433 show_log(opt, "");
3eefc189 434 shown = 1;
5f1c3f07 435 }
91539833 436 opt->loginfo = NULL;
06f59e9f 437 maybe_flush_or_die(stdout, "stdout");
3eefc189 438 return shown;
5f1c3f07 439}