Merge branch 't/summary/css-metadata' into refs/top-bases/master
[git/gitweb.git] / wt-status.c
blob7cf890f2433bf622fe8c18faa0b0c89be60ef1db
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 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
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 if (!strcasecmp(var+offset, "nobranch"))
39 return WT_STATUS_NOBRANCH;
40 die("bad config variable '%s'", var);
43 static const char* color(int slot)
45 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
48 void wt_status_prepare(struct wt_status *s)
50 unsigned char sha1[20];
51 const char *head;
53 memset(s, 0, sizeof(*s));
54 head = resolve_ref("HEAD", sha1, 0, NULL);
55 s->branch = head ? xstrdup(head) : NULL;
56 s->reference = "HEAD";
57 s->fp = stdout;
58 s->index_file = get_index_file();
61 static void wt_status_print_cached_header(struct wt_status *s)
63 const char *c = color(WT_STATUS_HEADER);
64 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
65 if (!s->is_initial) {
66 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
67 } else {
68 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
70 color_fprintf_ln(s->fp, c, "#");
73 static void wt_status_print_dirty_header(struct wt_status *s,
74 int has_deleted)
76 const char *c = color(WT_STATUS_HEADER);
77 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
78 if (!has_deleted)
79 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
80 else
81 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
82 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
83 color_fprintf_ln(s->fp, c, "#");
86 static void wt_status_print_untracked_header(struct wt_status *s)
88 const char *c = color(WT_STATUS_HEADER);
89 color_fprintf_ln(s->fp, c, "# Untracked files:");
90 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
91 color_fprintf_ln(s->fp, c, "#");
94 static void wt_status_print_trailer(struct wt_status *s)
96 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
99 #define quote_path quote_path_relative
101 static void wt_status_print_filepair(struct wt_status *s,
102 int t, struct diff_filepair *p)
104 const char *c = color(t);
105 const char *one, *two;
106 struct strbuf onebuf, twobuf;
108 strbuf_init(&onebuf, 0);
109 strbuf_init(&twobuf, 0);
110 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
111 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
113 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
114 switch (p->status) {
115 case DIFF_STATUS_ADDED:
116 color_fprintf(s->fp, c, "new file: %s", one);
117 break;
118 case DIFF_STATUS_COPIED:
119 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
120 break;
121 case DIFF_STATUS_DELETED:
122 color_fprintf(s->fp, c, "deleted: %s", one);
123 break;
124 case DIFF_STATUS_MODIFIED:
125 color_fprintf(s->fp, c, "modified: %s", one);
126 break;
127 case DIFF_STATUS_RENAMED:
128 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
129 break;
130 case DIFF_STATUS_TYPE_CHANGED:
131 color_fprintf(s->fp, c, "typechange: %s", one);
132 break;
133 case DIFF_STATUS_UNKNOWN:
134 color_fprintf(s->fp, c, "unknown: %s", one);
135 break;
136 case DIFF_STATUS_UNMERGED:
137 color_fprintf(s->fp, c, "unmerged: %s", one);
138 break;
139 default:
140 die("bug: unhandled diff status %c", p->status);
142 fprintf(s->fp, "\n");
143 strbuf_release(&onebuf);
144 strbuf_release(&twobuf);
147 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
148 struct diff_options *options,
149 void *data)
151 struct wt_status *s = data;
152 int shown_header = 0;
153 int i;
154 for (i = 0; i < q->nr; i++) {
155 if (q->queue[i]->status == 'U')
156 continue;
157 if (!shown_header) {
158 wt_status_print_cached_header(s);
159 s->commitable = 1;
160 shown_header = 1;
162 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
164 if (shown_header)
165 wt_status_print_trailer(s);
168 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
169 struct diff_options *options,
170 void *data)
172 struct wt_status *s = data;
173 int i;
174 if (q->nr) {
175 int has_deleted = 0;
176 s->workdir_dirty = 1;
177 for (i = 0; i < q->nr; i++)
178 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
179 has_deleted = 1;
180 break;
182 wt_status_print_dirty_header(s, has_deleted);
184 for (i = 0; i < q->nr; i++)
185 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
186 if (q->nr)
187 wt_status_print_trailer(s);
190 static void wt_status_print_initial(struct wt_status *s)
192 int i;
193 struct strbuf buf;
195 strbuf_init(&buf, 0);
196 if (active_nr) {
197 s->commitable = 1;
198 wt_status_print_cached_header(s);
200 for (i = 0; i < active_nr; i++) {
201 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
202 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
203 quote_path(active_cache[i]->name, -1,
204 &buf, s->prefix));
206 if (active_nr)
207 wt_status_print_trailer(s);
208 strbuf_release(&buf);
211 static void wt_status_print_updated(struct wt_status *s)
213 struct rev_info rev;
214 init_revisions(&rev, NULL);
215 setup_revisions(0, NULL, &rev, s->reference);
216 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
217 rev.diffopt.format_callback = wt_status_print_updated_cb;
218 rev.diffopt.format_callback_data = s;
219 rev.diffopt.detect_rename = 1;
220 rev.diffopt.rename_limit = 200;
221 rev.diffopt.break_opt = 0;
222 run_diff_index(&rev, 1);
225 static void wt_status_print_changed(struct wt_status *s)
227 struct rev_info rev;
228 init_revisions(&rev, "");
229 setup_revisions(0, NULL, &rev, NULL);
230 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
231 rev.diffopt.format_callback = wt_status_print_changed_cb;
232 rev.diffopt.format_callback_data = s;
233 run_diff_files(&rev, 0);
236 static void wt_status_print_submodule_summary(struct wt_status *s)
238 struct child_process sm_summary;
239 char summary_limit[64];
240 char index[PATH_MAX];
241 const char *env[] = { index, NULL };
242 const char *argv[] = {
243 "submodule",
244 "summary",
245 "--cached",
246 "--for-status",
247 "--summary-limit",
248 summary_limit,
249 s->amend ? "HEAD^" : "HEAD",
250 NULL
253 sprintf(summary_limit, "%d", wt_status_submodule_summary);
254 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
256 memset(&sm_summary, 0, sizeof(sm_summary));
257 sm_summary.argv = argv;
258 sm_summary.env = env;
259 sm_summary.git_cmd = 1;
260 sm_summary.no_stdin = 1;
261 fflush(s->fp);
262 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
263 run_command(&sm_summary);
266 static void wt_status_print_untracked(struct wt_status *s)
268 struct dir_struct dir;
269 int i;
270 int shown_header = 0;
271 struct strbuf buf;
273 strbuf_init(&buf, 0);
274 memset(&dir, 0, sizeof(dir));
276 if (!s->untracked) {
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;
289 if (0 <= pos)
290 die("bug in wt_status_print_untracked");
291 pos = -pos - 1;
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))
296 continue;
298 if (!shown_header) {
299 s->workdir_untracked = 1;
300 wt_status_print_untracked_header(s);
301 shown_header = 1;
303 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
304 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
305 quote_path(ent->name, ent->len,
306 &buf, s->prefix));
308 strbuf_release(&buf);
311 static void wt_status_print_verbose(struct wt_status *s)
313 struct rev_info rev;
315 init_revisions(&rev, NULL);
316 setup_revisions(0, NULL, &rev, s->reference);
317 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
318 rev.diffopt.detect_rename = 1;
319 rev.diffopt.file = s->fp;
320 rev.diffopt.close_file = 0;
321 run_diff_index(&rev, 1);
324 static void wt_status_print_tracking(struct wt_status *s)
326 struct strbuf sb = STRBUF_INIT;
327 const char *cp, *ep;
328 struct branch *branch;
330 assert(s->branch && !s->is_initial);
331 if (prefixcmp(s->branch, "refs/heads/"))
332 return;
333 branch = branch_get(s->branch + 11);
334 if (!format_tracking_info(branch, &sb))
335 return;
337 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
338 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
339 "# %.*s", (int)(ep - cp), cp);
340 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
343 void wt_status_print(struct wt_status *s)
345 unsigned char sha1[20];
346 const char *branch_color = color(WT_STATUS_HEADER);
348 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
349 if (s->branch) {
350 const char *on_what = "On branch ";
351 const char *branch_name = s->branch;
352 if (!prefixcmp(branch_name, "refs/heads/"))
353 branch_name += 11;
354 else if (!strcmp(branch_name, "HEAD")) {
355 branch_name = "";
356 branch_color = color(WT_STATUS_NOBRANCH);
357 on_what = "Not currently on any branch.";
359 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
360 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
361 if (!s->is_initial)
362 wt_status_print_tracking(s);
365 if (s->is_initial) {
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);
371 else {
372 wt_status_print_updated(s);
375 wt_status_print_changed(s);
376 if (wt_status_submodule_summary)
377 wt_status_print_submodule_summary(s);
378 if (show_untracked_files)
379 wt_status_print_untracked(s);
380 else if (s->commitable)
381 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
383 if (s->verbose && !s->is_initial)
384 wt_status_print_verbose(s);
385 if (!s->commitable) {
386 if (s->amend)
387 fprintf(s->fp, "# No changes\n");
388 else if (s->nowarn)
389 ; /* nothing */
390 else if (s->workdir_dirty)
391 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
392 else if (s->workdir_untracked)
393 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
394 else if (s->is_initial)
395 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
396 else if (!show_untracked_files)
397 printf("nothing to commit (use -u to show untracked files)\n");
398 else
399 printf("nothing to commit (working directory clean)\n");
403 int git_status_config(const char *k, const char *v, void *cb)
405 if (!strcmp(k, "status.submodulesummary")) {
406 int is_bool;
407 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
408 if (is_bool && wt_status_submodule_summary)
409 wt_status_submodule_summary = -1;
410 return 0;
412 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
413 wt_status_use_color = git_config_colorbool(k, v, -1);
414 return 0;
416 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
417 int slot = parse_status_slot(k, 13);
418 if (!v)
419 return config_error_nonbool(k);
420 color_parse(v, k, wt_status_colors[slot]);
421 return 0;
423 if (!strcmp(k, "status.relativepaths")) {
424 wt_status_relative_paths = git_config_bool(k, v);
425 return 0;
427 if (!strcmp(k, "status.showuntrackedfiles")) {
428 if (!v)
429 return config_error_nonbool(k);
430 else if (!strcmp(v, "no"))
431 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
432 else if (!strcmp(v, "normal"))
433 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
434 else if (!strcmp(v, "all"))
435 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
436 else
437 return error("Invalid untracked files mode '%s'", v);
438 return 0;
440 return git_color_default_config(k, v, cb);