10 #include "run-command.h"
13 static char default_wt_status_colors[][COLOR_MAXLEN] = {
14 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
15 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
16 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
17 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
18 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
19 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
22 static const char *color(int slot, struct wt_status *s)
24 return s->use_color > 0 ? s->color_palette[slot] : "";
27 void wt_status_prepare(struct wt_status *s)
29 unsigned char sha1[20];
32 memset(s, 0, sizeof(*s));
33 memcpy(s->color_palette, default_wt_status_colors,
34 sizeof(default_wt_status_colors));
35 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
37 s->relative_paths = 1;
38 head = resolve_ref("HEAD", sha1, 0, NULL);
39 s->branch = head ? xstrdup(head) : NULL;
40 s->reference = "HEAD";
42 s->index_file = get_index_file();
43 s->change.strdup_strings = 1;
44 s->untracked.strdup_strings = 1;
45 s->ignored.strdup_strings = 1;
48 static void wt_status_print_unmerged_header(struct wt_status *s)
50 const char *c = color(WT_STATUS_HEADER, s);
52 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
53 if (!advice_status_hints)
57 else if (!s->is_initial)
58 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
60 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
61 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
62 color_fprintf_ln(s->fp, c, "#");
65 static void wt_status_print_cached_header(struct wt_status *s)
67 const char *c = color(WT_STATUS_HEADER, s);
69 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
70 if (!advice_status_hints)
73 ; /* NEEDSWORK: use "git reset --unresolve"??? */
74 else if (!s->is_initial)
75 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
77 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
78 color_fprintf_ln(s->fp, c, "#");
81 static void wt_status_print_dirty_header(struct wt_status *s,
83 int has_dirty_submodules)
85 const char *c = color(WT_STATUS_HEADER, s);
87 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
88 if (!advice_status_hints)
91 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
93 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
94 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
95 if (has_dirty_submodules)
96 color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)");
97 color_fprintf_ln(s->fp, c, "#");
100 static void wt_status_print_other_header(struct wt_status *s,
104 const char *c = color(WT_STATUS_HEADER, s);
105 color_fprintf_ln(s->fp, c, "# %s files:", what);
106 if (!advice_status_hints)
108 color_fprintf_ln(s->fp, c, "# (use \"git %s <file>...\" to include in what will be committed)", how);
109 color_fprintf_ln(s->fp, c, "#");
112 static void wt_status_print_trailer(struct wt_status *s)
114 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
117 #define quote_path quote_path_relative
119 static void wt_status_print_unmerged_data(struct wt_status *s,
120 struct string_list_item *it)
122 const char *c = color(WT_STATUS_UNMERGED, s);
123 struct wt_status_change_data *d = it->util;
124 struct strbuf onebuf = STRBUF_INIT;
125 const char *one, *how = "bug";
127 one = quote_path(it->string, -1, &onebuf, s->prefix);
128 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
129 switch (d->stagemask) {
130 case 1: how = "both deleted:"; break;
131 case 2: how = "added by us:"; break;
132 case 3: how = "deleted by them:"; break;
133 case 4: how = "added by them:"; break;
134 case 5: how = "deleted by us:"; break;
135 case 6: how = "both added:"; break;
136 case 7: how = "both modified:"; break;
138 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
139 strbuf_release(&onebuf);
142 static void wt_status_print_change_data(struct wt_status *s,
144 struct string_list_item *it)
146 struct wt_status_change_data *d = it->util;
147 const char *c = color(change_type, s);
151 const char *one, *two;
152 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
153 struct strbuf extra = STRBUF_INIT;
155 one_name = two_name = it->string;
156 switch (change_type) {
157 case WT_STATUS_UPDATED:
158 status = d->index_status;
160 one_name = d->head_path;
162 case WT_STATUS_CHANGED:
163 if (d->new_submodule_commits || d->dirty_submodule) {
164 strbuf_addstr(&extra, " (");
165 if (d->new_submodule_commits)
166 strbuf_addf(&extra, "new commits, ");
167 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
168 strbuf_addf(&extra, "modified content, ");
169 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
170 strbuf_addf(&extra, "untracked content, ");
171 strbuf_setlen(&extra, extra.len - 2);
172 strbuf_addch(&extra, ')');
174 status = d->worktree_status;
178 one = quote_path(one_name, -1, &onebuf, s->prefix);
179 two = quote_path(two_name, -1, &twobuf, s->prefix);
181 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
183 case DIFF_STATUS_ADDED:
184 color_fprintf(s->fp, c, "new file: %s", one);
186 case DIFF_STATUS_COPIED:
187 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
189 case DIFF_STATUS_DELETED:
190 color_fprintf(s->fp, c, "deleted: %s", one);
192 case DIFF_STATUS_MODIFIED:
193 color_fprintf(s->fp, c, "modified: %s", one);
195 case DIFF_STATUS_RENAMED:
196 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
198 case DIFF_STATUS_TYPE_CHANGED:
199 color_fprintf(s->fp, c, "typechange: %s", one);
201 case DIFF_STATUS_UNKNOWN:
202 color_fprintf(s->fp, c, "unknown: %s", one);
204 case DIFF_STATUS_UNMERGED:
205 color_fprintf(s->fp, c, "unmerged: %s", one);
208 die("bug: unhandled diff status %c", status);
211 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
212 strbuf_release(&extra);
214 fprintf(s->fp, "\n");
215 strbuf_release(&onebuf);
216 strbuf_release(&twobuf);
219 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
220 struct diff_options *options,
223 struct wt_status *s = data;
228 s->workdir_dirty = 1;
229 for (i = 0; i < q->nr; i++) {
230 struct diff_filepair *p;
231 struct string_list_item *it;
232 struct wt_status_change_data *d;
235 it = string_list_insert(p->one->path, &s->change);
238 d = xcalloc(1, sizeof(*d));
241 if (!d->worktree_status)
242 d->worktree_status = p->status;
243 d->dirty_submodule = p->two->dirty_submodule;
244 if (S_ISGITLINK(p->two->mode))
245 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
249 static int unmerged_mask(const char *path)
252 struct cache_entry *ce;
254 pos = cache_name_pos(path, strlen(path));
260 while (pos < active_nr) {
261 ce = active_cache[pos++];
262 if (strcmp(ce->name, path) || !ce_stage(ce))
264 mask |= (1 << (ce_stage(ce) - 1));
269 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
270 struct diff_options *options,
273 struct wt_status *s = data;
276 for (i = 0; i < q->nr; i++) {
277 struct diff_filepair *p;
278 struct string_list_item *it;
279 struct wt_status_change_data *d;
282 it = string_list_insert(p->two->path, &s->change);
285 d = xcalloc(1, sizeof(*d));
288 if (!d->index_status)
289 d->index_status = p->status;
291 case DIFF_STATUS_COPIED:
292 case DIFF_STATUS_RENAMED:
293 d->head_path = xstrdup(p->one->path);
295 case DIFF_STATUS_UNMERGED:
296 d->stagemask = unmerged_mask(p->two->path);
302 static void wt_status_collect_changes_worktree(struct wt_status *s)
306 init_revisions(&rev, NULL);
307 setup_revisions(0, NULL, &rev, NULL);
308 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
309 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
310 if (!s->show_untracked_files)
311 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
312 rev.diffopt.format_callback = wt_status_collect_changed_cb;
313 rev.diffopt.format_callback_data = s;
314 rev.prune_data = s->pathspec;
315 run_diff_files(&rev, 0);
318 static void wt_status_collect_changes_index(struct wt_status *s)
321 struct setup_revision_opt opt;
323 init_revisions(&rev, NULL);
324 memset(&opt, 0, sizeof(opt));
325 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
326 setup_revisions(0, NULL, &rev, &opt);
328 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
329 rev.diffopt.format_callback = wt_status_collect_updated_cb;
330 rev.diffopt.format_callback_data = s;
331 rev.diffopt.detect_rename = 1;
332 rev.diffopt.rename_limit = 200;
333 rev.diffopt.break_opt = 0;
334 rev.prune_data = s->pathspec;
335 run_diff_index(&rev, 1);
338 static void wt_status_collect_changes_initial(struct wt_status *s)
342 for (i = 0; i < active_nr; i++) {
343 struct string_list_item *it;
344 struct wt_status_change_data *d;
345 struct cache_entry *ce = active_cache[i];
347 if (!ce_path_match(ce, s->pathspec))
349 it = string_list_insert(ce->name, &s->change);
352 d = xcalloc(1, sizeof(*d));
356 d->index_status = DIFF_STATUS_UNMERGED;
357 d->stagemask |= (1 << (ce_stage(ce) - 1));
360 d->index_status = DIFF_STATUS_ADDED;
364 static void wt_status_collect_untracked(struct wt_status *s)
367 struct dir_struct dir;
369 if (!s->show_untracked_files)
371 memset(&dir, 0, sizeof(dir));
372 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
374 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
375 setup_standard_excludes(&dir);
377 fill_directory(&dir, s->pathspec);
378 for (i = 0; i < dir.nr; i++) {
379 struct dir_entry *ent = dir.entries[i];
380 if (!cache_name_is_other(ent->name, ent->len))
382 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
384 string_list_insert(ent->name, &s->untracked);
388 if (s->show_ignored_files) {
390 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
391 fill_directory(&dir, s->pathspec);
392 for (i = 0; i < dir.nr; i++) {
393 struct dir_entry *ent = dir.entries[i];
394 if (!cache_name_is_other(ent->name, ent->len))
396 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
398 string_list_insert(ent->name, &s->ignored);
406 void wt_status_collect(struct wt_status *s)
408 wt_status_collect_changes_worktree(s);
411 wt_status_collect_changes_initial(s);
413 wt_status_collect_changes_index(s);
414 wt_status_collect_untracked(s);
417 static void wt_status_print_unmerged(struct wt_status *s)
419 int shown_header = 0;
422 for (i = 0; i < s->change.nr; i++) {
423 struct wt_status_change_data *d;
424 struct string_list_item *it;
425 it = &(s->change.items[i]);
430 wt_status_print_unmerged_header(s);
433 wt_status_print_unmerged_data(s, it);
436 wt_status_print_trailer(s);
440 static void wt_status_print_updated(struct wt_status *s)
442 int shown_header = 0;
445 for (i = 0; i < s->change.nr; i++) {
446 struct wt_status_change_data *d;
447 struct string_list_item *it;
448 it = &(s->change.items[i]);
450 if (!d->index_status ||
451 d->index_status == DIFF_STATUS_UNMERGED)
454 wt_status_print_cached_header(s);
458 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
461 wt_status_print_trailer(s);
467 * 1 : some change but no delete
469 static int wt_status_check_worktree_changes(struct wt_status *s,
470 int *dirty_submodules)
475 *dirty_submodules = 0;
477 for (i = 0; i < s->change.nr; i++) {
478 struct wt_status_change_data *d;
479 d = s->change.items[i].util;
480 if (!d->worktree_status ||
481 d->worktree_status == DIFF_STATUS_UNMERGED)
485 if (d->dirty_submodule)
486 *dirty_submodules = 1;
487 if (d->worktree_status == DIFF_STATUS_DELETED)
493 static void wt_status_print_changed(struct wt_status *s)
495 int i, dirty_submodules;
496 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
498 if (!worktree_changes)
501 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
503 for (i = 0; i < s->change.nr; i++) {
504 struct wt_status_change_data *d;
505 struct string_list_item *it;
506 it = &(s->change.items[i]);
508 if (!d->worktree_status ||
509 d->worktree_status == DIFF_STATUS_UNMERGED)
511 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
513 wt_status_print_trailer(s);
516 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
518 struct child_process sm_summary;
519 char summary_limit[64];
520 char index[PATH_MAX];
521 const char *env[] = { index, NULL };
522 const char *argv[] = {
525 uncommitted ? "--files" : "--cached",
529 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
533 sprintf(summary_limit, "%d", s->submodule_summary);
534 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
536 memset(&sm_summary, 0, sizeof(sm_summary));
537 sm_summary.argv = argv;
538 sm_summary.env = env;
539 sm_summary.git_cmd = 1;
540 sm_summary.no_stdin = 1;
542 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
543 run_command(&sm_summary);
546 static void wt_status_print_other(struct wt_status *s,
547 struct string_list *l,
552 struct strbuf buf = STRBUF_INIT;
554 if (!s->untracked.nr)
557 wt_status_print_other_header(s, what, how);
559 for (i = 0; i < l->nr; i++) {
560 struct string_list_item *it;
562 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
563 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
564 quote_path(it->string, strlen(it->string),
567 strbuf_release(&buf);
570 static void wt_status_print_verbose(struct wt_status *s)
573 struct setup_revision_opt opt;
575 init_revisions(&rev, NULL);
576 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
578 memset(&opt, 0, sizeof(opt));
579 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
580 setup_revisions(0, NULL, &rev, &opt);
582 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
583 rev.diffopt.detect_rename = 1;
584 rev.diffopt.file = s->fp;
585 rev.diffopt.close_file = 0;
587 * If we're not going to stdout, then we definitely don't
588 * want color, since we are going to the commit message
589 * file (and even the "auto" setting won't work, since it
590 * will have checked isatty on stdout).
593 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
594 run_diff_index(&rev, 1);
597 static void wt_status_print_tracking(struct wt_status *s)
599 struct strbuf sb = STRBUF_INIT;
601 struct branch *branch;
603 assert(s->branch && !s->is_initial);
604 if (prefixcmp(s->branch, "refs/heads/"))
606 branch = branch_get(s->branch + 11);
607 if (!format_tracking_info(branch, &sb))
610 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
611 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
612 "# %.*s", (int)(ep - cp), cp);
613 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
616 void wt_status_print(struct wt_status *s)
618 const char *branch_color = color(WT_STATUS_HEADER, s);
621 const char *on_what = "On branch ";
622 const char *branch_name = s->branch;
623 if (!prefixcmp(branch_name, "refs/heads/"))
625 else if (!strcmp(branch_name, "HEAD")) {
627 branch_color = color(WT_STATUS_NOBRANCH, s);
628 on_what = "Not currently on any branch.";
630 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
631 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
633 wt_status_print_tracking(s);
637 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
638 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
639 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
642 wt_status_print_updated(s);
643 wt_status_print_unmerged(s);
644 wt_status_print_changed(s);
645 if (s->submodule_summary) {
646 wt_status_print_submodule_summary(s, 0); /* staged */
647 wt_status_print_submodule_summary(s, 1); /* unstaged */
649 if (s->show_untracked_files) {
650 wt_status_print_other(s, &s->untracked, "Untracked", "add");
651 if (s->show_ignored_files)
652 wt_status_print_other(s, &s->ignored, "Ignored", "add -f");
653 } else if (s->commitable)
654 fprintf(s->fp, "# Untracked files not listed%s\n",
656 ? " (use -u option to show untracked files)" : "");
659 wt_status_print_verbose(s);
660 if (!s->commitable) {
662 fprintf(s->fp, "# No changes\n");
665 else if (s->workdir_dirty)
666 printf("no changes added to commit%s\n",
668 ? " (use \"git add\" and/or \"git commit -a\")" : "");
669 else if (s->untracked.nr)
670 printf("nothing added to commit but untracked files present%s\n",
672 ? " (use \"git add\" to track)" : "");
673 else if (s->is_initial)
674 printf("nothing to commit%s\n", advice_status_hints
675 ? " (create/copy files and use \"git add\" to track)" : "");
676 else if (!s->show_untracked_files)
677 printf("nothing to commit%s\n", advice_status_hints
678 ? " (use -u to show untracked files)" : "");
680 printf("nothing to commit%s\n", advice_status_hints
681 ? " (working directory clean)" : "");
685 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
688 struct wt_status_change_data *d = it->util;
689 const char *how = "??";
691 switch (d->stagemask) {
692 case 1: how = "DD"; break; /* both deleted */
693 case 2: how = "AU"; break; /* added by us */
694 case 3: how = "UD"; break; /* deleted by them */
695 case 4: how = "UA"; break; /* added by them */
696 case 5: how = "DU"; break; /* deleted by us */
697 case 6: how = "AA"; break; /* both added */
698 case 7: how = "UU"; break; /* both modified */
700 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
701 if (null_termination) {
702 fprintf(stdout, " %s%c", it->string, 0);
704 struct strbuf onebuf = STRBUF_INIT;
706 one = quote_path(it->string, -1, &onebuf, s->prefix);
707 printf(" %s\n", one);
708 strbuf_release(&onebuf);
712 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
715 struct wt_status_change_data *d = it->util;
718 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
721 if (d->worktree_status)
722 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
726 if (null_termination) {
727 fprintf(stdout, "%s%c", it->string, 0);
729 fprintf(stdout, "%s%c", d->head_path, 0);
731 struct strbuf onebuf = STRBUF_INIT;
734 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
735 printf("%s -> ", one);
736 strbuf_release(&onebuf);
738 one = quote_path(it->string, -1, &onebuf, s->prefix);
740 strbuf_release(&onebuf);
744 static void wt_shortstatus_other(int null_termination, struct string_list_item *it,
745 struct wt_status *s, const char *sign)
747 if (null_termination) {
748 fprintf(stdout, "%s %s%c", sign, it->string, 0);
750 struct strbuf onebuf = STRBUF_INIT;
752 one = quote_path(it->string, -1, &onebuf, s->prefix);
753 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
754 printf(" %s\n", one);
755 strbuf_release(&onebuf);
759 void wt_shortstatus_print(struct wt_status *s, int null_termination)
762 for (i = 0; i < s->change.nr; i++) {
763 struct wt_status_change_data *d;
764 struct string_list_item *it;
766 it = &(s->change.items[i]);
769 wt_shortstatus_unmerged(null_termination, it, s);
771 wt_shortstatus_status(null_termination, it, s);
773 for (i = 0; i < s->untracked.nr; i++) {
774 struct string_list_item *it;
776 it = &(s->untracked.items[i]);
777 wt_shortstatus_other(null_termination, it, s, "??");
779 for (i = 0; i < s->ignored.nr; i++) {
780 struct string_list_item *it;
782 it = &(s->ignored.items[i]);
783 wt_shortstatus_other(null_termination, it, s, "!!");
787 void wt_porcelain_print(struct wt_status *s, int null_termination)
790 s->relative_paths = 0;
792 wt_shortstatus_print(s, null_termination);