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