Merge branch 'nd/unpack-entry-optim-in-pack-objects'
[git] / wt-status.c
1 #include "cache.h"
2 #include "pathspec.h"
3 #include "wt-status.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 "argv-array.h"
13 #include "remote.h"
14 #include "refs.h"
15 #include "submodule.h"
16 #include "column.h"
17 #include "strbuf.h"
18
19 static char default_wt_status_colors[][COLOR_MAXLEN] = {
20         GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
21         GIT_COLOR_GREEN,  /* WT_STATUS_UPDATED */
22         GIT_COLOR_RED,    /* WT_STATUS_CHANGED */
23         GIT_COLOR_RED,    /* WT_STATUS_UNTRACKED */
24         GIT_COLOR_RED,    /* WT_STATUS_NOBRANCH */
25         GIT_COLOR_RED,    /* WT_STATUS_UNMERGED */
26         GIT_COLOR_GREEN,  /* WT_STATUS_LOCAL_BRANCH */
27         GIT_COLOR_RED,    /* WT_STATUS_REMOTE_BRANCH */
28         GIT_COLOR_NIL,    /* WT_STATUS_ONBRANCH */
29 };
30
31 static const char *color(int slot, struct wt_status *s)
32 {
33         const char *c = "";
34         if (want_color(s->use_color))
35                 c = s->color_palette[slot];
36         if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
37                 c = s->color_palette[WT_STATUS_HEADER];
38         return c;
39 }
40
41 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
42                 const char *fmt, va_list ap, const char *trail)
43 {
44         struct strbuf sb = STRBUF_INIT;
45         struct strbuf linebuf = STRBUF_INIT;
46         const char *line, *eol;
47
48         strbuf_vaddf(&sb, fmt, ap);
49         if (!sb.len) {
50                 if (s->display_comment_prefix) {
51                         strbuf_addch(&sb, comment_line_char);
52                         if (!trail)
53                                 strbuf_addch(&sb, ' ');
54                 }
55                 color_print_strbuf(s->fp, color, &sb);
56                 if (trail)
57                         fprintf(s->fp, "%s", trail);
58                 strbuf_release(&sb);
59                 return;
60         }
61         for (line = sb.buf; *line; line = eol + 1) {
62                 eol = strchr(line, '\n');
63
64                 strbuf_reset(&linebuf);
65                 if (at_bol && s->display_comment_prefix) {
66                         strbuf_addch(&linebuf, comment_line_char);
67                         if (*line != '\n' && *line != '\t')
68                                 strbuf_addch(&linebuf, ' ');
69                 }
70                 if (eol)
71                         strbuf_add(&linebuf, line, eol - line);
72                 else
73                         strbuf_addstr(&linebuf, line);
74                 color_print_strbuf(s->fp, color, &linebuf);
75                 if (eol)
76                         fprintf(s->fp, "\n");
77                 else
78                         break;
79                 at_bol = 1;
80         }
81         if (trail)
82                 fprintf(s->fp, "%s", trail);
83         strbuf_release(&linebuf);
84         strbuf_release(&sb);
85 }
86
87 void status_printf_ln(struct wt_status *s, const char *color,
88                         const char *fmt, ...)
89 {
90         va_list ap;
91
92         va_start(ap, fmt);
93         status_vprintf(s, 1, color, fmt, ap, "\n");
94         va_end(ap);
95 }
96
97 void status_printf(struct wt_status *s, const char *color,
98                         const char *fmt, ...)
99 {
100         va_list ap;
101
102         va_start(ap, fmt);
103         status_vprintf(s, 1, color, fmt, ap, NULL);
104         va_end(ap);
105 }
106
107 static void status_printf_more(struct wt_status *s, const char *color,
108                                const char *fmt, ...)
109 {
110         va_list ap;
111
112         va_start(ap, fmt);
113         status_vprintf(s, 0, color, fmt, ap, NULL);
114         va_end(ap);
115 }
116
117 void wt_status_prepare(struct wt_status *s)
118 {
119         unsigned char sha1[20];
120
121         memset(s, 0, sizeof(*s));
122         memcpy(s->color_palette, default_wt_status_colors,
123                sizeof(default_wt_status_colors));
124         s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
125         s->use_color = -1;
126         s->relative_paths = 1;
127         s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
128         s->reference = "HEAD";
129         s->fp = stdout;
130         s->index_file = get_index_file();
131         s->change.strdup_strings = 1;
132         s->untracked.strdup_strings = 1;
133         s->ignored.strdup_strings = 1;
134         s->show_branch = -1;  /* unspecified */
135         s->display_comment_prefix = 0;
136 }
137
138 static void wt_status_print_unmerged_header(struct wt_status *s)
139 {
140         int i;
141         int del_mod_conflict = 0;
142         int both_deleted = 0;
143         int not_deleted = 0;
144         const char *c = color(WT_STATUS_HEADER, s);
145
146         status_printf_ln(s, c, _("Unmerged paths:"));
147
148         for (i = 0; i < s->change.nr; i++) {
149                 struct string_list_item *it = &(s->change.items[i]);
150                 struct wt_status_change_data *d = it->util;
151
152                 switch (d->stagemask) {
153                 case 0:
154                         break;
155                 case 1:
156                         both_deleted = 1;
157                         break;
158                 case 3:
159                 case 5:
160                         del_mod_conflict = 1;
161                         break;
162                 default:
163                         not_deleted = 1;
164                         break;
165                 }
166         }
167
168         if (!s->hints)
169                 return;
170         if (s->whence != FROM_COMMIT)
171                 ;
172         else if (!s->is_initial)
173                 status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
174         else
175                 status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
176
177         if (!both_deleted) {
178                 if (!del_mod_conflict)
179                         status_printf_ln(s, c, _("  (use \"git add <file>...\" to mark resolution)"));
180                 else
181                         status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
182         } else if (!del_mod_conflict && !not_deleted) {
183                 status_printf_ln(s, c, _("  (use \"git rm <file>...\" to mark resolution)"));
184         } else {
185                 status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
186         }
187         status_printf_ln(s, c, "");
188 }
189
190 static void wt_status_print_cached_header(struct wt_status *s)
191 {
192         const char *c = color(WT_STATUS_HEADER, s);
193
194         status_printf_ln(s, c, _("Changes to be committed:"));
195         if (!s->hints)
196                 return;
197         if (s->whence != FROM_COMMIT)
198                 ; /* NEEDSWORK: use "git reset --unresolve"??? */
199         else if (!s->is_initial)
200                 status_printf_ln(s, c, _("  (use \"git reset %s <file>...\" to unstage)"), s->reference);
201         else
202                 status_printf_ln(s, c, _("  (use \"git rm --cached <file>...\" to unstage)"));
203         status_printf_ln(s, c, "");
204 }
205
206 static void wt_status_print_dirty_header(struct wt_status *s,
207                                          int has_deleted,
208                                          int has_dirty_submodules)
209 {
210         const char *c = color(WT_STATUS_HEADER, s);
211
212         status_printf_ln(s, c, _("Changes not staged for commit:"));
213         if (!s->hints)
214                 return;
215         if (!has_deleted)
216                 status_printf_ln(s, c, _("  (use \"git add <file>...\" to update what will be committed)"));
217         else
218                 status_printf_ln(s, c, _("  (use \"git add/rm <file>...\" to update what will be committed)"));
219         status_printf_ln(s, c, _("  (use \"git checkout -- <file>...\" to discard changes in working directory)"));
220         if (has_dirty_submodules)
221                 status_printf_ln(s, c, _("  (commit or discard the untracked or modified content in submodules)"));
222         status_printf_ln(s, c, "");
223 }
224
225 static void wt_status_print_other_header(struct wt_status *s,
226                                          const char *what,
227                                          const char *how)
228 {
229         const char *c = color(WT_STATUS_HEADER, s);
230         status_printf_ln(s, c, "%s:", what);
231         if (!s->hints)
232                 return;
233         status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
234         status_printf_ln(s, c, "");
235 }
236
237 static void wt_status_print_trailer(struct wt_status *s)
238 {
239         status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
240 }
241
242 #define quote_path quote_path_relative
243
244 static void wt_status_print_unmerged_data(struct wt_status *s,
245                                           struct string_list_item *it)
246 {
247         const char *c = color(WT_STATUS_UNMERGED, s);
248         struct wt_status_change_data *d = it->util;
249         struct strbuf onebuf = STRBUF_INIT;
250         const char *one, *how = _("bug");
251
252         one = quote_path(it->string, s->prefix, &onebuf);
253         status_printf(s, color(WT_STATUS_HEADER, s), "\t");
254         switch (d->stagemask) {
255         case 1: how = _("both deleted:"); break;
256         case 2: how = _("added by us:"); break;
257         case 3: how = _("deleted by them:"); break;
258         case 4: how = _("added by them:"); break;
259         case 5: how = _("deleted by us:"); break;
260         case 6: how = _("both added:"); break;
261         case 7: how = _("both modified:"); break;
262         }
263         status_printf_more(s, c, "%-20s%s\n", how, one);
264         strbuf_release(&onebuf);
265 }
266
267 static void wt_status_print_change_data(struct wt_status *s,
268                                         int change_type,
269                                         struct string_list_item *it)
270 {
271         struct wt_status_change_data *d = it->util;
272         const char *c = color(change_type, s);
273         int status;
274         char *one_name;
275         char *two_name;
276         const char *one, *two;
277         struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
278         struct strbuf extra = STRBUF_INIT;
279
280         one_name = two_name = it->string;
281         switch (change_type) {
282         case WT_STATUS_UPDATED:
283                 status = d->index_status;
284                 if (d->head_path)
285                         one_name = d->head_path;
286                 break;
287         case WT_STATUS_CHANGED:
288                 if (d->new_submodule_commits || d->dirty_submodule) {
289                         strbuf_addstr(&extra, " (");
290                         if (d->new_submodule_commits)
291                                 strbuf_addf(&extra, _("new commits, "));
292                         if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
293                                 strbuf_addf(&extra, _("modified content, "));
294                         if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
295                                 strbuf_addf(&extra, _("untracked content, "));
296                         strbuf_setlen(&extra, extra.len - 2);
297                         strbuf_addch(&extra, ')');
298                 }
299                 status = d->worktree_status;
300                 break;
301         default:
302                 die("BUG: unhandled change_type %d in wt_status_print_change_data",
303                     change_type);
304         }
305
306         one = quote_path(one_name, s->prefix, &onebuf);
307         two = quote_path(two_name, s->prefix, &twobuf);
308
309         status_printf(s, color(WT_STATUS_HEADER, s), "\t");
310         switch (status) {
311         case DIFF_STATUS_ADDED:
312                 status_printf_more(s, c, _("new file:   %s"), one);
313                 break;
314         case DIFF_STATUS_COPIED:
315                 status_printf_more(s, c, _("copied:     %s -> %s"), one, two);
316                 break;
317         case DIFF_STATUS_DELETED:
318                 status_printf_more(s, c, _("deleted:    %s"), one);
319                 break;
320         case DIFF_STATUS_MODIFIED:
321                 status_printf_more(s, c, _("modified:   %s"), one);
322                 break;
323         case DIFF_STATUS_RENAMED:
324                 status_printf_more(s, c, _("renamed:    %s -> %s"), one, two);
325                 break;
326         case DIFF_STATUS_TYPE_CHANGED:
327                 status_printf_more(s, c, _("typechange: %s"), one);
328                 break;
329         case DIFF_STATUS_UNKNOWN:
330                 status_printf_more(s, c, _("unknown:    %s"), one);
331                 break;
332         case DIFF_STATUS_UNMERGED:
333                 status_printf_more(s, c, _("unmerged:   %s"), one);
334                 break;
335         default:
336                 die(_("bug: unhandled diff status %c"), status);
337         }
338         if (extra.len) {
339                 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
340                 strbuf_release(&extra);
341         }
342         status_printf_more(s, GIT_COLOR_NORMAL, "\n");
343         strbuf_release(&onebuf);
344         strbuf_release(&twobuf);
345 }
346
347 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
348                                          struct diff_options *options,
349                                          void *data)
350 {
351         struct wt_status *s = data;
352         int i;
353
354         if (!q->nr)
355                 return;
356         s->workdir_dirty = 1;
357         for (i = 0; i < q->nr; i++) {
358                 struct diff_filepair *p;
359                 struct string_list_item *it;
360                 struct wt_status_change_data *d;
361
362                 p = q->queue[i];
363                 it = string_list_insert(&s->change, p->one->path);
364                 d = it->util;
365                 if (!d) {
366                         d = xcalloc(1, sizeof(*d));
367                         it->util = d;
368                 }
369                 if (!d->worktree_status)
370                         d->worktree_status = p->status;
371                 d->dirty_submodule = p->two->dirty_submodule;
372                 if (S_ISGITLINK(p->two->mode))
373                         d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
374         }
375 }
376
377 static int unmerged_mask(const char *path)
378 {
379         int pos, mask;
380         const struct cache_entry *ce;
381
382         pos = cache_name_pos(path, strlen(path));
383         if (0 <= pos)
384                 return 0;
385
386         mask = 0;
387         pos = -pos-1;
388         while (pos < active_nr) {
389                 ce = active_cache[pos++];
390                 if (strcmp(ce->name, path) || !ce_stage(ce))
391                         break;
392                 mask |= (1 << (ce_stage(ce) - 1));
393         }
394         return mask;
395 }
396
397 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
398                                          struct diff_options *options,
399                                          void *data)
400 {
401         struct wt_status *s = data;
402         int i;
403
404         for (i = 0; i < q->nr; i++) {
405                 struct diff_filepair *p;
406                 struct string_list_item *it;
407                 struct wt_status_change_data *d;
408
409                 p = q->queue[i];
410                 it = string_list_insert(&s->change, p->two->path);
411                 d = it->util;
412                 if (!d) {
413                         d = xcalloc(1, sizeof(*d));
414                         it->util = d;
415                 }
416                 if (!d->index_status)
417                         d->index_status = p->status;
418                 switch (p->status) {
419                 case DIFF_STATUS_COPIED:
420                 case DIFF_STATUS_RENAMED:
421                         d->head_path = xstrdup(p->one->path);
422                         break;
423                 case DIFF_STATUS_UNMERGED:
424                         d->stagemask = unmerged_mask(p->two->path);
425                         break;
426                 }
427         }
428 }
429
430 static void wt_status_collect_changes_worktree(struct wt_status *s)
431 {
432         struct rev_info rev;
433
434         init_revisions(&rev, NULL);
435         setup_revisions(0, NULL, &rev, NULL);
436         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
437         DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
438         if (!s->show_untracked_files)
439                 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
440         if (s->ignore_submodule_arg) {
441                 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
442                 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
443         }
444         rev.diffopt.format_callback = wt_status_collect_changed_cb;
445         rev.diffopt.format_callback_data = s;
446         copy_pathspec(&rev.prune_data, &s->pathspec);
447         run_diff_files(&rev, 0);
448 }
449
450 static void wt_status_collect_changes_index(struct wt_status *s)
451 {
452         struct rev_info rev;
453         struct setup_revision_opt opt;
454
455         init_revisions(&rev, NULL);
456         memset(&opt, 0, sizeof(opt));
457         opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
458         setup_revisions(0, NULL, &rev, &opt);
459
460         if (s->ignore_submodule_arg) {
461                 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
462                 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
463         }
464
465         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
466         rev.diffopt.format_callback = wt_status_collect_updated_cb;
467         rev.diffopt.format_callback_data = s;
468         rev.diffopt.detect_rename = 1;
469         rev.diffopt.rename_limit = 200;
470         rev.diffopt.break_opt = 0;
471         copy_pathspec(&rev.prune_data, &s->pathspec);
472         run_diff_index(&rev, 1);
473 }
474
475 static void wt_status_collect_changes_initial(struct wt_status *s)
476 {
477         int i;
478
479         for (i = 0; i < active_nr; i++) {
480                 struct string_list_item *it;
481                 struct wt_status_change_data *d;
482                 const struct cache_entry *ce = active_cache[i];
483
484                 if (!ce_path_match(ce, &s->pathspec))
485                         continue;
486                 it = string_list_insert(&s->change, ce->name);
487                 d = it->util;
488                 if (!d) {
489                         d = xcalloc(1, sizeof(*d));
490                         it->util = d;
491                 }
492                 if (ce_stage(ce)) {
493                         d->index_status = DIFF_STATUS_UNMERGED;
494                         d->stagemask |= (1 << (ce_stage(ce) - 1));
495                 }
496                 else
497                         d->index_status = DIFF_STATUS_ADDED;
498         }
499 }
500
501 static void wt_status_collect_untracked(struct wt_status *s)
502 {
503         int i;
504         struct dir_struct dir;
505         struct timeval t_begin;
506
507         if (!s->show_untracked_files)
508                 return;
509
510         if (advice_status_u_option)
511                 gettimeofday(&t_begin, NULL);
512
513         memset(&dir, 0, sizeof(dir));
514         if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
515                 dir.flags |=
516                         DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
517         if (s->show_ignored_files)
518                 dir.flags |= DIR_SHOW_IGNORED_TOO;
519         setup_standard_excludes(&dir);
520
521         fill_directory(&dir, &s->pathspec);
522
523         for (i = 0; i < dir.nr; i++) {
524                 struct dir_entry *ent = dir.entries[i];
525                 if (cache_name_is_other(ent->name, ent->len) &&
526                     match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
527                         string_list_insert(&s->untracked, ent->name);
528                 free(ent);
529         }
530
531         for (i = 0; i < dir.ignored_nr; i++) {
532                 struct dir_entry *ent = dir.ignored[i];
533                 if (cache_name_is_other(ent->name, ent->len) &&
534                     match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))
535                         string_list_insert(&s->ignored, ent->name);
536                 free(ent);
537         }
538
539         free(dir.entries);
540         free(dir.ignored);
541         clear_directory(&dir);
542
543         if (advice_status_u_option) {
544                 struct timeval t_end;
545                 gettimeofday(&t_end, NULL);
546                 s->untracked_in_ms =
547                         (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
548                         ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
549         }
550 }
551
552 void wt_status_collect(struct wt_status *s)
553 {
554         wt_status_collect_changes_worktree(s);
555
556         if (s->is_initial)
557                 wt_status_collect_changes_initial(s);
558         else
559                 wt_status_collect_changes_index(s);
560         wt_status_collect_untracked(s);
561 }
562
563 static void wt_status_print_unmerged(struct wt_status *s)
564 {
565         int shown_header = 0;
566         int i;
567
568         for (i = 0; i < s->change.nr; i++) {
569                 struct wt_status_change_data *d;
570                 struct string_list_item *it;
571                 it = &(s->change.items[i]);
572                 d = it->util;
573                 if (!d->stagemask)
574                         continue;
575                 if (!shown_header) {
576                         wt_status_print_unmerged_header(s);
577                         shown_header = 1;
578                 }
579                 wt_status_print_unmerged_data(s, it);
580         }
581         if (shown_header)
582                 wt_status_print_trailer(s);
583
584 }
585
586 static void wt_status_print_updated(struct wt_status *s)
587 {
588         int shown_header = 0;
589         int i;
590
591         for (i = 0; i < s->change.nr; i++) {
592                 struct wt_status_change_data *d;
593                 struct string_list_item *it;
594                 it = &(s->change.items[i]);
595                 d = it->util;
596                 if (!d->index_status ||
597                     d->index_status == DIFF_STATUS_UNMERGED)
598                         continue;
599                 if (!shown_header) {
600                         wt_status_print_cached_header(s);
601                         s->commitable = 1;
602                         shown_header = 1;
603                 }
604                 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
605         }
606         if (shown_header)
607                 wt_status_print_trailer(s);
608 }
609
610 /*
611  * -1 : has delete
612  *  0 : no change
613  *  1 : some change but no delete
614  */
615 static int wt_status_check_worktree_changes(struct wt_status *s,
616                                              int *dirty_submodules)
617 {
618         int i;
619         int changes = 0;
620
621         *dirty_submodules = 0;
622
623         for (i = 0; i < s->change.nr; i++) {
624                 struct wt_status_change_data *d;
625                 d = s->change.items[i].util;
626                 if (!d->worktree_status ||
627                     d->worktree_status == DIFF_STATUS_UNMERGED)
628                         continue;
629                 if (!changes)
630                         changes = 1;
631                 if (d->dirty_submodule)
632                         *dirty_submodules = 1;
633                 if (d->worktree_status == DIFF_STATUS_DELETED)
634                         changes = -1;
635         }
636         return changes;
637 }
638
639 static void wt_status_print_changed(struct wt_status *s)
640 {
641         int i, dirty_submodules;
642         int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
643
644         if (!worktree_changes)
645                 return;
646
647         wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
648
649         for (i = 0; i < s->change.nr; i++) {
650                 struct wt_status_change_data *d;
651                 struct string_list_item *it;
652                 it = &(s->change.items[i]);
653                 d = it->util;
654                 if (!d->worktree_status ||
655                     d->worktree_status == DIFF_STATUS_UNMERGED)
656                         continue;
657                 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
658         }
659         wt_status_print_trailer(s);
660 }
661
662 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
663 {
664         struct child_process sm_summary;
665         char summary_limit[64];
666         char index[PATH_MAX];
667         const char *env[] = { NULL, NULL };
668         struct argv_array argv = ARGV_ARRAY_INIT;
669         struct strbuf cmd_stdout = STRBUF_INIT;
670         struct strbuf summary = STRBUF_INIT;
671         char *summary_content;
672         size_t len;
673
674         sprintf(summary_limit, "%d", s->submodule_summary);
675         snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
676
677         env[0] = index;
678         argv_array_push(&argv, "submodule");
679         argv_array_push(&argv, "summary");
680         argv_array_push(&argv, uncommitted ? "--files" : "--cached");
681         argv_array_push(&argv, "--for-status");
682         argv_array_push(&argv, "--summary-limit");
683         argv_array_push(&argv, summary_limit);
684         if (!uncommitted)
685                 argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
686
687         memset(&sm_summary, 0, sizeof(sm_summary));
688         sm_summary.argv = argv.argv;
689         sm_summary.env = env;
690         sm_summary.git_cmd = 1;
691         sm_summary.no_stdin = 1;
692         fflush(s->fp);
693         sm_summary.out = -1;
694
695         run_command(&sm_summary);
696         argv_array_clear(&argv);
697
698         len = strbuf_read(&cmd_stdout, sm_summary.out, 1024);
699
700         /* prepend header, only if there's an actual output */
701         if (len) {
702                 if (uncommitted)
703                         strbuf_addstr(&summary, _("Submodules changed but not updated:"));
704                 else
705                         strbuf_addstr(&summary, _("Submodule changes to be committed:"));
706                 strbuf_addstr(&summary, "\n\n");
707         }
708         strbuf_addbuf(&summary, &cmd_stdout);
709         strbuf_release(&cmd_stdout);
710
711         if (s->display_comment_prefix) {
712                 summary_content = strbuf_detach(&summary, &len);
713                 strbuf_add_commented_lines(&summary, summary_content, len);
714                 free(summary_content);
715         }
716
717         fputs(summary.buf, s->fp);
718         strbuf_release(&summary);
719 }
720
721 static void wt_status_print_other(struct wt_status *s,
722                                   struct string_list *l,
723                                   const char *what,
724                                   const char *how)
725 {
726         int i;
727         struct strbuf buf = STRBUF_INIT;
728         static struct string_list output = STRING_LIST_INIT_DUP;
729         struct column_options copts;
730
731         if (!l->nr)
732                 return;
733
734         wt_status_print_other_header(s, what, how);
735
736         for (i = 0; i < l->nr; i++) {
737                 struct string_list_item *it;
738                 const char *path;
739                 it = &(l->items[i]);
740                 path = quote_path(it->string, s->prefix, &buf);
741                 if (column_active(s->colopts)) {
742                         string_list_append(&output, path);
743                         continue;
744                 }
745                 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
746                 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
747                                    "%s\n", path);
748         }
749
750         strbuf_release(&buf);
751         if (!column_active(s->colopts))
752                 goto conclude;
753
754         strbuf_addf(&buf, "%s%s\t%s",
755                     color(WT_STATUS_HEADER, s),
756                     s->display_comment_prefix ? "#" : "",
757                     color(WT_STATUS_UNTRACKED, s));
758         memset(&copts, 0, sizeof(copts));
759         copts.padding = 1;
760         copts.indent = buf.buf;
761         if (want_color(s->use_color))
762                 copts.nl = GIT_COLOR_RESET "\n";
763         print_columns(&output, s->colopts, &copts);
764         string_list_clear(&output, 0);
765         strbuf_release(&buf);
766 conclude:
767         status_printf_ln(s, GIT_COLOR_NORMAL, "");
768 }
769
770 static void wt_status_print_verbose(struct wt_status *s)
771 {
772         struct rev_info rev;
773         struct setup_revision_opt opt;
774
775         init_revisions(&rev, NULL);
776         DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
777
778         memset(&opt, 0, sizeof(opt));
779         opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
780         setup_revisions(0, NULL, &rev, &opt);
781
782         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
783         rev.diffopt.detect_rename = 1;
784         rev.diffopt.file = s->fp;
785         rev.diffopt.close_file = 0;
786         /*
787          * If we're not going to stdout, then we definitely don't
788          * want color, since we are going to the commit message
789          * file (and even the "auto" setting won't work, since it
790          * will have checked isatty on stdout).
791          */
792         if (s->fp != stdout)
793                 rev.diffopt.use_color = 0;
794         run_diff_index(&rev, 1);
795 }
796
797 static void wt_status_print_tracking(struct wt_status *s)
798 {
799         struct strbuf sb = STRBUF_INIT;
800         const char *cp, *ep;
801         struct branch *branch;
802         char comment_line_string[3];
803         int i;
804
805         assert(s->branch && !s->is_initial);
806         if (prefixcmp(s->branch, "refs/heads/"))
807                 return;
808         branch = branch_get(s->branch + 11);
809         if (!format_tracking_info(branch, &sb))
810                 return;
811
812         i = 0;
813         if (s->display_comment_prefix) {
814                 comment_line_string[i++] = comment_line_char;
815                 comment_line_string[i++] = ' ';
816         }
817         comment_line_string[i] = '\0';
818
819         for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
820                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
821                                  "%s%.*s", comment_line_string,
822                                  (int)(ep - cp), cp);
823         if (s->display_comment_prefix)
824                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
825                                  comment_line_char);
826         else
827                 fprintf_ln(s->fp, "");
828 }
829
830 static int has_unmerged(struct wt_status *s)
831 {
832         int i;
833
834         for (i = 0; i < s->change.nr; i++) {
835                 struct wt_status_change_data *d;
836                 d = s->change.items[i].util;
837                 if (d->stagemask)
838                         return 1;
839         }
840         return 0;
841 }
842
843 static void show_merge_in_progress(struct wt_status *s,
844                                 struct wt_status_state *state,
845                                 const char *color)
846 {
847         if (has_unmerged(s)) {
848                 status_printf_ln(s, color, _("You have unmerged paths."));
849                 if (s->hints)
850                         status_printf_ln(s, color,
851                                 _("  (fix conflicts and run \"git commit\")"));
852         } else {
853                 status_printf_ln(s, color,
854                         _("All conflicts fixed but you are still merging."));
855                 if (s->hints)
856                         status_printf_ln(s, color,
857                                 _("  (use \"git commit\" to conclude merge)"));
858         }
859         wt_status_print_trailer(s);
860 }
861
862 static void show_am_in_progress(struct wt_status *s,
863                                 struct wt_status_state *state,
864                                 const char *color)
865 {
866         status_printf_ln(s, color,
867                 _("You are in the middle of an am session."));
868         if (state->am_empty_patch)
869                 status_printf_ln(s, color,
870                         _("The current patch is empty."));
871         if (s->hints) {
872                 if (!state->am_empty_patch)
873                         status_printf_ln(s, color,
874                                 _("  (fix conflicts and then run \"git am --continue\")"));
875                 status_printf_ln(s, color,
876                         _("  (use \"git am --skip\" to skip this patch)"));
877                 status_printf_ln(s, color,
878                         _("  (use \"git am --abort\" to restore the original branch)"));
879         }
880         wt_status_print_trailer(s);
881 }
882
883 static char *read_line_from_git_path(const char *filename)
884 {
885         struct strbuf buf = STRBUF_INIT;
886         FILE *fp = fopen(git_path("%s", filename), "r");
887         if (!fp) {
888                 strbuf_release(&buf);
889                 return NULL;
890         }
891         strbuf_getline(&buf, fp, '\n');
892         if (!fclose(fp)) {
893                 return strbuf_detach(&buf, NULL);
894         } else {
895                 strbuf_release(&buf);
896                 return NULL;
897         }
898 }
899
900 static int split_commit_in_progress(struct wt_status *s)
901 {
902         int split_in_progress = 0;
903         char *head = read_line_from_git_path("HEAD");
904         char *orig_head = read_line_from_git_path("ORIG_HEAD");
905         char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
906         char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
907
908         if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
909             !s->branch || strcmp(s->branch, "HEAD"))
910                 return split_in_progress;
911
912         if (!strcmp(rebase_amend, rebase_orig_head)) {
913                 if (strcmp(head, rebase_amend))
914                         split_in_progress = 1;
915         } else if (strcmp(orig_head, rebase_orig_head)) {
916                 split_in_progress = 1;
917         }
918
919         if (!s->amend && !s->nowarn && !s->workdir_dirty)
920                 split_in_progress = 0;
921
922         free(head);
923         free(orig_head);
924         free(rebase_amend);
925         free(rebase_orig_head);
926         return split_in_progress;
927 }
928
929 static void show_rebase_in_progress(struct wt_status *s,
930                                 struct wt_status_state *state,
931                                 const char *color)
932 {
933         struct stat st;
934
935         if (has_unmerged(s)) {
936                 if (state->branch)
937                         status_printf_ln(s, color,
938                                          _("You are currently rebasing branch '%s' on '%s'."),
939                                          state->branch,
940                                          state->onto);
941                 else
942                         status_printf_ln(s, color,
943                                          _("You are currently rebasing."));
944                 if (s->hints) {
945                         status_printf_ln(s, color,
946                                 _("  (fix conflicts and then run \"git rebase --continue\")"));
947                         status_printf_ln(s, color,
948                                 _("  (use \"git rebase --skip\" to skip this patch)"));
949                         status_printf_ln(s, color,
950                                 _("  (use \"git rebase --abort\" to check out the original branch)"));
951                 }
952         } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
953                 if (state->branch)
954                         status_printf_ln(s, color,
955                                          _("You are currently rebasing branch '%s' on '%s'."),
956                                          state->branch,
957                                          state->onto);
958                 else
959                         status_printf_ln(s, color,
960                                          _("You are currently rebasing."));
961                 if (s->hints)
962                         status_printf_ln(s, color,
963                                 _("  (all conflicts fixed: run \"git rebase --continue\")"));
964         } else if (split_commit_in_progress(s)) {
965                 if (state->branch)
966                         status_printf_ln(s, color,
967                                          _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
968                                          state->branch,
969                                          state->onto);
970                 else
971                         status_printf_ln(s, color,
972                                          _("You are currently splitting a commit during a rebase."));
973                 if (s->hints)
974                         status_printf_ln(s, color,
975                                 _("  (Once your working directory is clean, run \"git rebase --continue\")"));
976         } else {
977                 if (state->branch)
978                         status_printf_ln(s, color,
979                                          _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
980                                          state->branch,
981                                          state->onto);
982                 else
983                         status_printf_ln(s, color,
984                                          _("You are currently editing a commit during a rebase."));
985                 if (s->hints && !s->amend) {
986                         status_printf_ln(s, color,
987                                 _("  (use \"git commit --amend\" to amend the current commit)"));
988                         status_printf_ln(s, color,
989                                 _("  (use \"git rebase --continue\" once you are satisfied with your changes)"));
990                 }
991         }
992         wt_status_print_trailer(s);
993 }
994
995 static void show_cherry_pick_in_progress(struct wt_status *s,
996                                         struct wt_status_state *state,
997                                         const char *color)
998 {
999         status_printf_ln(s, color, _("You are currently cherry-picking."));
1000         if (s->hints) {
1001                 if (has_unmerged(s))
1002                         status_printf_ln(s, color,
1003                                 _("  (fix conflicts and run \"git cherry-pick --continue\")"));
1004                 else
1005                         status_printf_ln(s, color,
1006                                 _("  (all conflicts fixed: run \"git cherry-pick --continue\")"));
1007                 status_printf_ln(s, color,
1008                         _("  (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1009         }
1010         wt_status_print_trailer(s);
1011 }
1012
1013 static void show_revert_in_progress(struct wt_status *s,
1014                                         struct wt_status_state *state,
1015                                         const char *color)
1016 {
1017         status_printf_ln(s, color, _("You are currently reverting commit %s."),
1018                          find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
1019         if (s->hints) {
1020                 if (has_unmerged(s))
1021                         status_printf_ln(s, color,
1022                                 _("  (fix conflicts and run \"git revert --continue\")"));
1023                 else
1024                         status_printf_ln(s, color,
1025                                 _("  (all conflicts fixed: run \"git revert --continue\")"));
1026                 status_printf_ln(s, color,
1027                         _("  (use \"git revert --abort\" to cancel the revert operation)"));
1028         }
1029         wt_status_print_trailer(s);
1030 }
1031
1032 static void show_bisect_in_progress(struct wt_status *s,
1033                                 struct wt_status_state *state,
1034                                 const char *color)
1035 {
1036         if (state->branch)
1037                 status_printf_ln(s, color,
1038                                  _("You are currently bisecting, started from branch '%s'."),
1039                                  state->branch);
1040         else
1041                 status_printf_ln(s, color,
1042                                  _("You are currently bisecting."));
1043         if (s->hints)
1044                 status_printf_ln(s, color,
1045                         _("  (use \"git bisect reset\" to get back to the original branch)"));
1046         wt_status_print_trailer(s);
1047 }
1048
1049 /*
1050  * Extract branch information from rebase/bisect
1051  */
1052 static char *read_and_strip_branch(const char *path)
1053 {
1054         struct strbuf sb = STRBUF_INIT;
1055         unsigned char sha1[20];
1056
1057         if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1058                 goto got_nothing;
1059
1060         while (&sb.len && sb.buf[sb.len - 1] == '\n')
1061                 strbuf_setlen(&sb, sb.len - 1);
1062         if (!sb.len)
1063                 goto got_nothing;
1064         if (!prefixcmp(sb.buf, "refs/heads/"))
1065                 strbuf_remove(&sb,0, strlen("refs/heads/"));
1066         else if (!prefixcmp(sb.buf, "refs/"))
1067                 ;
1068         else if (!get_sha1_hex(sb.buf, sha1)) {
1069                 const char *abbrev;
1070                 abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
1071                 strbuf_reset(&sb);
1072                 strbuf_addstr(&sb, abbrev);
1073         } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1074                 goto got_nothing;
1075         else                    /* bisect */
1076                 ;
1077         return strbuf_detach(&sb, NULL);
1078
1079 got_nothing:
1080         strbuf_release(&sb);
1081         return NULL;
1082 }
1083
1084 struct grab_1st_switch_cbdata {
1085         struct strbuf buf;
1086         unsigned char nsha1[20];
1087 };
1088
1089 static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
1090                            const char *email, unsigned long timestamp, int tz,
1091                            const char *message, void *cb_data)
1092 {
1093         struct grab_1st_switch_cbdata *cb = cb_data;
1094         const char *target = NULL, *end;
1095
1096         if (prefixcmp(message, "checkout: moving from "))
1097                 return 0;
1098         message += strlen("checkout: moving from ");
1099         target = strstr(message, " to ");
1100         if (!target)
1101                 return 0;
1102         target += strlen(" to ");
1103         strbuf_reset(&cb->buf);
1104         hashcpy(cb->nsha1, nsha1);
1105         for (end = target; *end && *end != '\n'; end++)
1106                 ;
1107         strbuf_add(&cb->buf, target, end - target);
1108         return 1;
1109 }
1110
1111 static void wt_status_get_detached_from(struct wt_status_state *state)
1112 {
1113         struct grab_1st_switch_cbdata cb;
1114         struct commit *commit;
1115         unsigned char sha1[20];
1116         char *ref = NULL;
1117
1118         strbuf_init(&cb.buf, 0);
1119         if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1120                 strbuf_release(&cb.buf);
1121                 return;
1122         }
1123
1124         if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 &&
1125             /* sha1 is a commit? match without further lookup */
1126             (!hashcmp(cb.nsha1, sha1) ||
1127              /* perhaps sha1 is a tag, try to dereference to a commit */
1128              ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
1129               !hashcmp(cb.nsha1, commit->object.sha1)))) {
1130                 int ofs;
1131                 if (!prefixcmp(ref, "refs/tags/"))
1132                         ofs = strlen("refs/tags/");
1133                 else if (!prefixcmp(ref, "refs/remotes/"))
1134                         ofs = strlen("refs/remotes/");
1135                 else
1136                         ofs = 0;
1137                 state->detached_from = xstrdup(ref + ofs);
1138         } else
1139                 state->detached_from =
1140                         xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
1141         hashcpy(state->detached_sha1, cb.nsha1);
1142
1143         free(ref);
1144         strbuf_release(&cb.buf);
1145 }
1146
1147 void wt_status_get_state(struct wt_status_state *state,
1148                          int get_detached_from)
1149 {
1150         struct stat st;
1151         unsigned char sha1[20];
1152
1153         if (!stat(git_path("MERGE_HEAD"), &st)) {
1154                 state->merge_in_progress = 1;
1155         } else if (!stat(git_path("rebase-apply"), &st)) {
1156                 if (!stat(git_path("rebase-apply/applying"), &st)) {
1157                         state->am_in_progress = 1;
1158                         if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1159                                 state->am_empty_patch = 1;
1160                 } else {
1161                         state->rebase_in_progress = 1;
1162                         state->branch = read_and_strip_branch("rebase-apply/head-name");
1163                         state->onto = read_and_strip_branch("rebase-apply/onto");
1164                 }
1165         } else if (!stat(git_path("rebase-merge"), &st)) {
1166                 if (!stat(git_path("rebase-merge/interactive"), &st))
1167                         state->rebase_interactive_in_progress = 1;
1168                 else
1169                         state->rebase_in_progress = 1;
1170                 state->branch = read_and_strip_branch("rebase-merge/head-name");
1171                 state->onto = read_and_strip_branch("rebase-merge/onto");
1172         } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1173                 state->cherry_pick_in_progress = 1;
1174         }
1175         if (!stat(git_path("BISECT_LOG"), &st)) {
1176                 state->bisect_in_progress = 1;
1177                 state->branch = read_and_strip_branch("BISECT_START");
1178         }
1179         if (!stat(git_path("REVERT_HEAD"), &st) &&
1180             !get_sha1("REVERT_HEAD", sha1)) {
1181                 state->revert_in_progress = 1;
1182                 hashcpy(state->revert_head_sha1, sha1);
1183         }
1184
1185         if (get_detached_from)
1186                 wt_status_get_detached_from(state);
1187 }
1188
1189 static void wt_status_print_state(struct wt_status *s,
1190                                   struct wt_status_state *state)
1191 {
1192         const char *state_color = color(WT_STATUS_HEADER, s);
1193         if (state->merge_in_progress)
1194                 show_merge_in_progress(s, state, state_color);
1195         else if (state->am_in_progress)
1196                 show_am_in_progress(s, state, state_color);
1197         else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1198                 show_rebase_in_progress(s, state, state_color);
1199         else if (state->cherry_pick_in_progress)
1200                 show_cherry_pick_in_progress(s, state, state_color);
1201         else if (state->revert_in_progress)
1202                 show_revert_in_progress(s, state, state_color);
1203         if (state->bisect_in_progress)
1204                 show_bisect_in_progress(s, state, state_color);
1205 }
1206
1207 void wt_status_print(struct wt_status *s)
1208 {
1209         const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1210         const char *branch_status_color = color(WT_STATUS_HEADER, s);
1211         struct wt_status_state state;
1212
1213         memset(&state, 0, sizeof(state));
1214         wt_status_get_state(&state,
1215                             s->branch && !strcmp(s->branch, "HEAD"));
1216
1217         if (s->branch) {
1218                 const char *on_what = _("On branch ");
1219                 const char *branch_name = s->branch;
1220                 if (!prefixcmp(branch_name, "refs/heads/"))
1221                         branch_name += 11;
1222                 else if (!strcmp(branch_name, "HEAD")) {
1223                         branch_status_color = color(WT_STATUS_NOBRANCH, s);
1224                         if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1225                                 on_what = _("rebase in progress; onto ");
1226                                 branch_name = state.onto;
1227                         } else if (state.detached_from) {
1228                                 unsigned char sha1[20];
1229                                 branch_name = state.detached_from;
1230                                 if (!get_sha1("HEAD", sha1) &&
1231                                     !hashcmp(sha1, state.detached_sha1))
1232                                         on_what = _("HEAD detached at ");
1233                                 else
1234                                         on_what = _("HEAD detached from ");
1235                         } else {
1236                                 branch_name = "";
1237                                 on_what = _("Not currently on any branch.");
1238                         }
1239                 }
1240                 status_printf(s, color(WT_STATUS_HEADER, s), "");
1241                 status_printf_more(s, branch_status_color, "%s", on_what);
1242                 status_printf_more(s, branch_color, "%s\n", branch_name);
1243                 if (!s->is_initial)
1244                         wt_status_print_tracking(s);
1245         }
1246
1247         wt_status_print_state(s, &state);
1248         free(state.branch);
1249         free(state.onto);
1250         free(state.detached_from);
1251
1252         if (s->is_initial) {
1253                 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1254                 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1255                 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1256         }
1257
1258         wt_status_print_updated(s);
1259         wt_status_print_unmerged(s);
1260         wt_status_print_changed(s);
1261         if (s->submodule_summary &&
1262             (!s->ignore_submodule_arg ||
1263              strcmp(s->ignore_submodule_arg, "all"))) {
1264                 wt_status_print_submodule_summary(s, 0);  /* staged */
1265                 wt_status_print_submodule_summary(s, 1);  /* unstaged */
1266         }
1267         if (s->show_untracked_files) {
1268                 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1269                 if (s->show_ignored_files)
1270                         wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1271                 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1272                         status_printf_ln(s, GIT_COLOR_NORMAL, "");
1273                         status_printf_ln(s, GIT_COLOR_NORMAL,
1274                                          _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1275                                            "may speed it up, but you have to be careful not to forget to add\n"
1276                                            "new files yourself (see 'git help status')."),
1277                                          s->untracked_in_ms / 1000.0);
1278                 }
1279         } else if (s->commitable)
1280                 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1281                         s->hints
1282                         ? _(" (use -u option to show untracked files)") : "");
1283
1284         if (s->verbose)
1285                 wt_status_print_verbose(s);
1286         if (!s->commitable) {
1287                 if (s->amend)
1288                         status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1289                 else if (s->nowarn)
1290                         ; /* nothing */
1291                 else if (s->workdir_dirty) {
1292                         if (s->hints)
1293                                 printf(_("no changes added to commit "
1294                                          "(use \"git add\" and/or \"git commit -a\")\n"));
1295                         else
1296                                 printf(_("no changes added to commit\n"));
1297                 } else if (s->untracked.nr) {
1298                         if (s->hints)
1299                                 printf(_("nothing added to commit but untracked files "
1300                                          "present (use \"git add\" to track)\n"));
1301                         else
1302                                 printf(_("nothing added to commit but untracked files present\n"));
1303                 } else if (s->is_initial) {
1304                         if (s->hints)
1305                                 printf(_("nothing to commit (create/copy files "
1306                                          "and use \"git add\" to track)\n"));
1307                         else
1308                                 printf(_("nothing to commit\n"));
1309                 } else if (!s->show_untracked_files) {
1310                         if (s->hints)
1311                                 printf(_("nothing to commit (use -u to show untracked files)\n"));
1312                         else
1313                                 printf(_("nothing to commit\n"));
1314                 } else
1315                         printf(_("nothing to commit, working directory clean\n"));
1316         }
1317 }
1318
1319 static void wt_shortstatus_unmerged(struct string_list_item *it,
1320                            struct wt_status *s)
1321 {
1322         struct wt_status_change_data *d = it->util;
1323         const char *how = "??";
1324
1325         switch (d->stagemask) {
1326         case 1: how = "DD"; break; /* both deleted */
1327         case 2: how = "AU"; break; /* added by us */
1328         case 3: how = "UD"; break; /* deleted by them */
1329         case 4: how = "UA"; break; /* added by them */
1330         case 5: how = "DU"; break; /* deleted by us */
1331         case 6: how = "AA"; break; /* both added */
1332         case 7: how = "UU"; break; /* both modified */
1333         }
1334         color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1335         if (s->null_termination) {
1336                 fprintf(stdout, " %s%c", it->string, 0);
1337         } else {
1338                 struct strbuf onebuf = STRBUF_INIT;
1339                 const char *one;
1340                 one = quote_path(it->string, s->prefix, &onebuf);
1341                 printf(" %s\n", one);
1342                 strbuf_release(&onebuf);
1343         }
1344 }
1345
1346 static void wt_shortstatus_status(struct string_list_item *it,
1347                          struct wt_status *s)
1348 {
1349         struct wt_status_change_data *d = it->util;
1350
1351         if (d->index_status)
1352                 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1353         else
1354                 putchar(' ');
1355         if (d->worktree_status)
1356                 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1357         else
1358                 putchar(' ');
1359         putchar(' ');
1360         if (s->null_termination) {
1361                 fprintf(stdout, "%s%c", it->string, 0);
1362                 if (d->head_path)
1363                         fprintf(stdout, "%s%c", d->head_path, 0);
1364         } else {
1365                 struct strbuf onebuf = STRBUF_INIT;
1366                 const char *one;
1367                 if (d->head_path) {
1368                         one = quote_path(d->head_path, s->prefix, &onebuf);
1369                         if (*one != '"' && strchr(one, ' ') != NULL) {
1370                                 putchar('"');
1371                                 strbuf_addch(&onebuf, '"');
1372                                 one = onebuf.buf;
1373                         }
1374                         printf("%s -> ", one);
1375                         strbuf_release(&onebuf);
1376                 }
1377                 one = quote_path(it->string, s->prefix, &onebuf);
1378                 if (*one != '"' && strchr(one, ' ') != NULL) {
1379                         putchar('"');
1380                         strbuf_addch(&onebuf, '"');
1381                         one = onebuf.buf;
1382                 }
1383                 printf("%s\n", one);
1384                 strbuf_release(&onebuf);
1385         }
1386 }
1387
1388 static void wt_shortstatus_other(struct string_list_item *it,
1389                                  struct wt_status *s, const char *sign)
1390 {
1391         if (s->null_termination) {
1392                 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1393         } else {
1394                 struct strbuf onebuf = STRBUF_INIT;
1395                 const char *one;
1396                 one = quote_path(it->string, s->prefix, &onebuf);
1397                 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1398                 printf(" %s\n", one);
1399                 strbuf_release(&onebuf);
1400         }
1401 }
1402
1403 static void wt_shortstatus_print_tracking(struct wt_status *s)
1404 {
1405         struct branch *branch;
1406         const char *header_color = color(WT_STATUS_HEADER, s);
1407         const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1408         const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1409
1410         const char *base;
1411         const char *branch_name;
1412         int num_ours, num_theirs;
1413         int upstream_is_gone = 0;
1414
1415         color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1416
1417         if (!s->branch)
1418                 return;
1419         branch_name = s->branch;
1420
1421         if (!prefixcmp(branch_name, "refs/heads/"))
1422                 branch_name += 11;
1423         else if (!strcmp(branch_name, "HEAD")) {
1424                 branch_name = _("HEAD (no branch)");
1425                 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1426         }
1427
1428         branch = branch_get(s->branch + 11);
1429         if (s->is_initial)
1430                 color_fprintf(s->fp, header_color, _("Initial commit on "));
1431
1432         color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1433
1434         switch (stat_tracking_info(branch, &num_ours, &num_theirs)) {
1435         case 0:
1436                 /* no base */
1437                 fputc(s->null_termination ? '\0' : '\n', s->fp);
1438                 return;
1439         case -1:
1440                 /* with "gone" base */
1441                 upstream_is_gone = 1;
1442                 break;
1443         default:
1444                 /* with base */
1445                 break;
1446         }
1447
1448         base = branch->merge[0]->dst;
1449         base = shorten_unambiguous_ref(base, 0);
1450         color_fprintf(s->fp, header_color, "...");
1451         color_fprintf(s->fp, branch_color_remote, "%s", base);
1452
1453         if (!upstream_is_gone && !num_ours && !num_theirs) {
1454                 fputc(s->null_termination ? '\0' : '\n', s->fp);
1455                 return;
1456         }
1457
1458         color_fprintf(s->fp, header_color, " [");
1459         if (upstream_is_gone) {
1460                 color_fprintf(s->fp, header_color, _("gone"));
1461         } else if (!num_ours) {
1462                 color_fprintf(s->fp, header_color, _("behind "));
1463                 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1464         } else if (!num_theirs) {
1465                 color_fprintf(s->fp, header_color, _("ahead "));
1466                 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1467         } else {
1468                 color_fprintf(s->fp, header_color, _("ahead "));
1469                 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1470                 color_fprintf(s->fp, header_color, _(", behind "));
1471                 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1472         }
1473
1474         color_fprintf(s->fp, header_color, "]");
1475         fputc(s->null_termination ? '\0' : '\n', s->fp);
1476 }
1477
1478 void wt_shortstatus_print(struct wt_status *s)
1479 {
1480         int i;
1481
1482         if (s->show_branch)
1483                 wt_shortstatus_print_tracking(s);
1484
1485         for (i = 0; i < s->change.nr; i++) {
1486                 struct wt_status_change_data *d;
1487                 struct string_list_item *it;
1488
1489                 it = &(s->change.items[i]);
1490                 d = it->util;
1491                 if (d->stagemask)
1492                         wt_shortstatus_unmerged(it, s);
1493                 else
1494                         wt_shortstatus_status(it, s);
1495         }
1496         for (i = 0; i < s->untracked.nr; i++) {
1497                 struct string_list_item *it;
1498
1499                 it = &(s->untracked.items[i]);
1500                 wt_shortstatus_other(it, s, "??");
1501         }
1502         for (i = 0; i < s->ignored.nr; i++) {
1503                 struct string_list_item *it;
1504
1505                 it = &(s->ignored.items[i]);
1506                 wt_shortstatus_other(it, s, "!!");
1507         }
1508 }
1509
1510 void wt_porcelain_print(struct wt_status *s)
1511 {
1512         s->use_color = 0;
1513         s->relative_paths = 0;
1514         s->prefix = NULL;
1515         wt_shortstatus_print(s);
1516 }