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