]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
git-rebase: allow rebasing a detached HEAD.
[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) {
74bd9029
JH
117 if (opt->left_right) {
118 if (commit->object.flags & BOUNDARY)
119 putchar('-');
120 else if (commit->object.flags & SYMMETRIC_LEFT)
121 putchar('<');
122 else
123 putchar('>');
124 }
c8c893c6
LT
125 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
126 if (opt->parents)
127 show_parents(commit, abbrev_commit);
1dcb6922 128 putchar(opt->diffopt.line_termination);
91539833
LT
129 return;
130 }
131
132 /*
a4d34e2d
LT
133 * The "oneline" format has several special cases:
134 * - The pretty-printed commit lacks a newline at the end
135 * of the buffer, but we do want to make sure that we
136 * have a newline there. If the separator isn't already
137 * a newline, add an extra one.
138 * - unlike other log messages, the one-line format does
139 * not have an empty line between entries.
91539833 140 */
a4d34e2d
LT
141 extra = "";
142 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
143 extra = "\n";
91539833
LT
144 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
145 putchar('\n');
146 opt->shown_one = 1;
147
148 /*
149 * Print header line of header..
150 */
3eefc189 151
596524b3 152 if (opt->commit_format == CMIT_FMT_EMAIL) {
698ce6f8 153 char *sha1 = sha1_to_hex(commit->object.sha1);
596524b3
JS
154 if (opt->total > 0) {
155 static char buffer[64];
156 snprintf(buffer, sizeof(buffer),
157 "Subject: [PATCH %d/%d] ",
158 opt->nr, opt->total);
159 subject = buffer;
8ac80a57 160 } else if (opt->total == 0)
596524b3 161 subject = "Subject: [PATCH] ";
8ac80a57
JS
162 else
163 subject = "Subject: ";
164
698ce6f8 165 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
d1566f78
JT
166 if (opt->message_id)
167 printf("Message-Id: <%s>\n", opt->message_id);
168 if (opt->ref_message_id)
169 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
170 opt->ref_message_id, opt->ref_message_id);
698ce6f8
JS
171 if (opt->mime_boundary) {
172 static char subject_buffer[1024];
173 static char buffer[1024];
174 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
20ff0680 175 "%s"
698ce6f8
JS
176 "MIME-Version: 1.0\n"
177 "Content-Type: multipart/mixed;\n"
178 " boundary=\"%s%s\"\n"
179 "\n"
180 "This is a multi-part message in MIME "
181 "format.\n"
182 "--%s%s\n"
183 "Content-Type: text/plain; "
184 "charset=UTF-8; format=fixed\n"
185 "Content-Transfer-Encoding: 8bit\n\n",
20ff0680 186 extra_headers ? extra_headers : "",
698ce6f8
JS
187 mime_boundary_leader, opt->mime_boundary,
188 mime_boundary_leader, opt->mime_boundary);
20ff0680 189 extra_headers = subject_buffer;
698ce6f8
JS
190
191 snprintf(buffer, sizeof(buffer) - 1,
192 "--%s%s\n"
193 "Content-Type: text/x-patch;\n"
194 " name=\"%s.diff\"\n"
195 "Content-Transfer-Encoding: 8bit\n"
196 "Content-Disposition: inline;\n"
197 " filename=\"%s.diff\"\n\n",
198 mime_boundary_leader, opt->mime_boundary,
199 sha1, sha1);
200 opt->diffopt.stat_sep = buffer;
201 }
596524b3 202 } else {
74bd9029
JH
203 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
204 stdout);
205 if (opt->commit_format != CMIT_FMT_ONELINE)
206 fputs("commit ", stdout);
207 if (opt->left_right) {
208 if (commit->object.flags & BOUNDARY)
209 putchar('-');
210 else if (commit->object.flags & SYMMETRIC_LEFT)
211 putchar('<');
212 else
213 putchar('>');
214 }
215 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
216 stdout);
c66b6c06
JH
217 if (opt->parents)
218 show_parents(commit, abbrev_commit);
73f0a157 219 if (parent)
3eefc189
JH
220 printf(" (from %s)",
221 diff_unique_abbrev(parent->object.sha1,
222 abbrev_commit));
ce436973
JK
223 printf("%s",
224 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
e557667e 225 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
3eefc189 226 }
91539833
LT
227
228 /*
229 * And then the pretty-printed message itself
230 */
3dfb9278
JF
231 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header,
232 sizeof(this_header), abbrev, subject,
233 extra_headers, opt->relative_date);
cf2251b6
JH
234
235 if (opt->add_signoff)
236 len = append_signoff(this_header, sizeof(this_header), len,
237 opt->add_signoff);
a4d34e2d 238 printf("%s%s%s", this_header, extra, sep);
91539833
LT
239}
240
cd2bdc53 241int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
242{
243 diffcore_std(&opt->diffopt);
91539833 244
5f1c3f07
JH
245 if (diff_queue_is_empty()) {
246 int saved_fmt = opt->diffopt.output_format;
247 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
248 diff_flush(&opt->diffopt);
249 opt->diffopt.output_format = saved_fmt;
250 return 0;
251 }
91539833 252
3969cf7d
JH
253 if (opt->loginfo && !opt->no_commit_id) {
254 /* When showing a verbose header (i.e. log message),
255 * and not in --pretty=oneline format, we would want
256 * an extra newline between the end of log and the
257 * output for readability.
258 */
39bc9a6c 259 show_log(opt, opt->diffopt.msg_sep);
3969cf7d
JH
260 if (opt->verbose_header &&
261 opt->commit_format != CMIT_FMT_ONELINE) {
262 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
263 if ((pch & opt->diffopt.output_format) == pch)
264 printf("---%c", opt->diffopt.line_termination);
265 else
266 putchar(opt->diffopt.line_termination);
267 }
268 }
5f1c3f07
JH
269 diff_flush(&opt->diffopt);
270 return 1;
271}
272
cd2bdc53 273static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
274{
275 unsigned const char *sha1 = commit->object.sha1;
276
91539833
LT
277 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
278 return !opt->loginfo;
5f1c3f07
JH
279}
280
91539833
LT
281/*
282 * Show the diff of a commit.
283 *
284 * Return true if we printed any log info messages
285 */
286static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 287{
91539833 288 int showed_log;
5f1c3f07
JH
289 struct commit_list *parents;
290 unsigned const char *sha1 = commit->object.sha1;
291
91539833
LT
292 if (!opt->diff)
293 return 0;
294
5f1c3f07 295 /* Root commit? */
91539833
LT
296 parents = commit->parents;
297 if (!parents) {
2b60356d
RS
298 if (opt->show_root_diff) {
299 diff_root_tree_sha1(sha1, "", &opt->diffopt);
300 log_tree_diff_flush(opt);
301 }
91539833 302 return !opt->loginfo;
5f1c3f07
JH
303 }
304
305 /* More than one parent? */
91539833 306 if (parents && parents->next) {
5f1c3f07
JH
307 if (opt->ignore_merges)
308 return 0;
309 else if (opt->combine_merges)
310 return do_diff_combined(opt, commit);
91539833
LT
311
312 /* If we show individual diffs, show the parent info */
313 log->parent = parents->item;
5f1c3f07
JH
314 }
315
91539833
LT
316 showed_log = 0;
317 for (;;) {
5f1c3f07 318 struct commit *parent = parents->item;
5f1c3f07 319
91539833
LT
320 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
321 log_tree_diff_flush(opt);
322
323 showed_log |= !opt->loginfo;
324
325 /* Set up the log info for the next parent, if any.. */
326 parents = parents->next;
327 if (!parents)
328 break;
329 log->parent = parents->item;
330 opt->loginfo = log;
331 }
332 return showed_log;
333}
334
335int log_tree_commit(struct rev_info *opt, struct commit *commit)
336{
337 struct log_info log;
3eefc189 338 int shown;
91539833
LT
339
340 log.commit = commit;
341 log.parent = NULL;
342 opt->loginfo = &log;
343
3eefc189
JH
344 shown = log_tree_diff(opt, commit, &log);
345 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 346 log.parent = NULL;
39bc9a6c 347 show_log(opt, "");
3eefc189 348 shown = 1;
5f1c3f07 349 }
91539833 350 opt->loginfo = NULL;
3eefc189 351 return shown;
5f1c3f07 352}