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 const char use_add_msg[] =
20 "use \"git add <file>...\" to update what will be committed";
21 static const char use_add_rm_msg[] =
22 "use \"git add/rm <file>...\" to update what will be committed";
23 static const char use_add_to_include_msg[] =
24 "use \"git add <file>...\" to include in what will be committed";
26 static int parse_status_slot(const char *var, int offset)
28 if (!strcasecmp(var+offset, "header"))
29 return WT_STATUS_HEADER;
30 if (!strcasecmp(var+offset, "updated")
31 || !strcasecmp(var+offset, "added"))
32 return WT_STATUS_UPDATED;
33 if (!strcasecmp(var+offset, "changed"))
34 return WT_STATUS_CHANGED;
35 if (!strcasecmp(var+offset, "untracked"))
36 return WT_STATUS_UNTRACKED;
37 die("bad config variable '%s'", var);
40 static const char* color(int slot)
42 return wt_status_use_color ? wt_status_colors[slot] : "";
45 void wt_status_prepare(struct wt_status *s)
47 unsigned char sha1[20];
50 memset(s, 0, sizeof(*s));
51 head = resolve_ref("HEAD", sha1, 0, NULL);
52 s->branch = head ? xstrdup(head) : NULL;
53 s->reference = "HEAD";
55 s->index_file = get_index_file();
58 static void wt_status_print_cached_header(struct wt_status *s)
60 const char *c = color(WT_STATUS_HEADER);
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_header(struct wt_status *s,
71 const char *main, const char *sub)
73 const char *c = color(WT_STATUS_HEADER);
74 color_fprintf_ln(s->fp, c, "# %s:", main);
75 color_fprintf_ln(s->fp, c, "# (%s)", sub);
76 color_fprintf_ln(s->fp, c, "#");
79 static void wt_status_print_trailer(struct wt_status *s)
81 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
84 static char *quote_path(const char *in, int len,
85 struct strbuf *out, const char *prefix)
90 strbuf_grow(out, len);
91 strbuf_setlen(out, 0);
94 while (prefix[off] && off < len && prefix[off] == in[off])
95 if (prefix[off] == '/') {
103 for (; *prefix; prefix++)
105 strbuf_addstr(out, "../");
108 for ( ; len > 0; in++, len--) {
113 strbuf_addstr(out, "\\n");
116 strbuf_addstr(out, "\\r");
119 strbuf_addch(out, ch);
125 strbuf_addstr(out, "./");
130 static void wt_status_print_filepair(struct wt_status *s,
131 int t, struct diff_filepair *p)
133 const char *c = color(t);
134 const char *one, *two;
135 struct strbuf onebuf, twobuf;
137 strbuf_init(&onebuf, 0);
138 strbuf_init(&twobuf, 0);
139 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
140 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
142 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
144 case DIFF_STATUS_ADDED:
145 color_fprintf(s->fp, c, "new file: %s", one);
147 case DIFF_STATUS_COPIED:
148 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
150 case DIFF_STATUS_DELETED:
151 color_fprintf(s->fp, c, "deleted: %s", one);
153 case DIFF_STATUS_MODIFIED:
154 color_fprintf(s->fp, c, "modified: %s", one);
156 case DIFF_STATUS_RENAMED:
157 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
159 case DIFF_STATUS_TYPE_CHANGED:
160 color_fprintf(s->fp, c, "typechange: %s", one);
162 case DIFF_STATUS_UNKNOWN:
163 color_fprintf(s->fp, c, "unknown: %s", one);
165 case DIFF_STATUS_UNMERGED:
166 color_fprintf(s->fp, c, "unmerged: %s", one);
169 die("bug: unhandled diff status %c", p->status);
171 fprintf(s->fp, "\n");
172 strbuf_release(&onebuf);
173 strbuf_release(&twobuf);
176 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
177 struct diff_options *options,
180 struct wt_status *s = data;
181 int shown_header = 0;
183 for (i = 0; i < q->nr; i++) {
184 if (q->queue[i]->status == 'U')
187 wt_status_print_cached_header(s);
191 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
194 wt_status_print_trailer(s);
197 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
198 struct diff_options *options,
201 struct wt_status *s = data;
204 const char *msg = use_add_msg;
205 s->workdir_dirty = 1;
206 for (i = 0; i < q->nr; i++)
207 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
208 msg = use_add_rm_msg;
211 wt_status_print_header(s, "Changed but not updated", msg);
213 for (i = 0; i < q->nr; i++)
214 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
216 wt_status_print_trailer(s);
219 static void wt_read_cache(struct wt_status *s)
222 read_cache_from(s->index_file);
225 static void wt_status_print_initial(struct wt_status *s)
230 strbuf_init(&buf, 0);
234 wt_status_print_cached_header(s);
236 for (i = 0; i < active_nr; i++) {
237 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
238 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
239 quote_path(active_cache[i]->name, -1,
243 wt_status_print_trailer(s);
244 strbuf_release(&buf);
247 static void wt_status_print_updated(struct wt_status *s)
250 init_revisions(&rev, NULL);
251 setup_revisions(0, NULL, &rev, s->reference);
252 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
253 rev.diffopt.format_callback = wt_status_print_updated_cb;
254 rev.diffopt.format_callback_data = s;
255 rev.diffopt.detect_rename = 1;
256 rev.diffopt.rename_limit = 100;
257 rev.diffopt.break_opt = 0;
259 run_diff_index(&rev, 1);
262 static void wt_status_print_changed(struct wt_status *s)
265 init_revisions(&rev, "");
266 setup_revisions(0, NULL, &rev, NULL);
267 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
268 rev.diffopt.format_callback = wt_status_print_changed_cb;
269 rev.diffopt.format_callback_data = s;
271 run_diff_files(&rev, 0);
274 static void wt_status_print_untracked(struct wt_status *s)
276 struct dir_struct dir;
278 int shown_header = 0;
281 strbuf_init(&buf, 0);
282 memset(&dir, 0, sizeof(dir));
285 dir.show_other_directories = 1;
286 dir.hide_empty_directories = 1;
288 setup_standard_excludes(&dir);
290 read_directory(&dir, ".", "", 0, NULL);
291 for(i = 0; i < dir.nr; i++) {
292 /* check for matching entry, which is unmerged; lifted from
293 * builtin-ls-files:show_other_files */
294 struct dir_entry *ent = dir.entries[i];
295 int pos = cache_name_pos(ent->name, ent->len);
296 struct cache_entry *ce;
298 die("bug in wt_status_print_untracked");
300 if (pos < active_nr) {
301 ce = active_cache[pos];
302 if (ce_namelen(ce) == ent->len &&
303 !memcmp(ce->name, ent->name, ent->len))
307 s->workdir_untracked = 1;
308 wt_status_print_header(s, "Untracked files",
309 use_add_to_include_msg);
312 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
313 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
314 quote_path(ent->name, ent->len,
317 strbuf_release(&buf);
320 static void wt_status_print_verbose(struct wt_status *s)
327 /* Sigh, the entire diff machinery is hardcoded to output to
328 * stdout. Do the dup-dance...*/
329 saved_stdout = dup(STDOUT_FILENO);
330 if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
331 die("couldn't redirect stdout\n");
333 init_revisions(&rev, NULL);
334 setup_revisions(0, NULL, &rev, s->reference);
335 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
336 rev.diffopt.detect_rename = 1;
338 run_diff_index(&rev, 1);
342 if (dup2(saved_stdout, STDOUT_FILENO) < 0)
343 die("couldn't restore stdout\n");
347 void wt_status_print(struct wt_status *s)
349 unsigned char sha1[20];
350 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
353 const char *on_what = "On branch ";
354 const char *branch_name = s->branch;
355 if (!prefixcmp(branch_name, "refs/heads/"))
357 else if (!strcmp(branch_name, "HEAD")) {
359 on_what = "Not currently on any branch.";
361 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
362 "# %s%s", on_what, branch_name);
366 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
367 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
368 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
369 wt_status_print_initial(s);
372 wt_status_print_updated(s);
375 wt_status_print_changed(s);
376 wt_status_print_untracked(s);
378 if (s->verbose && !s->is_initial)
379 wt_status_print_verbose(s);
380 if (!s->commitable) {
382 fprintf(s->fp, "# No changes\n");
383 else if (s->workdir_dirty)
384 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
385 else if (s->workdir_untracked)
386 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
387 else if (s->is_initial)
388 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
390 printf("nothing to commit (working directory clean)\n");
394 int git_status_config(const char *k, const char *v)
396 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
397 wt_status_use_color = git_config_colorbool(k, v, -1);
400 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
401 int slot = parse_status_slot(k, 13);
402 color_parse(v, k, wt_status_colors[slot]);
404 return git_default_config(k, v);