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 */
18 static const char* use_add_msg = "use \"git add <file>...\" to incrementally add content to commit";
20 static int parse_status_slot(const char *var, int offset)
22 if (!strcasecmp(var+offset, "header"))
23 return WT_STATUS_HEADER;
24 if (!strcasecmp(var+offset, "updated")
25 || !strcasecmp(var+offset, "added"))
26 return WT_STATUS_UPDATED;
27 if (!strcasecmp(var+offset, "changed"))
28 return WT_STATUS_CHANGED;
29 if (!strcasecmp(var+offset, "untracked"))
30 return WT_STATUS_UNTRACKED;
31 die("bad config variable '%s'", var);
34 static const char* color(int slot)
36 return wt_status_use_color ? wt_status_colors[slot] : "";
39 void wt_status_prepare(struct wt_status *s)
41 unsigned char sha1[20];
44 head = resolve_ref("HEAD", sha1, 0, NULL);
45 s->branch = head ? xstrdup(head) : NULL;
47 s->reference = "HEAD";
56 static void wt_status_print_cached_header(const char *reference)
58 const char *c = color(WT_STATUS_HEADER);
59 color_printf_ln(c, "# Cached changes to be committed:");
61 color_printf_ln(c, "# (use \"git reset %s <file>...\" and \"git rm --cached <file>...\" to unstage)", reference);
63 color_printf_ln(c, "# (use \"git rm --cached <file>...\" to unstage)");
65 color_printf_ln(c, "#");
68 static void wt_status_print_header(const char *main, const char *sub)
70 const char *c = color(WT_STATUS_HEADER);
71 color_printf_ln(c, "# %s:", main);
72 color_printf_ln(c, "# (%s)", sub);
73 color_printf_ln(c, "#");
76 static void wt_status_print_trailer(void)
78 color_printf_ln(color(WT_STATUS_HEADER), "#");
81 static const char *quote_crlf(const char *in, char *buf, size_t sz)
87 for (scan = in, out = buf; *scan; scan++) {
110 static void wt_status_print_filepair(int t, struct diff_filepair *p)
112 const char *c = color(t);
113 const char *one, *two;
114 char onebuf[PATH_MAX], twobuf[PATH_MAX];
116 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
117 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
119 color_printf(color(WT_STATUS_HEADER), "#\t");
121 case DIFF_STATUS_ADDED:
122 color_printf(c, "new file: %s", one);
124 case DIFF_STATUS_COPIED:
125 color_printf(c, "copied: %s -> %s", one, two);
127 case DIFF_STATUS_DELETED:
128 color_printf(c, "deleted: %s", one);
130 case DIFF_STATUS_MODIFIED:
131 color_printf(c, "modified: %s", one);
133 case DIFF_STATUS_RENAMED:
134 color_printf(c, "renamed: %s -> %s", one, two);
136 case DIFF_STATUS_TYPE_CHANGED:
137 color_printf(c, "typechange: %s", one);
139 case DIFF_STATUS_UNKNOWN:
140 color_printf(c, "unknown: %s", one);
142 case DIFF_STATUS_UNMERGED:
143 color_printf(c, "unmerged: %s", one);
146 die("bug: unhandled diff status %c", p->status);
151 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
152 struct diff_options *options,
155 struct wt_status *s = data;
156 int shown_header = 0;
158 for (i = 0; i < q->nr; i++) {
159 if (q->queue[i]->status == 'U')
162 wt_status_print_cached_header(s->reference);
166 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
169 wt_status_print_trailer();
172 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
173 struct diff_options *options,
176 struct wt_status *s = data;
179 s->workdir_clean = 0;
180 wt_status_print_header("Changed but not added", use_add_msg);
182 for (i = 0; i < q->nr; i++)
183 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
185 wt_status_print_trailer();
188 void wt_status_print_initial(struct wt_status *s)
196 wt_status_print_cached_header(NULL);
198 for (i = 0; i < active_nr; i++) {
199 color_printf(color(WT_STATUS_HEADER), "#\t");
200 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
201 quote_crlf(active_cache[i]->name,
205 wt_status_print_trailer();
208 static void wt_status_print_updated(struct wt_status *s)
211 init_revisions(&rev, NULL);
212 setup_revisions(0, NULL, &rev, s->reference);
213 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
214 rev.diffopt.format_callback = wt_status_print_updated_cb;
215 rev.diffopt.format_callback_data = s;
216 rev.diffopt.detect_rename = 1;
217 run_diff_index(&rev, 1);
220 static void wt_status_print_changed(struct wt_status *s)
223 init_revisions(&rev, "");
224 setup_revisions(0, NULL, &rev, NULL);
225 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
226 rev.diffopt.format_callback = wt_status_print_changed_cb;
227 rev.diffopt.format_callback_data = s;
228 run_diff_files(&rev, 0);
231 static void wt_status_print_untracked(struct wt_status *s)
233 struct dir_struct dir;
236 int shown_header = 0;
238 memset(&dir, 0, sizeof(dir));
240 dir.exclude_per_dir = ".gitignore";
242 dir.show_other_directories = 1;
243 dir.hide_empty_directories = 1;
245 x = git_path("info/exclude");
247 add_excludes_from_file(&dir, x);
249 read_directory(&dir, ".", "", 0);
250 for(i = 0; i < dir.nr; i++) {
251 /* check for matching entry, which is unmerged; lifted from
252 * builtin-ls-files:show_other_files */
253 struct dir_entry *ent = dir.entries[i];
254 int pos = cache_name_pos(ent->name, ent->len);
255 struct cache_entry *ce;
257 die("bug in wt_status_print_untracked");
259 if (pos < active_nr) {
260 ce = active_cache[pos];
261 if (ce_namelen(ce) == ent->len &&
262 !memcmp(ce->name, ent->name, ent->len))
266 s->workdir_clean = 0;
267 wt_status_print_header("Untracked files", use_add_msg);
270 color_printf(color(WT_STATUS_HEADER), "#\t");
271 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
272 ent->len, ent->name);
276 static void wt_status_print_verbose(struct wt_status *s)
279 init_revisions(&rev, NULL);
280 setup_revisions(0, NULL, &rev, s->reference);
281 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
282 rev.diffopt.detect_rename = 1;
283 run_diff_index(&rev, 1);
286 void wt_status_print(struct wt_status *s)
288 unsigned char sha1[20];
289 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
292 color_printf_ln(color(WT_STATUS_HEADER),
293 "# On branch %s", s->branch);
296 color_printf_ln(color(WT_STATUS_HEADER), "#");
297 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
298 color_printf_ln(color(WT_STATUS_HEADER), "#");
299 wt_status_print_initial(s);
302 wt_status_print_updated(s);
306 wt_status_print_changed(s);
307 wt_status_print_untracked(s);
309 if (s->verbose && !s->is_initial)
310 wt_status_print_verbose(s);
311 if (!s->commitable) {
313 printf("# No changes\n");
314 else if (s->workdir_clean)
316 ? "nothing to commit\n"
317 : "nothing to commit (working directory matches HEAD)\n");
319 printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
323 int git_status_config(const char *k, const char *v)
325 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
326 wt_status_use_color = git_config_colorbool(k, v);
329 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
330 int slot = parse_status_slot(k, 13);
331 color_parse(v, k, wt_status_colors[slot]);
333 return git_default_config(k, v);