log-tree.c 12.0 KB
Newer Older
1 2 3
#include "cache.h"
#include "diff.h"
#include "commit.h"
4
#include "graph.h"
5
#include "log-tree.h"
6
#include "reflog-walk.h"
7

8 9
struct decoration name_decoration = { "object names" };

10 11 12 13 14 15 16 17 18
static void show_parents(struct commit *commit, int abbrev)
{
	struct commit_list *p;
	for (p = commit->parents; p ; p = p->next) {
		struct commit *parent = p->item;
		printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
	}
}

19
void show_decorations(struct commit *commit)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
	const char *prefix;
	struct name_decoration *decoration;

	decoration = lookup_decoration(&name_decoration, &commit->object);
	if (!decoration)
		return;
	prefix = " (";
	while (decoration) {
		printf("%s%s", prefix, decoration->name);
		prefix = ", ";
		decoration = decoration->next;
	}
	putchar(')');
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*
 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
 * Signed-off-by: and Acked-by: lines.
 */
static int detect_any_signoff(char *letter, int size)
{
	char ch, *cp;
	int seen_colon = 0;
	int seen_at = 0;
	int seen_name = 0;
	int seen_head = 0;

	cp = letter + size;
	while (letter <= --cp && (ch = *cp) == '\n')
		continue;

	while (letter <= cp) {
		ch = *cp--;
		if (ch == '\n')
			break;

		if (!seen_at) {
			if (ch == '@')
				seen_at = 1;
			continue;
		}
		if (!seen_colon) {
			if (ch == '@')
				return 0;
			else if (ch == ':')
				seen_colon = 1;
			else
				seen_name = 1;
			continue;
		}
		if (('A' <= ch && ch <= 'Z') ||
		    ('a' <= ch && ch <= 'z') ||
		    ch == '-') {
			seen_head = 1;
			continue;
		}
		/* no empty last line doesn't match */
		return 0;
	}
	return seen_head && seen_name;
}

83
static void append_signoff(struct strbuf *sb, const char *signoff)
J
Junio C Hamano 已提交
84 85
{
	static const char signed_off_by[] = "Signed-off-by: ";
86
	size_t signoff_len = strlen(signoff);
87
	int has_signoff = 0;
88
	char *cp;
J
Junio C Hamano 已提交
89

90
	cp = sb->buf;
J
Junio C Hamano 已提交
91 92

	/* First see if we already have the sign-off by the signer */
93 94 95 96
	while ((cp = strstr(cp, signed_off_by))) {

		has_signoff = 1;

J
Junio C Hamano 已提交
97
		cp += strlen(signed_off_by);
98
		if (cp + signoff_len >= sb->buf + sb->len)
99 100 101 102 103 104
			break;
		if (strncmp(cp, signoff, signoff_len))
			continue;
		if (!isspace(cp[signoff_len]))
			continue;
		/* we already have him */
105
		return;
J
Junio C Hamano 已提交
106 107
	}

108
	if (!has_signoff)
109
		has_signoff = detect_any_signoff(sb->buf, sb->len);
110 111

	if (!has_signoff)
112
		strbuf_addch(sb, '\n');
113

114 115 116
	strbuf_addstr(sb, signed_off_by);
	strbuf_add(sb, signoff, signoff_len);
	strbuf_addch(sb, '\n');
J
Junio C Hamano 已提交
117 118
}

119 120 121 122 123 124 125 126 127 128
static unsigned int digits_in_number(unsigned int number)
{
	unsigned int i = 10, result = 1;
	while (i <= number) {
		i *= 10;
		result++;
	}
	return result;
}

129 130 131 132 133 134 135 136 137 138 139 140
static int has_non_ascii(const char *s)
{
	int ch;
	if (!s)
		return 0;
	while ((ch = *s++) != '\0') {
		if (non_ascii(ch))
			return 1;
	}
	return 0;
}

141
void log_write_email_headers(struct rev_info *opt, const char *name,
J
Junio C Hamano 已提交
142 143 144
			     const char **subject_p,
			     const char **extra_headers_p,
			     int *need_8bit_cte_p)
145 146 147
{
	const char *subject = NULL;
	const char *extra_headers = opt->extra_headers;
J
Junio C Hamano 已提交
148 149

	*need_8bit_cte_p = 0; /* unknown */
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	if (opt->total > 0) {
		static char buffer[64];
		snprintf(buffer, sizeof(buffer),
			 "Subject: [%s %0*d/%d] ",
			 opt->subject_prefix,
			 digits_in_number(opt->total),
			 opt->nr, opt->total);
		subject = buffer;
	} else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
		static char buffer[256];
		snprintf(buffer, sizeof(buffer),
			 "Subject: [%s] ",
			 opt->subject_prefix);
		subject = buffer;
	} else {
		subject = "Subject: ";
	}

	printf("From %s Mon Sep 17 00:00:00 2001\n", name);
169 170
	graph_show_oneline(opt->graph);
	if (opt->message_id) {
171
		printf("Message-Id: <%s>\n", opt->message_id);
172 173 174
		graph_show_oneline(opt->graph);
	}
	if (opt->ref_message_id) {
175 176
		printf("In-Reply-To: <%s>\nReferences: <%s>\n",
		       opt->ref_message_id, opt->ref_message_id);
177 178
		graph_show_oneline(opt->graph);
	}
179 180 181
	if (opt->mime_boundary) {
		static char subject_buffer[1024];
		static char buffer[1024];
J
Junio C Hamano 已提交
182
		*need_8bit_cte_p = -1; /* NEVER */
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		snprintf(subject_buffer, sizeof(subject_buffer) - 1,
			 "%s"
			 "MIME-Version: 1.0\n"
			 "Content-Type: multipart/mixed;"
			 " boundary=\"%s%s\"\n"
			 "\n"
			 "This is a multi-part message in MIME "
			 "format.\n"
			 "--%s%s\n"
			 "Content-Type: text/plain; "
			 "charset=UTF-8; format=fixed\n"
			 "Content-Transfer-Encoding: 8bit\n\n",
			 extra_headers ? extra_headers : "",
			 mime_boundary_leader, opt->mime_boundary,
			 mime_boundary_leader, opt->mime_boundary);
		extra_headers = subject_buffer;

		snprintf(buffer, sizeof(buffer) - 1,
201
			 "\n--%s%s\n"
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
			 "Content-Type: text/x-patch;"
			 " name=\"%s.diff\"\n"
			 "Content-Transfer-Encoding: 8bit\n"
			 "Content-Disposition: %s;"
			 " filename=\"%s.diff\"\n\n",
			 mime_boundary_leader, opt->mime_boundary,
			 name,
			 opt->no_inline ? "attachment" : "inline",
			 name);
		opt->diffopt.stat_sep = buffer;
	}
	*subject_p = subject;
	*extra_headers_p = extra_headers;
}

217
void show_log(struct rev_info *opt)
L
Linus Torvalds 已提交
218
{
219
	struct strbuf msgbuf;
T
Timo Hirvonen 已提交
220
	struct log_info *log = opt->loginfo;
L
Linus Torvalds 已提交
221 222 223
	struct commit *commit = log->commit, *parent = log->parent;
	int abbrev = opt->diffopt.abbrev;
	int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
224
	const char *subject = NULL, *extra_headers = opt->extra_headers;
225
	int need_8bit_cte = 0;
L
Linus Torvalds 已提交
226 227 228

	opt->loginfo = NULL;
	if (!opt->verbose_header) {
229 230
		graph_show_commit(opt->graph);

231 232 233 234 235 236 237 238 239 240 241
		if (!opt->graph) {
			if (commit->object.flags & BOUNDARY)
				putchar('-');
			else if (commit->object.flags & UNINTERESTING)
				putchar('^');
			else if (opt->left_right) {
				if (commit->object.flags & SYMMETRIC_LEFT)
					putchar('<');
				else
					putchar('>');
			}
242
		}
243
		fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
244
		if (opt->print_parents)
245
			show_parents(commit, abbrev_commit);
246
		show_decorations(commit);
247 248 249 250
		if (opt->graph && !graph_is_commit_finished(opt->graph)) {
			putchar('\n');
			graph_show_remainder(opt->graph);
		}
251
		putchar(opt->diffopt.line_termination);
L
Linus Torvalds 已提交
252 253 254 255
		return;
	}

	/*
256 257 258
	 * If use_terminator is set, add a newline at the end of the entry.
	 * Otherwise, add a diffopt.line_termination character before all
	 * entries but the first.  (IOW, as a separator between entries)
L
Linus Torvalds 已提交
259
	 */
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	if (opt->shown_one && !opt->use_terminator) {
		/*
		 * If entries are separated by a newline, the output
		 * should look human-readable.  If the last entry ended
		 * with a newline, print the graph output before this
		 * newline.  Otherwise it will end up as a completely blank
		 * line and will look like a gap in the graph.
		 *
		 * If the entry separator is not a newline, the output is
		 * primarily intended for programmatic consumption, and we
		 * never want the extra graph output before the entry
		 * separator.
		 */
		if (opt->diffopt.line_termination == '\n' &&
		    !opt->missing_newline)
			graph_show_padding(opt->graph);
L
Linus Torvalds 已提交
276
		putchar(opt->diffopt.line_termination);
277
	}
L
Linus Torvalds 已提交
278 279
	opt->shown_one = 1;

280 281 282 283 284 285
	/*
	 * If the history graph was requested,
	 * print the graph, up to this commit's line
	 */
	graph_show_commit(opt->graph);

L
Linus Torvalds 已提交
286 287 288
	/*
	 * Print header line of header..
	 */
289

290
	if (opt->commit_format == CMIT_FMT_EMAIL) {
291
		log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
J
Junio C Hamano 已提交
292 293
					&subject, &extra_headers,
					&need_8bit_cte);
294
	} else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
295
		fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
296 297
		if (opt->commit_format != CMIT_FMT_ONELINE)
			fputs("commit ", stdout);
298 299 300 301 302 303 304 305 306 307 308 309

		if (!opt->graph) {
			if (commit->object.flags & BOUNDARY)
				putchar('-');
			else if (commit->object.flags & UNINTERESTING)
				putchar('^');
			else if (opt->left_right) {
				if (commit->object.flags & SYMMETRIC_LEFT)
					putchar('<');
				else
					putchar('>');
			}
310 311 312
		}
		fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
		      stdout);
313
		if (opt->print_parents)
314
			show_parents(commit, abbrev_commit);
J
Junio C Hamano 已提交
315
		if (parent)
316 317 318
			printf(" (from %s)",
			       diff_unique_abbrev(parent->object.sha1,
						  abbrev_commit));
319
		show_decorations(commit);
320
		printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
321 322 323 324 325 326
		if (opt->commit_format == CMIT_FMT_ONELINE) {
			putchar(' ');
		} else {
			putchar('\n');
			graph_show_oneline(opt->graph);
		}
327
		if (opt->reflog_info) {
328 329 330 331 332 333
			/*
			 * setup_revisions() ensures that opt->reflog_info
			 * and opt->graph cannot both be set,
			 * so we don't need to worry about printing the
			 * graph info here.
			 */
334
			show_reflog_message(opt->reflog_info,
335
				    opt->commit_format == CMIT_FMT_ONELINE,
336
				    opt->date_mode);
337
			if (opt->commit_format == CMIT_FMT_ONELINE)
338 339
				return;
		}
340
	}
L
Linus Torvalds 已提交
341

342 343 344
	if (!commit->buffer)
		return;

L
Linus Torvalds 已提交
345 346 347
	/*
	 * And then the pretty-printed message itself
	 */
348
	strbuf_init(&msgbuf, 0);
349 350
	if (need_8bit_cte >= 0)
		need_8bit_cte = has_non_ascii(opt->add_signoff);
351
	pretty_print_commit(opt->commit_format, commit, &msgbuf,
352
			    abbrev, subject, extra_headers, opt->date_mode,
353
			    need_8bit_cte);
J
Junio C Hamano 已提交
354 355

	if (opt->add_signoff)
356
		append_signoff(&msgbuf, opt->add_signoff);
357
	if (opt->show_log_size) {
358
		printf("log size %i\n", (int)msgbuf.len);
359 360
		graph_show_oneline(opt->graph);
	}
361

362 363 364 365 366 367 368 369 370 371 372 373
	/*
	 * Set opt->missing_newline if msgbuf doesn't
	 * end in a newline (including if it is empty)
	 */
	if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
		opt->missing_newline = 1;
	else
		opt->missing_newline = 0;

	if (opt->graph)
		graph_show_commit_msg(opt->graph, &msgbuf);
	else
374
		fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
375 376 377
	if (opt->use_terminator) {
		if (!opt->missing_newline)
			graph_show_padding(opt->graph);
378
		putchar('\n');
379 380
	}

381
	strbuf_release(&msgbuf);
L
Linus Torvalds 已提交
382 383
}

384
int log_tree_diff_flush(struct rev_info *opt)
385 386
{
	diffcore_std(&opt->diffopt);
L
Linus Torvalds 已提交
387

388 389 390 391 392 393 394
	if (diff_queue_is_empty()) {
		int saved_fmt = opt->diffopt.output_format;
		opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
		diff_flush(&opt->diffopt);
		opt->diffopt.output_format = saved_fmt;
		return 0;
	}
L
Linus Torvalds 已提交
395

396 397 398 399 400 401
	if (opt->loginfo && !opt->no_commit_id) {
		/* When showing a verbose header (i.e. log message),
		 * and not in --pretty=oneline format, we would want
		 * an extra newline between the end of log and the
		 * output for readability.
		 */
402
		show_log(opt);
403 404
		if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
		    opt->verbose_header &&
405 406 407
		    opt->commit_format != CMIT_FMT_ONELINE) {
			int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
			if ((pch & opt->diffopt.output_format) == pch)
L
Linus Torvalds 已提交
408 409
				printf("---");
			putchar('\n');
410 411
		}
	}
412 413 414 415
	diff_flush(&opt->diffopt);
	return 1;
}

416
static int do_diff_combined(struct rev_info *opt, struct commit *commit)
417 418 419
{
	unsigned const char *sha1 = commit->object.sha1;

L
Linus Torvalds 已提交
420 421
	diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
	return !opt->loginfo;
422 423
}

L
Linus Torvalds 已提交
424 425 426 427 428 429
/*
 * Show the diff of a commit.
 *
 * Return true if we printed any log info messages
 */
static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
430
{
L
Linus Torvalds 已提交
431
	int showed_log;
432 433 434
	struct commit_list *parents;
	unsigned const char *sha1 = commit->object.sha1;

435
	if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
L
Linus Torvalds 已提交
436 437
		return 0;

438
	/* Root commit? */
L
Linus Torvalds 已提交
439 440
	parents = commit->parents;
	if (!parents) {
R
Rene Scharfe 已提交
441 442 443 444
		if (opt->show_root_diff) {
			diff_root_tree_sha1(sha1, "", &opt->diffopt);
			log_tree_diff_flush(opt);
		}
L
Linus Torvalds 已提交
445
		return !opt->loginfo;
446 447 448
	}

	/* More than one parent? */
L
Linus Torvalds 已提交
449
	if (parents && parents->next) {
450 451 452 453
		if (opt->ignore_merges)
			return 0;
		else if (opt->combine_merges)
			return do_diff_combined(opt, commit);
L
Linus Torvalds 已提交
454 455 456

		/* If we show individual diffs, show the parent info */
		log->parent = parents->item;
457 458
	}

L
Linus Torvalds 已提交
459 460
	showed_log = 0;
	for (;;) {
461 462
		struct commit *parent = parents->item;

L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
		log_tree_diff_flush(opt);

		showed_log |= !opt->loginfo;

		/* Set up the log info for the next parent, if any.. */
		parents = parents->next;
		if (!parents)
			break;
		log->parent = parents->item;
		opt->loginfo = log;
	}
	return showed_log;
}

int log_tree_commit(struct rev_info *opt, struct commit *commit)
{
	struct log_info log;
481
	int shown;
L
Linus Torvalds 已提交
482 483 484 485 486

	log.commit = commit;
	log.parent = NULL;
	opt->loginfo = &log;

487 488
	shown = log_tree_diff(opt, commit, &log);
	if (!shown && opt->loginfo && opt->always_show_header) {
L
Linus Torvalds 已提交
489
		log.parent = NULL;
490
		show_log(opt);
491
		shown = 1;
492
	}
L
Linus Torvalds 已提交
493
	opt->loginfo = NULL;
494
	maybe_flush_or_die(stdout, "stdout");
495
	return shown;
496
}