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