builtin-log.c 33.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Builtin "git log" and related commands (show, whatchanged)
 *
 * (C) Copyright 2006 Linus Torvalds
 *		 2006 Junio Hamano
 */
#include "cache.h"
8
#include "color.h"
9 10 11 12
#include "commit.h"
#include "diff.h"
#include "revision.h"
#include "log-tree.h"
13
#include "builtin.h"
14
#include "tag.h"
L
Linus Torvalds 已提交
15
#include "reflog-walk.h"
16
#include "patch-ids.h"
17
#include "run-command.h"
18
#include "shortlog.h"
19
#include "remote.h"
20
#include "string-list.h"
21
#include "parse-options.h"
22

H
Heikki Orsila 已提交
23 24 25
/* Set a default date-time format for git log ("log.date" config variable) */
static const char *default_date_mode = NULL;

26
static int default_show_root = 1;
27
static const char *fmt_patch_subject_prefix = "PATCH";
28
static const char *fmt_pretty;
29

30
static void cmd_log_init(int argc, const char **argv, const char *prefix,
31 32
		      struct rev_info *rev)
{
J
Junio C Hamano 已提交
33 34
	int i;

35 36
	rev->abbrev = DEFAULT_ABBREV;
	rev->commit_format = CMIT_FMT_DEFAULT;
37
	if (fmt_pretty)
38
		get_commit_format(fmt_pretty, rev);
39
	rev->verbose_header = 1;
40
	DIFF_OPT_SET(&rev->diffopt, RECURSIVE);
41
	rev->show_root_diff = default_show_root;
42
	rev->subject_prefix = fmt_patch_subject_prefix;
J
Jeff King 已提交
43
	DIFF_OPT_SET(&rev->diffopt, ALLOW_TEXTCONV);
H
Heikki Orsila 已提交
44 45 46 47

	if (default_date_mode)
		rev->date_mode = parse_date_format(default_date_mode);

48
	argc = setup_revisions(argc, argv, rev, "HEAD");
H
Heikki Orsila 已提交
49

50 51
	if (rev->diffopt.pickaxe || rev->diffopt.filter)
		rev->always_show_header = 0;
52
	if (DIFF_OPT_TST(&rev->diffopt, FOLLOW_RENAMES)) {
53 54 55 56
		rev->always_show_header = 0;
		if (rev->diffopt.nr_paths != 1)
			usage("git logs can only follow renames on one pathname at a time");
	}
J
Junio C Hamano 已提交
57 58
	for (i = 1; i < argc; i++) {
		const char *arg = argv[i];
59
		if (!strcmp(arg, "--decorate")) {
R
René Scharfe 已提交
60
			load_ref_decorations();
61
			rev->show_decorations = 1;
62 63
		} else if (!strcmp(arg, "--source")) {
			rev->show_source = 1;
64
		} else
J
Junio C Hamano 已提交
65 66
			die("unrecognized argument: %s", arg);
	}
67 68
}

L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80
/*
 * This gives a rough estimate for how many commits we
 * will print out in the list.
 */
static int estimate_commit_count(struct rev_info *rev, struct commit_list *list)
{
	int n = 0;

	while (list) {
		struct commit *commit = list->item;
		unsigned int flags = commit->object.flags;
		list = list->next;
81
		if (!(flags & (TREESAME | UNINTERESTING)))
L
Linus Torvalds 已提交
82
			n++;
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	}
	return n;
}

static void show_early_header(struct rev_info *rev, const char *stage, int nr)
{
	if (rev->shown_one) {
		rev->shown_one = 0;
		if (rev->commit_format != CMIT_FMT_ONELINE)
			putchar(rev->diffopt.line_termination);
	}
	printf("Final output: %d %s\n", nr, stage);
}

struct itimerval early_output_timer;

99 100 101
static void log_show_early(struct rev_info *revs, struct commit_list *list)
{
	int i = revs->early_output;
L
Linus Torvalds 已提交
102
	int show_header = 1;
103 104 105 106

	sort_in_topological_order(&list, revs->lifo);
	while (list && i) {
		struct commit *commit = list->item;
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		switch (simplify_commit(revs, commit)) {
		case commit_show:
			if (show_header) {
				int n = estimate_commit_count(revs, list);
				show_early_header(revs, "incomplete", n);
				show_header = 0;
			}
			log_tree_commit(revs, commit);
			i--;
			break;
		case commit_ignore:
			break;
		case commit_error:
			return;
		}
122 123
		list = list->next;
	}
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	/* Did we already get enough commits for the early output? */
	if (!i)
		return;

	/*
	 * ..if no, then repeat it twice a second until we
	 * do.
	 *
	 * NOTE! We don't use "it_interval", because if the
	 * reader isn't listening, we want our output to be
	 * throttled by the writing, and not have the timer
	 * trigger every second even if we're blocked on a
	 * reader!
	 */
	early_output_timer.it_value.tv_sec = 0;
	early_output_timer.it_value.tv_usec = 500000;
	setitimer(ITIMER_REAL, &early_output_timer, NULL);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

static void early_output(int signal)
{
	show_early_output = log_show_early;
}

static void setup_early_output(struct rev_info *rev)
{
	struct sigaction sa;

	/*
	 * Set up the signal handler, minimally intrusively:
	 * we only set a single volatile integer word (not
	 * using sigatomic_t - trying to avoid unnecessary
	 * system dependencies and headers), and using
	 * SA_RESTART.
	 */
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = early_output;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	sigaction(SIGALRM, &sa, NULL);

	/*
	 * If we can get the whole output in less than a
	 * tenth of a second, don't even bother doing the
	 * early-output thing..
	 *
	 * This is a one-time-only trigger.
	 */
L
Linus Torvalds 已提交
173 174 175
	early_output_timer.it_value.tv_sec = 0;
	early_output_timer.it_value.tv_usec = 100000;
	setitimer(ITIMER_REAL, &early_output_timer, NULL);
176 177 178 179
}

static void finish_early_output(struct rev_info *rev)
{
L
Linus Torvalds 已提交
180
	int n = estimate_commit_count(rev, rev->commits);
181
	signal(SIGALRM, SIG_IGN);
L
Linus Torvalds 已提交
182
	show_early_header(rev, "done", n);
183 184
}

185 186 187
static int cmd_log_walk(struct rev_info *rev)
{
	struct commit *commit;
188

189 190 191
	if (rev->early_output)
		setup_early_output(rev);

192 193
	if (prepare_revision_walk(rev))
		die("revision walk setup failed");
194 195 196 197

	if (rev->early_output)
		finish_early_output(rev);

198
	/*
199 200 201
	 * For --check and --exit-code, the exit code is based on CHECK_FAILED
	 * and HAS_CHANGES being accumulated in rev->diffopt, so be careful to
	 * retain that state information if replacing rev->diffopt in this loop
202
	 */
203 204
	while ((commit = get_revision(rev)) != NULL) {
		log_tree_commit(rev, commit);
205 206 207 208 209
		if (!rev->reflog_info) {
			/* we allow cycles in reflog ancestry */
			free(commit->buffer);
			commit->buffer = NULL;
		}
L
Linus Torvalds 已提交
210 211
		free_commit_list(commit->parents);
		commit->parents = NULL;
212
	}
213 214 215 216
	if (rev->diffopt.output_format & DIFF_FORMAT_CHECKDIFF &&
	    DIFF_OPT_TST(&rev->diffopt, CHECK_FAILED)) {
		return 02;
	}
217
	return diff_result_code(&rev->diffopt, 0);
218 219
}

220
static int git_log_config(const char *var, const char *value, void *cb)
221
{
222 223
	if (!strcmp(var, "format.pretty"))
		return git_config_string(&fmt_pretty, var, value);
224 225
	if (!strcmp(var, "format.subjectprefix"))
		return git_config_string(&fmt_patch_subject_prefix, var, value);
H
Heikki Orsila 已提交
226 227
	if (!strcmp(var, "log.date"))
		return git_config_string(&default_date_mode, var, value);
228 229 230 231
	if (!strcmp(var, "log.showroot")) {
		default_show_root = git_config_bool(var, value);
		return 0;
	}
232
	return git_diff_ui_config(var, value, cb);
233 234
}

235
int cmd_whatchanged(int argc, const char **argv, const char *prefix)
236 237 238
{
	struct rev_info rev;

239
	git_config(git_log_config, NULL);
240 241 242 243

	if (diff_use_color_default == -1)
		diff_use_color_default = git_use_color_default;

244
	init_revisions(&rev, prefix);
245
	rev.diff = 1;
L
Linus Torvalds 已提交
246
	rev.simplify_history = 0;
247
	cmd_log_init(argc, argv, prefix, &rev);
248 249 250
	if (!rev.diffopt.output_format)
		rev.diffopt.output_format = DIFF_FORMAT_RAW;
	return cmd_log_walk(&rev);
251 252
}

253 254
static void show_tagger(char *buf, int len, struct rev_info *rev)
{
255
	struct strbuf out = STRBUF_INIT;
256

257 258 259 260 261
	pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
		git_log_output_encoding ?
		git_log_output_encoding: git_commit_encoding);
	printf("%s\n", out.buf);
	strbuf_release(&out);
262 263 264 265
}

static int show_object(const unsigned char *sha1, int show_tag_object,
	struct rev_info *rev)
266 267
{
	unsigned long size;
268 269
	enum object_type type;
	char *buf = read_sha1_file(sha1, &type, &size);
270 271 272 273 274
	int offset = 0;

	if (!buf)
		return error("Could not read object %s", sha1_to_hex(sha1));

275 276 277
	if (show_tag_object)
		while (offset < size && buf[offset] != '\n') {
			int new_offset = offset + 1;
278 279
			while (new_offset < size && buf[new_offset++] != '\n')
				; /* do nothing */
280 281 282
			if (!prefixcmp(buf + offset, "tagger "))
				show_tagger(buf + offset + 7,
					    new_offset - offset - 7, rev);
283 284 285 286 287 288 289 290 291 292 293
			offset = new_offset;
		}

	if (offset < size)
		fwrite(buf + offset, size - offset, 1, stdout);
	free(buf);
	return 0;
}

static int show_tree_object(const unsigned char *sha1,
		const char *base, int baselen,
294
		const char *pathname, unsigned mode, int stage, void *context)
295 296 297 298 299
{
	printf("%s%s\n", pathname, S_ISDIR(mode) ? "/" : "");
	return 0;
}

300
int cmd_show(int argc, const char **argv, const char *prefix)
301 302
{
	struct rev_info rev;
303 304
	struct object_array_entry *objects;
	int i, count, ret = 0;
305

306
	git_config(git_log_config, NULL);
307 308 309 310

	if (diff_use_color_default == -1)
		diff_use_color_default = git_use_color_default;

311
	init_revisions(&rev, prefix);
312 313 314 315 316 317
	rev.diff = 1;
	rev.combine_merges = 1;
	rev.dense_combined_merges = 1;
	rev.always_show_header = 1;
	rev.ignore_merges = 0;
	rev.no_walk = 1;
318
	cmd_log_init(argc, argv, prefix, &rev);
319 320 321 322 323 324 325 326

	count = rev.pending.nr;
	objects = rev.pending.objects;
	for (i = 0; i < count && !ret; i++) {
		struct object *o = objects[i].item;
		const char *name = objects[i].name;
		switch (o->type) {
		case OBJ_BLOB:
327
			ret = show_object(o->sha1, 0, NULL);
328 329 330 331
			break;
		case OBJ_TAG: {
			struct tag *t = (struct tag *)o;

332
			printf("%stag %s%s\n",
333
					diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
334
					t->tag,
335
					diff_get_color_opt(&rev.diffopt, DIFF_RESET));
336
			ret = show_object(o->sha1, 1, &rev);
337 338 339 340 341 342 343
			if (ret)
				break;
			o = parse_object(t->tagged->sha1);
			if (!o)
				ret = error("Could not read object %s",
					    sha1_to_hex(t->tagged->sha1));
			objects[i].item = o;
344 345 346 347 348
			i--;
			break;
		}
		case OBJ_TREE:
			printf("%stree %s%s\n\n",
349
					diff_get_color_opt(&rev.diffopt, DIFF_COMMIT),
350
					name,
351
					diff_get_color_opt(&rev.diffopt, DIFF_RESET));
352
			read_tree_recursive((struct tree *)o, "", 0, 0, NULL,
353
					show_tree_object, NULL);
354 355 356 357 358 359 360 361 362 363 364 365 366
			break;
		case OBJ_COMMIT:
			rev.pending.nr = rev.pending.alloc = 0;
			rev.pending.objects = NULL;
			add_object_array(o, name, &rev.pending);
			ret = cmd_log_walk(&rev);
			break;
		default:
			ret = error("Unknown type: %d", o->type);
		}
	}
	free(objects);
	return ret;
367 368
}

L
Linus Torvalds 已提交
369 370 371 372 373 374 375
/*
 * This is equivalent to "git log -g --abbrev-commit --pretty=oneline"
 */
int cmd_log_reflog(int argc, const char **argv, const char *prefix)
{
	struct rev_info rev;

376
	git_config(git_log_config, NULL);
377 378 379 380

	if (diff_use_color_default == -1)
		diff_use_color_default = git_use_color_default;

L
Linus Torvalds 已提交
381 382 383 384 385 386 387 388 389 390 391 392
	init_revisions(&rev, prefix);
	init_reflog_walk(&rev.reflog_info);
	rev.abbrev_commit = 1;
	rev.verbose_header = 1;
	cmd_log_init(argc, argv, prefix, &rev);

	/*
	 * This means that we override whatever commit format the user gave
	 * on the cmd line.  Sad, but cmd_log_init() currently doesn't
	 * allow us to set a different default.
	 */
	rev.commit_format = CMIT_FMT_ONELINE;
393
	rev.use_terminator = 1;
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404
	rev.always_show_header = 1;

	/*
	 * We get called through "git reflog", so unlike the other log
	 * routines, we need to set up our pager manually..
	 */
	setup_pager();

	return cmd_log_walk(&rev);
}

405
int cmd_log(int argc, const char **argv, const char *prefix)
406 407 408
{
	struct rev_info rev;

409
	git_config(git_log_config, NULL);
410 411 412 413

	if (diff_use_color_default == -1)
		diff_use_color_default = git_use_color_default;

414
	init_revisions(&rev, prefix);
415
	rev.always_show_header = 1;
416
	cmd_log_init(argc, argv, prefix, &rev);
417
	return cmd_log_walk(&rev);
418
}
419

420
/* format-patch */
421

422
static const char *fmt_patch_suffix = ".patch";
423
static int numbered = 0;
424
static int auto_number = 1;
425

426 427
static char *default_attach = NULL;

D
Daniel Barkalow 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
static char **extra_hdr;
static int extra_hdr_nr;
static int extra_hdr_alloc;

static char **extra_to;
static int extra_to_nr;
static int extra_to_alloc;

static char **extra_cc;
static int extra_cc_nr;
static int extra_cc_alloc;

static void add_header(const char *value)
{
	int len = strlen(value);
443
	while (len && value[len - 1] == '\n')
D
Daniel Barkalow 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		len--;
	if (!strncasecmp(value, "to: ", 4)) {
		ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);
		extra_to[extra_to_nr++] = xstrndup(value + 4, len - 4);
		return;
	}
	if (!strncasecmp(value, "cc: ", 4)) {
		ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
		extra_cc[extra_cc_nr++] = xstrndup(value + 4, len - 4);
		return;
	}
	ALLOC_GROW(extra_hdr, extra_hdr_nr + 1, extra_hdr_alloc);
	extra_hdr[extra_hdr_nr++] = xstrndup(value, len);
}

459 460 461
#define THREAD_SHALLOW 1
#define THREAD_DEEP 2
static int thread = 0;
462
static int do_signoff = 0;
463

464
static int git_format_config(const char *var, const char *value, void *cb)
465 466
{
	if (!strcmp(var, "format.headers")) {
467 468
		if (!value)
			die("format.headers without value");
D
Daniel Barkalow 已提交
469
		add_header(value);
470 471
		return 0;
	}
472 473
	if (!strcmp(var, "format.suffix"))
		return git_config_string(&fmt_patch_suffix, var, value);
474 475 476 477 478 479 480
	if (!strcmp(var, "format.cc")) {
		if (!value)
			return config_error_nonbool(var);
		ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
		extra_cc[extra_cc_nr++] = xstrdup(value);
		return 0;
	}
481
	if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
482 483
		return 0;
	}
484
	if (!strcmp(var, "format.numbered")) {
485
		if (value && !strcasecmp(value, "auto")) {
486 487 488 489
			auto_number = 1;
			return 0;
		}
		numbered = git_config_bool(var, value);
490
		auto_number = auto_number && numbered;
491 492
		return 0;
	}
493 494 495 496 497 498 499
	if (!strcmp(var, "format.attach")) {
		if (value && *value)
			default_attach = xstrdup(value);
		else
			default_attach = xstrdup(git_version_string);
		return 0;
	}
500 501 502 503 504 505 506 507 508 509 510 511
	if (!strcmp(var, "format.thread")) {
		if (value && !strcasecmp(value, "deep")) {
			thread = THREAD_DEEP;
			return 0;
		}
		if (value && !strcasecmp(value, "shallow")) {
			thread = THREAD_SHALLOW;
			return 0;
		}
		thread = git_config_bool(var, value) && THREAD_SHALLOW;
		return 0;
	}
512 513 514 515
	if (!strcmp(var, "format.signoff")) {
		do_signoff = git_config_bool(var, value);
		return 0;
	}
516

517
	return git_log_config(var, value, cb);
518 519
}

520
static FILE *realstdout = NULL;
521
static const char *output_directory = NULL;
522
static int outdir_offset;
523

524
static int reopen_stdout(struct commit *commit, struct rev_info *rev)
525
{
526
	struct strbuf filename = STRBUF_INIT;
527
	int suffix_len = strlen(fmt_patch_suffix) + 1;
528

529
	if (output_directory) {
530 531 532
		strbuf_addstr(&filename, output_directory);
		if (filename.len >=
		    PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
533
			return error("name of output directory is too long");
534 535
		if (filename.buf[filename.len - 1] != '/')
			strbuf_addch(&filename, '/');
536
	}
537

538
	get_patch_filename(commit, rev->nr, fmt_patch_suffix, &filename);
539

N
Nate Case 已提交
540
	if (!DIFF_OPT_TST(&rev->diffopt, QUIET))
541
		fprintf(realstdout, "%s\n", filename.buf + outdir_offset);
N
Nate Case 已提交
542

543 544
	if (freopen(filename.buf, "w", stdout) == NULL)
		return error("Cannot open patch file %s", filename.buf);
545

546
	strbuf_release(&filename);
547
	return 0;
548 549
}

550
static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids, const char *prefix)
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
	struct rev_info check_rev;
	struct commit *commit;
	struct object *o1, *o2;
	unsigned flags1, flags2;

	if (rev->pending.nr != 2)
		die("Need exactly one range.");

	o1 = rev->pending.objects[0].item;
	flags1 = o1->flags;
	o2 = rev->pending.objects[1].item;
	flags2 = o2->flags;

	if ((flags1 & UNINTERESTING) == (flags2 & UNINTERESTING))
		die("Not a range.");

568
	init_patch_ids(ids);
569 570

	/* given a range a..b get all patch ids for b..a */
571
	init_revisions(&check_rev, prefix);
572 573 574 575
	o1->flags ^= UNINTERESTING;
	o2->flags ^= UNINTERESTING;
	add_pending_object(&check_rev, o1, "o1");
	add_pending_object(&check_rev, o2, "o2");
576 577
	if (prepare_revision_walk(&check_rev))
		die("revision walk setup failed");
578 579 580 581 582 583

	while ((commit = get_revision(&check_rev)) != NULL) {
		/* ignore merges */
		if (commit->parents && commit->parents->next)
			continue;

584
		add_commit_patch_id(commit, ids);
585 586 587
	}

	/* reset for next revision walk */
588 589 590 591
	clear_commit_marks((struct commit *)o1,
			SEEN | UNINTERESTING | SHOWN | ADDED);
	clear_commit_marks((struct commit *)o2,
			SEEN | UNINTERESTING | SHOWN | ADDED);
592 593 594 595
	o1->flags = flags1;
	o2->flags = flags2;
}

596
static void gen_message_id(struct rev_info *info, char *base)
597
{
598
	const char *committer = git_committer_info(IDENT_WARN_ON_NO_NAME);
599 600
	const char *email_start = strrchr(committer, '<');
	const char *email_end = strrchr(committer, '>');
601
	struct strbuf buf = STRBUF_INIT;
602
	if (!email_start || !email_end || email_start > email_end - 1)
603
		die("Could not extract email from committer identity.");
604 605 606 607
	strbuf_addf(&buf, "%s.%lu.git.%.*s", base,
		    (unsigned long) time(NULL),
		    (int)(email_end - email_start - 1), email_start + 1);
	info->message_id = strbuf_detach(&buf, NULL);
608 609
}

610 611 612 613
static void make_cover_letter(struct rev_info *rev, int use_stdout,
			      int numbered, int numbered_files,
			      struct commit *origin,
			      int nr, struct commit **list, struct commit *head)
614 615 616 617 618 619
{
	const char *committer;
	const char *subject_start = NULL;
	const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
	const char *msg;
	const char *extra_headers = rev->extra_headers;
620
	struct shortlog log;
621
	struct strbuf sb = STRBUF_INIT;
622
	int i;
623
	const char *encoding = "UTF-8";
624
	struct diff_options opts;
J
Junio C Hamano 已提交
625
	int need_8bit_cte = 0;
626
	struct commit *commit = NULL;
627 628 629 630

	if (rev->commit_format != CMIT_FMT_EMAIL)
		die("Cover letter needs email format");

631
	committer = git_committer_info(0);
632

633 634 635 636 637 638 639 640 641 642 643 644 645
	if (!numbered_files) {
		/*
		 * We fake a commit for the cover letter so we get the filename
		 * desired.
		 */
		commit = xcalloc(1, sizeof(*commit));
		commit->buffer = xmalloc(400);
		snprintf(commit->buffer, 400,
			"tree 0000000000000000000000000000000000000000\n"
			"parent %s\n"
			"author %s\n"
			"committer %s\n\n"
			"cover letter\n",
646
			sha1_to_hex(head->object.sha1), committer, committer);
647 648 649
	}

	if (!use_stdout && reopen_stdout(commit, rev))
650 651
		return;

652
	if (commit) {
653

654 655 656
		free(commit->buffer);
		free(commit);
	}
657

658
	log_write_email_headers(rev, head, &subject_start, &extra_headers,
J
Junio C Hamano 已提交
659
				&need_8bit_cte);
660 661 662 663 664

	msg = body;
	pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
		     encoding);
	pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
J
Junio C Hamano 已提交
665
		      encoding, need_8bit_cte);
666 667 668 669 670
	pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
	printf("%s\n", sb.buf);

	strbuf_release(&sb);

671
	shortlog_init(&log);
672 673 674 675
	log.wrap_lines = 1;
	log.wrap = 72;
	log.in1 = 2;
	log.in2 = 4;
676 677 678 679 680
	for (i = 0; i < nr; i++)
		shortlog_add_commit(&log, list[i]);

	shortlog_output(&log);

681
	/*
682
	 * We can only do diffstat with a unique reference point
683 684 685 686
	 */
	if (!origin)
		return;

687 688
	memcpy(&opts, &rev->diffopt, sizeof(opts));
	opts.output_format = DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
689

690 691 692 693 694 695 696
	diff_setup_done(&opts);

	diff_tree_sha1(origin->tree->object.sha1,
		       head->tree->object.sha1,
		       "", &opts);
	diffcore_std(&opts);
	diff_flush(&opts);
697 698

	printf("\n");
699 700
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
static const char *clean_message_id(const char *msg_id)
{
	char ch;
	const char *a, *z, *m;

	m = msg_id;
	while ((ch = *m) && (isspace(ch) || (ch == '<')))
		m++;
	a = m;
	z = NULL;
	while ((ch = *m)) {
		if (!isspace(ch) && (ch != '>'))
			z = m;
		m++;
	}
	if (!z)
		die("insane in-reply-to: %s", msg_id);
	if (++z == m)
		return a;
P
Pierre Habouzit 已提交
720
	return xmemdupz(a, z - a);
721 722
}

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
static const char *set_outdir(const char *prefix, const char *output_directory)
{
	if (output_directory && is_absolute_path(output_directory))
		return output_directory;

	if (!prefix || !*prefix) {
		if (output_directory)
			return output_directory;
		/* The user did not explicitly ask for "./" */
		outdir_offset = 2;
		return "./";
	}

	outdir_offset = strlen(prefix);
	if (!output_directory)
		return prefix;

	return xstrdup(prefix_filename(prefix, outdir_offset,
				       output_directory));
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
static const char * const builtin_format_patch_usage[] = {
	"git format-patch [options] [<since> | <revision range>]",
	NULL
};

static int keep_subject = 0;

static int keep_callback(const struct option *opt, const char *arg, int unset)
{
	((struct rev_info *)opt->value)->total = -1;
	keep_subject = 1;
	return 0;
}

static int subject_prefix = 0;

static int subject_prefix_callback(const struct option *opt, const char *arg,
			    int unset)
{
	subject_prefix = 1;
	((struct rev_info *)opt->value)->subject_prefix = arg;
	return 0;
}

768 769
static int numbered_cmdline_opt = 0;

770 771 772
static int numbered_callback(const struct option *opt, const char *arg,
			     int unset)
{
773
	*(int *)opt->value = numbered_cmdline_opt = unset ? 0 : 1;
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
	if (unset)
		auto_number =  0;
	return 0;
}

static int no_numbered_callback(const struct option *opt, const char *arg,
				int unset)
{
	return numbered_callback(opt, arg, 1);
}

static int output_directory_callback(const struct option *opt, const char *arg,
			      int unset)
{
	const char **dir = (const char **)opt->value;
	if (*dir)
		die("Two output directories?");
	*dir = arg;
	return 0;
}

static int thread_callback(const struct option *opt, const char *arg, int unset)
{
	int *thread = (int *)opt->value;
	if (unset)
		*thread = 0;
	else if (!arg || !strcmp(arg, "shallow"))
		*thread = THREAD_SHALLOW;
	else if (!strcmp(arg, "deep"))
		*thread = THREAD_DEEP;
	else
		return 1;
	return 0;
}

static int attach_callback(const struct option *opt, const char *arg, int unset)
{
	struct rev_info *rev = (struct rev_info *)opt->value;
	if (unset)
		rev->mime_boundary = NULL;
	else if (arg)
		rev->mime_boundary = arg;
	else
		rev->mime_boundary = git_version_string;
	rev->no_inline = unset ? 0 : 1;
	return 0;
}

static int inline_callback(const struct option *opt, const char *arg, int unset)
{
	struct rev_info *rev = (struct rev_info *)opt->value;
	if (unset)
		rev->mime_boundary = NULL;
	else if (arg)
		rev->mime_boundary = arg;
	else
		rev->mime_boundary = git_version_string;
	rev->no_inline = 0;
	return 0;
}

static int header_callback(const struct option *opt, const char *arg, int unset)
{
	add_header(arg);
	return 0;
}

static int cc_callback(const struct option *opt, const char *arg, int unset)
{
	ALLOC_GROW(extra_cc, extra_cc_nr + 1, extra_cc_alloc);
	extra_cc[extra_cc_nr++] = xstrdup(arg);
	return 0;
}

848
int cmd_format_patch(int argc, const char **argv, const char *prefix)
849 850 851 852
{
	struct commit *commit;
	struct commit **list = NULL;
	struct rev_info rev;
853
	int nr = 0, total, i;
854
	int use_stdout = 0;
855
	int start_number = -1;
856
	int numbered_files = 0;		/* _just_ numbers */
857
	int ignore_if_in_upstream = 0;
858
	int cover_letter = 0;
859
	int boundary_count = 0;
860
	int no_binary_diff = 0;
861
	struct commit *origin = NULL, *head = NULL;
862
	const char *in_reply_to = NULL;
863
	struct patch_ids ids;
J
Junio C Hamano 已提交
864
	char *add_signoff = NULL;
865
	struct strbuf buf = STRBUF_INIT;
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	const struct option builtin_format_patch_options[] = {
		{ OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
			    "use [PATCH n/m] even with a single patch",
			    PARSE_OPT_NOARG, numbered_callback },
		{ OPTION_CALLBACK, 'N', "no-numbered", &numbered, NULL,
			    "use [PATCH] even with multiple patches",
			    PARSE_OPT_NOARG, no_numbered_callback },
		OPT_BOOLEAN('s', "signoff", &do_signoff, "add Signed-off-by:"),
		OPT_BOOLEAN(0, "stdout", &use_stdout,
			    "print patches to standard out"),
		OPT_BOOLEAN(0, "cover-letter", &cover_letter,
			    "generate a cover letter"),
		OPT_BOOLEAN(0, "numbered-files", &numbered_files,
			    "use simple number sequence for output file names"),
		OPT_STRING(0, "suffix", &fmt_patch_suffix, "sfx",
			    "use <sfx> instead of '.patch'"),
		OPT_INTEGER(0, "start-number", &start_number,
			    "start numbering patches at <n> instead of 1"),
		{ OPTION_CALLBACK, 0, "subject-prefix", &rev, "prefix",
			    "Use [<prefix>] instead of [PATCH]",
			    PARSE_OPT_NONEG, subject_prefix_callback },
		{ OPTION_CALLBACK, 'o', "output-directory", &output_directory,
			    "dir", "store resulting files in <dir>",
			    PARSE_OPT_NONEG, output_directory_callback },
		{ OPTION_CALLBACK, 'k', "keep-subject", &rev, NULL,
			    "don't strip/add [PATCH]",
			    PARSE_OPT_NOARG | PARSE_OPT_NONEG, keep_callback },
		OPT_BOOLEAN(0, "no-binary", &no_binary_diff,
			    "don't output binary diffs"),
		OPT_BOOLEAN(0, "ignore-if-in-upstream", &ignore_if_in_upstream,
			    "don't include a patch matching a commit upstream"),
		OPT_GROUP("Messaging"),
		{ OPTION_CALLBACK, 0, "add-header", NULL, "header",
			    "add email header", PARSE_OPT_NONEG,
			    header_callback },
		{ OPTION_CALLBACK, 0, "cc", NULL, "email", "add Cc: header",
			    PARSE_OPT_NONEG, cc_callback },
		OPT_STRING(0, "in-reply-to", &in_reply_to, "message-id",
			    "make first mail a reply to <message-id>"),
		{ OPTION_CALLBACK, 0, "attach", &rev, "boundary",
			    "attach the patch", PARSE_OPT_OPTARG,
			    attach_callback },
		{ OPTION_CALLBACK, 0, "inline", &rev, "boundary",
			    "inline the patch",
			    PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
			    inline_callback },
		{ OPTION_CALLBACK, 0, "thread", &thread, "style",
			    "enable message threading, styles: shallow, deep",
			    PARSE_OPT_OPTARG, thread_callback },
		OPT_END()
	};
917

918
	git_config(git_format_config, NULL);
919
	init_revisions(&rev, prefix);
920 921 922 923 924
	rev.commit_format = CMIT_FMT_EMAIL;
	rev.verbose_header = 1;
	rev.diff = 1;
	rev.combine_merges = 0;
	rev.ignore_merges = 1;
925
	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
926

927
	rev.subject_prefix = fmt_patch_subject_prefix;
928

929 930 931 932 933
	if (default_attach) {
		rev.mime_boundary = default_attach;
		rev.no_inline = 1;
	}

934 935
	/*
	 * Parse the arguments before setup_revisions(), or something
936
	 * like "git format-patch -o a123 HEAD^.." may fail; a123 is
937 938
	 * possibly a valid SHA1.
	 */
939
	argc = parse_options(argc, argv, prefix, builtin_format_patch_options,
940 941
			     builtin_format_patch_usage,
			     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);
942

943 944 945 946 947 948 949 950 951 952
	if (do_signoff) {
		const char *committer;
		const char *endpos;
		committer = git_committer_info(IDENT_ERROR_ON_NO_NAME);
		endpos = strchr(committer, '>');
		if (!endpos)
			die("bogus committer info %s", committer);
		add_signoff = xmemdupz(committer, endpos - committer + 1);
	}

D
Daniel Barkalow 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	for (i = 0; i < extra_hdr_nr; i++) {
		strbuf_addstr(&buf, extra_hdr[i]);
		strbuf_addch(&buf, '\n');
	}

	if (extra_to_nr)
		strbuf_addstr(&buf, "To: ");
	for (i = 0; i < extra_to_nr; i++) {
		if (i)
			strbuf_addstr(&buf, "    ");
		strbuf_addstr(&buf, extra_to[i]);
		if (i + 1 < extra_to_nr)
			strbuf_addch(&buf, ',');
		strbuf_addch(&buf, '\n');
	}

	if (extra_cc_nr)
		strbuf_addstr(&buf, "Cc: ");
	for (i = 0; i < extra_cc_nr; i++) {
		if (i)
			strbuf_addstr(&buf, "    ");
		strbuf_addstr(&buf, extra_cc[i]);
		if (i + 1 < extra_cc_nr)
			strbuf_addch(&buf, ',');
		strbuf_addch(&buf, '\n');
	}

	rev.extra_headers = strbuf_detach(&buf, 0);

982
	if (start_number < 0)
983
		start_number = 1;
984 985 986 987 988 989 990 991 992

	/*
	 * If numbered is set solely due to format.numbered in config,
	 * and it would conflict with --keep-subject (-k) from the
	 * command line, reset "numbered".
	 */
	if (numbered && keep_subject && !numbered_cmdline_opt)
		numbered = 0;

993
	if (numbered && keep_subject)
994
		die ("-n and -k are mutually exclusive.");
995 996
	if (keep_subject && subject_prefix)
		die ("--subject-prefix and -k are mutually exclusive.");
997

998 999 1000
	argc = setup_revisions(argc, argv, &rev, "HEAD");
	if (argc > 1)
		die ("unrecognized argument: %s", argv[1]);
1001

1002 1003
	if (!rev.diffopt.output_format
		|| rev.diffopt.output_format == DIFF_FORMAT_PATCH)
1004
		rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_PATCH;
1005

1006
	if (!DIFF_OPT_TST(&rev.diffopt, TEXT) && !no_binary_diff)
1007
		DIFF_OPT_SET(&rev.diffopt, BINARY);
1008

1009 1010
	if (!use_stdout)
		output_directory = set_outdir(prefix, output_directory);
1011

1012 1013 1014 1015 1016 1017 1018 1019
	if (output_directory) {
		if (use_stdout)
			die("standard output, or directory, which one?");
		if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
			die("Could not create directory %s",
			    output_directory);
	}

1020
	if (rev.pending.nr == 1) {
1021 1022 1023 1024 1025 1026
		if (rev.max_count < 0 && !rev.show_root_diff) {
			/*
			 * This is traditional behaviour of "git format-patch
			 * origin" that prepares what the origin side still
			 * does not have.
			 */
J
Junio C Hamano 已提交
1027
			rev.pending.objects[0].item->flags |= UNINTERESTING;
1028
			add_head_to_pending(&rev);
J
Junio C Hamano 已提交
1029
		}
1030 1031 1032 1033
		/*
		 * Otherwise, it is "format-patch -22 HEAD", and/or
		 * "format-patch --root HEAD".  The user wants
		 * get_revision() to do the usual traversal.
J
Junio C Hamano 已提交
1034
		 */
1035
	}
1036 1037 1038 1039 1040 1041 1042

	/*
	 * We cannot move this anywhere earlier because we do want to
	 * know if --root was given explicitly from the comand line.
	 */
	rev.show_root_diff = 1;

1043 1044 1045 1046 1047
	if (cover_letter) {
		/* remember the range */
		int i;
		for (i = 0; i < rev.pending.nr; i++) {
			struct object *o = rev.pending.objects[i].item;
1048
			if (!(o->flags & UNINTERESTING))
1049 1050 1051 1052 1053 1054
				head = (struct commit *)o;
		}
		/* We can't generate a cover letter without any patches */
		if (!head)
			return 0;
	}
1055

1056
	if (ignore_if_in_upstream)
1057
		get_patch_ids(&rev, &ids, prefix);
1058

1059
	if (!use_stdout)
1060
		realstdout = xfdopen(xdup(1), "w");
1061

1062 1063
	if (prepare_revision_walk(&rev))
		die("revision walk setup failed");
1064
	rev.boundary = 1;
1065
	while ((commit = get_revision(&rev)) != NULL) {
1066 1067 1068 1069 1070 1071
		if (commit->object.flags & BOUNDARY) {
			boundary_count++;
			origin = (boundary_count == 1) ? commit : NULL;
			continue;
		}

1072 1073 1074
		/* ignore merges */
		if (commit->parents && commit->parents->next)
			continue;
1075 1076

		if (ignore_if_in_upstream &&
1077
				has_commit_patch_id(commit, &ids))
1078 1079
			continue;

1080
		nr++;
J
Jonas Fonseca 已提交
1081
		list = xrealloc(list, nr * sizeof(list[0]));
1082 1083
		list[nr - 1] = commit;
	}
1084
	total = nr;
1085 1086
	if (!keep_subject && auto_number && total > 1)
		numbered = 1;
1087
	if (numbered)
1088
		rev.total = total + start_number - 1;
1089 1090 1091 1092 1093 1094
	if (in_reply_to || thread || cover_letter)
		rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
	if (in_reply_to) {
		const char *msgid = clean_message_id(in_reply_to);
		string_list_append(msgid, rev.ref_message_ids);
	}
1095 1096
	rev.numbered_files = numbered_files;
	rev.patch_suffix = fmt_patch_suffix;
1097 1098 1099 1100
	if (cover_letter) {
		if (thread)
			gen_message_id(&rev, "cover");
		make_cover_letter(&rev, use_stdout, numbered, numbered_files,
1101
				  origin, nr, list, head);
1102 1103 1104 1105
		total++;
		start_number--;
	}
	rev.add_signoff = add_signoff;
1106 1107 1108
	while (0 <= --nr) {
		int shown;
		commit = list[nr];
1109
		rev.nr = total - nr + (start_number - 1);
1110
		/* Make the second and subsequent mails replies to the first */
1111
		if (thread) {
1112
			/* Have we already had a message ID? */
1113
			if (rev.message_id) {
1114
				/*
1115 1116 1117 1118 1119 1120
				 * For deep threading: make every mail
				 * a reply to the previous one, no
				 * matter what other options are set.
				 *
				 * For shallow threading:
				 *
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
				 * Without --cover-letter and
				 * --in-reply-to, make every mail a
				 * reply to the one before.
				 *
				 * With --in-reply-to but no
				 * --cover-letter, make every mail a
				 * reply to the <reply-to>.
				 *
				 * With --cover-letter, make every
				 * mail but the cover letter a reply
				 * to the cover letter.  The cover
				 * letter is a reply to the
				 * --in-reply-to, if specified.
1134
				 */
1135 1136
				if (thread == THREAD_SHALLOW
				    && rev.ref_message_ids->nr > 0
1137
				    && (!cover_letter || rev.nr > 1))
1138 1139
					free(rev.message_id);
				else
1140 1141
					string_list_append(rev.message_id,
							   rev.ref_message_ids);
1142
			}
1143
			gen_message_id(&rev, sha1_to_hex(commit->object.sha1));
1144
		}
1145

1146 1147
		if (!use_stdout && reopen_stdout(numbered_files ? NULL : commit,
						 &rev))
1148
			die("Failed to create output files");
1149 1150 1151
		shown = log_tree_commit(&rev, commit);
		free(commit->buffer);
		commit->buffer = NULL;
1152 1153 1154 1155 1156 1157 1158 1159 1160

		/* We put one extra blank line between formatted
		 * patches and this flag is used by log-tree code
		 * to see if it needs to emit a LF before showing
		 * the log; when using one file per patch, we do
		 * not want the extra blank line.
		 */
		if (!use_stdout)
			rev.shown_one = 0;
1161 1162 1163 1164 1165 1166 1167 1168
		if (shown) {
			if (rev.mime_boundary)
				printf("\n--%s%s--\n\n\n",
				       mime_boundary_leader,
				       rev.mime_boundary);
			else
				printf("-- \n%s\n\n", git_version_string);
		}
1169 1170
		if (!use_stdout)
			fclose(stdout);
1171 1172
	}
	free(list);
1173 1174
	if (ignore_if_in_upstream)
		free_patch_ids(&ids);
1175 1176 1177
	return 0;
}

R
Rene Scharfe 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
static int add_pending_commit(const char *arg, struct rev_info *revs, int flags)
{
	unsigned char sha1[20];
	if (get_sha1(arg, sha1) == 0) {
		struct commit *commit = lookup_commit_reference(sha1);
		if (commit) {
			commit->object.flags |= flags;
			add_pending_object(revs, &commit->object, arg);
			return 0;
		}
	}
	return -1;
}

static const char cherry_usage[] =
1193
"git cherry [-v] [<upstream> [<head> [<limit>]]]";
R
Rene Scharfe 已提交
1194 1195 1196
int cmd_cherry(int argc, const char **argv, const char *prefix)
{
	struct rev_info revs;
1197
	struct patch_ids ids;
R
Rene Scharfe 已提交
1198 1199
	struct commit *commit;
	struct commit_list *list = NULL;
1200
	struct branch *current_branch;
R
Rene Scharfe 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	const char *upstream;
	const char *head = "HEAD";
	const char *limit = NULL;
	int verbose = 0;

	if (argc > 1 && !strcmp(argv[1], "-v")) {
		verbose = 1;
		argc--;
		argv++;
	}

	switch (argc) {
	case 4:
		limit = argv[3];
		/* FALLTHROUGH */
	case 3:
		head = argv[2];
		/* FALLTHROUGH */
	case 2:
		upstream = argv[1];
		break;
	default:
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		current_branch = branch_get(NULL);
		if (!current_branch || !current_branch->merge
					|| !current_branch->merge[0]
					|| !current_branch->merge[0]->dst) {
			fprintf(stderr, "Could not find a tracked"
					" remote branch, please"
					" specify <upstream> manually.\n");
			usage(cherry_usage);
		}

		upstream = current_branch->merge[0]->dst;
R
Rene Scharfe 已提交
1234 1235 1236 1237 1238 1239
	}

	init_revisions(&revs, prefix);
	revs.diff = 1;
	revs.combine_merges = 0;
	revs.ignore_merges = 1;
1240
	DIFF_OPT_SET(&revs.diffopt, RECURSIVE);
R
Rene Scharfe 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253

	if (add_pending_commit(head, &revs, 0))
		die("Unknown commit %s", head);
	if (add_pending_commit(upstream, &revs, UNINTERESTING))
		die("Unknown commit %s", upstream);

	/* Don't say anything if head and upstream are the same. */
	if (revs.pending.nr == 2) {
		struct object_array_entry *o = revs.pending.objects;
		if (hashcmp(o[0].item->sha1, o[1].item->sha1) == 0)
			return 0;
	}

1254
	get_patch_ids(&revs, &ids, prefix);
R
Rene Scharfe 已提交
1255 1256 1257 1258 1259

	if (limit && add_pending_commit(limit, &revs, UNINTERESTING))
		die("Unknown commit %s", limit);

	/* reverse the list of commits */
1260 1261
	if (prepare_revision_walk(&revs))
		die("revision walk setup failed");
R
Rene Scharfe 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	while ((commit = get_revision(&revs)) != NULL) {
		/* ignore merges */
		if (commit->parents && commit->parents->next)
			continue;

		commit_list_insert(commit, &list);
	}

	while (list) {
		char sign = '+';

		commit = list->item;
1274
		if (has_commit_patch_id(commit, &ids))
R
Rene Scharfe 已提交
1275 1276 1277
			sign = '-';

		if (verbose) {
1278
			struct strbuf buf = STRBUF_INIT;
1279
			pretty_print_commit(CMIT_FMT_ONELINE, commit,
1280
			                    &buf, 0, NULL, NULL, 0, 0);
R
Rene Scharfe 已提交
1281
			printf("%c %s %s\n", sign,
1282 1283
			       sha1_to_hex(commit->object.sha1), buf.buf);
			strbuf_release(&buf);
R
Rene Scharfe 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292
		}
		else {
			printf("%c %s\n", sign,
			       sha1_to_hex(commit->object.sha1));
		}

		list = list->next;
	}

1293
	free_patch_ids(&ids);
R
Rene Scharfe 已提交
1294 1295
	return 0;
}