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