wt-status.c 64.6 KB
Newer Older
1
#include "cache.h"
2 3 4 5 6 7 8
#include "wt-status.h"
#include "object.h"
#include "dir.h"
#include "commit.h"
#include "diff.h"
#include "revision.h"
#include "diffcore.h"
9
#include "quote.h"
10
#include "run-command.h"
11
#include "argv-array.h"
12
#include "remote.h"
13
#include "refs.h"
14
#include "submodule.h"
Duy Nguyen's avatar
Duy Nguyen committed
15
#include "column.h"
16
#include "strbuf.h"
17
#include "utf8.h"
18
#include "worktree.h"
19
#include "lockfile.h"
20

21
static const char cut_line[] =
22 23
"------------------------ >8 ------------------------\n";

24
static char default_wt_status_colors[][COLOR_MAXLEN] = {
25 26 27 28 29
	GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
	GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
	GIT_COLOR_RED,    /* WT_STATUS_CHANGED */
	GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
	GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
30
	GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
31 32
	GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
	GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
33
	GIT_COLOR_NIL,    /* WT_STATUS_ONBRANCH */
34
};
35

36
static const char *color(int slot, struct wt_status *s)
37
{
38 39 40
	const char *c = "";
	if (want_color(s->use_color))
		c = s->color_palette[slot];
41 42 43
	if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
		c = s->color_palette[WT_STATUS_HEADER];
	return c;
44 45
}

46 47 48 49 50 51 52 53 54
static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
		const char *fmt, va_list ap, const char *trail)
{
	struct strbuf sb = STRBUF_INIT;
	struct strbuf linebuf = STRBUF_INIT;
	const char *line, *eol;

	strbuf_vaddf(&sb, fmt, ap);
	if (!sb.len) {
55 56 57 58 59
		if (s->display_comment_prefix) {
			strbuf_addch(&sb, comment_line_char);
			if (!trail)
				strbuf_addch(&sb, ' ');
		}
60 61 62 63 64 65 66 67 68 69
		color_print_strbuf(s->fp, color, &sb);
		if (trail)
			fprintf(s->fp, "%s", trail);
		strbuf_release(&sb);
		return;
	}
	for (line = sb.buf; *line; line = eol + 1) {
		eol = strchr(line, '\n');

		strbuf_reset(&linebuf);
70
		if (at_bol && s->display_comment_prefix) {
71
			strbuf_addch(&linebuf, comment_line_char);
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
			if (*line != '\n' && *line != '\t')
				strbuf_addch(&linebuf, ' ');
		}
		if (eol)
			strbuf_add(&linebuf, line, eol - line);
		else
			strbuf_addstr(&linebuf, line);
		color_print_strbuf(s->fp, color, &linebuf);
		if (eol)
			fprintf(s->fp, "\n");
		else
			break;
		at_bol = 1;
	}
	if (trail)
		fprintf(s->fp, "%s", trail);
	strbuf_release(&linebuf);
	strbuf_release(&sb);
}

void status_printf_ln(struct wt_status *s, const char *color,
			const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	status_vprintf(s, 1, color, fmt, ap, "\n");
	va_end(ap);
}

void status_printf(struct wt_status *s, const char *color,
			const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	status_vprintf(s, 1, color, fmt, ap, NULL);
	va_end(ap);
}

112 113
static void status_printf_more(struct wt_status *s, const char *color,
			       const char *fmt, ...)
114 115 116 117 118 119 120 121
{
	va_list ap;

	va_start(ap, fmt);
	status_vprintf(s, 0, color, fmt, ap, NULL);
	va_end(ap);
}

122 123
void wt_status_prepare(struct wt_status *s)
{
124
	memset(s, 0, sizeof(*s));
125 126
	memcpy(s->color_palette, default_wt_status_colors,
	       sizeof(default_wt_status_colors));
127 128 129
	s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
	s->use_color = -1;
	s->relative_paths = 1;
130
	s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
131
	s->reference = "HEAD";
132
	s->fp = stdout;
133
	s->index_file = get_index_file();
134
	s->change.strdup_strings = 1;
135
	s->untracked.strdup_strings = 1;
136
	s->ignored.strdup_strings = 1;
137
	s->show_branch = -1;  /* unspecified */
138
	s->show_stash = 0;
139
	s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
140
	s->display_comment_prefix = 0;
141 142
}

143
static void wt_longstatus_print_unmerged_header(struct wt_status *s)
144
{
145 146 147 148
	int i;
	int del_mod_conflict = 0;
	int both_deleted = 0;
	int not_deleted = 0;
149
	const char *c = color(WT_STATUS_HEADER, s);
150

151
	status_printf_ln(s, c, _("Unmerged paths:"));
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	for (i = 0; i < s->change.nr; i++) {
		struct string_list_item *it = &(s->change.items[i]);
		struct wt_status_change_data *d = it->util;

		switch (d->stagemask) {
		case 0:
			break;
		case 1:
			both_deleted = 1;
			break;
		case 3:
		case 5:
			del_mod_conflict = 1;
			break;
		default:
			not_deleted = 1;
			break;
		}
	}

173
	if (!s->hints)
174
		return;
175
	if (s->whence != FROM_COMMIT)
176 177
		;
	else if (!s->is_initial)
178
		status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
179
	else
180
		status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
181 182 183 184 185 186 187 188 189 190 191

	if (!both_deleted) {
		if (!del_mod_conflict)
			status_printf_ln(s, c, _("  (use \"git add <file>...\" to mark resolution)"));
		else
			status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
	} else if (!del_mod_conflict && !not_deleted) {
		status_printf_ln(s, c, _("  (use \"git rm <file>...\" to mark resolution)"));
	} else {
		status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
	}
192
	status_printf_ln(s, c, "%s", "");
193 194
}

195
static void wt_longstatus_print_cached_header(struct wt_status *s)
196
{
197
	const char *c = color(WT_STATUS_HEADER, s);
198

199
	status_printf_ln(s, c, _("Changes to be committed:"));
200
	if (!s->hints)
201
		return;
202
	if (s->whence != FROM_COMMIT)
203 204
		; /* NEEDSWORK: use "git reset --unresolve"??? */
	else if (!s->is_initial)
205
		status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
206
	else
207
		status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
208
	status_printf_ln(s, c, "%s", "");
209 210
}

211 212 213
static void wt_longstatus_print_dirty_header(struct wt_status *s,
					     int has_deleted,
					     int has_dirty_submodules)
214
{
215
	const char *c = color(WT_STATUS_HEADER, s);
216

217
	status_printf_ln(s, c, _("Changes not staged for commit:"));
218
	if (!s->hints)
219
		return;
220
	if (!has_deleted)
221
		status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
222
	else
223 224
		status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" to update what will be committed)"));
	status_printf_ln(s, c, _("  (use \"git checkout -- <file>...\" to discard changes in working directory)"));
225
	if (has_dirty_submodules)
226
		status_printf_ln(s, c, _("  (commit or discard the untracked or modified content in submodules)"));
227
	status_printf_ln(s, c, "%s", "");
228 229
}

230 231 232
static void wt_longstatus_print_other_header(struct wt_status *s,
					     const char *what,
					     const char *how)
233
{
234
	const char *c = color(WT_STATUS_HEADER, s);
Duy Nguyen's avatar
Duy Nguyen committed
235
	status_printf_ln(s, c, "%s:", what);
236
	if (!s->hints)
237
		return;
238
	status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
239
	status_printf_ln(s, c, "%s", "");
240 241
}

242
static void wt_longstatus_print_trailer(struct wt_status *s)
243
{
244
	status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
245 246
}

247
#define quote_path quote_path_relative
248

249
static const char *wt_status_unmerged_status_string(int stagemask)
250
{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	switch (stagemask) {
	case 1:
		return _("both deleted:");
	case 2:
		return _("added by us:");
	case 3:
		return _("deleted by them:");
	case 4:
		return _("added by them:");
	case 5:
		return _("deleted by us:");
	case 6:
		return _("both added:");
	case 7:
		return _("both modified:");
	default:
267
		die("BUG: unhandled unmerged status %x", stagemask);
268 269 270
	}
}

271 272 273 274
static const char *wt_status_diff_status_string(int status)
{
	switch (status) {
	case DIFF_STATUS_ADDED:
275
		return _("new file:");
276
	case DIFF_STATUS_COPIED:
277
		return _("copied:");
278
	case DIFF_STATUS_DELETED:
279
		return _("deleted:");
280
	case DIFF_STATUS_MODIFIED:
281
		return _("modified:");
282
	case DIFF_STATUS_RENAMED:
283
		return _("renamed:");
284
	case DIFF_STATUS_TYPE_CHANGED:
285
		return _("typechange:");
286
	case DIFF_STATUS_UNKNOWN:
287
		return _("unknown:");
288
	case DIFF_STATUS_UNMERGED:
289
		return _("unmerged:");
290 291 292 293 294
	default:
		return NULL;
	}
}

295 296 297 298 299 300 301 302 303 304 305 306 307
static int maxwidth(const char *(*label)(int), int minval, int maxval)
{
	int result = 0, i;

	for (i = minval; i <= maxval; i++) {
		const char *s = label(i);
		int len = s ? utf8_strwidth(s) : 0;
		if (len > result)
			result = len;
	}
	return result;
}

308 309
static void wt_longstatus_print_unmerged_data(struct wt_status *s,
					      struct string_list_item *it)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
	const char *c = color(WT_STATUS_UNMERGED, s);
	struct wt_status_change_data *d = it->util;
	struct strbuf onebuf = STRBUF_INIT;
	static char *padding;
	static int label_width;
	const char *one, *how;
	int len;

	if (!padding) {
		label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
		label_width += strlen(" ");
		padding = xmallocz(label_width);
		memset(padding, ' ', label_width);
	}

	one = quote_path(it->string, s->prefix, &onebuf);
	status_printf(s, color(WT_STATUS_HEADER, s), "\t");

	how = wt_status_unmerged_status_string(d->stagemask);
	len = label_width - utf8_strwidth(how);
	status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
	strbuf_release(&onebuf);
}

335 336 337
static void wt_longstatus_print_change_data(struct wt_status *s,
					    int change_type,
					    struct string_list_item *it)
338
{
339
	struct wt_status_change_data *d = it->util;
340
	const char *c = color(change_type, s);
341
	int status;
342 343
	char *one_name;
	char *two_name;
344
	const char *one, *two;
345
	struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
346
	struct strbuf extra = STRBUF_INIT;
347
	static char *padding;
348
	static int label_width;
349 350 351 352
	const char *what;
	int len;

	if (!padding) {
353 354
		/* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
		label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
355 356 357
		label_width += strlen(" ");
		padding = xmallocz(label_width);
		memset(padding, ' ', label_width);
358
	}
359

360 361 362 363 364 365
	one_name = two_name = it->string;
	switch (change_type) {
	case WT_STATUS_UPDATED:
		status = d->index_status;
		break;
	case WT_STATUS_CHANGED:
366 367 368
		if (d->new_submodule_commits || d->dirty_submodule) {
			strbuf_addstr(&extra, " (");
			if (d->new_submodule_commits)
369
				strbuf_addstr(&extra, _("new commits, "));
370
			if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
371
				strbuf_addstr(&extra, _("modified content, "));
372
			if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
373
				strbuf_addstr(&extra, _("untracked content, "));
374 375 376
			strbuf_setlen(&extra, extra.len - 2);
			strbuf_addch(&extra, ')');
		}
377 378
		status = d->worktree_status;
		break;
379
	default:
380
		die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
381
		    change_type);
382 383
	}

384 385 386 387 388 389 390 391
	/*
	 * Only pick up the rename it's relevant. If the rename is for
	 * the changed section and we're printing the updated section,
	 * ignore it.
	 */
	if (d->rename_status == status)
		one_name = d->rename_source;

392 393
	one = quote_path(one_name, s->prefix, &onebuf);
	two = quote_path(two_name, s->prefix, &twobuf);
394

395
	status_printf(s, color(WT_STATUS_HEADER, s), "\t");
396 397
	what = wt_status_diff_status_string(status);
	if (!what)
398
		die("BUG: unhandled diff status %c", status);
399
	len = label_width - utf8_strwidth(what);
400
	assert(len >= 0);
401
	if (one_name != two_name)
402
		status_printf_more(s, c, "%s%.*s%s -> %s",
403 404
				   what, len, padding, one, two);
	else
405
		status_printf_more(s, c, "%s%.*s%s",
406
				   what, len, padding, one);
407
	if (extra.len) {
408
		status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
409 410
		strbuf_release(&extra);
	}
411
	status_printf_more(s, GIT_COLOR_NORMAL, "\n");
412 413
	strbuf_release(&onebuf);
	strbuf_release(&twobuf);
414 415
}

416 417
static char short_submodule_status(struct wt_status_change_data *d)
{
418 419 420 421 422 423 424 425 426
	if (d->new_submodule_commits)
		return 'M';
	if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
		return 'm';
	if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
		return '?';
	return d->worktree_status;
}

427 428 429
static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
					 struct diff_options *options,
					 void *data)
430 431 432
{
	struct wt_status *s = data;
	int i;
433 434 435 436

	if (!q->nr)
		return;
	s->workdir_dirty = 1;
437
	for (i = 0; i < q->nr; i++) {
438 439 440 441 442
		struct diff_filepair *p;
		struct string_list_item *it;
		struct wt_status_change_data *d;

		p = q->queue[i];
443
		it = string_list_insert(&s->change, p->two->path);
444 445 446 447
		d = it->util;
		if (!d) {
			d = xcalloc(1, sizeof(*d));
			it->util = d;
448
		}
449 450
		if (!d->worktree_status)
			d->worktree_status = p->status;
451 452
		if (S_ISGITLINK(p->two->mode)) {
			d->dirty_submodule = p->two->dirty_submodule;
453 454
			d->new_submodule_commits = !!oidcmp(&p->one->oid,
							    &p->two->oid);
455 456 457
			if (s->status_format == STATUS_FORMAT_SHORT)
				d->worktree_status = short_submodule_status(d);
		}
458 459 460

		switch (p->status) {
		case DIFF_STATUS_ADDED:
461
			d->mode_worktree = p->two->mode;
462 463 464 465 466 467 468 469
			break;

		case DIFF_STATUS_DELETED:
			d->mode_index = p->one->mode;
			oidcpy(&d->oid_index, &p->one->oid);
			/* mode_worktree is zero for a delete. */
			break;

470 471 472 473 474 475 476 477
		case DIFF_STATUS_COPIED:
		case DIFF_STATUS_RENAMED:
			if (d->rename_status)
				die("BUG: multiple renames on the same target? how?");
			d->rename_source = xstrdup(p->one->path);
			d->rename_score = p->score * 100 / MAX_SCORE;
			d->rename_status = p->status;
			/* fallthru */
478 479 480 481 482 483 484 485
		case DIFF_STATUS_MODIFIED:
		case DIFF_STATUS_TYPE_CHANGED:
		case DIFF_STATUS_UNMERGED:
			d->mode_index = p->one->mode;
			d->mode_worktree = p->two->mode;
			oidcpy(&d->oid_index, &p->one->oid);
			break;

486 487
		default:
			die("BUG: unhandled diff-files status '%c'", p->status);
488 489 490
			break;
		}

491 492 493
	}
}

494 495 496
static int unmerged_mask(const char *path)
{
	int pos, mask;
497
	const struct cache_entry *ce;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

	pos = cache_name_pos(path, strlen(path));
	if (0 <= pos)
		return 0;

	mask = 0;
	pos = -pos-1;
	while (pos < active_nr) {
		ce = active_cache[pos++];
		if (strcmp(ce->name, path) || !ce_stage(ce))
			break;
		mask |= (1 << (ce_stage(ce) - 1));
	}
	return mask;
}

514 515 516
static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
					 struct diff_options *options,
					 void *data)
517
{
518
	struct wt_status *s = data;
519
	int i;
520 521 522 523 524 525 526

	for (i = 0; i < q->nr; i++) {
		struct diff_filepair *p;
		struct string_list_item *it;
		struct wt_status_change_data *d;

		p = q->queue[i];
527
		it = string_list_insert(&s->change, p->two->path);
528 529 530 531 532 533 534 535
		d = it->util;
		if (!d) {
			d = xcalloc(1, sizeof(*d));
			it->util = d;
		}
		if (!d->index_status)
			d->index_status = p->status;
		switch (p->status) {
536 537 538 539 540 541 542 543 544 545 546
		case DIFF_STATUS_ADDED:
			/* Leave {mode,oid}_head zero for an add. */
			d->mode_index = p->two->mode;
			oidcpy(&d->oid_index, &p->two->oid);
			break;
		case DIFF_STATUS_DELETED:
			d->mode_head = p->one->mode;
			oidcpy(&d->oid_head, &p->one->oid);
			/* Leave {mode,oid}_index zero for a delete. */
			break;

547 548
		case DIFF_STATUS_COPIED:
		case DIFF_STATUS_RENAMED:
549 550
			if (d->rename_status)
				die("BUG: multiple renames on the same target? how?");
551 552 553
			d->rename_source = xstrdup(p->one->path);
			d->rename_score = p->score * 100 / MAX_SCORE;
			d->rename_status = p->status;
554 555 556 557 558 559 560
			/* fallthru */
		case DIFF_STATUS_MODIFIED:
		case DIFF_STATUS_TYPE_CHANGED:
			d->mode_head = p->one->mode;
			d->mode_index = p->two->mode;
			oidcpy(&d->oid_head, &p->one->oid);
			oidcpy(&d->oid_index, &p->two->oid);
561
			break;
562 563
		case DIFF_STATUS_UNMERGED:
			d->stagemask = unmerged_mask(p->two->path);
564 565 566 567 568
			/*
			 * Don't bother setting {mode,oid}_{head,index} since the print
			 * code will output the stage values directly and not use the
			 * values in these fields.
			 */
569
			break;
570 571 572 573

		default:
			die("BUG: unhandled diff-index status '%c'", p->status);
			break;
574
		}
575
	}
576 577
}

578
static void wt_status_collect_changes_worktree(struct wt_status *s)
579 580
{
	struct rev_info rev;
581 582 583 584

	init_revisions(&rev, NULL);
	setup_revisions(0, NULL, &rev, NULL);
	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
585
	rev.diffopt.flags.dirty_submodules = 1;
586
	rev.diffopt.ita_invisible_in_index = 1;
587
	if (!s->show_untracked_files)
588
		rev.diffopt.flags.ignore_untracked_in_submodules = 1;
589
	if (s->ignore_submodule_arg) {
590
		rev.diffopt.flags.override_submodule_config = 1;
591
		handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
592
	}
593 594
	rev.diffopt.format_callback = wt_status_collect_changed_cb;
	rev.diffopt.format_callback_data = s;
595
	copy_pathspec(&rev.prune_data, &s->pathspec);
596 597 598 599 600 601
	run_diff_files(&rev, 0);
}

static void wt_status_collect_changes_index(struct wt_status *s)
{
	struct rev_info rev;
602
	struct setup_revision_opt opt;
603

604
	init_revisions(&rev, NULL);
605 606 607 608
	memset(&opt, 0, sizeof(opt));
	opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
	setup_revisions(0, NULL, &rev, &opt);

609
	rev.diffopt.flags.override_submodule_config = 1;
610
	rev.diffopt.ita_invisible_in_index = 1;
611
	if (s->ignore_submodule_arg) {
612
		handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
613 614 615 616 617 618 619 620 621 622
	} else {
		/*
		 * Unless the user did explicitly request a submodule ignore
		 * mode by passing a command line option we do not ignore any
		 * changed submodule SHA-1s when comparing index and HEAD, no
		 * matter what is configured. Otherwise the user won't be
		 * shown any submodules she manually added (and which are
		 * staged to be committed), which would be really confusing.
		 */
		handle_ignore_submodules_arg(&rev.diffopt, "dirty");
623
	}
624

625
	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
626
	rev.diffopt.format_callback = wt_status_collect_updated_cb;
627
	rev.diffopt.format_callback_data = s;
628
	rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
Jeff King's avatar
Jeff King committed
629
	rev.diffopt.rename_limit = 200;
630
	rev.diffopt.break_opt = 0;
631
	copy_pathspec(&rev.prune_data, &s->pathspec);
632 633 634
	run_diff_index(&rev, 1);
}

635 636 637 638 639 640 641
static void wt_status_collect_changes_initial(struct wt_status *s)
{
	int i;

	for (i = 0; i < active_nr; i++) {
		struct string_list_item *it;
		struct wt_status_change_data *d;
642
		const struct cache_entry *ce = active_cache[i];
643

644
		if (!ce_path_match(ce, &s->pathspec, NULL))
645
			continue;
646 647
		if (ce_intent_to_add(ce))
			continue;
648
		it = string_list_insert(&s->change, ce->name);
649 650 651 652 653
		d = it->util;
		if (!d) {
			d = xcalloc(1, sizeof(*d));
			it->util = d;
		}
654
		if (ce_stage(ce)) {
655
			d->index_status = DIFF_STATUS_UNMERGED;
656
			d->stagemask |= (1 << (ce_stage(ce) - 1));
657 658 659 660 661 662
			/*
			 * Don't bother setting {mode,oid}_{head,index} since the print
			 * code will output the stage values directly and not use the
			 * values in these fields.
			 */
		} else {
663
			d->index_status = DIFF_STATUS_ADDED;
664 665
			/* Leave {mode,oid}_head zero for adds. */
			d->mode_index = ce->ce_mode;
666
			oidcpy(&d->oid_index, &ce->oid);
667
		}
668 669 670
	}
}

671 672 673 674
static void wt_status_collect_untracked(struct wt_status *s)
{
	int i;
	struct dir_struct dir;
675
	uint64_t t_begin = getnanotime();
676 677 678

	if (!s->show_untracked_files)
		return;
679

680 681 682 683
	memset(&dir, 0, sizeof(dir));
	if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
		dir.flags |=
			DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
684
	if (s->show_ignored_mode) {
685
		dir.flags |= DIR_SHOW_IGNORED_TOO;
686 687 688 689

		if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
			dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
	} else {
690
		dir.untracked = the_index.untracked;
691 692
	}

693 694
	setup_standard_excludes(&dir);

695
	fill_directory(&dir, &the_index, &s->pathspec);
696

697
	for (i = 0; i < dir.nr; i++) {
698
		struct dir_entry *ent = dir.entries[i];
699
		if (cache_name_is_other(ent->name, ent->len) &&
700
		    dir_path_match(ent, &s->pathspec, 0, NULL))
701
			string_list_insert(&s->untracked, ent->name);
702
		free(ent);
703
	}
704

705 706 707
	for (i = 0; i < dir.ignored_nr; i++) {
		struct dir_entry *ent = dir.ignored[i];
		if (cache_name_is_other(ent->name, ent->len) &&
708
		    dir_path_match(ent, &s->pathspec, 0, NULL))
709 710
			string_list_insert(&s->ignored, ent->name);
		free(ent);
711 712
	}

713
	free(dir.entries);
714 715
	free(dir.ignored);
	clear_directory(&dir);
716

717 718
	if (advice_status_u_option)
		s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
719 720 721
}

void wt_status_collect(struct wt_status *s)
722 723 724 725 726 727 728
{
	wt_status_collect_changes_worktree(s);

	if (s->is_initial)
		wt_status_collect_changes_initial(s);
	else
		wt_status_collect_changes_index(s);
729
	wt_status_collect_untracked(s);
730 731
}

732
static void wt_longstatus_print_unmerged(struct wt_status *s)
733 734 735 736 737 738 739 740 741 742 743 744
{
	int shown_header = 0;
	int i;

	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		struct string_list_item *it;
		it = &(s->change.items[i]);
		d = it->util;
		if (!d->stagemask)
			continue;
		if (!shown_header) {
745
			wt_longstatus_print_unmerged_header(s);
746 747
			shown_header = 1;
		}
748
		wt_longstatus_print_unmerged_data(s, it);
749 750
	}
	if (shown_header)
751
		wt_longstatus_print_trailer(s);
752 753 754

}

755
static void wt_longstatus_print_updated(struct wt_status *s)
756 757 758 759 760 761 762 763 764 765 766 767 768
{
	int shown_header = 0;
	int i;

	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		struct string_list_item *it;
		it = &(s->change.items[i]);
		d = it->util;
		if (!d->index_status ||
		    d->index_status == DIFF_STATUS_UNMERGED)
			continue;
		if (!shown_header) {
769
			wt_longstatus_print_cached_header(s);
770 771 772
			s->commitable = 1;
			shown_header = 1;
		}
773
		wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
774 775
	}
	if (shown_header)
776
		wt_longstatus_print_trailer(s);
777 778 779 780 781 782 783
}

/*
 * -1 : has delete
 *  0 : no change
 *  1 : some change but no delete
 */
784 785
static int wt_status_check_worktree_changes(struct wt_status *s,
					     int *dirty_submodules)
786 787 788 789
{
	int i;
	int changes = 0;

790 791
	*dirty_submodules = 0;

792 793 794
	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		d = s->change.items[i].util;
795 796
		if (!d->worktree_status ||
		    d->worktree_status == DIFF_STATUS_UNMERGED)
797
			continue;
798 799 800 801
		if (!changes)
			changes = 1;
		if (d->dirty_submodule)
			*dirty_submodules = 1;
802
		if (d->worktree_status == DIFF_STATUS_DELETED)
803
			changes = -1;
804 805 806 807
	}
	return changes;
}

808
static void wt_longstatus_print_changed(struct wt_status *s)
809
{
810 811
	int i, dirty_submodules;
	int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
812 813 814 815

	if (!worktree_changes)
		return;

816
	wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
817 818 819 820 821 822

	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		struct string_list_item *it;
		it = &(s->change.items[i]);
		d = it->util;
823 824
		if (!d->worktree_status ||
		    d->worktree_status == DIFF_STATUS_UNMERGED)
825
			continue;
826
		wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
827
	}
828
	wt_longstatus_print_trailer(s);
829 830
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
			    const char *email, timestamp_t timestamp, int tz,
			    const char *message, void *cb_data)
{
	int *c = cb_data;
	(*c)++;
	return 0;
}

static void wt_longstatus_print_stash_summary(struct wt_status *s)
{
	int stash_count = 0;

	for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
	if (stash_count > 0)
		status_printf_ln(s, GIT_COLOR_NORMAL,
				 Q_("Your stash currently has %d entry",
				    "Your stash currently has %d entries", stash_count),
				 stash_count);
}

852
static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
853
{
854
	struct child_process sm_summary = CHILD_PROCESS_INIT;
855 856 857
	struct strbuf cmd_stdout = STRBUF_INIT;
	struct strbuf summary = STRBUF_INIT;
	char *summary_content;
858

859 860
	argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
			 s->index_file);
861

862 863 864 865 866 867
	argv_array_push(&sm_summary.args, "submodule");
	argv_array_push(&sm_summary.args, "summary");
	argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
	argv_array_push(&sm_summary.args, "--for-status");
	argv_array_push(&sm_summary.args, "--summary-limit");
	argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
868
	if (!uncommitted)
869
		argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
870

871 872
	sm_summary.git_cmd = 1;
	sm_summary.no_stdin = 1;
873

874
	capture_command(&sm_summary, &cmd_stdout, 1024);
875 876

	/* prepend header, only if there's an actual output */
877
	if (cmd_stdout.len) {
878 879 880 881 882 883 884 885 886
		if (uncommitted)
			strbuf_addstr(&summary, _("Submodules changed but not updated:"));
		else
			strbuf_addstr(&summary, _("Submodule changes to be committed:"));
		strbuf_addstr(&summary, "\n\n");
	}
	strbuf_addbuf(&summary, &cmd_stdout);
	strbuf_release(&cmd_stdout);

887
	if (s->display_comment_prefix) {
888
		size_t len;
889 890 891 892
		summary_content = strbuf_detach(&summary, &len);
		strbuf_add_commented_lines(&summary, summary_content, len);
		free(summary_content);
	}
893 894 895

	fputs(summary.buf, s->fp);
	strbuf_release(&summary);
896 897
}

898 899 900 901
static void wt_longstatus_print_other(struct wt_status *s,
				      struct string_list *l,
				      const char *what,
				      const char *how)
902 903
{
	int i;
904
	struct strbuf buf = STRBUF_INIT;
Duy Nguyen's avatar
Duy Nguyen committed
905 906
	static struct string_list output = STRING_LIST_INIT_DUP;
	struct column_options copts;
907

908
	if (!l->nr)
909
		return;
910

911
	wt_longstatus_print_other_header(s, what, how);
912 913

	for (i = 0; i < l->nr; i++) {
914
		struct string_list_item *it;
Duy Nguyen's avatar
Duy Nguyen committed
915
		const char *path;
916
		it = &(l->items[i]);
917
		path = quote_path(it->string, s->prefix, &buf);
Duy Nguyen's avatar
Duy Nguyen committed
918 919 920 921
		if (column_active(s->colopts)) {
			string_list_append(&output, path);
			continue;
		}
922 923
		status_printf(s, color(WT_STATUS_HEADER, s), "\t");
		status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
Duy Nguyen's avatar
Duy Nguyen committed
924
				   "%s\n", path);
925
	}
Duy Nguyen's avatar
Duy Nguyen committed
926 927 928

	strbuf_release(&buf);
	if (!column_active(s->colopts))
929
		goto conclude;
Duy Nguyen's avatar
Duy Nguyen committed
930

931
	strbuf_addf(&buf, "%s%s\t%s",
Duy Nguyen's avatar
Duy Nguyen committed
932
		    color(WT_STATUS_HEADER, s),
933
		    s->display_comment_prefix ? "#" : "",
Duy Nguyen's avatar
Duy Nguyen committed
934 935 936 937 938 939 940 941
		    color(WT_STATUS_UNTRACKED, s));
	memset(&copts, 0, sizeof(copts));
	copts.padding = 1;
	copts.indent = buf.buf;
	if (want_color(s->use_color))
		copts.nl = GIT_COLOR_RESET "\n";
	print_columns(&output, s->colopts, &copts);
	string_list_clear(&output, 0);
942
	strbuf_release(&buf);
943
conclude:
944
	status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
945 946
}

947
size_t wt_status_locate_end(const char *s, size_t len)
948 949 950 951
{
	const char *p;
	struct strbuf pattern = STRBUF_INIT;

952
	strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
953 954 955 956
	if (starts_with(s, pattern.buf + 1))
		len = 0;
	else if ((p = strstr(s, pattern.buf)))
		len = p - s + 1;
957
	strbuf_release(&pattern);
958
	return len;
959 960
}

961 962
void wt_status_add_cut_line(FILE *fp)
{
963
	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
964 965 966 967 968 969 970 971
	struct strbuf buf = STRBUF_INIT;

	fprintf(fp, "%c %s", comment_line_char, cut_line);
	strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
	fputs(buf.buf, fp);
	strbuf_release(&buf);
}

972
static void wt_longstatus_print_verbose(struct wt_status *s)
973 974
{
	struct rev_info rev;
975
	struct setup_revision_opt opt;
976 977
	int dirty_submodules;
	const char *c = color(WT_STATUS_HEADER, s);
978

979
	init_revisions(&rev, NULL);
980
	rev.diffopt.flags.allow_textconv = 1;
981
	rev.diffopt.ita_invisible_in_index = 1;
982 983 984 985 986

	memset(&opt, 0, sizeof(opt));
	opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
	setup_revisions(0, NULL, &rev, &opt);

987
	rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
988
	rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
989 990
	rev.diffopt.file = s->fp;
	rev.diffopt.close_file = 0;
991 992 993 994
	/*
	 * If we're not going to stdout, then we definitely don't
	 * want color, since we are going to the commit message
	 * file (and even the "auto" setting won't work, since it
995 996 997
	 * will have checked isatty on stdout). But we then do want
	 * to insert the scissor line here to reliably remove the
	 * diff before committing.
998
	 */
999
	if (s->fp != stdout) {
1000
		rev.diffopt.use_color = 0;
1001
		wt_status_add_cut_line(s->fp);
1002
	}
1003 1004 1005
	if (s->verbose > 1 && s->commitable) {
		/* print_updated() printed a header, so do we */
		if (s->fp != stdout)
1006
			wt_longstatus_print_trailer(s);
1007 1008 1009 1010
		status_printf_ln(s, c, _("Changes to be committed:"));
		rev.diffopt.a_prefix = "c/";
		rev.diffopt.b_prefix = "i/";
	} /* else use prefix as per user config */
1011
	run_diff_index(&rev, 1);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	if (s->verbose > 1 &&
	    wt_status_check_worktree_changes(s, &dirty_submodules)) {
		status_printf_ln(s, c,
			"--------------------------------------------------");
		status_printf_ln(s, c, _("Changes not staged for commit:"));
		setup_work_tree();
		rev.diffopt.a_prefix = "i/";
		rev.diffopt.b_prefix = "w/";
		run_diff_files(&rev, 0);
	}
1022 1023
}

1024
static void wt_longstatus_print_tracking(struct wt_status *s)
1025 1026
{
	struct strbuf sb = STRBUF_INIT;
1027
	const char *cp, *ep, *branch_name;
1028
	struct branch *branch;
1029 1030
	char comment_line_string[3];
	int i;
1031 1032

	assert(s->branch && !s->is_initial);
1033
	if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1034
		return;
1035
	branch = branch_get(branch_name);
1036
	if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1037 1038
		return;

1039 1040 1041 1042 1043 1044 1045
	i = 0;
	if (s->display_comment_prefix) {
		comment_line_string[i++] = comment_line_char;
		comment_line_string[i++] = ' ';
	}
	comment_line_string[i] = '\0';

1046
	for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1047
		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1048
				 "%s%.*s", comment_line_string,
1049
				 (int)(ep - cp), cp);
1050 1051 1052 1053
	if (s->display_comment_prefix)
		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
				 comment_line_char);
	else
1054
		fputs("\n", s->fp);
1055
	strbuf_release(&sb);
1056 1057
}

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
static int has_unmerged(struct wt_status *s)
{
	int i;

	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		d = s->change.items[i].util;
		if (d->stagemask)
			return 1;
	}
	return 0;
}

static void show_merge_in_progress(struct wt_status *s,
				struct wt_status_state *state,
				const char *color)
{
	if (has_unmerged(s)) {
		status_printf_ln(s, color, _("You have unmerged paths."));
1077
		if (s->hints) {
1078
			status_printf_ln(s, color,
1079
					 _("  (fix conflicts and run \"git commit\")"));
1080
			status_printf_ln(s, color,
1081 1082
					 _("  (use \"git merge --abort\" to abort the merge)"));
		}
1083
	} else {
1084
		s-> commitable = 1;
1085 1086
		status_printf_ln(s, color,
			_("All conflicts fixed but you are still merging."));
1087
		if (s->hints)
1088 1089 1090
			status_printf_ln(s, color,
				_("  (use \"git commit\" to conclude merge)"));
	}
1091
	wt_longstatus_print_trailer(s);
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
}

static void show_am_in_progress(struct wt_status *s,
				struct wt_status_state *state,
				const char *color)
{
	status_printf_ln(s, color,
		_("You are in the middle of an am session."));
	if (state->am_empty_patch)
		status_printf_ln(s, color,
			_("The current patch is empty."));
1103
	if (s->hints) {
1104 1105
		if (!state->am_empty_patch)
			status_printf_ln(s, color,
1106
				_("  (fix conflicts and then run \"git am --continue\")"));
1107 1108 1109 1110 1111
		status_printf_ln(s, color,
			_("  (use \"git am --skip\" to skip this patch)"));
		status_printf_ln(s, color,
			_("  (use \"git am --abort\" to restore the original branch)"));
	}
1112
	wt_longstatus_print_trailer(s);
1113 1114
}

1115 1116 1117
static char *read_line_from_git_path(const char *filename)
{
	struct strbuf buf = STRBUF_INIT;
1118 1119
	FILE *fp = fopen_or_warn(git_path("%s", filename), "r");

1120 1121 1122 1123
	if (!fp) {
		strbuf_release(&buf);
		return NULL;
	}
1124
	strbuf_getline_lf(&buf, fp);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	if (!fclose(fp)) {
		return strbuf_detach(&buf, NULL);
	} else {
		strbuf_release(&buf);
		return NULL;
	}
}

static int split_commit_in_progress(struct wt_status *s)
{
	int split_in_progress = 0;
1136
	char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1137

1138
	if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1139
	    !s->branch || strcmp(s->branch, "HEAD"))
1140
		return 0;
1141

1142 1143 1144 1145
	head = read_line_from_git_path("HEAD");
	orig_head = read_line_from_git_path("ORIG_HEAD");
	rebase_amend = read_line_from_git_path("rebase-merge/amend");
	rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1146

1147 1148 1149 1150 1151 1152
	if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
		; /* fall through, no split in progress */
	else if (!strcmp(rebase_amend, rebase_orig_head))
		split_in_progress = !!strcmp(head, rebase_amend);
	else if (strcmp(orig_head, rebase_orig_head))
		split_in_progress = 1;
1153 1154 1155 1156 1157

	free(head);
	free(orig_head);
	free(rebase_amend);
	free(rebase_orig_head);
1158

1159 1160 1161
	return split_in_progress;
}

1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
/*
 * Turn
 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
 * into
 * "pick d6a2f03 some message"
 *
 * The function assumes that the line does not contain useless spaces
 * before or after the command.
 */
static void abbrev_sha1_in_line(struct strbuf *line)
{
	struct strbuf **split;
	int i;

	if (starts_with(line->buf, "exec ") ||
	    starts_with(line->buf, "x "))
		return;

	split = strbuf_split_max(line, ' ', 3);
	if (split[0] && split[1]) {
1182
		struct object_id oid;
1183 1184 1185 1186 1187 1188

		/*
		 * strbuf_split_max left a space. Trim it and re-add
		 * it after abbreviation.
		 */
		strbuf_trim(split[1]);
1189
		if (!get_oid(split[1]->buf, &oid)) {
1190
			strbuf_reset(split[1]);
1191
			strbuf_add_unique_abbrev(split[1], &oid,
1192 1193
						 DEFAULT_ABBREV);
			strbuf_addch(split[1], ' ');
1194 1195
			strbuf_reset(line);
			for (i = 0; split[i]; i++)
1196
				strbuf_addbuf(line, split[i]);
1197 1198
		}
	}
1199
	strbuf_list_free(split);
1200 1201
}

1202
static int read_rebase_todolist(const char *fname, struct string_list *lines)
1203 1204 1205 1206
{
	struct strbuf line = STRBUF_INIT;
	FILE *f = fopen(git_path("%s", fname), "r");

1207 1208 1209
	if (!f) {
		if (errno == ENOENT)
			return -1;
1210 1211
		die_errno("Could not open file %s for reading",
			  git_path("%s", fname));
1212
	}
1213
	while (!strbuf_getline_lf(&line, f)) {
1214 1215 1216 1217 1218 1219 1220 1221
		if (line.len && line.buf[0] == comment_line_char)
			continue;
		strbuf_trim(&