wt-status.c: rework the way changes to the index and work tree are summarized
[git] / wt-status.c
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"
13
14 int wt_status_relative_paths = 1;
15 int wt_status_use_color = -1;
16 static int wt_status_submodule_summary;
17 static char wt_status_colors[][COLOR_MAXLEN] = {
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 */
23 };
24
25 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
26
27 static int parse_status_slot(const char *var, int offset)
28 {
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);
41 }
42
43 static const char *color(int slot)
44 {
45         return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
46 }
47
48 void wt_status_prepare(struct wt_status *s)
49 {
50         unsigned char sha1[20];
51         const char *head;
52
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();
59         s->change.strdup_strings = 1;
60 }
61
62 static void wt_status_print_cached_header(struct wt_status *s)
63 {
64         const char *c = color(WT_STATUS_HEADER);
65         color_fprintf_ln(s->fp, c, "# Changes to be committed:");
66         if (!s->is_initial) {
67                 color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
68         } else {
69                 color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
70         }
71         color_fprintf_ln(s->fp, c, "#");
72 }
73
74 static void wt_status_print_dirty_header(struct wt_status *s,
75                                          int has_deleted)
76 {
77         const char *c = color(WT_STATUS_HEADER);
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)");
83         color_fprintf_ln(s->fp, c, "#   (use \"git checkout -- <file>...\" to discard changes in working directory)");
84         color_fprintf_ln(s->fp, c, "#");
85 }
86
87 static 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)");
92         color_fprintf_ln(s->fp, c, "#");
93 }
94
95 static void wt_status_print_trailer(struct wt_status *s)
96 {
97         color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
98 }
99
100 #define quote_path quote_path_relative
101
102 static void wt_status_print_change_data(struct wt_status *s,
103                                         int change_type,
104                                         struct string_list_item *it)
105 {
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;
111         const char *one, *two;
112         struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
113
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);
128
129         color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
130         switch (status) {
131         case DIFF_STATUS_ADDED:
132                 color_fprintf(s->fp, c, "new file:   %s", one);
133                 break;
134         case DIFF_STATUS_COPIED:
135                 color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
136                 break;
137         case DIFF_STATUS_DELETED:
138                 color_fprintf(s->fp, c, "deleted:    %s", one);
139                 break;
140         case DIFF_STATUS_MODIFIED:
141                 color_fprintf(s->fp, c, "modified:   %s", one);
142                 break;
143         case DIFF_STATUS_RENAMED:
144                 color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
145                 break;
146         case DIFF_STATUS_TYPE_CHANGED:
147                 color_fprintf(s->fp, c, "typechange: %s", one);
148                 break;
149         case DIFF_STATUS_UNKNOWN:
150                 color_fprintf(s->fp, c, "unknown:    %s", one);
151                 break;
152         case DIFF_STATUS_UNMERGED:
153                 color_fprintf(s->fp, c, "unmerged:   %s", one);
154                 break;
155         default:
156                 die("bug: unhandled diff status %c", status);
157         }
158         fprintf(s->fp, "\n");
159         strbuf_release(&onebuf);
160         strbuf_release(&twobuf);
161 }
162
163 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
164                                          struct diff_options *options,
165                                          void *data)
166 {
167         struct wt_status *s = data;
168         int i;
169
170         if (!q->nr)
171                 return;
172         s->workdir_dirty = 1;
173         for (i = 0; i < q->nr; i++) {
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;
184                 }
185                 if (!d->worktree_status)
186                         d->worktree_status = p->status;
187         }
188 }
189
190 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
191                                          struct diff_options *options,
192                                          void *data)
193 {
194         struct wt_status *s = data;
195         int i;
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                 }
217         }
218 }
219
220 static void wt_status_collect_changes_worktree(struct wt_status *s)
221 {
222         struct rev_info rev;
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
232 static void wt_status_collect_changes_index(struct wt_status *s)
233 {
234         struct rev_info rev;
235
236         init_revisions(&rev, NULL);
237         setup_revisions(0, NULL, &rev,
238                 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
239         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
240         rev.diffopt.format_callback = wt_status_collect_updated_cb;
241         rev.diffopt.format_callback_data = s;
242         rev.diffopt.detect_rename = 1;
243         rev.diffopt.rename_limit = 200;
244         rev.diffopt.break_opt = 0;
245         run_diff_index(&rev, 1);
246 }
247
248 static 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
270 void 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
280 static 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  */
309 static 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
326 static void wt_status_print_changed(struct wt_status *s)
327 {
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);
346 }
347
348 static 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
378 static void wt_status_print_untracked(struct wt_status *s)
379 {
380         struct dir_struct dir;
381         int i;
382         int shown_header = 0;
383         struct strbuf buf = STRBUF_INIT;
384
385         memset(&dir, 0, sizeof(dir));
386
387         if (!s->untracked)
388                 dir.flags |=
389                         DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
390         setup_standard_excludes(&dir);
391
392         fill_directory(&dir, NULL);
393         for(i = 0; i < dir.nr; i++) {
394                 struct dir_entry *ent = dir.entries[i];
395                 if (!cache_name_is_other(ent->name, ent->len))
396                         continue;
397                 if (!shown_header) {
398                         s->workdir_untracked = 1;
399                         wt_status_print_untracked_header(s);
400                         shown_header = 1;
401                 }
402                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
403                 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
404                                 quote_path(ent->name, ent->len,
405                                         &buf, s->prefix));
406         }
407         strbuf_release(&buf);
408 }
409
410 static void wt_status_print_verbose(struct wt_status *s)
411 {
412         struct rev_info rev;
413
414         init_revisions(&rev, NULL);
415         DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
416         setup_revisions(0, NULL, &rev,
417                 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
418         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
419         rev.diffopt.detect_rename = 1;
420         rev.diffopt.file = s->fp;
421         rev.diffopt.close_file = 0;
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);
430         run_diff_index(&rev, 1);
431 }
432
433 static 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
452 void wt_status_print(struct wt_status *s)
453 {
454         unsigned char sha1[20];
455         const char *branch_color = color(WT_STATUS_HEADER);
456
457         s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
458         if (s->branch) {
459                 const char *on_what = "On branch ";
460                 const char *branch_name = s->branch;
461                 if (!prefixcmp(branch_name, "refs/heads/"))
462                         branch_name += 11;
463                 else if (!strcmp(branch_name, "HEAD")) {
464                         branch_name = "";
465                         branch_color = color(WT_STATUS_NOBRANCH);
466                         on_what = "Not currently on any branch.";
467                 }
468                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
469                 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
470                 if (!s->is_initial)
471                         wt_status_print_tracking(s);
472         }
473
474         wt_status_collect_changes(s);
475
476         if (s->is_initial) {
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), "#");
480         }
481
482         wt_status_print_updated(s);
483         wt_status_print_changed(s);
484         if (wt_status_submodule_summary)
485                 wt_status_print_submodule_summary(s);
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");
490
491         if (s->verbose)
492                 wt_status_print_verbose(s);
493         if (!s->commitable) {
494                 if (s->amend)
495                         fprintf(s->fp, "# No changes\n");
496                 else if (s->nowarn)
497                         ; /* nothing */
498                 else if (s->workdir_dirty)
499                         printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
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");
504                 else if (!show_untracked_files)
505                         printf("nothing to commit (use -u to show untracked files)\n");
506                 else
507                         printf("nothing to commit (working directory clean)\n");
508         }
509 }
510
511 int git_status_config(const char *k, const char *v, void *cb)
512 {
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         }
520         if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
521                 wt_status_use_color = git_config_colorbool(k, v, -1);
522                 return 0;
523         }
524         if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
525                 int slot = parse_status_slot(k, 13);
526                 if (!v)
527                         return config_error_nonbool(k);
528                 color_parse(v, k, wt_status_colors[slot]);
529                 return 0;
530         }
531         if (!strcmp(k, "status.relativepaths")) {
532                 wt_status_relative_paths = git_config_bool(k, v);
533                 return 0;
534         }
535         if (!strcmp(k, "status.showuntrackedfiles")) {
536                 if (!v)
537                         return config_error_nonbool(k);
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         }
548         return git_diff_ui_config(k, v, cb);
549 }