wt-status.c 66.7 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
void wt_status_prepare(struct repository *r, struct wt_status *s)
123
{
124
	memset(s, 0, sizeof(*s));
125
	s->repo = r;
126 127
	memcpy(s->color_palette, default_wt_status_colors,
	       sizeof(default_wt_status_colors));
128 129 130
	s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
	s->use_color = -1;
	s->relative_paths = 1;
131
	s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
132
	s->reference = "HEAD";
133
	s->fp = stdout;
134
	s->index_file = get_index_file();
135
	s->change.strdup_strings = 1;
136
	s->untracked.strdup_strings = 1;
137
	s->ignored.strdup_strings = 1;
138
	s->show_branch = -1;  /* unspecified */
139
	s->show_stash = 0;
140
	s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
141
	s->display_comment_prefix = 0;
142 143 144
	s->detect_rename = -1;
	s->rename_score = -1;
	s->rename_limit = -1;
145 146
}

147
static void wt_longstatus_print_unmerged_header(struct wt_status *s)
148
{
149 150 151 152
	int i;
	int del_mod_conflict = 0;
	int both_deleted = 0;
	int not_deleted = 0;
153
	const char *c = color(WT_STATUS_HEADER, s);
154

155
	status_printf_ln(s, c, _("Unmerged paths:"));
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

	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;
		}
	}

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

	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)"));
	}
196
	status_printf_ln(s, c, "%s", "");
197 198
}

199
static void wt_longstatus_print_cached_header(struct wt_status *s)
200
{
201
	const char *c = color(WT_STATUS_HEADER, s);
202

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

215 216 217
static void wt_longstatus_print_dirty_header(struct wt_status *s,
					     int has_deleted,
					     int has_dirty_submodules)
218
{
219
	const char *c = color(WT_STATUS_HEADER, s);
220

221
	status_printf_ln(s, c, _("Changes not staged for commit:"));
222
	if (!s->hints)
223
		return;
224
	if (!has_deleted)
225
		status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
226
	else
227 228
		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)"));
229
	if (has_dirty_submodules)
230
		status_printf_ln(s, c, _("  (commit or discard the untracked or modified content in submodules)"));
231
	status_printf_ln(s, c, "%s", "");
232 233
}

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

246
static void wt_longstatus_print_trailer(struct wt_status *s)
247
{
248
	status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
249 250
}

251
#define quote_path quote_path_relative
252

253
static const char *wt_status_unmerged_status_string(int stagemask)
254
{
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	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:
271
		BUG("unhandled unmerged status %x", stagemask);
272 273 274
	}
}

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

299 300 301 302 303 304 305 306 307 308 309 310 311
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;
}

312 313
static void wt_longstatus_print_unmerged_data(struct wt_status *s,
					      struct string_list_item *it)
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
{
	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);
}

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

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

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

388 389 390 391 392 393 394 395
	/*
	 * 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;

396 397
	one = quote_path(one_name, s->prefix, &onebuf);
	two = quote_path(two_name, s->prefix, &twobuf);
398

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

420 421
static char short_submodule_status(struct wt_status_change_data *d)
{
422 423 424 425 426 427 428 429 430
	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;
}

431 432 433
static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
					 struct diff_options *options,
					 void *data)
434 435 436
{
	struct wt_status *s = data;
	int i;
437 438 439 440

	if (!q->nr)
		return;
	s->workdir_dirty = 1;
441
	for (i = 0; i < q->nr; i++) {
442 443 444 445 446
		struct diff_filepair *p;
		struct string_list_item *it;
		struct wt_status_change_data *d;

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

		switch (p->status) {
		case DIFF_STATUS_ADDED:
465
			d->mode_worktree = p->two->mode;
466 467 468 469 470 471 472 473
			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;

474 475 476
		case DIFF_STATUS_COPIED:
		case DIFF_STATUS_RENAMED:
			if (d->rename_status)
477
				BUG("multiple renames on the same target? how?");
478 479 480 481
			d->rename_source = xstrdup(p->one->path);
			d->rename_score = p->score * 100 / MAX_SCORE;
			d->rename_status = p->status;
			/* fallthru */
482 483 484 485 486 487 488 489
		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;

490
		default:
491
			BUG("unhandled diff-files status '%c'", p->status);
492 493 494
			break;
		}

495 496 497
	}
}

498
static int unmerged_mask(struct index_state *istate, const char *path)
499 500
{
	int pos, mask;
501
	const struct cache_entry *ce;
502

503
	pos = index_name_pos(istate, path, strlen(path));
504 505 506 507 508
	if (0 <= pos)
		return 0;

	mask = 0;
	pos = -pos-1;
509 510
	while (pos < istate->cache_nr) {
		ce = istate->cache[pos++];
511 512 513 514 515 516 517
		if (strcmp(ce->name, path) || !ce_stage(ce))
			break;
		mask |= (1 << (ce_stage(ce) - 1));
	}
	return mask;
}

518 519 520
static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
					 struct diff_options *options,
					 void *data)
521
{
522
	struct wt_status *s = data;
523
	int i;
524 525 526 527 528 529 530

	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];
531
		it = string_list_insert(&s->change, p->two->path);
532 533 534 535 536 537 538 539
		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) {
540 541 542 543
		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);
544
			s->committable = 1;
545 546 547 548
			break;
		case DIFF_STATUS_DELETED:
			d->mode_head = p->one->mode;
			oidcpy(&d->oid_head, &p->one->oid);
549
			s->committable = 1;
550 551 552
			/* Leave {mode,oid}_index zero for a delete. */
			break;

553 554
		case DIFF_STATUS_COPIED:
		case DIFF_STATUS_RENAMED:
555
			if (d->rename_status)
556
				BUG("multiple renames on the same target? how?");
557 558 559
			d->rename_source = xstrdup(p->one->path);
			d->rename_score = p->score * 100 / MAX_SCORE;
			d->rename_status = p->status;
560 561 562 563 564 565 566
			/* 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);
567
			s->committable = 1;
568
			break;
569
		case DIFF_STATUS_UNMERGED:
570 571
			d->stagemask = unmerged_mask(s->repo->index,
						     p->two->path);
572 573 574 575 576
			/*
			 * 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.
			 */
577
			break;
578 579

		default:
580
			BUG("unhandled diff-index status '%c'", p->status);
581
			break;
582
		}
583
	}
584 585
}

586
static void wt_status_collect_changes_worktree(struct wt_status *s)
587 588
{
	struct rev_info rev;
589

590
	repo_init_revisions(s->repo, &rev, NULL);
591 592
	setup_revisions(0, NULL, &rev, NULL);
	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
593
	rev.diffopt.flags.dirty_submodules = 1;
594
	rev.diffopt.ita_invisible_in_index = 1;
595
	if (!s->show_untracked_files)
596
		rev.diffopt.flags.ignore_untracked_in_submodules = 1;
597
	if (s->ignore_submodule_arg) {
598
		rev.diffopt.flags.override_submodule_config = 1;
599
		handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
600
	}
601 602
	rev.diffopt.format_callback = wt_status_collect_changed_cb;
	rev.diffopt.format_callback_data = s;
603 604 605
	rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
	rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
	rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
606
	copy_pathspec(&rev.prune_data, &s->pathspec);
607 608 609 610 611 612
	run_diff_files(&rev, 0);
}

static void wt_status_collect_changes_index(struct wt_status *s)
{
	struct rev_info rev;
613
	struct setup_revision_opt opt;
614

615
	repo_init_revisions(s->repo, &rev, NULL);
616
	memset(&opt, 0, sizeof(opt));
617
	opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
618 619
	setup_revisions(0, NULL, &rev, &opt);

620
	rev.diffopt.flags.override_submodule_config = 1;
621
	rev.diffopt.ita_invisible_in_index = 1;
622
	if (s->ignore_submodule_arg) {
623
		handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
624 625 626 627 628 629 630 631 632 633
	} 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");
634
	}
635

636
	rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
637
	rev.diffopt.format_callback = wt_status_collect_updated_cb;
638
	rev.diffopt.format_callback_data = s;
639 640 641
	rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
	rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
	rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
642
	copy_pathspec(&rev.prune_data, &s->pathspec);
643 644 645
	run_diff_index(&rev, 1);
}

646 647
static void wt_status_collect_changes_initial(struct wt_status *s)
{
648
	struct index_state *istate = s->repo->index;
649 650
	int i;

651
	for (i = 0; i < istate->cache_nr; i++) {
652 653
		struct string_list_item *it;
		struct wt_status_change_data *d;
654
		const struct cache_entry *ce = istate->cache[i];
655

656
		if (!ce_path_match(istate, ce, &s->pathspec, NULL))
657
			continue;
658 659
		if (ce_intent_to_add(ce))
			continue;
660
		it = string_list_insert(&s->change, ce->name);
661 662 663 664 665
		d = it->util;
		if (!d) {
			d = xcalloc(1, sizeof(*d));
			it->util = d;
		}
666
		if (ce_stage(ce)) {
667
			d->index_status = DIFF_STATUS_UNMERGED;
668
			d->stagemask |= (1 << (ce_stage(ce) - 1));
669 670 671 672 673
			/*
			 * 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.
			 */
674
			s->committable = 1;
675
		} else {
676
			d->index_status = DIFF_STATUS_ADDED;
677 678
			/* Leave {mode,oid}_head zero for adds. */
			d->mode_index = ce->ce_mode;
679
			oidcpy(&d->oid_index, &ce->oid);
680
			s->committable = 1;
681
		}
682 683 684
	}
}

685 686 687 688
static void wt_status_collect_untracked(struct wt_status *s)
{
	int i;
	struct dir_struct dir;
689
	uint64_t t_begin = getnanotime();
690
	struct index_state *istate = s->repo->index;
691 692 693

	if (!s->show_untracked_files)
		return;
694

695 696 697 698
	memset(&dir, 0, sizeof(dir));
	if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
		dir.flags |=
			DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
699
	if (s->show_ignored_mode) {
700
		dir.flags |= DIR_SHOW_IGNORED_TOO;
701 702 703 704

		if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
			dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
	} else {
705
		dir.untracked = istate->untracked;
706 707
	}

708 709
	setup_standard_excludes(&dir);

710
	fill_directory(&dir, istate, &s->pathspec);
711

712
	for (i = 0; i < dir.nr; i++) {
713
		struct dir_entry *ent = dir.entries[i];
714 715
		if (index_name_is_other(istate, ent->name, ent->len) &&
		    dir_path_match(istate, ent, &s->pathspec, 0, NULL))
716
			string_list_insert(&s->untracked, ent->name);
717
		free(ent);
718
	}
719

720 721
	for (i = 0; i < dir.ignored_nr; i++) {
		struct dir_entry *ent = dir.ignored[i];
722 723
		if (index_name_is_other(istate, ent->name, ent->len) &&
		    dir_path_match(istate, ent, &s->pathspec, 0, NULL))
724 725
			string_list_insert(&s->ignored, ent->name);
		free(ent);
726 727
	}

728
	free(dir.entries);
729 730
	free(dir.ignored);
	clear_directory(&dir);
731

732 733
	if (advice_status_u_option)
		s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748
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;
}

749
void wt_status_collect(struct wt_status *s)
750
{
751
	trace2_region_enter("status", "worktrees", s->repo);
752
	wt_status_collect_changes_worktree(s);
753 754 755 756
	trace2_region_leave("status", "worktrees", s->repo);

	if (s->is_initial) {
		trace2_region_enter("status", "initial", s->repo);
757
		wt_status_collect_changes_initial(s);
758 759 760
		trace2_region_leave("status", "initial", s->repo);
	} else {
		trace2_region_enter("status", "index", s->repo);
761
		wt_status_collect_changes_index(s);
762 763 764 765
		trace2_region_leave("status", "index", s->repo);
	}

	trace2_region_enter("status", "untracked", s->repo);
766
	wt_status_collect_untracked(s);
767
	trace2_region_leave("status", "untracked", s->repo);
768

769
	wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
770
	if (s->state.merge_in_progress && !has_unmerged(s))
771
		s->committable = 1;
772 773
}

774 775 776 777 778
void wt_status_collect_free_buffers(struct wt_status *s)
{
	free(s->state.branch);
	free(s->state.onto);
	free(s->state.detached_from);
779 780
}

781
static void wt_longstatus_print_unmerged(struct wt_status *s)
782 783 784 785 786 787 788 789 790 791 792 793
{
	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) {
794
			wt_longstatus_print_unmerged_header(s);
795 796
			shown_header = 1;
		}
797
		wt_longstatus_print_unmerged_data(s, it);
798 799
	}
	if (shown_header)
800
		wt_longstatus_print_trailer(s);
801 802 803

}

804
static void wt_longstatus_print_updated(struct wt_status *s)
805 806 807 808 809 810 811 812 813 814 815 816 817
{
	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) {
818
			wt_longstatus_print_cached_header(s);
819 820
			shown_header = 1;
		}
821
		wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
822 823
	}
	if (shown_header)
824
		wt_longstatus_print_trailer(s);
825 826 827 828 829 830 831
}

/*
 * -1 : has delete
 *  0 : no change
 *  1 : some change but no delete
 */
832 833
static int wt_status_check_worktree_changes(struct wt_status *s,
					     int *dirty_submodules)
834 835 836 837
{
	int i;
	int changes = 0;

838 839
	*dirty_submodules = 0;

840 841 842
	for (i = 0; i < s->change.nr; i++) {
		struct wt_status_change_data *d;
		d = s->change.items[i].util;
843 844
		if (!d->worktree_status ||
		    d->worktree_status == DIFF_STATUS_UNMERGED)
845
			continue;
846 847 848 849
		if (!changes)
			changes = 1;
		if (d->dirty_submodule)
			*dirty_submodules = 1;
850
		if (d->worktree_status == DIFF_STATUS_DELETED)
851
			changes = -1;
852 853 854 855
	}
	return changes;
}

856
static void wt_longstatus_print_changed(struct wt_status *s)
857
{
858 859
	int i, dirty_submodules;
	int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
860 861 862 863

	if (!worktree_changes)
		return;

864
	wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
865 866 867 868 869 870

	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;
871 872
		if (!d->worktree_status ||
		    d->worktree_status == DIFF_STATUS_UNMERGED)
873
			continue;
874
		wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
875
	}
876
	wt_longstatus_print_trailer(s);
877 878
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
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);
}

900
static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
901
{
902
	struct child_process sm_summary = CHILD_PROCESS_INIT;
903 904 905
	struct strbuf cmd_stdout = STRBUF_INIT;
	struct strbuf summary = STRBUF_INIT;
	char *summary_content;
906

907 908
	argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
			 s->index_file);
909

910 911 912 913 914 915
	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);
916
	if (!uncommitted)
917
		argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
918

919 920
	sm_summary.git_cmd = 1;
	sm_summary.no_stdin = 1;
921

922
	capture_command(&sm_summary, &cmd_stdout, 1024);
923 924

	/* prepend header, only if there's an actual output */
925
	if (cmd_stdout.len) {
926 927 928 929 930 931 932 933 934
		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);

935
	if (s->display_comment_prefix) {
936
		size_t len;
937 938 939 940
		summary_content = strbuf_detach(&summary, &len);
		strbuf_add_commented_lines(&summary, summary_content, len);
		free(summary_content);
	}
941 942 943

	fputs(summary.buf, s->fp);
	strbuf_release(&summary);
944 945
}

946 947 948 949
static void wt_longstatus_print_other(struct wt_status *s,
				      struct string_list *l,
				      const char *what,
				      const char *how)
950 951
{
	int i;
952
	struct strbuf buf = STRBUF_INIT;
Duy Nguyen's avatar
Duy Nguyen committed
953 954
	static struct string_list output = STRING_LIST_INIT_DUP;
	struct column_options copts;
955

956
	if (!l->nr)
957
		return;
958

959
	wt_longstatus_print_other_header(s, what, how);
960 961

	for (i = 0; i < l->nr; i++) {
962
		struct string_list_item *it;
Duy Nguyen's avatar
Duy Nguyen committed
963
		const char *path;
964
		it = &(l->items[i]);
965
		path = quote_path(it->string, s->prefix, &buf);
Duy Nguyen's avatar
Duy Nguyen committed
966 967 968 969
		if (column_active(s->colopts)) {
			string_list_append(&output, path);
			continue;
		}
970 971
		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
972
				   "%s\n", path);
973
	}
Duy Nguyen's avatar
Duy Nguyen committed
974 975 976

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

979
	strbuf_addf(&buf, "%s%s\t%s",
Duy Nguyen's avatar
Duy Nguyen committed
980
		    color(WT_STATUS_HEADER, s),
981
		    s->display_comment_prefix ? "#" : "",
Duy Nguyen's avatar
Duy Nguyen committed
982 983 984 985 986 987 988 989
		    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);
990
	strbuf_release(&buf);
991
conclude:
992
	status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
993 994
}

995
size_t wt_status_locate_end(const char *s, size_t len)
996 997 998 999
{
	const char *p;
	struct strbuf pattern = STRBUF_INIT;

1000
	strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
1001 1002 1003 1004
	if (starts_with(s, pattern.buf + 1))
		len = 0;
	else if ((p = strstr(s, pattern.buf)))
		len = p - s + 1;
1005
	strbuf_release(&pattern);
1006
	return len;
1007 1008
}

1009 1010
void wt_status_add_cut_line(FILE *fp)
{
1011
	const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1012 1013 1014 1015 1016 1017 1018 1019
	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);
}

1020
static void wt_longstatus_print_verbose(struct wt_status *s)
1021 1022
{
	struct rev_info rev;
1023
	struct setup_revision_opt opt;
1024 1025
	int dirty_submodules;
	const char *c = color(WT_STATUS_HEADER, s);
1026

1027
	repo_init_revisions(s->repo, &rev, NULL);
1028
	rev.diffopt.flags.allow_textconv = 1;
1029
	rev.diffopt.ita_invisible_in_index = 1;
1030 1031

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

1035
	rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1036 1037 1038
	rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
	rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
	rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1039 1040
	rev.diffopt.file = s->fp;
	rev.diffopt.close_file = 0;
1041 1042 1043 1044
	/*
	 * 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
1045 1046 1047
	 * will have checked isatty on stdout). But we then do want
	 * to insert the scissor line here to reliably remove the
	 * diff before committing.
1048
	 */
1049
	if (s->fp != stdout) {
1050
		rev.diffopt.use_color = 0;
1051
		wt_status_add_cut_line(s->fp);
1052
	}
1053
	if (s->verbose > 1 && s->committable) {
1054 1055
		/* print_updated() printed a header, so do we */
		if (s->fp != stdout)
1056
			wt_longstatus_print_trailer(s);
1057 1058 1059 1060
		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 */
1061
	run_diff_index(&rev, 1);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	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);
	}
1072 1073
}

1074
static void wt_longstatus_print_tracking(struct wt_status *s)
1075 1076
{
	struct strbuf sb = STRBUF_INIT;
1077
	const char *cp, *ep, *branch_name;
1078
	struct branch *branch;
1079 1080
	char comment_line_string[3];
	int i;
1081 1082

	assert(s->branch && !s->is_initial);
1083
	if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1084
		return;
1085
	branch = branch_get(branch_name);
1086
	if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1087 1088
		return;

1089 1090 1091 1092 1093 1094 1095
	i = 0;
	if (s->display_comment_prefix) {
		comment_line_string[i++] = comment_line_char;
		comment_line_string[i++] = ' ';
	}
	comment_line_string[i] = '\0';

1096
	for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1097
		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1098
				 "%s%.*s", comment_line_string,
1099
				 (int)(ep - cp), cp);
1100 1101 1102 1103
	if (s->display_comment_prefix)
		color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
				 comment_line_char);
	else
1104
		fputs("\n", s->fp);
1105
	strbuf_release(&sb);
1106 1107
}

1108
static void show_merge_in_progress(struct wt_status *s,
1109
				   const char *color)
1110 1111 1112
{
	if (has_unmerged(s)) {
		status_printf_ln(s, color, _("You have unmerged paths."));
1113
		if (s->hints) {
1114
			status_printf_ln(s, color,
1115
					 _("  (fix conflicts and run \"git commit\")"));
1116
			status_printf_ln(s, color,
1117 1118
					 _("  (use \"git merge --abort\" to abort the merge)"));
		}
1119 1120 1121
	} else {
		status_printf_ln(s, color,
			_("All conflicts fixed but you are still merging."));
1122
		if (s->hints)
1123 1124 1125
			status_printf_ln(s, color,
				_("  (use \"git commit\" to conclude merge)"));
	}
1126
	wt_longstatus_print_trailer(s);
1127 1128 1129 1130 1131 1132 1133
}

static void show_am_in_progress(struct wt_status *s,
				const char *color)
{
	status_printf_ln(s, color,
		_("You are in the middle of an am session."));
1134
	if (s->state.am_empty_patch)
1135 1136
		status_printf_ln(s, color,
			_("The current patch is empty."));
1137
	if (s->hints) {
1138
		if (!s->state.am_empty_patch)
1139
			status_printf_ln(s, color,
1140
				_("  (fix conflicts and then run \"git am --continue\")"));
1141 1142 1143 1144 1145
		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)"));
	}
1146
	wt_longstatus_print_trailer(s);