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