Merge branch 'jk/maint-ls-files-other' into maint
[git/mingw.git] / wt-status.c
blob64cedfcbe14ab0448ab6225b50244a45492a546a
1 #include "cache.h"
2 #include "wt-status.h"
3 #include "color.h"
4 #include "object.h"
5 #include "dir.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "diffcore.h"
10 #include "quote.h"
11 #include "run-command.h"
12 #include "remote.h"
14 int wt_status_relative_paths = 1;
15 int wt_status_use_color = -1;
16 int wt_status_submodule_summary;
17 static char wt_status_colors[][COLOR_MAXLEN] = {
18 "", /* WT_STATUS_HEADER: normal */
19 "\033[32m", /* WT_STATUS_UPDATED: green */
20 "\033[31m", /* WT_STATUS_CHANGED: red */
21 "\033[31m", /* WT_STATUS_UNTRACKED: red */
22 "\033[31m", /* WT_STATUS_NOBRANCH: red */
25 static const char use_add_msg[] =
26 "use \"git add <file>...\" to update what will be committed";
27 static const char use_add_rm_msg[] =
28 "use \"git add/rm <file>...\" to update what will be committed";
29 static const char use_add_to_include_msg[] =
30 "use \"git add <file>...\" to include in what will be committed";
31 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
33 static int parse_status_slot(const char *var, int offset)
35 if (!strcasecmp(var+offset, "header"))
36 return WT_STATUS_HEADER;
37 if (!strcasecmp(var+offset, "updated")
38 || !strcasecmp(var+offset, "added"))
39 return WT_STATUS_UPDATED;
40 if (!strcasecmp(var+offset, "changed"))
41 return WT_STATUS_CHANGED;
42 if (!strcasecmp(var+offset, "untracked"))
43 return WT_STATUS_UNTRACKED;
44 if (!strcasecmp(var+offset, "nobranch"))
45 return WT_STATUS_NOBRANCH;
46 die("bad config variable '%s'", var);
49 static const char* color(int slot)
51 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
54 void wt_status_prepare(struct wt_status *s)
56 unsigned char sha1[20];
57 const char *head;
59 memset(s, 0, sizeof(*s));
60 head = resolve_ref("HEAD", sha1, 0, NULL);
61 s->branch = head ? xstrdup(head) : NULL;
62 s->reference = "HEAD";
63 s->fp = stdout;
64 s->index_file = get_index_file();
67 static void wt_status_print_cached_header(struct wt_status *s)
69 const char *c = color(WT_STATUS_HEADER);
70 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
71 if (!s->is_initial) {
72 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
73 } else {
74 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
76 color_fprintf_ln(s->fp, c, "#");
79 static void wt_status_print_header(struct wt_status *s,
80 const char *main, const char *sub)
82 const char *c = color(WT_STATUS_HEADER);
83 color_fprintf_ln(s->fp, c, "# %s:", main);
84 color_fprintf_ln(s->fp, c, "# (%s)", sub);
85 color_fprintf_ln(s->fp, c, "#");
88 static void wt_status_print_trailer(struct wt_status *s)
90 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
93 #define quote_path quote_path_relative
95 static void wt_status_print_filepair(struct wt_status *s,
96 int t, struct diff_filepair *p)
98 const char *c = color(t);
99 const char *one, *two;
100 struct strbuf onebuf, twobuf;
102 strbuf_init(&onebuf, 0);
103 strbuf_init(&twobuf, 0);
104 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
105 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
107 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
108 switch (p->status) {
109 case DIFF_STATUS_ADDED:
110 color_fprintf(s->fp, c, "new file: %s", one);
111 break;
112 case DIFF_STATUS_COPIED:
113 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
114 break;
115 case DIFF_STATUS_DELETED:
116 color_fprintf(s->fp, c, "deleted: %s", one);
117 break;
118 case DIFF_STATUS_MODIFIED:
119 color_fprintf(s->fp, c, "modified: %s", one);
120 break;
121 case DIFF_STATUS_RENAMED:
122 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
123 break;
124 case DIFF_STATUS_TYPE_CHANGED:
125 color_fprintf(s->fp, c, "typechange: %s", one);
126 break;
127 case DIFF_STATUS_UNKNOWN:
128 color_fprintf(s->fp, c, "unknown: %s", one);
129 break;
130 case DIFF_STATUS_UNMERGED:
131 color_fprintf(s->fp, c, "unmerged: %s", one);
132 break;
133 default:
134 die("bug: unhandled diff status %c", p->status);
136 fprintf(s->fp, "\n");
137 strbuf_release(&onebuf);
138 strbuf_release(&twobuf);
141 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
142 struct diff_options *options,
143 void *data)
145 struct wt_status *s = data;
146 int shown_header = 0;
147 int i;
148 for (i = 0; i < q->nr; i++) {
149 if (q->queue[i]->status == 'U')
150 continue;
151 if (!shown_header) {
152 wt_status_print_cached_header(s);
153 s->commitable = 1;
154 shown_header = 1;
156 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
158 if (shown_header)
159 wt_status_print_trailer(s);
162 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
163 struct diff_options *options,
164 void *data)
166 struct wt_status *s = data;
167 int i;
168 if (q->nr) {
169 const char *msg = use_add_msg;
170 s->workdir_dirty = 1;
171 for (i = 0; i < q->nr; i++)
172 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
173 msg = use_add_rm_msg;
174 break;
176 wt_status_print_header(s, "Changed but not updated", msg);
178 for (i = 0; i < q->nr; i++)
179 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
180 if (q->nr)
181 wt_status_print_trailer(s);
184 static void wt_status_print_initial(struct wt_status *s)
186 int i;
187 struct strbuf buf;
189 strbuf_init(&buf, 0);
190 if (active_nr) {
191 s->commitable = 1;
192 wt_status_print_cached_header(s);
194 for (i = 0; i < active_nr; i++) {
195 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
196 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
197 quote_path(active_cache[i]->name, -1,
198 &buf, s->prefix));
200 if (active_nr)
201 wt_status_print_trailer(s);
202 strbuf_release(&buf);
205 static void wt_status_print_updated(struct wt_status *s)
207 struct rev_info rev;
208 init_revisions(&rev, NULL);
209 setup_revisions(0, NULL, &rev, s->reference);
210 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
211 rev.diffopt.format_callback = wt_status_print_updated_cb;
212 rev.diffopt.format_callback_data = s;
213 rev.diffopt.detect_rename = 1;
214 rev.diffopt.rename_limit = 200;
215 rev.diffopt.break_opt = 0;
216 run_diff_index(&rev, 1);
219 static void wt_status_print_changed(struct wt_status *s)
221 struct rev_info rev;
222 init_revisions(&rev, "");
223 setup_revisions(0, NULL, &rev, NULL);
224 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
225 rev.diffopt.format_callback = wt_status_print_changed_cb;
226 rev.diffopt.format_callback_data = s;
227 run_diff_files(&rev, 0);
230 static void wt_status_print_submodule_summary(struct wt_status *s)
232 struct child_process sm_summary;
233 char summary_limit[64];
234 char index[PATH_MAX];
235 const char *env[] = { index, NULL };
236 const char *argv[] = {
237 "submodule",
238 "summary",
239 "--cached",
240 "--for-status",
241 "--summary-limit",
242 summary_limit,
243 s->amend ? "HEAD^" : "HEAD",
244 NULL
247 sprintf(summary_limit, "%d", wt_status_submodule_summary);
248 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
250 memset(&sm_summary, 0, sizeof(sm_summary));
251 sm_summary.argv = argv;
252 sm_summary.env = env;
253 sm_summary.git_cmd = 1;
254 sm_summary.no_stdin = 1;
255 fflush(s->fp);
256 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
257 run_command(&sm_summary);
260 static void wt_status_print_untracked(struct wt_status *s)
262 struct dir_struct dir;
263 int i;
264 int shown_header = 0;
265 struct strbuf buf;
267 strbuf_init(&buf, 0);
268 memset(&dir, 0, sizeof(dir));
270 if (!s->untracked) {
271 dir.show_other_directories = 1;
272 dir.hide_empty_directories = 1;
274 setup_standard_excludes(&dir);
276 read_directory(&dir, ".", "", 0, NULL);
277 for(i = 0; i < dir.nr; i++) {
278 struct dir_entry *ent = dir.entries[i];
279 if (!cache_name_is_other(ent->name, ent->len))
280 continue;
281 if (!shown_header) {
282 s->workdir_untracked = 1;
283 wt_status_print_header(s, "Untracked files",
284 use_add_to_include_msg);
285 shown_header = 1;
287 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
288 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
289 quote_path(ent->name, ent->len,
290 &buf, s->prefix));
292 strbuf_release(&buf);
295 static void wt_status_print_verbose(struct wt_status *s)
297 struct rev_info rev;
299 init_revisions(&rev, NULL);
300 setup_revisions(0, NULL, &rev, s->reference);
301 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
302 rev.diffopt.detect_rename = 1;
303 rev.diffopt.file = s->fp;
304 rev.diffopt.close_file = 0;
305 run_diff_index(&rev, 1);
308 static void wt_status_print_tracking(struct wt_status *s)
310 struct strbuf sb = STRBUF_INIT;
311 const char *cp, *ep;
312 struct branch *branch;
314 assert(s->branch && !s->is_initial);
315 if (prefixcmp(s->branch, "refs/heads/"))
316 return;
317 branch = branch_get(s->branch + 11);
318 if (!format_tracking_info(branch, &sb))
319 return;
321 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
322 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
323 "# %.*s", (int)(ep - cp), cp);
324 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
327 void wt_status_print(struct wt_status *s)
329 unsigned char sha1[20];
330 const char *branch_color = color(WT_STATUS_HEADER);
332 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
333 if (s->branch) {
334 const char *on_what = "On branch ";
335 const char *branch_name = s->branch;
336 if (!prefixcmp(branch_name, "refs/heads/"))
337 branch_name += 11;
338 else if (!strcmp(branch_name, "HEAD")) {
339 branch_name = "";
340 branch_color = color(WT_STATUS_NOBRANCH);
341 on_what = "Not currently on any branch.";
343 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
344 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
345 if (!s->is_initial)
346 wt_status_print_tracking(s);
349 if (s->is_initial) {
350 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
351 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
352 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
353 wt_status_print_initial(s);
355 else {
356 wt_status_print_updated(s);
359 wt_status_print_changed(s);
360 if (wt_status_submodule_summary)
361 wt_status_print_submodule_summary(s);
362 if (show_untracked_files)
363 wt_status_print_untracked(s);
364 else if (s->commitable)
365 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
367 if (s->verbose && !s->is_initial)
368 wt_status_print_verbose(s);
369 if (!s->commitable) {
370 if (s->amend)
371 fprintf(s->fp, "# No changes\n");
372 else if (s->nowarn)
373 ; /* nothing */
374 else if (s->workdir_dirty)
375 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
376 else if (s->workdir_untracked)
377 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
378 else if (s->is_initial)
379 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
380 else if (!show_untracked_files)
381 printf("nothing to commit (use -u to show untracked files)\n");
382 else
383 printf("nothing to commit (working directory clean)\n");
387 int git_status_config(const char *k, const char *v, void *cb)
389 if (!strcmp(k, "status.submodulesummary")) {
390 int is_bool;
391 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
392 if (is_bool && wt_status_submodule_summary)
393 wt_status_submodule_summary = -1;
394 return 0;
396 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
397 wt_status_use_color = git_config_colorbool(k, v, -1);
398 return 0;
400 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
401 int slot = parse_status_slot(k, 13);
402 if (!v)
403 return config_error_nonbool(k);
404 color_parse(v, k, wt_status_colors[slot]);
405 return 0;
407 if (!strcmp(k, "status.relativepaths")) {
408 wt_status_relative_paths = git_config_bool(k, v);
409 return 0;
411 if (!strcmp(k, "status.showuntrackedfiles")) {
412 if (!v)
413 return config_error_nonbool(k);
414 else if (!strcmp(v, "no"))
415 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
416 else if (!strcmp(v, "normal"))
417 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
418 else if (!strcmp(v, "all"))
419 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
420 else
421 return error("Invalid untracked files mode '%s'", v);
422 return 0;
424 return git_color_default_config(k, v, cb);