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