11 int wt_status_relative_paths = 1;
12 int wt_status_use_color = -1;
13 static char wt_status_colors[][COLOR_MAXLEN] = {
14 "", /* WT_STATUS_HEADER: normal */
15 "\033[32m", /* WT_STATUS_UPDATED: green */
16 "\033[31m", /* WT_STATUS_CHANGED: red */
17 "\033[31m", /* WT_STATUS_UNTRACKED: red */
20 static const char use_add_msg[] =
21 "use \"git add <file>...\" to update what will be committed";
22 static const char use_add_rm_msg[] =
23 "use \"git add/rm <file>...\" to update what will be committed";
24 static const char use_add_to_include_msg[] =
25 "use \"git add <file>...\" to include in what will be committed";
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 die("bad config variable '%s'", var);
41 static const char* color(int slot)
43 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
46 void wt_status_prepare(struct wt_status *s)
48 unsigned char sha1[20];
51 memset(s, 0, sizeof(*s));
52 head = resolve_ref("HEAD", sha1, 0, NULL);
53 s->branch = head ? xstrdup(head) : NULL;
54 s->reference = "HEAD";
56 s->index_file = get_index_file();
59 static void wt_status_print_cached_header(struct wt_status *s)
61 const char *c = color(WT_STATUS_HEADER);
62 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
64 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
66 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
68 color_fprintf_ln(s->fp, c, "#");
71 static void wt_status_print_header(struct wt_status *s,
72 const char *main, const char *sub)
74 const char *c = color(WT_STATUS_HEADER);
75 color_fprintf_ln(s->fp, c, "# %s:", main);
76 color_fprintf_ln(s->fp, c, "# (%s)", sub);
77 color_fprintf_ln(s->fp, c, "#");
80 static void wt_status_print_trailer(struct wt_status *s)
82 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
85 static char *quote_path(const char *in, int len,
86 struct strbuf *out, const char *prefix)
91 strbuf_grow(out, len);
92 strbuf_setlen(out, 0);
95 while (prefix[off] && off < len && prefix[off] == in[off])
96 if (prefix[off] == '/') {
104 for (; *prefix; prefix++)
106 strbuf_addstr(out, "../");
109 for ( ; len > 0; in++, len--) {
114 strbuf_addstr(out, "\\n");
117 strbuf_addstr(out, "\\r");
120 strbuf_addch(out, ch);
126 strbuf_addstr(out, "./");
131 static void wt_status_print_filepair(struct wt_status *s,
132 int t, struct diff_filepair *p)
134 const char *c = color(t);
135 const char *one, *two;
136 struct strbuf onebuf, twobuf;
138 strbuf_init(&onebuf, 0);
139 strbuf_init(&twobuf, 0);
140 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
141 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
143 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
145 case DIFF_STATUS_ADDED:
146 color_fprintf(s->fp, c, "new file: %s", one);
148 case DIFF_STATUS_COPIED:
149 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
151 case DIFF_STATUS_DELETED:
152 color_fprintf(s->fp, c, "deleted: %s", one);
154 case DIFF_STATUS_MODIFIED:
155 color_fprintf(s->fp, c, "modified: %s", one);
157 case DIFF_STATUS_RENAMED:
158 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
160 case DIFF_STATUS_TYPE_CHANGED:
161 color_fprintf(s->fp, c, "typechange: %s", one);
163 case DIFF_STATUS_UNKNOWN:
164 color_fprintf(s->fp, c, "unknown: %s", one);
166 case DIFF_STATUS_UNMERGED:
167 color_fprintf(s->fp, c, "unmerged: %s", one);
170 die("bug: unhandled diff status %c", p->status);
172 fprintf(s->fp, "\n");
173 strbuf_release(&onebuf);
174 strbuf_release(&twobuf);
177 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
178 struct diff_options *options,
181 struct wt_status *s = data;
182 int shown_header = 0;
184 for (i = 0; i < q->nr; i++) {
185 if (q->queue[i]->status == 'U')
188 wt_status_print_cached_header(s);
192 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
195 wt_status_print_trailer(s);
198 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
199 struct diff_options *options,
202 struct wt_status *s = data;
205 const char *msg = use_add_msg;
206 s->workdir_dirty = 1;
207 for (i = 0; i < q->nr; i++)
208 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
209 msg = use_add_rm_msg;
212 wt_status_print_header(s, "Changed but not updated", msg);
214 for (i = 0; i < q->nr; i++)
215 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
217 wt_status_print_trailer(s);
220 static void wt_status_print_initial(struct wt_status *s)
225 strbuf_init(&buf, 0);
228 wt_status_print_cached_header(s);
230 for (i = 0; i < active_nr; i++) {
231 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
232 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
233 quote_path(active_cache[i]->name, -1,
237 wt_status_print_trailer(s);
238 strbuf_release(&buf);
241 static void wt_status_print_updated(struct wt_status *s)
244 init_revisions(&rev, NULL);
245 setup_revisions(0, NULL, &rev, s->reference);
246 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
247 rev.diffopt.format_callback = wt_status_print_updated_cb;
248 rev.diffopt.format_callback_data = s;
249 rev.diffopt.detect_rename = 1;
250 rev.diffopt.rename_limit = 100;
251 rev.diffopt.break_opt = 0;
252 run_diff_index(&rev, 1);
255 static void wt_status_print_changed(struct wt_status *s)
258 init_revisions(&rev, "");
259 setup_revisions(0, NULL, &rev, NULL);
260 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
261 rev.diffopt.format_callback = wt_status_print_changed_cb;
262 rev.diffopt.format_callback_data = s;
263 run_diff_files(&rev, 0);
266 static void wt_status_print_untracked(struct wt_status *s)
268 struct dir_struct dir;
270 int shown_header = 0;
273 strbuf_init(&buf, 0);
274 memset(&dir, 0, sizeof(dir));
277 dir.show_other_directories = 1;
278 dir.hide_empty_directories = 1;
280 setup_standard_excludes(&dir);
282 read_directory(&dir, ".", "", 0, NULL);
283 for(i = 0; i < dir.nr; i++) {
284 /* check for matching entry, which is unmerged; lifted from
285 * builtin-ls-files:show_other_files */
286 struct dir_entry *ent = dir.entries[i];
287 int pos = cache_name_pos(ent->name, ent->len);
288 struct cache_entry *ce;
290 die("bug in wt_status_print_untracked");
292 if (pos < active_nr) {
293 ce = active_cache[pos];
294 if (ce_namelen(ce) == ent->len &&
295 !memcmp(ce->name, ent->name, ent->len))
299 s->workdir_untracked = 1;
300 wt_status_print_header(s, "Untracked files",
301 use_add_to_include_msg);
304 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
305 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
306 quote_path(ent->name, ent->len,
309 strbuf_release(&buf);
312 static void wt_status_print_verbose(struct wt_status *s)
319 /* Sigh, the entire diff machinery is hardcoded to output to
320 * stdout. Do the dup-dance...*/
321 saved_stdout = dup(STDOUT_FILENO);
322 if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
323 die("couldn't redirect stdout\n");
325 init_revisions(&rev, NULL);
326 setup_revisions(0, NULL, &rev, s->reference);
327 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
328 rev.diffopt.detect_rename = 1;
329 run_diff_index(&rev, 1);
333 if (dup2(saved_stdout, STDOUT_FILENO) < 0)
334 die("couldn't restore stdout\n");
338 void wt_status_print(struct wt_status *s)
340 unsigned char sha1[20];
341 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
344 const char *on_what = "On branch ";
345 const char *branch_name = s->branch;
346 if (!prefixcmp(branch_name, "refs/heads/"))
348 else if (!strcmp(branch_name, "HEAD")) {
350 on_what = "Not currently on any branch.";
352 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
353 "# %s%s", on_what, branch_name);
357 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
358 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
359 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
360 wt_status_print_initial(s);
363 wt_status_print_updated(s);
366 wt_status_print_changed(s);
367 wt_status_print_untracked(s);
369 if (s->verbose && !s->is_initial)
370 wt_status_print_verbose(s);
371 if (!s->commitable) {
373 fprintf(s->fp, "# No changes\n");
376 else if (s->workdir_dirty)
377 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
378 else if (s->workdir_untracked)
379 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
380 else if (s->is_initial)
381 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
383 printf("nothing to commit (working directory clean)\n");
387 int git_status_config(const char *k, const char *v)
389 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
390 wt_status_use_color = git_config_colorbool(k, v, -1);
393 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
394 int slot = parse_status_slot(k, 13);
396 return config_error_nonbool(k);
397 color_parse(v, k, wt_status_colors[slot]);
400 if (!strcmp(k, "status.relativepaths")) {
401 wt_status_relative_paths = git_config_bool(k, v);
404 return git_color_default_config(k, v);