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