10 #include "run-command.h"
11 #include "argv-array.h"
14 #include "submodule.h"
21 static const char cut_line[] =
22 "------------------------ >8 ------------------------\n";
24 static char default_wt_status_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
30 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
31 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
33 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
36 static const char *color(int slot, struct wt_status *s)
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
41 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
46 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
53 strbuf_vaddf(&sb, fmt, ap);
55 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
58 strbuf_addch(&sb, ' ');
60 color_print_strbuf(s->fp, color, &sb);
62 fprintf(s->fp, "%s", trail);
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
69 strbuf_reset(&linebuf);
70 if (at_bol && s->display_comment_prefix) {
71 strbuf_addch(&linebuf, comment_line_char);
72 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
76 strbuf_add(&linebuf, line, eol - line);
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
92 void status_printf_ln(struct wt_status *s, const char *color,
98 status_vprintf(s, 1, color, fmt, ap, "\n");
102 void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
108 status_vprintf(s, 1, color, fmt, ap, NULL);
112 static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
118 status_vprintf(s, 0, color, fmt, ap, NULL);
122 void wt_status_prepare(struct wt_status *s)
124 memset(s, 0, sizeof(*s));
125 memcpy(s->color_palette, default_wt_status_colors,
126 sizeof(default_wt_status_colors));
127 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
129 s->relative_paths = 1;
130 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
131 s->reference = "HEAD";
133 s->index_file = get_index_file();
134 s->change.strdup_strings = 1;
135 s->untracked.strdup_strings = 1;
136 s->ignored.strdup_strings = 1;
137 s->show_branch = -1; /* unspecified */
139 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
140 s->display_comment_prefix = 0;
141 s->detect_rename = -1;
142 s->rename_score = -1;
143 s->rename_limit = -1;
146 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
149 int del_mod_conflict = 0;
150 int both_deleted = 0;
152 const char *c = color(WT_STATUS_HEADER, s);
154 status_printf_ln(s, c, _("Unmerged paths:"));
156 for (i = 0; i < s->change.nr; i++) {
157 struct string_list_item *it = &(s->change.items[i]);
158 struct wt_status_change_data *d = it->util;
160 switch (d->stagemask) {
168 del_mod_conflict = 1;
178 if (s->whence != FROM_COMMIT)
180 else if (!s->is_initial)
181 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
183 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
186 if (!del_mod_conflict)
187 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
189 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
190 } else if (!del_mod_conflict && !not_deleted) {
191 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
193 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
195 status_printf_ln(s, c, "%s", "");
198 static void wt_longstatus_print_cached_header(struct wt_status *s)
200 const char *c = color(WT_STATUS_HEADER, s);
202 status_printf_ln(s, c, _("Changes to be committed:"));
205 if (s->whence != FROM_COMMIT)
206 ; /* NEEDSWORK: use "git reset --unresolve"??? */
207 else if (!s->is_initial)
208 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
210 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
211 status_printf_ln(s, c, "%s", "");
214 static void wt_longstatus_print_dirty_header(struct wt_status *s,
216 int has_dirty_submodules)
218 const char *c = color(WT_STATUS_HEADER, s);
220 status_printf_ln(s, c, _("Changes not staged for commit:"));
224 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
226 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
227 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
228 if (has_dirty_submodules)
229 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
230 status_printf_ln(s, c, "%s", "");
233 static void wt_longstatus_print_other_header(struct wt_status *s,
237 const char *c = color(WT_STATUS_HEADER, s);
238 status_printf_ln(s, c, "%s:", what);
241 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
242 status_printf_ln(s, c, "%s", "");
245 static void wt_longstatus_print_trailer(struct wt_status *s)
247 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
250 #define quote_path quote_path_relative
252 static const char *wt_status_unmerged_status_string(int stagemask)
256 return _("both deleted:");
258 return _("added by us:");
260 return _("deleted by them:");
262 return _("added by them:");
264 return _("deleted by us:");
266 return _("both added:");
268 return _("both modified:");
270 BUG("unhandled unmerged status %x", stagemask);
274 static const char *wt_status_diff_status_string(int status)
277 case DIFF_STATUS_ADDED:
278 return _("new file:");
279 case DIFF_STATUS_COPIED:
281 case DIFF_STATUS_DELETED:
282 return _("deleted:");
283 case DIFF_STATUS_MODIFIED:
284 return _("modified:");
285 case DIFF_STATUS_RENAMED:
286 return _("renamed:");
287 case DIFF_STATUS_TYPE_CHANGED:
288 return _("typechange:");
289 case DIFF_STATUS_UNKNOWN:
290 return _("unknown:");
291 case DIFF_STATUS_UNMERGED:
292 return _("unmerged:");
298 static int maxwidth(const char *(*label)(int), int minval, int maxval)
302 for (i = minval; i <= maxval; i++) {
303 const char *s = label(i);
304 int len = s ? utf8_strwidth(s) : 0;
311 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
312 struct string_list_item *it)
314 const char *c = color(WT_STATUS_UNMERGED, s);
315 struct wt_status_change_data *d = it->util;
316 struct strbuf onebuf = STRBUF_INIT;
317 static char *padding;
318 static int label_width;
319 const char *one, *how;
323 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
324 label_width += strlen(" ");
325 padding = xmallocz(label_width);
326 memset(padding, ' ', label_width);
329 one = quote_path(it->string, s->prefix, &onebuf);
330 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
332 how = wt_status_unmerged_status_string(d->stagemask);
333 len = label_width - utf8_strwidth(how);
334 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
335 strbuf_release(&onebuf);
338 static void wt_longstatus_print_change_data(struct wt_status *s,
340 struct string_list_item *it)
342 struct wt_status_change_data *d = it->util;
343 const char *c = color(change_type, s);
347 const char *one, *two;
348 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
349 struct strbuf extra = STRBUF_INIT;
350 static char *padding;
351 static int label_width;
356 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
357 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
358 label_width += strlen(" ");
359 padding = xmallocz(label_width);
360 memset(padding, ' ', label_width);
363 one_name = two_name = it->string;
364 switch (change_type) {
365 case WT_STATUS_UPDATED:
366 status = d->index_status;
368 case WT_STATUS_CHANGED:
369 if (d->new_submodule_commits || d->dirty_submodule) {
370 strbuf_addstr(&extra, " (");
371 if (d->new_submodule_commits)
372 strbuf_addstr(&extra, _("new commits, "));
373 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
374 strbuf_addstr(&extra, _("modified content, "));
375 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
376 strbuf_addstr(&extra, _("untracked content, "));
377 strbuf_setlen(&extra, extra.len - 2);
378 strbuf_addch(&extra, ')');
380 status = d->worktree_status;
383 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
388 * Only pick up the rename it's relevant. If the rename is for
389 * the changed section and we're printing the updated section,
392 if (d->rename_status == status)
393 one_name = d->rename_source;
395 one = quote_path(one_name, s->prefix, &onebuf);
396 two = quote_path(two_name, s->prefix, &twobuf);
398 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
399 what = wt_status_diff_status_string(status);
401 BUG("unhandled diff status %c", status);
402 len = label_width - utf8_strwidth(what);
404 if (one_name != two_name)
405 status_printf_more(s, c, "%s%.*s%s -> %s",
406 what, len, padding, one, two);
408 status_printf_more(s, c, "%s%.*s%s",
409 what, len, padding, one);
411 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
412 strbuf_release(&extra);
414 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
415 strbuf_release(&onebuf);
416 strbuf_release(&twobuf);
419 static char short_submodule_status(struct wt_status_change_data *d)
421 if (d->new_submodule_commits)
423 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
425 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
427 return d->worktree_status;
430 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
431 struct diff_options *options,
434 struct wt_status *s = data;
439 s->workdir_dirty = 1;
440 for (i = 0; i < q->nr; i++) {
441 struct diff_filepair *p;
442 struct string_list_item *it;
443 struct wt_status_change_data *d;
446 it = string_list_insert(&s->change, p->two->path);
449 d = xcalloc(1, sizeof(*d));
452 if (!d->worktree_status)
453 d->worktree_status = p->status;
454 if (S_ISGITLINK(p->two->mode)) {
455 d->dirty_submodule = p->two->dirty_submodule;
456 d->new_submodule_commits = !!oidcmp(&p->one->oid,
458 if (s->status_format == STATUS_FORMAT_SHORT)
459 d->worktree_status = short_submodule_status(d);
463 case DIFF_STATUS_ADDED:
464 d->mode_worktree = p->two->mode;
467 case DIFF_STATUS_DELETED:
468 d->mode_index = p->one->mode;
469 oidcpy(&d->oid_index, &p->one->oid);
470 /* mode_worktree is zero for a delete. */
473 case DIFF_STATUS_COPIED:
474 case DIFF_STATUS_RENAMED:
475 if (d->rename_status)
476 BUG("multiple renames on the same target? how?");
477 d->rename_source = xstrdup(p->one->path);
478 d->rename_score = p->score * 100 / MAX_SCORE;
479 d->rename_status = p->status;
481 case DIFF_STATUS_MODIFIED:
482 case DIFF_STATUS_TYPE_CHANGED:
483 case DIFF_STATUS_UNMERGED:
484 d->mode_index = p->one->mode;
485 d->mode_worktree = p->two->mode;
486 oidcpy(&d->oid_index, &p->one->oid);
490 BUG("unhandled diff-files status '%c'", p->status);
497 static int unmerged_mask(const char *path)
500 const struct cache_entry *ce;
502 pos = cache_name_pos(path, strlen(path));
508 while (pos < active_nr) {
509 ce = active_cache[pos++];
510 if (strcmp(ce->name, path) || !ce_stage(ce))
512 mask |= (1 << (ce_stage(ce) - 1));
517 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
518 struct diff_options *options,
521 struct wt_status *s = data;
524 for (i = 0; i < q->nr; i++) {
525 struct diff_filepair *p;
526 struct string_list_item *it;
527 struct wt_status_change_data *d;
530 it = string_list_insert(&s->change, p->two->path);
533 d = xcalloc(1, sizeof(*d));
536 if (!d->index_status)
537 d->index_status = p->status;
539 case DIFF_STATUS_ADDED:
540 /* Leave {mode,oid}_head zero for an add. */
541 d->mode_index = p->two->mode;
542 oidcpy(&d->oid_index, &p->two->oid);
544 case DIFF_STATUS_DELETED:
545 d->mode_head = p->one->mode;
546 oidcpy(&d->oid_head, &p->one->oid);
547 /* Leave {mode,oid}_index zero for a delete. */
550 case DIFF_STATUS_COPIED:
551 case DIFF_STATUS_RENAMED:
552 if (d->rename_status)
553 BUG("multiple renames on the same target? how?");
554 d->rename_source = xstrdup(p->one->path);
555 d->rename_score = p->score * 100 / MAX_SCORE;
556 d->rename_status = p->status;
558 case DIFF_STATUS_MODIFIED:
559 case DIFF_STATUS_TYPE_CHANGED:
560 d->mode_head = p->one->mode;
561 d->mode_index = p->two->mode;
562 oidcpy(&d->oid_head, &p->one->oid);
563 oidcpy(&d->oid_index, &p->two->oid);
565 case DIFF_STATUS_UNMERGED:
566 d->stagemask = unmerged_mask(p->two->path);
568 * Don't bother setting {mode,oid}_{head,index} since the print
569 * code will output the stage values directly and not use the
570 * values in these fields.
575 BUG("unhandled diff-index status '%c'", p->status);
581 static void wt_status_collect_changes_worktree(struct wt_status *s)
585 init_revisions(&rev, NULL);
586 setup_revisions(0, NULL, &rev, NULL);
587 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
588 rev.diffopt.flags.dirty_submodules = 1;
589 rev.diffopt.ita_invisible_in_index = 1;
590 if (!s->show_untracked_files)
591 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
592 if (s->ignore_submodule_arg) {
593 rev.diffopt.flags.override_submodule_config = 1;
594 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
596 rev.diffopt.format_callback = wt_status_collect_changed_cb;
597 rev.diffopt.format_callback_data = s;
598 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
599 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
600 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
601 copy_pathspec(&rev.prune_data, &s->pathspec);
602 run_diff_files(&rev, 0);
605 static void wt_status_collect_changes_index(struct wt_status *s)
608 struct setup_revision_opt opt;
610 init_revisions(&rev, NULL);
611 memset(&opt, 0, sizeof(opt));
612 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
613 setup_revisions(0, NULL, &rev, &opt);
615 rev.diffopt.flags.override_submodule_config = 1;
616 rev.diffopt.ita_invisible_in_index = 1;
617 if (s->ignore_submodule_arg) {
618 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
621 * Unless the user did explicitly request a submodule ignore
622 * mode by passing a command line option we do not ignore any
623 * changed submodule SHA-1s when comparing index and HEAD, no
624 * matter what is configured. Otherwise the user won't be
625 * shown any submodules she manually added (and which are
626 * staged to be committed), which would be really confusing.
628 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
631 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
632 rev.diffopt.format_callback = wt_status_collect_updated_cb;
633 rev.diffopt.format_callback_data = s;
634 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
635 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
636 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
637 copy_pathspec(&rev.prune_data, &s->pathspec);
638 run_diff_index(&rev, 1);
641 static void wt_status_collect_changes_initial(struct wt_status *s)
645 for (i = 0; i < active_nr; i++) {
646 struct string_list_item *it;
647 struct wt_status_change_data *d;
648 const struct cache_entry *ce = active_cache[i];
650 if (!ce_path_match(ce, &s->pathspec, NULL))
652 if (ce_intent_to_add(ce))
654 it = string_list_insert(&s->change, ce->name);
657 d = xcalloc(1, sizeof(*d));
661 d->index_status = DIFF_STATUS_UNMERGED;
662 d->stagemask |= (1 << (ce_stage(ce) - 1));
664 * Don't bother setting {mode,oid}_{head,index} since the print
665 * code will output the stage values directly and not use the
666 * values in these fields.
669 d->index_status = DIFF_STATUS_ADDED;
670 /* Leave {mode,oid}_head zero for adds. */
671 d->mode_index = ce->ce_mode;
672 oidcpy(&d->oid_index, &ce->oid);
677 static void wt_status_collect_untracked(struct wt_status *s)
680 struct dir_struct dir;
681 uint64_t t_begin = getnanotime();
683 if (!s->show_untracked_files)
686 memset(&dir, 0, sizeof(dir));
687 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
689 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
690 if (s->show_ignored_mode) {
691 dir.flags |= DIR_SHOW_IGNORED_TOO;
693 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
694 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
696 dir.untracked = the_index.untracked;
699 setup_standard_excludes(&dir);
701 fill_directory(&dir, &the_index, &s->pathspec);
703 for (i = 0; i < dir.nr; i++) {
704 struct dir_entry *ent = dir.entries[i];
705 if (cache_name_is_other(ent->name, ent->len) &&
706 dir_path_match(ent, &s->pathspec, 0, NULL))
707 string_list_insert(&s->untracked, ent->name);
711 for (i = 0; i < dir.ignored_nr; i++) {
712 struct dir_entry *ent = dir.ignored[i];
713 if (cache_name_is_other(ent->name, ent->len) &&
714 dir_path_match(ent, &s->pathspec, 0, NULL))
715 string_list_insert(&s->ignored, ent->name);
721 clear_directory(&dir);
723 if (advice_status_u_option)
724 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
727 void wt_status_collect(struct wt_status *s)
729 wt_status_collect_changes_worktree(s);
732 wt_status_collect_changes_initial(s);
734 wt_status_collect_changes_index(s);
735 wt_status_collect_untracked(s);
738 static void wt_longstatus_print_unmerged(struct wt_status *s)
740 int shown_header = 0;
743 for (i = 0; i < s->change.nr; i++) {
744 struct wt_status_change_data *d;
745 struct string_list_item *it;
746 it = &(s->change.items[i]);
751 wt_longstatus_print_unmerged_header(s);
754 wt_longstatus_print_unmerged_data(s, it);
757 wt_longstatus_print_trailer(s);
761 static void wt_longstatus_print_updated(struct wt_status *s)
763 int shown_header = 0;
766 for (i = 0; i < s->change.nr; i++) {
767 struct wt_status_change_data *d;
768 struct string_list_item *it;
769 it = &(s->change.items[i]);
771 if (!d->index_status ||
772 d->index_status == DIFF_STATUS_UNMERGED)
775 wt_longstatus_print_cached_header(s);
779 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
782 wt_longstatus_print_trailer(s);
788 * 1 : some change but no delete
790 static int wt_status_check_worktree_changes(struct wt_status *s,
791 int *dirty_submodules)
796 *dirty_submodules = 0;
798 for (i = 0; i < s->change.nr; i++) {
799 struct wt_status_change_data *d;
800 d = s->change.items[i].util;
801 if (!d->worktree_status ||
802 d->worktree_status == DIFF_STATUS_UNMERGED)
806 if (d->dirty_submodule)
807 *dirty_submodules = 1;
808 if (d->worktree_status == DIFF_STATUS_DELETED)
814 static void wt_longstatus_print_changed(struct wt_status *s)
816 int i, dirty_submodules;
817 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
819 if (!worktree_changes)
822 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
824 for (i = 0; i < s->change.nr; i++) {
825 struct wt_status_change_data *d;
826 struct string_list_item *it;
827 it = &(s->change.items[i]);
829 if (!d->worktree_status ||
830 d->worktree_status == DIFF_STATUS_UNMERGED)
832 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
834 wt_longstatus_print_trailer(s);
837 static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
838 const char *email, timestamp_t timestamp, int tz,
839 const char *message, void *cb_data)
846 static void wt_longstatus_print_stash_summary(struct wt_status *s)
850 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
852 status_printf_ln(s, GIT_COLOR_NORMAL,
853 Q_("Your stash currently has %d entry",
854 "Your stash currently has %d entries", stash_count),
858 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
860 struct child_process sm_summary = CHILD_PROCESS_INIT;
861 struct strbuf cmd_stdout = STRBUF_INIT;
862 struct strbuf summary = STRBUF_INIT;
863 char *summary_content;
865 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
868 argv_array_push(&sm_summary.args, "submodule");
869 argv_array_push(&sm_summary.args, "summary");
870 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
871 argv_array_push(&sm_summary.args, "--for-status");
872 argv_array_push(&sm_summary.args, "--summary-limit");
873 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
875 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
877 sm_summary.git_cmd = 1;
878 sm_summary.no_stdin = 1;
880 capture_command(&sm_summary, &cmd_stdout, 1024);
882 /* prepend header, only if there's an actual output */
883 if (cmd_stdout.len) {
885 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
887 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
888 strbuf_addstr(&summary, "\n\n");
890 strbuf_addbuf(&summary, &cmd_stdout);
891 strbuf_release(&cmd_stdout);
893 if (s->display_comment_prefix) {
895 summary_content = strbuf_detach(&summary, &len);
896 strbuf_add_commented_lines(&summary, summary_content, len);
897 free(summary_content);
900 fputs(summary.buf, s->fp);
901 strbuf_release(&summary);
904 static void wt_longstatus_print_other(struct wt_status *s,
905 struct string_list *l,
910 struct strbuf buf = STRBUF_INIT;
911 static struct string_list output = STRING_LIST_INIT_DUP;
912 struct column_options copts;
917 wt_longstatus_print_other_header(s, what, how);
919 for (i = 0; i < l->nr; i++) {
920 struct string_list_item *it;
923 path = quote_path(it->string, s->prefix, &buf);
924 if (column_active(s->colopts)) {
925 string_list_append(&output, path);
928 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
929 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
933 strbuf_release(&buf);
934 if (!column_active(s->colopts))
937 strbuf_addf(&buf, "%s%s\t%s",
938 color(WT_STATUS_HEADER, s),
939 s->display_comment_prefix ? "#" : "",
940 color(WT_STATUS_UNTRACKED, s));
941 memset(&copts, 0, sizeof(copts));
943 copts.indent = buf.buf;
944 if (want_color(s->use_color))
945 copts.nl = GIT_COLOR_RESET "\n";
946 print_columns(&output, s->colopts, &copts);
947 string_list_clear(&output, 0);
948 strbuf_release(&buf);
950 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
953 size_t wt_status_locate_end(const char *s, size_t len)
956 struct strbuf pattern = STRBUF_INIT;
958 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
959 if (starts_with(s, pattern.buf + 1))
961 else if ((p = strstr(s, pattern.buf)))
963 strbuf_release(&pattern);
967 void wt_status_add_cut_line(FILE *fp)
969 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
970 struct strbuf buf = STRBUF_INIT;
972 fprintf(fp, "%c %s", comment_line_char, cut_line);
973 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
975 strbuf_release(&buf);
978 static void wt_longstatus_print_verbose(struct wt_status *s)
981 struct setup_revision_opt opt;
982 int dirty_submodules;
983 const char *c = color(WT_STATUS_HEADER, s);
985 init_revisions(&rev, NULL);
986 rev.diffopt.flags.allow_textconv = 1;
987 rev.diffopt.ita_invisible_in_index = 1;
989 memset(&opt, 0, sizeof(opt));
990 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
991 setup_revisions(0, NULL, &rev, &opt);
993 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
994 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
995 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
996 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
997 rev.diffopt.file = s->fp;
998 rev.diffopt.close_file = 0;
1000 * If we're not going to stdout, then we definitely don't
1001 * want color, since we are going to the commit message
1002 * file (and even the "auto" setting won't work, since it
1003 * will have checked isatty on stdout). But we then do want
1004 * to insert the scissor line here to reliably remove the
1005 * diff before committing.
1007 if (s->fp != stdout) {
1008 rev.diffopt.use_color = 0;
1009 wt_status_add_cut_line(s->fp);
1011 if (s->verbose > 1 && s->commitable) {
1012 /* print_updated() printed a header, so do we */
1013 if (s->fp != stdout)
1014 wt_longstatus_print_trailer(s);
1015 status_printf_ln(s, c, _("Changes to be committed:"));
1016 rev.diffopt.a_prefix = "c/";
1017 rev.diffopt.b_prefix = "i/";
1018 } /* else use prefix as per user config */
1019 run_diff_index(&rev, 1);
1020 if (s->verbose > 1 &&
1021 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1022 status_printf_ln(s, c,
1023 "--------------------------------------------------");
1024 status_printf_ln(s, c, _("Changes not staged for commit:"));
1026 rev.diffopt.a_prefix = "i/";
1027 rev.diffopt.b_prefix = "w/";
1028 run_diff_files(&rev, 0);
1032 static void wt_longstatus_print_tracking(struct wt_status *s)
1034 struct strbuf sb = STRBUF_INIT;
1035 const char *cp, *ep, *branch_name;
1036 struct branch *branch;
1037 char comment_line_string[3];
1040 assert(s->branch && !s->is_initial);
1041 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1043 branch = branch_get(branch_name);
1044 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1048 if (s->display_comment_prefix) {
1049 comment_line_string[i++] = comment_line_char;
1050 comment_line_string[i++] = ' ';
1052 comment_line_string[i] = '\0';
1054 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1055 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1056 "%s%.*s", comment_line_string,
1057 (int)(ep - cp), cp);
1058 if (s->display_comment_prefix)
1059 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1063 strbuf_release(&sb);
1066 static int has_unmerged(struct wt_status *s)
1070 for (i = 0; i < s->change.nr; i++) {
1071 struct wt_status_change_data *d;
1072 d = s->change.items[i].util;
1079 static void show_merge_in_progress(struct wt_status *s,
1080 struct wt_status_state *state,
1083 if (has_unmerged(s)) {
1084 status_printf_ln(s, color, _("You have unmerged paths."));
1086 status_printf_ln(s, color,
1087 _(" (fix conflicts and run \"git commit\")"));
1088 status_printf_ln(s, color,
1089 _(" (use \"git merge --abort\" to abort the merge)"));
1093 status_printf_ln(s, color,
1094 _("All conflicts fixed but you are still merging."));
1096 status_printf_ln(s, color,
1097 _(" (use \"git commit\" to conclude merge)"));
1099 wt_longstatus_print_trailer(s);
1102 static void show_am_in_progress(struct wt_status *s,
1103 struct wt_status_state *state,
1106 status_printf_ln(s, color,
1107 _("You are in the middle of an am session."));
1108 if (state->am_empty_patch)
1109 status_printf_ln(s, color,
1110 _("The current patch is empty."));
1112 if (!state->am_empty_patch)
1113 status_printf_ln(s, color,
1114 _(" (fix conflicts and then run \"git am --continue\")"));
1115 status_printf_ln(s, color,
1116 _(" (use \"git am --skip\" to skip this patch)"));
1117 status_printf_ln(s, color,
1118 _(" (use \"git am --abort\" to restore the original branch)"));
1120 wt_longstatus_print_trailer(s);
1123 static char *read_line_from_git_path(const char *filename)
1125 struct strbuf buf = STRBUF_INIT;
1126 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1129 strbuf_release(&buf);
1132 strbuf_getline_lf(&buf, fp);
1134 return strbuf_detach(&buf, NULL);
1136 strbuf_release(&buf);
1141 static int split_commit_in_progress(struct wt_status *s)
1143 int split_in_progress = 0;
1144 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1146 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1147 !s->branch || strcmp(s->branch, "HEAD"))
1150 head = read_line_from_git_path("HEAD");
1151 orig_head = read_line_from_git_path("ORIG_HEAD");
1152 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1153 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1155 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1156 ; /* fall through, no split in progress */
1157 else if (!strcmp(rebase_amend, rebase_orig_head))
1158 split_in_progress = !!strcmp(head, rebase_amend);
1159 else if (strcmp(orig_head, rebase_orig_head))
1160 split_in_progress = 1;
1165 free(rebase_orig_head);
1167 return split_in_progress;
1172 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1174 * "pick d6a2f03 some message"
1176 * The function assumes that the line does not contain useless spaces
1177 * before or after the command.
1179 static void abbrev_sha1_in_line(struct strbuf *line)
1181 struct strbuf **split;
1184 if (starts_with(line->buf, "exec ") ||
1185 starts_with(line->buf, "x "))
1188 split = strbuf_split_max(line, ' ', 3);
1189 if (split[0] && split[1]) {
1190 struct object_id oid;
1193 * strbuf_split_max left a space. Trim it and re-add
1194 * it after abbreviation.
1196 strbuf_trim(split[1]);
1197 if (!get_oid(split[1]->buf, &oid)) {
1198 strbuf_reset(split[1]);
1199 strbuf_add_unique_abbrev(split[1], &oid,
1201 strbuf_addch(split[1], ' ');
1203 for (i = 0; split[i]; i++)
1204 strbuf_addbuf(line, split[i]);
1207 strbuf_list_free(split);
1210 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1212 struct strbuf line = STRBUF_INIT;
1213 FILE *f = fopen(git_path("%s", fname), "r");
1216 if (errno == ENOENT)
1218 die_errno("Could not open file %s for reading",
1219 git_path("%s", fname));
1221 while (!strbuf_getline_lf(&line, f)) {
1222 if (line.len && line.buf[0] == comment_line_char)
1227 abbrev_sha1_in_line(&line);
1228 string_list_append(lines, line.buf);
1231 strbuf_release(&line);
1235 static void show_rebase_information(struct wt_status *s,
1236 struct wt_status_state *state,
1239 if (state->rebase_interactive_in_progress) {
1241 int nr_lines_to_show = 2;
1243 struct string_list have_done = STRING_LIST_INIT_DUP;
1244 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1246 read_rebase_todolist("rebase-merge/done", &have_done);
1247 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1249 status_printf_ln(s, color,
1250 _("git-rebase-todo is missing."));
1251 if (have_done.nr == 0)
1252 status_printf_ln(s, color, _("No commands done."));
1254 status_printf_ln(s, color,
1255 Q_("Last command done (%d command done):",
1256 "Last commands done (%d commands done):",
1259 for (i = (have_done.nr > nr_lines_to_show)
1260 ? have_done.nr - nr_lines_to_show : 0;
1263 status_printf_ln(s, color, " %s", have_done.items[i].string);
1264 if (have_done.nr > nr_lines_to_show && s->hints)
1265 status_printf_ln(s, color,
1266 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1269 if (yet_to_do.nr == 0)
1270 status_printf_ln(s, color,
1271 _("No commands remaining."));
1273 status_printf_ln(s, color,
1274 Q_("Next command to do (%d remaining command):",
1275 "Next commands to do (%d remaining commands):",
1278 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1279 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1281 status_printf_ln(s, color,
1282 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1284 string_list_clear(&yet_to_do, 0);
1285 string_list_clear(&have_done, 0);
1289 static void print_rebase_state(struct wt_status *s,
1290 struct wt_status_state *state,
1294 status_printf_ln(s, color,
1295 _("You are currently rebasing branch '%s' on '%s'."),
1299 status_printf_ln(s, color,
1300 _("You are currently rebasing."));
1303 static void show_rebase_in_progress(struct wt_status *s,
1304 struct wt_status_state *state,
1309 show_rebase_information(s, state, color);
1310 if (has_unmerged(s)) {
1311 print_rebase_state(s, state, color);
1313 status_printf_ln(s, color,
1314 _(" (fix conflicts and then run \"git rebase --continue\")"));
1315 status_printf_ln(s, color,
1316 _(" (use \"git rebase --skip\" to skip this patch)"));
1317 status_printf_ln(s, color,
1318 _(" (use \"git rebase --abort\" to check out the original branch)"));
1320 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(the_repository), &st)) {
1321 print_rebase_state(s, state, color);
1323 status_printf_ln(s, color,
1324 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1325 } else if (split_commit_in_progress(s)) {
1327 status_printf_ln(s, color,
1328 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1332 status_printf_ln(s, color,
1333 _("You are currently splitting a commit during a rebase."));
1335 status_printf_ln(s, color,
1336 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1339 status_printf_ln(s, color,
1340 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1344 status_printf_ln(s, color,
1345 _("You are currently editing a commit during a rebase."));
1346 if (s->hints && !s->amend) {
1347 status_printf_ln(s, color,
1348 _(" (use \"git commit --amend\" to amend the current commit)"));
1349 status_printf_ln(s, color,
1350 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1353 wt_longstatus_print_trailer(s);
1356 static void show_cherry_pick_in_progress(struct wt_status *s,
1357 struct wt_status_state *state,
1360 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1361 find_unique_abbrev(&state->cherry_pick_head_oid, DEFAULT_ABBREV));
1363 if (has_unmerged(s))
1364 status_printf_ln(s, color,
1365 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1367 status_printf_ln(s, color,
1368 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1369 status_printf_ln(s, color,
1370 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1372 wt_longstatus_print_trailer(s);
1375 static void show_revert_in_progress(struct wt_status *s,
1376 struct wt_status_state *state,
1379 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1380 find_unique_abbrev(&state->revert_head_oid, DEFAULT_ABBREV));
1382 if (has_unmerged(s))
1383 status_printf_ln(s, color,
1384 _(" (fix conflicts and run \"git revert --continue\")"));
1386 status_printf_ln(s, color,
1387 _(" (all conflicts fixed: run \"git revert --continue\")"));
1388 status_printf_ln(s, color,
1389 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1391 wt_longstatus_print_trailer(s);
1394 static void show_bisect_in_progress(struct wt_status *s,
1395 struct wt_status_state *state,
1399 status_printf_ln(s, color,
1400 _("You are currently bisecting, started from branch '%s'."),
1403 status_printf_ln(s, color,
1404 _("You are currently bisecting."));
1406 status_printf_ln(s, color,
1407 _(" (use \"git bisect reset\" to get back to the original branch)"));
1408 wt_longstatus_print_trailer(s);
1412 * Extract branch information from rebase/bisect
1414 static char *get_branch(const struct worktree *wt, const char *path)
1416 struct strbuf sb = STRBUF_INIT;
1417 struct object_id oid;
1418 const char *branch_name;
1420 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1423 while (sb.len && sb.buf[sb.len - 1] == '\n')
1424 strbuf_setlen(&sb, sb.len - 1);
1427 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1428 strbuf_remove(&sb, 0, branch_name - sb.buf);
1429 else if (starts_with(sb.buf, "refs/"))
1431 else if (!get_oid_hex(sb.buf, &oid)) {
1433 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1434 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1438 return strbuf_detach(&sb, NULL);
1441 strbuf_release(&sb);
1445 struct grab_1st_switch_cbdata {
1447 struct object_id noid;
1450 static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
1451 const char *email, timestamp_t timestamp, int tz,
1452 const char *message, void *cb_data)
1454 struct grab_1st_switch_cbdata *cb = cb_data;
1455 const char *target = NULL, *end;
1457 if (!skip_prefix(message, "checkout: moving from ", &message))
1459 target = strstr(message, " to ");
1462 target += strlen(" to ");
1463 strbuf_reset(&cb->buf);
1464 oidcpy(&cb->noid, noid);
1465 end = strchrnul(target, '\n');
1466 strbuf_add(&cb->buf, target, end - target);
1467 if (!strcmp(cb->buf.buf, "HEAD")) {
1468 /* HEAD is relative. Resolve it to the right reflog entry. */
1469 strbuf_reset(&cb->buf);
1470 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1475 static void wt_status_get_detached_from(struct wt_status_state *state)
1477 struct grab_1st_switch_cbdata cb;
1478 struct commit *commit;
1479 struct object_id oid;
1482 strbuf_init(&cb.buf, 0);
1483 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1484 strbuf_release(&cb.buf);
1488 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 &&
1489 /* sha1 is a commit? match without further lookup */
1490 (!oidcmp(&cb.noid, &oid) ||
1491 /* perhaps sha1 is a tag, try to dereference to a commit */
1492 ((commit = lookup_commit_reference_gently(the_repository, &oid, 1)) != NULL &&
1493 !oidcmp(&cb.noid, &commit->object.oid)))) {
1494 const char *from = ref;
1495 if (!skip_prefix(from, "refs/tags/", &from))
1496 skip_prefix(from, "refs/remotes/", &from);
1497 state->detached_from = xstrdup(from);
1499 state->detached_from =
1500 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
1501 oidcpy(&state->detached_oid, &cb.noid);
1502 state->detached_at = !get_oid("HEAD", &oid) &&
1503 !oidcmp(&oid, &state->detached_oid);
1506 strbuf_release(&cb.buf);
1509 int wt_status_check_rebase(const struct worktree *wt,
1510 struct wt_status_state *state)
1514 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1515 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1516 state->am_in_progress = 1;
1517 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1518 state->am_empty_patch = 1;
1520 state->rebase_in_progress = 1;
1521 state->branch = get_branch(wt, "rebase-apply/head-name");
1522 state->onto = get_branch(wt, "rebase-apply/onto");
1524 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1525 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1526 state->rebase_interactive_in_progress = 1;
1528 state->rebase_in_progress = 1;
1529 state->branch = get_branch(wt, "rebase-merge/head-name");
1530 state->onto = get_branch(wt, "rebase-merge/onto");
1536 int wt_status_check_bisect(const struct worktree *wt,
1537 struct wt_status_state *state)
1541 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1542 state->bisect_in_progress = 1;
1543 state->branch = get_branch(wt, "BISECT_START");
1549 void wt_status_get_state(struct wt_status_state *state,
1550 int get_detached_from)
1553 struct object_id oid;
1555 if (!stat(git_path_merge_head(the_repository), &st)) {
1556 state->merge_in_progress = 1;
1557 } else if (wt_status_check_rebase(NULL, state)) {
1559 } else if (!stat(git_path_cherry_pick_head(the_repository), &st) &&
1560 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1561 state->cherry_pick_in_progress = 1;
1562 oidcpy(&state->cherry_pick_head_oid, &oid);
1564 wt_status_check_bisect(NULL, state);
1565 if (!stat(git_path_revert_head(the_repository), &st) &&
1566 !get_oid("REVERT_HEAD", &oid)) {
1567 state->revert_in_progress = 1;
1568 oidcpy(&state->revert_head_oid, &oid);
1571 if (get_detached_from)
1572 wt_status_get_detached_from(state);
1575 static void wt_longstatus_print_state(struct wt_status *s,
1576 struct wt_status_state *state)
1578 const char *state_color = color(WT_STATUS_HEADER, s);
1579 if (state->merge_in_progress)
1580 show_merge_in_progress(s, state, state_color);
1581 else if (state->am_in_progress)
1582 show_am_in_progress(s, state, state_color);
1583 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1584 show_rebase_in_progress(s, state, state_color);
1585 else if (state->cherry_pick_in_progress)
1586 show_cherry_pick_in_progress(s, state, state_color);
1587 else if (state->revert_in_progress)
1588 show_revert_in_progress(s, state, state_color);
1589 if (state->bisect_in_progress)
1590 show_bisect_in_progress(s, state, state_color);
1593 static void wt_longstatus_print(struct wt_status *s)
1595 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1596 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1597 struct wt_status_state state;
1599 memset(&state, 0, sizeof(state));
1600 wt_status_get_state(&state,
1601 s->branch && !strcmp(s->branch, "HEAD"));
1604 const char *on_what = _("On branch ");
1605 const char *branch_name = s->branch;
1606 if (!strcmp(branch_name, "HEAD")) {
1607 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1608 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1609 if (state.rebase_interactive_in_progress)
1610 on_what = _("interactive rebase in progress; onto ");
1612 on_what = _("rebase in progress; onto ");
1613 branch_name = state.onto;
1614 } else if (state.detached_from) {
1615 branch_name = state.detached_from;
1616 if (state.detached_at)
1617 on_what = _("HEAD detached at ");
1619 on_what = _("HEAD detached from ");
1622 on_what = _("Not currently on any branch.");
1625 skip_prefix(branch_name, "refs/heads/", &branch_name);
1626 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1627 status_printf_more(s, branch_status_color, "%s", on_what);
1628 status_printf_more(s, branch_color, "%s\n", branch_name);
1630 wt_longstatus_print_tracking(s);
1633 wt_longstatus_print_state(s, &state);
1636 free(state.detached_from);
1638 if (s->is_initial) {
1639 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1640 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1642 ? _("Initial commit")
1643 : _("No commits yet"));
1644 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1647 wt_longstatus_print_updated(s);
1648 wt_longstatus_print_unmerged(s);
1649 wt_longstatus_print_changed(s);
1650 if (s->submodule_summary &&
1651 (!s->ignore_submodule_arg ||
1652 strcmp(s->ignore_submodule_arg, "all"))) {
1653 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1654 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1656 if (s->show_untracked_files) {
1657 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1658 if (s->show_ignored_mode)
1659 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1660 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1661 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1662 status_printf_ln(s, GIT_COLOR_NORMAL,
1663 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1664 "may speed it up, but you have to be careful not to forget to add\n"
1665 "new files yourself (see 'git help status')."),
1666 s->untracked_in_ms / 1000.0);
1668 } else if (s->commitable)
1669 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1671 ? _(" (use -u option to show untracked files)") : "");
1674 wt_longstatus_print_verbose(s);
1675 if (!s->commitable) {
1677 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1680 else if (s->workdir_dirty) {
1682 printf(_("no changes added to commit "
1683 "(use \"git add\" and/or \"git commit -a\")\n"));
1685 printf(_("no changes added to commit\n"));
1686 } else if (s->untracked.nr) {
1688 printf(_("nothing added to commit but untracked files "
1689 "present (use \"git add\" to track)\n"));
1691 printf(_("nothing added to commit but untracked files present\n"));
1692 } else if (s->is_initial) {
1694 printf(_("nothing to commit (create/copy files "
1695 "and use \"git add\" to track)\n"));
1697 printf(_("nothing to commit\n"));
1698 } else if (!s->show_untracked_files) {
1700 printf(_("nothing to commit (use -u to show untracked files)\n"));
1702 printf(_("nothing to commit\n"));
1704 printf(_("nothing to commit, working tree clean\n"));
1707 wt_longstatus_print_stash_summary(s);
1710 static void wt_shortstatus_unmerged(struct string_list_item *it,
1711 struct wt_status *s)
1713 struct wt_status_change_data *d = it->util;
1714 const char *how = "??";
1716 switch (d->stagemask) {
1717 case 1: how = "DD"; break; /* both deleted */
1718 case 2: how = "AU"; break; /* added by us */
1719 case 3: how = "UD"; break; /* deleted by them */
1720 case 4: how = "UA"; break; /* added by them */
1721 case 5: how = "DU"; break; /* deleted by us */
1722 case 6: how = "AA"; break; /* both added */
1723 case 7: how = "UU"; break; /* both modified */
1725 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1726 if (s->null_termination) {
1727 fprintf(stdout, " %s%c", it->string, 0);
1729 struct strbuf onebuf = STRBUF_INIT;
1731 one = quote_path(it->string, s->prefix, &onebuf);
1732 printf(" %s\n", one);
1733 strbuf_release(&onebuf);
1737 static void wt_shortstatus_status(struct string_list_item *it,
1738 struct wt_status *s)
1740 struct wt_status_change_data *d = it->util;
1742 if (d->index_status)
1743 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1746 if (d->worktree_status)
1747 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1751 if (s->null_termination) {
1752 fprintf(stdout, "%s%c", it->string, 0);
1753 if (d->rename_source)
1754 fprintf(stdout, "%s%c", d->rename_source, 0);
1756 struct strbuf onebuf = STRBUF_INIT;
1759 if (d->rename_source) {
1760 one = quote_path(d->rename_source, s->prefix, &onebuf);
1761 if (*one != '"' && strchr(one, ' ') != NULL) {
1763 strbuf_addch(&onebuf, '"');
1766 printf("%s -> ", one);
1767 strbuf_release(&onebuf);
1769 one = quote_path(it->string, s->prefix, &onebuf);
1770 if (*one != '"' && strchr(one, ' ') != NULL) {
1772 strbuf_addch(&onebuf, '"');
1775 printf("%s\n", one);
1776 strbuf_release(&onebuf);
1780 static void wt_shortstatus_other(struct string_list_item *it,
1781 struct wt_status *s, const char *sign)
1783 if (s->null_termination) {
1784 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1786 struct strbuf onebuf = STRBUF_INIT;
1788 one = quote_path(it->string, s->prefix, &onebuf);
1789 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1790 printf(" %s\n", one);
1791 strbuf_release(&onebuf);
1795 static void wt_shortstatus_print_tracking(struct wt_status *s)
1797 struct branch *branch;
1798 const char *header_color = color(WT_STATUS_HEADER, s);
1799 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1800 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1804 const char *branch_name;
1805 int num_ours, num_theirs, sti;
1806 int upstream_is_gone = 0;
1808 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1812 branch_name = s->branch;
1814 #define LABEL(string) (s->no_gettext ? (string) : _(string))
1817 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
1819 if (!strcmp(s->branch, "HEAD")) {
1820 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1821 LABEL(N_("HEAD (no branch)")));
1825 skip_prefix(branch_name, "refs/heads/", &branch_name);
1827 branch = branch_get(branch_name);
1829 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1831 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
1832 s->ahead_behind_flags);
1837 upstream_is_gone = 1;
1840 short_base = shorten_unambiguous_ref(base, 0);
1841 color_fprintf(s->fp, header_color, "...");
1842 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1845 if (!upstream_is_gone && !sti)
1848 color_fprintf(s->fp, header_color, " [");
1849 if (upstream_is_gone) {
1850 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
1851 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
1852 color_fprintf(s->fp, header_color, LABEL(N_("different")));
1853 } else if (!num_ours) {
1854 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
1855 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1856 } else if (!num_theirs) {
1857 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1858 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1860 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1861 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1862 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
1863 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1866 color_fprintf(s->fp, header_color, "]");
1868 fputc(s->null_termination ? '\0' : '\n', s->fp);
1871 static void wt_shortstatus_print(struct wt_status *s)
1873 struct string_list_item *it;
1876 wt_shortstatus_print_tracking(s);
1878 for_each_string_list_item(it, &s->change) {
1879 struct wt_status_change_data *d = it->util;
1882 wt_shortstatus_unmerged(it, s);
1884 wt_shortstatus_status(it, s);
1886 for_each_string_list_item(it, &s->untracked)
1887 wt_shortstatus_other(it, s, "??");
1889 for_each_string_list_item(it, &s->ignored)
1890 wt_shortstatus_other(it, s, "!!");
1893 static void wt_porcelain_print(struct wt_status *s)
1896 s->relative_paths = 0;
1899 wt_shortstatus_print(s);
1903 * Print branch information for porcelain v2 output. These lines
1904 * are printed when the '--branch' parameter is given.
1906 * # branch.oid <commit><eol>
1907 * # branch.head <head><eol>
1908 * [# branch.upstream <upstream><eol>
1909 * [# branch.ab +<ahead> -<behind><eol>]]
1911 * <commit> ::= the current commit hash or the the literal
1912 * "(initial)" to indicate an initialized repo
1915 * <head> ::= <branch_name> the current branch name or
1916 * "(detached)" literal when detached head or
1917 * "(unknown)" when something is wrong.
1919 * <upstream> ::= the upstream branch name, when set.
1921 * <ahead> ::= integer ahead value or '?'.
1923 * <behind> ::= integer behind value or '?'.
1925 * The end-of-line is defined by the -z flag.
1927 * <eol> ::= NUL when -z,
1930 * When an upstream is set and present, the 'branch.ab' line will
1931 * be printed with the ahead/behind counts for the branch and the
1932 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1933 * are different, '?' will be substituted for the actual count.
1935 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1937 struct branch *branch;
1939 const char *branch_name;
1940 struct wt_status_state state;
1941 int ab_info, nr_ahead, nr_behind;
1942 char eol = s->null_termination ? '\0' : '\n';
1944 memset(&state, 0, sizeof(state));
1945 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1947 fprintf(s->fp, "# branch.oid %s%c",
1948 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1952 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1954 if (!strcmp(s->branch, "HEAD")) {
1955 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1957 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1958 branch_name = state.onto;
1959 else if (state.detached_from)
1960 branch_name = state.detached_from;
1965 skip_prefix(s->branch, "refs/heads/", &branch_name);
1967 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1970 /* Lookup stats on the upstream tracking branch, if set. */
1971 branch = branch_get(branch_name);
1973 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1974 &base, s->ahead_behind_flags);
1976 base = shorten_unambiguous_ref(base, 0);
1977 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1982 if (nr_ahead || nr_behind)
1983 fprintf(s->fp, "# branch.ab +%d -%d%c",
1984 nr_ahead, nr_behind, eol);
1986 fprintf(s->fp, "# branch.ab +? -?%c",
1988 } else if (!ab_info) {
1990 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
1997 free(state.detached_from);
2001 * Convert various submodule status values into a
2002 * fixed-length string of characters in the buffer provided.
2004 static void wt_porcelain_v2_submodule_state(
2005 struct wt_status_change_data *d,
2008 if (S_ISGITLINK(d->mode_head) ||
2009 S_ISGITLINK(d->mode_index) ||
2010 S_ISGITLINK(d->mode_worktree)) {
2012 sub[1] = d->new_submodule_commits ? 'C' : '.';
2013 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2014 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2025 * Fix-up changed entries before we print them.
2027 static void wt_porcelain_v2_fix_up_changed(
2028 struct string_list_item *it,
2029 struct wt_status *s)
2031 struct wt_status_change_data *d = it->util;
2033 if (!d->index_status) {
2035 * This entry is unchanged in the index (relative to the head).
2036 * Therefore, the collect_updated_cb was never called for this
2037 * entry (during the head-vs-index scan) and so the head column
2038 * fields were never set.
2040 * We must have data for the index column (from the
2041 * index-vs-worktree scan (otherwise, this entry should not be
2042 * in the list of changes)).
2044 * Copy index column fields to the head column, so that our
2045 * output looks complete.
2047 assert(d->mode_head == 0);
2048 d->mode_head = d->mode_index;
2049 oidcpy(&d->oid_head, &d->oid_index);
2052 if (!d->worktree_status) {
2054 * This entry is unchanged in the worktree (relative to the index).
2055 * Therefore, the collect_changed_cb was never called for this entry
2056 * (during the index-vs-worktree scan) and so the worktree column
2057 * fields were never set.
2059 * We must have data for the index column (from the head-vs-index
2062 * Copy the index column fields to the worktree column so that
2063 * our output looks complete.
2065 * Note that we only have a mode field in the worktree column
2066 * because the scan code tries really hard to not have to compute it.
2068 assert(d->mode_worktree == 0);
2069 d->mode_worktree = d->mode_index;
2074 * Print porcelain v2 info for tracked entries with changes.
2076 static void wt_porcelain_v2_print_changed_entry(
2077 struct string_list_item *it,
2078 struct wt_status *s)
2080 struct wt_status_change_data *d = it->util;
2081 struct strbuf buf = STRBUF_INIT;
2082 struct strbuf buf_from = STRBUF_INIT;
2083 const char *path = NULL;
2084 const char *path_from = NULL;
2086 char submodule_token[5];
2087 char sep_char, eol_char;
2089 wt_porcelain_v2_fix_up_changed(it, s);
2090 wt_porcelain_v2_submodule_state(d, submodule_token);
2092 key[0] = d->index_status ? d->index_status : '.';
2093 key[1] = d->worktree_status ? d->worktree_status : '.';
2096 if (s->null_termination) {
2098 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2099 * A single NUL character separates them.
2104 path_from = d->rename_source;
2107 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2108 * The source path is only present when necessary.
2109 * A single TAB separates them (because paths can contain spaces
2110 * which are not escaped and C-quoting does escape TAB characters).
2114 path = quote_path(it->string, s->prefix, &buf);
2115 if (d->rename_source)
2116 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
2120 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2121 key, submodule_token,
2122 d->mode_head, d->mode_index, d->mode_worktree,
2123 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2124 d->rename_status, d->rename_score,
2125 path, sep_char, path_from, eol_char);
2127 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2128 key, submodule_token,
2129 d->mode_head, d->mode_index, d->mode_worktree,
2130 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2133 strbuf_release(&buf);
2134 strbuf_release(&buf_from);
2138 * Print porcelain v2 status info for unmerged entries.
2140 static void wt_porcelain_v2_print_unmerged_entry(
2141 struct string_list_item *it,
2142 struct wt_status *s)
2144 struct wt_status_change_data *d = it->util;
2145 const struct cache_entry *ce;
2146 struct strbuf buf_index = STRBUF_INIT;
2147 const char *path_index = NULL;
2148 int pos, stage, sum;
2151 struct object_id oid;
2154 char submodule_token[5];
2155 char unmerged_prefix = 'u';
2156 char eol_char = s->null_termination ? '\0' : '\n';
2158 wt_porcelain_v2_submodule_state(d, submodule_token);
2160 switch (d->stagemask) {
2161 case 1: key = "DD"; break; /* both deleted */
2162 case 2: key = "AU"; break; /* added by us */
2163 case 3: key = "UD"; break; /* deleted by them */
2164 case 4: key = "UA"; break; /* added by them */
2165 case 5: key = "DU"; break; /* deleted by us */
2166 case 6: key = "AA"; break; /* both added */
2167 case 7: key = "UU"; break; /* both modified */
2169 BUG("unhandled unmerged status %x", d->stagemask);
2173 * Disregard d.aux.porcelain_v2 data that we accumulated
2174 * for the head and index columns during the scans and
2175 * replace with the actual stage data.
2177 * Note that this is a last-one-wins for each the individual
2178 * stage [123] columns in the event of multiple cache entries
2181 memset(stages, 0, sizeof(stages));
2183 pos = cache_name_pos(it->string, strlen(it->string));
2186 while (pos < active_nr) {
2187 ce = active_cache[pos++];
2188 stage = ce_stage(ce);
2189 if (strcmp(ce->name, it->string) || !stage)
2191 stages[stage - 1].mode = ce->ce_mode;
2192 oidcpy(&stages[stage - 1].oid, &ce->oid);
2193 sum |= (1 << (stage - 1));
2195 if (sum != d->stagemask)
2196 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2198 if (s->null_termination)
2199 path_index = it->string;
2201 path_index = quote_path(it->string, s->prefix, &buf_index);
2203 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2204 unmerged_prefix, key, submodule_token,
2205 stages[0].mode, /* stage 1 */
2206 stages[1].mode, /* stage 2 */
2207 stages[2].mode, /* stage 3 */
2209 oid_to_hex(&stages[0].oid), /* stage 1 */
2210 oid_to_hex(&stages[1].oid), /* stage 2 */
2211 oid_to_hex(&stages[2].oid), /* stage 3 */
2215 strbuf_release(&buf_index);
2219 * Print porcelain V2 status info for untracked and ignored entries.
2221 static void wt_porcelain_v2_print_other(
2222 struct string_list_item *it,
2223 struct wt_status *s,
2226 struct strbuf buf = STRBUF_INIT;
2230 if (s->null_termination) {
2234 path = quote_path(it->string, s->prefix, &buf);
2238 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2240 strbuf_release(&buf);
2244 * Print porcelain V2 status.
2247 * [<v2_changed_items>]*
2248 * [<v2_unmerged_items>]*
2249 * [<v2_untracked_items>]*
2250 * [<v2_ignored_items>]*
2253 static void wt_porcelain_v2_print(struct wt_status *s)
2255 struct wt_status_change_data *d;
2256 struct string_list_item *it;
2260 wt_porcelain_v2_print_tracking(s);
2262 for (i = 0; i < s->change.nr; i++) {
2263 it = &(s->change.items[i]);
2266 wt_porcelain_v2_print_changed_entry(it, s);
2269 for (i = 0; i < s->change.nr; i++) {
2270 it = &(s->change.items[i]);
2273 wt_porcelain_v2_print_unmerged_entry(it, s);
2276 for (i = 0; i < s->untracked.nr; i++) {
2277 it = &(s->untracked.items[i]);
2278 wt_porcelain_v2_print_other(it, s, '?');
2281 for (i = 0; i < s->ignored.nr; i++) {
2282 it = &(s->ignored.items[i]);
2283 wt_porcelain_v2_print_other(it, s, '!');
2287 void wt_status_print(struct wt_status *s)
2289 switch (s->status_format) {
2290 case STATUS_FORMAT_SHORT:
2291 wt_shortstatus_print(s);
2293 case STATUS_FORMAT_PORCELAIN:
2294 wt_porcelain_print(s);
2296 case STATUS_FORMAT_PORCELAIN_V2:
2297 wt_porcelain_v2_print(s);
2299 case STATUS_FORMAT_UNSPECIFIED:
2300 BUG("finalize_deferred_config() should have been called");
2302 case STATUS_FORMAT_NONE:
2303 case STATUS_FORMAT_LONG:
2304 wt_longstatus_print(s);
2310 * Returns 1 if there are unstaged changes, 0 otherwise.
2312 int has_unstaged_changes(int ignore_submodules)
2314 struct rev_info rev_info;
2317 init_revisions(&rev_info, NULL);
2318 if (ignore_submodules) {
2319 rev_info.diffopt.flags.ignore_submodules = 1;
2320 rev_info.diffopt.flags.override_submodule_config = 1;
2322 rev_info.diffopt.flags.quick = 1;
2323 diff_setup_done(&rev_info.diffopt);
2324 result = run_diff_files(&rev_info, 0);
2325 return diff_result_code(&rev_info.diffopt, result);
2329 * Returns 1 if there are uncommitted changes, 0 otherwise.
2331 int has_uncommitted_changes(int ignore_submodules)
2333 struct rev_info rev_info;
2336 if (is_cache_unborn())
2339 init_revisions(&rev_info, NULL);
2340 if (ignore_submodules)
2341 rev_info.diffopt.flags.ignore_submodules = 1;
2342 rev_info.diffopt.flags.quick = 1;
2344 add_head_to_pending(&rev_info);
2345 if (!rev_info.pending.nr) {
2347 * We have no head (or it's corrupt); use the empty tree,
2348 * which will complain if the index is non-empty.
2350 struct tree *tree = lookup_tree(the_repository, the_hash_algo->empty_tree);
2351 add_pending_object(&rev_info, &tree->object, "");
2354 diff_setup_done(&rev_info.diffopt);
2355 result = run_diff_index(&rev_info, 1);
2356 return diff_result_code(&rev_info.diffopt, result);
2360 * If the work tree has unstaged or uncommitted changes, dies with the
2361 * appropriate message.
2363 int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2365 struct lock_file lock_file = LOCK_INIT;
2368 fd = hold_locked_index(&lock_file, 0);
2369 refresh_cache(REFRESH_QUIET);
2371 update_index_if_able(&the_index, &lock_file);
2372 rollback_lock_file(&lock_file);
2374 if (has_unstaged_changes(ignore_submodules)) {
2375 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2376 error(_("cannot %s: You have unstaged changes."), _(action));
2380 if (has_uncommitted_changes(ignore_submodules)) {
2382 error(_("additionally, your index contains uncommitted changes."));
2384 error(_("cannot %s: Your index contains uncommitted changes."),