10 #include "run-command.h"
12 #include "submodule.h"
14 static char default_wt_status_colors[][COLOR_MAXLEN] = {
15 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
16 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
17 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
18 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
19 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
20 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
23 static const char *color(int slot, struct wt_status *s)
25 return s->use_color > 0 ? s->color_palette[slot] : "";
28 void wt_status_prepare(struct wt_status *s)
30 unsigned char sha1[20];
33 memset(s, 0, sizeof(*s));
34 memcpy(s->color_palette, default_wt_status_colors,
35 sizeof(default_wt_status_colors));
36 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
38 s->relative_paths = 1;
39 head = resolve_ref("HEAD", sha1, 0, NULL);
40 s->branch = head ? xstrdup(head) : NULL;
41 s->reference = "HEAD";
43 s->index_file = get_index_file();
44 s->change.strdup_strings = 1;
45 s->untracked.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 if (s->ignore_submodule_arg)
311 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
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 if (s->ignore_submodule_arg)
329 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
331 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
332 rev.diffopt.format_callback = wt_status_collect_updated_cb;
333 rev.diffopt.format_callback_data = s;
334 rev.diffopt.detect_rename = 1;
335 rev.diffopt.rename_limit = 200;
336 rev.diffopt.break_opt = 0;
337 rev.prune_data = s->pathspec;
338 run_diff_index(&rev, 1);
341 static void wt_status_collect_changes_initial(struct wt_status *s)
345 for (i = 0; i < active_nr; i++) {
346 struct string_list_item *it;
347 struct wt_status_change_data *d;
348 struct cache_entry *ce = active_cache[i];
350 if (!ce_path_match(ce, s->pathspec))
352 it = string_list_insert(ce->name, &s->change);
355 d = xcalloc(1, sizeof(*d));
359 d->index_status = DIFF_STATUS_UNMERGED;
360 d->stagemask |= (1 << (ce_stage(ce) - 1));
363 d->index_status = DIFF_STATUS_ADDED;
367 static void wt_status_collect_untracked(struct wt_status *s)
370 struct dir_struct dir;
372 if (!s->show_untracked_files)
374 memset(&dir, 0, sizeof(dir));
375 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
377 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
378 setup_standard_excludes(&dir);
380 fill_directory(&dir, s->pathspec);
381 for (i = 0; i < dir.nr; i++) {
382 struct dir_entry *ent = dir.entries[i];
383 if (!cache_name_is_other(ent->name, ent->len))
385 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
387 s->workdir_untracked = 1;
388 string_list_insert(ent->name, &s->untracked);
392 void wt_status_collect(struct wt_status *s)
394 wt_status_collect_changes_worktree(s);
397 wt_status_collect_changes_initial(s);
399 wt_status_collect_changes_index(s);
400 wt_status_collect_untracked(s);
403 static void wt_status_print_unmerged(struct wt_status *s)
405 int shown_header = 0;
408 for (i = 0; i < s->change.nr; i++) {
409 struct wt_status_change_data *d;
410 struct string_list_item *it;
411 it = &(s->change.items[i]);
416 wt_status_print_unmerged_header(s);
419 wt_status_print_unmerged_data(s, it);
422 wt_status_print_trailer(s);
426 static void wt_status_print_updated(struct wt_status *s)
428 int shown_header = 0;
431 for (i = 0; i < s->change.nr; i++) {
432 struct wt_status_change_data *d;
433 struct string_list_item *it;
434 it = &(s->change.items[i]);
436 if (!d->index_status ||
437 d->index_status == DIFF_STATUS_UNMERGED)
440 wt_status_print_cached_header(s);
444 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
447 wt_status_print_trailer(s);
453 * 1 : some change but no delete
455 static int wt_status_check_worktree_changes(struct wt_status *s,
456 int *dirty_submodules)
461 *dirty_submodules = 0;
463 for (i = 0; i < s->change.nr; i++) {
464 struct wt_status_change_data *d;
465 d = s->change.items[i].util;
466 if (!d->worktree_status ||
467 d->worktree_status == DIFF_STATUS_UNMERGED)
471 if (d->dirty_submodule)
472 *dirty_submodules = 1;
473 if (d->worktree_status == DIFF_STATUS_DELETED)
479 static void wt_status_print_changed(struct wt_status *s)
481 int i, dirty_submodules;
482 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
484 if (!worktree_changes)
487 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
489 for (i = 0; i < s->change.nr; i++) {
490 struct wt_status_change_data *d;
491 struct string_list_item *it;
492 it = &(s->change.items[i]);
494 if (!d->worktree_status ||
495 d->worktree_status == DIFF_STATUS_UNMERGED)
497 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
499 wt_status_print_trailer(s);
502 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
504 struct child_process sm_summary;
505 char summary_limit[64];
506 char index[PATH_MAX];
507 const char *env[] = { index, NULL };
508 const char *argv[] = {
511 uncommitted ? "--files" : "--cached",
515 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
519 sprintf(summary_limit, "%d", s->submodule_summary);
520 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
522 memset(&sm_summary, 0, sizeof(sm_summary));
523 sm_summary.argv = argv;
524 sm_summary.env = env;
525 sm_summary.git_cmd = 1;
526 sm_summary.no_stdin = 1;
528 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
529 run_command(&sm_summary);
532 static void wt_status_print_untracked(struct wt_status *s)
535 struct strbuf buf = STRBUF_INIT;
537 if (!s->untracked.nr)
540 wt_status_print_untracked_header(s);
541 for (i = 0; i < s->untracked.nr; i++) {
542 struct string_list_item *it;
543 it = &(s->untracked.items[i]);
544 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
545 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
546 quote_path(it->string, strlen(it->string),
549 strbuf_release(&buf);
552 static void wt_status_print_verbose(struct wt_status *s)
555 struct setup_revision_opt opt;
557 init_revisions(&rev, NULL);
558 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
560 memset(&opt, 0, sizeof(opt));
561 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
562 setup_revisions(0, NULL, &rev, &opt);
564 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
565 rev.diffopt.detect_rename = 1;
566 rev.diffopt.file = s->fp;
567 rev.diffopt.close_file = 0;
569 * If we're not going to stdout, then we definitely don't
570 * want color, since we are going to the commit message
571 * file (and even the "auto" setting won't work, since it
572 * will have checked isatty on stdout).
575 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
576 run_diff_index(&rev, 1);
579 static void wt_status_print_tracking(struct wt_status *s)
581 struct strbuf sb = STRBUF_INIT;
583 struct branch *branch;
585 assert(s->branch && !s->is_initial);
586 if (prefixcmp(s->branch, "refs/heads/"))
588 branch = branch_get(s->branch + 11);
589 if (!format_tracking_info(branch, &sb))
592 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
593 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
594 "# %.*s", (int)(ep - cp), cp);
595 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
598 void wt_status_print(struct wt_status *s)
600 const char *branch_color = color(WT_STATUS_HEADER, s);
603 const char *on_what = "On branch ";
604 const char *branch_name = s->branch;
605 if (!prefixcmp(branch_name, "refs/heads/"))
607 else if (!strcmp(branch_name, "HEAD")) {
609 branch_color = color(WT_STATUS_NOBRANCH, s);
610 on_what = "Not currently on any branch.";
612 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
613 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
615 wt_status_print_tracking(s);
619 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
620 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
621 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
624 wt_status_print_updated(s);
625 wt_status_print_unmerged(s);
626 wt_status_print_changed(s);
627 if (s->submodule_summary &&
628 (!s->ignore_submodule_arg ||
629 strcmp(s->ignore_submodule_arg, "all"))) {
630 wt_status_print_submodule_summary(s, 0); /* staged */
631 wt_status_print_submodule_summary(s, 1); /* unstaged */
633 if (s->show_untracked_files)
634 wt_status_print_untracked(s);
635 else if (s->commitable)
636 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
639 wt_status_print_verbose(s);
640 if (!s->commitable) {
642 fprintf(s->fp, "# No changes\n");
645 else if (s->workdir_dirty)
646 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
647 else if (s->untracked.nr)
648 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
649 else if (s->is_initial)
650 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
651 else if (!s->show_untracked_files)
652 printf("nothing to commit (use -u to show untracked files)\n");
654 printf("nothing to commit (working directory clean)\n");
658 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
661 struct wt_status_change_data *d = it->util;
662 const char *how = "??";
664 switch (d->stagemask) {
665 case 1: how = "DD"; break; /* both deleted */
666 case 2: how = "AU"; break; /* added by us */
667 case 3: how = "UD"; break; /* deleted by them */
668 case 4: how = "UA"; break; /* added by them */
669 case 5: how = "DU"; break; /* deleted by us */
670 case 6: how = "AA"; break; /* both added */
671 case 7: how = "UU"; break; /* both modified */
673 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
674 if (null_termination) {
675 fprintf(stdout, " %s%c", it->string, 0);
677 struct strbuf onebuf = STRBUF_INIT;
679 one = quote_path(it->string, -1, &onebuf, s->prefix);
680 printf(" %s\n", one);
681 strbuf_release(&onebuf);
685 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
688 struct wt_status_change_data *d = it->util;
691 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
694 if (d->worktree_status)
695 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
699 if (null_termination) {
700 fprintf(stdout, "%s%c", it->string, 0);
702 fprintf(stdout, "%s%c", d->head_path, 0);
704 struct strbuf onebuf = STRBUF_INIT;
707 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
708 printf("%s -> ", one);
709 strbuf_release(&onebuf);
711 one = quote_path(it->string, -1, &onebuf, s->prefix);
713 strbuf_release(&onebuf);
717 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
720 if (null_termination) {
721 fprintf(stdout, "?? %s%c", it->string, 0);
723 struct strbuf onebuf = STRBUF_INIT;
725 one = quote_path(it->string, -1, &onebuf, s->prefix);
726 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
727 printf(" %s\n", one);
728 strbuf_release(&onebuf);
732 void wt_shortstatus_print(struct wt_status *s, int null_termination)
735 for (i = 0; i < s->change.nr; i++) {
736 struct wt_status_change_data *d;
737 struct string_list_item *it;
739 it = &(s->change.items[i]);
742 wt_shortstatus_unmerged(null_termination, it, s);
744 wt_shortstatus_status(null_termination, it, s);
746 for (i = 0; i < s->untracked.nr; i++) {
747 struct string_list_item *it;
749 it = &(s->untracked.items[i]);
750 wt_shortstatus_untracked(null_termination, it, s);
754 void wt_porcelain_print(struct wt_status *s, int null_termination)
757 s->relative_paths = 0;
759 wt_shortstatus_print(s, null_termination);