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 const char *quote_crlf(const char *in, char *buf, size_t sz)
90 for (scan = in, out = buf; *scan; scan++) {
113 static void wt_status_print_filepair(struct wt_status *s,
114 int t, struct diff_filepair *p)
116 const char *c = color(t);
117 const char *one, *two;
118 char onebuf[PATH_MAX], twobuf[PATH_MAX];
120 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
121 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
123 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
125 case DIFF_STATUS_ADDED:
126 color_fprintf(s->fp, c, "new file: %s", one);
128 case DIFF_STATUS_COPIED:
129 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
131 case DIFF_STATUS_DELETED:
132 color_fprintf(s->fp, c, "deleted: %s", one);
134 case DIFF_STATUS_MODIFIED:
135 color_fprintf(s->fp, c, "modified: %s", one);
137 case DIFF_STATUS_RENAMED:
138 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
140 case DIFF_STATUS_TYPE_CHANGED:
141 color_fprintf(s->fp, c, "typechange: %s", one);
143 case DIFF_STATUS_UNKNOWN:
144 color_fprintf(s->fp, c, "unknown: %s", one);
146 case DIFF_STATUS_UNMERGED:
147 color_fprintf(s->fp, c, "unmerged: %s", one);
150 die("bug: unhandled diff status %c", p->status);
152 fprintf(s->fp, "\n");
155 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
156 struct diff_options *options,
159 struct wt_status *s = data;
160 int shown_header = 0;
162 for (i = 0; i < q->nr; i++) {
163 if (q->queue[i]->status == 'U')
166 wt_status_print_cached_header(s);
170 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
173 wt_status_print_trailer(s);
176 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
177 struct diff_options *options,
180 struct wt_status *s = data;
183 const char *msg = use_add_msg;
184 s->workdir_dirty = 1;
185 for (i = 0; i < q->nr; i++)
186 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
187 msg = use_add_rm_msg;
190 wt_status_print_header(s, "Changed but not updated", msg);
192 for (i = 0; i < q->nr; i++)
193 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
195 wt_status_print_trailer(s);
198 static void wt_read_cache(struct wt_status *s)
201 read_cache_from(s->index_file);
204 static void wt_status_print_initial(struct wt_status *s)
212 wt_status_print_cached_header(s);
214 for (i = 0; i < active_nr; i++) {
215 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
216 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
217 quote_crlf(active_cache[i]->name,
221 wt_status_print_trailer(s);
224 static void wt_status_print_updated(struct wt_status *s)
227 init_revisions(&rev, NULL);
228 setup_revisions(0, NULL, &rev, s->reference);
229 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
230 rev.diffopt.format_callback = wt_status_print_updated_cb;
231 rev.diffopt.format_callback_data = s;
232 rev.diffopt.detect_rename = 1;
233 rev.diffopt.rename_limit = 100;
234 rev.diffopt.break_opt = 0;
236 run_diff_index(&rev, 1);
239 static void wt_status_print_changed(struct wt_status *s)
242 init_revisions(&rev, "");
243 setup_revisions(0, NULL, &rev, NULL);
244 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
245 rev.diffopt.format_callback = wt_status_print_changed_cb;
246 rev.diffopt.format_callback_data = s;
248 run_diff_files(&rev, 0);
251 static void wt_status_print_untracked(struct wt_status *s)
253 struct dir_struct dir;
255 int shown_header = 0;
257 memset(&dir, 0, sizeof(dir));
260 dir.show_other_directories = 1;
261 dir.hide_empty_directories = 1;
263 setup_standard_excludes(&dir);
265 read_directory(&dir, ".", "", 0, NULL);
266 for(i = 0; i < dir.nr; i++) {
267 /* check for matching entry, which is unmerged; lifted from
268 * builtin-ls-files:show_other_files */
269 struct dir_entry *ent = dir.entries[i];
270 int pos = cache_name_pos(ent->name, ent->len);
271 struct cache_entry *ce;
273 die("bug in wt_status_print_untracked");
275 if (pos < active_nr) {
276 ce = active_cache[pos];
277 if (ce_namelen(ce) == ent->len &&
278 !memcmp(ce->name, ent->name, ent->len))
282 s->workdir_untracked = 1;
283 wt_status_print_header(s, "Untracked files",
284 use_add_to_include_msg);
287 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
288 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%.*s",
289 ent->len, ent->name);
293 static void wt_status_print_verbose(struct wt_status *s)
296 init_revisions(&rev, NULL);
297 setup_revisions(0, NULL, &rev, s->reference);
298 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
299 rev.diffopt.detect_rename = 1;
301 run_diff_index(&rev, 1);
304 void wt_status_print(struct wt_status *s)
306 unsigned char sha1[20];
307 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
310 const char *on_what = "On branch ";
311 const char *branch_name = s->branch;
312 if (!prefixcmp(branch_name, "refs/heads/"))
314 else if (!strcmp(branch_name, "HEAD")) {
316 on_what = "Not currently on any branch.";
318 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
319 "# %s%s", on_what, branch_name);
323 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
324 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
325 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
326 wt_status_print_initial(s);
329 wt_status_print_updated(s);
332 wt_status_print_changed(s);
333 wt_status_print_untracked(s);
335 if (s->verbose && !s->is_initial)
336 wt_status_print_verbose(s);
337 if (!s->commitable) {
339 fprintf(s->fp, "# No changes\n");
340 else if (s->workdir_dirty)
341 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
342 else if (s->workdir_untracked)
343 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
344 else if (s->is_initial)
345 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
347 printf("nothing to commit (working directory clean)\n");
351 int git_status_config(const char *k, const char *v)
353 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
354 wt_status_use_color = git_config_colorbool(k, v);
357 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
358 int slot = parse_status_slot(k, 13);
359 color_parse(v, k, wt_status_colors[slot]);
361 return git_default_config(k, v);