completion: keep other config var completion in camelCase
[git.git] / wt-status.c
blob32f3bcaebd43aab58aea440ff704ca5340c61963
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 "argv-array.h"
12 #include "remote.h"
13 #include "refs.h"
14 #include "submodule.h"
15 #include "column.h"
16 #include "strbuf.h"
17 #include "utf8.h"
18 #include "worktree.h"
19 #include "lockfile.h"
21 static const char cut_line[] =
22 "------------------------ >8 ------------------------\n";
24 static char default_wt_status_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
30 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
31 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
33 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
36 static const char *color(int slot, struct wt_status *s)
38 const char *c = "";
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
41 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
43 return c;
46 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
53 strbuf_vaddf(&sb, fmt, ap);
54 if (!sb.len) {
55 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
57 if (!trail)
58 strbuf_addch(&sb, ' ');
60 color_print_strbuf(s->fp, color, &sb);
61 if (trail)
62 fprintf(s->fp, "%s", trail);
63 strbuf_release(&sb);
64 return;
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
69 strbuf_reset(&linebuf);
70 if (at_bol && s->display_comment_prefix) {
71 strbuf_addch(&linebuf, comment_line_char);
72 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
75 if (eol)
76 strbuf_add(&linebuf, line, eol - line);
77 else
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
80 if (eol)
81 fprintf(s->fp, "\n");
82 else
83 break;
84 at_bol = 1;
86 if (trail)
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
89 strbuf_release(&sb);
92 void status_printf_ln(struct wt_status *s, const char *color,
93 const char *fmt, ...)
95 va_list ap;
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, "\n");
99 va_end(ap);
102 void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
105 va_list ap;
107 va_start(ap, fmt);
108 status_vprintf(s, 1, color, fmt, ap, NULL);
109 va_end(ap);
112 static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 status_vprintf(s, 0, color, fmt, ap, NULL);
119 va_end(ap);
122 void wt_status_prepare(struct wt_status *s)
124 memset(s, 0, sizeof(*s));
125 memcpy(s->color_palette, default_wt_status_colors,
126 sizeof(default_wt_status_colors));
127 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
128 s->use_color = -1;
129 s->relative_paths = 1;
130 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
131 s->reference = "HEAD";
132 s->fp = stdout;
133 s->index_file = get_index_file();
134 s->change.strdup_strings = 1;
135 s->untracked.strdup_strings = 1;
136 s->ignored.strdup_strings = 1;
137 s->show_branch = -1; /* unspecified */
138 s->show_stash = 0;
139 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
140 s->display_comment_prefix = 0;
143 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
145 int i;
146 int del_mod_conflict = 0;
147 int both_deleted = 0;
148 int not_deleted = 0;
149 const char *c = color(WT_STATUS_HEADER, s);
151 status_printf_ln(s, c, _("Unmerged paths:"));
153 for (i = 0; i < s->change.nr; i++) {
154 struct string_list_item *it = &(s->change.items[i]);
155 struct wt_status_change_data *d = it->util;
157 switch (d->stagemask) {
158 case 0:
159 break;
160 case 1:
161 both_deleted = 1;
162 break;
163 case 3:
164 case 5:
165 del_mod_conflict = 1;
166 break;
167 default:
168 not_deleted = 1;
169 break;
173 if (!s->hints)
174 return;
175 if (s->whence != FROM_COMMIT)
177 else if (!s->is_initial)
178 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
179 else
180 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
182 if (!both_deleted) {
183 if (!del_mod_conflict)
184 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
185 else
186 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
187 } else if (!del_mod_conflict && !not_deleted) {
188 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
189 } else {
190 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
192 status_printf_ln(s, c, "%s", "");
195 static void wt_longstatus_print_cached_header(struct wt_status *s)
197 const char *c = color(WT_STATUS_HEADER, s);
199 status_printf_ln(s, c, _("Changes to be committed:"));
200 if (!s->hints)
201 return;
202 if (s->whence != FROM_COMMIT)
203 ; /* NEEDSWORK: use "git reset --unresolve"??? */
204 else if (!s->is_initial)
205 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
206 else
207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
208 status_printf_ln(s, c, "%s", "");
211 static void wt_longstatus_print_dirty_header(struct wt_status *s,
212 int has_deleted,
213 int has_dirty_submodules)
215 const char *c = color(WT_STATUS_HEADER, s);
217 status_printf_ln(s, c, _("Changes not staged for commit:"));
218 if (!s->hints)
219 return;
220 if (!has_deleted)
221 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
222 else
223 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
224 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
225 if (has_dirty_submodules)
226 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
227 status_printf_ln(s, c, "%s", "");
230 static void wt_longstatus_print_other_header(struct wt_status *s,
231 const char *what,
232 const char *how)
234 const char *c = color(WT_STATUS_HEADER, s);
235 status_printf_ln(s, c, "%s:", what);
236 if (!s->hints)
237 return;
238 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
239 status_printf_ln(s, c, "%s", "");
242 static void wt_longstatus_print_trailer(struct wt_status *s)
244 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
247 #define quote_path quote_path_relative
249 static const char *wt_status_unmerged_status_string(int stagemask)
251 switch (stagemask) {
252 case 1:
253 return _("both deleted:");
254 case 2:
255 return _("added by us:");
256 case 3:
257 return _("deleted by them:");
258 case 4:
259 return _("added by them:");
260 case 5:
261 return _("deleted by us:");
262 case 6:
263 return _("both added:");
264 case 7:
265 return _("both modified:");
266 default:
267 die("BUG: unhandled unmerged status %x", stagemask);
271 static const char *wt_status_diff_status_string(int status)
273 switch (status) {
274 case DIFF_STATUS_ADDED:
275 return _("new file:");
276 case DIFF_STATUS_COPIED:
277 return _("copied:");
278 case DIFF_STATUS_DELETED:
279 return _("deleted:");
280 case DIFF_STATUS_MODIFIED:
281 return _("modified:");
282 case DIFF_STATUS_RENAMED:
283 return _("renamed:");
284 case DIFF_STATUS_TYPE_CHANGED:
285 return _("typechange:");
286 case DIFF_STATUS_UNKNOWN:
287 return _("unknown:");
288 case DIFF_STATUS_UNMERGED:
289 return _("unmerged:");
290 default:
291 return NULL;
295 static int maxwidth(const char *(*label)(int), int minval, int maxval)
297 int result = 0, i;
299 for (i = minval; i <= maxval; i++) {
300 const char *s = label(i);
301 int len = s ? utf8_strwidth(s) : 0;
302 if (len > result)
303 result = len;
305 return result;
308 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
309 struct string_list_item *it)
311 const char *c = color(WT_STATUS_UNMERGED, s);
312 struct wt_status_change_data *d = it->util;
313 struct strbuf onebuf = STRBUF_INIT;
314 static char *padding;
315 static int label_width;
316 const char *one, *how;
317 int len;
319 if (!padding) {
320 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
321 label_width += strlen(" ");
322 padding = xmallocz(label_width);
323 memset(padding, ' ', label_width);
326 one = quote_path(it->string, s->prefix, &onebuf);
327 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
329 how = wt_status_unmerged_status_string(d->stagemask);
330 len = label_width - utf8_strwidth(how);
331 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
332 strbuf_release(&onebuf);
335 static void wt_longstatus_print_change_data(struct wt_status *s,
336 int change_type,
337 struct string_list_item *it)
339 struct wt_status_change_data *d = it->util;
340 const char *c = color(change_type, s);
341 int status;
342 char *one_name;
343 char *two_name;
344 const char *one, *two;
345 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
346 struct strbuf extra = STRBUF_INIT;
347 static char *padding;
348 static int label_width;
349 const char *what;
350 int len;
352 if (!padding) {
353 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
354 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
355 label_width += strlen(" ");
356 padding = xmallocz(label_width);
357 memset(padding, ' ', label_width);
360 one_name = two_name = it->string;
361 switch (change_type) {
362 case WT_STATUS_UPDATED:
363 status = d->index_status;
364 break;
365 case WT_STATUS_CHANGED:
366 if (d->new_submodule_commits || d->dirty_submodule) {
367 strbuf_addstr(&extra, " (");
368 if (d->new_submodule_commits)
369 strbuf_addstr(&extra, _("new commits, "));
370 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
371 strbuf_addstr(&extra, _("modified content, "));
372 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
373 strbuf_addstr(&extra, _("untracked content, "));
374 strbuf_setlen(&extra, extra.len - 2);
375 strbuf_addch(&extra, ')');
377 status = d->worktree_status;
378 break;
379 default:
380 die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
381 change_type);
385 * Only pick up the rename it's relevant. If the rename is for
386 * the changed section and we're printing the updated section,
387 * ignore it.
389 if (d->rename_status == status)
390 one_name = d->rename_source;
392 one = quote_path(one_name, s->prefix, &onebuf);
393 two = quote_path(two_name, s->prefix, &twobuf);
395 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
396 what = wt_status_diff_status_string(status);
397 if (!what)
398 die("BUG: unhandled diff status %c", status);
399 len = label_width - utf8_strwidth(what);
400 assert(len >= 0);
401 if (one_name != two_name)
402 status_printf_more(s, c, "%s%.*s%s -> %s",
403 what, len, padding, one, two);
404 else
405 status_printf_more(s, c, "%s%.*s%s",
406 what, len, padding, one);
407 if (extra.len) {
408 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
409 strbuf_release(&extra);
411 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
412 strbuf_release(&onebuf);
413 strbuf_release(&twobuf);
416 static char short_submodule_status(struct wt_status_change_data *d)
418 if (d->new_submodule_commits)
419 return 'M';
420 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
421 return 'm';
422 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
423 return '?';
424 return d->worktree_status;
427 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
428 struct diff_options *options,
429 void *data)
431 struct wt_status *s = data;
432 int i;
434 if (!q->nr)
435 return;
436 s->workdir_dirty = 1;
437 for (i = 0; i < q->nr; i++) {
438 struct diff_filepair *p;
439 struct string_list_item *it;
440 struct wt_status_change_data *d;
442 p = q->queue[i];
443 it = string_list_insert(&s->change, p->two->path);
444 d = it->util;
445 if (!d) {
446 d = xcalloc(1, sizeof(*d));
447 it->util = d;
449 if (!d->worktree_status)
450 d->worktree_status = p->status;
451 if (S_ISGITLINK(p->two->mode)) {
452 d->dirty_submodule = p->two->dirty_submodule;
453 d->new_submodule_commits = !!oidcmp(&p->one->oid,
454 &p->two->oid);
455 if (s->status_format == STATUS_FORMAT_SHORT)
456 d->worktree_status = short_submodule_status(d);
459 switch (p->status) {
460 case DIFF_STATUS_ADDED:
461 d->mode_worktree = p->two->mode;
462 break;
464 case DIFF_STATUS_DELETED:
465 d->mode_index = p->one->mode;
466 oidcpy(&d->oid_index, &p->one->oid);
467 /* mode_worktree is zero for a delete. */
468 break;
470 case DIFF_STATUS_COPIED:
471 case DIFF_STATUS_RENAMED:
472 if (d->rename_status)
473 die("BUG: multiple renames on the same target? how?");
474 d->rename_source = xstrdup(p->one->path);
475 d->rename_score = p->score * 100 / MAX_SCORE;
476 d->rename_status = p->status;
477 /* fallthru */
478 case DIFF_STATUS_MODIFIED:
479 case DIFF_STATUS_TYPE_CHANGED:
480 case DIFF_STATUS_UNMERGED:
481 d->mode_index = p->one->mode;
482 d->mode_worktree = p->two->mode;
483 oidcpy(&d->oid_index, &p->one->oid);
484 break;
486 default:
487 die("BUG: unhandled diff-files status '%c'", p->status);
488 break;
494 static int unmerged_mask(const char *path)
496 int pos, mask;
497 const struct cache_entry *ce;
499 pos = cache_name_pos(path, strlen(path));
500 if (0 <= pos)
501 return 0;
503 mask = 0;
504 pos = -pos-1;
505 while (pos < active_nr) {
506 ce = active_cache[pos++];
507 if (strcmp(ce->name, path) || !ce_stage(ce))
508 break;
509 mask |= (1 << (ce_stage(ce) - 1));
511 return mask;
514 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
515 struct diff_options *options,
516 void *data)
518 struct wt_status *s = data;
519 int i;
521 for (i = 0; i < q->nr; i++) {
522 struct diff_filepair *p;
523 struct string_list_item *it;
524 struct wt_status_change_data *d;
526 p = q->queue[i];
527 it = string_list_insert(&s->change, p->two->path);
528 d = it->util;
529 if (!d) {
530 d = xcalloc(1, sizeof(*d));
531 it->util = d;
533 if (!d->index_status)
534 d->index_status = p->status;
535 switch (p->status) {
536 case DIFF_STATUS_ADDED:
537 /* Leave {mode,oid}_head zero for an add. */
538 d->mode_index = p->two->mode;
539 oidcpy(&d->oid_index, &p->two->oid);
540 break;
541 case DIFF_STATUS_DELETED:
542 d->mode_head = p->one->mode;
543 oidcpy(&d->oid_head, &p->one->oid);
544 /* Leave {mode,oid}_index zero for a delete. */
545 break;
547 case DIFF_STATUS_COPIED:
548 case DIFF_STATUS_RENAMED:
549 if (d->rename_status)
550 die("BUG: multiple renames on the same target? how?");
551 d->rename_source = xstrdup(p->one->path);
552 d->rename_score = p->score * 100 / MAX_SCORE;
553 d->rename_status = p->status;
554 /* fallthru */
555 case DIFF_STATUS_MODIFIED:
556 case DIFF_STATUS_TYPE_CHANGED:
557 d->mode_head = p->one->mode;
558 d->mode_index = p->two->mode;
559 oidcpy(&d->oid_head, &p->one->oid);
560 oidcpy(&d->oid_index, &p->two->oid);
561 break;
562 case DIFF_STATUS_UNMERGED:
563 d->stagemask = unmerged_mask(p->two->path);
565 * Don't bother setting {mode,oid}_{head,index} since the print
566 * code will output the stage values directly and not use the
567 * values in these fields.
569 break;
571 default:
572 die("BUG: unhandled diff-index status '%c'", p->status);
573 break;
578 static void wt_status_collect_changes_worktree(struct wt_status *s)
580 struct rev_info rev;
582 init_revisions(&rev, NULL);
583 setup_revisions(0, NULL, &rev, NULL);
584 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
585 rev.diffopt.flags.dirty_submodules = 1;
586 rev.diffopt.ita_invisible_in_index = 1;
587 if (!s->show_untracked_files)
588 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
589 if (s->ignore_submodule_arg) {
590 rev.diffopt.flags.override_submodule_config = 1;
591 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
593 rev.diffopt.format_callback = wt_status_collect_changed_cb;
594 rev.diffopt.format_callback_data = s;
595 copy_pathspec(&rev.prune_data, &s->pathspec);
596 run_diff_files(&rev, 0);
599 static void wt_status_collect_changes_index(struct wt_status *s)
601 struct rev_info rev;
602 struct setup_revision_opt opt;
604 init_revisions(&rev, NULL);
605 memset(&opt, 0, sizeof(opt));
606 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
607 setup_revisions(0, NULL, &rev, &opt);
609 rev.diffopt.flags.override_submodule_config = 1;
610 rev.diffopt.ita_invisible_in_index = 1;
611 if (s->ignore_submodule_arg) {
612 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
613 } else {
615 * Unless the user did explicitly request a submodule ignore
616 * mode by passing a command line option we do not ignore any
617 * changed submodule SHA-1s when comparing index and HEAD, no
618 * matter what is configured. Otherwise the user won't be
619 * shown any submodules she manually added (and which are
620 * staged to be committed), which would be really confusing.
622 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
625 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
626 rev.diffopt.format_callback = wt_status_collect_updated_cb;
627 rev.diffopt.format_callback_data = s;
628 copy_pathspec(&rev.prune_data, &s->pathspec);
629 run_diff_index(&rev, 1);
632 static void wt_status_collect_changes_initial(struct wt_status *s)
634 int i;
636 for (i = 0; i < active_nr; i++) {
637 struct string_list_item *it;
638 struct wt_status_change_data *d;
639 const struct cache_entry *ce = active_cache[i];
641 if (!ce_path_match(ce, &s->pathspec, NULL))
642 continue;
643 if (ce_intent_to_add(ce))
644 continue;
645 it = string_list_insert(&s->change, ce->name);
646 d = it->util;
647 if (!d) {
648 d = xcalloc(1, sizeof(*d));
649 it->util = d;
651 if (ce_stage(ce)) {
652 d->index_status = DIFF_STATUS_UNMERGED;
653 d->stagemask |= (1 << (ce_stage(ce) - 1));
655 * Don't bother setting {mode,oid}_{head,index} since the print
656 * code will output the stage values directly and not use the
657 * values in these fields.
659 } else {
660 d->index_status = DIFF_STATUS_ADDED;
661 /* Leave {mode,oid}_head zero for adds. */
662 d->mode_index = ce->ce_mode;
663 oidcpy(&d->oid_index, &ce->oid);
668 static void wt_status_collect_untracked(struct wt_status *s)
670 int i;
671 struct dir_struct dir;
672 uint64_t t_begin = getnanotime();
674 if (!s->show_untracked_files)
675 return;
677 memset(&dir, 0, sizeof(dir));
678 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
679 dir.flags |=
680 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
681 if (s->show_ignored_mode) {
682 dir.flags |= DIR_SHOW_IGNORED_TOO;
684 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
685 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
686 } else {
687 dir.untracked = the_index.untracked;
690 setup_standard_excludes(&dir);
692 fill_directory(&dir, &the_index, &s->pathspec);
694 for (i = 0; i < dir.nr; i++) {
695 struct dir_entry *ent = dir.entries[i];
696 if (cache_name_is_other(ent->name, ent->len) &&
697 dir_path_match(ent, &s->pathspec, 0, NULL))
698 string_list_insert(&s->untracked, ent->name);
699 free(ent);
702 for (i = 0; i < dir.ignored_nr; i++) {
703 struct dir_entry *ent = dir.ignored[i];
704 if (cache_name_is_other(ent->name, ent->len) &&
705 dir_path_match(ent, &s->pathspec, 0, NULL))
706 string_list_insert(&s->ignored, ent->name);
707 free(ent);
710 free(dir.entries);
711 free(dir.ignored);
712 clear_directory(&dir);
714 if (advice_status_u_option)
715 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
718 void wt_status_collect(struct wt_status *s)
720 wt_status_collect_changes_worktree(s);
722 if (s->is_initial)
723 wt_status_collect_changes_initial(s);
724 else
725 wt_status_collect_changes_index(s);
726 wt_status_collect_untracked(s);
729 static void wt_longstatus_print_unmerged(struct wt_status *s)
731 int shown_header = 0;
732 int i;
734 for (i = 0; i < s->change.nr; i++) {
735 struct wt_status_change_data *d;
736 struct string_list_item *it;
737 it = &(s->change.items[i]);
738 d = it->util;
739 if (!d->stagemask)
740 continue;
741 if (!shown_header) {
742 wt_longstatus_print_unmerged_header(s);
743 shown_header = 1;
745 wt_longstatus_print_unmerged_data(s, it);
747 if (shown_header)
748 wt_longstatus_print_trailer(s);
752 static void wt_longstatus_print_updated(struct wt_status *s)
754 int shown_header = 0;
755 int i;
757 for (i = 0; i < s->change.nr; i++) {
758 struct wt_status_change_data *d;
759 struct string_list_item *it;
760 it = &(s->change.items[i]);
761 d = it->util;
762 if (!d->index_status ||
763 d->index_status == DIFF_STATUS_UNMERGED)
764 continue;
765 if (!shown_header) {
766 wt_longstatus_print_cached_header(s);
767 s->commitable = 1;
768 shown_header = 1;
770 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
772 if (shown_header)
773 wt_longstatus_print_trailer(s);
777 * -1 : has delete
778 * 0 : no change
779 * 1 : some change but no delete
781 static int wt_status_check_worktree_changes(struct wt_status *s,
782 int *dirty_submodules)
784 int i;
785 int changes = 0;
787 *dirty_submodules = 0;
789 for (i = 0; i < s->change.nr; i++) {
790 struct wt_status_change_data *d;
791 d = s->change.items[i].util;
792 if (!d->worktree_status ||
793 d->worktree_status == DIFF_STATUS_UNMERGED)
794 continue;
795 if (!changes)
796 changes = 1;
797 if (d->dirty_submodule)
798 *dirty_submodules = 1;
799 if (d->worktree_status == DIFF_STATUS_DELETED)
800 changes = -1;
802 return changes;
805 static void wt_longstatus_print_changed(struct wt_status *s)
807 int i, dirty_submodules;
808 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
810 if (!worktree_changes)
811 return;
813 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
815 for (i = 0; i < s->change.nr; i++) {
816 struct wt_status_change_data *d;
817 struct string_list_item *it;
818 it = &(s->change.items[i]);
819 d = it->util;
820 if (!d->worktree_status ||
821 d->worktree_status == DIFF_STATUS_UNMERGED)
822 continue;
823 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
825 wt_longstatus_print_trailer(s);
828 static int stash_count_refs(struct object_id *ooid, struct object_id *noid,
829 const char *email, timestamp_t timestamp, int tz,
830 const char *message, void *cb_data)
832 int *c = cb_data;
833 (*c)++;
834 return 0;
837 static void wt_longstatus_print_stash_summary(struct wt_status *s)
839 int stash_count = 0;
841 for_each_reflog_ent("refs/stash", stash_count_refs, &stash_count);
842 if (stash_count > 0)
843 status_printf_ln(s, GIT_COLOR_NORMAL,
844 Q_("Your stash currently has %d entry",
845 "Your stash currently has %d entries", stash_count),
846 stash_count);
849 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
851 struct child_process sm_summary = CHILD_PROCESS_INIT;
852 struct strbuf cmd_stdout = STRBUF_INIT;
853 struct strbuf summary = STRBUF_INIT;
854 char *summary_content;
856 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
857 s->index_file);
859 argv_array_push(&sm_summary.args, "submodule");
860 argv_array_push(&sm_summary.args, "summary");
861 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
862 argv_array_push(&sm_summary.args, "--for-status");
863 argv_array_push(&sm_summary.args, "--summary-limit");
864 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
865 if (!uncommitted)
866 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
868 sm_summary.git_cmd = 1;
869 sm_summary.no_stdin = 1;
871 capture_command(&sm_summary, &cmd_stdout, 1024);
873 /* prepend header, only if there's an actual output */
874 if (cmd_stdout.len) {
875 if (uncommitted)
876 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
877 else
878 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
879 strbuf_addstr(&summary, "\n\n");
881 strbuf_addbuf(&summary, &cmd_stdout);
882 strbuf_release(&cmd_stdout);
884 if (s->display_comment_prefix) {
885 size_t len;
886 summary_content = strbuf_detach(&summary, &len);
887 strbuf_add_commented_lines(&summary, summary_content, len);
888 free(summary_content);
891 fputs(summary.buf, s->fp);
892 strbuf_release(&summary);
895 static void wt_longstatus_print_other(struct wt_status *s,
896 struct string_list *l,
897 const char *what,
898 const char *how)
900 int i;
901 struct strbuf buf = STRBUF_INIT;
902 static struct string_list output = STRING_LIST_INIT_DUP;
903 struct column_options copts;
905 if (!l->nr)
906 return;
908 wt_longstatus_print_other_header(s, what, how);
910 for (i = 0; i < l->nr; i++) {
911 struct string_list_item *it;
912 const char *path;
913 it = &(l->items[i]);
914 path = quote_path(it->string, s->prefix, &buf);
915 if (column_active(s->colopts)) {
916 string_list_append(&output, path);
917 continue;
919 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
920 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
921 "%s\n", path);
924 strbuf_release(&buf);
925 if (!column_active(s->colopts))
926 goto conclude;
928 strbuf_addf(&buf, "%s%s\t%s",
929 color(WT_STATUS_HEADER, s),
930 s->display_comment_prefix ? "#" : "",
931 color(WT_STATUS_UNTRACKED, s));
932 memset(&copts, 0, sizeof(copts));
933 copts.padding = 1;
934 copts.indent = buf.buf;
935 if (want_color(s->use_color))
936 copts.nl = GIT_COLOR_RESET "\n";
937 print_columns(&output, s->colopts, &copts);
938 string_list_clear(&output, 0);
939 strbuf_release(&buf);
940 conclude:
941 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
944 size_t wt_status_locate_end(const char *s, size_t len)
946 const char *p;
947 struct strbuf pattern = STRBUF_INIT;
949 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
950 if (starts_with(s, pattern.buf + 1))
951 len = 0;
952 else if ((p = strstr(s, pattern.buf)))
953 len = p - s + 1;
954 strbuf_release(&pattern);
955 return len;
958 void wt_status_add_cut_line(FILE *fp)
960 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
961 struct strbuf buf = STRBUF_INIT;
963 fprintf(fp, "%c %s", comment_line_char, cut_line);
964 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
965 fputs(buf.buf, fp);
966 strbuf_release(&buf);
969 static void wt_longstatus_print_verbose(struct wt_status *s)
971 struct rev_info rev;
972 struct setup_revision_opt opt;
973 int dirty_submodules;
974 const char *c = color(WT_STATUS_HEADER, s);
976 init_revisions(&rev, NULL);
977 rev.diffopt.flags.allow_textconv = 1;
978 rev.diffopt.ita_invisible_in_index = 1;
980 memset(&opt, 0, sizeof(opt));
981 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
982 setup_revisions(0, NULL, &rev, &opt);
984 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
985 rev.diffopt.file = s->fp;
986 rev.diffopt.close_file = 0;
988 * If we're not going to stdout, then we definitely don't
989 * want color, since we are going to the commit message
990 * file (and even the "auto" setting won't work, since it
991 * will have checked isatty on stdout). But we then do want
992 * to insert the scissor line here to reliably remove the
993 * diff before committing.
995 if (s->fp != stdout) {
996 rev.diffopt.use_color = 0;
997 wt_status_add_cut_line(s->fp);
999 if (s->verbose > 1 && s->commitable) {
1000 /* print_updated() printed a header, so do we */
1001 if (s->fp != stdout)
1002 wt_longstatus_print_trailer(s);
1003 status_printf_ln(s, c, _("Changes to be committed:"));
1004 rev.diffopt.a_prefix = "c/";
1005 rev.diffopt.b_prefix = "i/";
1006 } /* else use prefix as per user config */
1007 run_diff_index(&rev, 1);
1008 if (s->verbose > 1 &&
1009 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1010 status_printf_ln(s, c,
1011 "--------------------------------------------------");
1012 status_printf_ln(s, c, _("Changes not staged for commit:"));
1013 setup_work_tree();
1014 rev.diffopt.a_prefix = "i/";
1015 rev.diffopt.b_prefix = "w/";
1016 run_diff_files(&rev, 0);
1020 static void wt_longstatus_print_tracking(struct wt_status *s)
1022 struct strbuf sb = STRBUF_INIT;
1023 const char *cp, *ep, *branch_name;
1024 struct branch *branch;
1025 char comment_line_string[3];
1026 int i;
1028 assert(s->branch && !s->is_initial);
1029 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1030 return;
1031 branch = branch_get(branch_name);
1032 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1033 return;
1035 i = 0;
1036 if (s->display_comment_prefix) {
1037 comment_line_string[i++] = comment_line_char;
1038 comment_line_string[i++] = ' ';
1040 comment_line_string[i] = '\0';
1042 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1043 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1044 "%s%.*s", comment_line_string,
1045 (int)(ep - cp), cp);
1046 if (s->display_comment_prefix)
1047 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1048 comment_line_char);
1049 else
1050 fputs("\n", s->fp);
1051 strbuf_release(&sb);
1054 static int has_unmerged(struct wt_status *s)
1056 int i;
1058 for (i = 0; i < s->change.nr; i++) {
1059 struct wt_status_change_data *d;
1060 d = s->change.items[i].util;
1061 if (d->stagemask)
1062 return 1;
1064 return 0;
1067 static void show_merge_in_progress(struct wt_status *s,
1068 struct wt_status_state *state,
1069 const char *color)
1071 if (has_unmerged(s)) {
1072 status_printf_ln(s, color, _("You have unmerged paths."));
1073 if (s->hints) {
1074 status_printf_ln(s, color,
1075 _(" (fix conflicts and run \"git commit\")"));
1076 status_printf_ln(s, color,
1077 _(" (use \"git merge --abort\" to abort the merge)"));
1079 } else {
1080 s-> commitable = 1;
1081 status_printf_ln(s, color,
1082 _("All conflicts fixed but you are still merging."));
1083 if (s->hints)
1084 status_printf_ln(s, color,
1085 _(" (use \"git commit\" to conclude merge)"));
1087 wt_longstatus_print_trailer(s);
1090 static void show_am_in_progress(struct wt_status *s,
1091 struct wt_status_state *state,
1092 const char *color)
1094 status_printf_ln(s, color,
1095 _("You are in the middle of an am session."));
1096 if (state->am_empty_patch)
1097 status_printf_ln(s, color,
1098 _("The current patch is empty."));
1099 if (s->hints) {
1100 if (!state->am_empty_patch)
1101 status_printf_ln(s, color,
1102 _(" (fix conflicts and then run \"git am --continue\")"));
1103 status_printf_ln(s, color,
1104 _(" (use \"git am --skip\" to skip this patch)"));
1105 status_printf_ln(s, color,
1106 _(" (use \"git am --abort\" to restore the original branch)"));
1108 wt_longstatus_print_trailer(s);
1111 static char *read_line_from_git_path(const char *filename)
1113 struct strbuf buf = STRBUF_INIT;
1114 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1116 if (!fp) {
1117 strbuf_release(&buf);
1118 return NULL;
1120 strbuf_getline_lf(&buf, fp);
1121 if (!fclose(fp)) {
1122 return strbuf_detach(&buf, NULL);
1123 } else {
1124 strbuf_release(&buf);
1125 return NULL;
1129 static int split_commit_in_progress(struct wt_status *s)
1131 int split_in_progress = 0;
1132 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1134 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1135 !s->branch || strcmp(s->branch, "HEAD"))
1136 return 0;
1138 head = read_line_from_git_path("HEAD");
1139 orig_head = read_line_from_git_path("ORIG_HEAD");
1140 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1141 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1143 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1144 ; /* fall through, no split in progress */
1145 else if (!strcmp(rebase_amend, rebase_orig_head))
1146 split_in_progress = !!strcmp(head, rebase_amend);
1147 else if (strcmp(orig_head, rebase_orig_head))
1148 split_in_progress = 1;
1150 free(head);
1151 free(orig_head);
1152 free(rebase_amend);
1153 free(rebase_orig_head);
1155 return split_in_progress;
1159 * Turn
1160 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1161 * into
1162 * "pick d6a2f03 some message"
1164 * The function assumes that the line does not contain useless spaces
1165 * before or after the command.
1167 static void abbrev_sha1_in_line(struct strbuf *line)
1169 struct strbuf **split;
1170 int i;
1172 if (starts_with(line->buf, "exec ") ||
1173 starts_with(line->buf, "x "))
1174 return;
1176 split = strbuf_split_max(line, ' ', 3);
1177 if (split[0] && split[1]) {
1178 struct object_id oid;
1181 * strbuf_split_max left a space. Trim it and re-add
1182 * it after abbreviation.
1184 strbuf_trim(split[1]);
1185 if (!get_oid(split[1]->buf, &oid)) {
1186 strbuf_reset(split[1]);
1187 strbuf_add_unique_abbrev(split[1], &oid,
1188 DEFAULT_ABBREV);
1189 strbuf_addch(split[1], ' ');
1190 strbuf_reset(line);
1191 for (i = 0; split[i]; i++)
1192 strbuf_addbuf(line, split[i]);
1195 strbuf_list_free(split);
1198 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1200 struct strbuf line = STRBUF_INIT;
1201 FILE *f = fopen(git_path("%s", fname), "r");
1203 if (!f) {
1204 if (errno == ENOENT)
1205 return -1;
1206 die_errno("Could not open file %s for reading",
1207 git_path("%s", fname));
1209 while (!strbuf_getline_lf(&line, f)) {
1210 if (line.len && line.buf[0] == comment_line_char)
1211 continue;
1212 strbuf_trim(&line);
1213 if (!line.len)
1214 continue;
1215 abbrev_sha1_in_line(&line);
1216 string_list_append(lines, line.buf);
1218 fclose(f);
1219 strbuf_release(&line);
1220 return 0;
1223 static void show_rebase_information(struct wt_status *s,
1224 struct wt_status_state *state,
1225 const char *color)
1227 if (state->rebase_interactive_in_progress) {
1228 int i;
1229 int nr_lines_to_show = 2;
1231 struct string_list have_done = STRING_LIST_INIT_DUP;
1232 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1234 read_rebase_todolist("rebase-merge/done", &have_done);
1235 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1236 &yet_to_do))
1237 status_printf_ln(s, color,
1238 _("git-rebase-todo is missing."));
1239 if (have_done.nr == 0)
1240 status_printf_ln(s, color, _("No commands done."));
1241 else {
1242 status_printf_ln(s, color,
1243 Q_("Last command done (%d command done):",
1244 "Last commands done (%d commands done):",
1245 have_done.nr),
1246 have_done.nr);
1247 for (i = (have_done.nr > nr_lines_to_show)
1248 ? have_done.nr - nr_lines_to_show : 0;
1249 i < have_done.nr;
1250 i++)
1251 status_printf_ln(s, color, " %s", have_done.items[i].string);
1252 if (have_done.nr > nr_lines_to_show && s->hints)
1253 status_printf_ln(s, color,
1254 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1257 if (yet_to_do.nr == 0)
1258 status_printf_ln(s, color,
1259 _("No commands remaining."));
1260 else {
1261 status_printf_ln(s, color,
1262 Q_("Next command to do (%d remaining command):",
1263 "Next commands to do (%d remaining commands):",
1264 yet_to_do.nr),
1265 yet_to_do.nr);
1266 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1267 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1268 if (s->hints)
1269 status_printf_ln(s, color,
1270 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1272 string_list_clear(&yet_to_do, 0);
1273 string_list_clear(&have_done, 0);
1277 static void print_rebase_state(struct wt_status *s,
1278 struct wt_status_state *state,
1279 const char *color)
1281 if (state->branch)
1282 status_printf_ln(s, color,
1283 _("You are currently rebasing branch '%s' on '%s'."),
1284 state->branch,
1285 state->onto);
1286 else
1287 status_printf_ln(s, color,
1288 _("You are currently rebasing."));
1291 static void show_rebase_in_progress(struct wt_status *s,
1292 struct wt_status_state *state,
1293 const char *color)
1295 struct stat st;
1297 show_rebase_information(s, state, color);
1298 if (has_unmerged(s)) {
1299 print_rebase_state(s, state, color);
1300 if (s->hints) {
1301 status_printf_ln(s, color,
1302 _(" (fix conflicts and then run \"git rebase --continue\")"));
1303 status_printf_ln(s, color,
1304 _(" (use \"git rebase --skip\" to skip this patch)"));
1305 status_printf_ln(s, color,
1306 _(" (use \"git rebase --abort\" to check out the original branch)"));
1308 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
1309 print_rebase_state(s, state, color);
1310 if (s->hints)
1311 status_printf_ln(s, color,
1312 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1313 } else if (split_commit_in_progress(s)) {
1314 if (state->branch)
1315 status_printf_ln(s, color,
1316 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1317 state->branch,
1318 state->onto);
1319 else
1320 status_printf_ln(s, color,
1321 _("You are currently splitting a commit during a rebase."));
1322 if (s->hints)
1323 status_printf_ln(s, color,
1324 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1325 } else {
1326 if (state->branch)
1327 status_printf_ln(s, color,
1328 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1329 state->branch,
1330 state->onto);
1331 else
1332 status_printf_ln(s, color,
1333 _("You are currently editing a commit during a rebase."));
1334 if (s->hints && !s->amend) {
1335 status_printf_ln(s, color,
1336 _(" (use \"git commit --amend\" to amend the current commit)"));
1337 status_printf_ln(s, color,
1338 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1341 wt_longstatus_print_trailer(s);
1344 static void show_cherry_pick_in_progress(struct wt_status *s,
1345 struct wt_status_state *state,
1346 const char *color)
1348 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1349 find_unique_abbrev(&state->cherry_pick_head_oid, DEFAULT_ABBREV));
1350 if (s->hints) {
1351 if (has_unmerged(s))
1352 status_printf_ln(s, color,
1353 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1354 else
1355 status_printf_ln(s, color,
1356 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1357 status_printf_ln(s, color,
1358 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1360 wt_longstatus_print_trailer(s);
1363 static void show_revert_in_progress(struct wt_status *s,
1364 struct wt_status_state *state,
1365 const char *color)
1367 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1368 find_unique_abbrev(&state->revert_head_oid, DEFAULT_ABBREV));
1369 if (s->hints) {
1370 if (has_unmerged(s))
1371 status_printf_ln(s, color,
1372 _(" (fix conflicts and run \"git revert --continue\")"));
1373 else
1374 status_printf_ln(s, color,
1375 _(" (all conflicts fixed: run \"git revert --continue\")"));
1376 status_printf_ln(s, color,
1377 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1379 wt_longstatus_print_trailer(s);
1382 static void show_bisect_in_progress(struct wt_status *s,
1383 struct wt_status_state *state,
1384 const char *color)
1386 if (state->branch)
1387 status_printf_ln(s, color,
1388 _("You are currently bisecting, started from branch '%s'."),
1389 state->branch);
1390 else
1391 status_printf_ln(s, color,
1392 _("You are currently bisecting."));
1393 if (s->hints)
1394 status_printf_ln(s, color,
1395 _(" (use \"git bisect reset\" to get back to the original branch)"));
1396 wt_longstatus_print_trailer(s);
1400 * Extract branch information from rebase/bisect
1402 static char *get_branch(const struct worktree *wt, const char *path)
1404 struct strbuf sb = STRBUF_INIT;
1405 struct object_id oid;
1406 const char *branch_name;
1408 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1409 goto got_nothing;
1411 while (sb.len && sb.buf[sb.len - 1] == '\n')
1412 strbuf_setlen(&sb, sb.len - 1);
1413 if (!sb.len)
1414 goto got_nothing;
1415 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1416 strbuf_remove(&sb, 0, branch_name - sb.buf);
1417 else if (starts_with(sb.buf, "refs/"))
1419 else if (!get_oid_hex(sb.buf, &oid)) {
1420 strbuf_reset(&sb);
1421 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1422 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1423 goto got_nothing;
1424 else /* bisect */
1426 return strbuf_detach(&sb, NULL);
1428 got_nothing:
1429 strbuf_release(&sb);
1430 return NULL;
1433 struct grab_1st_switch_cbdata {
1434 struct strbuf buf;
1435 struct object_id noid;
1438 static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
1439 const char *email, timestamp_t timestamp, int tz,
1440 const char *message, void *cb_data)
1442 struct grab_1st_switch_cbdata *cb = cb_data;
1443 const char *target = NULL, *end;
1445 if (!skip_prefix(message, "checkout: moving from ", &message))
1446 return 0;
1447 target = strstr(message, " to ");
1448 if (!target)
1449 return 0;
1450 target += strlen(" to ");
1451 strbuf_reset(&cb->buf);
1452 oidcpy(&cb->noid, noid);
1453 end = strchrnul(target, '\n');
1454 strbuf_add(&cb->buf, target, end - target);
1455 if (!strcmp(cb->buf.buf, "HEAD")) {
1456 /* HEAD is relative. Resolve it to the right reflog entry. */
1457 strbuf_reset(&cb->buf);
1458 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1460 return 1;
1463 static void wt_status_get_detached_from(struct wt_status_state *state)
1465 struct grab_1st_switch_cbdata cb;
1466 struct commit *commit;
1467 struct object_id oid;
1468 char *ref = NULL;
1470 strbuf_init(&cb.buf, 0);
1471 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1472 strbuf_release(&cb.buf);
1473 return;
1476 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref) == 1 &&
1477 /* sha1 is a commit? match without further lookup */
1478 (!oidcmp(&cb.noid, &oid) ||
1479 /* perhaps sha1 is a tag, try to dereference to a commit */
1480 ((commit = lookup_commit_reference_gently(&oid, 1)) != NULL &&
1481 !oidcmp(&cb.noid, &commit->object.oid)))) {
1482 const char *from = ref;
1483 if (!skip_prefix(from, "refs/tags/", &from))
1484 skip_prefix(from, "refs/remotes/", &from);
1485 state->detached_from = xstrdup(from);
1486 } else
1487 state->detached_from =
1488 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
1489 oidcpy(&state->detached_oid, &cb.noid);
1490 state->detached_at = !get_oid("HEAD", &oid) &&
1491 !oidcmp(&oid, &state->detached_oid);
1493 free(ref);
1494 strbuf_release(&cb.buf);
1497 int wt_status_check_rebase(const struct worktree *wt,
1498 struct wt_status_state *state)
1500 struct stat st;
1502 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1503 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1504 state->am_in_progress = 1;
1505 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1506 state->am_empty_patch = 1;
1507 } else {
1508 state->rebase_in_progress = 1;
1509 state->branch = get_branch(wt, "rebase-apply/head-name");
1510 state->onto = get_branch(wt, "rebase-apply/onto");
1512 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1513 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1514 state->rebase_interactive_in_progress = 1;
1515 else
1516 state->rebase_in_progress = 1;
1517 state->branch = get_branch(wt, "rebase-merge/head-name");
1518 state->onto = get_branch(wt, "rebase-merge/onto");
1519 } else
1520 return 0;
1521 return 1;
1524 int wt_status_check_bisect(const struct worktree *wt,
1525 struct wt_status_state *state)
1527 struct stat st;
1529 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1530 state->bisect_in_progress = 1;
1531 state->branch = get_branch(wt, "BISECT_START");
1532 return 1;
1534 return 0;
1537 void wt_status_get_state(struct wt_status_state *state,
1538 int get_detached_from)
1540 struct stat st;
1541 struct object_id oid;
1543 if (!stat(git_path_merge_head(), &st)) {
1544 state->merge_in_progress = 1;
1545 } else if (wt_status_check_rebase(NULL, state)) {
1546 ; /* all set */
1547 } else if (!stat(git_path_cherry_pick_head(), &st) &&
1548 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1549 state->cherry_pick_in_progress = 1;
1550 oidcpy(&state->cherry_pick_head_oid, &oid);
1552 wt_status_check_bisect(NULL, state);
1553 if (!stat(git_path_revert_head(), &st) &&
1554 !get_oid("REVERT_HEAD", &oid)) {
1555 state->revert_in_progress = 1;
1556 oidcpy(&state->revert_head_oid, &oid);
1559 if (get_detached_from)
1560 wt_status_get_detached_from(state);
1563 static void wt_longstatus_print_state(struct wt_status *s,
1564 struct wt_status_state *state)
1566 const char *state_color = color(WT_STATUS_HEADER, s);
1567 if (state->merge_in_progress)
1568 show_merge_in_progress(s, state, state_color);
1569 else if (state->am_in_progress)
1570 show_am_in_progress(s, state, state_color);
1571 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1572 show_rebase_in_progress(s, state, state_color);
1573 else if (state->cherry_pick_in_progress)
1574 show_cherry_pick_in_progress(s, state, state_color);
1575 else if (state->revert_in_progress)
1576 show_revert_in_progress(s, state, state_color);
1577 if (state->bisect_in_progress)
1578 show_bisect_in_progress(s, state, state_color);
1581 static void wt_longstatus_print(struct wt_status *s)
1583 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1584 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1585 struct wt_status_state state;
1587 memset(&state, 0, sizeof(state));
1588 wt_status_get_state(&state,
1589 s->branch && !strcmp(s->branch, "HEAD"));
1591 if (s->branch) {
1592 const char *on_what = _("On branch ");
1593 const char *branch_name = s->branch;
1594 if (!strcmp(branch_name, "HEAD")) {
1595 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1596 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1597 if (state.rebase_interactive_in_progress)
1598 on_what = _("interactive rebase in progress; onto ");
1599 else
1600 on_what = _("rebase in progress; onto ");
1601 branch_name = state.onto;
1602 } else if (state.detached_from) {
1603 branch_name = state.detached_from;
1604 if (state.detached_at)
1605 on_what = _("HEAD detached at ");
1606 else
1607 on_what = _("HEAD detached from ");
1608 } else {
1609 branch_name = "";
1610 on_what = _("Not currently on any branch.");
1612 } else
1613 skip_prefix(branch_name, "refs/heads/", &branch_name);
1614 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1615 status_printf_more(s, branch_status_color, "%s", on_what);
1616 status_printf_more(s, branch_color, "%s\n", branch_name);
1617 if (!s->is_initial)
1618 wt_longstatus_print_tracking(s);
1621 wt_longstatus_print_state(s, &state);
1622 free(state.branch);
1623 free(state.onto);
1624 free(state.detached_from);
1626 if (s->is_initial) {
1627 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1628 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1629 s->commit_template
1630 ? _("Initial commit")
1631 : _("No commits yet"));
1632 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1635 wt_longstatus_print_updated(s);
1636 wt_longstatus_print_unmerged(s);
1637 wt_longstatus_print_changed(s);
1638 if (s->submodule_summary &&
1639 (!s->ignore_submodule_arg ||
1640 strcmp(s->ignore_submodule_arg, "all"))) {
1641 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1642 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1644 if (s->show_untracked_files) {
1645 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1646 if (s->show_ignored_mode)
1647 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1648 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1649 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1650 status_printf_ln(s, GIT_COLOR_NORMAL,
1651 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1652 "may speed it up, but you have to be careful not to forget to add\n"
1653 "new files yourself (see 'git help status')."),
1654 s->untracked_in_ms / 1000.0);
1656 } else if (s->commitable)
1657 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1658 s->hints
1659 ? _(" (use -u option to show untracked files)") : "");
1661 if (s->verbose)
1662 wt_longstatus_print_verbose(s);
1663 if (!s->commitable) {
1664 if (s->amend)
1665 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1666 else if (s->nowarn)
1667 ; /* nothing */
1668 else if (s->workdir_dirty) {
1669 if (s->hints)
1670 printf(_("no changes added to commit "
1671 "(use \"git add\" and/or \"git commit -a\")\n"));
1672 else
1673 printf(_("no changes added to commit\n"));
1674 } else if (s->untracked.nr) {
1675 if (s->hints)
1676 printf(_("nothing added to commit but untracked files "
1677 "present (use \"git add\" to track)\n"));
1678 else
1679 printf(_("nothing added to commit but untracked files present\n"));
1680 } else if (s->is_initial) {
1681 if (s->hints)
1682 printf(_("nothing to commit (create/copy files "
1683 "and use \"git add\" to track)\n"));
1684 else
1685 printf(_("nothing to commit\n"));
1686 } else if (!s->show_untracked_files) {
1687 if (s->hints)
1688 printf(_("nothing to commit (use -u to show untracked files)\n"));
1689 else
1690 printf(_("nothing to commit\n"));
1691 } else
1692 printf(_("nothing to commit, working tree clean\n"));
1694 if(s->show_stash)
1695 wt_longstatus_print_stash_summary(s);
1698 static void wt_shortstatus_unmerged(struct string_list_item *it,
1699 struct wt_status *s)
1701 struct wt_status_change_data *d = it->util;
1702 const char *how = "??";
1704 switch (d->stagemask) {
1705 case 1: how = "DD"; break; /* both deleted */
1706 case 2: how = "AU"; break; /* added by us */
1707 case 3: how = "UD"; break; /* deleted by them */
1708 case 4: how = "UA"; break; /* added by them */
1709 case 5: how = "DU"; break; /* deleted by us */
1710 case 6: how = "AA"; break; /* both added */
1711 case 7: how = "UU"; break; /* both modified */
1713 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1714 if (s->null_termination) {
1715 fprintf(stdout, " %s%c", it->string, 0);
1716 } else {
1717 struct strbuf onebuf = STRBUF_INIT;
1718 const char *one;
1719 one = quote_path(it->string, s->prefix, &onebuf);
1720 printf(" %s\n", one);
1721 strbuf_release(&onebuf);
1725 static void wt_shortstatus_status(struct string_list_item *it,
1726 struct wt_status *s)
1728 struct wt_status_change_data *d = it->util;
1730 if (d->index_status)
1731 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1732 else
1733 putchar(' ');
1734 if (d->worktree_status)
1735 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1736 else
1737 putchar(' ');
1738 putchar(' ');
1739 if (s->null_termination) {
1740 fprintf(stdout, "%s%c", it->string, 0);
1741 if (d->rename_source)
1742 fprintf(stdout, "%s%c", d->rename_source, 0);
1743 } else {
1744 struct strbuf onebuf = STRBUF_INIT;
1745 const char *one;
1747 if (d->rename_source) {
1748 one = quote_path(d->rename_source, s->prefix, &onebuf);
1749 if (*one != '"' && strchr(one, ' ') != NULL) {
1750 putchar('"');
1751 strbuf_addch(&onebuf, '"');
1752 one = onebuf.buf;
1754 printf("%s -> ", one);
1755 strbuf_release(&onebuf);
1757 one = quote_path(it->string, s->prefix, &onebuf);
1758 if (*one != '"' && strchr(one, ' ') != NULL) {
1759 putchar('"');
1760 strbuf_addch(&onebuf, '"');
1761 one = onebuf.buf;
1763 printf("%s\n", one);
1764 strbuf_release(&onebuf);
1768 static void wt_shortstatus_other(struct string_list_item *it,
1769 struct wt_status *s, const char *sign)
1771 if (s->null_termination) {
1772 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1773 } else {
1774 struct strbuf onebuf = STRBUF_INIT;
1775 const char *one;
1776 one = quote_path(it->string, s->prefix, &onebuf);
1777 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1778 printf(" %s\n", one);
1779 strbuf_release(&onebuf);
1783 static void wt_shortstatus_print_tracking(struct wt_status *s)
1785 struct branch *branch;
1786 const char *header_color = color(WT_STATUS_HEADER, s);
1787 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1788 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1790 const char *base;
1791 char *short_base;
1792 const char *branch_name;
1793 int num_ours, num_theirs, sti;
1794 int upstream_is_gone = 0;
1796 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1798 if (!s->branch)
1799 return;
1800 branch_name = s->branch;
1802 #define LABEL(string) (s->no_gettext ? (string) : _(string))
1804 if (s->is_initial)
1805 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
1807 if (!strcmp(s->branch, "HEAD")) {
1808 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1809 LABEL(N_("HEAD (no branch)")));
1810 goto conclude;
1813 skip_prefix(branch_name, "refs/heads/", &branch_name);
1815 branch = branch_get(branch_name);
1817 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1819 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
1820 s->ahead_behind_flags);
1821 if (sti < 0) {
1822 if (!base)
1823 goto conclude;
1825 upstream_is_gone = 1;
1828 short_base = shorten_unambiguous_ref(base, 0);
1829 color_fprintf(s->fp, header_color, "...");
1830 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
1831 free(short_base);
1833 if (!upstream_is_gone && !sti)
1834 goto conclude;
1836 color_fprintf(s->fp, header_color, " [");
1837 if (upstream_is_gone) {
1838 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
1839 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
1840 color_fprintf(s->fp, header_color, LABEL(N_("different")));
1841 } else if (!num_ours) {
1842 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
1843 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1844 } else if (!num_theirs) {
1845 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1846 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1847 } else {
1848 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1849 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1850 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
1851 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1854 color_fprintf(s->fp, header_color, "]");
1855 conclude:
1856 fputc(s->null_termination ? '\0' : '\n', s->fp);
1859 static void wt_shortstatus_print(struct wt_status *s)
1861 struct string_list_item *it;
1863 if (s->show_branch)
1864 wt_shortstatus_print_tracking(s);
1866 for_each_string_list_item(it, &s->change) {
1867 struct wt_status_change_data *d = it->util;
1869 if (d->stagemask)
1870 wt_shortstatus_unmerged(it, s);
1871 else
1872 wt_shortstatus_status(it, s);
1874 for_each_string_list_item(it, &s->untracked)
1875 wt_shortstatus_other(it, s, "??");
1877 for_each_string_list_item(it, &s->ignored)
1878 wt_shortstatus_other(it, s, "!!");
1881 static void wt_porcelain_print(struct wt_status *s)
1883 s->use_color = 0;
1884 s->relative_paths = 0;
1885 s->prefix = NULL;
1886 s->no_gettext = 1;
1887 wt_shortstatus_print(s);
1891 * Print branch information for porcelain v2 output. These lines
1892 * are printed when the '--branch' parameter is given.
1894 * # branch.oid <commit><eol>
1895 * # branch.head <head><eol>
1896 * [# branch.upstream <upstream><eol>
1897 * [# branch.ab +<ahead> -<behind><eol>]]
1899 * <commit> ::= the current commit hash or the the literal
1900 * "(initial)" to indicate an initialized repo
1901 * with no commits.
1903 * <head> ::= <branch_name> the current branch name or
1904 * "(detached)" literal when detached head or
1905 * "(unknown)" when something is wrong.
1907 * <upstream> ::= the upstream branch name, when set.
1909 * <ahead> ::= integer ahead value or '?'.
1911 * <behind> ::= integer behind value or '?'.
1913 * The end-of-line is defined by the -z flag.
1915 * <eol> ::= NUL when -z,
1916 * LF when NOT -z.
1918 * When an upstream is set and present, the 'branch.ab' line will
1919 * be printed with the ahead/behind counts for the branch and the
1920 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1921 * are different, '?' will be substituted for the actual count.
1923 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1925 struct branch *branch;
1926 const char *base;
1927 const char *branch_name;
1928 struct wt_status_state state;
1929 int ab_info, nr_ahead, nr_behind;
1930 char eol = s->null_termination ? '\0' : '\n';
1932 memset(&state, 0, sizeof(state));
1933 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1935 fprintf(s->fp, "# branch.oid %s%c",
1936 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1937 eol);
1939 if (!s->branch)
1940 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1941 else {
1942 if (!strcmp(s->branch, "HEAD")) {
1943 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1945 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1946 branch_name = state.onto;
1947 else if (state.detached_from)
1948 branch_name = state.detached_from;
1949 else
1950 branch_name = "";
1951 } else {
1952 branch_name = NULL;
1953 skip_prefix(s->branch, "refs/heads/", &branch_name);
1955 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1958 /* Lookup stats on the upstream tracking branch, if set. */
1959 branch = branch_get(branch_name);
1960 base = NULL;
1961 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
1962 &base, s->ahead_behind_flags);
1963 if (base) {
1964 base = shorten_unambiguous_ref(base, 0);
1965 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1966 free((char *)base);
1968 if (ab_info > 0) {
1969 /* different */
1970 if (nr_ahead || nr_behind)
1971 fprintf(s->fp, "# branch.ab +%d -%d%c",
1972 nr_ahead, nr_behind, eol);
1973 else
1974 fprintf(s->fp, "# branch.ab +? -?%c",
1975 eol);
1976 } else if (!ab_info) {
1977 /* same */
1978 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
1983 free(state.branch);
1984 free(state.onto);
1985 free(state.detached_from);
1989 * Convert various submodule status values into a
1990 * fixed-length string of characters in the buffer provided.
1992 static void wt_porcelain_v2_submodule_state(
1993 struct wt_status_change_data *d,
1994 char sub[5])
1996 if (S_ISGITLINK(d->mode_head) ||
1997 S_ISGITLINK(d->mode_index) ||
1998 S_ISGITLINK(d->mode_worktree)) {
1999 sub[0] = 'S';
2000 sub[1] = d->new_submodule_commits ? 'C' : '.';
2001 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2002 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2003 } else {
2004 sub[0] = 'N';
2005 sub[1] = '.';
2006 sub[2] = '.';
2007 sub[3] = '.';
2009 sub[4] = 0;
2013 * Fix-up changed entries before we print them.
2015 static void wt_porcelain_v2_fix_up_changed(
2016 struct string_list_item *it,
2017 struct wt_status *s)
2019 struct wt_status_change_data *d = it->util;
2021 if (!d->index_status) {
2023 * This entry is unchanged in the index (relative to the head).
2024 * Therefore, the collect_updated_cb was never called for this
2025 * entry (during the head-vs-index scan) and so the head column
2026 * fields were never set.
2028 * We must have data for the index column (from the
2029 * index-vs-worktree scan (otherwise, this entry should not be
2030 * in the list of changes)).
2032 * Copy index column fields to the head column, so that our
2033 * output looks complete.
2035 assert(d->mode_head == 0);
2036 d->mode_head = d->mode_index;
2037 oidcpy(&d->oid_head, &d->oid_index);
2040 if (!d->worktree_status) {
2042 * This entry is unchanged in the worktree (relative to the index).
2043 * Therefore, the collect_changed_cb was never called for this entry
2044 * (during the index-vs-worktree scan) and so the worktree column
2045 * fields were never set.
2047 * We must have data for the index column (from the head-vs-index
2048 * scan).
2050 * Copy the index column fields to the worktree column so that
2051 * our output looks complete.
2053 * Note that we only have a mode field in the worktree column
2054 * because the scan code tries really hard to not have to compute it.
2056 assert(d->mode_worktree == 0);
2057 d->mode_worktree = d->mode_index;
2062 * Print porcelain v2 info for tracked entries with changes.
2064 static void wt_porcelain_v2_print_changed_entry(
2065 struct string_list_item *it,
2066 struct wt_status *s)
2068 struct wt_status_change_data *d = it->util;
2069 struct strbuf buf = STRBUF_INIT;
2070 struct strbuf buf_from = STRBUF_INIT;
2071 const char *path = NULL;
2072 const char *path_from = NULL;
2073 char key[3];
2074 char submodule_token[5];
2075 char sep_char, eol_char;
2077 wt_porcelain_v2_fix_up_changed(it, s);
2078 wt_porcelain_v2_submodule_state(d, submodule_token);
2080 key[0] = d->index_status ? d->index_status : '.';
2081 key[1] = d->worktree_status ? d->worktree_status : '.';
2082 key[2] = 0;
2084 if (s->null_termination) {
2086 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2087 * A single NUL character separates them.
2089 sep_char = '\0';
2090 eol_char = '\0';
2091 path = it->string;
2092 path_from = d->rename_source;
2093 } else {
2095 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2096 * The source path is only present when necessary.
2097 * A single TAB separates them (because paths can contain spaces
2098 * which are not escaped and C-quoting does escape TAB characters).
2100 sep_char = '\t';
2101 eol_char = '\n';
2102 path = quote_path(it->string, s->prefix, &buf);
2103 if (d->rename_source)
2104 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
2107 if (path_from)
2108 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2109 key, submodule_token,
2110 d->mode_head, d->mode_index, d->mode_worktree,
2111 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2112 d->rename_status, d->rename_score,
2113 path, sep_char, path_from, eol_char);
2114 else
2115 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2116 key, submodule_token,
2117 d->mode_head, d->mode_index, d->mode_worktree,
2118 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2119 path, eol_char);
2121 strbuf_release(&buf);
2122 strbuf_release(&buf_from);
2126 * Print porcelain v2 status info for unmerged entries.
2128 static void wt_porcelain_v2_print_unmerged_entry(
2129 struct string_list_item *it,
2130 struct wt_status *s)
2132 struct wt_status_change_data *d = it->util;
2133 const struct cache_entry *ce;
2134 struct strbuf buf_index = STRBUF_INIT;
2135 const char *path_index = NULL;
2136 int pos, stage, sum;
2137 struct {
2138 int mode;
2139 struct object_id oid;
2140 } stages[3];
2141 char *key;
2142 char submodule_token[5];
2143 char unmerged_prefix = 'u';
2144 char eol_char = s->null_termination ? '\0' : '\n';
2146 wt_porcelain_v2_submodule_state(d, submodule_token);
2148 switch (d->stagemask) {
2149 case 1: key = "DD"; break; /* both deleted */
2150 case 2: key = "AU"; break; /* added by us */
2151 case 3: key = "UD"; break; /* deleted by them */
2152 case 4: key = "UA"; break; /* added by them */
2153 case 5: key = "DU"; break; /* deleted by us */
2154 case 6: key = "AA"; break; /* both added */
2155 case 7: key = "UU"; break; /* both modified */
2156 default:
2157 die("BUG: unhandled unmerged status %x", d->stagemask);
2161 * Disregard d.aux.porcelain_v2 data that we accumulated
2162 * for the head and index columns during the scans and
2163 * replace with the actual stage data.
2165 * Note that this is a last-one-wins for each the individual
2166 * stage [123] columns in the event of multiple cache entries
2167 * for same stage.
2169 memset(stages, 0, sizeof(stages));
2170 sum = 0;
2171 pos = cache_name_pos(it->string, strlen(it->string));
2172 assert(pos < 0);
2173 pos = -pos-1;
2174 while (pos < active_nr) {
2175 ce = active_cache[pos++];
2176 stage = ce_stage(ce);
2177 if (strcmp(ce->name, it->string) || !stage)
2178 break;
2179 stages[stage - 1].mode = ce->ce_mode;
2180 oidcpy(&stages[stage - 1].oid, &ce->oid);
2181 sum |= (1 << (stage - 1));
2183 if (sum != d->stagemask)
2184 die("BUG: observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2186 if (s->null_termination)
2187 path_index = it->string;
2188 else
2189 path_index = quote_path(it->string, s->prefix, &buf_index);
2191 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2192 unmerged_prefix, key, submodule_token,
2193 stages[0].mode, /* stage 1 */
2194 stages[1].mode, /* stage 2 */
2195 stages[2].mode, /* stage 3 */
2196 d->mode_worktree,
2197 oid_to_hex(&stages[0].oid), /* stage 1 */
2198 oid_to_hex(&stages[1].oid), /* stage 2 */
2199 oid_to_hex(&stages[2].oid), /* stage 3 */
2200 path_index,
2201 eol_char);
2203 strbuf_release(&buf_index);
2207 * Print porcelain V2 status info for untracked and ignored entries.
2209 static void wt_porcelain_v2_print_other(
2210 struct string_list_item *it,
2211 struct wt_status *s,
2212 char prefix)
2214 struct strbuf buf = STRBUF_INIT;
2215 const char *path;
2216 char eol_char;
2218 if (s->null_termination) {
2219 path = it->string;
2220 eol_char = '\0';
2221 } else {
2222 path = quote_path(it->string, s->prefix, &buf);
2223 eol_char = '\n';
2226 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2228 strbuf_release(&buf);
2232 * Print porcelain V2 status.
2234 * [<v2_branch>]
2235 * [<v2_changed_items>]*
2236 * [<v2_unmerged_items>]*
2237 * [<v2_untracked_items>]*
2238 * [<v2_ignored_items>]*
2241 static void wt_porcelain_v2_print(struct wt_status *s)
2243 struct wt_status_change_data *d;
2244 struct string_list_item *it;
2245 int i;
2247 if (s->show_branch)
2248 wt_porcelain_v2_print_tracking(s);
2250 for (i = 0; i < s->change.nr; i++) {
2251 it = &(s->change.items[i]);
2252 d = it->util;
2253 if (!d->stagemask)
2254 wt_porcelain_v2_print_changed_entry(it, s);
2257 for (i = 0; i < s->change.nr; i++) {
2258 it = &(s->change.items[i]);
2259 d = it->util;
2260 if (d->stagemask)
2261 wt_porcelain_v2_print_unmerged_entry(it, s);
2264 for (i = 0; i < s->untracked.nr; i++) {
2265 it = &(s->untracked.items[i]);
2266 wt_porcelain_v2_print_other(it, s, '?');
2269 for (i = 0; i < s->ignored.nr; i++) {
2270 it = &(s->ignored.items[i]);
2271 wt_porcelain_v2_print_other(it, s, '!');
2275 void wt_status_print(struct wt_status *s)
2277 switch (s->status_format) {
2278 case STATUS_FORMAT_SHORT:
2279 wt_shortstatus_print(s);
2280 break;
2281 case STATUS_FORMAT_PORCELAIN:
2282 wt_porcelain_print(s);
2283 break;
2284 case STATUS_FORMAT_PORCELAIN_V2:
2285 wt_porcelain_v2_print(s);
2286 break;
2287 case STATUS_FORMAT_UNSPECIFIED:
2288 die("BUG: finalize_deferred_config() should have been called");
2289 break;
2290 case STATUS_FORMAT_NONE:
2291 case STATUS_FORMAT_LONG:
2292 wt_longstatus_print(s);
2293 break;
2298 * Returns 1 if there are unstaged changes, 0 otherwise.
2300 int has_unstaged_changes(int ignore_submodules)
2302 struct rev_info rev_info;
2303 int result;
2305 init_revisions(&rev_info, NULL);
2306 if (ignore_submodules) {
2307 rev_info.diffopt.flags.ignore_submodules = 1;
2308 rev_info.diffopt.flags.override_submodule_config = 1;
2310 rev_info.diffopt.flags.quick = 1;
2311 diff_setup_done(&rev_info.diffopt);
2312 result = run_diff_files(&rev_info, 0);
2313 return diff_result_code(&rev_info.diffopt, result);
2317 * Returns 1 if there are uncommitted changes, 0 otherwise.
2319 int has_uncommitted_changes(int ignore_submodules)
2321 struct rev_info rev_info;
2322 int result;
2324 if (is_cache_unborn())
2325 return 0;
2327 init_revisions(&rev_info, NULL);
2328 if (ignore_submodules)
2329 rev_info.diffopt.flags.ignore_submodules = 1;
2330 rev_info.diffopt.flags.quick = 1;
2331 add_head_to_pending(&rev_info);
2332 diff_setup_done(&rev_info.diffopt);
2333 result = run_diff_index(&rev_info, 1);
2334 return diff_result_code(&rev_info.diffopt, result);
2338 * If the work tree has unstaged or uncommitted changes, dies with the
2339 * appropriate message.
2341 int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2343 struct lock_file lock_file = LOCK_INIT;
2344 int err = 0, fd;
2346 fd = hold_locked_index(&lock_file, 0);
2347 refresh_cache(REFRESH_QUIET);
2348 if (0 <= fd)
2349 update_index_if_able(&the_index, &lock_file);
2350 rollback_lock_file(&lock_file);
2352 if (has_unstaged_changes(ignore_submodules)) {
2353 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2354 error(_("cannot %s: You have unstaged changes."), _(action));
2355 err = 1;
2358 if (has_uncommitted_changes(ignore_submodules)) {
2359 if (err)
2360 error(_("additionally, your index contains uncommitted changes."));
2361 else
2362 error(_("cannot %s: Your index contains uncommitted changes."),
2363 _(action));
2364 err = 1;
2367 if (err) {
2368 if (hint)
2369 error("%s", hint);
2370 if (!gently)
2371 exit(128);
2374 return err;