git
/
wt-status.c
2683 строки · 74.8 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "advice.h"
5#include "wt-status.h"
6#include "object.h"
7#include "dir.h"
8#include "commit.h"
9#include "diff.h"
10#include "environment.h"
11#include "gettext.h"
12#include "hash.h"
13#include "hex.h"
14#include "object-name.h"
15#include "path.h"
16#include "revision.h"
17#include "diffcore.h"
18#include "quote.h"
19#include "run-command.h"
20#include "strvec.h"
21#include "remote.h"
22#include "refs.h"
23#include "submodule.h"
24#include "column.h"
25#include "read-cache.h"
26#include "setup.h"
27#include "strbuf.h"
28#include "trace.h"
29#include "trace2.h"
30#include "tree.h"
31#include "utf8.h"
32#include "worktree.h"
33#include "lockfile.h"
34#include "sequencer.h"
35#include "fsmonitor-settings.h"
36
37#define AB_DELAY_WARNING_IN_MS (2 * 1000)
38#define UF_DELAY_WARNING_IN_MS (2 * 1000)
39
40static const char cut_line[] =
41"------------------------ >8 ------------------------\n";
42
43static char default_wt_status_colors[][COLOR_MAXLEN] = {
44GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
45GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
46GIT_COLOR_RED, /* WT_STATUS_CHANGED */
47GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
48GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
49GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
50GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
51GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
52GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
53};
54
55static const char *color(int slot, struct wt_status *s)
56{
57const char *c = "";
58if (want_color(s->use_color))
59c = s->color_palette[slot];
60if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
61c = s->color_palette[WT_STATUS_HEADER];
62return c;
63}
64
65static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
66const char *fmt, va_list ap, const char *trail)
67{
68struct strbuf sb = STRBUF_INIT;
69struct strbuf linebuf = STRBUF_INIT;
70const char *line, *eol;
71
72strbuf_vaddf(&sb, fmt, ap);
73if (!sb.len) {
74if (s->display_comment_prefix) {
75strbuf_addstr(&sb, comment_line_str);
76if (!trail)
77strbuf_addch(&sb, ' ');
78}
79color_print_strbuf(s->fp, color, &sb);
80if (trail)
81fprintf(s->fp, "%s", trail);
82strbuf_release(&sb);
83return;
84}
85for (line = sb.buf; *line; line = eol + 1) {
86eol = strchr(line, '\n');
87
88strbuf_reset(&linebuf);
89if (at_bol && s->display_comment_prefix) {
90strbuf_addstr(&linebuf, comment_line_str);
91if (*line != '\n' && *line != '\t')
92strbuf_addch(&linebuf, ' ');
93}
94if (eol)
95strbuf_add(&linebuf, line, eol - line);
96else
97strbuf_addstr(&linebuf, line);
98color_print_strbuf(s->fp, color, &linebuf);
99if (eol)
100fprintf(s->fp, "\n");
101else
102break;
103at_bol = 1;
104}
105if (trail)
106fprintf(s->fp, "%s", trail);
107strbuf_release(&linebuf);
108strbuf_release(&sb);
109}
110
111void status_printf_ln(struct wt_status *s, const char *color,
112const char *fmt, ...)
113{
114va_list ap;
115
116va_start(ap, fmt);
117status_vprintf(s, 1, color, fmt, ap, "\n");
118va_end(ap);
119}
120
121void status_printf(struct wt_status *s, const char *color,
122const char *fmt, ...)
123{
124va_list ap;
125
126va_start(ap, fmt);
127status_vprintf(s, 1, color, fmt, ap, NULL);
128va_end(ap);
129}
130
131__attribute__((format (printf, 3, 4)))
132static void status_printf_more(struct wt_status *s, const char *color,
133const char *fmt, ...)
134{
135va_list ap;
136
137va_start(ap, fmt);
138status_vprintf(s, 0, color, fmt, ap, NULL);
139va_end(ap);
140}
141
142void wt_status_prepare(struct repository *r, struct wt_status *s)
143{
144memset(s, 0, sizeof(*s));
145s->repo = r;
146memcpy(s->color_palette, default_wt_status_colors,
147sizeof(default_wt_status_colors));
148s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
149s->use_color = -1;
150s->relative_paths = 1;
151s->branch = refs_resolve_refdup(get_main_ref_store(the_repository),
152"HEAD", 0, NULL, NULL);
153s->reference = "HEAD";
154s->fp = stdout;
155s->index_file = get_index_file();
156s->change.strdup_strings = 1;
157s->untracked.strdup_strings = 1;
158s->ignored.strdup_strings = 1;
159s->show_branch = -1; /* unspecified */
160s->show_stash = 0;
161s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
162s->display_comment_prefix = 0;
163s->detect_rename = -1;
164s->rename_score = -1;
165s->rename_limit = -1;
166}
167
168static void wt_longstatus_print_unmerged_header(struct wt_status *s)
169{
170int i;
171int del_mod_conflict = 0;
172int both_deleted = 0;
173int not_deleted = 0;
174const char *c = color(WT_STATUS_HEADER, s);
175
176status_printf_ln(s, c, _("Unmerged paths:"));
177
178for (i = 0; i < s->change.nr; i++) {
179struct string_list_item *it = &(s->change.items[i]);
180struct wt_status_change_data *d = it->util;
181
182switch (d->stagemask) {
183case 0:
184break;
185case 1:
186both_deleted = 1;
187break;
188case 3:
189case 5:
190del_mod_conflict = 1;
191break;
192default:
193not_deleted = 1;
194break;
195}
196}
197
198if (!s->hints)
199return;
200if (s->whence != FROM_COMMIT)
201;
202else if (!s->is_initial) {
203if (!strcmp(s->reference, "HEAD"))
204status_printf_ln(s, c,
205_(" (use \"git restore --staged <file>...\" to unstage)"));
206else
207status_printf_ln(s, c,
208_(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
209s->reference);
210} else
211status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
212
213if (!both_deleted) {
214if (!del_mod_conflict)
215status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
216else
217status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
218} else if (!del_mod_conflict && !not_deleted) {
219status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
220} else {
221status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
222}
223}
224
225static void wt_longstatus_print_cached_header(struct wt_status *s)
226{
227const char *c = color(WT_STATUS_HEADER, s);
228
229status_printf_ln(s, c, _("Changes to be committed:"));
230if (!s->hints)
231return;
232if (s->whence != FROM_COMMIT)
233; /* NEEDSWORK: use "git reset --unresolve"??? */
234else if (!s->is_initial) {
235if (!strcmp(s->reference, "HEAD"))
236status_printf_ln(s, c
237, _(" (use \"git restore --staged <file>...\" to unstage)"));
238else
239status_printf_ln(s, c,
240_(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
241s->reference);
242} else
243status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
244}
245
246static void wt_longstatus_print_dirty_header(struct wt_status *s,
247int has_deleted,
248int has_dirty_submodules)
249{
250const char *c = color(WT_STATUS_HEADER, s);
251
252status_printf_ln(s, c, _("Changes not staged for commit:"));
253if (!s->hints)
254return;
255if (!has_deleted)
256status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
257else
258status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
259status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
260if (has_dirty_submodules)
261status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
262}
263
264static void wt_longstatus_print_other_header(struct wt_status *s,
265const char *what,
266const char *how)
267{
268const char *c = color(WT_STATUS_HEADER, s);
269status_printf_ln(s, c, "%s:", what);
270if (!s->hints)
271return;
272status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
273}
274
275static void wt_longstatus_print_trailer(struct wt_status *s)
276{
277status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
278}
279
280static const char *wt_status_unmerged_status_string(int stagemask)
281{
282switch (stagemask) {
283case 1:
284return _("both deleted:");
285case 2:
286return _("added by us:");
287case 3:
288return _("deleted by them:");
289case 4:
290return _("added by them:");
291case 5:
292return _("deleted by us:");
293case 6:
294return _("both added:");
295case 7:
296return _("both modified:");
297default:
298BUG("unhandled unmerged status %x", stagemask);
299}
300}
301
302static const char *wt_status_diff_status_string(int status)
303{
304switch (status) {
305case DIFF_STATUS_ADDED:
306return _("new file:");
307case DIFF_STATUS_COPIED:
308return _("copied:");
309case DIFF_STATUS_DELETED:
310return _("deleted:");
311case DIFF_STATUS_MODIFIED:
312return _("modified:");
313case DIFF_STATUS_RENAMED:
314return _("renamed:");
315case DIFF_STATUS_TYPE_CHANGED:
316return _("typechange:");
317case DIFF_STATUS_UNKNOWN:
318return _("unknown:");
319case DIFF_STATUS_UNMERGED:
320return _("unmerged:");
321default:
322return NULL;
323}
324}
325
326static int maxwidth(const char *(*label)(int), int minval, int maxval)
327{
328int result = 0, i;
329
330for (i = minval; i <= maxval; i++) {
331const char *s = label(i);
332int len = s ? utf8_strwidth(s) : 0;
333if (len > result)
334result = len;
335}
336return result;
337}
338
339static void wt_longstatus_print_unmerged_data(struct wt_status *s,
340struct string_list_item *it)
341{
342const char *c = color(WT_STATUS_UNMERGED, s);
343struct wt_status_change_data *d = it->util;
344struct strbuf onebuf = STRBUF_INIT;
345static char *padding;
346static int label_width;
347const char *one, *how;
348int len;
349
350if (!padding) {
351label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
352label_width += strlen(" ");
353padding = xmallocz(label_width);
354memset(padding, ' ', label_width);
355}
356
357one = quote_path(it->string, s->prefix, &onebuf, 0);
358status_printf(s, color(WT_STATUS_HEADER, s), "\t");
359
360how = wt_status_unmerged_status_string(d->stagemask);
361len = label_width - utf8_strwidth(how);
362status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
363strbuf_release(&onebuf);
364}
365
366static void wt_longstatus_print_change_data(struct wt_status *s,
367int change_type,
368struct string_list_item *it)
369{
370struct wt_status_change_data *d = it->util;
371const char *c = color(change_type, s);
372int status;
373char *one_name;
374char *two_name;
375const char *one, *two;
376struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
377struct strbuf extra = STRBUF_INIT;
378static char *padding;
379static int label_width;
380const char *what;
381int len;
382
383if (!padding) {
384/* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
385label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
386label_width += strlen(" ");
387padding = xmallocz(label_width);
388memset(padding, ' ', label_width);
389}
390
391one_name = two_name = it->string;
392switch (change_type) {
393case WT_STATUS_UPDATED:
394status = d->index_status;
395break;
396case WT_STATUS_CHANGED:
397if (d->new_submodule_commits || d->dirty_submodule) {
398strbuf_addstr(&extra, " (");
399if (d->new_submodule_commits)
400strbuf_addstr(&extra, _("new commits, "));
401if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
402strbuf_addstr(&extra, _("modified content, "));
403if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
404strbuf_addstr(&extra, _("untracked content, "));
405strbuf_setlen(&extra, extra.len - 2);
406strbuf_addch(&extra, ')');
407}
408status = d->worktree_status;
409break;
410default:
411BUG("unhandled change_type %d in wt_longstatus_print_change_data",
412change_type);
413}
414
415/*
416* Only pick up the rename it's relevant. If the rename is for
417* the changed section and we're printing the updated section,
418* ignore it.
419*/
420if (d->rename_status == status)
421one_name = d->rename_source;
422
423one = quote_path(one_name, s->prefix, &onebuf, 0);
424two = quote_path(two_name, s->prefix, &twobuf, 0);
425
426status_printf(s, color(WT_STATUS_HEADER, s), "\t");
427what = wt_status_diff_status_string(status);
428if (!what)
429BUG("unhandled diff status %c", status);
430len = label_width - utf8_strwidth(what);
431assert(len >= 0);
432if (one_name != two_name)
433status_printf_more(s, c, "%s%.*s%s -> %s",
434what, len, padding, one, two);
435else
436status_printf_more(s, c, "%s%.*s%s",
437what, len, padding, one);
438if (extra.len) {
439status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
440strbuf_release(&extra);
441}
442status_printf_more(s, GIT_COLOR_NORMAL, "\n");
443strbuf_release(&onebuf);
444strbuf_release(&twobuf);
445}
446
447static char short_submodule_status(struct wt_status_change_data *d)
448{
449if (d->new_submodule_commits)
450return 'M';
451if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
452return 'm';
453if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
454return '?';
455return d->worktree_status;
456}
457
458static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
459struct diff_options *options UNUSED,
460void *data)
461{
462struct wt_status *s = data;
463int i;
464
465if (!q->nr)
466return;
467s->workdir_dirty = 1;
468for (i = 0; i < q->nr; i++) {
469struct diff_filepair *p;
470struct string_list_item *it;
471struct wt_status_change_data *d;
472
473p = q->queue[i];
474it = string_list_insert(&s->change, p->two->path);
475d = it->util;
476if (!d) {
477CALLOC_ARRAY(d, 1);
478it->util = d;
479}
480if (!d->worktree_status)
481d->worktree_status = p->status;
482if (S_ISGITLINK(p->two->mode)) {
483d->dirty_submodule = p->two->dirty_submodule;
484d->new_submodule_commits = !oideq(&p->one->oid,
485&p->two->oid);
486if (s->status_format == STATUS_FORMAT_SHORT)
487d->worktree_status = short_submodule_status(d);
488}
489
490switch (p->status) {
491case DIFF_STATUS_ADDED:
492d->mode_worktree = p->two->mode;
493break;
494
495case DIFF_STATUS_DELETED:
496d->mode_index = p->one->mode;
497oidcpy(&d->oid_index, &p->one->oid);
498/* mode_worktree is zero for a delete. */
499break;
500
501case DIFF_STATUS_COPIED:
502case DIFF_STATUS_RENAMED:
503if (d->rename_status)
504BUG("multiple renames on the same target? how?");
505d->rename_source = xstrdup(p->one->path);
506d->rename_score = p->score * 100 / MAX_SCORE;
507d->rename_status = p->status;
508/* fallthru */
509case DIFF_STATUS_MODIFIED:
510case DIFF_STATUS_TYPE_CHANGED:
511case DIFF_STATUS_UNMERGED:
512d->mode_index = p->one->mode;
513d->mode_worktree = p->two->mode;
514oidcpy(&d->oid_index, &p->one->oid);
515break;
516
517default:
518BUG("unhandled diff-files status '%c'", p->status);
519break;
520}
521
522}
523}
524
525static int unmerged_mask(struct index_state *istate, const char *path)
526{
527int pos, mask;
528const struct cache_entry *ce;
529
530pos = index_name_pos(istate, path, strlen(path));
531if (0 <= pos)
532return 0;
533
534mask = 0;
535pos = -pos-1;
536while (pos < istate->cache_nr) {
537ce = istate->cache[pos++];
538if (strcmp(ce->name, path) || !ce_stage(ce))
539break;
540mask |= (1 << (ce_stage(ce) - 1));
541}
542return mask;
543}
544
545static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
546struct diff_options *options UNUSED,
547void *data)
548{
549struct wt_status *s = data;
550int i;
551
552for (i = 0; i < q->nr; i++) {
553struct diff_filepair *p;
554struct string_list_item *it;
555struct wt_status_change_data *d;
556
557p = q->queue[i];
558it = string_list_insert(&s->change, p->two->path);
559d = it->util;
560if (!d) {
561CALLOC_ARRAY(d, 1);
562it->util = d;
563}
564if (!d->index_status)
565d->index_status = p->status;
566switch (p->status) {
567case DIFF_STATUS_ADDED:
568/* Leave {mode,oid}_head zero for an add. */
569d->mode_index = p->two->mode;
570oidcpy(&d->oid_index, &p->two->oid);
571s->committable = 1;
572break;
573case DIFF_STATUS_DELETED:
574d->mode_head = p->one->mode;
575oidcpy(&d->oid_head, &p->one->oid);
576s->committable = 1;
577/* Leave {mode,oid}_index zero for a delete. */
578break;
579
580case DIFF_STATUS_COPIED:
581case DIFF_STATUS_RENAMED:
582if (d->rename_status)
583BUG("multiple renames on the same target? how?");
584d->rename_source = xstrdup(p->one->path);
585d->rename_score = p->score * 100 / MAX_SCORE;
586d->rename_status = p->status;
587/* fallthru */
588case DIFF_STATUS_MODIFIED:
589case DIFF_STATUS_TYPE_CHANGED:
590d->mode_head = p->one->mode;
591d->mode_index = p->two->mode;
592oidcpy(&d->oid_head, &p->one->oid);
593oidcpy(&d->oid_index, &p->two->oid);
594s->committable = 1;
595break;
596case DIFF_STATUS_UNMERGED:
597d->stagemask = unmerged_mask(s->repo->index,
598p->two->path);
599/*
600* Don't bother setting {mode,oid}_{head,index} since the print
601* code will output the stage values directly and not use the
602* values in these fields.
603*/
604break;
605
606default:
607BUG("unhandled diff-index status '%c'", p->status);
608break;
609}
610}
611}
612
613static void wt_status_collect_changes_worktree(struct wt_status *s)
614{
615struct rev_info rev;
616
617repo_init_revisions(s->repo, &rev, NULL);
618setup_revisions(0, NULL, &rev, NULL);
619rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
620rev.diffopt.flags.dirty_submodules = 1;
621rev.diffopt.ita_invisible_in_index = 1;
622if (!s->show_untracked_files)
623rev.diffopt.flags.ignore_untracked_in_submodules = 1;
624if (s->ignore_submodule_arg) {
625rev.diffopt.flags.override_submodule_config = 1;
626handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
627} else if (!rev.diffopt.flags.ignore_submodule_set &&
628s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
629handle_ignore_submodules_arg(&rev.diffopt, "none");
630rev.diffopt.format_callback = wt_status_collect_changed_cb;
631rev.diffopt.format_callback_data = s;
632rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
633rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
634rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
635copy_pathspec(&rev.prune_data, &s->pathspec);
636run_diff_files(&rev, 0);
637release_revisions(&rev);
638}
639
640static void wt_status_collect_changes_index(struct wt_status *s)
641{
642struct rev_info rev;
643struct setup_revision_opt opt;
644
645repo_init_revisions(s->repo, &rev, NULL);
646memset(&opt, 0, sizeof(opt));
647opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
648setup_revisions(0, NULL, &rev, &opt);
649
650rev.diffopt.flags.override_submodule_config = 1;
651rev.diffopt.ita_invisible_in_index = 1;
652if (s->ignore_submodule_arg) {
653handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
654} else {
655/*
656* Unless the user did explicitly request a submodule ignore
657* mode by passing a command line option we do not ignore any
658* changed submodule SHA-1s when comparing index and HEAD, no
659* matter what is configured. Otherwise the user won't be
660* shown any submodules manually added (and which are
661* staged to be committed), which would be really confusing.
662*/
663handle_ignore_submodules_arg(&rev.diffopt, "dirty");
664}
665
666rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
667rev.diffopt.format_callback = wt_status_collect_updated_cb;
668rev.diffopt.format_callback_data = s;
669rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
670rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
671rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
672
673/*
674* The `recursive` option must be enabled to allow the diff to recurse
675* into subdirectories of sparse directory index entries. If it is not
676* enabled, a subdirectory containing file(s) with changes is reported
677* as "modified", rather than the modified files themselves.
678*/
679rev.diffopt.flags.recursive = 1;
680
681copy_pathspec(&rev.prune_data, &s->pathspec);
682run_diff_index(&rev, DIFF_INDEX_CACHED);
683release_revisions(&rev);
684}
685
686static int add_file_to_list(const struct object_id *oid,
687struct strbuf *base, const char *path,
688unsigned int mode, void *context)
689{
690struct string_list_item *it;
691struct wt_status_change_data *d;
692struct wt_status *s = context;
693struct strbuf full_name = STRBUF_INIT;
694
695if (S_ISDIR(mode))
696return READ_TREE_RECURSIVE;
697
698strbuf_add(&full_name, base->buf, base->len);
699strbuf_addstr(&full_name, path);
700it = string_list_insert(&s->change, full_name.buf);
701d = it->util;
702if (!d) {
703CALLOC_ARRAY(d, 1);
704it->util = d;
705}
706
707d->index_status = DIFF_STATUS_ADDED;
708/* Leave {mode,oid}_head zero for adds. */
709d->mode_index = mode;
710oidcpy(&d->oid_index, oid);
711s->committable = 1;
712strbuf_release(&full_name);
713return 0;
714}
715
716static void wt_status_collect_changes_initial(struct wt_status *s)
717{
718struct index_state *istate = s->repo->index;
719int i;
720
721for (i = 0; i < istate->cache_nr; i++) {
722struct string_list_item *it;
723struct wt_status_change_data *d;
724const struct cache_entry *ce = istate->cache[i];
725
726if (!ce_path_match(istate, ce, &s->pathspec, NULL))
727continue;
728if (ce_intent_to_add(ce))
729continue;
730if (S_ISSPARSEDIR(ce->ce_mode)) {
731/*
732* This is a sparse directory entry, so we want to collect all
733* of the added files within the tree. This requires recursively
734* expanding the trees to find the elements that are new in this
735* tree and marking them with DIFF_STATUS_ADDED.
736*/
737struct strbuf base = STRBUF_INIT;
738struct pathspec ps = { 0 };
739struct tree *tree = lookup_tree(istate->repo, &ce->oid);
740
741ps.recursive = 1;
742ps.has_wildcard = 1;
743ps.max_depth = -1;
744
745strbuf_add(&base, ce->name, ce->ce_namelen);
746read_tree_at(istate->repo, tree, &base, 0, &ps,
747add_file_to_list, s);
748continue;
749}
750
751it = string_list_insert(&s->change, ce->name);
752d = it->util;
753if (!d) {
754CALLOC_ARRAY(d, 1);
755it->util = d;
756}
757if (ce_stage(ce)) {
758d->index_status = DIFF_STATUS_UNMERGED;
759d->stagemask |= (1 << (ce_stage(ce) - 1));
760/*
761* Don't bother setting {mode,oid}_{head,index} since the print
762* code will output the stage values directly and not use the
763* values in these fields.
764*/
765s->committable = 1;
766} else {
767d->index_status = DIFF_STATUS_ADDED;
768/* Leave {mode,oid}_head zero for adds. */
769d->mode_index = ce->ce_mode;
770oidcpy(&d->oid_index, &ce->oid);
771s->committable = 1;
772}
773}
774}
775
776static void wt_status_collect_untracked(struct wt_status *s)
777{
778int i;
779struct dir_struct dir = DIR_INIT;
780uint64_t t_begin = getnanotime();
781struct index_state *istate = s->repo->index;
782
783if (!s->show_untracked_files)
784return;
785
786if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
787dir.flags |=
788DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
789if (s->show_ignored_mode) {
790dir.flags |= DIR_SHOW_IGNORED_TOO;
791
792if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
793dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
794} else {
795dir.untracked = istate->untracked;
796}
797
798setup_standard_excludes(&dir);
799
800fill_directory(&dir, istate, &s->pathspec);
801
802for (i = 0; i < dir.nr; i++) {
803struct dir_entry *ent = dir.entries[i];
804if (index_name_is_other(istate, ent->name, ent->len))
805string_list_insert(&s->untracked, ent->name);
806}
807
808for (i = 0; i < dir.ignored_nr; i++) {
809struct dir_entry *ent = dir.ignored[i];
810if (index_name_is_other(istate, ent->name, ent->len))
811string_list_insert(&s->ignored, ent->name);
812}
813
814dir_clear(&dir);
815
816if (advice_enabled(ADVICE_STATUS_U_OPTION))
817s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
818}
819
820static int has_unmerged(struct wt_status *s)
821{
822int i;
823
824for (i = 0; i < s->change.nr; i++) {
825struct wt_status_change_data *d;
826d = s->change.items[i].util;
827if (d->stagemask)
828return 1;
829}
830return 0;
831}
832
833void wt_status_collect(struct wt_status *s)
834{
835trace2_region_enter("status", "worktrees", s->repo);
836wt_status_collect_changes_worktree(s);
837trace2_region_leave("status", "worktrees", s->repo);
838
839if (s->is_initial) {
840trace2_region_enter("status", "initial", s->repo);
841wt_status_collect_changes_initial(s);
842trace2_region_leave("status", "initial", s->repo);
843} else {
844trace2_region_enter("status", "index", s->repo);
845wt_status_collect_changes_index(s);
846trace2_region_leave("status", "index", s->repo);
847}
848
849trace2_region_enter("status", "untracked", s->repo);
850wt_status_collect_untracked(s);
851trace2_region_leave("status", "untracked", s->repo);
852
853wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
854if (s->state.merge_in_progress && !has_unmerged(s))
855s->committable = 1;
856}
857
858void wt_status_collect_free_buffers(struct wt_status *s)
859{
860wt_status_state_free_buffers(&s->state);
861}
862
863void wt_status_state_free_buffers(struct wt_status_state *state)
864{
865FREE_AND_NULL(state->branch);
866FREE_AND_NULL(state->onto);
867FREE_AND_NULL(state->detached_from);
868FREE_AND_NULL(state->bisecting_from);
869}
870
871static void wt_longstatus_print_unmerged(struct wt_status *s)
872{
873int shown_header = 0;
874int i;
875
876for (i = 0; i < s->change.nr; i++) {
877struct wt_status_change_data *d;
878struct string_list_item *it;
879it = &(s->change.items[i]);
880d = it->util;
881if (!d->stagemask)
882continue;
883if (!shown_header) {
884wt_longstatus_print_unmerged_header(s);
885shown_header = 1;
886}
887wt_longstatus_print_unmerged_data(s, it);
888}
889if (shown_header)
890wt_longstatus_print_trailer(s);
891
892}
893
894static void wt_longstatus_print_updated(struct wt_status *s)
895{
896int shown_header = 0;
897int i;
898
899for (i = 0; i < s->change.nr; i++) {
900struct wt_status_change_data *d;
901struct string_list_item *it;
902it = &(s->change.items[i]);
903d = it->util;
904if (!d->index_status ||
905d->index_status == DIFF_STATUS_UNMERGED)
906continue;
907if (!shown_header) {
908wt_longstatus_print_cached_header(s);
909shown_header = 1;
910}
911wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
912}
913if (shown_header)
914wt_longstatus_print_trailer(s);
915}
916
917/*
918* -1 : has delete
919* 0 : no change
920* 1 : some change but no delete
921*/
922static int wt_status_check_worktree_changes(struct wt_status *s,
923int *dirty_submodules)
924{
925int i;
926int changes = 0;
927
928*dirty_submodules = 0;
929
930for (i = 0; i < s->change.nr; i++) {
931struct wt_status_change_data *d;
932d = s->change.items[i].util;
933if (!d->worktree_status ||
934d->worktree_status == DIFF_STATUS_UNMERGED)
935continue;
936if (!changes)
937changes = 1;
938if (d->dirty_submodule)
939*dirty_submodules = 1;
940if (d->worktree_status == DIFF_STATUS_DELETED)
941changes = -1;
942}
943return changes;
944}
945
946static void wt_longstatus_print_changed(struct wt_status *s)
947{
948int i, dirty_submodules;
949int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
950
951if (!worktree_changes)
952return;
953
954wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
955
956for (i = 0; i < s->change.nr; i++) {
957struct wt_status_change_data *d;
958struct string_list_item *it;
959it = &(s->change.items[i]);
960d = it->util;
961if (!d->worktree_status ||
962d->worktree_status == DIFF_STATUS_UNMERGED)
963continue;
964wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
965}
966wt_longstatus_print_trailer(s);
967}
968
969static int stash_count_refs(struct object_id *ooid UNUSED,
970struct object_id *noid UNUSED,
971const char *email UNUSED,
972timestamp_t timestamp UNUSED, int tz UNUSED,
973const char *message UNUSED, void *cb_data)
974{
975int *c = cb_data;
976(*c)++;
977return 0;
978}
979
980static int count_stash_entries(void)
981{
982int n = 0;
983refs_for_each_reflog_ent(get_main_ref_store(the_repository),
984"refs/stash", stash_count_refs, &n);
985return n;
986}
987
988static void wt_longstatus_print_stash_summary(struct wt_status *s)
989{
990int stash_count = count_stash_entries();
991
992if (stash_count > 0)
993status_printf_ln(s, GIT_COLOR_NORMAL,
994Q_("Your stash currently has %d entry",
995"Your stash currently has %d entries", stash_count),
996stash_count);
997}
998
999static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
1000{
1001struct child_process sm_summary = CHILD_PROCESS_INIT;
1002struct strbuf cmd_stdout = STRBUF_INIT;
1003struct strbuf summary = STRBUF_INIT;
1004char *summary_content;
1005
1006strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1007
1008strvec_push(&sm_summary.args, "submodule");
1009strvec_push(&sm_summary.args, "summary");
1010strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1011strvec_push(&sm_summary.args, "--for-status");
1012strvec_push(&sm_summary.args, "--summary-limit");
1013strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1014if (!uncommitted)
1015strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1016
1017sm_summary.git_cmd = 1;
1018sm_summary.no_stdin = 1;
1019
1020capture_command(&sm_summary, &cmd_stdout, 1024);
1021
1022/* prepend header, only if there's an actual output */
1023if (cmd_stdout.len) {
1024if (uncommitted)
1025strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1026else
1027strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1028strbuf_addstr(&summary, "\n\n");
1029}
1030strbuf_addbuf(&summary, &cmd_stdout);
1031strbuf_release(&cmd_stdout);
1032
1033if (s->display_comment_prefix) {
1034size_t len;
1035summary_content = strbuf_detach(&summary, &len);
1036strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1037free(summary_content);
1038}
1039
1040fputs(summary.buf, s->fp);
1041strbuf_release(&summary);
1042}
1043
1044static void wt_longstatus_print_other(struct wt_status *s,
1045struct string_list *l,
1046const char *what,
1047const char *how)
1048{
1049int i;
1050struct strbuf buf = STRBUF_INIT;
1051static struct string_list output = STRING_LIST_INIT_DUP;
1052struct column_options copts;
1053
1054if (!l->nr)
1055return;
1056
1057wt_longstatus_print_other_header(s, what, how);
1058
1059for (i = 0; i < l->nr; i++) {
1060struct string_list_item *it;
1061const char *path;
1062it = &(l->items[i]);
1063path = quote_path(it->string, s->prefix, &buf, 0);
1064if (column_active(s->colopts)) {
1065string_list_append(&output, path);
1066continue;
1067}
1068status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1069status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1070"%s\n", path);
1071}
1072
1073strbuf_release(&buf);
1074if (!column_active(s->colopts))
1075goto conclude;
1076
1077strbuf_addf(&buf, "%s%s\t%s",
1078color(WT_STATUS_HEADER, s),
1079s->display_comment_prefix ? "#" : "",
1080color(WT_STATUS_UNTRACKED, s));
1081memset(&copts, 0, sizeof(copts));
1082copts.padding = 1;
1083copts.indent = buf.buf;
1084if (want_color(s->use_color))
1085copts.nl = GIT_COLOR_RESET "\n";
1086print_columns(&output, s->colopts, &copts);
1087string_list_clear(&output, 0);
1088strbuf_release(&buf);
1089conclude:
1090status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1091}
1092
1093size_t wt_status_locate_end(const char *s, size_t len)
1094{
1095const char *p;
1096struct strbuf pattern = STRBUF_INIT;
1097
1098strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line);
1099if (starts_with(s, pattern.buf + 1))
1100len = 0;
1101else if ((p = strstr(s, pattern.buf))) {
1102size_t newlen = p - s + 1;
1103if (newlen < len)
1104len = newlen;
1105}
1106strbuf_release(&pattern);
1107return len;
1108}
1109
1110void wt_status_append_cut_line(struct strbuf *buf)
1111{
1112const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1113
1114strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1115strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1116}
1117
1118void wt_status_add_cut_line(struct wt_status *s)
1119{
1120struct strbuf buf = STRBUF_INIT;
1121
1122if (s->added_cut_line)
1123return;
1124s->added_cut_line = 1;
1125wt_status_append_cut_line(&buf);
1126fputs(buf.buf, s->fp);
1127strbuf_release(&buf);
1128}
1129
1130static void wt_longstatus_print_verbose(struct wt_status *s)
1131{
1132struct rev_info rev;
1133struct setup_revision_opt opt;
1134int dirty_submodules;
1135const char *c = color(WT_STATUS_HEADER, s);
1136
1137repo_init_revisions(s->repo, &rev, NULL);
1138rev.diffopt.flags.allow_textconv = 1;
1139rev.diffopt.ita_invisible_in_index = 1;
1140
1141memset(&opt, 0, sizeof(opt));
1142opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
1143setup_revisions(0, NULL, &rev, &opt);
1144
1145rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1146rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1147rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1148rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1149rev.diffopt.file = s->fp;
1150rev.diffopt.close_file = 0;
1151/*
1152* If we're not going to stdout, then we definitely don't
1153* want color, since we are going to the commit message
1154* file (and even the "auto" setting won't work, since it
1155* will have checked isatty on stdout). But we then do want
1156* to insert the scissor line here to reliably remove the
1157* diff before committing, if we didn't already include one
1158* before.
1159*/
1160if (s->fp != stdout) {
1161rev.diffopt.use_color = 0;
1162wt_status_add_cut_line(s);
1163}
1164if (s->verbose > 1 && s->committable) {
1165/* print_updated() printed a header, so do we */
1166if (s->fp != stdout)
1167wt_longstatus_print_trailer(s);
1168status_printf_ln(s, c, _("Changes to be committed:"));
1169rev.diffopt.a_prefix = "c/";
1170rev.diffopt.b_prefix = "i/";
1171} /* else use prefix as per user config */
1172run_diff_index(&rev, DIFF_INDEX_CACHED);
1173if (s->verbose > 1 &&
1174wt_status_check_worktree_changes(s, &dirty_submodules)) {
1175status_printf_ln(s, c,
1176"--------------------------------------------------");
1177status_printf_ln(s, c, _("Changes not staged for commit:"));
1178setup_work_tree();
1179rev.diffopt.a_prefix = "i/";
1180rev.diffopt.b_prefix = "w/";
1181run_diff_files(&rev, 0);
1182}
1183release_revisions(&rev);
1184}
1185
1186static void wt_longstatus_print_tracking(struct wt_status *s)
1187{
1188struct strbuf sb = STRBUF_INIT;
1189const char *cp, *ep, *branch_name;
1190struct branch *branch;
1191uint64_t t_begin = 0;
1192
1193assert(s->branch && !s->is_initial);
1194if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1195return;
1196branch = branch_get(branch_name);
1197
1198t_begin = getnanotime();
1199
1200if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1201!s->commit_template))
1202return;
1203
1204if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1205s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1206uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1207if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1208strbuf_addf(&sb, _("\n"
1209"It took %.2f seconds to compute the branch ahead/behind values.\n"
1210"You can use '--no-ahead-behind' to avoid this.\n"),
1211t_delta_in_ms / 1000.0);
1212}
1213}
1214
1215for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1216color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1217"%s%s%.*s",
1218s->display_comment_prefix ? comment_line_str : "",
1219s->display_comment_prefix ? " " : "",
1220(int)(ep - cp), cp);
1221if (s->display_comment_prefix)
1222color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
1223comment_line_str);
1224else
1225fputs("\n", s->fp);
1226strbuf_release(&sb);
1227}
1228
1229static int uf_was_slow(struct wt_status *s)
1230{
1231if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1232s->untracked_in_ms = 3250;
1233return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1234}
1235
1236static void show_merge_in_progress(struct wt_status *s,
1237const char *color)
1238{
1239if (has_unmerged(s)) {
1240status_printf_ln(s, color, _("You have unmerged paths."));
1241if (s->hints) {
1242status_printf_ln(s, color,
1243_(" (fix conflicts and run \"git commit\")"));
1244status_printf_ln(s, color,
1245_(" (use \"git merge --abort\" to abort the merge)"));
1246}
1247} else {
1248status_printf_ln(s, color,
1249_("All conflicts fixed but you are still merging."));
1250if (s->hints)
1251status_printf_ln(s, color,
1252_(" (use \"git commit\" to conclude merge)"));
1253}
1254wt_longstatus_print_trailer(s);
1255}
1256
1257static void show_am_in_progress(struct wt_status *s,
1258const char *color)
1259{
1260int am_empty_patch;
1261
1262status_printf_ln(s, color,
1263_("You are in the middle of an am session."));
1264if (s->state.am_empty_patch)
1265status_printf_ln(s, color,
1266_("The current patch is empty."));
1267if (s->hints) {
1268am_empty_patch = s->state.am_empty_patch;
1269if (!am_empty_patch)
1270status_printf_ln(s, color,
1271_(" (fix conflicts and then run \"git am --continue\")"));
1272status_printf_ln(s, color,
1273_(" (use \"git am --skip\" to skip this patch)"));
1274if (am_empty_patch)
1275status_printf_ln(s, color,
1276_(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1277status_printf_ln(s, color,
1278_(" (use \"git am --abort\" to restore the original branch)"));
1279}
1280wt_longstatus_print_trailer(s);
1281}
1282
1283static char *read_line_from_git_path(const char *filename)
1284{
1285struct strbuf buf = STRBUF_INIT;
1286FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1287
1288if (!fp) {
1289strbuf_release(&buf);
1290return NULL;
1291}
1292strbuf_getline_lf(&buf, fp);
1293if (!fclose(fp)) {
1294return strbuf_detach(&buf, NULL);
1295} else {
1296strbuf_release(&buf);
1297return NULL;
1298}
1299}
1300
1301static int split_commit_in_progress(struct wt_status *s)
1302{
1303int split_in_progress = 0;
1304struct object_id head_oid, orig_head_oid;
1305char *rebase_amend, *rebase_orig_head;
1306int head_flags, orig_head_flags;
1307
1308if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1309!s->branch || strcmp(s->branch, "HEAD"))
1310return 0;
1311
1312if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1313&head_oid, &head_flags) ||
1314refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1315&orig_head_oid, &orig_head_flags))
1316return 0;
1317if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1318return 0;
1319
1320rebase_amend = read_line_from_git_path("rebase-merge/amend");
1321rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1322
1323if (!rebase_amend || !rebase_orig_head)
1324; /* fall through, no split in progress */
1325else if (!strcmp(rebase_amend, rebase_orig_head))
1326split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1327else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
1328split_in_progress = 1;
1329
1330free(rebase_amend);
1331free(rebase_orig_head);
1332
1333return split_in_progress;
1334}
1335
1336/*
1337* Turn
1338* "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1339* into
1340* "pick d6a2f03 some message"
1341*
1342* The function assumes that the line does not contain useless spaces
1343* before or after the command.
1344*/
1345static void abbrev_oid_in_line(struct strbuf *line)
1346{
1347struct strbuf **split;
1348int i;
1349
1350if (starts_with(line->buf, "exec ") ||
1351starts_with(line->buf, "x ") ||
1352starts_with(line->buf, "label ") ||
1353starts_with(line->buf, "l "))
1354return;
1355
1356split = strbuf_split_max(line, ' ', 3);
1357if (split[0] && split[1]) {
1358struct object_id oid;
1359
1360/*
1361* strbuf_split_max left a space. Trim it and re-add
1362* it after abbreviation.
1363*/
1364strbuf_trim(split[1]);
1365if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1366strbuf_reset(split[1]);
1367strbuf_add_unique_abbrev(split[1], &oid,
1368DEFAULT_ABBREV);
1369strbuf_addch(split[1], ' ');
1370strbuf_reset(line);
1371for (i = 0; split[i]; i++)
1372strbuf_addbuf(line, split[i]);
1373}
1374}
1375strbuf_list_free(split);
1376}
1377
1378static int read_rebase_todolist(const char *fname, struct string_list *lines)
1379{
1380struct strbuf line = STRBUF_INIT;
1381FILE *f = fopen(git_path("%s", fname), "r");
1382
1383if (!f) {
1384if (errno == ENOENT)
1385return -1;
1386die_errno("Could not open file %s for reading",
1387git_path("%s", fname));
1388}
1389while (!strbuf_getline_lf(&line, f)) {
1390if (starts_with(line.buf, comment_line_str))
1391continue;
1392strbuf_trim(&line);
1393if (!line.len)
1394continue;
1395abbrev_oid_in_line(&line);
1396string_list_append(lines, line.buf);
1397}
1398fclose(f);
1399strbuf_release(&line);
1400return 0;
1401}
1402
1403static void show_rebase_information(struct wt_status *s,
1404const char *color)
1405{
1406if (s->state.rebase_interactive_in_progress) {
1407int i;
1408int nr_lines_to_show = 2;
1409
1410struct string_list have_done = STRING_LIST_INIT_DUP;
1411struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1412
1413read_rebase_todolist("rebase-merge/done", &have_done);
1414if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1415&yet_to_do))
1416status_printf_ln(s, color,
1417_("git-rebase-todo is missing."));
1418if (have_done.nr == 0)
1419status_printf_ln(s, color, _("No commands done."));
1420else {
1421status_printf_ln(s, color,
1422Q_("Last command done (%"PRIuMAX" command done):",
1423"Last commands done (%"PRIuMAX" commands done):",
1424have_done.nr),
1425(uintmax_t)have_done.nr);
1426for (i = (have_done.nr > nr_lines_to_show)
1427? have_done.nr - nr_lines_to_show : 0;
1428i < have_done.nr;
1429i++)
1430status_printf_ln(s, color, " %s", have_done.items[i].string);
1431if (have_done.nr > nr_lines_to_show && s->hints)
1432status_printf_ln(s, color,
1433_(" (see more in file %s)"), git_path("rebase-merge/done"));
1434}
1435
1436if (yet_to_do.nr == 0)
1437status_printf_ln(s, color,
1438_("No commands remaining."));
1439else {
1440status_printf_ln(s, color,
1441Q_("Next command to do (%"PRIuMAX" remaining command):",
1442"Next commands to do (%"PRIuMAX" remaining commands):",
1443yet_to_do.nr),
1444(uintmax_t)yet_to_do.nr);
1445for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1446status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1447if (s->hints)
1448status_printf_ln(s, color,
1449_(" (use \"git rebase --edit-todo\" to view and edit)"));
1450}
1451string_list_clear(&yet_to_do, 0);
1452string_list_clear(&have_done, 0);
1453}
1454}
1455
1456static void print_rebase_state(struct wt_status *s,
1457const char *color)
1458{
1459if (s->state.branch)
1460status_printf_ln(s, color,
1461_("You are currently rebasing branch '%s' on '%s'."),
1462s->state.branch,
1463s->state.onto);
1464else
1465status_printf_ln(s, color,
1466_("You are currently rebasing."));
1467}
1468
1469static void show_rebase_in_progress(struct wt_status *s,
1470const char *color)
1471{
1472struct stat st;
1473
1474show_rebase_information(s, color);
1475if (has_unmerged(s)) {
1476print_rebase_state(s, color);
1477if (s->hints) {
1478status_printf_ln(s, color,
1479_(" (fix conflicts and then run \"git rebase --continue\")"));
1480status_printf_ln(s, color,
1481_(" (use \"git rebase --skip\" to skip this patch)"));
1482status_printf_ln(s, color,
1483_(" (use \"git rebase --abort\" to check out the original branch)"));
1484}
1485} else if (s->state.rebase_in_progress ||
1486!stat(git_path_merge_msg(s->repo), &st)) {
1487print_rebase_state(s, color);
1488if (s->hints)
1489status_printf_ln(s, color,
1490_(" (all conflicts fixed: run \"git rebase --continue\")"));
1491} else if (split_commit_in_progress(s)) {
1492if (s->state.branch)
1493status_printf_ln(s, color,
1494_("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1495s->state.branch,
1496s->state.onto);
1497else
1498status_printf_ln(s, color,
1499_("You are currently splitting a commit during a rebase."));
1500if (s->hints)
1501status_printf_ln(s, color,
1502_(" (Once your working directory is clean, run \"git rebase --continue\")"));
1503} else {
1504if (s->state.branch)
1505status_printf_ln(s, color,
1506_("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1507s->state.branch,
1508s->state.onto);
1509else
1510status_printf_ln(s, color,
1511_("You are currently editing a commit during a rebase."));
1512if (s->hints && !s->amend) {
1513status_printf_ln(s, color,
1514_(" (use \"git commit --amend\" to amend the current commit)"));
1515status_printf_ln(s, color,
1516_(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1517}
1518}
1519wt_longstatus_print_trailer(s);
1520}
1521
1522static void show_cherry_pick_in_progress(struct wt_status *s,
1523const char *color)
1524{
1525if (is_null_oid(&s->state.cherry_pick_head_oid))
1526status_printf_ln(s, color,
1527_("Cherry-pick currently in progress."));
1528else
1529status_printf_ln(s, color,
1530_("You are currently cherry-picking commit %s."),
1531repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1532DEFAULT_ABBREV));
1533
1534if (s->hints) {
1535if (has_unmerged(s))
1536status_printf_ln(s, color,
1537_(" (fix conflicts and run \"git cherry-pick --continue\")"));
1538else if (is_null_oid(&s->state.cherry_pick_head_oid))
1539status_printf_ln(s, color,
1540_(" (run \"git cherry-pick --continue\" to continue)"));
1541else
1542status_printf_ln(s, color,
1543_(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1544status_printf_ln(s, color,
1545_(" (use \"git cherry-pick --skip\" to skip this patch)"));
1546status_printf_ln(s, color,
1547_(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1548}
1549wt_longstatus_print_trailer(s);
1550}
1551
1552static void show_revert_in_progress(struct wt_status *s,
1553const char *color)
1554{
1555if (is_null_oid(&s->state.revert_head_oid))
1556status_printf_ln(s, color,
1557_("Revert currently in progress."));
1558else
1559status_printf_ln(s, color,
1560_("You are currently reverting commit %s."),
1561repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1562DEFAULT_ABBREV));
1563if (s->hints) {
1564if (has_unmerged(s))
1565status_printf_ln(s, color,
1566_(" (fix conflicts and run \"git revert --continue\")"));
1567else if (is_null_oid(&s->state.revert_head_oid))
1568status_printf_ln(s, color,
1569_(" (run \"git revert --continue\" to continue)"));
1570else
1571status_printf_ln(s, color,
1572_(" (all conflicts fixed: run \"git revert --continue\")"));
1573status_printf_ln(s, color,
1574_(" (use \"git revert --skip\" to skip this patch)"));
1575status_printf_ln(s, color,
1576_(" (use \"git revert --abort\" to cancel the revert operation)"));
1577}
1578wt_longstatus_print_trailer(s);
1579}
1580
1581static void show_bisect_in_progress(struct wt_status *s,
1582const char *color)
1583{
1584if (s->state.bisecting_from)
1585status_printf_ln(s, color,
1586_("You are currently bisecting, started from branch '%s'."),
1587s->state.bisecting_from);
1588else
1589status_printf_ln(s, color,
1590_("You are currently bisecting."));
1591if (s->hints)
1592status_printf_ln(s, color,
1593_(" (use \"git bisect reset\" to get back to the original branch)"));
1594wt_longstatus_print_trailer(s);
1595}
1596
1597static void show_sparse_checkout_in_use(struct wt_status *s,
1598const char *color)
1599{
1600if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1601return;
1602
1603if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1604status_printf_ln(s, color, _("You are in a sparse checkout."));
1605else
1606status_printf_ln(s, color,
1607_("You are in a sparse checkout with %d%% of tracked files present."),
1608s->state.sparse_checkout_percentage);
1609wt_longstatus_print_trailer(s);
1610}
1611
1612/*
1613* Extract branch information from rebase/bisect
1614*/
1615static char *get_branch(const struct worktree *wt, const char *path)
1616{
1617struct strbuf sb = STRBUF_INIT;
1618struct object_id oid;
1619const char *branch_name;
1620
1621if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1622goto got_nothing;
1623
1624while (sb.len && sb.buf[sb.len - 1] == '\n')
1625strbuf_setlen(&sb, sb.len - 1);
1626if (!sb.len)
1627goto got_nothing;
1628if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1629strbuf_remove(&sb, 0, branch_name - sb.buf);
1630else if (starts_with(sb.buf, "refs/"))
1631;
1632else if (!get_oid_hex(sb.buf, &oid)) {
1633strbuf_reset(&sb);
1634strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1635} else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1636goto got_nothing;
1637else /* bisect */
1638;
1639return strbuf_detach(&sb, NULL);
1640
1641got_nothing:
1642strbuf_release(&sb);
1643return NULL;
1644}
1645
1646struct grab_1st_switch_cbdata {
1647struct strbuf buf;
1648struct object_id noid;
1649};
1650
1651static int grab_1st_switch(struct object_id *ooid UNUSED,
1652struct object_id *noid,
1653const char *email UNUSED,
1654timestamp_t timestamp UNUSED, int tz UNUSED,
1655const char *message, void *cb_data)
1656{
1657struct grab_1st_switch_cbdata *cb = cb_data;
1658const char *target = NULL, *end;
1659
1660if (!skip_prefix(message, "checkout: moving from ", &message))
1661return 0;
1662target = strstr(message, " to ");
1663if (!target)
1664return 0;
1665target += strlen(" to ");
1666strbuf_reset(&cb->buf);
1667oidcpy(&cb->noid, noid);
1668end = strchrnul(target, '\n');
1669strbuf_add(&cb->buf, target, end - target);
1670if (!strcmp(cb->buf.buf, "HEAD")) {
1671/* HEAD is relative. Resolve it to the right reflog entry. */
1672strbuf_reset(&cb->buf);
1673strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1674}
1675return 1;
1676}
1677
1678static void wt_status_get_detached_from(struct repository *r,
1679struct wt_status_state *state)
1680{
1681struct grab_1st_switch_cbdata cb;
1682struct commit *commit;
1683struct object_id oid;
1684char *ref = NULL;
1685
1686strbuf_init(&cb.buf, 0);
1687if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) {
1688strbuf_release(&cb.buf);
1689return;
1690}
1691
1692if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
16931) == 1 &&
1694/* oid is a commit? match without further lookup */
1695(oideq(&cb.noid, &oid) ||
1696/* perhaps oid is a tag, try to dereference to a commit */
1697((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1698oideq(&cb.noid, &commit->object.oid)))) {
1699const char *from = ref;
1700if (!skip_prefix(from, "refs/tags/", &from))
1701skip_prefix(from, "refs/remotes/", &from);
1702state->detached_from = xstrdup(from);
1703} else
1704state->detached_from =
1705xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1706oidcpy(&state->detached_oid, &cb.noid);
1707state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1708oideq(&oid, &state->detached_oid);
1709
1710free(ref);
1711strbuf_release(&cb.buf);
1712}
1713
1714int wt_status_check_rebase(const struct worktree *wt,
1715struct wt_status_state *state)
1716{
1717struct stat st;
1718
1719if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1720if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1721state->am_in_progress = 1;
1722if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1723state->am_empty_patch = 1;
1724} else {
1725state->rebase_in_progress = 1;
1726state->branch = get_branch(wt, "rebase-apply/head-name");
1727state->onto = get_branch(wt, "rebase-apply/onto");
1728}
1729} else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1730if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1731state->rebase_interactive_in_progress = 1;
1732else
1733state->rebase_in_progress = 1;
1734state->branch = get_branch(wt, "rebase-merge/head-name");
1735state->onto = get_branch(wt, "rebase-merge/onto");
1736} else
1737return 0;
1738return 1;
1739}
1740
1741int wt_status_check_bisect(const struct worktree *wt,
1742struct wt_status_state *state)
1743{
1744struct stat st;
1745
1746if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1747state->bisect_in_progress = 1;
1748state->bisecting_from = get_branch(wt, "BISECT_START");
1749return 1;
1750}
1751return 0;
1752}
1753
1754static void wt_status_check_sparse_checkout(struct repository *r,
1755struct wt_status_state *state)
1756{
1757int skip_worktree = 0;
1758int i;
1759
1760if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1761/*
1762* Don't compute percentage of checked out files if we
1763* aren't in a sparse checkout or would get division by 0.
1764*/
1765state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1766return;
1767}
1768
1769if (r->index->sparse_index) {
1770state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1771return;
1772}
1773
1774for (i = 0; i < r->index->cache_nr; i++) {
1775struct cache_entry *ce = r->index->cache[i];
1776if (ce_skip_worktree(ce))
1777skip_worktree++;
1778}
1779
1780state->sparse_checkout_percentage =
1781100 - (100 * skip_worktree)/r->index->cache_nr;
1782}
1783
1784void wt_status_get_state(struct repository *r,
1785struct wt_status_state *state,
1786int get_detached_from)
1787{
1788struct stat st;
1789struct object_id oid;
1790enum replay_action action;
1791
1792if (!stat(git_path_merge_head(r), &st)) {
1793wt_status_check_rebase(NULL, state);
1794state->merge_in_progress = 1;
1795} else if (wt_status_check_rebase(NULL, state)) {
1796; /* all set */
1797} else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1798!repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1799state->cherry_pick_in_progress = 1;
1800oidcpy(&state->cherry_pick_head_oid, &oid);
1801}
1802wt_status_check_bisect(NULL, state);
1803if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1804!repo_get_oid(r, "REVERT_HEAD", &oid)) {
1805state->revert_in_progress = 1;
1806oidcpy(&state->revert_head_oid, &oid);
1807}
1808if (!sequencer_get_last_command(r, &action)) {
1809if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1810state->cherry_pick_in_progress = 1;
1811oidcpy(&state->cherry_pick_head_oid, null_oid());
1812} else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1813state->revert_in_progress = 1;
1814oidcpy(&state->revert_head_oid, null_oid());
1815}
1816}
1817if (get_detached_from)
1818wt_status_get_detached_from(r, state);
1819wt_status_check_sparse_checkout(r, state);
1820}
1821
1822static void wt_longstatus_print_state(struct wt_status *s)
1823{
1824const char *state_color = color(WT_STATUS_HEADER, s);
1825struct wt_status_state *state = &s->state;
1826
1827if (state->merge_in_progress) {
1828if (state->rebase_interactive_in_progress) {
1829show_rebase_information(s, state_color);
1830fputs("\n", s->fp);
1831}
1832show_merge_in_progress(s, state_color);
1833} else if (state->am_in_progress)
1834show_am_in_progress(s, state_color);
1835else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1836show_rebase_in_progress(s, state_color);
1837else if (state->cherry_pick_in_progress)
1838show_cherry_pick_in_progress(s, state_color);
1839else if (state->revert_in_progress)
1840show_revert_in_progress(s, state_color);
1841if (state->bisect_in_progress)
1842show_bisect_in_progress(s, state_color);
1843
1844if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1845show_sparse_checkout_in_use(s, state_color);
1846}
1847
1848static void wt_longstatus_print(struct wt_status *s)
1849{
1850const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1851const char *branch_status_color = color(WT_STATUS_HEADER, s);
1852enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1853
1854if (s->branch) {
1855const char *on_what = _("On branch ");
1856const char *branch_name = s->branch;
1857if (!strcmp(branch_name, "HEAD")) {
1858branch_status_color = color(WT_STATUS_NOBRANCH, s);
1859if (s->state.rebase_in_progress ||
1860s->state.rebase_interactive_in_progress) {
1861if (s->state.rebase_interactive_in_progress)
1862on_what = _("interactive rebase in progress; onto ");
1863else
1864on_what = _("rebase in progress; onto ");
1865branch_name = s->state.onto;
1866} else if (s->state.detached_from) {
1867branch_name = s->state.detached_from;
1868if (s->state.detached_at)
1869on_what = _("HEAD detached at ");
1870else
1871on_what = _("HEAD detached from ");
1872} else {
1873branch_name = "";
1874on_what = _("Not currently on any branch.");
1875}
1876} else
1877skip_prefix(branch_name, "refs/heads/", &branch_name);
1878status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1879status_printf_more(s, branch_status_color, "%s", on_what);
1880status_printf_more(s, branch_color, "%s\n", branch_name);
1881if (!s->is_initial)
1882wt_longstatus_print_tracking(s);
1883}
1884
1885wt_longstatus_print_state(s);
1886
1887if (s->is_initial) {
1888status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1889status_printf_ln(s, color(WT_STATUS_HEADER, s),
1890s->commit_template
1891? _("Initial commit")
1892: _("No commits yet"));
1893status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1894}
1895
1896wt_longstatus_print_updated(s);
1897wt_longstatus_print_unmerged(s);
1898wt_longstatus_print_changed(s);
1899if (s->submodule_summary &&
1900(!s->ignore_submodule_arg ||
1901strcmp(s->ignore_submodule_arg, "all"))) {
1902wt_longstatus_print_submodule_summary(s, 0); /* staged */
1903wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1904}
1905if (s->show_untracked_files) {
1906wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1907if (s->show_ignored_mode)
1908wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1909if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1910status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1911if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1912status_printf_ln(s, GIT_COLOR_NORMAL,
1913_("It took %.2f seconds to enumerate untracked files,\n"
1914"but the results were cached, and subsequent runs may be faster."),
1915s->untracked_in_ms / 1000.0);
1916} else {
1917status_printf_ln(s, GIT_COLOR_NORMAL,
1918_("It took %.2f seconds to enumerate untracked files."),
1919s->untracked_in_ms / 1000.0);
1920}
1921status_printf_ln(s, GIT_COLOR_NORMAL,
1922_("See 'git help status' for information on how to improve this."));
1923status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1924}
1925} else if (s->committable)
1926status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1927s->hints
1928? _(" (use -u option to show untracked files)") : "");
1929
1930if (s->verbose)
1931wt_longstatus_print_verbose(s);
1932if (!s->committable) {
1933if (s->amend)
1934status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1935else if (s->nowarn)
1936; /* nothing */
1937else if (s->workdir_dirty) {
1938if (s->hints)
1939fprintf(s->fp, _("no changes added to commit "
1940"(use \"git add\" and/or "
1941"\"git commit -a\")\n"));
1942else
1943fprintf(s->fp, _("no changes added to "
1944"commit\n"));
1945} else if (s->untracked.nr) {
1946if (s->hints)
1947fprintf(s->fp, _("nothing added to commit but "
1948"untracked files present (use "
1949"\"git add\" to track)\n"));
1950else
1951fprintf(s->fp, _("nothing added to commit but "
1952"untracked files present\n"));
1953} else if (s->is_initial) {
1954if (s->hints)
1955fprintf(s->fp, _("nothing to commit (create/"
1956"copy files and use \"git "
1957"add\" to track)\n"));
1958else
1959fprintf(s->fp, _("nothing to commit\n"));
1960} else if (!s->show_untracked_files) {
1961if (s->hints)
1962fprintf(s->fp, _("nothing to commit (use -u to "
1963"show untracked files)\n"));
1964else
1965fprintf(s->fp, _("nothing to commit\n"));
1966} else
1967fprintf(s->fp, _("nothing to commit, working tree "
1968"clean\n"));
1969}
1970if(s->show_stash)
1971wt_longstatus_print_stash_summary(s);
1972}
1973
1974static void wt_shortstatus_unmerged(struct string_list_item *it,
1975struct wt_status *s)
1976{
1977struct wt_status_change_data *d = it->util;
1978const char *how = "??";
1979
1980switch (d->stagemask) {
1981case 1: how = "DD"; break; /* both deleted */
1982case 2: how = "AU"; break; /* added by us */
1983case 3: how = "UD"; break; /* deleted by them */
1984case 4: how = "UA"; break; /* added by them */
1985case 5: how = "DU"; break; /* deleted by us */
1986case 6: how = "AA"; break; /* both added */
1987case 7: how = "UU"; break; /* both modified */
1988}
1989color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1990if (s->null_termination) {
1991fprintf(s->fp, " %s%c", it->string, 0);
1992} else {
1993struct strbuf onebuf = STRBUF_INIT;
1994const char *one;
1995one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1996fprintf(s->fp, " %s\n", one);
1997strbuf_release(&onebuf);
1998}
1999}
2000
2001static void wt_shortstatus_status(struct string_list_item *it,
2002struct wt_status *s)
2003{
2004struct wt_status_change_data *d = it->util;
2005
2006if (d->index_status)
2007color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
2008else
2009fputc(' ', s->fp);
2010if (d->worktree_status)
2011color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2012else
2013fputc(' ', s->fp);
2014fputc(' ', s->fp);
2015if (s->null_termination) {
2016fprintf(s->fp, "%s%c", it->string, 0);
2017if (d->rename_source)
2018fprintf(s->fp, "%s%c", d->rename_source, 0);
2019} else {
2020struct strbuf onebuf = STRBUF_INIT;
2021const char *one;
2022
2023if (d->rename_source) {
2024one = quote_path(d->rename_source, s->prefix, &onebuf,
2025QUOTE_PATH_QUOTE_SP);
2026fprintf(s->fp, "%s -> ", one);
2027strbuf_release(&onebuf);
2028}
2029one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2030fprintf(s->fp, "%s\n", one);
2031strbuf_release(&onebuf);
2032}
2033}
2034
2035static void wt_shortstatus_other(struct string_list_item *it,
2036struct wt_status *s, const char *sign)
2037{
2038if (s->null_termination) {
2039fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2040} else {
2041struct strbuf onebuf = STRBUF_INIT;
2042const char *one;
2043one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2044color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2045fprintf(s->fp, " %s\n", one);
2046strbuf_release(&onebuf);
2047}
2048}
2049
2050static void wt_shortstatus_print_tracking(struct wt_status *s)
2051{
2052struct branch *branch;
2053const char *header_color = color(WT_STATUS_HEADER, s);
2054const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2055const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2056
2057const char *base;
2058char *short_base;
2059const char *branch_name;
2060int num_ours, num_theirs, sti;
2061int upstream_is_gone = 0;
2062
2063color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2064
2065if (!s->branch)
2066return;
2067branch_name = s->branch;
2068
2069#define LABEL(string) (s->no_gettext ? (string) : _(string))
2070
2071if (s->is_initial)
2072color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2073
2074if (!strcmp(s->branch, "HEAD")) {
2075color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2076LABEL(N_("HEAD (no branch)")));
2077goto conclude;
2078}
2079
2080skip_prefix(branch_name, "refs/heads/", &branch_name);
2081
2082branch = branch_get(branch_name);
2083
2084color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2085
2086sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
20870, s->ahead_behind_flags);
2088if (sti < 0) {
2089if (!base)
2090goto conclude;
2091
2092upstream_is_gone = 1;
2093}
2094
2095short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2096base, 0);
2097color_fprintf(s->fp, header_color, "...");
2098color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2099free(short_base);
2100
2101if (!upstream_is_gone && !sti)
2102goto conclude;
2103
2104color_fprintf(s->fp, header_color, " [");
2105if (upstream_is_gone) {
2106color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2107} else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2108color_fprintf(s->fp, header_color, LABEL(N_("different")));
2109} else if (!num_ours) {
2110color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2111color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2112} else if (!num_theirs) {
2113color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2114color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2115} else {
2116color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2117color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2118color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2119color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2120}
2121
2122color_fprintf(s->fp, header_color, "]");
2123conclude:
2124fputc(s->null_termination ? '\0' : '\n', s->fp);
2125}
2126
2127static void wt_shortstatus_print(struct wt_status *s)
2128{
2129struct string_list_item *it;
2130
2131if (s->show_branch)
2132wt_shortstatus_print_tracking(s);
2133
2134for_each_string_list_item(it, &s->change) {
2135struct wt_status_change_data *d = it->util;
2136
2137if (d->stagemask)
2138wt_shortstatus_unmerged(it, s);
2139else
2140wt_shortstatus_status(it, s);
2141}
2142for_each_string_list_item(it, &s->untracked)
2143wt_shortstatus_other(it, s, "??");
2144
2145for_each_string_list_item(it, &s->ignored)
2146wt_shortstatus_other(it, s, "!!");
2147}
2148
2149static void wt_porcelain_print(struct wt_status *s)
2150{
2151s->use_color = 0;
2152s->relative_paths = 0;
2153s->prefix = NULL;
2154s->no_gettext = 1;
2155wt_shortstatus_print(s);
2156}
2157
2158/*
2159* Print branch information for porcelain v2 output. These lines
2160* are printed when the '--branch' parameter is given.
2161*
2162* # branch.oid <commit><eol>
2163* # branch.head <head><eol>
2164* [# branch.upstream <upstream><eol>
2165* [# branch.ab +<ahead> -<behind><eol>]]
2166*
2167* <commit> ::= the current commit hash or the literal
2168* "(initial)" to indicate an initialized repo
2169* with no commits.
2170*
2171* <head> ::= <branch_name> the current branch name or
2172* "(detached)" literal when detached head or
2173* "(unknown)" when something is wrong.
2174*
2175* <upstream> ::= the upstream branch name, when set.
2176*
2177* <ahead> ::= integer ahead value or '?'.
2178*
2179* <behind> ::= integer behind value or '?'.
2180*
2181* The end-of-line is defined by the -z flag.
2182*
2183* <eol> ::= NUL when -z,
2184* LF when NOT -z.
2185*
2186* When an upstream is set and present, the 'branch.ab' line will
2187* be printed with the ahead/behind counts for the branch and the
2188* upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2189* are different, '?' will be substituted for the actual count.
2190*/
2191static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2192{
2193struct branch *branch;
2194const char *base;
2195const char *branch_name;
2196int ab_info, nr_ahead, nr_behind;
2197char eol = s->null_termination ? '\0' : '\n';
2198
2199fprintf(s->fp, "# branch.oid %s%c",
2200(s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2201eol);
2202
2203if (!s->branch)
2204fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2205else {
2206if (!strcmp(s->branch, "HEAD")) {
2207fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2208
2209if (s->state.rebase_in_progress ||
2210s->state.rebase_interactive_in_progress)
2211branch_name = s->state.onto;
2212else if (s->state.detached_from)
2213branch_name = s->state.detached_from;
2214else
2215branch_name = "";
2216} else {
2217branch_name = NULL;
2218skip_prefix(s->branch, "refs/heads/", &branch_name);
2219
2220fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2221}
2222
2223/* Lookup stats on the upstream tracking branch, if set. */
2224branch = branch_get(branch_name);
2225base = NULL;
2226ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2227&base, 0, s->ahead_behind_flags);
2228if (base) {
2229base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2230base, 0);
2231fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2232free((char *)base);
2233
2234if (ab_info > 0) {
2235/* different */
2236if (nr_ahead || nr_behind)
2237fprintf(s->fp, "# branch.ab +%d -%d%c",
2238nr_ahead, nr_behind, eol);
2239else
2240fprintf(s->fp, "# branch.ab +? -?%c",
2241eol);
2242} else if (!ab_info) {
2243/* same */
2244fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2245}
2246}
2247}
2248}
2249
2250/*
2251* Print the stash count in a porcelain-friendly format
2252*/
2253static void wt_porcelain_v2_print_stash(struct wt_status *s)
2254{
2255int stash_count = count_stash_entries();
2256char eol = s->null_termination ? '\0' : '\n';
2257
2258if (stash_count > 0)
2259fprintf(s->fp, "# stash %d%c", stash_count, eol);
2260}
2261
2262/*
2263* Convert various submodule status values into a
2264* fixed-length string of characters in the buffer provided.
2265*/
2266static void wt_porcelain_v2_submodule_state(
2267struct wt_status_change_data *d,
2268char sub[5])
2269{
2270if (S_ISGITLINK(d->mode_head) ||
2271S_ISGITLINK(d->mode_index) ||
2272S_ISGITLINK(d->mode_worktree)) {
2273sub[0] = 'S';
2274sub[1] = d->new_submodule_commits ? 'C' : '.';
2275sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2276sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2277} else {
2278sub[0] = 'N';
2279sub[1] = '.';
2280sub[2] = '.';
2281sub[3] = '.';
2282}
2283sub[4] = 0;
2284}
2285
2286/*
2287* Fix-up changed entries before we print them.
2288*/
2289static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2290{
2291struct wt_status_change_data *d = it->util;
2292
2293if (!d->index_status) {
2294/*
2295* This entry is unchanged in the index (relative to the head).
2296* Therefore, the collect_updated_cb was never called for this
2297* entry (during the head-vs-index scan) and so the head column
2298* fields were never set.
2299*
2300* We must have data for the index column (from the
2301* index-vs-worktree scan (otherwise, this entry should not be
2302* in the list of changes)).
2303*
2304* Copy index column fields to the head column, so that our
2305* output looks complete.
2306*/
2307assert(d->mode_head == 0);
2308d->mode_head = d->mode_index;
2309oidcpy(&d->oid_head, &d->oid_index);
2310}
2311
2312if (!d->worktree_status) {
2313/*
2314* This entry is unchanged in the worktree (relative to the index).
2315* Therefore, the collect_changed_cb was never called for this entry
2316* (during the index-vs-worktree scan) and so the worktree column
2317* fields were never set.
2318*
2319* We must have data for the index column (from the head-vs-index
2320* scan).
2321*
2322* Copy the index column fields to the worktree column so that
2323* our output looks complete.
2324*
2325* Note that we only have a mode field in the worktree column
2326* because the scan code tries really hard to not have to compute it.
2327*/
2328assert(d->mode_worktree == 0);
2329d->mode_worktree = d->mode_index;
2330}
2331}
2332
2333/*
2334* Print porcelain v2 info for tracked entries with changes.
2335*/
2336static void wt_porcelain_v2_print_changed_entry(
2337struct string_list_item *it,
2338struct wt_status *s)
2339{
2340struct wt_status_change_data *d = it->util;
2341struct strbuf buf = STRBUF_INIT;
2342struct strbuf buf_from = STRBUF_INIT;
2343const char *path = NULL;
2344const char *path_from = NULL;
2345char key[3];
2346char submodule_token[5];
2347char sep_char, eol_char;
2348
2349wt_porcelain_v2_fix_up_changed(it);
2350wt_porcelain_v2_submodule_state(d, submodule_token);
2351
2352key[0] = d->index_status ? d->index_status : '.';
2353key[1] = d->worktree_status ? d->worktree_status : '.';
2354key[2] = 0;
2355
2356if (s->null_termination) {
2357/*
2358* In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2359* A single NUL character separates them.
2360*/
2361sep_char = '\0';
2362eol_char = '\0';
2363path = it->string;
2364path_from = d->rename_source;
2365} else {
2366/*
2367* Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2368* The source path is only present when necessary.
2369* A single TAB separates them (because paths can contain spaces
2370* which are not escaped and C-quoting does escape TAB characters).
2371*/
2372sep_char = '\t';
2373eol_char = '\n';
2374path = quote_path(it->string, s->prefix, &buf, 0);
2375if (d->rename_source)
2376path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2377}
2378
2379if (path_from)
2380fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2381key, submodule_token,
2382d->mode_head, d->mode_index, d->mode_worktree,
2383oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2384d->rename_status, d->rename_score,
2385path, sep_char, path_from, eol_char);
2386else
2387fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2388key, submodule_token,
2389d->mode_head, d->mode_index, d->mode_worktree,
2390oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2391path, eol_char);
2392
2393strbuf_release(&buf);
2394strbuf_release(&buf_from);
2395}
2396
2397/*
2398* Print porcelain v2 status info for unmerged entries.
2399*/
2400static void wt_porcelain_v2_print_unmerged_entry(
2401struct string_list_item *it,
2402struct wt_status *s)
2403{
2404struct wt_status_change_data *d = it->util;
2405struct index_state *istate = s->repo->index;
2406const struct cache_entry *ce;
2407struct strbuf buf_index = STRBUF_INIT;
2408const char *path_index = NULL;
2409int pos, stage, sum;
2410struct {
2411int mode;
2412struct object_id oid;
2413} stages[3];
2414const char *key;
2415char submodule_token[5];
2416char unmerged_prefix = 'u';
2417char eol_char = s->null_termination ? '\0' : '\n';
2418
2419wt_porcelain_v2_submodule_state(d, submodule_token);
2420
2421switch (d->stagemask) {
2422case 1: key = "DD"; break; /* both deleted */
2423case 2: key = "AU"; break; /* added by us */
2424case 3: key = "UD"; break; /* deleted by them */
2425case 4: key = "UA"; break; /* added by them */
2426case 5: key = "DU"; break; /* deleted by us */
2427case 6: key = "AA"; break; /* both added */
2428case 7: key = "UU"; break; /* both modified */
2429default:
2430BUG("unhandled unmerged status %x", d->stagemask);
2431}
2432
2433/*
2434* Disregard d.aux.porcelain_v2 data that we accumulated
2435* for the head and index columns during the scans and
2436* replace with the actual stage data.
2437*
2438* Note that this is a last-one-wins for each the individual
2439* stage [123] columns in the event of multiple cache entries
2440* for same stage.
2441*/
2442memset(stages, 0, sizeof(stages));
2443sum = 0;
2444pos = index_name_pos(istate, it->string, strlen(it->string));
2445assert(pos < 0);
2446pos = -pos-1;
2447while (pos < istate->cache_nr) {
2448ce = istate->cache[pos++];
2449stage = ce_stage(ce);
2450if (strcmp(ce->name, it->string) || !stage)
2451break;
2452stages[stage - 1].mode = ce->ce_mode;
2453oidcpy(&stages[stage - 1].oid, &ce->oid);
2454sum |= (1 << (stage - 1));
2455}
2456if (sum != d->stagemask)
2457BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2458
2459if (s->null_termination)
2460path_index = it->string;
2461else
2462path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2463
2464fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2465unmerged_prefix, key, submodule_token,
2466stages[0].mode, /* stage 1 */
2467stages[1].mode, /* stage 2 */
2468stages[2].mode, /* stage 3 */
2469d->mode_worktree,
2470oid_to_hex(&stages[0].oid), /* stage 1 */
2471oid_to_hex(&stages[1].oid), /* stage 2 */
2472oid_to_hex(&stages[2].oid), /* stage 3 */
2473path_index,
2474eol_char);
2475
2476strbuf_release(&buf_index);
2477}
2478
2479/*
2480* Print porcelain V2 status info for untracked and ignored entries.
2481*/
2482static void wt_porcelain_v2_print_other(
2483struct string_list_item *it,
2484struct wt_status *s,
2485char prefix)
2486{
2487struct strbuf buf = STRBUF_INIT;
2488const char *path;
2489char eol_char;
2490
2491if (s->null_termination) {
2492path = it->string;
2493eol_char = '\0';
2494} else {
2495path = quote_path(it->string, s->prefix, &buf, 0);
2496eol_char = '\n';
2497}
2498
2499fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2500
2501strbuf_release(&buf);
2502}
2503
2504/*
2505* Print porcelain V2 status.
2506*
2507* [<v2_branch>]
2508* [<v2_changed_items>]*
2509* [<v2_unmerged_items>]*
2510* [<v2_untracked_items>]*
2511* [<v2_ignored_items>]*
2512*
2513*/
2514static void wt_porcelain_v2_print(struct wt_status *s)
2515{
2516struct wt_status_change_data *d;
2517struct string_list_item *it;
2518int i;
2519
2520if (s->show_branch)
2521wt_porcelain_v2_print_tracking(s);
2522
2523if (s->show_stash)
2524wt_porcelain_v2_print_stash(s);
2525
2526for (i = 0; i < s->change.nr; i++) {
2527it = &(s->change.items[i]);
2528d = it->util;
2529if (!d->stagemask)
2530wt_porcelain_v2_print_changed_entry(it, s);
2531}
2532
2533for (i = 0; i < s->change.nr; i++) {
2534it = &(s->change.items[i]);
2535d = it->util;
2536if (d->stagemask)
2537wt_porcelain_v2_print_unmerged_entry(it, s);
2538}
2539
2540for (i = 0; i < s->untracked.nr; i++) {
2541it = &(s->untracked.items[i]);
2542wt_porcelain_v2_print_other(it, s, '?');
2543}
2544
2545for (i = 0; i < s->ignored.nr; i++) {
2546it = &(s->ignored.items[i]);
2547wt_porcelain_v2_print_other(it, s, '!');
2548}
2549}
2550
2551void wt_status_print(struct wt_status *s)
2552{
2553trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2554trace2_data_intmax("status", s->repo, "count/untracked",
2555s->untracked.nr);
2556trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2557
2558trace2_region_enter("status", "print", s->repo);
2559
2560switch (s->status_format) {
2561case STATUS_FORMAT_SHORT:
2562wt_shortstatus_print(s);
2563break;
2564case STATUS_FORMAT_PORCELAIN:
2565wt_porcelain_print(s);
2566break;
2567case STATUS_FORMAT_PORCELAIN_V2:
2568wt_porcelain_v2_print(s);
2569break;
2570case STATUS_FORMAT_UNSPECIFIED:
2571BUG("finalize_deferred_config() should have been called");
2572break;
2573case STATUS_FORMAT_NONE:
2574case STATUS_FORMAT_LONG:
2575wt_longstatus_print(s);
2576break;
2577}
2578
2579trace2_region_leave("status", "print", s->repo);
2580}
2581
2582/**
2583* Returns 1 if there are unstaged changes, 0 otherwise.
2584*/
2585int has_unstaged_changes(struct repository *r, int ignore_submodules)
2586{
2587struct rev_info rev_info;
2588int result;
2589
2590repo_init_revisions(r, &rev_info, NULL);
2591if (ignore_submodules) {
2592rev_info.diffopt.flags.ignore_submodules = 1;
2593rev_info.diffopt.flags.override_submodule_config = 1;
2594}
2595rev_info.diffopt.flags.quick = 1;
2596diff_setup_done(&rev_info.diffopt);
2597run_diff_files(&rev_info, 0);
2598result = diff_result_code(&rev_info.diffopt);
2599release_revisions(&rev_info);
2600return result;
2601}
2602
2603/**
2604* Returns 1 if there are uncommitted changes, 0 otherwise.
2605*/
2606int has_uncommitted_changes(struct repository *r,
2607int ignore_submodules)
2608{
2609struct rev_info rev_info;
2610int result;
2611
2612if (is_index_unborn(r->index))
2613return 0;
2614
2615repo_init_revisions(r, &rev_info, NULL);
2616if (ignore_submodules)
2617rev_info.diffopt.flags.ignore_submodules = 1;
2618rev_info.diffopt.flags.quick = 1;
2619
2620add_head_to_pending(&rev_info);
2621if (!rev_info.pending.nr) {
2622/*
2623* We have no head (or it's corrupt); use the empty tree,
2624* which will complain if the index is non-empty.
2625*/
2626struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2627add_pending_object(&rev_info, &tree->object, "");
2628}
2629
2630diff_setup_done(&rev_info.diffopt);
2631run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2632result = diff_result_code(&rev_info.diffopt);
2633release_revisions(&rev_info);
2634return result;
2635}
2636
2637/**
2638* If the work tree has unstaged or uncommitted changes, dies with the
2639* appropriate message.
2640*/
2641int require_clean_work_tree(struct repository *r,
2642const char *action,
2643const char *hint,
2644int ignore_submodules,
2645int gently)
2646{
2647struct lock_file lock_file = LOCK_INIT;
2648int err = 0, fd;
2649
2650fd = repo_hold_locked_index(r, &lock_file, 0);
2651refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2652if (0 <= fd)
2653repo_update_index_if_able(r, &lock_file);
2654rollback_lock_file(&lock_file);
2655
2656if (has_unstaged_changes(r, ignore_submodules)) {
2657/* TRANSLATORS: the action is e.g. "pull with rebase" */
2658error(_("cannot %s: You have unstaged changes."), _(action));
2659err = 1;
2660}
2661
2662if (has_uncommitted_changes(r, ignore_submodules)) {
2663if (err)
2664error(_("additionally, your index contains uncommitted changes."));
2665else
2666error(_("cannot %s: Your index contains uncommitted changes."),
2667_(action));
2668err = 1;
2669}
2670
2671if (err) {
2672if (hint) {
2673if (!*hint)
2674BUG("empty hint passed to require_clean_work_tree();"
2675" use NULL instead");
2676error("%s", hint);
2677}
2678if (!gently)
2679exit(128);
2680}
2681
2682return err;
2683}
2684