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;
47 static void wt_status_print_unmerged_header(struct wt_status *s)
49 const char *c = color(WT_STATUS_HEADER, s);
50 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
51 if (!advice_status_hints)
54 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
56 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
57 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to mark resolution)");
58 color_fprintf_ln(s->fp, c, "#");
61 static void wt_status_print_cached_header(struct wt_status *s)
63 const char *c = color(WT_STATUS_HEADER, s);
64 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
65 if (!advice_status_hints)
68 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
70 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
72 color_fprintf_ln(s->fp, c, "#");
75 static void wt_status_print_dirty_header(struct wt_status *s,
78 const char *c = color(WT_STATUS_HEADER, s);
79 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
80 if (!advice_status_hints)
83 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
85 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
86 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
87 color_fprintf_ln(s->fp, c, "#");
90 static void wt_status_print_untracked_header(struct wt_status *s)
92 const char *c = color(WT_STATUS_HEADER, s);
93 color_fprintf_ln(s->fp, c, "# Untracked files:");
94 if (!advice_status_hints)
96 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
97 color_fprintf_ln(s->fp, c, "#");
100 static void wt_status_print_trailer(struct wt_status *s)
102 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
105 #define quote_path quote_path_relative
107 static void wt_status_print_unmerged_data(struct wt_status *s,
108 struct string_list_item *it)
110 const char *c = color(WT_STATUS_UNMERGED, s);
111 struct wt_status_change_data *d = it->util;
112 struct strbuf onebuf = STRBUF_INIT;
113 const char *one, *how = "bug";
115 one = quote_path(it->string, -1, &onebuf, s->prefix);
116 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
117 switch (d->stagemask) {
118 case 1: how = "both deleted:"; break;
119 case 2: how = "added by us:"; break;
120 case 3: how = "deleted by them:"; break;
121 case 4: how = "added by them:"; break;
122 case 5: how = "deleted by us:"; break;
123 case 6: how = "both added:"; break;
124 case 7: how = "both modified:"; break;
126 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
127 strbuf_release(&onebuf);
130 static void wt_status_print_change_data(struct wt_status *s,
132 struct string_list_item *it)
134 struct wt_status_change_data *d = it->util;
135 const char *c = color(change_type, s);
139 const char *one, *two;
140 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
142 one_name = two_name = it->string;
143 switch (change_type) {
144 case WT_STATUS_UPDATED:
145 status = d->index_status;
147 one_name = d->head_path;
149 case WT_STATUS_CHANGED:
150 status = d->worktree_status;
154 one = quote_path(one_name, -1, &onebuf, s->prefix);
155 two = quote_path(two_name, -1, &twobuf, s->prefix);
157 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
159 case DIFF_STATUS_ADDED:
160 color_fprintf(s->fp, c, "new file: %s", one);
162 case DIFF_STATUS_COPIED:
163 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
165 case DIFF_STATUS_DELETED:
166 color_fprintf(s->fp, c, "deleted: %s", one);
168 case DIFF_STATUS_MODIFIED:
169 color_fprintf(s->fp, c, "modified: %s", one);
171 case DIFF_STATUS_RENAMED:
172 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
174 case DIFF_STATUS_TYPE_CHANGED:
175 color_fprintf(s->fp, c, "typechange: %s", one);
177 case DIFF_STATUS_UNKNOWN:
178 color_fprintf(s->fp, c, "unknown: %s", one);
180 case DIFF_STATUS_UNMERGED:
181 color_fprintf(s->fp, c, "unmerged: %s", one);
184 die("bug: unhandled diff status %c", status);
186 fprintf(s->fp, "\n");
187 strbuf_release(&onebuf);
188 strbuf_release(&twobuf);
191 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
192 struct diff_options *options,
195 struct wt_status *s = data;
200 s->workdir_dirty = 1;
201 for (i = 0; i < q->nr; i++) {
202 struct diff_filepair *p;
203 struct string_list_item *it;
204 struct wt_status_change_data *d;
207 it = string_list_insert(p->one->path, &s->change);
210 d = xcalloc(1, sizeof(*d));
213 if (!d->worktree_status)
214 d->worktree_status = p->status;
218 static int unmerged_mask(const char *path)
221 struct cache_entry *ce;
223 pos = cache_name_pos(path, strlen(path));
229 while (pos < active_nr) {
230 ce = active_cache[pos++];
231 if (strcmp(ce->name, path) || !ce_stage(ce))
233 mask |= (1 << (ce_stage(ce) - 1));
238 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
239 struct diff_options *options,
242 struct wt_status *s = data;
245 for (i = 0; i < q->nr; i++) {
246 struct diff_filepair *p;
247 struct string_list_item *it;
248 struct wt_status_change_data *d;
251 it = string_list_insert(p->two->path, &s->change);
254 d = xcalloc(1, sizeof(*d));
257 if (!d->index_status)
258 d->index_status = p->status;
260 case DIFF_STATUS_COPIED:
261 case DIFF_STATUS_RENAMED:
262 d->head_path = xstrdup(p->one->path);
264 case DIFF_STATUS_UNMERGED:
265 d->stagemask = unmerged_mask(p->two->path);
271 static void wt_status_collect_changes_worktree(struct wt_status *s)
275 init_revisions(&rev, NULL);
276 setup_revisions(0, NULL, &rev, NULL);
277 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
278 rev.diffopt.format_callback = wt_status_collect_changed_cb;
279 rev.diffopt.format_callback_data = s;
280 rev.prune_data = s->pathspec;
281 run_diff_files(&rev, 0);
284 static void wt_status_collect_changes_index(struct wt_status *s)
288 init_revisions(&rev, NULL);
289 setup_revisions(0, NULL, &rev,
290 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
291 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
292 rev.diffopt.format_callback = wt_status_collect_updated_cb;
293 rev.diffopt.format_callback_data = s;
294 rev.diffopt.detect_rename = 1;
295 rev.diffopt.rename_limit = 200;
296 rev.diffopt.break_opt = 0;
297 rev.prune_data = s->pathspec;
298 run_diff_index(&rev, 1);
301 static void wt_status_collect_changes_initial(struct wt_status *s)
305 for (i = 0; i < active_nr; i++) {
306 struct string_list_item *it;
307 struct wt_status_change_data *d;
308 struct cache_entry *ce = active_cache[i];
310 if (!ce_path_match(ce, s->pathspec))
312 it = string_list_insert(ce->name, &s->change);
315 d = xcalloc(1, sizeof(*d));
319 d->index_status = DIFF_STATUS_UNMERGED;
320 d->stagemask |= (1 << (ce_stage(ce) - 1));
323 d->index_status = DIFF_STATUS_ADDED;
327 static void wt_status_collect_untracked(struct wt_status *s)
330 struct dir_struct dir;
332 if (!s->show_untracked_files)
334 memset(&dir, 0, sizeof(dir));
335 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
337 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
338 setup_standard_excludes(&dir);
340 fill_directory(&dir, NULL);
341 for (i = 0; i < dir.nr; i++) {
342 struct dir_entry *ent = dir.entries[i];
343 if (!cache_name_is_other(ent->name, ent->len))
345 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
347 s->workdir_untracked = 1;
348 string_list_insert(ent->name, &s->untracked);
352 void wt_status_collect(struct wt_status *s)
354 wt_status_collect_changes_worktree(s);
357 wt_status_collect_changes_initial(s);
359 wt_status_collect_changes_index(s);
360 wt_status_collect_untracked(s);
363 static void wt_status_print_unmerged(struct wt_status *s)
365 int shown_header = 0;
368 for (i = 0; i < s->change.nr; i++) {
369 struct wt_status_change_data *d;
370 struct string_list_item *it;
371 it = &(s->change.items[i]);
376 wt_status_print_unmerged_header(s);
379 wt_status_print_unmerged_data(s, it);
382 wt_status_print_trailer(s);
386 static void wt_status_print_updated(struct wt_status *s)
388 int shown_header = 0;
391 for (i = 0; i < s->change.nr; i++) {
392 struct wt_status_change_data *d;
393 struct string_list_item *it;
394 it = &(s->change.items[i]);
396 if (!d->index_status ||
397 d->index_status == DIFF_STATUS_UNMERGED)
400 wt_status_print_cached_header(s);
404 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
407 wt_status_print_trailer(s);
413 * 1 : some change but no delete
415 static int wt_status_check_worktree_changes(struct wt_status *s)
420 for (i = 0; i < s->change.nr; i++) {
421 struct wt_status_change_data *d;
422 d = s->change.items[i].util;
423 if (!d->worktree_status ||
424 d->worktree_status == DIFF_STATUS_UNMERGED)
427 if (d->worktree_status == DIFF_STATUS_DELETED)
433 static void wt_status_print_changed(struct wt_status *s)
436 int worktree_changes = wt_status_check_worktree_changes(s);
438 if (!worktree_changes)
441 wt_status_print_dirty_header(s, worktree_changes < 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->worktree_status ||
449 d->worktree_status == DIFF_STATUS_UNMERGED)
451 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
453 wt_status_print_trailer(s);
456 static void wt_status_print_submodule_summary(struct wt_status *s)
458 struct child_process sm_summary;
459 char summary_limit[64];
460 char index[PATH_MAX];
461 const char *env[] = { index, NULL };
462 const char *argv[] = {
469 s->amend ? "HEAD^" : "HEAD",
473 sprintf(summary_limit, "%d", s->submodule_summary);
474 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
476 memset(&sm_summary, 0, sizeof(sm_summary));
477 sm_summary.argv = argv;
478 sm_summary.env = env;
479 sm_summary.git_cmd = 1;
480 sm_summary.no_stdin = 1;
482 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
483 run_command(&sm_summary);
486 static void wt_status_print_untracked(struct wt_status *s)
489 struct strbuf buf = STRBUF_INIT;
491 if (!s->untracked.nr)
494 wt_status_print_untracked_header(s);
495 for (i = 0; i < s->untracked.nr; i++) {
496 struct string_list_item *it;
497 it = &(s->untracked.items[i]);
498 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
499 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
500 quote_path(it->string, strlen(it->string),
503 strbuf_release(&buf);
506 static void wt_status_print_verbose(struct wt_status *s)
510 init_revisions(&rev, NULL);
511 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
512 setup_revisions(0, NULL, &rev,
513 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
514 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
515 rev.diffopt.detect_rename = 1;
516 rev.diffopt.file = s->fp;
517 rev.diffopt.close_file = 0;
519 * If we're not going to stdout, then we definitely don't
520 * want color, since we are going to the commit message
521 * file (and even the "auto" setting won't work, since it
522 * will have checked isatty on stdout).
525 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
526 run_diff_index(&rev, 1);
529 static void wt_status_print_tracking(struct wt_status *s)
531 struct strbuf sb = STRBUF_INIT;
533 struct branch *branch;
535 assert(s->branch && !s->is_initial);
536 if (prefixcmp(s->branch, "refs/heads/"))
538 branch = branch_get(s->branch + 11);
539 if (!format_tracking_info(branch, &sb))
542 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
543 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
544 "# %.*s", (int)(ep - cp), cp);
545 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
548 void wt_status_print(struct wt_status *s)
550 const char *branch_color = color(WT_STATUS_HEADER, s);
553 const char *on_what = "On branch ";
554 const char *branch_name = s->branch;
555 if (!prefixcmp(branch_name, "refs/heads/"))
557 else if (!strcmp(branch_name, "HEAD")) {
559 branch_color = color(WT_STATUS_NOBRANCH, s);
560 on_what = "Not currently on any branch.";
562 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
563 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
565 wt_status_print_tracking(s);
569 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
570 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
571 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
574 wt_status_print_updated(s);
575 wt_status_print_unmerged(s);
576 wt_status_print_changed(s);
577 if (s->submodule_summary)
578 wt_status_print_submodule_summary(s);
579 if (s->show_untracked_files)
580 wt_status_print_untracked(s);
581 else if (s->commitable)
582 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
585 wt_status_print_verbose(s);
586 if (!s->commitable) {
588 fprintf(s->fp, "# No changes\n");
591 else if (s->workdir_dirty)
592 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
593 else if (s->untracked.nr)
594 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
595 else if (s->is_initial)
596 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
597 else if (!s->show_untracked_files)
598 printf("nothing to commit (use -u to show untracked files)\n");
600 printf("nothing to commit (working directory clean)\n");