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;
46 static void wt_status_print_unmerged_header(struct wt_status *s)
48 const char *c = color(WT_STATUS_HEADER, s);
49 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
51 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
53 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
54 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to mark resolution)");
55 color_fprintf_ln(s->fp, c, "#");
58 static void wt_status_print_cached_header(struct wt_status *s)
60 const char *c = color(WT_STATUS_HEADER, s);
61 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
63 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
65 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
67 color_fprintf_ln(s->fp, c, "#");
70 static void wt_status_print_dirty_header(struct wt_status *s,
73 const char *c = color(WT_STATUS_HEADER, s);
74 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
76 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
78 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
79 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
80 color_fprintf_ln(s->fp, c, "#");
83 static void wt_status_print_untracked_header(struct wt_status *s)
85 const char *c = color(WT_STATUS_HEADER, s);
86 color_fprintf_ln(s->fp, c, "# Untracked files:");
87 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
88 color_fprintf_ln(s->fp, c, "#");
91 static void wt_status_print_trailer(struct wt_status *s)
93 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
96 #define quote_path quote_path_relative
98 static void wt_status_print_unmerged_data(struct wt_status *s,
99 struct string_list_item *it)
101 const char *c = color(WT_STATUS_UNMERGED, s);
102 struct wt_status_change_data *d = it->util;
103 struct strbuf onebuf = STRBUF_INIT;
104 const char *one, *how = "bug";
106 one = quote_path(it->string, -1, &onebuf, s->prefix);
107 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
108 switch (d->stagemask) {
109 case 1: how = "both deleted:"; break;
110 case 2: how = "added by us:"; break;
111 case 3: how = "deleted by them:"; break;
112 case 4: how = "added by them:"; break;
113 case 5: how = "deleted by us:"; break;
114 case 6: how = "both added:"; break;
115 case 7: how = "both modified:"; break;
117 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
118 strbuf_release(&onebuf);
121 static void wt_status_print_change_data(struct wt_status *s,
123 struct string_list_item *it)
125 struct wt_status_change_data *d = it->util;
126 const char *c = color(change_type, s);
130 const char *one, *two;
131 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
133 one_name = two_name = it->string;
134 switch (change_type) {
135 case WT_STATUS_UPDATED:
136 status = d->index_status;
138 one_name = d->head_path;
140 case WT_STATUS_CHANGED:
141 status = d->worktree_status;
145 one = quote_path(one_name, -1, &onebuf, s->prefix);
146 two = quote_path(two_name, -1, &twobuf, s->prefix);
148 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
150 case DIFF_STATUS_ADDED:
151 color_fprintf(s->fp, c, "new file: %s", one);
153 case DIFF_STATUS_COPIED:
154 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
156 case DIFF_STATUS_DELETED:
157 color_fprintf(s->fp, c, "deleted: %s", one);
159 case DIFF_STATUS_MODIFIED:
160 color_fprintf(s->fp, c, "modified: %s", one);
162 case DIFF_STATUS_RENAMED:
163 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
165 case DIFF_STATUS_TYPE_CHANGED:
166 color_fprintf(s->fp, c, "typechange: %s", one);
168 case DIFF_STATUS_UNKNOWN:
169 color_fprintf(s->fp, c, "unknown: %s", one);
171 case DIFF_STATUS_UNMERGED:
172 color_fprintf(s->fp, c, "unmerged: %s", one);
175 die("bug: unhandled diff status %c", status);
177 fprintf(s->fp, "\n");
178 strbuf_release(&onebuf);
179 strbuf_release(&twobuf);
182 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
183 struct diff_options *options,
186 struct wt_status *s = data;
191 s->workdir_dirty = 1;
192 for (i = 0; i < q->nr; i++) {
193 struct diff_filepair *p;
194 struct string_list_item *it;
195 struct wt_status_change_data *d;
198 it = string_list_insert(p->one->path, &s->change);
201 d = xcalloc(1, sizeof(*d));
204 if (!d->worktree_status)
205 d->worktree_status = p->status;
209 static int unmerged_mask(const char *path)
212 struct cache_entry *ce;
214 pos = cache_name_pos(path, strlen(path));
220 while (pos < active_nr) {
221 ce = active_cache[pos++];
222 if (strcmp(ce->name, path) || !ce_stage(ce))
224 mask |= (1 << (ce_stage(ce) - 1));
229 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
230 struct diff_options *options,
233 struct wt_status *s = data;
236 for (i = 0; i < q->nr; i++) {
237 struct diff_filepair *p;
238 struct string_list_item *it;
239 struct wt_status_change_data *d;
242 it = string_list_insert(p->two->path, &s->change);
245 d = xcalloc(1, sizeof(*d));
248 if (!d->index_status)
249 d->index_status = p->status;
251 case DIFF_STATUS_COPIED:
252 case DIFF_STATUS_RENAMED:
253 d->head_path = xstrdup(p->one->path);
255 case DIFF_STATUS_UNMERGED:
256 d->stagemask = unmerged_mask(p->two->path);
262 static void wt_status_collect_changes_worktree(struct wt_status *s)
266 init_revisions(&rev, NULL);
267 setup_revisions(0, NULL, &rev, NULL);
268 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
269 rev.diffopt.format_callback = wt_status_collect_changed_cb;
270 rev.diffopt.format_callback_data = s;
271 run_diff_files(&rev, 0);
274 static void wt_status_collect_changes_index(struct wt_status *s)
278 init_revisions(&rev, NULL);
279 setup_revisions(0, NULL, &rev,
280 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
281 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
282 rev.diffopt.format_callback = wt_status_collect_updated_cb;
283 rev.diffopt.format_callback_data = s;
284 rev.diffopt.detect_rename = 1;
285 rev.diffopt.rename_limit = 200;
286 rev.diffopt.break_opt = 0;
287 run_diff_index(&rev, 1);
290 static void wt_status_collect_changes_initial(struct wt_status *s)
294 for (i = 0; i < active_nr; i++) {
295 struct string_list_item *it;
296 struct wt_status_change_data *d;
297 struct cache_entry *ce = active_cache[i];
299 it = string_list_insert(ce->name, &s->change);
302 d = xcalloc(1, sizeof(*d));
306 d->index_status = DIFF_STATUS_UNMERGED;
307 d->stagemask |= (1 << (ce_stage(ce) - 1));
310 d->index_status = DIFF_STATUS_ADDED;
314 void wt_status_collect_changes(struct wt_status *s)
316 wt_status_collect_changes_worktree(s);
319 wt_status_collect_changes_initial(s);
321 wt_status_collect_changes_index(s);
324 static void wt_status_print_unmerged(struct wt_status *s)
326 int shown_header = 0;
329 for (i = 0; i < s->change.nr; i++) {
330 struct wt_status_change_data *d;
331 struct string_list_item *it;
332 it = &(s->change.items[i]);
337 wt_status_print_unmerged_header(s);
340 wt_status_print_unmerged_data(s, it);
343 wt_status_print_trailer(s);
347 static void wt_status_print_updated(struct wt_status *s)
349 int shown_header = 0;
352 for (i = 0; i < s->change.nr; i++) {
353 struct wt_status_change_data *d;
354 struct string_list_item *it;
355 it = &(s->change.items[i]);
357 if (!d->index_status ||
358 d->index_status == DIFF_STATUS_UNMERGED)
361 wt_status_print_cached_header(s);
365 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
368 wt_status_print_trailer(s);
374 * 1 : some change but no delete
376 static int wt_status_check_worktree_changes(struct wt_status *s)
381 for (i = 0; i < s->change.nr; i++) {
382 struct wt_status_change_data *d;
383 d = s->change.items[i].util;
384 if (!d->worktree_status ||
385 d->worktree_status == DIFF_STATUS_UNMERGED)
388 if (d->worktree_status == DIFF_STATUS_DELETED)
394 static void wt_status_print_changed(struct wt_status *s)
397 int worktree_changes = wt_status_check_worktree_changes(s);
399 if (!worktree_changes)
402 wt_status_print_dirty_header(s, worktree_changes < 0);
404 for (i = 0; i < s->change.nr; i++) {
405 struct wt_status_change_data *d;
406 struct string_list_item *it;
407 it = &(s->change.items[i]);
409 if (!d->worktree_status ||
410 d->worktree_status == DIFF_STATUS_UNMERGED)
412 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
414 wt_status_print_trailer(s);
417 static void wt_status_print_submodule_summary(struct wt_status *s)
419 struct child_process sm_summary;
420 char summary_limit[64];
421 char index[PATH_MAX];
422 const char *env[] = { index, NULL };
423 const char *argv[] = {
430 s->amend ? "HEAD^" : "HEAD",
434 sprintf(summary_limit, "%d", s->submodule_summary);
435 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
437 memset(&sm_summary, 0, sizeof(sm_summary));
438 sm_summary.argv = argv;
439 sm_summary.env = env;
440 sm_summary.git_cmd = 1;
441 sm_summary.no_stdin = 1;
443 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
444 run_command(&sm_summary);
447 static void wt_status_print_untracked(struct wt_status *s)
449 struct dir_struct dir;
451 int shown_header = 0;
452 struct strbuf buf = STRBUF_INIT;
454 memset(&dir, 0, sizeof(dir));
455 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
457 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
458 setup_standard_excludes(&dir);
460 fill_directory(&dir, NULL);
461 for(i = 0; i < dir.nr; i++) {
462 struct dir_entry *ent = dir.entries[i];
463 if (!cache_name_is_other(ent->name, ent->len))
466 s->workdir_untracked = 1;
467 wt_status_print_untracked_header(s);
470 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
471 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
472 quote_path(ent->name, ent->len,
475 strbuf_release(&buf);
478 static void wt_status_print_verbose(struct wt_status *s)
482 init_revisions(&rev, NULL);
483 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
484 setup_revisions(0, NULL, &rev,
485 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
486 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
487 rev.diffopt.detect_rename = 1;
488 rev.diffopt.file = s->fp;
489 rev.diffopt.close_file = 0;
491 * If we're not going to stdout, then we definitely don't
492 * want color, since we are going to the commit message
493 * file (and even the "auto" setting won't work, since it
494 * will have checked isatty on stdout).
497 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
498 run_diff_index(&rev, 1);
501 static void wt_status_print_tracking(struct wt_status *s)
503 struct strbuf sb = STRBUF_INIT;
505 struct branch *branch;
507 assert(s->branch && !s->is_initial);
508 if (prefixcmp(s->branch, "refs/heads/"))
510 branch = branch_get(s->branch + 11);
511 if (!format_tracking_info(branch, &sb))
514 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
515 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
516 "# %.*s", (int)(ep - cp), cp);
517 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
520 void wt_status_print(struct wt_status *s)
522 unsigned char sha1[20];
523 const char *branch_color = color(WT_STATUS_HEADER, s);
525 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
527 const char *on_what = "On branch ";
528 const char *branch_name = s->branch;
529 if (!prefixcmp(branch_name, "refs/heads/"))
531 else if (!strcmp(branch_name, "HEAD")) {
533 branch_color = color(WT_STATUS_NOBRANCH, s);
534 on_what = "Not currently on any branch.";
536 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
537 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
539 wt_status_print_tracking(s);
542 wt_status_collect_changes(s);
545 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
546 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
547 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
550 wt_status_print_unmerged(s);
551 wt_status_print_updated(s);
552 wt_status_print_changed(s);
553 if (s->submodule_summary)
554 wt_status_print_submodule_summary(s);
555 if (s->show_untracked_files)
556 wt_status_print_untracked(s);
557 else if (s->commitable)
558 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
561 wt_status_print_verbose(s);
562 if (!s->commitable) {
564 fprintf(s->fp, "# No changes\n");
567 else if (s->workdir_dirty)
568 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
569 else if (s->workdir_untracked)
570 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
571 else if (s->is_initial)
572 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
573 else if (!s->show_untracked_files)
574 printf("nothing to commit (use -u to show untracked files)\n");
576 printf("nothing to commit (working directory clean)\n");