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