11 #include "run-command.h"
14 int wt_status_relative_paths = 1;
15 int wt_status_use_color = -1;
16 static int wt_status_submodule_summary;
17 static char wt_status_colors[][COLOR_MAXLEN] = {
18 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
25 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
27 static int parse_status_slot(const char *var, int offset)
29 if (!strcasecmp(var+offset, "header"))
30 return WT_STATUS_HEADER;
31 if (!strcasecmp(var+offset, "updated")
32 || !strcasecmp(var+offset, "added"))
33 return WT_STATUS_UPDATED;
34 if (!strcasecmp(var+offset, "changed"))
35 return WT_STATUS_CHANGED;
36 if (!strcasecmp(var+offset, "untracked"))
37 return WT_STATUS_UNTRACKED;
38 if (!strcasecmp(var+offset, "nobranch"))
39 return WT_STATUS_NOBRANCH;
40 die("bad config variable '%s'", var);
43 static const char *color(int slot)
45 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
48 void wt_status_prepare(struct wt_status *s)
50 unsigned char sha1[20];
53 memset(s, 0, sizeof(*s));
54 head = resolve_ref("HEAD", sha1, 0, NULL);
55 s->branch = head ? xstrdup(head) : NULL;
56 s->reference = "HEAD";
58 s->index_file = get_index_file();
59 s->change.strdup_strings = 1;
62 static void wt_status_print_cached_header(struct wt_status *s)
64 const char *c = color(WT_STATUS_HEADER);
65 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
67 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
69 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
71 color_fprintf_ln(s->fp, c, "#");
74 static void wt_status_print_dirty_header(struct wt_status *s,
77 const char *c = color(WT_STATUS_HEADER);
78 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
80 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
82 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
83 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
84 color_fprintf_ln(s->fp, c, "#");
87 static void wt_status_print_untracked_header(struct wt_status *s)
89 const char *c = color(WT_STATUS_HEADER);
90 color_fprintf_ln(s->fp, c, "# Untracked files:");
91 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
92 color_fprintf_ln(s->fp, c, "#");
95 static void wt_status_print_trailer(struct wt_status *s)
97 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
100 #define quote_path quote_path_relative
102 static void wt_status_print_change_data(struct wt_status *s,
104 struct string_list_item *it)
106 struct wt_status_change_data *d = it->util;
107 const char *c = color(change_type);
111 const char *one, *two;
112 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
114 one_name = two_name = it->string;
115 switch (change_type) {
116 case WT_STATUS_UPDATED:
117 status = d->index_status;
119 one_name = d->head_path;
121 case WT_STATUS_CHANGED:
122 status = d->worktree_status;
126 one = quote_path(one_name, -1, &onebuf, s->prefix);
127 two = quote_path(two_name, -1, &twobuf, s->prefix);
129 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
131 case DIFF_STATUS_ADDED:
132 color_fprintf(s->fp, c, "new file: %s", one);
134 case DIFF_STATUS_COPIED:
135 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
137 case DIFF_STATUS_DELETED:
138 color_fprintf(s->fp, c, "deleted: %s", one);
140 case DIFF_STATUS_MODIFIED:
141 color_fprintf(s->fp, c, "modified: %s", one);
143 case DIFF_STATUS_RENAMED:
144 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
146 case DIFF_STATUS_TYPE_CHANGED:
147 color_fprintf(s->fp, c, "typechange: %s", one);
149 case DIFF_STATUS_UNKNOWN:
150 color_fprintf(s->fp, c, "unknown: %s", one);
152 case DIFF_STATUS_UNMERGED:
153 color_fprintf(s->fp, c, "unmerged: %s", one);
156 die("bug: unhandled diff status %c", status);
158 fprintf(s->fp, "\n");
159 strbuf_release(&onebuf);
160 strbuf_release(&twobuf);
163 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
164 struct diff_options *options,
167 struct wt_status *s = data;
172 s->workdir_dirty = 1;
173 for (i = 0; i < q->nr; i++) {
174 struct diff_filepair *p;
175 struct string_list_item *it;
176 struct wt_status_change_data *d;
179 it = string_list_insert(p->one->path, &s->change);
182 d = xcalloc(1, sizeof(*d));
185 if (!d->worktree_status)
186 d->worktree_status = p->status;
190 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
191 struct diff_options *options,
194 struct wt_status *s = data;
197 for (i = 0; i < q->nr; i++) {
198 struct diff_filepair *p;
199 struct string_list_item *it;
200 struct wt_status_change_data *d;
203 it = string_list_insert(p->two->path, &s->change);
206 d = xcalloc(1, sizeof(*d));
209 if (!d->index_status)
210 d->index_status = p->status;
212 case DIFF_STATUS_COPIED:
213 case DIFF_STATUS_RENAMED:
214 d->head_path = xstrdup(p->one->path);
220 static void wt_status_collect_changes_worktree(struct wt_status *s)
224 init_revisions(&rev, NULL);
225 setup_revisions(0, NULL, &rev, NULL);
226 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
227 rev.diffopt.format_callback = wt_status_collect_changed_cb;
228 rev.diffopt.format_callback_data = s;
229 run_diff_files(&rev, 0);
232 static void wt_status_collect_changes_index(struct wt_status *s)
236 init_revisions(&rev, NULL);
237 setup_revisions(0, NULL, &rev,
238 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
239 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
240 rev.diffopt.format_callback = wt_status_collect_updated_cb;
241 rev.diffopt.format_callback_data = s;
242 rev.diffopt.detect_rename = 1;
243 rev.diffopt.rename_limit = 200;
244 rev.diffopt.break_opt = 0;
245 run_diff_index(&rev, 1);
248 static void wt_status_collect_changes_initial(struct wt_status *s)
252 for (i = 0; i < active_nr; i++) {
253 struct string_list_item *it;
254 struct wt_status_change_data *d;
255 struct cache_entry *ce = active_cache[i];
257 it = string_list_insert(ce->name, &s->change);
260 d = xcalloc(1, sizeof(*d));
264 d->index_status = DIFF_STATUS_UNMERGED;
266 d->index_status = DIFF_STATUS_ADDED;
270 void wt_status_collect_changes(struct wt_status *s)
272 wt_status_collect_changes_worktree(s);
275 wt_status_collect_changes_initial(s);
277 wt_status_collect_changes_index(s);
280 static void wt_status_print_updated(struct wt_status *s)
282 int shown_header = 0;
285 for (i = 0; i < s->change.nr; i++) {
286 struct wt_status_change_data *d;
287 struct string_list_item *it;
288 it = &(s->change.items[i]);
290 if (!d->index_status ||
291 d->index_status == DIFF_STATUS_UNMERGED)
294 wt_status_print_cached_header(s);
298 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
301 wt_status_print_trailer(s);
307 * 1 : some change but no delete
309 static int wt_status_check_worktree_changes(struct wt_status *s)
314 for (i = 0; i < s->change.nr; i++) {
315 struct wt_status_change_data *d;
316 d = s->change.items[i].util;
317 if (!d->worktree_status)
320 if (d->worktree_status == DIFF_STATUS_DELETED)
326 static void wt_status_print_changed(struct wt_status *s)
329 int worktree_changes = wt_status_check_worktree_changes(s);
331 if (!worktree_changes)
334 wt_status_print_dirty_header(s, worktree_changes < 0);
336 for (i = 0; i < s->change.nr; i++) {
337 struct wt_status_change_data *d;
338 struct string_list_item *it;
339 it = &(s->change.items[i]);
341 if (!d->worktree_status)
343 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
345 wt_status_print_trailer(s);
348 static void wt_status_print_submodule_summary(struct wt_status *s)
350 struct child_process sm_summary;
351 char summary_limit[64];
352 char index[PATH_MAX];
353 const char *env[] = { index, NULL };
354 const char *argv[] = {
361 s->amend ? "HEAD^" : "HEAD",
365 sprintf(summary_limit, "%d", wt_status_submodule_summary);
366 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
368 memset(&sm_summary, 0, sizeof(sm_summary));
369 sm_summary.argv = argv;
370 sm_summary.env = env;
371 sm_summary.git_cmd = 1;
372 sm_summary.no_stdin = 1;
374 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
375 run_command(&sm_summary);
378 static void wt_status_print_untracked(struct wt_status *s)
380 struct dir_struct dir;
382 int shown_header = 0;
383 struct strbuf buf = STRBUF_INIT;
385 memset(&dir, 0, sizeof(dir));
389 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
390 setup_standard_excludes(&dir);
392 fill_directory(&dir, NULL);
393 for(i = 0; i < dir.nr; i++) {
394 struct dir_entry *ent = dir.entries[i];
395 if (!cache_name_is_other(ent->name, ent->len))
398 s->workdir_untracked = 1;
399 wt_status_print_untracked_header(s);
402 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
403 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
404 quote_path(ent->name, ent->len,
407 strbuf_release(&buf);
410 static void wt_status_print_verbose(struct wt_status *s)
414 init_revisions(&rev, NULL);
415 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
416 setup_revisions(0, NULL, &rev,
417 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
418 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
419 rev.diffopt.detect_rename = 1;
420 rev.diffopt.file = s->fp;
421 rev.diffopt.close_file = 0;
423 * If we're not going to stdout, then we definitely don't
424 * want color, since we are going to the commit message
425 * file (and even the "auto" setting won't work, since it
426 * will have checked isatty on stdout).
429 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
430 run_diff_index(&rev, 1);
433 static void wt_status_print_tracking(struct wt_status *s)
435 struct strbuf sb = STRBUF_INIT;
437 struct branch *branch;
439 assert(s->branch && !s->is_initial);
440 if (prefixcmp(s->branch, "refs/heads/"))
442 branch = branch_get(s->branch + 11);
443 if (!format_tracking_info(branch, &sb))
446 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
447 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
448 "# %.*s", (int)(ep - cp), cp);
449 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
452 void wt_status_print(struct wt_status *s)
454 unsigned char sha1[20];
455 const char *branch_color = color(WT_STATUS_HEADER);
457 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
459 const char *on_what = "On branch ";
460 const char *branch_name = s->branch;
461 if (!prefixcmp(branch_name, "refs/heads/"))
463 else if (!strcmp(branch_name, "HEAD")) {
465 branch_color = color(WT_STATUS_NOBRANCH);
466 on_what = "Not currently on any branch.";
468 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
469 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
471 wt_status_print_tracking(s);
474 wt_status_collect_changes(s);
477 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
478 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
479 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
482 wt_status_print_updated(s);
483 wt_status_print_changed(s);
484 if (wt_status_submodule_summary)
485 wt_status_print_submodule_summary(s);
486 if (show_untracked_files)
487 wt_status_print_untracked(s);
488 else if (s->commitable)
489 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
492 wt_status_print_verbose(s);
493 if (!s->commitable) {
495 fprintf(s->fp, "# No changes\n");
498 else if (s->workdir_dirty)
499 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
500 else if (s->workdir_untracked)
501 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
502 else if (s->is_initial)
503 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
504 else if (!show_untracked_files)
505 printf("nothing to commit (use -u to show untracked files)\n");
507 printf("nothing to commit (working directory clean)\n");
511 int git_status_config(const char *k, const char *v, void *cb)
513 if (!strcmp(k, "status.submodulesummary")) {
515 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
516 if (is_bool && wt_status_submodule_summary)
517 wt_status_submodule_summary = -1;
520 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
521 wt_status_use_color = git_config_colorbool(k, v, -1);
524 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
525 int slot = parse_status_slot(k, 13);
527 return config_error_nonbool(k);
528 color_parse(v, k, wt_status_colors[slot]);
531 if (!strcmp(k, "status.relativepaths")) {
532 wt_status_relative_paths = git_config_bool(k, v);
535 if (!strcmp(k, "status.showuntrackedfiles")) {
537 return config_error_nonbool(k);
538 else if (!strcmp(v, "no"))
539 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
540 else if (!strcmp(v, "normal"))
541 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
542 else if (!strcmp(v, "all"))
543 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
545 return error("Invalid untracked files mode '%s'", v);
548 return git_diff_ui_config(k, v, cb);