Merge branch 'mg/advice-statushints'
[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         s->ignored.strdup_strings = 1;
46 }
47
48 static void wt_status_print_unmerged_header(struct wt_status *s)
49 {
50         const char *c = color(WT_STATUS_HEADER, s);
51
52         color_fprintf_ln(s->fp, c, "# Unmerged paths:");
53         if (!advice_status_hints)
54                 return;
55         if (s->in_merge)
56                 ;
57         else if (!s->is_initial)
58                 color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
59         else
60                 color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
61         color_fprintf_ln(s->fp, c, "#   (use \"git add/rm <file>...\" as appropriate to mark resolution)");
62         color_fprintf_ln(s->fp, c, "#");
63 }
64
65 static void wt_status_print_cached_header(struct wt_status *s)
66 {
67         const char *c = color(WT_STATUS_HEADER, s);
68
69         color_fprintf_ln(s->fp, c, "# Changes to be committed:");
70         if (!advice_status_hints)
71                 return;
72         if (s->in_merge)
73                 ; /* NEEDSWORK: use "git reset --unresolve"??? */
74         else if (!s->is_initial)
75                 color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
76         else
77                 color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
78         color_fprintf_ln(s->fp, c, "#");
79 }
80
81 static void wt_status_print_dirty_header(struct wt_status *s,
82                                          int has_deleted,
83                                          int has_dirty_submodules)
84 {
85         const char *c = color(WT_STATUS_HEADER, s);
86
87         color_fprintf_ln(s->fp, c, "# Changed but not updated:");
88         if (!advice_status_hints)
89                 return;
90         if (!has_deleted)
91                 color_fprintf_ln(s->fp, c, "#   (use \"git add <file>...\" to update what will be committed)");
92         else
93                 color_fprintf_ln(s->fp, c, "#   (use \"git add/rm <file>...\" to update what will be committed)");
94         color_fprintf_ln(s->fp, c, "#   (use \"git checkout -- <file>...\" to discard changes in working directory)");
95         if (has_dirty_submodules)
96                 color_fprintf_ln(s->fp, c, "#   (commit or discard the untracked or modified content in submodules)");
97         color_fprintf_ln(s->fp, c, "#");
98 }
99
100 static void wt_status_print_other_header(struct wt_status *s,
101                                          const char *what,
102                                          const char *how)
103 {
104         const char *c = color(WT_STATUS_HEADER, s);
105         color_fprintf_ln(s->fp, c, "# %s files:", what);
106         if (!advice_status_hints)
107                 return;
108         color_fprintf_ln(s->fp, c, "#   (use \"git %s <file>...\" to include in what will be committed)", how);
109         color_fprintf_ln(s->fp, c, "#");
110 }
111
112 static void wt_status_print_trailer(struct wt_status *s)
113 {
114         color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
115 }
116
117 #define quote_path quote_path_relative
118
119 static void wt_status_print_unmerged_data(struct wt_status *s,
120                                           struct string_list_item *it)
121 {
122         const char *c = color(WT_STATUS_UNMERGED, s);
123         struct wt_status_change_data *d = it->util;
124         struct strbuf onebuf = STRBUF_INIT;
125         const char *one, *how = "bug";
126
127         one = quote_path(it->string, -1, &onebuf, s->prefix);
128         color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
129         switch (d->stagemask) {
130         case 1: how = "both deleted:"; break;
131         case 2: how = "added by us:"; break;
132         case 3: how = "deleted by them:"; break;
133         case 4: how = "added by them:"; break;
134         case 5: how = "deleted by us:"; break;
135         case 6: how = "both added:"; break;
136         case 7: how = "both modified:"; break;
137         }
138         color_fprintf(s->fp, c, "%-20s%s\n", how, one);
139         strbuf_release(&onebuf);
140 }
141
142 static void wt_status_print_change_data(struct wt_status *s,
143                                         int change_type,
144                                         struct string_list_item *it)
145 {
146         struct wt_status_change_data *d = it->util;
147         const char *c = color(change_type, s);
148         int status = status;
149         char *one_name;
150         char *two_name;
151         const char *one, *two;
152         struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
153         struct strbuf extra = STRBUF_INIT;
154
155         one_name = two_name = it->string;
156         switch (change_type) {
157         case WT_STATUS_UPDATED:
158                 status = d->index_status;
159                 if (d->head_path)
160                         one_name = d->head_path;
161                 break;
162         case WT_STATUS_CHANGED:
163                 if (d->new_submodule_commits || d->dirty_submodule) {
164                         strbuf_addstr(&extra, " (");
165                         if (d->new_submodule_commits)
166                                 strbuf_addf(&extra, "new commits, ");
167                         if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
168                                 strbuf_addf(&extra, "modified content, ");
169                         if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
170                                 strbuf_addf(&extra, "untracked content, ");
171                         strbuf_setlen(&extra, extra.len - 2);
172                         strbuf_addch(&extra, ')');
173                 }
174                 status = d->worktree_status;
175                 break;
176         }
177
178         one = quote_path(one_name, -1, &onebuf, s->prefix);
179         two = quote_path(two_name, -1, &twobuf, s->prefix);
180
181         color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
182         switch (status) {
183         case DIFF_STATUS_ADDED:
184                 color_fprintf(s->fp, c, "new file:   %s", one);
185                 break;
186         case DIFF_STATUS_COPIED:
187                 color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
188                 break;
189         case DIFF_STATUS_DELETED:
190                 color_fprintf(s->fp, c, "deleted:    %s", one);
191                 break;
192         case DIFF_STATUS_MODIFIED:
193                 color_fprintf(s->fp, c, "modified:   %s", one);
194                 break;
195         case DIFF_STATUS_RENAMED:
196                 color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
197                 break;
198         case DIFF_STATUS_TYPE_CHANGED:
199                 color_fprintf(s->fp, c, "typechange: %s", one);
200                 break;
201         case DIFF_STATUS_UNKNOWN:
202                 color_fprintf(s->fp, c, "unknown:    %s", one);
203                 break;
204         case DIFF_STATUS_UNMERGED:
205                 color_fprintf(s->fp, c, "unmerged:   %s", one);
206                 break;
207         default:
208                 die("bug: unhandled diff status %c", status);
209         }
210         if (extra.len) {
211                 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
212                 strbuf_release(&extra);
213         }
214         fprintf(s->fp, "\n");
215         strbuf_release(&onebuf);
216         strbuf_release(&twobuf);
217 }
218
219 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
220                                          struct diff_options *options,
221                                          void *data)
222 {
223         struct wt_status *s = data;
224         int i;
225
226         if (!q->nr)
227                 return;
228         s->workdir_dirty = 1;
229         for (i = 0; i < q->nr; i++) {
230                 struct diff_filepair *p;
231                 struct string_list_item *it;
232                 struct wt_status_change_data *d;
233
234                 p = q->queue[i];
235                 it = string_list_insert(p->one->path, &s->change);
236                 d = it->util;
237                 if (!d) {
238                         d = xcalloc(1, sizeof(*d));
239                         it->util = d;
240                 }
241                 if (!d->worktree_status)
242                         d->worktree_status = p->status;
243                 d->dirty_submodule = p->two->dirty_submodule;
244                 if (S_ISGITLINK(p->two->mode))
245                         d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
246         }
247 }
248
249 static int unmerged_mask(const char *path)
250 {
251         int pos, mask;
252         struct cache_entry *ce;
253
254         pos = cache_name_pos(path, strlen(path));
255         if (0 <= pos)
256                 return 0;
257
258         mask = 0;
259         pos = -pos-1;
260         while (pos < active_nr) {
261                 ce = active_cache[pos++];
262                 if (strcmp(ce->name, path) || !ce_stage(ce))
263                         break;
264                 mask |= (1 << (ce_stage(ce) - 1));
265         }
266         return mask;
267 }
268
269 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
270                                          struct diff_options *options,
271                                          void *data)
272 {
273         struct wt_status *s = data;
274         int i;
275
276         for (i = 0; i < q->nr; i++) {
277                 struct diff_filepair *p;
278                 struct string_list_item *it;
279                 struct wt_status_change_data *d;
280
281                 p = q->queue[i];
282                 it = string_list_insert(p->two->path, &s->change);
283                 d = it->util;
284                 if (!d) {
285                         d = xcalloc(1, sizeof(*d));
286                         it->util = d;
287                 }
288                 if (!d->index_status)
289                         d->index_status = p->status;
290                 switch (p->status) {
291                 case DIFF_STATUS_COPIED:
292                 case DIFF_STATUS_RENAMED:
293                         d->head_path = xstrdup(p->one->path);
294                         break;
295                 case DIFF_STATUS_UNMERGED:
296                         d->stagemask = unmerged_mask(p->two->path);
297                         break;
298                 }
299         }
300 }
301
302 static void wt_status_collect_changes_worktree(struct wt_status *s)
303 {
304         struct rev_info rev;
305
306         init_revisions(&rev, NULL);
307         setup_revisions(0, NULL, &rev, NULL);
308         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
309         DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
310         if (!s->show_untracked_files)
311                 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
312         rev.diffopt.format_callback = wt_status_collect_changed_cb;
313         rev.diffopt.format_callback_data = s;
314         rev.prune_data = s->pathspec;
315         run_diff_files(&rev, 0);
316 }
317
318 static void wt_status_collect_changes_index(struct wt_status *s)
319 {
320         struct rev_info rev;
321         struct setup_revision_opt opt;
322
323         init_revisions(&rev, NULL);
324         memset(&opt, 0, sizeof(opt));
325         opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
326         setup_revisions(0, NULL, &rev, &opt);
327
328         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
329         rev.diffopt.format_callback = wt_status_collect_updated_cb;
330         rev.diffopt.format_callback_data = s;
331         rev.diffopt.detect_rename = 1;
332         rev.diffopt.rename_limit = 200;
333         rev.diffopt.break_opt = 0;
334         rev.prune_data = s->pathspec;
335         run_diff_index(&rev, 1);
336 }
337
338 static void wt_status_collect_changes_initial(struct wt_status *s)
339 {
340         int i;
341
342         for (i = 0; i < active_nr; i++) {
343                 struct string_list_item *it;
344                 struct wt_status_change_data *d;
345                 struct cache_entry *ce = active_cache[i];
346
347                 if (!ce_path_match(ce, s->pathspec))
348                         continue;
349                 it = string_list_insert(ce->name, &s->change);
350                 d = it->util;
351                 if (!d) {
352                         d = xcalloc(1, sizeof(*d));
353                         it->util = d;
354                 }
355                 if (ce_stage(ce)) {
356                         d->index_status = DIFF_STATUS_UNMERGED;
357                         d->stagemask |= (1 << (ce_stage(ce) - 1));
358                 }
359                 else
360                         d->index_status = DIFF_STATUS_ADDED;
361         }
362 }
363
364 static void wt_status_collect_untracked(struct wt_status *s)
365 {
366         int i;
367         struct dir_struct dir;
368
369         if (!s->show_untracked_files)
370                 return;
371         memset(&dir, 0, sizeof(dir));
372         if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
373                 dir.flags |=
374                         DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
375         setup_standard_excludes(&dir);
376
377         fill_directory(&dir, s->pathspec);
378         for (i = 0; i < dir.nr; i++) {
379                 struct dir_entry *ent = dir.entries[i];
380                 if (!cache_name_is_other(ent->name, ent->len))
381                         continue;
382                 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
383                         continue;
384                 string_list_insert(ent->name, &s->untracked);
385                 free(ent);
386         }
387
388         if (s->show_ignored_files) {
389                 dir.nr = 0;
390                 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
391                 fill_directory(&dir, s->pathspec);
392                 for (i = 0; i < dir.nr; i++) {
393                         struct dir_entry *ent = dir.entries[i];
394                         if (!cache_name_is_other(ent->name, ent->len))
395                                 continue;
396                         if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
397                                 continue;
398                         string_list_insert(ent->name, &s->ignored);
399                         free(ent);
400                 }
401         }
402
403         free(dir.entries);
404 }
405
406 void wt_status_collect(struct wt_status *s)
407 {
408         wt_status_collect_changes_worktree(s);
409
410         if (s->is_initial)
411                 wt_status_collect_changes_initial(s);
412         else
413                 wt_status_collect_changes_index(s);
414         wt_status_collect_untracked(s);
415 }
416
417 static void wt_status_print_unmerged(struct wt_status *s)
418 {
419         int shown_header = 0;
420         int i;
421
422         for (i = 0; i < s->change.nr; i++) {
423                 struct wt_status_change_data *d;
424                 struct string_list_item *it;
425                 it = &(s->change.items[i]);
426                 d = it->util;
427                 if (!d->stagemask)
428                         continue;
429                 if (!shown_header) {
430                         wt_status_print_unmerged_header(s);
431                         shown_header = 1;
432                 }
433                 wt_status_print_unmerged_data(s, it);
434         }
435         if (shown_header)
436                 wt_status_print_trailer(s);
437
438 }
439
440 static void wt_status_print_updated(struct wt_status *s)
441 {
442         int shown_header = 0;
443         int i;
444
445         for (i = 0; i < s->change.nr; i++) {
446                 struct wt_status_change_data *d;
447                 struct string_list_item *it;
448                 it = &(s->change.items[i]);
449                 d = it->util;
450                 if (!d->index_status ||
451                     d->index_status == DIFF_STATUS_UNMERGED)
452                         continue;
453                 if (!shown_header) {
454                         wt_status_print_cached_header(s);
455                         s->commitable = 1;
456                         shown_header = 1;
457                 }
458                 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
459         }
460         if (shown_header)
461                 wt_status_print_trailer(s);
462 }
463
464 /*
465  * -1 : has delete
466  *  0 : no change
467  *  1 : some change but no delete
468  */
469 static int wt_status_check_worktree_changes(struct wt_status *s,
470                                              int *dirty_submodules)
471 {
472         int i;
473         int changes = 0;
474
475         *dirty_submodules = 0;
476
477         for (i = 0; i < s->change.nr; i++) {
478                 struct wt_status_change_data *d;
479                 d = s->change.items[i].util;
480                 if (!d->worktree_status ||
481                     d->worktree_status == DIFF_STATUS_UNMERGED)
482                         continue;
483                 if (!changes)
484                         changes = 1;
485                 if (d->dirty_submodule)
486                         *dirty_submodules = 1;
487                 if (d->worktree_status == DIFF_STATUS_DELETED)
488                         changes = -1;
489         }
490         return changes;
491 }
492
493 static void wt_status_print_changed(struct wt_status *s)
494 {
495         int i, dirty_submodules;
496         int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
497
498         if (!worktree_changes)
499                 return;
500
501         wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
502
503         for (i = 0; i < s->change.nr; i++) {
504                 struct wt_status_change_data *d;
505                 struct string_list_item *it;
506                 it = &(s->change.items[i]);
507                 d = it->util;
508                 if (!d->worktree_status ||
509                     d->worktree_status == DIFF_STATUS_UNMERGED)
510                         continue;
511                 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
512         }
513         wt_status_print_trailer(s);
514 }
515
516 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
517 {
518         struct child_process sm_summary;
519         char summary_limit[64];
520         char index[PATH_MAX];
521         const char *env[] = { index, NULL };
522         const char *argv[] = {
523                 "submodule",
524                 "summary",
525                 uncommitted ? "--files" : "--cached",
526                 "--for-status",
527                 "--summary-limit",
528                 summary_limit,
529                 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
530                 NULL
531         };
532
533         sprintf(summary_limit, "%d", s->submodule_summary);
534         snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
535
536         memset(&sm_summary, 0, sizeof(sm_summary));
537         sm_summary.argv = argv;
538         sm_summary.env = env;
539         sm_summary.git_cmd = 1;
540         sm_summary.no_stdin = 1;
541         fflush(s->fp);
542         sm_summary.out = dup(fileno(s->fp));    /* run_command closes it */
543         run_command(&sm_summary);
544 }
545
546 static void wt_status_print_other(struct wt_status *s,
547                                   struct string_list *l,
548                                   const char *what,
549                                   const char *how)
550 {
551         int i;
552         struct strbuf buf = STRBUF_INIT;
553
554         if (!s->untracked.nr)
555                 return;
556
557         wt_status_print_other_header(s, what, how);
558
559         for (i = 0; i < l->nr; i++) {
560                 struct string_list_item *it;
561                 it = &(l->items[i]);
562                 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
563                 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
564                                  quote_path(it->string, strlen(it->string),
565                                             &buf, s->prefix));
566         }
567         strbuf_release(&buf);
568 }
569
570 static void wt_status_print_verbose(struct wt_status *s)
571 {
572         struct rev_info rev;
573         struct setup_revision_opt opt;
574
575         init_revisions(&rev, NULL);
576         DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
577
578         memset(&opt, 0, sizeof(opt));
579         opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
580         setup_revisions(0, NULL, &rev, &opt);
581
582         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
583         rev.diffopt.detect_rename = 1;
584         rev.diffopt.file = s->fp;
585         rev.diffopt.close_file = 0;
586         /*
587          * If we're not going to stdout, then we definitely don't
588          * want color, since we are going to the commit message
589          * file (and even the "auto" setting won't work, since it
590          * will have checked isatty on stdout).
591          */
592         if (s->fp != stdout)
593                 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
594         run_diff_index(&rev, 1);
595 }
596
597 static void wt_status_print_tracking(struct wt_status *s)
598 {
599         struct strbuf sb = STRBUF_INIT;
600         const char *cp, *ep;
601         struct branch *branch;
602
603         assert(s->branch && !s->is_initial);
604         if (prefixcmp(s->branch, "refs/heads/"))
605                 return;
606         branch = branch_get(s->branch + 11);
607         if (!format_tracking_info(branch, &sb))
608                 return;
609
610         for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
611                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
612                                  "# %.*s", (int)(ep - cp), cp);
613         color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
614 }
615
616 void wt_status_print(struct wt_status *s)
617 {
618         const char *branch_color = color(WT_STATUS_HEADER, s);
619
620         if (s->branch) {
621                 const char *on_what = "On branch ";
622                 const char *branch_name = s->branch;
623                 if (!prefixcmp(branch_name, "refs/heads/"))
624                         branch_name += 11;
625                 else if (!strcmp(branch_name, "HEAD")) {
626                         branch_name = "";
627                         branch_color = color(WT_STATUS_NOBRANCH, s);
628                         on_what = "Not currently on any branch.";
629                 }
630                 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
631                 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
632                 if (!s->is_initial)
633                         wt_status_print_tracking(s);
634         }
635
636         if (s->is_initial) {
637                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
638                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
639                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
640         }
641
642         wt_status_print_updated(s);
643         wt_status_print_unmerged(s);
644         wt_status_print_changed(s);
645         if (s->submodule_summary) {
646                 wt_status_print_submodule_summary(s, 0);  /* staged */
647                 wt_status_print_submodule_summary(s, 1);  /* unstaged */
648         }
649         if (s->show_untracked_files) {
650                 wt_status_print_other(s, &s->untracked, "Untracked", "add");
651                 if (s->show_ignored_files)
652                         wt_status_print_other(s, &s->ignored, "Ignored", "add -f");
653         } else if (s->commitable)
654                 fprintf(s->fp, "# Untracked files not listed%s\n",
655                         advice_status_hints
656                         ? " (use -u option to show untracked files)" : "");
657
658         if (s->verbose)
659                 wt_status_print_verbose(s);
660         if (!s->commitable) {
661                 if (s->amend)
662                         fprintf(s->fp, "# No changes\n");
663                 else if (s->nowarn)
664                         ; /* nothing */
665                 else if (s->workdir_dirty)
666                         printf("no changes added to commit%s\n",
667                                 advice_status_hints
668                                 ? " (use \"git add\" and/or \"git commit -a\")" : "");
669                 else if (s->untracked.nr)
670                         printf("nothing added to commit but untracked files present%s\n",
671                                 advice_status_hints
672                                 ? " (use \"git add\" to track)" : "");
673                 else if (s->is_initial)
674                         printf("nothing to commit%s\n", advice_status_hints
675                                 ? " (create/copy files and use \"git add\" to track)" : "");
676                 else if (!s->show_untracked_files)
677                         printf("nothing to commit%s\n", advice_status_hints
678                                 ? " (use -u to show untracked files)" : "");
679                 else
680                         printf("nothing to commit%s\n", advice_status_hints
681                                 ? " (working directory clean)" : "");
682         }
683 }
684
685 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
686                            struct wt_status *s)
687 {
688         struct wt_status_change_data *d = it->util;
689         const char *how = "??";
690
691         switch (d->stagemask) {
692         case 1: how = "DD"; break; /* both deleted */
693         case 2: how = "AU"; break; /* added by us */
694         case 3: how = "UD"; break; /* deleted by them */
695         case 4: how = "UA"; break; /* added by them */
696         case 5: how = "DU"; break; /* deleted by us */
697         case 6: how = "AA"; break; /* both added */
698         case 7: how = "UU"; break; /* both modified */
699         }
700         color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
701         if (null_termination) {
702                 fprintf(stdout, " %s%c", it->string, 0);
703         } else {
704                 struct strbuf onebuf = STRBUF_INIT;
705                 const char *one;
706                 one = quote_path(it->string, -1, &onebuf, s->prefix);
707                 printf(" %s\n", one);
708                 strbuf_release(&onebuf);
709         }
710 }
711
712 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
713                          struct wt_status *s)
714 {
715         struct wt_status_change_data *d = it->util;
716
717         if (d->index_status)
718                 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
719         else
720                 putchar(' ');
721         if (d->worktree_status)
722                 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
723         else
724                 putchar(' ');
725         putchar(' ');
726         if (null_termination) {
727                 fprintf(stdout, "%s%c", it->string, 0);
728                 if (d->head_path)
729                         fprintf(stdout, "%s%c", d->head_path, 0);
730         } else {
731                 struct strbuf onebuf = STRBUF_INIT;
732                 const char *one;
733                 if (d->head_path) {
734                         one = quote_path(d->head_path, -1, &onebuf, s->prefix);
735                         printf("%s -> ", one);
736                         strbuf_release(&onebuf);
737                 }
738                 one = quote_path(it->string, -1, &onebuf, s->prefix);
739                 printf("%s\n", one);
740                 strbuf_release(&onebuf);
741         }
742 }
743
744 static void wt_shortstatus_other(int null_termination, struct string_list_item *it,
745                                  struct wt_status *s, const char *sign)
746 {
747         if (null_termination) {
748                 fprintf(stdout, "%s %s%c", sign, it->string, 0);
749         } else {
750                 struct strbuf onebuf = STRBUF_INIT;
751                 const char *one;
752                 one = quote_path(it->string, -1, &onebuf, s->prefix);
753                 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
754                 printf(" %s\n", one);
755                 strbuf_release(&onebuf);
756         }
757 }
758
759 void wt_shortstatus_print(struct wt_status *s, int null_termination)
760 {
761         int i;
762         for (i = 0; i < s->change.nr; i++) {
763                 struct wt_status_change_data *d;
764                 struct string_list_item *it;
765
766                 it = &(s->change.items[i]);
767                 d = it->util;
768                 if (d->stagemask)
769                         wt_shortstatus_unmerged(null_termination, it, s);
770                 else
771                         wt_shortstatus_status(null_termination, it, s);
772         }
773         for (i = 0; i < s->untracked.nr; i++) {
774                 struct string_list_item *it;
775
776                 it = &(s->untracked.items[i]);
777                 wt_shortstatus_other(null_termination, it, s, "??");
778         }
779         for (i = 0; i < s->ignored.nr; i++) {
780                 struct string_list_item *it;
781
782                 it = &(s->ignored.items[i]);
783                 wt_shortstatus_other(null_termination, it, s, "!!");
784         }
785 }
786
787 void wt_porcelain_print(struct wt_status *s, int null_termination)
788 {
789         s->use_color = 0;
790         s->relative_paths = 0;
791         s->prefix = NULL;
792         wt_shortstatus_print(s, null_termination);
793 }