11 #include "run-command.h"
12 #include "argv-array.h"
15 #include "submodule.h"
20 static char default_wt_status_colors[][COLOR_MAXLEN] = {
21 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
22 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
23 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
24 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
25 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
26 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
27 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
28 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
29 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
32 static const char *color(int slot, struct wt_status *s)
35 if (want_color(s->use_color))
36 c = s->color_palette[slot];
37 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
38 c = s->color_palette[WT_STATUS_HEADER];
42 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
43 const char *fmt, va_list ap, const char *trail)
45 struct strbuf sb = STRBUF_INIT;
46 struct strbuf linebuf = STRBUF_INIT;
47 const char *line, *eol;
49 strbuf_vaddf(&sb, fmt, ap);
51 if (s->display_comment_prefix) {
52 strbuf_addch(&sb, comment_line_char);
54 strbuf_addch(&sb, ' ');
56 color_print_strbuf(s->fp, color, &sb);
58 fprintf(s->fp, "%s", trail);
62 for (line = sb.buf; *line; line = eol + 1) {
63 eol = strchr(line, '\n');
65 strbuf_reset(&linebuf);
66 if (at_bol && s->display_comment_prefix) {
67 strbuf_addch(&linebuf, comment_line_char);
68 if (*line != '\n' && *line != '\t')
69 strbuf_addch(&linebuf, ' ');
72 strbuf_add(&linebuf, line, eol - line);
74 strbuf_addstr(&linebuf, line);
75 color_print_strbuf(s->fp, color, &linebuf);
83 fprintf(s->fp, "%s", trail);
84 strbuf_release(&linebuf);
88 void status_printf_ln(struct wt_status *s, const char *color,
94 status_vprintf(s, 1, color, fmt, ap, "\n");
98 void status_printf(struct wt_status *s, const char *color,
104 status_vprintf(s, 1, color, fmt, ap, NULL);
108 static void status_printf_more(struct wt_status *s, const char *color,
109 const char *fmt, ...)
114 status_vprintf(s, 0, color, fmt, ap, NULL);
118 void wt_status_prepare(struct wt_status *s)
120 unsigned char sha1[20];
122 memset(s, 0, sizeof(*s));
123 memcpy(s->color_palette, default_wt_status_colors,
124 sizeof(default_wt_status_colors));
125 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
127 s->relative_paths = 1;
128 s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
129 s->reference = "HEAD";
131 s->index_file = get_index_file();
132 s->change.strdup_strings = 1;
133 s->untracked.strdup_strings = 1;
134 s->ignored.strdup_strings = 1;
135 s->show_branch = -1; /* unspecified */
136 s->display_comment_prefix = 0;
139 static void wt_status_print_unmerged_header(struct wt_status *s)
142 int del_mod_conflict = 0;
143 int both_deleted = 0;
145 const char *c = color(WT_STATUS_HEADER, s);
147 status_printf_ln(s, c, _("Unmerged paths:"));
149 for (i = 0; i < s->change.nr; i++) {
150 struct string_list_item *it = &(s->change.items[i]);
151 struct wt_status_change_data *d = it->util;
153 switch (d->stagemask) {
161 del_mod_conflict = 1;
171 if (s->whence != FROM_COMMIT)
173 else if (!s->is_initial)
174 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
176 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
179 if (!del_mod_conflict)
180 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
182 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
183 } else if (!del_mod_conflict && !not_deleted) {
184 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
186 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
188 status_printf_ln(s, c, "");
191 static void wt_status_print_cached_header(struct wt_status *s)
193 const char *c = color(WT_STATUS_HEADER, s);
195 status_printf_ln(s, c, _("Changes to be committed:"));
198 if (s->whence != FROM_COMMIT)
199 ; /* NEEDSWORK: use "git reset --unresolve"??? */
200 else if (!s->is_initial)
201 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
203 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
204 status_printf_ln(s, c, "");
207 static void wt_status_print_dirty_header(struct wt_status *s,
209 int has_dirty_submodules)
211 const char *c = color(WT_STATUS_HEADER, s);
213 status_printf_ln(s, c, _("Changes not staged for commit:"));
217 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
219 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
220 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
221 if (has_dirty_submodules)
222 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
223 status_printf_ln(s, c, "");
226 static void wt_status_print_other_header(struct wt_status *s,
230 const char *c = color(WT_STATUS_HEADER, s);
231 status_printf_ln(s, c, "%s:", what);
234 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
235 status_printf_ln(s, c, "");
238 static void wt_status_print_trailer(struct wt_status *s)
240 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
243 #define quote_path quote_path_relative
245 static void wt_status_print_unmerged_data(struct wt_status *s,
246 struct string_list_item *it)
248 const char *c = color(WT_STATUS_UNMERGED, s);
249 struct wt_status_change_data *d = it->util;
250 struct strbuf onebuf = STRBUF_INIT;
251 const char *one, *how = _("bug");
253 one = quote_path(it->string, s->prefix, &onebuf);
254 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
255 switch (d->stagemask) {
256 case 1: how = _("both deleted:"); break;
257 case 2: how = _("added by us:"); break;
258 case 3: how = _("deleted by them:"); break;
259 case 4: how = _("added by them:"); break;
260 case 5: how = _("deleted by us:"); break;
261 case 6: how = _("both added:"); break;
262 case 7: how = _("both modified:"); break;
264 status_printf_more(s, c, "%-20s%s\n", how, one);
265 strbuf_release(&onebuf);
268 static const char *wt_status_diff_status_string(int status)
271 case DIFF_STATUS_ADDED:
272 return _("new file");
273 case DIFF_STATUS_COPIED:
275 case DIFF_STATUS_DELETED:
277 case DIFF_STATUS_MODIFIED:
278 return _("modified");
279 case DIFF_STATUS_RENAMED:
281 case DIFF_STATUS_TYPE_CHANGED:
282 return _("typechange");
283 case DIFF_STATUS_UNKNOWN:
285 case DIFF_STATUS_UNMERGED:
286 return _("unmerged");
292 static void wt_status_print_change_data(struct wt_status *s,
294 struct string_list_item *it)
296 struct wt_status_change_data *d = it->util;
297 const char *c = color(change_type, s);
301 const char *one, *two;
302 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
303 struct strbuf extra = STRBUF_INIT;
304 static char *padding;
310 /* If DIFF_STATUS_* uses outside this range, we're in trouble */
311 for (status = 'A'; status <= 'Z'; status++) {
312 what = wt_status_diff_status_string(status);
313 len = what ? strlen(what) : 0;
317 width += 2; /* colon and a space */
318 padding = xmallocz(width);
319 memset(padding, ' ', width);
322 one_name = two_name = it->string;
323 switch (change_type) {
324 case WT_STATUS_UPDATED:
325 status = d->index_status;
327 one_name = d->head_path;
329 case WT_STATUS_CHANGED:
330 if (d->new_submodule_commits || d->dirty_submodule) {
331 strbuf_addstr(&extra, " (");
332 if (d->new_submodule_commits)
333 strbuf_addf(&extra, _("new commits, "));
334 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
335 strbuf_addf(&extra, _("modified content, "));
336 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
337 strbuf_addf(&extra, _("untracked content, "));
338 strbuf_setlen(&extra, extra.len - 2);
339 strbuf_addch(&extra, ')');
341 status = d->worktree_status;
344 die("BUG: unhandled change_type %d in wt_status_print_change_data",
348 one = quote_path(one_name, s->prefix, &onebuf);
349 two = quote_path(two_name, s->prefix, &twobuf);
351 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
352 what = wt_status_diff_status_string(status);
354 die(_("bug: unhandled diff status %c"), status);
355 /* 1 for colon, which is not part of "what" */
356 len = strlen(padding) - (utf8_strwidth(what) + 1);
358 if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
359 status_printf_more(s, c, "%s:%.*s%s -> %s",
360 what, len, padding, one, two);
362 status_printf_more(s, c, "%s:%.*s%s",
363 what, len, padding, one);
365 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
366 strbuf_release(&extra);
368 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
369 strbuf_release(&onebuf);
370 strbuf_release(&twobuf);
373 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
374 struct diff_options *options,
377 struct wt_status *s = data;
382 s->workdir_dirty = 1;
383 for (i = 0; i < q->nr; i++) {
384 struct diff_filepair *p;
385 struct string_list_item *it;
386 struct wt_status_change_data *d;
389 it = string_list_insert(&s->change, p->one->path);
392 d = xcalloc(1, sizeof(*d));
395 if (!d->worktree_status)
396 d->worktree_status = p->status;
397 d->dirty_submodule = p->two->dirty_submodule;
398 if (S_ISGITLINK(p->two->mode))
399 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
403 static int unmerged_mask(const char *path)
406 const struct cache_entry *ce;
408 pos = cache_name_pos(path, strlen(path));
414 while (pos < active_nr) {
415 ce = active_cache[pos++];
416 if (strcmp(ce->name, path) || !ce_stage(ce))
418 mask |= (1 << (ce_stage(ce) - 1));
423 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
424 struct diff_options *options,
427 struct wt_status *s = data;
430 for (i = 0; i < q->nr; i++) {
431 struct diff_filepair *p;
432 struct string_list_item *it;
433 struct wt_status_change_data *d;
436 it = string_list_insert(&s->change, p->two->path);
439 d = xcalloc(1, sizeof(*d));
442 if (!d->index_status)
443 d->index_status = p->status;
445 case DIFF_STATUS_COPIED:
446 case DIFF_STATUS_RENAMED:
447 d->head_path = xstrdup(p->one->path);
449 case DIFF_STATUS_UNMERGED:
450 d->stagemask = unmerged_mask(p->two->path);
456 static void wt_status_collect_changes_worktree(struct wt_status *s)
460 init_revisions(&rev, NULL);
461 setup_revisions(0, NULL, &rev, NULL);
462 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
463 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
464 if (!s->show_untracked_files)
465 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
466 if (s->ignore_submodule_arg) {
467 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
468 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
470 rev.diffopt.format_callback = wt_status_collect_changed_cb;
471 rev.diffopt.format_callback_data = s;
472 copy_pathspec(&rev.prune_data, &s->pathspec);
473 run_diff_files(&rev, 0);
476 static void wt_status_collect_changes_index(struct wt_status *s)
479 struct setup_revision_opt opt;
481 init_revisions(&rev, NULL);
482 memset(&opt, 0, sizeof(opt));
483 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
484 setup_revisions(0, NULL, &rev, &opt);
486 if (s->ignore_submodule_arg) {
487 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
488 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
491 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
492 rev.diffopt.format_callback = wt_status_collect_updated_cb;
493 rev.diffopt.format_callback_data = s;
494 rev.diffopt.detect_rename = 1;
495 rev.diffopt.rename_limit = 200;
496 rev.diffopt.break_opt = 0;
497 copy_pathspec(&rev.prune_data, &s->pathspec);
498 run_diff_index(&rev, 1);
501 static void wt_status_collect_changes_initial(struct wt_status *s)
505 for (i = 0; i < active_nr; i++) {
506 struct string_list_item *it;
507 struct wt_status_change_data *d;
508 const struct cache_entry *ce = active_cache[i];
510 if (!ce_path_match(ce, &s->pathspec))
512 it = string_list_insert(&s->change, ce->name);
515 d = xcalloc(1, sizeof(*d));
519 d->index_status = DIFF_STATUS_UNMERGED;
520 d->stagemask |= (1 << (ce_stage(ce) - 1));
523 d->index_status = DIFF_STATUS_ADDED;
527 static void wt_status_collect_untracked(struct wt_status *s)
530 struct dir_struct dir;
531 struct timeval t_begin;
533 if (!s->show_untracked_files)
536 if (advice_status_u_option)
537 gettimeofday(&t_begin, NULL);
539 memset(&dir, 0, sizeof(dir));
540 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
542 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
543 if (s->show_ignored_files)
544 dir.flags |= DIR_SHOW_IGNORED_TOO;
545 setup_standard_excludes(&dir);
547 fill_directory(&dir, &s->pathspec);
549 for (i = 0; i < dir.nr; i++) {
550 struct dir_entry *ent = dir.entries[i];
551 if (cache_name_is_other(ent->name, ent->len) &&
552 match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
553 string_list_insert(&s->untracked, ent->name);
557 for (i = 0; i < dir.ignored_nr; i++) {
558 struct dir_entry *ent = dir.ignored[i];
559 if (cache_name_is_other(ent->name, ent->len) &&
560 match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
561 string_list_insert(&s->ignored, ent->name);
567 clear_directory(&dir);
569 if (advice_status_u_option) {
570 struct timeval t_end;
571 gettimeofday(&t_end, NULL);
573 (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
574 ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
578 void wt_status_collect(struct wt_status *s)
580 wt_status_collect_changes_worktree(s);
583 wt_status_collect_changes_initial(s);
585 wt_status_collect_changes_index(s);
586 wt_status_collect_untracked(s);
589 static void wt_status_print_unmerged(struct wt_status *s)
591 int shown_header = 0;
594 for (i = 0; i < s->change.nr; i++) {
595 struct wt_status_change_data *d;
596 struct string_list_item *it;
597 it = &(s->change.items[i]);
602 wt_status_print_unmerged_header(s);
605 wt_status_print_unmerged_data(s, it);
608 wt_status_print_trailer(s);
612 static void wt_status_print_updated(struct wt_status *s)
614 int shown_header = 0;
617 for (i = 0; i < s->change.nr; i++) {
618 struct wt_status_change_data *d;
619 struct string_list_item *it;
620 it = &(s->change.items[i]);
622 if (!d->index_status ||
623 d->index_status == DIFF_STATUS_UNMERGED)
626 wt_status_print_cached_header(s);
630 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
633 wt_status_print_trailer(s);
639 * 1 : some change but no delete
641 static int wt_status_check_worktree_changes(struct wt_status *s,
642 int *dirty_submodules)
647 *dirty_submodules = 0;
649 for (i = 0; i < s->change.nr; i++) {
650 struct wt_status_change_data *d;
651 d = s->change.items[i].util;
652 if (!d->worktree_status ||
653 d->worktree_status == DIFF_STATUS_UNMERGED)
657 if (d->dirty_submodule)
658 *dirty_submodules = 1;
659 if (d->worktree_status == DIFF_STATUS_DELETED)
665 static void wt_status_print_changed(struct wt_status *s)
667 int i, dirty_submodules;
668 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
670 if (!worktree_changes)
673 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
675 for (i = 0; i < s->change.nr; i++) {
676 struct wt_status_change_data *d;
677 struct string_list_item *it;
678 it = &(s->change.items[i]);
680 if (!d->worktree_status ||
681 d->worktree_status == DIFF_STATUS_UNMERGED)
683 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
685 wt_status_print_trailer(s);
688 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
690 struct child_process sm_summary;
691 char summary_limit[64];
692 char index[PATH_MAX];
693 const char *env[] = { NULL, NULL };
694 struct argv_array argv = ARGV_ARRAY_INIT;
695 struct strbuf cmd_stdout = STRBUF_INIT;
696 struct strbuf summary = STRBUF_INIT;
697 char *summary_content;
700 sprintf(summary_limit, "%d", s->submodule_summary);
701 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
704 argv_array_push(&argv, "submodule");
705 argv_array_push(&argv, "summary");
706 argv_array_push(&argv, uncommitted ? "--files" : "--cached");
707 argv_array_push(&argv, "--for-status");
708 argv_array_push(&argv, "--summary-limit");
709 argv_array_push(&argv, summary_limit);
711 argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
713 memset(&sm_summary, 0, sizeof(sm_summary));
714 sm_summary.argv = argv.argv;
715 sm_summary.env = env;
716 sm_summary.git_cmd = 1;
717 sm_summary.no_stdin = 1;
721 run_command(&sm_summary);
722 argv_array_clear(&argv);
724 len = strbuf_read(&cmd_stdout, sm_summary.out, 1024);
726 /* prepend header, only if there's an actual output */
729 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
731 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
732 strbuf_addstr(&summary, "\n\n");
734 strbuf_addbuf(&summary, &cmd_stdout);
735 strbuf_release(&cmd_stdout);
737 if (s->display_comment_prefix) {
738 summary_content = strbuf_detach(&summary, &len);
739 strbuf_add_commented_lines(&summary, summary_content, len);
740 free(summary_content);
743 fputs(summary.buf, s->fp);
744 strbuf_release(&summary);
747 static void wt_status_print_other(struct wt_status *s,
748 struct string_list *l,
753 struct strbuf buf = STRBUF_INIT;
754 static struct string_list output = STRING_LIST_INIT_DUP;
755 struct column_options copts;
760 wt_status_print_other_header(s, what, how);
762 for (i = 0; i < l->nr; i++) {
763 struct string_list_item *it;
766 path = quote_path(it->string, s->prefix, &buf);
767 if (column_active(s->colopts)) {
768 string_list_append(&output, path);
771 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
772 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
776 strbuf_release(&buf);
777 if (!column_active(s->colopts))
780 strbuf_addf(&buf, "%s%s\t%s",
781 color(WT_STATUS_HEADER, s),
782 s->display_comment_prefix ? "#" : "",
783 color(WT_STATUS_UNTRACKED, s));
784 memset(&copts, 0, sizeof(copts));
786 copts.indent = buf.buf;
787 if (want_color(s->use_color))
788 copts.nl = GIT_COLOR_RESET "\n";
789 print_columns(&output, s->colopts, &copts);
790 string_list_clear(&output, 0);
791 strbuf_release(&buf);
793 status_printf_ln(s, GIT_COLOR_NORMAL, "");
796 static void wt_status_print_verbose(struct wt_status *s)
799 struct setup_revision_opt opt;
801 init_revisions(&rev, NULL);
802 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
804 memset(&opt, 0, sizeof(opt));
805 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
806 setup_revisions(0, NULL, &rev, &opt);
808 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
809 rev.diffopt.detect_rename = 1;
810 rev.diffopt.file = s->fp;
811 rev.diffopt.close_file = 0;
813 * If we're not going to stdout, then we definitely don't
814 * want color, since we are going to the commit message
815 * file (and even the "auto" setting won't work, since it
816 * will have checked isatty on stdout).
819 rev.diffopt.use_color = 0;
820 run_diff_index(&rev, 1);
823 static void wt_status_print_tracking(struct wt_status *s)
825 struct strbuf sb = STRBUF_INIT;
827 struct branch *branch;
828 char comment_line_string[3];
831 assert(s->branch && !s->is_initial);
832 if (prefixcmp(s->branch, "refs/heads/"))
834 branch = branch_get(s->branch + 11);
835 if (!format_tracking_info(branch, &sb))
839 if (s->display_comment_prefix) {
840 comment_line_string[i++] = comment_line_char;
841 comment_line_string[i++] = ' ';
843 comment_line_string[i] = '\0';
845 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
846 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
847 "%s%.*s", comment_line_string,
849 if (s->display_comment_prefix)
850 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
853 fprintf_ln(s->fp, "");
856 static int has_unmerged(struct wt_status *s)
860 for (i = 0; i < s->change.nr; i++) {
861 struct wt_status_change_data *d;
862 d = s->change.items[i].util;
869 static void show_merge_in_progress(struct wt_status *s,
870 struct wt_status_state *state,
873 if (has_unmerged(s)) {
874 status_printf_ln(s, color, _("You have unmerged paths."));
876 status_printf_ln(s, color,
877 _(" (fix conflicts and run \"git commit\")"));
879 status_printf_ln(s, color,
880 _("All conflicts fixed but you are still merging."));
882 status_printf_ln(s, color,
883 _(" (use \"git commit\" to conclude merge)"));
885 wt_status_print_trailer(s);
888 static void show_am_in_progress(struct wt_status *s,
889 struct wt_status_state *state,
892 status_printf_ln(s, color,
893 _("You are in the middle of an am session."));
894 if (state->am_empty_patch)
895 status_printf_ln(s, color,
896 _("The current patch is empty."));
898 if (!state->am_empty_patch)
899 status_printf_ln(s, color,
900 _(" (fix conflicts and then run \"git am --continue\")"));
901 status_printf_ln(s, color,
902 _(" (use \"git am --skip\" to skip this patch)"));
903 status_printf_ln(s, color,
904 _(" (use \"git am --abort\" to restore the original branch)"));
906 wt_status_print_trailer(s);
909 static char *read_line_from_git_path(const char *filename)
911 struct strbuf buf = STRBUF_INIT;
912 FILE *fp = fopen(git_path("%s", filename), "r");
914 strbuf_release(&buf);
917 strbuf_getline(&buf, fp, '\n');
919 return strbuf_detach(&buf, NULL);
921 strbuf_release(&buf);
926 static int split_commit_in_progress(struct wt_status *s)
928 int split_in_progress = 0;
929 char *head = read_line_from_git_path("HEAD");
930 char *orig_head = read_line_from_git_path("ORIG_HEAD");
931 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
932 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
934 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
935 !s->branch || strcmp(s->branch, "HEAD"))
936 return split_in_progress;
938 if (!strcmp(rebase_amend, rebase_orig_head)) {
939 if (strcmp(head, rebase_amend))
940 split_in_progress = 1;
941 } else if (strcmp(orig_head, rebase_orig_head)) {
942 split_in_progress = 1;
945 if (!s->amend && !s->nowarn && !s->workdir_dirty)
946 split_in_progress = 0;
951 free(rebase_orig_head);
952 return split_in_progress;
955 static void show_rebase_in_progress(struct wt_status *s,
956 struct wt_status_state *state,
961 if (has_unmerged(s)) {
963 status_printf_ln(s, color,
964 _("You are currently rebasing branch '%s' on '%s'."),
968 status_printf_ln(s, color,
969 _("You are currently rebasing."));
971 status_printf_ln(s, color,
972 _(" (fix conflicts and then run \"git rebase --continue\")"));
973 status_printf_ln(s, color,
974 _(" (use \"git rebase --skip\" to skip this patch)"));
975 status_printf_ln(s, color,
976 _(" (use \"git rebase --abort\" to check out the original branch)"));
978 } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
980 status_printf_ln(s, color,
981 _("You are currently rebasing branch '%s' on '%s'."),
985 status_printf_ln(s, color,
986 _("You are currently rebasing."));
988 status_printf_ln(s, color,
989 _(" (all conflicts fixed: run \"git rebase --continue\")"));
990 } else if (split_commit_in_progress(s)) {
992 status_printf_ln(s, color,
993 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
997 status_printf_ln(s, color,
998 _("You are currently splitting a commit during a rebase."));
1000 status_printf_ln(s, color,
1001 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1004 status_printf_ln(s, color,
1005 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1009 status_printf_ln(s, color,
1010 _("You are currently editing a commit during a rebase."));
1011 if (s->hints && !s->amend) {
1012 status_printf_ln(s, color,
1013 _(" (use \"git commit --amend\" to amend the current commit)"));
1014 status_printf_ln(s, color,
1015 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1018 wt_status_print_trailer(s);
1021 static void show_cherry_pick_in_progress(struct wt_status *s,
1022 struct wt_status_state *state,
1025 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1026 find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
1028 if (has_unmerged(s))
1029 status_printf_ln(s, color,
1030 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1032 status_printf_ln(s, color,
1033 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1034 status_printf_ln(s, color,
1035 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1037 wt_status_print_trailer(s);
1040 static void show_revert_in_progress(struct wt_status *s,
1041 struct wt_status_state *state,
1044 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1045 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
1047 if (has_unmerged(s))
1048 status_printf_ln(s, color,
1049 _(" (fix conflicts and run \"git revert --continue\")"));
1051 status_printf_ln(s, color,
1052 _(" (all conflicts fixed: run \"git revert --continue\")"));
1053 status_printf_ln(s, color,
1054 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1056 wt_status_print_trailer(s);
1059 static void show_bisect_in_progress(struct wt_status *s,
1060 struct wt_status_state *state,
1064 status_printf_ln(s, color,
1065 _("You are currently bisecting, started from branch '%s'."),
1068 status_printf_ln(s, color,
1069 _("You are currently bisecting."));
1071 status_printf_ln(s, color,
1072 _(" (use \"git bisect reset\" to get back to the original branch)"));
1073 wt_status_print_trailer(s);
1077 * Extract branch information from rebase/bisect
1079 static char *read_and_strip_branch(const char *path)
1081 struct strbuf sb = STRBUF_INIT;
1082 unsigned char sha1[20];
1084 if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1087 while (&sb.len && sb.buf[sb.len - 1] == '\n')
1088 strbuf_setlen(&sb, sb.len - 1);
1091 if (!prefixcmp(sb.buf, "refs/heads/"))
1092 strbuf_remove(&sb,0, strlen("refs/heads/"));
1093 else if (!prefixcmp(sb.buf, "refs/"))
1095 else if (!get_sha1_hex(sb.buf, sha1)) {
1097 abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1099 strbuf_addstr(&sb, abbrev);
1100 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1104 return strbuf_detach(&sb, NULL);
1107 strbuf_release(&sb);
1111 struct grab_1st_switch_cbdata {
1113 unsigned char nsha1[20];
1116 static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
1117 const char *email, unsigned long timestamp, int tz,
1118 const char *message, void *cb_data)
1120 struct grab_1st_switch_cbdata *cb = cb_data;
1121 const char *target = NULL, *end;
1123 if (prefixcmp(message, "checkout: moving from "))
1125 message += strlen("checkout: moving from ");
1126 target = strstr(message, " to ");
1129 target += strlen(" to ");
1130 strbuf_reset(&cb->buf);
1131 hashcpy(cb->nsha1, nsha1);
1132 for (end = target; *end && *end != '\n'; end++)
1134 strbuf_add(&cb->buf, target, end - target);
1138 static void wt_status_get_detached_from(struct wt_status_state *state)
1140 struct grab_1st_switch_cbdata cb;
1141 struct commit *commit;
1142 unsigned char sha1[20];
1145 strbuf_init(&cb.buf, 0);
1146 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1147 strbuf_release(&cb.buf);
1151 if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 &&
1152 /* sha1 is a commit? match without further lookup */
1153 (!hashcmp(cb.nsha1, sha1) ||
1154 /* perhaps sha1 is a tag, try to dereference to a commit */
1155 ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
1156 !hashcmp(cb.nsha1, commit->object.sha1)))) {
1158 if (!prefixcmp(ref, "refs/tags/"))
1159 ofs = strlen("refs/tags/");
1160 else if (!prefixcmp(ref, "refs/remotes/"))
1161 ofs = strlen("refs/remotes/");
1164 state->detached_from = xstrdup(ref + ofs);
1166 state->detached_from =
1167 xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
1168 hashcpy(state->detached_sha1, cb.nsha1);
1171 strbuf_release(&cb.buf);
1174 void wt_status_get_state(struct wt_status_state *state,
1175 int get_detached_from)
1178 unsigned char sha1[20];
1180 if (!stat(git_path("MERGE_HEAD"), &st)) {
1181 state->merge_in_progress = 1;
1182 } else if (!stat(git_path("rebase-apply"), &st)) {
1183 if (!stat(git_path("rebase-apply/applying"), &st)) {
1184 state->am_in_progress = 1;
1185 if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1186 state->am_empty_patch = 1;
1188 state->rebase_in_progress = 1;
1189 state->branch = read_and_strip_branch("rebase-apply/head-name");
1190 state->onto = read_and_strip_branch("rebase-apply/onto");
1192 } else if (!stat(git_path("rebase-merge"), &st)) {
1193 if (!stat(git_path("rebase-merge/interactive"), &st))
1194 state->rebase_interactive_in_progress = 1;
1196 state->rebase_in_progress = 1;
1197 state->branch = read_and_strip_branch("rebase-merge/head-name");
1198 state->onto = read_and_strip_branch("rebase-merge/onto");
1199 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st) &&
1200 !get_sha1("CHERRY_PICK_HEAD", sha1)) {
1201 state->cherry_pick_in_progress = 1;
1202 hashcpy(state->cherry_pick_head_sha1, sha1);
1204 if (!stat(git_path("BISECT_LOG"), &st)) {
1205 state->bisect_in_progress = 1;
1206 state->branch = read_and_strip_branch("BISECT_START");
1208 if (!stat(git_path("REVERT_HEAD"), &st) &&
1209 !get_sha1("REVERT_HEAD", sha1)) {
1210 state->revert_in_progress = 1;
1211 hashcpy(state->revert_head_sha1, sha1);
1214 if (get_detached_from)
1215 wt_status_get_detached_from(state);
1218 static void wt_status_print_state(struct wt_status *s,
1219 struct wt_status_state *state)
1221 const char *state_color = color(WT_STATUS_HEADER, s);
1222 if (state->merge_in_progress)
1223 show_merge_in_progress(s, state, state_color);
1224 else if (state->am_in_progress)
1225 show_am_in_progress(s, state, state_color);
1226 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1227 show_rebase_in_progress(s, state, state_color);
1228 else if (state->cherry_pick_in_progress)
1229 show_cherry_pick_in_progress(s, state, state_color);
1230 else if (state->revert_in_progress)
1231 show_revert_in_progress(s, state, state_color);
1232 if (state->bisect_in_progress)
1233 show_bisect_in_progress(s, state, state_color);
1236 void wt_status_print(struct wt_status *s)
1238 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1239 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1240 struct wt_status_state state;
1242 memset(&state, 0, sizeof(state));
1243 wt_status_get_state(&state,
1244 s->branch && !strcmp(s->branch, "HEAD"));
1247 const char *on_what = _("On branch ");
1248 const char *branch_name = s->branch;
1249 if (!prefixcmp(branch_name, "refs/heads/"))
1251 else if (!strcmp(branch_name, "HEAD")) {
1252 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1253 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1254 on_what = _("rebase in progress; onto ");
1255 branch_name = state.onto;
1256 } else if (state.detached_from) {
1257 unsigned char sha1[20];
1258 branch_name = state.detached_from;
1259 if (!get_sha1("HEAD", sha1) &&
1260 !hashcmp(sha1, state.detached_sha1))
1261 on_what = _("HEAD detached at ");
1263 on_what = _("HEAD detached from ");
1266 on_what = _("Not currently on any branch.");
1269 status_printf(s, color(WT_STATUS_HEADER, s), "");
1270 status_printf_more(s, branch_status_color, "%s", on_what);
1271 status_printf_more(s, branch_color, "%s\n", branch_name);
1273 wt_status_print_tracking(s);
1276 wt_status_print_state(s, &state);
1279 free(state.detached_from);
1281 if (s->is_initial) {
1282 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1283 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1284 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1287 wt_status_print_updated(s);
1288 wt_status_print_unmerged(s);
1289 wt_status_print_changed(s);
1290 if (s->submodule_summary &&
1291 (!s->ignore_submodule_arg ||
1292 strcmp(s->ignore_submodule_arg, "all"))) {
1293 wt_status_print_submodule_summary(s, 0); /* staged */
1294 wt_status_print_submodule_summary(s, 1); /* unstaged */
1296 if (s->show_untracked_files) {
1297 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1298 if (s->show_ignored_files)
1299 wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1300 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1301 status_printf_ln(s, GIT_COLOR_NORMAL, "");
1302 status_printf_ln(s, GIT_COLOR_NORMAL,
1303 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1304 "may speed it up, but you have to be careful not to forget to add\n"
1305 "new files yourself (see 'git help status')."),
1306 s->untracked_in_ms / 1000.0);
1308 } else if (s->commitable)
1309 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1311 ? _(" (use -u option to show untracked files)") : "");
1314 wt_status_print_verbose(s);
1315 if (!s->commitable) {
1317 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1320 else if (s->workdir_dirty) {
1322 printf(_("no changes added to commit "
1323 "(use \"git add\" and/or \"git commit -a\")\n"));
1325 printf(_("no changes added to commit\n"));
1326 } else if (s->untracked.nr) {
1328 printf(_("nothing added to commit but untracked files "
1329 "present (use \"git add\" to track)\n"));
1331 printf(_("nothing added to commit but untracked files present\n"));
1332 } else if (s->is_initial) {
1334 printf(_("nothing to commit (create/copy files "
1335 "and use \"git add\" to track)\n"));
1337 printf(_("nothing to commit\n"));
1338 } else if (!s->show_untracked_files) {
1340 printf(_("nothing to commit (use -u to show untracked files)\n"));
1342 printf(_("nothing to commit\n"));
1344 printf(_("nothing to commit, working directory clean\n"));
1348 static void wt_shortstatus_unmerged(struct string_list_item *it,
1349 struct wt_status *s)
1351 struct wt_status_change_data *d = it->util;
1352 const char *how = "??";
1354 switch (d->stagemask) {
1355 case 1: how = "DD"; break; /* both deleted */
1356 case 2: how = "AU"; break; /* added by us */
1357 case 3: how = "UD"; break; /* deleted by them */
1358 case 4: how = "UA"; break; /* added by them */
1359 case 5: how = "DU"; break; /* deleted by us */
1360 case 6: how = "AA"; break; /* both added */
1361 case 7: how = "UU"; break; /* both modified */
1363 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1364 if (s->null_termination) {
1365 fprintf(stdout, " %s%c", it->string, 0);
1367 struct strbuf onebuf = STRBUF_INIT;
1369 one = quote_path(it->string, s->prefix, &onebuf);
1370 printf(" %s\n", one);
1371 strbuf_release(&onebuf);
1375 static void wt_shortstatus_status(struct string_list_item *it,
1376 struct wt_status *s)
1378 struct wt_status_change_data *d = it->util;
1380 if (d->index_status)
1381 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1384 if (d->worktree_status)
1385 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1389 if (s->null_termination) {
1390 fprintf(stdout, "%s%c", it->string, 0);
1392 fprintf(stdout, "%s%c", d->head_path, 0);
1394 struct strbuf onebuf = STRBUF_INIT;
1397 one = quote_path(d->head_path, s->prefix, &onebuf);
1398 if (*one != '"' && strchr(one, ' ') != NULL) {
1400 strbuf_addch(&onebuf, '"');
1403 printf("%s -> ", one);
1404 strbuf_release(&onebuf);
1406 one = quote_path(it->string, s->prefix, &onebuf);
1407 if (*one != '"' && strchr(one, ' ') != NULL) {
1409 strbuf_addch(&onebuf, '"');
1412 printf("%s\n", one);
1413 strbuf_release(&onebuf);
1417 static void wt_shortstatus_other(struct string_list_item *it,
1418 struct wt_status *s, const char *sign)
1420 if (s->null_termination) {
1421 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1423 struct strbuf onebuf = STRBUF_INIT;
1425 one = quote_path(it->string, s->prefix, &onebuf);
1426 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1427 printf(" %s\n", one);
1428 strbuf_release(&onebuf);
1432 static void wt_shortstatus_print_tracking(struct wt_status *s)
1434 struct branch *branch;
1435 const char *header_color = color(WT_STATUS_HEADER, s);
1436 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1437 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1440 const char *branch_name;
1441 int num_ours, num_theirs;
1442 int upstream_is_gone = 0;
1444 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1448 branch_name = s->branch;
1450 if (!prefixcmp(branch_name, "refs/heads/"))
1452 else if (!strcmp(branch_name, "HEAD")) {
1453 branch_name = _("HEAD (no branch)");
1454 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1457 branch = branch_get(s->branch + 11);
1459 color_fprintf(s->fp, header_color, _("Initial commit on "));
1461 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1463 switch (stat_tracking_info(branch, &num_ours, &num_theirs)) {
1466 fputc(s->null_termination ? '\0' : '\n', s->fp);
1469 /* with "gone" base */
1470 upstream_is_gone = 1;
1477 base = branch->merge[0]->dst;
1478 base = shorten_unambiguous_ref(base, 0);
1479 color_fprintf(s->fp, header_color, "...");
1480 color_fprintf(s->fp, branch_color_remote, "%s", base);
1482 if (!upstream_is_gone && !num_ours && !num_theirs) {
1483 fputc(s->null_termination ? '\0' : '\n', s->fp);
1487 color_fprintf(s->fp, header_color, " [");
1488 if (upstream_is_gone) {
1489 color_fprintf(s->fp, header_color, _("gone"));
1490 } else if (!num_ours) {
1491 color_fprintf(s->fp, header_color, _("behind "));
1492 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1493 } else if (!num_theirs) {
1494 color_fprintf(s->fp, header_color, _("ahead "));
1495 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1497 color_fprintf(s->fp, header_color, _("ahead "));
1498 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1499 color_fprintf(s->fp, header_color, _(", behind "));
1500 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1503 color_fprintf(s->fp, header_color, "]");
1504 fputc(s->null_termination ? '\0' : '\n', s->fp);
1507 void wt_shortstatus_print(struct wt_status *s)
1512 wt_shortstatus_print_tracking(s);
1514 for (i = 0; i < s->change.nr; i++) {
1515 struct wt_status_change_data *d;
1516 struct string_list_item *it;
1518 it = &(s->change.items[i]);
1521 wt_shortstatus_unmerged(it, s);
1523 wt_shortstatus_status(it, s);
1525 for (i = 0; i < s->untracked.nr; i++) {
1526 struct string_list_item *it;
1528 it = &(s->untracked.items[i]);
1529 wt_shortstatus_other(it, s, "??");
1531 for (i = 0; i < s->ignored.nr; i++) {
1532 struct string_list_item *it;
1534 it = &(s->ignored.items[i]);
1535 wt_shortstatus_other(it, s, "!!");
1539 void wt_porcelain_print(struct wt_status *s)
1542 s->relative_paths = 0;
1544 wt_shortstatus_print(s);