11 int wt_status_use_color = 0;
12 static char wt_status_colors[][COLOR_MAXLEN] = {
13 "", /* WT_STATUS_HEADER: normal */
14 "\033[32m", /* WT_STATUS_UPDATED: green */
15 "\033[31m", /* WT_STATUS_CHANGED: red */
16 "\033[31m", /* WT_STATUS_UNTRACKED: red */
19 static int parse_status_slot(const char *var, int offset)
21 if (!strcasecmp(var+offset, "header"))
22 return WT_STATUS_HEADER;
23 if (!strcasecmp(var+offset, "updated"))
24 return WT_STATUS_UPDATED;
25 if (!strcasecmp(var+offset, "changed"))
26 return WT_STATUS_CHANGED;
27 if (!strcasecmp(var+offset, "untracked"))
28 return WT_STATUS_UNTRACKED;
29 die("bad config variable '%s'", var);
32 static const char* color(int slot)
34 return wt_status_use_color ? wt_status_colors[slot] : "";
37 void wt_status_prepare(struct wt_status *s)
39 unsigned char sha1[20];
42 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
44 head = resolve_ref("HEAD", sha1, 0, NULL);
45 s->branch = head ? xstrdup(head) : NULL;
47 s->reference = "HEAD";
54 static void wt_status_print_header(const char *main, const char *sub)
56 const char *c = color(WT_STATUS_HEADER);
57 color_printf_ln(c, "# %s:", main);
58 color_printf_ln(c, "# (%s)", sub);
59 color_printf_ln(c, "#");
62 static void wt_status_print_trailer(void)
64 color_printf_ln(color(WT_STATUS_HEADER), "#");
67 static void wt_status_print_filepair(int t, struct diff_filepair *p)
69 const char *c = color(t);
70 color_printf(color(WT_STATUS_HEADER), "#\t");
72 case DIFF_STATUS_ADDED:
73 color_printf(c, "new file: %s", p->one->path); break;
74 case DIFF_STATUS_COPIED:
75 color_printf(c, "copied: %s -> %s",
76 p->one->path, p->two->path);
78 case DIFF_STATUS_DELETED:
79 color_printf(c, "deleted: %s", p->one->path); break;
80 case DIFF_STATUS_MODIFIED:
81 color_printf(c, "modified: %s", p->one->path); break;
82 case DIFF_STATUS_RENAMED:
83 color_printf(c, "renamed: %s -> %s",
84 p->one->path, p->two->path);
86 case DIFF_STATUS_TYPE_CHANGED:
87 color_printf(c, "typechange: %s", p->one->path); break;
88 case DIFF_STATUS_UNKNOWN:
89 color_printf(c, "unknown: %s", p->one->path); break;
90 case DIFF_STATUS_UNMERGED:
91 color_printf(c, "unmerged: %s", p->one->path); break;
93 die("bug: unhandled diff status %c", p->status);
98 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
99 struct diff_options *options,
102 struct wt_status *s = data;
103 int shown_header = 0;
107 for (i = 0; i < q->nr; i++) {
108 if (q->queue[i]->status == 'U')
111 wt_status_print_header("Updated but not checked in",
116 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
119 wt_status_print_trailer();
122 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
123 struct diff_options *options,
128 wt_status_print_header("Changed but not updated",
129 "use git-update-index to mark for commit");
130 for (i = 0; i < q->nr; i++)
131 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
133 wt_status_print_trailer();
136 void wt_status_print_initial(struct wt_status *s)
142 wt_status_print_header("Updated but not checked in",
145 for (i = 0; i < active_nr; i++) {
146 color_printf(color(WT_STATUS_HEADER), "#\t");
147 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
148 active_cache[i]->name);
151 wt_status_print_trailer();
154 static void wt_status_print_updated(struct wt_status *s)
157 const char *argv[] = { NULL, NULL, NULL };
158 argv[1] = s->reference;
159 init_revisions(&rev, NULL);
160 setup_revisions(2, argv, &rev, NULL);
161 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
162 rev.diffopt.format_callback = wt_status_print_updated_cb;
163 rev.diffopt.format_callback_data = s;
164 rev.diffopt.detect_rename = 1;
165 run_diff_index(&rev, 1);
168 static void wt_status_print_changed(struct wt_status *s)
171 const char *argv[] = { NULL, NULL };
172 init_revisions(&rev, "");
173 setup_revisions(1, argv, &rev, NULL);
174 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
175 rev.diffopt.format_callback = wt_status_print_changed_cb;
176 rev.diffopt.format_callback_data = s;
177 run_diff_files(&rev, 0);
180 static void wt_status_print_untracked(const struct wt_status *s)
182 struct dir_struct dir;
185 int shown_header = 0;
187 memset(&dir, 0, sizeof(dir));
189 dir.exclude_per_dir = ".gitignore";
191 dir.show_other_directories = 1;
192 dir.hide_empty_directories = 1;
194 x = git_path("info/exclude");
196 add_excludes_from_file(&dir, x);
198 read_directory(&dir, ".", "", 0);
199 for(i = 0; i < dir.nr; i++) {
200 /* check for matching entry, which is unmerged; lifted from
201 * builtin-ls-files:show_other_files */
202 struct dir_entry *ent = dir.entries[i];
203 int pos = cache_name_pos(ent->name, ent->len);
204 struct cache_entry *ce;
206 die("bug in wt_status_print_untracked");
208 if (pos < active_nr) {
209 ce = active_cache[pos];
210 if (ce_namelen(ce) == ent->len &&
211 !memcmp(ce->name, ent->name, ent->len))
215 wt_status_print_header("Untracked files",
216 "use \"git add\" to add to commit");
219 color_printf(color(WT_STATUS_HEADER), "#\t");
220 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
221 ent->len, ent->name);
225 static void wt_status_print_verbose(struct wt_status *s)
228 const char *argv[] = { NULL, NULL, NULL };
229 argv[1] = s->reference;
230 init_revisions(&rev, NULL);
231 setup_revisions(2, argv, &rev, NULL);
232 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
233 rev.diffopt.detect_rename = 1;
234 run_diff_index(&rev, 1);
237 void wt_status_print(struct wt_status *s)
239 if (s->branch && strcmp(s->branch, "refs/heads/master"))
240 color_printf_ln(color(WT_STATUS_HEADER),
241 "# On branch %s", s->branch);
244 color_printf_ln(color(WT_STATUS_HEADER), "#");
245 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
246 color_printf_ln(color(WT_STATUS_HEADER), "#");
247 wt_status_print_initial(s);
250 wt_status_print_updated(s);
254 wt_status_print_changed(s);
255 wt_status_print_untracked(s);
257 if (s->verbose && !s->is_initial)
258 wt_status_print_verbose(s);
260 printf("%s\n", s->amend ? "# No changes" : "nothing to commit");
263 int git_status_config(const char *k, const char *v)
265 if (!strcmp(k, "status.color")) {
266 wt_status_use_color = git_config_colorbool(k, v);
269 if (!strncmp(k, "status.color.", 13)) {
270 int slot = parse_status_slot(k, 13);
271 color_parse(v, k, wt_status_colors[slot]);
273 return git_default_config(k, v);