]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
builtin-reflog.c: don't install new reflog on write failure
[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
39bc9a6c 140void show_log(struct rev_info *opt, const char *sep)
91539833 141{
674d1727 142 struct strbuf msgbuf;
39bc9a6c 143 struct log_info *log = opt->loginfo;
91539833
LT
144 struct commit *commit = log->commit, *parent = log->parent;
145 int abbrev = opt->diffopt.abbrev;
146 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
a4d34e2d 147 const char *extra;
20ff0680 148 const char *subject = NULL, *extra_headers = opt->extra_headers;
91539833
LT
149
150 opt->loginfo = NULL;
151 if (!opt->verbose_header) {
3131b713
LT
152 if (commit->object.flags & BOUNDARY)
153 putchar('-');
154 else if (commit->object.flags & UNINTERESTING)
155 putchar('^');
156 else if (opt->left_right) {
157 if (commit->object.flags & SYMMETRIC_LEFT)
74bd9029
JH
158 putchar('<');
159 else
160 putchar('>');
161 }
c8c893c6
LT
162 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
163 if (opt->parents)
164 show_parents(commit, abbrev_commit);
ca135e7a 165 show_decorations(commit);
1dcb6922 166 putchar(opt->diffopt.line_termination);
91539833
LT
167 return;
168 }
169
170 /*
a4d34e2d
LT
171 * The "oneline" format has several special cases:
172 * - The pretty-printed commit lacks a newline at the end
173 * of the buffer, but we do want to make sure that we
174 * have a newline there. If the separator isn't already
175 * a newline, add an extra one.
176 * - unlike other log messages, the one-line format does
177 * not have an empty line between entries.
91539833 178 */
a4d34e2d
LT
179 extra = "";
180 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
181 extra = "\n";
91539833 182 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
abd4e222 183 putchar(opt->diffopt.line_termination);
91539833
LT
184 opt->shown_one = 1;
185
186 /*
187 * Print header line of header..
188 */
3eefc189 189
596524b3 190 if (opt->commit_format == CMIT_FMT_EMAIL) {
698ce6f8 191 char *sha1 = sha1_to_hex(commit->object.sha1);
596524b3
JS
192 if (opt->total > 0) {
193 static char buffer[64];
194 snprintf(buffer, sizeof(buffer),
2d9e4a47
RJ
195 "Subject: [%s %0*d/%d] ",
196 opt->subject_prefix,
e00de24b 197 digits_in_number(opt->total),
596524b3
JS
198 opt->nr, opt->total);
199 subject = buffer;
88c447e8 200 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
2d9e4a47
RJ
201 static char buffer[256];
202 snprintf(buffer, sizeof(buffer),
203 "Subject: [%s] ",
204 opt->subject_prefix);
205 subject = buffer;
206 } else {
8ac80a57 207 subject = "Subject: ";
2d9e4a47 208 }
8ac80a57 209
698ce6f8 210 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
d1566f78
JT
211 if (opt->message_id)
212 printf("Message-Id: <%s>\n", opt->message_id);
213 if (opt->ref_message_id)
214 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
215 opt->ref_message_id, opt->ref_message_id);
698ce6f8
JS
216 if (opt->mime_boundary) {
217 static char subject_buffer[1024];
218 static char buffer[1024];
219 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
20ff0680 220 "%s"
698ce6f8 221 "MIME-Version: 1.0\n"
33ee4cfb 222 "Content-Type: multipart/mixed;"
698ce6f8
JS
223 " boundary=\"%s%s\"\n"
224 "\n"
225 "This is a multi-part message in MIME "
226 "format.\n"
227 "--%s%s\n"
228 "Content-Type: text/plain; "
229 "charset=UTF-8; format=fixed\n"
230 "Content-Transfer-Encoding: 8bit\n\n",
20ff0680 231 extra_headers ? extra_headers : "",
698ce6f8
JS
232 mime_boundary_leader, opt->mime_boundary,
233 mime_boundary_leader, opt->mime_boundary);
20ff0680 234 extra_headers = subject_buffer;
698ce6f8
JS
235
236 snprintf(buffer, sizeof(buffer) - 1,
237 "--%s%s\n"
33ee4cfb 238 "Content-Type: text/x-patch;"
698ce6f8
JS
239 " name=\"%s.diff\"\n"
240 "Content-Transfer-Encoding: 8bit\n"
33ee4cfb 241 "Content-Disposition: %s;"
698ce6f8
JS
242 " filename=\"%s.diff\"\n\n",
243 mime_boundary_leader, opt->mime_boundary,
c112f689
JS
244 sha1,
245 opt->no_inline ? "attachment" : "inline",
246 sha1);
698ce6f8
JS
247 opt->diffopt.stat_sep = buffer;
248 }
e52a5de4 249 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 250 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
251 if (opt->commit_format != CMIT_FMT_ONELINE)
252 fputs("commit ", stdout);
e330a406
LT
253 if (commit->object.flags & BOUNDARY)
254 putchar('-');
3131b713
LT
255 else if (commit->object.flags & UNINTERESTING)
256 putchar('^');
e330a406
LT
257 else if (opt->left_right) {
258 if (commit->object.flags & SYMMETRIC_LEFT)
74bd9029
JH
259 putchar('<');
260 else
261 putchar('>');
262 }
263 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
264 stdout);
c66b6c06
JH
265 if (opt->parents)
266 show_parents(commit, abbrev_commit);
73f0a157 267 if (parent)
3eefc189
JH
268 printf(" (from %s)",
269 diff_unique_abbrev(parent->object.sha1,
270 abbrev_commit));
ca135e7a 271 show_decorations(commit);
8f67f8ae 272 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
e557667e 273 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
903b45fe 274 if (opt->reflog_info) {
4d12a471 275 show_reflog_message(opt->reflog_info,
4e244cbc 276 opt->commit_format == CMIT_FMT_ONELINE,
a7b02ccf 277 opt->date_mode);
903b45fe
NP
278 if (opt->commit_format == CMIT_FMT_ONELINE) {
279 printf("%s", sep);
280 return;
281 }
282 }
3eefc189 283 }
91539833 284
3131b713
LT
285 if (!commit->buffer)
286 return;
287
91539833
LT
288 /*
289 * And then the pretty-printed message itself
290 */
674d1727
PH
291 strbuf_init(&msgbuf, 0);
292 pretty_print_commit(opt->commit_format, commit, &msgbuf,
4593fb84
JH
293 abbrev, subject, extra_headers, opt->date_mode,
294 has_non_ascii(opt->add_signoff));
cf2251b6
JH
295
296 if (opt->add_signoff)
674d1727 297 append_signoff(&msgbuf, opt->add_signoff);
9fa3465d 298 if (opt->show_log_size)
674d1727 299 printf("log size %i\n", (int)msgbuf.len);
9fa3465d 300
cc61ae82
JH
301 if (msgbuf.len)
302 printf("%s%s%s", msgbuf.buf, extra, sep);
674d1727 303 strbuf_release(&msgbuf);
91539833
LT
304}
305
cd2bdc53 306int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
307{
308 diffcore_std(&opt->diffopt);
91539833 309
5f1c3f07
JH
310 if (diff_queue_is_empty()) {
311 int saved_fmt = opt->diffopt.output_format;
312 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
313 diff_flush(&opt->diffopt);
314 opt->diffopt.output_format = saved_fmt;
315 return 0;
316 }
91539833 317
3969cf7d
JH
318 if (opt->loginfo && !opt->no_commit_id) {
319 /* When showing a verbose header (i.e. log message),
320 * and not in --pretty=oneline format, we would want
321 * an extra newline between the end of log and the
322 * output for readability.
323 */
39bc9a6c 324 show_log(opt, opt->diffopt.msg_sep);
304b5af6
LT
325 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
326 opt->verbose_header &&
3969cf7d
JH
327 opt->commit_format != CMIT_FMT_ONELINE) {
328 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
329 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
330 printf("---");
331 putchar('\n');
3969cf7d
JH
332 }
333 }
5f1c3f07
JH
334 diff_flush(&opt->diffopt);
335 return 1;
336}
337
cd2bdc53 338static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
339{
340 unsigned const char *sha1 = commit->object.sha1;
341
91539833
LT
342 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
343 return !opt->loginfo;
5f1c3f07
JH
344}
345
91539833
LT
346/*
347 * Show the diff of a commit.
348 *
349 * Return true if we printed any log info messages
350 */
351static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 352{
91539833 353 int showed_log;
5f1c3f07
JH
354 struct commit_list *parents;
355 unsigned const char *sha1 = commit->object.sha1;
356
91539833
LT
357 if (!opt->diff)
358 return 0;
359
5f1c3f07 360 /* Root commit? */
91539833
LT
361 parents = commit->parents;
362 if (!parents) {
2b60356d
RS
363 if (opt->show_root_diff) {
364 diff_root_tree_sha1(sha1, "", &opt->diffopt);
365 log_tree_diff_flush(opt);
366 }
91539833 367 return !opt->loginfo;
5f1c3f07
JH
368 }
369
370 /* More than one parent? */
91539833 371 if (parents && parents->next) {
5f1c3f07
JH
372 if (opt->ignore_merges)
373 return 0;
374 else if (opt->combine_merges)
375 return do_diff_combined(opt, commit);
91539833
LT
376
377 /* If we show individual diffs, show the parent info */
378 log->parent = parents->item;
5f1c3f07
JH
379 }
380
91539833
LT
381 showed_log = 0;
382 for (;;) {
5f1c3f07 383 struct commit *parent = parents->item;
5f1c3f07 384
91539833
LT
385 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
386 log_tree_diff_flush(opt);
387
388 showed_log |= !opt->loginfo;
389
390 /* Set up the log info for the next parent, if any.. */
391 parents = parents->next;
392 if (!parents)
393 break;
394 log->parent = parents->item;
395 opt->loginfo = log;
396 }
397 return showed_log;
398}
399
400int log_tree_commit(struct rev_info *opt, struct commit *commit)
401{
402 struct log_info log;
3eefc189 403 int shown;
91539833
LT
404
405 log.commit = commit;
406 log.parent = NULL;
407 opt->loginfo = &log;
408
3eefc189
JH
409 shown = log_tree_diff(opt, commit, &log);
410 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 411 log.parent = NULL;
39bc9a6c 412 show_log(opt, "");
3eefc189 413 shown = 1;
5f1c3f07 414 }
91539833 415 opt->loginfo = NULL;
06f59e9f 416 maybe_flush_or_die(stdout, "stdout");
3eefc189 417 return shown;
5f1c3f07 418}