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 run_diff_files(&rev, 0);
283 static void wt_status_collect_changes_index(struct wt_status *s)
287 init_revisions(&rev, NULL);
288 setup_revisions(0, NULL, &rev,
289 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
290 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
291 rev.diffopt.format_callback = wt_status_collect_updated_cb;
292 rev.diffopt.format_callback_data = s;
293 rev.diffopt.detect_rename = 1;
294 rev.diffopt.rename_limit = 200;
295 rev.diffopt.break_opt = 0;
296 run_diff_index(&rev, 1);
299 static void wt_status_collect_changes_initial(struct wt_status *s)
303 for (i = 0; i < active_nr; i++) {
304 struct string_list_item *it;
305 struct wt_status_change_data *d;
306 struct cache_entry *ce = active_cache[i];
308 it = string_list_insert(ce->name, &s->change);
311 d = xcalloc(1, sizeof(*d));
315 d->index_status = DIFF_STATUS_UNMERGED;
316 d->stagemask |= (1 << (ce_stage(ce) - 1));
319 d->index_status = DIFF_STATUS_ADDED;
323 static void wt_status_collect_untracked(struct wt_status *s)
326 struct dir_struct dir;
328 if (!s->show_untracked_files)
330 memset(&dir, 0, sizeof(dir));
331 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
333 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
334 setup_standard_excludes(&dir);
336 fill_directory(&dir, NULL);
337 for (i = 0; i < dir.nr; i++) {
338 struct dir_entry *ent = dir.entries[i];
339 if (!cache_name_is_other(ent->name, ent->len))
341 s->workdir_untracked = 1;
342 string_list_insert(ent->name, &s->untracked);
346 void wt_status_collect(struct wt_status *s)
348 wt_status_collect_changes_worktree(s);
351 wt_status_collect_changes_initial(s);
353 wt_status_collect_changes_index(s);
354 wt_status_collect_untracked(s);
357 static void wt_status_print_unmerged(struct wt_status *s)
359 int shown_header = 0;
362 for (i = 0; i < s->change.nr; i++) {
363 struct wt_status_change_data *d;
364 struct string_list_item *it;
365 it = &(s->change.items[i]);
370 wt_status_print_unmerged_header(s);
373 wt_status_print_unmerged_data(s, it);
376 wt_status_print_trailer(s);
380 static void wt_status_print_updated(struct wt_status *s)
382 int shown_header = 0;
385 for (i = 0; i < s->change.nr; i++) {
386 struct wt_status_change_data *d;
387 struct string_list_item *it;
388 it = &(s->change.items[i]);
390 if (!d->index_status ||
391 d->index_status == DIFF_STATUS_UNMERGED)
394 wt_status_print_cached_header(s);
398 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
401 wt_status_print_trailer(s);
407 * 1 : some change but no delete
409 static int wt_status_check_worktree_changes(struct wt_status *s)
414 for (i = 0; i < s->change.nr; i++) {
415 struct wt_status_change_data *d;
416 d = s->change.items[i].util;
417 if (!d->worktree_status ||
418 d->worktree_status == DIFF_STATUS_UNMERGED)
421 if (d->worktree_status == DIFF_STATUS_DELETED)
427 static void wt_status_print_changed(struct wt_status *s)
430 int worktree_changes = wt_status_check_worktree_changes(s);
432 if (!worktree_changes)
435 wt_status_print_dirty_header(s, worktree_changes < 0);
437 for (i = 0; i < s->change.nr; i++) {
438 struct wt_status_change_data *d;
439 struct string_list_item *it;
440 it = &(s->change.items[i]);
442 if (!d->worktree_status ||
443 d->worktree_status == DIFF_STATUS_UNMERGED)
445 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
447 wt_status_print_trailer(s);
450 static void wt_status_print_submodule_summary(struct wt_status *s)
452 struct child_process sm_summary;
453 char summary_limit[64];
454 char index[PATH_MAX];
455 const char *env[] = { index, NULL };
456 const char *argv[] = {
463 s->amend ? "HEAD^" : "HEAD",
467 sprintf(summary_limit, "%d", s->submodule_summary);
468 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
470 memset(&sm_summary, 0, sizeof(sm_summary));
471 sm_summary.argv = argv;
472 sm_summary.env = env;
473 sm_summary.git_cmd = 1;
474 sm_summary.no_stdin = 1;
476 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
477 run_command(&sm_summary);
480 static void wt_status_print_untracked(struct wt_status *s)
483 struct strbuf buf = STRBUF_INIT;
485 if (!s->untracked.nr)
488 wt_status_print_untracked_header(s);
489 for (i = 0; i < s->untracked.nr; i++) {
490 struct string_list_item *it;
491 it = &(s->untracked.items[i]);
492 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
493 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
494 quote_path(it->string, strlen(it->string),
497 strbuf_release(&buf);
500 static void wt_status_print_verbose(struct wt_status *s)
504 init_revisions(&rev, NULL);
505 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
506 setup_revisions(0, NULL, &rev,
507 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
508 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
509 rev.diffopt.detect_rename = 1;
510 rev.diffopt.file = s->fp;
511 rev.diffopt.close_file = 0;
513 * If we're not going to stdout, then we definitely don't
514 * want color, since we are going to the commit message
515 * file (and even the "auto" setting won't work, since it
516 * will have checked isatty on stdout).
519 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
520 run_diff_index(&rev, 1);
523 static void wt_status_print_tracking(struct wt_status *s)
525 struct strbuf sb = STRBUF_INIT;
527 struct branch *branch;
529 assert(s->branch && !s->is_initial);
530 if (prefixcmp(s->branch, "refs/heads/"))
532 branch = branch_get(s->branch + 11);
533 if (!format_tracking_info(branch, &sb))
536 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
537 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
538 "# %.*s", (int)(ep - cp), cp);
539 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
542 void wt_status_print(struct wt_status *s)
544 unsigned char sha1[20];
545 const char *branch_color = color(WT_STATUS_HEADER, s);
547 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
549 const char *on_what = "On branch ";
550 const char *branch_name = s->branch;
551 if (!prefixcmp(branch_name, "refs/heads/"))
553 else if (!strcmp(branch_name, "HEAD")) {
555 branch_color = color(WT_STATUS_NOBRANCH, s);
556 on_what = "Not currently on any branch.";
558 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
559 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
561 wt_status_print_tracking(s);
564 wt_status_collect(s);
567 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
568 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
569 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
572 wt_status_print_updated(s);
573 wt_status_print_unmerged(s);
574 wt_status_print_changed(s);
575 if (s->submodule_summary)
576 wt_status_print_submodule_summary(s);
577 if (s->show_untracked_files)
578 wt_status_print_untracked(s);
579 else if (s->commitable)
580 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
583 wt_status_print_verbose(s);
584 if (!s->commitable) {
586 fprintf(s->fp, "# No changes\n");
589 else if (s->workdir_dirty)
590 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
591 else if (s->untracked.nr)
592 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
593 else if (s->is_initial)
594 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
595 else if (!s->show_untracked_files)
596 printf("nothing to commit (use -u to show untracked files)\n");
598 printf("nothing to commit (working directory clean)\n");