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