i18n: make the translation of -u advice in one go
[git/git-svn.git] / wt-status.c
blobbd1da378071f34139effb879be5fcf1c44198508
1 #include "cache.h"
2 #include "wt-status.h"
3 #include "object.h"
4 #include "dir.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "diffcore.h"
9 #include "quote.h"
10 #include "run-command.h"
11 #include "remote.h"
12 #include "refs.h"
13 #include "submodule.h"
14 #include "column.h"
15 #include "strbuf.h"
17 static char default_wt_status_colors[][COLOR_MAXLEN] = {
18 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
23 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
24 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
25 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
26 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
29 static const char *color(int slot, struct wt_status *s)
31 const char *c = "";
32 if (want_color(s->use_color))
33 c = s->color_palette[slot];
34 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
35 c = s->color_palette[WT_STATUS_HEADER];
36 return c;
39 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
40 const char *fmt, va_list ap, const char *trail)
42 struct strbuf sb = STRBUF_INIT;
43 struct strbuf linebuf = STRBUF_INIT;
44 const char *line, *eol;
46 strbuf_vaddf(&sb, fmt, ap);
47 if (!sb.len) {
48 strbuf_addch(&sb, '#');
49 if (!trail)
50 strbuf_addch(&sb, ' ');
51 color_print_strbuf(s->fp, color, &sb);
52 if (trail)
53 fprintf(s->fp, "%s", trail);
54 strbuf_release(&sb);
55 return;
57 for (line = sb.buf; *line; line = eol + 1) {
58 eol = strchr(line, '\n');
60 strbuf_reset(&linebuf);
61 if (at_bol) {
62 strbuf_addch(&linebuf, '#');
63 if (*line != '\n' && *line != '\t')
64 strbuf_addch(&linebuf, ' ');
66 if (eol)
67 strbuf_add(&linebuf, line, eol - line);
68 else
69 strbuf_addstr(&linebuf, line);
70 color_print_strbuf(s->fp, color, &linebuf);
71 if (eol)
72 fprintf(s->fp, "\n");
73 else
74 break;
75 at_bol = 1;
77 if (trail)
78 fprintf(s->fp, "%s", trail);
79 strbuf_release(&linebuf);
80 strbuf_release(&sb);
83 void status_printf_ln(struct wt_status *s, const char *color,
84 const char *fmt, ...)
86 va_list ap;
88 va_start(ap, fmt);
89 status_vprintf(s, 1, color, fmt, ap, "\n");
90 va_end(ap);
93 void status_printf(struct wt_status *s, const char *color,
94 const char *fmt, ...)
96 va_list ap;
98 va_start(ap, fmt);
99 status_vprintf(s, 1, color, fmt, ap, NULL);
100 va_end(ap);
103 static void status_printf_more(struct wt_status *s, const char *color,
104 const char *fmt, ...)
106 va_list ap;
108 va_start(ap, fmt);
109 status_vprintf(s, 0, color, fmt, ap, NULL);
110 va_end(ap);
113 void wt_status_prepare(struct wt_status *s)
115 unsigned char sha1[20];
117 memset(s, 0, sizeof(*s));
118 memcpy(s->color_palette, default_wt_status_colors,
119 sizeof(default_wt_status_colors));
120 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
121 s->use_color = -1;
122 s->relative_paths = 1;
123 s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
124 s->reference = "HEAD";
125 s->fp = stdout;
126 s->index_file = get_index_file();
127 s->change.strdup_strings = 1;
128 s->untracked.strdup_strings = 1;
129 s->ignored.strdup_strings = 1;
132 static void wt_status_print_unmerged_header(struct wt_status *s)
134 int i;
135 int del_mod_conflict = 0;
136 int both_deleted = 0;
137 int not_deleted = 0;
138 const char *c = color(WT_STATUS_HEADER, s);
140 status_printf_ln(s, c, _("Unmerged paths:"));
142 for (i = 0; i < s->change.nr; i++) {
143 struct string_list_item *it = &(s->change.items[i]);
144 struct wt_status_change_data *d = it->util;
146 switch (d->stagemask) {
147 case 0:
148 break;
149 case 1:
150 both_deleted = 1;
151 break;
152 case 3:
153 case 5:
154 del_mod_conflict = 1;
155 break;
156 default:
157 not_deleted = 1;
158 break;
162 if (!advice_status_hints)
163 return;
164 if (s->whence != FROM_COMMIT)
166 else if (!s->is_initial)
167 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
168 else
169 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
171 if (!both_deleted) {
172 if (!del_mod_conflict)
173 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
174 else
175 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
176 } else if (!del_mod_conflict && !not_deleted) {
177 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
178 } else {
179 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
181 status_printf_ln(s, c, "");
184 static void wt_status_print_cached_header(struct wt_status *s)
186 const char *c = color(WT_STATUS_HEADER, s);
188 status_printf_ln(s, c, _("Changes to be committed:"));
189 if (!advice_status_hints)
190 return;
191 if (s->whence != FROM_COMMIT)
192 ; /* NEEDSWORK: use "git reset --unresolve"??? */
193 else if (!s->is_initial)
194 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
195 else
196 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
197 status_printf_ln(s, c, "");
200 static void wt_status_print_dirty_header(struct wt_status *s,
201 int has_deleted,
202 int has_dirty_submodules)
204 const char *c = color(WT_STATUS_HEADER, s);
206 status_printf_ln(s, c, _("Changes not staged for commit:"));
207 if (!advice_status_hints)
208 return;
209 if (!has_deleted)
210 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
211 else
212 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
213 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
214 if (has_dirty_submodules)
215 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
216 status_printf_ln(s, c, "");
219 static void wt_status_print_other_header(struct wt_status *s,
220 const char *what,
221 const char *how)
223 const char *c = color(WT_STATUS_HEADER, s);
224 status_printf_ln(s, c, "%s:", what);
225 if (!advice_status_hints)
226 return;
227 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
228 status_printf_ln(s, c, "");
231 static void wt_status_print_trailer(struct wt_status *s)
233 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
236 #define quote_path quote_path_relative
238 static void wt_status_print_unmerged_data(struct wt_status *s,
239 struct string_list_item *it)
241 const char *c = color(WT_STATUS_UNMERGED, s);
242 struct wt_status_change_data *d = it->util;
243 struct strbuf onebuf = STRBUF_INIT;
244 const char *one, *how = _("bug");
246 one = quote_path(it->string, -1, &onebuf, s->prefix);
247 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
248 switch (d->stagemask) {
249 case 1: how = _("both deleted:"); break;
250 case 2: how = _("added by us:"); break;
251 case 3: how = _("deleted by them:"); break;
252 case 4: how = _("added by them:"); break;
253 case 5: how = _("deleted by us:"); break;
254 case 6: how = _("both added:"); break;
255 case 7: how = _("both modified:"); break;
257 status_printf_more(s, c, "%-20s%s\n", how, one);
258 strbuf_release(&onebuf);
261 static void wt_status_print_change_data(struct wt_status *s,
262 int change_type,
263 struct string_list_item *it)
265 struct wt_status_change_data *d = it->util;
266 const char *c = color(change_type, s);
267 int status = status;
268 char *one_name;
269 char *two_name;
270 const char *one, *two;
271 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
272 struct strbuf extra = STRBUF_INIT;
274 one_name = two_name = it->string;
275 switch (change_type) {
276 case WT_STATUS_UPDATED:
277 status = d->index_status;
278 if (d->head_path)
279 one_name = d->head_path;
280 break;
281 case WT_STATUS_CHANGED:
282 if (d->new_submodule_commits || d->dirty_submodule) {
283 strbuf_addstr(&extra, " (");
284 if (d->new_submodule_commits)
285 strbuf_addf(&extra, _("new commits, "));
286 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
287 strbuf_addf(&extra, _("modified content, "));
288 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
289 strbuf_addf(&extra, _("untracked content, "));
290 strbuf_setlen(&extra, extra.len - 2);
291 strbuf_addch(&extra, ')');
293 status = d->worktree_status;
294 break;
297 one = quote_path(one_name, -1, &onebuf, s->prefix);
298 two = quote_path(two_name, -1, &twobuf, s->prefix);
300 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
301 switch (status) {
302 case DIFF_STATUS_ADDED:
303 status_printf_more(s, c, _("new file: %s"), one);
304 break;
305 case DIFF_STATUS_COPIED:
306 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
307 break;
308 case DIFF_STATUS_DELETED:
309 status_printf_more(s, c, _("deleted: %s"), one);
310 break;
311 case DIFF_STATUS_MODIFIED:
312 status_printf_more(s, c, _("modified: %s"), one);
313 break;
314 case DIFF_STATUS_RENAMED:
315 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
316 break;
317 case DIFF_STATUS_TYPE_CHANGED:
318 status_printf_more(s, c, _("typechange: %s"), one);
319 break;
320 case DIFF_STATUS_UNKNOWN:
321 status_printf_more(s, c, _("unknown: %s"), one);
322 break;
323 case DIFF_STATUS_UNMERGED:
324 status_printf_more(s, c, _("unmerged: %s"), one);
325 break;
326 default:
327 die(_("bug: unhandled diff status %c"), status);
329 if (extra.len) {
330 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
331 strbuf_release(&extra);
333 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
334 strbuf_release(&onebuf);
335 strbuf_release(&twobuf);
338 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
339 struct diff_options *options,
340 void *data)
342 struct wt_status *s = data;
343 int i;
345 if (!q->nr)
346 return;
347 s->workdir_dirty = 1;
348 for (i = 0; i < q->nr; i++) {
349 struct diff_filepair *p;
350 struct string_list_item *it;
351 struct wt_status_change_data *d;
353 p = q->queue[i];
354 it = string_list_insert(&s->change, p->one->path);
355 d = it->util;
356 if (!d) {
357 d = xcalloc(1, sizeof(*d));
358 it->util = d;
360 if (!d->worktree_status)
361 d->worktree_status = p->status;
362 d->dirty_submodule = p->two->dirty_submodule;
363 if (S_ISGITLINK(p->two->mode))
364 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
368 static int unmerged_mask(const char *path)
370 int pos, mask;
371 struct cache_entry *ce;
373 pos = cache_name_pos(path, strlen(path));
374 if (0 <= pos)
375 return 0;
377 mask = 0;
378 pos = -pos-1;
379 while (pos < active_nr) {
380 ce = active_cache[pos++];
381 if (strcmp(ce->name, path) || !ce_stage(ce))
382 break;
383 mask |= (1 << (ce_stage(ce) - 1));
385 return mask;
388 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
389 struct diff_options *options,
390 void *data)
392 struct wt_status *s = data;
393 int i;
395 for (i = 0; i < q->nr; i++) {
396 struct diff_filepair *p;
397 struct string_list_item *it;
398 struct wt_status_change_data *d;
400 p = q->queue[i];
401 it = string_list_insert(&s->change, p->two->path);
402 d = it->util;
403 if (!d) {
404 d = xcalloc(1, sizeof(*d));
405 it->util = d;
407 if (!d->index_status)
408 d->index_status = p->status;
409 switch (p->status) {
410 case DIFF_STATUS_COPIED:
411 case DIFF_STATUS_RENAMED:
412 d->head_path = xstrdup(p->one->path);
413 break;
414 case DIFF_STATUS_UNMERGED:
415 d->stagemask = unmerged_mask(p->two->path);
416 break;
421 static void wt_status_collect_changes_worktree(struct wt_status *s)
423 struct rev_info rev;
425 init_revisions(&rev, NULL);
426 setup_revisions(0, NULL, &rev, NULL);
427 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
428 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
429 if (!s->show_untracked_files)
430 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
431 if (s->ignore_submodule_arg) {
432 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
433 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
435 rev.diffopt.format_callback = wt_status_collect_changed_cb;
436 rev.diffopt.format_callback_data = s;
437 init_pathspec(&rev.prune_data, s->pathspec);
438 run_diff_files(&rev, 0);
441 static void wt_status_collect_changes_index(struct wt_status *s)
443 struct rev_info rev;
444 struct setup_revision_opt opt;
446 init_revisions(&rev, NULL);
447 memset(&opt, 0, sizeof(opt));
448 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
449 setup_revisions(0, NULL, &rev, &opt);
451 if (s->ignore_submodule_arg) {
452 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
453 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
456 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
457 rev.diffopt.format_callback = wt_status_collect_updated_cb;
458 rev.diffopt.format_callback_data = s;
459 rev.diffopt.detect_rename = 1;
460 rev.diffopt.rename_limit = 200;
461 rev.diffopt.break_opt = 0;
462 init_pathspec(&rev.prune_data, s->pathspec);
463 run_diff_index(&rev, 1);
466 static void wt_status_collect_changes_initial(struct wt_status *s)
468 struct pathspec pathspec;
469 int i;
471 init_pathspec(&pathspec, s->pathspec);
472 for (i = 0; i < active_nr; i++) {
473 struct string_list_item *it;
474 struct wt_status_change_data *d;
475 struct cache_entry *ce = active_cache[i];
477 if (!ce_path_match(ce, &pathspec))
478 continue;
479 it = string_list_insert(&s->change, ce->name);
480 d = it->util;
481 if (!d) {
482 d = xcalloc(1, sizeof(*d));
483 it->util = d;
485 if (ce_stage(ce)) {
486 d->index_status = DIFF_STATUS_UNMERGED;
487 d->stagemask |= (1 << (ce_stage(ce) - 1));
489 else
490 d->index_status = DIFF_STATUS_ADDED;
492 free_pathspec(&pathspec);
495 static void wt_status_collect_untracked(struct wt_status *s)
497 int i;
498 struct dir_struct dir;
499 struct timeval t_begin;
501 if (!s->show_untracked_files)
502 return;
504 if (advice_status_u_option)
505 gettimeofday(&t_begin, NULL);
507 memset(&dir, 0, sizeof(dir));
508 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
509 dir.flags |=
510 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
511 setup_standard_excludes(&dir);
513 fill_directory(&dir, s->pathspec);
514 for (i = 0; i < dir.nr; i++) {
515 struct dir_entry *ent = dir.entries[i];
516 if (cache_name_is_other(ent->name, ent->len) &&
517 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
518 string_list_insert(&s->untracked, ent->name);
519 free(ent);
522 if (s->show_ignored_files) {
523 dir.nr = 0;
524 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
525 fill_directory(&dir, s->pathspec);
526 for (i = 0; i < dir.nr; i++) {
527 struct dir_entry *ent = dir.entries[i];
528 if (cache_name_is_other(ent->name, ent->len) &&
529 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
530 string_list_insert(&s->ignored, ent->name);
531 free(ent);
535 free(dir.entries);
537 if (advice_status_u_option) {
538 struct timeval t_end;
539 gettimeofday(&t_end, NULL);
540 s->untracked_in_ms =
541 (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
542 ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
546 void wt_status_collect(struct wt_status *s)
548 wt_status_collect_changes_worktree(s);
550 if (s->is_initial)
551 wt_status_collect_changes_initial(s);
552 else
553 wt_status_collect_changes_index(s);
554 wt_status_collect_untracked(s);
557 static void wt_status_print_unmerged(struct wt_status *s)
559 int shown_header = 0;
560 int i;
562 for (i = 0; i < s->change.nr; i++) {
563 struct wt_status_change_data *d;
564 struct string_list_item *it;
565 it = &(s->change.items[i]);
566 d = it->util;
567 if (!d->stagemask)
568 continue;
569 if (!shown_header) {
570 wt_status_print_unmerged_header(s);
571 shown_header = 1;
573 wt_status_print_unmerged_data(s, it);
575 if (shown_header)
576 wt_status_print_trailer(s);
580 static void wt_status_print_updated(struct wt_status *s)
582 int shown_header = 0;
583 int i;
585 for (i = 0; i < s->change.nr; i++) {
586 struct wt_status_change_data *d;
587 struct string_list_item *it;
588 it = &(s->change.items[i]);
589 d = it->util;
590 if (!d->index_status ||
591 d->index_status == DIFF_STATUS_UNMERGED)
592 continue;
593 if (!shown_header) {
594 wt_status_print_cached_header(s);
595 s->commitable = 1;
596 shown_header = 1;
598 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
600 if (shown_header)
601 wt_status_print_trailer(s);
605 * -1 : has delete
606 * 0 : no change
607 * 1 : some change but no delete
609 static int wt_status_check_worktree_changes(struct wt_status *s,
610 int *dirty_submodules)
612 int i;
613 int changes = 0;
615 *dirty_submodules = 0;
617 for (i = 0; i < s->change.nr; i++) {
618 struct wt_status_change_data *d;
619 d = s->change.items[i].util;
620 if (!d->worktree_status ||
621 d->worktree_status == DIFF_STATUS_UNMERGED)
622 continue;
623 if (!changes)
624 changes = 1;
625 if (d->dirty_submodule)
626 *dirty_submodules = 1;
627 if (d->worktree_status == DIFF_STATUS_DELETED)
628 changes = -1;
630 return changes;
633 static void wt_status_print_changed(struct wt_status *s)
635 int i, dirty_submodules;
636 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
638 if (!worktree_changes)
639 return;
641 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
643 for (i = 0; i < s->change.nr; i++) {
644 struct wt_status_change_data *d;
645 struct string_list_item *it;
646 it = &(s->change.items[i]);
647 d = it->util;
648 if (!d->worktree_status ||
649 d->worktree_status == DIFF_STATUS_UNMERGED)
650 continue;
651 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
653 wt_status_print_trailer(s);
656 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
658 struct child_process sm_summary;
659 char summary_limit[64];
660 char index[PATH_MAX];
661 const char *env[] = { NULL, NULL };
662 const char *argv[8];
664 env[0] = index;
665 argv[0] = "submodule";
666 argv[1] = "summary";
667 argv[2] = uncommitted ? "--files" : "--cached";
668 argv[3] = "--for-status";
669 argv[4] = "--summary-limit";
670 argv[5] = summary_limit;
671 argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
672 argv[7] = NULL;
674 sprintf(summary_limit, "%d", s->submodule_summary);
675 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
677 memset(&sm_summary, 0, sizeof(sm_summary));
678 sm_summary.argv = argv;
679 sm_summary.env = env;
680 sm_summary.git_cmd = 1;
681 sm_summary.no_stdin = 1;
682 fflush(s->fp);
683 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
684 run_command(&sm_summary);
687 static void wt_status_print_other(struct wt_status *s,
688 struct string_list *l,
689 const char *what,
690 const char *how)
692 int i;
693 struct strbuf buf = STRBUF_INIT;
694 static struct string_list output = STRING_LIST_INIT_DUP;
695 struct column_options copts;
697 if (!l->nr)
698 return;
700 wt_status_print_other_header(s, what, how);
702 for (i = 0; i < l->nr; i++) {
703 struct string_list_item *it;
704 const char *path;
705 it = &(l->items[i]);
706 path = quote_path(it->string, strlen(it->string),
707 &buf, s->prefix);
708 if (column_active(s->colopts)) {
709 string_list_append(&output, path);
710 continue;
712 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
713 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
714 "%s\n", path);
717 strbuf_release(&buf);
718 if (!column_active(s->colopts))
719 return;
721 strbuf_addf(&buf, "%s#\t%s",
722 color(WT_STATUS_HEADER, s),
723 color(WT_STATUS_UNTRACKED, s));
724 memset(&copts, 0, sizeof(copts));
725 copts.padding = 1;
726 copts.indent = buf.buf;
727 if (want_color(s->use_color))
728 copts.nl = GIT_COLOR_RESET "\n";
729 print_columns(&output, s->colopts, &copts);
730 string_list_clear(&output, 0);
731 strbuf_release(&buf);
734 static void wt_status_print_verbose(struct wt_status *s)
736 struct rev_info rev;
737 struct setup_revision_opt opt;
739 init_revisions(&rev, NULL);
740 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
742 memset(&opt, 0, sizeof(opt));
743 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
744 setup_revisions(0, NULL, &rev, &opt);
746 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
747 rev.diffopt.detect_rename = 1;
748 rev.diffopt.file = s->fp;
749 rev.diffopt.close_file = 0;
751 * If we're not going to stdout, then we definitely don't
752 * want color, since we are going to the commit message
753 * file (and even the "auto" setting won't work, since it
754 * will have checked isatty on stdout).
756 if (s->fp != stdout)
757 rev.diffopt.use_color = 0;
758 run_diff_index(&rev, 1);
761 static void wt_status_print_tracking(struct wt_status *s)
763 struct strbuf sb = STRBUF_INIT;
764 const char *cp, *ep;
765 struct branch *branch;
767 assert(s->branch && !s->is_initial);
768 if (prefixcmp(s->branch, "refs/heads/"))
769 return;
770 branch = branch_get(s->branch + 11);
771 if (!format_tracking_info(branch, &sb))
772 return;
774 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
775 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
776 "# %.*s", (int)(ep - cp), cp);
777 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
780 static int has_unmerged(struct wt_status *s)
782 int i;
784 for (i = 0; i < s->change.nr; i++) {
785 struct wt_status_change_data *d;
786 d = s->change.items[i].util;
787 if (d->stagemask)
788 return 1;
790 return 0;
793 static void show_merge_in_progress(struct wt_status *s,
794 struct wt_status_state *state,
795 const char *color)
797 if (has_unmerged(s)) {
798 status_printf_ln(s, color, _("You have unmerged paths."));
799 if (advice_status_hints)
800 status_printf_ln(s, color,
801 _(" (fix conflicts and run \"git commit\")"));
802 } else {
803 status_printf_ln(s, color,
804 _("All conflicts fixed but you are still merging."));
805 if (advice_status_hints)
806 status_printf_ln(s, color,
807 _(" (use \"git commit\" to conclude merge)"));
809 wt_status_print_trailer(s);
812 static void show_am_in_progress(struct wt_status *s,
813 struct wt_status_state *state,
814 const char *color)
816 status_printf_ln(s, color,
817 _("You are in the middle of an am session."));
818 if (state->am_empty_patch)
819 status_printf_ln(s, color,
820 _("The current patch is empty."));
821 if (advice_status_hints) {
822 if (!state->am_empty_patch)
823 status_printf_ln(s, color,
824 _(" (fix conflicts and then run \"git am --resolved\")"));
825 status_printf_ln(s, color,
826 _(" (use \"git am --skip\" to skip this patch)"));
827 status_printf_ln(s, color,
828 _(" (use \"git am --abort\" to restore the original branch)"));
830 wt_status_print_trailer(s);
833 static char *read_line_from_git_path(const char *filename)
835 struct strbuf buf = STRBUF_INIT;
836 FILE *fp = fopen(git_path("%s", filename), "r");
837 if (!fp) {
838 strbuf_release(&buf);
839 return NULL;
841 strbuf_getline(&buf, fp, '\n');
842 if (!fclose(fp)) {
843 return strbuf_detach(&buf, NULL);
844 } else {
845 strbuf_release(&buf);
846 return NULL;
850 static int split_commit_in_progress(struct wt_status *s)
852 int split_in_progress = 0;
853 char *head = read_line_from_git_path("HEAD");
854 char *orig_head = read_line_from_git_path("ORIG_HEAD");
855 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
856 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
858 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
859 !s->branch || strcmp(s->branch, "HEAD"))
860 return split_in_progress;
862 if (!strcmp(rebase_amend, rebase_orig_head)) {
863 if (strcmp(head, rebase_amend))
864 split_in_progress = 1;
865 } else if (strcmp(orig_head, rebase_orig_head)) {
866 split_in_progress = 1;
869 if (!s->amend && !s->nowarn && !s->workdir_dirty)
870 split_in_progress = 0;
872 free(head);
873 free(orig_head);
874 free(rebase_amend);
875 free(rebase_orig_head);
876 return split_in_progress;
879 static void show_rebase_in_progress(struct wt_status *s,
880 struct wt_status_state *state,
881 const char *color)
883 struct stat st;
885 if (has_unmerged(s)) {
886 status_printf_ln(s, color, _("You are currently rebasing."));
887 if (advice_status_hints) {
888 status_printf_ln(s, color,
889 _(" (fix conflicts and then run \"git rebase --continue\")"));
890 status_printf_ln(s, color,
891 _(" (use \"git rebase --skip\" to skip this patch)"));
892 status_printf_ln(s, color,
893 _(" (use \"git rebase --abort\" to check out the original branch)"));
895 } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
896 status_printf_ln(s, color, _("You are currently rebasing."));
897 if (advice_status_hints)
898 status_printf_ln(s, color,
899 _(" (all conflicts fixed: run \"git rebase --continue\")"));
900 } else if (split_commit_in_progress(s)) {
901 status_printf_ln(s, color, _("You are currently splitting a commit during a rebase."));
902 if (advice_status_hints)
903 status_printf_ln(s, color,
904 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
905 } else {
906 status_printf_ln(s, color, _("You are currently editing a commit during a rebase."));
907 if (advice_status_hints && !s->amend) {
908 status_printf_ln(s, color,
909 _(" (use \"git commit --amend\" to amend the current commit)"));
910 status_printf_ln(s, color,
911 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
914 wt_status_print_trailer(s);
917 static void show_cherry_pick_in_progress(struct wt_status *s,
918 struct wt_status_state *state,
919 const char *color)
921 status_printf_ln(s, color, _("You are currently cherry-picking."));
922 if (advice_status_hints) {
923 if (has_unmerged(s))
924 status_printf_ln(s, color,
925 _(" (fix conflicts and run \"git commit\")"));
926 else
927 status_printf_ln(s, color,
928 _(" (all conflicts fixed: run \"git commit\")"));
930 wt_status_print_trailer(s);
933 static void show_bisect_in_progress(struct wt_status *s,
934 struct wt_status_state *state,
935 const char *color)
937 status_printf_ln(s, color, _("You are currently bisecting."));
938 if (advice_status_hints)
939 status_printf_ln(s, color,
940 _(" (use \"git bisect reset\" to get back to the original branch)"));
941 wt_status_print_trailer(s);
944 static void wt_status_print_state(struct wt_status *s)
946 const char *state_color = color(WT_STATUS_HEADER, s);
947 struct wt_status_state state;
948 struct stat st;
950 memset(&state, 0, sizeof(state));
952 if (!stat(git_path("MERGE_HEAD"), &st)) {
953 state.merge_in_progress = 1;
954 } else if (!stat(git_path("rebase-apply"), &st)) {
955 if (!stat(git_path("rebase-apply/applying"), &st)) {
956 state.am_in_progress = 1;
957 if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
958 state.am_empty_patch = 1;
959 } else {
960 state.rebase_in_progress = 1;
962 } else if (!stat(git_path("rebase-merge"), &st)) {
963 if (!stat(git_path("rebase-merge/interactive"), &st))
964 state.rebase_interactive_in_progress = 1;
965 else
966 state.rebase_in_progress = 1;
967 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
968 state.cherry_pick_in_progress = 1;
970 if (!stat(git_path("BISECT_LOG"), &st))
971 state.bisect_in_progress = 1;
973 if (state.merge_in_progress)
974 show_merge_in_progress(s, &state, state_color);
975 else if (state.am_in_progress)
976 show_am_in_progress(s, &state, state_color);
977 else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
978 show_rebase_in_progress(s, &state, state_color);
979 else if (state.cherry_pick_in_progress)
980 show_cherry_pick_in_progress(s, &state, state_color);
981 if (state.bisect_in_progress)
982 show_bisect_in_progress(s, &state, state_color);
985 void wt_status_print(struct wt_status *s)
987 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
988 const char *branch_status_color = color(WT_STATUS_HEADER, s);
990 if (s->branch) {
991 const char *on_what = _("On branch ");
992 const char *branch_name = s->branch;
993 if (!prefixcmp(branch_name, "refs/heads/"))
994 branch_name += 11;
995 else if (!strcmp(branch_name, "HEAD")) {
996 branch_name = "";
997 branch_status_color = color(WT_STATUS_NOBRANCH, s);
998 on_what = _("Not currently on any branch.");
1000 status_printf(s, color(WT_STATUS_HEADER, s), "");
1001 status_printf_more(s, branch_status_color, "%s", on_what);
1002 status_printf_more(s, branch_color, "%s\n", branch_name);
1003 if (!s->is_initial)
1004 wt_status_print_tracking(s);
1007 wt_status_print_state(s);
1008 if (s->is_initial) {
1009 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1010 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1011 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1014 wt_status_print_updated(s);
1015 wt_status_print_unmerged(s);
1016 wt_status_print_changed(s);
1017 if (s->submodule_summary &&
1018 (!s->ignore_submodule_arg ||
1019 strcmp(s->ignore_submodule_arg, "all"))) {
1020 wt_status_print_submodule_summary(s, 0); /* staged */
1021 wt_status_print_submodule_summary(s, 1); /* unstaged */
1023 if (s->show_untracked_files) {
1024 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1025 if (s->show_ignored_files)
1026 wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1027 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1028 status_printf_ln(s, GIT_COLOR_NORMAL, "");
1029 status_printf_ln(s, GIT_COLOR_NORMAL,
1030 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1031 "may speed it up, but you have to be careful not to forget to add\n"
1032 "new files yourself (see 'git help status')."),
1033 s->untracked_in_ms / 1000.0);
1035 } else if (s->commitable)
1036 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1037 advice_status_hints
1038 ? _(" (use -u option to show untracked files)") : "");
1040 if (s->verbose)
1041 wt_status_print_verbose(s);
1042 if (!s->commitable) {
1043 if (s->amend)
1044 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1045 else if (s->nowarn)
1046 ; /* nothing */
1047 else if (s->workdir_dirty) {
1048 if (advice_status_hints)
1049 printf(_("no changes added to commit "
1050 "(use \"git add\" and/or \"git commit -a\")\n"));
1051 else
1052 printf(_("no changes added to commit\n"));
1053 } else if (s->untracked.nr) {
1054 if (advice_status_hints)
1055 printf(_("nothing added to commit but untracked files "
1056 "present (use \"git add\" to track)\n"));
1057 else
1058 printf(_("nothing added to commit but untracked files present\n"));
1059 } else if (s->is_initial) {
1060 if (advice_status_hints)
1061 printf(_("nothing to commit (create/copy files "
1062 "and use \"git add\" to track)\n"));
1063 else
1064 printf(_("nothing to commit\n"));
1065 } else if (!s->show_untracked_files) {
1066 if (advice_status_hints)
1067 printf(_("nothing to commit (use -u to show untracked files)\n"));
1068 else
1069 printf(_("nothing to commit\n"));
1070 } else
1071 printf(_("nothing to commit, working directory clean\n"));
1075 static void wt_shortstatus_unmerged(struct string_list_item *it,
1076 struct wt_status *s)
1078 struct wt_status_change_data *d = it->util;
1079 const char *how = "??";
1081 switch (d->stagemask) {
1082 case 1: how = "DD"; break; /* both deleted */
1083 case 2: how = "AU"; break; /* added by us */
1084 case 3: how = "UD"; break; /* deleted by them */
1085 case 4: how = "UA"; break; /* added by them */
1086 case 5: how = "DU"; break; /* deleted by us */
1087 case 6: how = "AA"; break; /* both added */
1088 case 7: how = "UU"; break; /* both modified */
1090 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1091 if (s->null_termination) {
1092 fprintf(stdout, " %s%c", it->string, 0);
1093 } else {
1094 struct strbuf onebuf = STRBUF_INIT;
1095 const char *one;
1096 one = quote_path(it->string, -1, &onebuf, s->prefix);
1097 printf(" %s\n", one);
1098 strbuf_release(&onebuf);
1102 static void wt_shortstatus_status(struct string_list_item *it,
1103 struct wt_status *s)
1105 struct wt_status_change_data *d = it->util;
1107 if (d->index_status)
1108 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1109 else
1110 putchar(' ');
1111 if (d->worktree_status)
1112 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1113 else
1114 putchar(' ');
1115 putchar(' ');
1116 if (s->null_termination) {
1117 fprintf(stdout, "%s%c", it->string, 0);
1118 if (d->head_path)
1119 fprintf(stdout, "%s%c", d->head_path, 0);
1120 } else {
1121 struct strbuf onebuf = STRBUF_INIT;
1122 const char *one;
1123 if (d->head_path) {
1124 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
1125 if (*one != '"' && strchr(one, ' ') != NULL) {
1126 putchar('"');
1127 strbuf_addch(&onebuf, '"');
1128 one = onebuf.buf;
1130 printf("%s -> ", one);
1131 strbuf_release(&onebuf);
1133 one = quote_path(it->string, -1, &onebuf, s->prefix);
1134 if (*one != '"' && strchr(one, ' ') != NULL) {
1135 putchar('"');
1136 strbuf_addch(&onebuf, '"');
1137 one = onebuf.buf;
1139 printf("%s\n", one);
1140 strbuf_release(&onebuf);
1144 static void wt_shortstatus_other(struct string_list_item *it,
1145 struct wt_status *s, const char *sign)
1147 if (s->null_termination) {
1148 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1149 } else {
1150 struct strbuf onebuf = STRBUF_INIT;
1151 const char *one;
1152 one = quote_path(it->string, -1, &onebuf, s->prefix);
1153 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1154 printf(" %s\n", one);
1155 strbuf_release(&onebuf);
1159 static void wt_shortstatus_print_tracking(struct wt_status *s)
1161 struct branch *branch;
1162 const char *header_color = color(WT_STATUS_HEADER, s);
1163 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1164 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1166 const char *base;
1167 const char *branch_name;
1168 int num_ours, num_theirs;
1170 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1172 if (!s->branch)
1173 return;
1174 branch_name = s->branch;
1176 if (!prefixcmp(branch_name, "refs/heads/"))
1177 branch_name += 11;
1178 else if (!strcmp(branch_name, "HEAD")) {
1179 branch_name = _("HEAD (no branch)");
1180 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1183 branch = branch_get(s->branch + 11);
1184 if (s->is_initial)
1185 color_fprintf(s->fp, header_color, _("Initial commit on "));
1186 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1187 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1188 fputc(s->null_termination ? '\0' : '\n', s->fp);
1189 return;
1192 base = branch->merge[0]->dst;
1193 base = shorten_unambiguous_ref(base, 0);
1194 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1195 color_fprintf(s->fp, header_color, "...");
1196 color_fprintf(s->fp, branch_color_remote, "%s", base);
1198 color_fprintf(s->fp, header_color, " [");
1199 if (!num_ours) {
1200 color_fprintf(s->fp, header_color, _("behind "));
1201 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1202 } else if (!num_theirs) {
1203 color_fprintf(s->fp, header_color, _("ahead "));
1204 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1205 } else {
1206 color_fprintf(s->fp, header_color, _("ahead "));
1207 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1208 color_fprintf(s->fp, header_color, _(", behind "));
1209 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1212 color_fprintf(s->fp, header_color, "]");
1213 fputc(s->null_termination ? '\0' : '\n', s->fp);
1216 void wt_shortstatus_print(struct wt_status *s)
1218 int i;
1220 if (s->show_branch)
1221 wt_shortstatus_print_tracking(s);
1223 for (i = 0; i < s->change.nr; i++) {
1224 struct wt_status_change_data *d;
1225 struct string_list_item *it;
1227 it = &(s->change.items[i]);
1228 d = it->util;
1229 if (d->stagemask)
1230 wt_shortstatus_unmerged(it, s);
1231 else
1232 wt_shortstatus_status(it, s);
1234 for (i = 0; i < s->untracked.nr; i++) {
1235 struct string_list_item *it;
1237 it = &(s->untracked.items[i]);
1238 wt_shortstatus_other(it, s, "??");
1240 for (i = 0; i < s->ignored.nr; i++) {
1241 struct string_list_item *it;
1243 it = &(s->ignored.items[i]);
1244 wt_shortstatus_other(it, s, "!!");
1248 void wt_porcelain_print(struct wt_status *s)
1250 s->use_color = 0;
1251 s->relative_paths = 0;
1252 s->prefix = NULL;
1253 wt_shortstatus_print(s);