wt-status.c: rework the way changes to the index and work tree are summarized
[git/git.git] / wt-status.c
CommitLineData
85023577 1#include "cache.h"
c91f0d92
JK
2#include "wt-status.h"
3#include "color.h"
c91f0d92
JK
4#include "object.h"
5#include "dir.h"
6#include "commit.h"
7#include "diff.h"
8#include "revision.h"
9#include "diffcore.h"
a734d0b1 10#include "quote.h"
ac8d5afc 11#include "run-command.h"
b6975ab5 12#include "remote.h"
c91f0d92 13
46f721c8 14int wt_status_relative_paths = 1;
6b2f2d98 15int wt_status_use_color = -1;
2af202be 16static int wt_status_submodule_summary;
c91f0d92 17static char wt_status_colors[][COLOR_MAXLEN] = {
dc6ebd4c
AL
18 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
c91f0d92 23};
4d229653 24
4bfee30a 25enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
c91f0d92
JK
26
27static int parse_status_slot(const char *var, int offset)
28{
29 if (!strcasecmp(var+offset, "header"))
30 return WT_STATUS_HEADER;
82dca848
SP
31 if (!strcasecmp(var+offset, "updated")
32 || !strcasecmp(var+offset, "added"))
c91f0d92
JK
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;
950ce2e2
CP
38 if (!strcasecmp(var+offset, "nobranch"))
39 return WT_STATUS_NOBRANCH;
c91f0d92
JK
40 die("bad config variable '%s'", var);
41}
42
4b25d091 43static const char *color(int slot)
c91f0d92 44{
6b2f2d98 45 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
c91f0d92
JK
46}
47
48void wt_status_prepare(struct wt_status *s)
49{
50 unsigned char sha1[20];
51 const char *head;
52
cc46a743 53 memset(s, 0, sizeof(*s));
8da19775 54 head = resolve_ref("HEAD", sha1, 0, NULL);
f62363fb 55 s->branch = head ? xstrdup(head) : NULL;
c91f0d92 56 s->reference = "HEAD";
f26a0012 57 s->fp = stdout;
0f729f21 58 s->index_file = get_index_file();
50b7e70f 59 s->change.strdup_strings = 1;
c91f0d92
JK
60}
61
f26a0012 62static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb
JR
63{
64 const char *c = color(WT_STATUS_HEADER);
f26a0012 65 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
ff58b9aa 66 if (!s->is_initial) {
f26a0012 67 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
3c1eb9cb 68 } else {
f26a0012 69 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
3c1eb9cb 70 }
f26a0012 71 color_fprintf_ln(s->fp, c, "#");
3c1eb9cb
JR
72}
73
bb914b14
AM
74static void wt_status_print_dirty_header(struct wt_status *s,
75 int has_deleted)
c91f0d92
JK
76{
77 const char *c = color(WT_STATUS_HEADER);
bb914b14
AM
78 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
79 if (!has_deleted)
80 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
81 else
82 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
4d6e4c4d 83 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
bb914b14
AM
84 color_fprintf_ln(s->fp, c, "#");
85}
86
87static void wt_status_print_untracked_header(struct wt_status *s)
88{
89 const char *c = color(WT_STATUS_HEADER);
90 color_fprintf_ln(s->fp, c, "# Untracked files:");
91 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
f26a0012 92 color_fprintf_ln(s->fp, c, "#");
c91f0d92
JK
93}
94
f26a0012 95static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 96{
f26a0012 97 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
98}
99
a734d0b1 100#define quote_path quote_path_relative
3a946802 101
50b7e70f
JH
102static void wt_status_print_change_data(struct wt_status *s,
103 int change_type,
104 struct string_list_item *it)
c91f0d92 105{
50b7e70f
JH
106 struct wt_status_change_data *d = it->util;
107 const char *c = color(change_type);
108 int status = status;
109 char *one_name;
110 char *two_name;
3a946802 111 const char *one, *two;
f285a2d7 112 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
3a946802 113
50b7e70f
JH
114 one_name = two_name = it->string;
115 switch (change_type) {
116 case WT_STATUS_UPDATED:
117 status = d->index_status;
118 if (d->head_path)
119 one_name = d->head_path;
120 break;
121 case WT_STATUS_CHANGED:
122 status = d->worktree_status;
123 break;
124 }
125
126 one = quote_path(one_name, -1, &onebuf, s->prefix);
127 two = quote_path(two_name, -1, &twobuf, s->prefix);
3a946802 128
f26a0012 129 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
50b7e70f 130 switch (status) {
c91f0d92 131 case DIFF_STATUS_ADDED:
f26a0012 132 color_fprintf(s->fp, c, "new file: %s", one);
3a946802 133 break;
c91f0d92 134 case DIFF_STATUS_COPIED:
f26a0012 135 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
c91f0d92
JK
136 break;
137 case DIFF_STATUS_DELETED:
f26a0012 138 color_fprintf(s->fp, c, "deleted: %s", one);
3a946802 139 break;
c91f0d92 140 case DIFF_STATUS_MODIFIED:
f26a0012 141 color_fprintf(s->fp, c, "modified: %s", one);
3a946802 142 break;
c91f0d92 143 case DIFF_STATUS_RENAMED:
f26a0012 144 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
c91f0d92
JK
145 break;
146 case DIFF_STATUS_TYPE_CHANGED:
f26a0012 147 color_fprintf(s->fp, c, "typechange: %s", one);
3a946802 148 break;
c91f0d92 149 case DIFF_STATUS_UNKNOWN:
f26a0012 150 color_fprintf(s->fp, c, "unknown: %s", one);
3a946802 151 break;
c91f0d92 152 case DIFF_STATUS_UNMERGED:
f26a0012 153 color_fprintf(s->fp, c, "unmerged: %s", one);
3a946802 154 break;
c91f0d92 155 default:
50b7e70f 156 die("bug: unhandled diff status %c", status);
c91f0d92 157 }
f26a0012 158 fprintf(s->fp, "\n");
367c9886
JS
159 strbuf_release(&onebuf);
160 strbuf_release(&twobuf);
c91f0d92
JK
161}
162
50b7e70f
JH
163static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
164 struct diff_options *options,
165 void *data)
c91f0d92
JK
166{
167 struct wt_status *s = data;
c91f0d92 168 int i;
50b7e70f
JH
169
170 if (!q->nr)
171 return;
172 s->workdir_dirty = 1;
c91f0d92 173 for (i = 0; i < q->nr; i++) {
50b7e70f
JH
174 struct diff_filepair *p;
175 struct string_list_item *it;
176 struct wt_status_change_data *d;
177
178 p = q->queue[i];
179 it = string_list_insert(p->one->path, &s->change);
180 d = it->util;
181 if (!d) {
182 d = xcalloc(1, sizeof(*d));
183 it->util = d;
c91f0d92 184 }
50b7e70f
JH
185 if (!d->worktree_status)
186 d->worktree_status = p->status;
c91f0d92 187 }
c91f0d92
JK
188}
189
50b7e70f
JH
190static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
191 struct diff_options *options,
192 void *data)
c91f0d92 193{
6e458bf6 194 struct wt_status *s = data;
c91f0d92 195 int i;
50b7e70f
JH
196
197 for (i = 0; i < q->nr; i++) {
198 struct diff_filepair *p;
199 struct string_list_item *it;
200 struct wt_status_change_data *d;
201
202 p = q->queue[i];
203 it = string_list_insert(p->two->path, &s->change);
204 d = it->util;
205 if (!d) {
206 d = xcalloc(1, sizeof(*d));
207 it->util = d;
208 }
209 if (!d->index_status)
210 d->index_status = p->status;
211 switch (p->status) {
212 case DIFF_STATUS_COPIED:
213 case DIFF_STATUS_RENAMED:
214 d->head_path = xstrdup(p->one->path);
215 break;
216 }
6e458bf6 217 }
c91f0d92
JK
218}
219
50b7e70f 220static void wt_status_collect_changes_worktree(struct wt_status *s)
c91f0d92
JK
221{
222 struct rev_info rev;
50b7e70f
JH
223
224 init_revisions(&rev, NULL);
225 setup_revisions(0, NULL, &rev, NULL);
226 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
227 rev.diffopt.format_callback = wt_status_collect_changed_cb;
228 rev.diffopt.format_callback_data = s;
229 run_diff_files(&rev, 0);
230}
231
232static void wt_status_collect_changes_index(struct wt_status *s)
233{
234 struct rev_info rev;
235
c91f0d92 236 init_revisions(&rev, NULL);
c1e255b7
JK
237 setup_revisions(0, NULL, &rev,
238 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
c91f0d92 239 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
50b7e70f 240 rev.diffopt.format_callback = wt_status_collect_updated_cb;
c91f0d92
JK
241 rev.diffopt.format_callback_data = s;
242 rev.diffopt.detect_rename = 1;
50705915 243 rev.diffopt.rename_limit = 200;
f714fb84 244 rev.diffopt.break_opt = 0;
c91f0d92
JK
245 run_diff_index(&rev, 1);
246}
247
50b7e70f
JH
248static void wt_status_collect_changes_initial(struct wt_status *s)
249{
250 int i;
251
252 for (i = 0; i < active_nr; i++) {
253 struct string_list_item *it;
254 struct wt_status_change_data *d;
255 struct cache_entry *ce = active_cache[i];
256
257 it = string_list_insert(ce->name, &s->change);
258 d = it->util;
259 if (!d) {
260 d = xcalloc(1, sizeof(*d));
261 it->util = d;
262 }
263 if (ce_stage(ce))
264 d->index_status = DIFF_STATUS_UNMERGED;
265 else
266 d->index_status = DIFF_STATUS_ADDED;
267 }
268}
269
270void wt_status_collect_changes(struct wt_status *s)
271{
272 wt_status_collect_changes_worktree(s);
273
274 if (s->is_initial)
275 wt_status_collect_changes_initial(s);
276 else
277 wt_status_collect_changes_index(s);
278}
279
280static void wt_status_print_updated(struct wt_status *s)
281{
282 int shown_header = 0;
283 int i;
284
285 for (i = 0; i < s->change.nr; i++) {
286 struct wt_status_change_data *d;
287 struct string_list_item *it;
288 it = &(s->change.items[i]);
289 d = it->util;
290 if (!d->index_status ||
291 d->index_status == DIFF_STATUS_UNMERGED)
292 continue;
293 if (!shown_header) {
294 wt_status_print_cached_header(s);
295 s->commitable = 1;
296 shown_header = 1;
297 }
298 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
299 }
300 if (shown_header)
301 wt_status_print_trailer(s);
302}
303
304/*
305 * -1 : has delete
306 * 0 : no change
307 * 1 : some change but no delete
308 */
309static int wt_status_check_worktree_changes(struct wt_status *s)
310{
311 int i;
312 int changes = 0;
313
314 for (i = 0; i < s->change.nr; i++) {
315 struct wt_status_change_data *d;
316 d = s->change.items[i].util;
317 if (!d->worktree_status)
318 continue;
319 changes = 1;
320 if (d->worktree_status == DIFF_STATUS_DELETED)
321 return -1;
322 }
323 return changes;
324}
325
c91f0d92
JK
326static void wt_status_print_changed(struct wt_status *s)
327{
50b7e70f
JH
328 int i;
329 int worktree_changes = wt_status_check_worktree_changes(s);
330
331 if (!worktree_changes)
332 return;
333
334 wt_status_print_dirty_header(s, worktree_changes < 0);
335
336 for (i = 0; i < s->change.nr; i++) {
337 struct wt_status_change_data *d;
338 struct string_list_item *it;
339 it = &(s->change.items[i]);
340 d = it->util;
341 if (!d->worktree_status)
342 continue;
343 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
344 }
345 wt_status_print_trailer(s);
c91f0d92
JK
346}
347
ac8d5afc
PY
348static void wt_status_print_submodule_summary(struct wt_status *s)
349{
350 struct child_process sm_summary;
351 char summary_limit[64];
352 char index[PATH_MAX];
353 const char *env[] = { index, NULL };
354 const char *argv[] = {
355 "submodule",
356 "summary",
357 "--cached",
358 "--for-status",
359 "--summary-limit",
360 summary_limit,
361 s->amend ? "HEAD^" : "HEAD",
362 NULL
363 };
364
365 sprintf(summary_limit, "%d", wt_status_submodule_summary);
366 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
367
368 memset(&sm_summary, 0, sizeof(sm_summary));
369 sm_summary.argv = argv;
370 sm_summary.env = env;
371 sm_summary.git_cmd = 1;
372 sm_summary.no_stdin = 1;
373 fflush(s->fp);
374 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
375 run_command(&sm_summary);
376}
377
6e458bf6 378static void wt_status_print_untracked(struct wt_status *s)
c91f0d92
JK
379{
380 struct dir_struct dir;
c91f0d92
JK
381 int i;
382 int shown_header = 0;
f285a2d7 383 struct strbuf buf = STRBUF_INIT;
c91f0d92
JK
384
385 memset(&dir, 0, sizeof(dir));
386
7c4c97c0
JS
387 if (!s->untracked)
388 dir.flags |=
389 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
039bc64e 390 setup_standard_excludes(&dir);
c91f0d92 391
1d8842d9 392 fill_directory(&dir, NULL);
c91f0d92 393 for(i = 0; i < dir.nr; i++) {
c91f0d92 394 struct dir_entry *ent = dir.entries[i];
98fa4738
JK
395 if (!cache_name_is_other(ent->name, ent->len))
396 continue;
c91f0d92 397 if (!shown_header) {
2a3a3c24 398 s->workdir_untracked = 1;
bb914b14 399 wt_status_print_untracked_header(s);
c91f0d92
JK
400 shown_header = 1;
401 }
f26a0012 402 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
367c9886
JS
403 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
404 quote_path(ent->name, ent->len,
405 &buf, s->prefix));
c91f0d92 406 }
367c9886 407 strbuf_release(&buf);
c91f0d92
JK
408}
409
410static void wt_status_print_verbose(struct wt_status *s)
411{
412 struct rev_info rev;
99a12694 413
c91f0d92 414 init_revisions(&rev, NULL);
5ec11af6 415 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
1324fb6f
JK
416 setup_revisions(0, NULL, &rev,
417 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
c91f0d92
JK
418 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
419 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
420 rev.diffopt.file = s->fp;
421 rev.diffopt.close_file = 0;
4f672ad6
JK
422 /*
423 * If we're not going to stdout, then we definitely don't
424 * want color, since we are going to the commit message
425 * file (and even the "auto" setting won't work, since it
426 * will have checked isatty on stdout).
427 */
428 if (s->fp != stdout)
429 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
c91f0d92
JK
430 run_diff_index(&rev, 1);
431}
432
b6975ab5
JH
433static void wt_status_print_tracking(struct wt_status *s)
434{
435 struct strbuf sb = STRBUF_INIT;
436 const char *cp, *ep;
437 struct branch *branch;
438
439 assert(s->branch && !s->is_initial);
440 if (prefixcmp(s->branch, "refs/heads/"))
441 return;
442 branch = branch_get(s->branch + 11);
443 if (!format_tracking_info(branch, &sb))
444 return;
445
446 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
447 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
448 "# %.*s", (int)(ep - cp), cp);
449 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
450}
451
c91f0d92
JK
452void wt_status_print(struct wt_status *s)
453{
98bf8a47 454 unsigned char sha1[20];
950ce2e2 455 const char *branch_color = color(WT_STATUS_HEADER);
98bf8a47 456
950ce2e2 457 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
bda324cf
JH
458 if (s->branch) {
459 const char *on_what = "On branch ";
460 const char *branch_name = s->branch;
cc44c765 461 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
462 branch_name += 11;
463 else if (!strcmp(branch_name, "HEAD")) {
464 branch_name = "";
950ce2e2 465 branch_color = color(WT_STATUS_NOBRANCH);
bda324cf
JH
466 on_what = "Not currently on any branch.";
467 }
950ce2e2
CP
468 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
469 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
b6975ab5
JH
470 if (!s->is_initial)
471 wt_status_print_tracking(s);
bda324cf 472 }
c91f0d92 473
50b7e70f
JH
474 wt_status_collect_changes(s);
475
c91f0d92 476 if (s->is_initial) {
f26a0012
KH
477 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
478 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
479 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
480 }
481
c1e255b7 482 wt_status_print_updated(s);
c91f0d92 483 wt_status_print_changed(s);
ac8d5afc
PY
484 if (wt_status_submodule_summary)
485 wt_status_print_submodule_summary(s);
6c2ce048
MSO
486 if (show_untracked_files)
487 wt_status_print_untracked(s);
488 else if (s->commitable)
489 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
c91f0d92 490
1324fb6f 491 if (s->verbose)
c91f0d92 492 wt_status_print_verbose(s);
6e458bf6
JR
493 if (!s->commitable) {
494 if (s->amend)
f26a0012 495 fprintf(s->fp, "# No changes\n");
37d07f8f
JH
496 else if (s->nowarn)
497 ; /* nothing */
2a3a3c24 498 else if (s->workdir_dirty)
dcbc7bbe 499 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
2a3a3c24
JR
500 else if (s->workdir_untracked)
501 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
502 else if (s->is_initial)
503 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
6c2ce048
MSO
504 else if (!show_untracked_files)
505 printf("nothing to commit (use -u to show untracked files)\n");
2a3a3c24
JR
506 else
507 printf("nothing to commit (working directory clean)\n");
6e458bf6 508 }
c91f0d92
JK
509}
510
ef90d6d4 511int git_status_config(const char *k, const char *v, void *cb)
c91f0d92 512{
ac8d5afc
PY
513 if (!strcmp(k, "status.submodulesummary")) {
514 int is_bool;
515 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
516 if (is_bool && wt_status_submodule_summary)
517 wt_status_submodule_summary = -1;
518 return 0;
519 }
a159ca0c 520 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
0f6f5a40 521 wt_status_use_color = git_config_colorbool(k, v, -1);
c91f0d92
JK
522 return 0;
523 }
1968d77d 524 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
c91f0d92 525 int slot = parse_status_slot(k, 13);
451d36bb
JH
526 if (!v)
527 return config_error_nonbool(k);
c91f0d92 528 color_parse(v, k, wt_status_colors[slot]);
46f721c8
JK
529 return 0;
530 }
531 if (!strcmp(k, "status.relativepaths")) {
532 wt_status_relative_paths = git_config_bool(k, v);
533 return 0;
c91f0d92 534 }
d6293d1f
MSO
535 if (!strcmp(k, "status.showuntrackedfiles")) {
536 if (!v)
c96a6d36 537 return config_error_nonbool(k);
d6293d1f
MSO
538 else if (!strcmp(v, "no"))
539 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
540 else if (!strcmp(v, "normal"))
541 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
542 else if (!strcmp(v, "all"))
543 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
544 else
545 return error("Invalid untracked files mode '%s'", v);
546 return 0;
547 }
4f672ad6 548 return git_diff_ui_config(k, v, cb);
c91f0d92 549}