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_untracked_header(struct wt_status *s)
102 const char *c = color(WT_STATUS_HEADER, s);
103 color_fprintf_ln(s->fp, c, "# Untracked files:");
104 if (!advice_status_hints)
106 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
107 color_fprintf_ln(s->fp, c, "#");
110 static void wt_status_print_trailer(struct wt_status *s)
112 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
115 #define quote_path quote_path_relative
117 static void wt_status_print_unmerged_data(struct wt_status *s,
118 struct string_list_item *it)
120 const char *c = color(WT_STATUS_UNMERGED, s);
121 struct wt_status_change_data *d = it->util;
122 struct strbuf onebuf = STRBUF_INIT;
123 const char *one, *how = "bug";
125 one = quote_path(it->string, -1, &onebuf, s->prefix);
126 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
127 switch (d->stagemask) {
128 case 1: how = "both deleted:"; break;
129 case 2: how = "added by us:"; break;
130 case 3: how = "deleted by them:"; break;
131 case 4: how = "added by them:"; break;
132 case 5: how = "deleted by us:"; break;
133 case 6: how = "both added:"; break;
134 case 7: how = "both modified:"; break;
136 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
137 strbuf_release(&onebuf);
140 static void wt_status_print_change_data(struct wt_status *s,
142 struct string_list_item *it)
144 struct wt_status_change_data *d = it->util;
145 const char *c = color(change_type, s);
149 const char *one, *two;
150 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
151 struct strbuf extra = STRBUF_INIT;
153 one_name = two_name = it->string;
154 switch (change_type) {
155 case WT_STATUS_UPDATED:
156 status = d->index_status;
158 one_name = d->head_path;
160 case WT_STATUS_CHANGED:
161 if (d->new_submodule_commits || d->dirty_submodule) {
162 strbuf_addstr(&extra, " (");
163 if (d->new_submodule_commits)
164 strbuf_addf(&extra, "new commits, ");
165 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
166 strbuf_addf(&extra, "modified content, ");
167 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
168 strbuf_addf(&extra, "untracked content, ");
169 strbuf_setlen(&extra, extra.len - 2);
170 strbuf_addch(&extra, ')');
172 status = d->worktree_status;
176 one = quote_path(one_name, -1, &onebuf, s->prefix);
177 two = quote_path(two_name, -1, &twobuf, s->prefix);
179 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
181 case DIFF_STATUS_ADDED:
182 color_fprintf(s->fp, c, "new file: %s", one);
184 case DIFF_STATUS_COPIED:
185 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
187 case DIFF_STATUS_DELETED:
188 color_fprintf(s->fp, c, "deleted: %s", one);
190 case DIFF_STATUS_MODIFIED:
191 color_fprintf(s->fp, c, "modified: %s", one);
193 case DIFF_STATUS_RENAMED:
194 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
196 case DIFF_STATUS_TYPE_CHANGED:
197 color_fprintf(s->fp, c, "typechange: %s", one);
199 case DIFF_STATUS_UNKNOWN:
200 color_fprintf(s->fp, c, "unknown: %s", one);
202 case DIFF_STATUS_UNMERGED:
203 color_fprintf(s->fp, c, "unmerged: %s", one);
206 die("bug: unhandled diff status %c", status);
209 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
210 strbuf_release(&extra);
212 fprintf(s->fp, "\n");
213 strbuf_release(&onebuf);
214 strbuf_release(&twobuf);
217 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
218 struct diff_options *options,
221 struct wt_status *s = data;
226 s->workdir_dirty = 1;
227 for (i = 0; i < q->nr; i++) {
228 struct diff_filepair *p;
229 struct string_list_item *it;
230 struct wt_status_change_data *d;
233 it = string_list_insert(p->one->path, &s->change);
236 d = xcalloc(1, sizeof(*d));
239 if (!d->worktree_status)
240 d->worktree_status = p->status;
241 d->dirty_submodule = p->two->dirty_submodule;
242 if (S_ISGITLINK(p->two->mode))
243 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
247 static int unmerged_mask(const char *path)
250 struct cache_entry *ce;
252 pos = cache_name_pos(path, strlen(path));
258 while (pos < active_nr) {
259 ce = active_cache[pos++];
260 if (strcmp(ce->name, path) || !ce_stage(ce))
262 mask |= (1 << (ce_stage(ce) - 1));
267 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
268 struct diff_options *options,
271 struct wt_status *s = data;
274 for (i = 0; i < q->nr; i++) {
275 struct diff_filepair *p;
276 struct string_list_item *it;
277 struct wt_status_change_data *d;
280 it = string_list_insert(p->two->path, &s->change);
283 d = xcalloc(1, sizeof(*d));
286 if (!d->index_status)
287 d->index_status = p->status;
289 case DIFF_STATUS_COPIED:
290 case DIFF_STATUS_RENAMED:
291 d->head_path = xstrdup(p->one->path);
293 case DIFF_STATUS_UNMERGED:
294 d->stagemask = unmerged_mask(p->two->path);
300 static void wt_status_collect_changes_worktree(struct wt_status *s)
304 init_revisions(&rev, NULL);
305 setup_revisions(0, NULL, &rev, NULL);
306 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
307 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
308 if (!s->show_untracked_files)
309 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
310 rev.diffopt.format_callback = wt_status_collect_changed_cb;
311 rev.diffopt.format_callback_data = s;
312 rev.prune_data = s->pathspec;
313 run_diff_files(&rev, 0);
316 static void wt_status_collect_changes_index(struct wt_status *s)
319 struct setup_revision_opt opt;
321 init_revisions(&rev, NULL);
322 memset(&opt, 0, sizeof(opt));
323 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
324 setup_revisions(0, NULL, &rev, &opt);
326 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
327 rev.diffopt.format_callback = wt_status_collect_updated_cb;
328 rev.diffopt.format_callback_data = s;
329 rev.diffopt.detect_rename = 1;
330 rev.diffopt.rename_limit = 200;
331 rev.diffopt.break_opt = 0;
332 rev.prune_data = s->pathspec;
333 run_diff_index(&rev, 1);
336 static void wt_status_collect_changes_initial(struct wt_status *s)
340 for (i = 0; i < active_nr; i++) {
341 struct string_list_item *it;
342 struct wt_status_change_data *d;
343 struct cache_entry *ce = active_cache[i];
345 if (!ce_path_match(ce, s->pathspec))
347 it = string_list_insert(ce->name, &s->change);
350 d = xcalloc(1, sizeof(*d));
354 d->index_status = DIFF_STATUS_UNMERGED;
355 d->stagemask |= (1 << (ce_stage(ce) - 1));
358 d->index_status = DIFF_STATUS_ADDED;
362 static void wt_status_collect_untracked(struct wt_status *s)
365 struct dir_struct dir;
367 if (!s->show_untracked_files)
369 memset(&dir, 0, sizeof(dir));
370 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
372 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
373 setup_standard_excludes(&dir);
375 fill_directory(&dir, s->pathspec);
376 for (i = 0; i < dir.nr; i++) {
377 struct dir_entry *ent = dir.entries[i];
378 if (!cache_name_is_other(ent->name, ent->len))
380 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
382 string_list_insert(ent->name, &s->untracked);
386 if (s->show_ignored_files) {
388 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
389 fill_directory(&dir, s->pathspec);
390 for (i = 0; i < dir.nr; i++) {
391 struct dir_entry *ent = dir.entries[i];
392 if (!cache_name_is_other(ent->name, ent->len))
394 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
396 string_list_insert(ent->name, &s->ignored);
404 void wt_status_collect(struct wt_status *s)
406 wt_status_collect_changes_worktree(s);
409 wt_status_collect_changes_initial(s);
411 wt_status_collect_changes_index(s);
412 wt_status_collect_untracked(s);
415 static void wt_status_print_unmerged(struct wt_status *s)
417 int shown_header = 0;
420 for (i = 0; i < s->change.nr; i++) {
421 struct wt_status_change_data *d;
422 struct string_list_item *it;
423 it = &(s->change.items[i]);
428 wt_status_print_unmerged_header(s);
431 wt_status_print_unmerged_data(s, it);
434 wt_status_print_trailer(s);
438 static void wt_status_print_updated(struct wt_status *s)
440 int shown_header = 0;
443 for (i = 0; i < s->change.nr; i++) {
444 struct wt_status_change_data *d;
445 struct string_list_item *it;
446 it = &(s->change.items[i]);
448 if (!d->index_status ||
449 d->index_status == DIFF_STATUS_UNMERGED)
452 wt_status_print_cached_header(s);
456 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
459 wt_status_print_trailer(s);
465 * 1 : some change but no delete
467 static int wt_status_check_worktree_changes(struct wt_status *s,
468 int *dirty_submodules)
473 *dirty_submodules = 0;
475 for (i = 0; i < s->change.nr; i++) {
476 struct wt_status_change_data *d;
477 d = s->change.items[i].util;
478 if (!d->worktree_status ||
479 d->worktree_status == DIFF_STATUS_UNMERGED)
483 if (d->dirty_submodule)
484 *dirty_submodules = 1;
485 if (d->worktree_status == DIFF_STATUS_DELETED)
491 static void wt_status_print_changed(struct wt_status *s)
493 int i, dirty_submodules;
494 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
496 if (!worktree_changes)
499 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
501 for (i = 0; i < s->change.nr; i++) {
502 struct wt_status_change_data *d;
503 struct string_list_item *it;
504 it = &(s->change.items[i]);
506 if (!d->worktree_status ||
507 d->worktree_status == DIFF_STATUS_UNMERGED)
509 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
511 wt_status_print_trailer(s);
514 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
516 struct child_process sm_summary;
517 char summary_limit[64];
518 char index[PATH_MAX];
519 const char *env[] = { index, NULL };
520 const char *argv[] = {
523 uncommitted ? "--files" : "--cached",
527 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
531 sprintf(summary_limit, "%d", s->submodule_summary);
532 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
534 memset(&sm_summary, 0, sizeof(sm_summary));
535 sm_summary.argv = argv;
536 sm_summary.env = env;
537 sm_summary.git_cmd = 1;
538 sm_summary.no_stdin = 1;
540 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
541 run_command(&sm_summary);
544 static void wt_status_print_untracked(struct wt_status *s)
547 struct strbuf buf = STRBUF_INIT;
549 if (!s->untracked.nr)
552 wt_status_print_untracked_header(s);
553 for (i = 0; i < s->untracked.nr; i++) {
554 struct string_list_item *it;
555 it = &(s->untracked.items[i]);
556 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
557 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
558 quote_path(it->string, strlen(it->string),
561 strbuf_release(&buf);
564 static void wt_status_print_verbose(struct wt_status *s)
567 struct setup_revision_opt opt;
569 init_revisions(&rev, NULL);
570 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
572 memset(&opt, 0, sizeof(opt));
573 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
574 setup_revisions(0, NULL, &rev, &opt);
576 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
577 rev.diffopt.detect_rename = 1;
578 rev.diffopt.file = s->fp;
579 rev.diffopt.close_file = 0;
581 * If we're not going to stdout, then we definitely don't
582 * want color, since we are going to the commit message
583 * file (and even the "auto" setting won't work, since it
584 * will have checked isatty on stdout).
587 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
588 run_diff_index(&rev, 1);
591 static void wt_status_print_tracking(struct wt_status *s)
593 struct strbuf sb = STRBUF_INIT;
595 struct branch *branch;
597 assert(s->branch && !s->is_initial);
598 if (prefixcmp(s->branch, "refs/heads/"))
600 branch = branch_get(s->branch + 11);
601 if (!format_tracking_info(branch, &sb))
604 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
605 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
606 "# %.*s", (int)(ep - cp), cp);
607 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
610 void wt_status_print(struct wt_status *s)
612 const char *branch_color = color(WT_STATUS_HEADER, s);
615 const char *on_what = "On branch ";
616 const char *branch_name = s->branch;
617 if (!prefixcmp(branch_name, "refs/heads/"))
619 else if (!strcmp(branch_name, "HEAD")) {
621 branch_color = color(WT_STATUS_NOBRANCH, s);
622 on_what = "Not currently on any branch.";
624 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
625 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
627 wt_status_print_tracking(s);
631 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
632 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
633 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
636 wt_status_print_updated(s);
637 wt_status_print_unmerged(s);
638 wt_status_print_changed(s);
639 if (s->submodule_summary) {
640 wt_status_print_submodule_summary(s, 0); /* staged */
641 wt_status_print_submodule_summary(s, 1); /* unstaged */
643 if (s->show_untracked_files)
644 wt_status_print_untracked(s);
645 else if (s->commitable)
646 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
649 wt_status_print_verbose(s);
650 if (!s->commitable) {
652 fprintf(s->fp, "# No changes\n");
655 else if (s->workdir_dirty)
656 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
657 else if (s->untracked.nr)
658 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
659 else if (s->is_initial)
660 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
661 else if (!s->show_untracked_files)
662 printf("nothing to commit (use -u to show untracked files)\n");
664 printf("nothing to commit (working directory clean)\n");
668 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
671 struct wt_status_change_data *d = it->util;
672 const char *how = "??";
674 switch (d->stagemask) {
675 case 1: how = "DD"; break; /* both deleted */
676 case 2: how = "AU"; break; /* added by us */
677 case 3: how = "UD"; break; /* deleted by them */
678 case 4: how = "UA"; break; /* added by them */
679 case 5: how = "DU"; break; /* deleted by us */
680 case 6: how = "AA"; break; /* both added */
681 case 7: how = "UU"; break; /* both modified */
683 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
684 if (null_termination) {
685 fprintf(stdout, " %s%c", it->string, 0);
687 struct strbuf onebuf = STRBUF_INIT;
689 one = quote_path(it->string, -1, &onebuf, s->prefix);
690 printf(" %s\n", one);
691 strbuf_release(&onebuf);
695 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
698 struct wt_status_change_data *d = it->util;
701 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
704 if (d->worktree_status)
705 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
709 if (null_termination) {
710 fprintf(stdout, "%s%c", it->string, 0);
712 fprintf(stdout, "%s%c", d->head_path, 0);
714 struct strbuf onebuf = STRBUF_INIT;
717 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
718 printf("%s -> ", one);
719 strbuf_release(&onebuf);
721 one = quote_path(it->string, -1, &onebuf, s->prefix);
723 strbuf_release(&onebuf);
727 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
730 if (null_termination) {
731 fprintf(stdout, "?? %s%c", it->string, 0);
733 struct strbuf onebuf = STRBUF_INIT;
735 one = quote_path(it->string, -1, &onebuf, s->prefix);
736 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
737 printf(" %s\n", one);
738 strbuf_release(&onebuf);
742 void wt_shortstatus_print(struct wt_status *s, int null_termination)
745 for (i = 0; i < s->change.nr; i++) {
746 struct wt_status_change_data *d;
747 struct string_list_item *it;
749 it = &(s->change.items[i]);
752 wt_shortstatus_unmerged(null_termination, it, s);
754 wt_shortstatus_status(null_termination, it, s);
756 for (i = 0; i < s->untracked.nr; i++) {
757 struct string_list_item *it;
759 it = &(s->untracked.items[i]);
760 wt_shortstatus_untracked(null_termination, it, s);
764 void wt_porcelain_print(struct wt_status *s, int null_termination)
767 s->relative_paths = 0;
769 wt_shortstatus_print(s, null_termination);