The eighth batch
[alt-git.git] / wt-status.c
blob3162241a570cb60393607f06a8a12963b71afb78
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 "strvec.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"
20 #include "sequencer.h"
21 #include "fsmonitor-settings.h"
23 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
24 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
26 static const char cut_line[] =
27 "------------------------ >8 ------------------------\n";
29 static char default_wt_status_colors[][COLOR_MAXLEN] = {
30 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
31 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
32 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
33 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
34 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
35 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
36 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
37 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
38 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
41 static const char *color(int slot, struct wt_status *s)
43 const char *c = "";
44 if (want_color(s->use_color))
45 c = s->color_palette[slot];
46 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
47 c = s->color_palette[WT_STATUS_HEADER];
48 return c;
51 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
52 const char *fmt, va_list ap, const char *trail)
54 struct strbuf sb = STRBUF_INIT;
55 struct strbuf linebuf = STRBUF_INIT;
56 const char *line, *eol;
58 strbuf_vaddf(&sb, fmt, ap);
59 if (!sb.len) {
60 if (s->display_comment_prefix) {
61 strbuf_addch(&sb, comment_line_char);
62 if (!trail)
63 strbuf_addch(&sb, ' ');
65 color_print_strbuf(s->fp, color, &sb);
66 if (trail)
67 fprintf(s->fp, "%s", trail);
68 strbuf_release(&sb);
69 return;
71 for (line = sb.buf; *line; line = eol + 1) {
72 eol = strchr(line, '\n');
74 strbuf_reset(&linebuf);
75 if (at_bol && s->display_comment_prefix) {
76 strbuf_addch(&linebuf, comment_line_char);
77 if (*line != '\n' && *line != '\t')
78 strbuf_addch(&linebuf, ' ');
80 if (eol)
81 strbuf_add(&linebuf, line, eol - line);
82 else
83 strbuf_addstr(&linebuf, line);
84 color_print_strbuf(s->fp, color, &linebuf);
85 if (eol)
86 fprintf(s->fp, "\n");
87 else
88 break;
89 at_bol = 1;
91 if (trail)
92 fprintf(s->fp, "%s", trail);
93 strbuf_release(&linebuf);
94 strbuf_release(&sb);
97 void status_printf_ln(struct wt_status *s, const char *color,
98 const char *fmt, ...)
100 va_list ap;
102 va_start(ap, fmt);
103 status_vprintf(s, 1, color, fmt, ap, "\n");
104 va_end(ap);
107 void status_printf(struct wt_status *s, const char *color,
108 const char *fmt, ...)
110 va_list ap;
112 va_start(ap, fmt);
113 status_vprintf(s, 1, color, fmt, ap, NULL);
114 va_end(ap);
117 static void status_printf_more(struct wt_status *s, const char *color,
118 const char *fmt, ...)
120 va_list ap;
122 va_start(ap, fmt);
123 status_vprintf(s, 0, color, fmt, ap, NULL);
124 va_end(ap);
127 void wt_status_prepare(struct repository *r, struct wt_status *s)
129 memset(s, 0, sizeof(*s));
130 s->repo = r;
131 memcpy(s->color_palette, default_wt_status_colors,
132 sizeof(default_wt_status_colors));
133 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
134 s->use_color = -1;
135 s->relative_paths = 1;
136 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
137 s->reference = "HEAD";
138 s->fp = stdout;
139 s->index_file = get_index_file();
140 s->change.strdup_strings = 1;
141 s->untracked.strdup_strings = 1;
142 s->ignored.strdup_strings = 1;
143 s->show_branch = -1; /* unspecified */
144 s->show_stash = 0;
145 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
146 s->display_comment_prefix = 0;
147 s->detect_rename = -1;
148 s->rename_score = -1;
149 s->rename_limit = -1;
152 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
154 int i;
155 int del_mod_conflict = 0;
156 int both_deleted = 0;
157 int not_deleted = 0;
158 const char *c = color(WT_STATUS_HEADER, s);
160 status_printf_ln(s, c, _("Unmerged paths:"));
162 for (i = 0; i < s->change.nr; i++) {
163 struct string_list_item *it = &(s->change.items[i]);
164 struct wt_status_change_data *d = it->util;
166 switch (d->stagemask) {
167 case 0:
168 break;
169 case 1:
170 both_deleted = 1;
171 break;
172 case 3:
173 case 5:
174 del_mod_conflict = 1;
175 break;
176 default:
177 not_deleted = 1;
178 break;
182 if (!s->hints)
183 return;
184 if (s->whence != FROM_COMMIT)
186 else if (!s->is_initial) {
187 if (!strcmp(s->reference, "HEAD"))
188 status_printf_ln(s, c,
189 _(" (use \"git restore --staged <file>...\" to unstage)"));
190 else
191 status_printf_ln(s, c,
192 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
193 s->reference);
194 } else
195 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
197 if (!both_deleted) {
198 if (!del_mod_conflict)
199 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
200 else
201 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
202 } else if (!del_mod_conflict && !not_deleted) {
203 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
204 } else {
205 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
209 static void wt_longstatus_print_cached_header(struct wt_status *s)
211 const char *c = color(WT_STATUS_HEADER, s);
213 status_printf_ln(s, c, _("Changes to be committed:"));
214 if (!s->hints)
215 return;
216 if (s->whence != FROM_COMMIT)
217 ; /* NEEDSWORK: use "git reset --unresolve"??? */
218 else if (!s->is_initial) {
219 if (!strcmp(s->reference, "HEAD"))
220 status_printf_ln(s, c
221 , _(" (use \"git restore --staged <file>...\" to unstage)"));
222 else
223 status_printf_ln(s, c,
224 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
225 s->reference);
226 } else
227 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
230 static void wt_longstatus_print_dirty_header(struct wt_status *s,
231 int has_deleted,
232 int has_dirty_submodules)
234 const char *c = color(WT_STATUS_HEADER, s);
236 status_printf_ln(s, c, _("Changes not staged for commit:"));
237 if (!s->hints)
238 return;
239 if (!has_deleted)
240 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
241 else
242 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
243 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
244 if (has_dirty_submodules)
245 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
248 static void wt_longstatus_print_other_header(struct wt_status *s,
249 const char *what,
250 const char *how)
252 const char *c = color(WT_STATUS_HEADER, s);
253 status_printf_ln(s, c, "%s:", what);
254 if (!s->hints)
255 return;
256 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
259 static void wt_longstatus_print_trailer(struct wt_status *s)
261 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
264 static const char *wt_status_unmerged_status_string(int stagemask)
266 switch (stagemask) {
267 case 1:
268 return _("both deleted:");
269 case 2:
270 return _("added by us:");
271 case 3:
272 return _("deleted by them:");
273 case 4:
274 return _("added by them:");
275 case 5:
276 return _("deleted by us:");
277 case 6:
278 return _("both added:");
279 case 7:
280 return _("both modified:");
281 default:
282 BUG("unhandled unmerged status %x", stagemask);
286 static const char *wt_status_diff_status_string(int status)
288 switch (status) {
289 case DIFF_STATUS_ADDED:
290 return _("new file:");
291 case DIFF_STATUS_COPIED:
292 return _("copied:");
293 case DIFF_STATUS_DELETED:
294 return _("deleted:");
295 case DIFF_STATUS_MODIFIED:
296 return _("modified:");
297 case DIFF_STATUS_RENAMED:
298 return _("renamed:");
299 case DIFF_STATUS_TYPE_CHANGED:
300 return _("typechange:");
301 case DIFF_STATUS_UNKNOWN:
302 return _("unknown:");
303 case DIFF_STATUS_UNMERGED:
304 return _("unmerged:");
305 default:
306 return NULL;
310 static int maxwidth(const char *(*label)(int), int minval, int maxval)
312 int result = 0, i;
314 for (i = minval; i <= maxval; i++) {
315 const char *s = label(i);
316 int len = s ? utf8_strwidth(s) : 0;
317 if (len > result)
318 result = len;
320 return result;
323 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
324 struct string_list_item *it)
326 const char *c = color(WT_STATUS_UNMERGED, s);
327 struct wt_status_change_data *d = it->util;
328 struct strbuf onebuf = STRBUF_INIT;
329 static char *padding;
330 static int label_width;
331 const char *one, *how;
332 int len;
334 if (!padding) {
335 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
336 label_width += strlen(" ");
337 padding = xmallocz(label_width);
338 memset(padding, ' ', label_width);
341 one = quote_path(it->string, s->prefix, &onebuf, 0);
342 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
344 how = wt_status_unmerged_status_string(d->stagemask);
345 len = label_width - utf8_strwidth(how);
346 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
347 strbuf_release(&onebuf);
350 static void wt_longstatus_print_change_data(struct wt_status *s,
351 int change_type,
352 struct string_list_item *it)
354 struct wt_status_change_data *d = it->util;
355 const char *c = color(change_type, s);
356 int status;
357 char *one_name;
358 char *two_name;
359 const char *one, *two;
360 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
361 struct strbuf extra = STRBUF_INIT;
362 static char *padding;
363 static int label_width;
364 const char *what;
365 int len;
367 if (!padding) {
368 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
369 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
370 label_width += strlen(" ");
371 padding = xmallocz(label_width);
372 memset(padding, ' ', label_width);
375 one_name = two_name = it->string;
376 switch (change_type) {
377 case WT_STATUS_UPDATED:
378 status = d->index_status;
379 break;
380 case WT_STATUS_CHANGED:
381 if (d->new_submodule_commits || d->dirty_submodule) {
382 strbuf_addstr(&extra, " (");
383 if (d->new_submodule_commits)
384 strbuf_addstr(&extra, _("new commits, "));
385 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
386 strbuf_addstr(&extra, _("modified content, "));
387 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
388 strbuf_addstr(&extra, _("untracked content, "));
389 strbuf_setlen(&extra, extra.len - 2);
390 strbuf_addch(&extra, ')');
392 status = d->worktree_status;
393 break;
394 default:
395 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
396 change_type);
400 * Only pick up the rename it's relevant. If the rename is for
401 * the changed section and we're printing the updated section,
402 * ignore it.
404 if (d->rename_status == status)
405 one_name = d->rename_source;
407 one = quote_path(one_name, s->prefix, &onebuf, 0);
408 two = quote_path(two_name, s->prefix, &twobuf, 0);
410 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
411 what = wt_status_diff_status_string(status);
412 if (!what)
413 BUG("unhandled diff status %c", status);
414 len = label_width - utf8_strwidth(what);
415 assert(len >= 0);
416 if (one_name != two_name)
417 status_printf_more(s, c, "%s%.*s%s -> %s",
418 what, len, padding, one, two);
419 else
420 status_printf_more(s, c, "%s%.*s%s",
421 what, len, padding, one);
422 if (extra.len) {
423 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
424 strbuf_release(&extra);
426 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
427 strbuf_release(&onebuf);
428 strbuf_release(&twobuf);
431 static char short_submodule_status(struct wt_status_change_data *d)
433 if (d->new_submodule_commits)
434 return 'M';
435 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
436 return 'm';
437 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
438 return '?';
439 return d->worktree_status;
442 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
443 struct diff_options *options UNUSED,
444 void *data)
446 struct wt_status *s = data;
447 int i;
449 if (!q->nr)
450 return;
451 s->workdir_dirty = 1;
452 for (i = 0; i < q->nr; i++) {
453 struct diff_filepair *p;
454 struct string_list_item *it;
455 struct wt_status_change_data *d;
457 p = q->queue[i];
458 it = string_list_insert(&s->change, p->two->path);
459 d = it->util;
460 if (!d) {
461 CALLOC_ARRAY(d, 1);
462 it->util = d;
464 if (!d->worktree_status)
465 d->worktree_status = p->status;
466 if (S_ISGITLINK(p->two->mode)) {
467 d->dirty_submodule = p->two->dirty_submodule;
468 d->new_submodule_commits = !oideq(&p->one->oid,
469 &p->two->oid);
470 if (s->status_format == STATUS_FORMAT_SHORT)
471 d->worktree_status = short_submodule_status(d);
474 switch (p->status) {
475 case DIFF_STATUS_ADDED:
476 d->mode_worktree = p->two->mode;
477 break;
479 case DIFF_STATUS_DELETED:
480 d->mode_index = p->one->mode;
481 oidcpy(&d->oid_index, &p->one->oid);
482 /* mode_worktree is zero for a delete. */
483 break;
485 case DIFF_STATUS_COPIED:
486 case DIFF_STATUS_RENAMED:
487 if (d->rename_status)
488 BUG("multiple renames on the same target? how?");
489 d->rename_source = xstrdup(p->one->path);
490 d->rename_score = p->score * 100 / MAX_SCORE;
491 d->rename_status = p->status;
492 /* fallthru */
493 case DIFF_STATUS_MODIFIED:
494 case DIFF_STATUS_TYPE_CHANGED:
495 case DIFF_STATUS_UNMERGED:
496 d->mode_index = p->one->mode;
497 d->mode_worktree = p->two->mode;
498 oidcpy(&d->oid_index, &p->one->oid);
499 break;
501 default:
502 BUG("unhandled diff-files status '%c'", p->status);
503 break;
509 static int unmerged_mask(struct index_state *istate, const char *path)
511 int pos, mask;
512 const struct cache_entry *ce;
514 pos = index_name_pos(istate, path, strlen(path));
515 if (0 <= pos)
516 return 0;
518 mask = 0;
519 pos = -pos-1;
520 while (pos < istate->cache_nr) {
521 ce = istate->cache[pos++];
522 if (strcmp(ce->name, path) || !ce_stage(ce))
523 break;
524 mask |= (1 << (ce_stage(ce) - 1));
526 return mask;
529 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
530 struct diff_options *options UNUSED,
531 void *data)
533 struct wt_status *s = data;
534 int i;
536 for (i = 0; i < q->nr; i++) {
537 struct diff_filepair *p;
538 struct string_list_item *it;
539 struct wt_status_change_data *d;
541 p = q->queue[i];
542 it = string_list_insert(&s->change, p->two->path);
543 d = it->util;
544 if (!d) {
545 CALLOC_ARRAY(d, 1);
546 it->util = d;
548 if (!d->index_status)
549 d->index_status = p->status;
550 switch (p->status) {
551 case DIFF_STATUS_ADDED:
552 /* Leave {mode,oid}_head zero for an add. */
553 d->mode_index = p->two->mode;
554 oidcpy(&d->oid_index, &p->two->oid);
555 s->committable = 1;
556 break;
557 case DIFF_STATUS_DELETED:
558 d->mode_head = p->one->mode;
559 oidcpy(&d->oid_head, &p->one->oid);
560 s->committable = 1;
561 /* Leave {mode,oid}_index zero for a delete. */
562 break;
564 case DIFF_STATUS_COPIED:
565 case DIFF_STATUS_RENAMED:
566 if (d->rename_status)
567 BUG("multiple renames on the same target? how?");
568 d->rename_source = xstrdup(p->one->path);
569 d->rename_score = p->score * 100 / MAX_SCORE;
570 d->rename_status = p->status;
571 /* fallthru */
572 case DIFF_STATUS_MODIFIED:
573 case DIFF_STATUS_TYPE_CHANGED:
574 d->mode_head = p->one->mode;
575 d->mode_index = p->two->mode;
576 oidcpy(&d->oid_head, &p->one->oid);
577 oidcpy(&d->oid_index, &p->two->oid);
578 s->committable = 1;
579 break;
580 case DIFF_STATUS_UNMERGED:
581 d->stagemask = unmerged_mask(s->repo->index,
582 p->two->path);
584 * Don't bother setting {mode,oid}_{head,index} since the print
585 * code will output the stage values directly and not use the
586 * values in these fields.
588 break;
590 default:
591 BUG("unhandled diff-index status '%c'", p->status);
592 break;
597 static void wt_status_collect_changes_worktree(struct wt_status *s)
599 struct rev_info rev;
601 repo_init_revisions(s->repo, &rev, NULL);
602 setup_revisions(0, NULL, &rev, NULL);
603 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
604 rev.diffopt.flags.dirty_submodules = 1;
605 rev.diffopt.ita_invisible_in_index = 1;
606 if (!s->show_untracked_files)
607 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
608 if (s->ignore_submodule_arg) {
609 rev.diffopt.flags.override_submodule_config = 1;
610 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
611 } else if (!rev.diffopt.flags.ignore_submodule_set &&
612 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
613 handle_ignore_submodules_arg(&rev.diffopt, "none");
614 rev.diffopt.format_callback = wt_status_collect_changed_cb;
615 rev.diffopt.format_callback_data = s;
616 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
617 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
618 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
619 copy_pathspec(&rev.prune_data, &s->pathspec);
620 run_diff_files(&rev, 0);
621 release_revisions(&rev);
624 static void wt_status_collect_changes_index(struct wt_status *s)
626 struct rev_info rev;
627 struct setup_revision_opt opt;
629 repo_init_revisions(s->repo, &rev, NULL);
630 memset(&opt, 0, sizeof(opt));
631 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
632 setup_revisions(0, NULL, &rev, &opt);
634 rev.diffopt.flags.override_submodule_config = 1;
635 rev.diffopt.ita_invisible_in_index = 1;
636 if (s->ignore_submodule_arg) {
637 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
638 } else {
640 * Unless the user did explicitly request a submodule ignore
641 * mode by passing a command line option we do not ignore any
642 * changed submodule SHA-1s when comparing index and HEAD, no
643 * matter what is configured. Otherwise the user won't be
644 * shown any submodules manually added (and which are
645 * staged to be committed), which would be really confusing.
647 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
650 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
651 rev.diffopt.format_callback = wt_status_collect_updated_cb;
652 rev.diffopt.format_callback_data = s;
653 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
654 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
655 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
658 * The `recursive` option must be enabled to allow the diff to recurse
659 * into subdirectories of sparse directory index entries. If it is not
660 * enabled, a subdirectory containing file(s) with changes is reported
661 * as "modified", rather than the modified files themselves.
663 rev.diffopt.flags.recursive = 1;
665 copy_pathspec(&rev.prune_data, &s->pathspec);
666 run_diff_index(&rev, 1);
667 release_revisions(&rev);
670 static int add_file_to_list(const struct object_id *oid,
671 struct strbuf *base, const char *path,
672 unsigned int mode, void *context)
674 struct string_list_item *it;
675 struct wt_status_change_data *d;
676 struct wt_status *s = context;
677 struct strbuf full_name = STRBUF_INIT;
679 if (S_ISDIR(mode))
680 return READ_TREE_RECURSIVE;
682 strbuf_add(&full_name, base->buf, base->len);
683 strbuf_addstr(&full_name, path);
684 it = string_list_insert(&s->change, full_name.buf);
685 d = it->util;
686 if (!d) {
687 CALLOC_ARRAY(d, 1);
688 it->util = d;
691 d->index_status = DIFF_STATUS_ADDED;
692 /* Leave {mode,oid}_head zero for adds. */
693 d->mode_index = mode;
694 oidcpy(&d->oid_index, oid);
695 s->committable = 1;
696 strbuf_release(&full_name);
697 return 0;
700 static void wt_status_collect_changes_initial(struct wt_status *s)
702 struct index_state *istate = s->repo->index;
703 int i;
705 for (i = 0; i < istate->cache_nr; i++) {
706 struct string_list_item *it;
707 struct wt_status_change_data *d;
708 const struct cache_entry *ce = istate->cache[i];
710 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
711 continue;
712 if (ce_intent_to_add(ce))
713 continue;
714 if (S_ISSPARSEDIR(ce->ce_mode)) {
716 * This is a sparse directory entry, so we want to collect all
717 * of the added files within the tree. This requires recursively
718 * expanding the trees to find the elements that are new in this
719 * tree and marking them with DIFF_STATUS_ADDED.
721 struct strbuf base = STRBUF_INIT;
722 struct pathspec ps = { 0 };
723 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
725 ps.recursive = 1;
726 ps.has_wildcard = 1;
727 ps.max_depth = -1;
729 strbuf_add(&base, ce->name, ce->ce_namelen);
730 read_tree_at(istate->repo, tree, &base, &ps,
731 add_file_to_list, s);
732 continue;
735 it = string_list_insert(&s->change, ce->name);
736 d = it->util;
737 if (!d) {
738 CALLOC_ARRAY(d, 1);
739 it->util = d;
741 if (ce_stage(ce)) {
742 d->index_status = DIFF_STATUS_UNMERGED;
743 d->stagemask |= (1 << (ce_stage(ce) - 1));
745 * Don't bother setting {mode,oid}_{head,index} since the print
746 * code will output the stage values directly and not use the
747 * values in these fields.
749 s->committable = 1;
750 } else {
751 d->index_status = DIFF_STATUS_ADDED;
752 /* Leave {mode,oid}_head zero for adds. */
753 d->mode_index = ce->ce_mode;
754 oidcpy(&d->oid_index, &ce->oid);
755 s->committable = 1;
760 static void wt_status_collect_untracked(struct wt_status *s)
762 int i;
763 struct dir_struct dir = DIR_INIT;
764 uint64_t t_begin = getnanotime();
765 struct index_state *istate = s->repo->index;
767 if (!s->show_untracked_files)
768 return;
770 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
771 dir.flags |=
772 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
773 if (s->show_ignored_mode) {
774 dir.flags |= DIR_SHOW_IGNORED_TOO;
776 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
777 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
778 } else {
779 dir.untracked = istate->untracked;
782 setup_standard_excludes(&dir);
784 fill_directory(&dir, istate, &s->pathspec);
786 for (i = 0; i < dir.nr; i++) {
787 struct dir_entry *ent = dir.entries[i];
788 if (index_name_is_other(istate, ent->name, ent->len))
789 string_list_insert(&s->untracked, ent->name);
792 for (i = 0; i < dir.ignored_nr; i++) {
793 struct dir_entry *ent = dir.ignored[i];
794 if (index_name_is_other(istate, ent->name, ent->len))
795 string_list_insert(&s->ignored, ent->name);
798 dir_clear(&dir);
800 if (advice_enabled(ADVICE_STATUS_U_OPTION))
801 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
804 static int has_unmerged(struct wt_status *s)
806 int i;
808 for (i = 0; i < s->change.nr; i++) {
809 struct wt_status_change_data *d;
810 d = s->change.items[i].util;
811 if (d->stagemask)
812 return 1;
814 return 0;
817 void wt_status_collect(struct wt_status *s)
819 trace2_region_enter("status", "worktrees", s->repo);
820 wt_status_collect_changes_worktree(s);
821 trace2_region_leave("status", "worktrees", s->repo);
823 if (s->is_initial) {
824 trace2_region_enter("status", "initial", s->repo);
825 wt_status_collect_changes_initial(s);
826 trace2_region_leave("status", "initial", s->repo);
827 } else {
828 trace2_region_enter("status", "index", s->repo);
829 wt_status_collect_changes_index(s);
830 trace2_region_leave("status", "index", s->repo);
833 trace2_region_enter("status", "untracked", s->repo);
834 wt_status_collect_untracked(s);
835 trace2_region_leave("status", "untracked", s->repo);
837 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
838 if (s->state.merge_in_progress && !has_unmerged(s))
839 s->committable = 1;
842 void wt_status_collect_free_buffers(struct wt_status *s)
844 wt_status_state_free_buffers(&s->state);
847 void wt_status_state_free_buffers(struct wt_status_state *state)
849 FREE_AND_NULL(state->branch);
850 FREE_AND_NULL(state->onto);
851 FREE_AND_NULL(state->detached_from);
854 static void wt_longstatus_print_unmerged(struct wt_status *s)
856 int shown_header = 0;
857 int i;
859 for (i = 0; i < s->change.nr; i++) {
860 struct wt_status_change_data *d;
861 struct string_list_item *it;
862 it = &(s->change.items[i]);
863 d = it->util;
864 if (!d->stagemask)
865 continue;
866 if (!shown_header) {
867 wt_longstatus_print_unmerged_header(s);
868 shown_header = 1;
870 wt_longstatus_print_unmerged_data(s, it);
872 if (shown_header)
873 wt_longstatus_print_trailer(s);
877 static void wt_longstatus_print_updated(struct wt_status *s)
879 int shown_header = 0;
880 int i;
882 for (i = 0; i < s->change.nr; i++) {
883 struct wt_status_change_data *d;
884 struct string_list_item *it;
885 it = &(s->change.items[i]);
886 d = it->util;
887 if (!d->index_status ||
888 d->index_status == DIFF_STATUS_UNMERGED)
889 continue;
890 if (!shown_header) {
891 wt_longstatus_print_cached_header(s);
892 shown_header = 1;
894 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
896 if (shown_header)
897 wt_longstatus_print_trailer(s);
901 * -1 : has delete
902 * 0 : no change
903 * 1 : some change but no delete
905 static int wt_status_check_worktree_changes(struct wt_status *s,
906 int *dirty_submodules)
908 int i;
909 int changes = 0;
911 *dirty_submodules = 0;
913 for (i = 0; i < s->change.nr; i++) {
914 struct wt_status_change_data *d;
915 d = s->change.items[i].util;
916 if (!d->worktree_status ||
917 d->worktree_status == DIFF_STATUS_UNMERGED)
918 continue;
919 if (!changes)
920 changes = 1;
921 if (d->dirty_submodule)
922 *dirty_submodules = 1;
923 if (d->worktree_status == DIFF_STATUS_DELETED)
924 changes = -1;
926 return changes;
929 static void wt_longstatus_print_changed(struct wt_status *s)
931 int i, dirty_submodules;
932 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
934 if (!worktree_changes)
935 return;
937 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
939 for (i = 0; i < s->change.nr; i++) {
940 struct wt_status_change_data *d;
941 struct string_list_item *it;
942 it = &(s->change.items[i]);
943 d = it->util;
944 if (!d->worktree_status ||
945 d->worktree_status == DIFF_STATUS_UNMERGED)
946 continue;
947 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
949 wt_longstatus_print_trailer(s);
952 static int stash_count_refs(struct object_id *ooid UNUSED,
953 struct object_id *noid UNUSED,
954 const char *email UNUSED,
955 timestamp_t timestamp UNUSED, int tz UNUSED,
956 const char *message UNUSED, void *cb_data)
958 int *c = cb_data;
959 (*c)++;
960 return 0;
963 static int count_stash_entries(void)
965 int n = 0;
966 for_each_reflog_ent("refs/stash", stash_count_refs, &n);
967 return n;
970 static void wt_longstatus_print_stash_summary(struct wt_status *s)
972 int stash_count = count_stash_entries();
974 if (stash_count > 0)
975 status_printf_ln(s, GIT_COLOR_NORMAL,
976 Q_("Your stash currently has %d entry",
977 "Your stash currently has %d entries", stash_count),
978 stash_count);
981 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
983 struct child_process sm_summary = CHILD_PROCESS_INIT;
984 struct strbuf cmd_stdout = STRBUF_INIT;
985 struct strbuf summary = STRBUF_INIT;
986 char *summary_content;
988 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
990 strvec_push(&sm_summary.args, "submodule");
991 strvec_push(&sm_summary.args, "summary");
992 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
993 strvec_push(&sm_summary.args, "--for-status");
994 strvec_push(&sm_summary.args, "--summary-limit");
995 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
996 if (!uncommitted)
997 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
999 sm_summary.git_cmd = 1;
1000 sm_summary.no_stdin = 1;
1002 capture_command(&sm_summary, &cmd_stdout, 1024);
1004 /* prepend header, only if there's an actual output */
1005 if (cmd_stdout.len) {
1006 if (uncommitted)
1007 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1008 else
1009 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1010 strbuf_addstr(&summary, "\n\n");
1012 strbuf_addbuf(&summary, &cmd_stdout);
1013 strbuf_release(&cmd_stdout);
1015 if (s->display_comment_prefix) {
1016 size_t len;
1017 summary_content = strbuf_detach(&summary, &len);
1018 strbuf_add_commented_lines(&summary, summary_content, len);
1019 free(summary_content);
1022 fputs(summary.buf, s->fp);
1023 strbuf_release(&summary);
1026 static void wt_longstatus_print_other(struct wt_status *s,
1027 struct string_list *l,
1028 const char *what,
1029 const char *how)
1031 int i;
1032 struct strbuf buf = STRBUF_INIT;
1033 static struct string_list output = STRING_LIST_INIT_DUP;
1034 struct column_options copts;
1036 if (!l->nr)
1037 return;
1039 wt_longstatus_print_other_header(s, what, how);
1041 for (i = 0; i < l->nr; i++) {
1042 struct string_list_item *it;
1043 const char *path;
1044 it = &(l->items[i]);
1045 path = quote_path(it->string, s->prefix, &buf, 0);
1046 if (column_active(s->colopts)) {
1047 string_list_append(&output, path);
1048 continue;
1050 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1051 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1052 "%s\n", path);
1055 strbuf_release(&buf);
1056 if (!column_active(s->colopts))
1057 goto conclude;
1059 strbuf_addf(&buf, "%s%s\t%s",
1060 color(WT_STATUS_HEADER, s),
1061 s->display_comment_prefix ? "#" : "",
1062 color(WT_STATUS_UNTRACKED, s));
1063 memset(&copts, 0, sizeof(copts));
1064 copts.padding = 1;
1065 copts.indent = buf.buf;
1066 if (want_color(s->use_color))
1067 copts.nl = GIT_COLOR_RESET "\n";
1068 print_columns(&output, s->colopts, &copts);
1069 string_list_clear(&output, 0);
1070 strbuf_release(&buf);
1071 conclude:
1072 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1075 size_t wt_status_locate_end(const char *s, size_t len)
1077 const char *p;
1078 struct strbuf pattern = STRBUF_INIT;
1080 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
1081 if (starts_with(s, pattern.buf + 1))
1082 len = 0;
1083 else if ((p = strstr(s, pattern.buf)))
1084 len = p - s + 1;
1085 strbuf_release(&pattern);
1086 return len;
1089 void wt_status_append_cut_line(struct strbuf *buf)
1091 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1093 strbuf_commented_addf(buf, "%s", cut_line);
1094 strbuf_add_commented_lines(buf, explanation, strlen(explanation));
1097 void wt_status_add_cut_line(FILE *fp)
1099 struct strbuf buf = STRBUF_INIT;
1101 wt_status_append_cut_line(&buf);
1102 fputs(buf.buf, fp);
1103 strbuf_release(&buf);
1106 static void wt_longstatus_print_verbose(struct wt_status *s)
1108 struct rev_info rev;
1109 struct setup_revision_opt opt;
1110 int dirty_submodules;
1111 const char *c = color(WT_STATUS_HEADER, s);
1113 repo_init_revisions(s->repo, &rev, NULL);
1114 rev.diffopt.flags.allow_textconv = 1;
1115 rev.diffopt.ita_invisible_in_index = 1;
1117 memset(&opt, 0, sizeof(opt));
1118 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
1119 setup_revisions(0, NULL, &rev, &opt);
1121 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1122 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1123 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1124 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1125 rev.diffopt.file = s->fp;
1126 rev.diffopt.close_file = 0;
1128 * If we're not going to stdout, then we definitely don't
1129 * want color, since we are going to the commit message
1130 * file (and even the "auto" setting won't work, since it
1131 * will have checked isatty on stdout). But we then do want
1132 * to insert the scissor line here to reliably remove the
1133 * diff before committing.
1135 if (s->fp != stdout) {
1136 rev.diffopt.use_color = 0;
1137 wt_status_add_cut_line(s->fp);
1139 if (s->verbose > 1 && s->committable) {
1140 /* print_updated() printed a header, so do we */
1141 if (s->fp != stdout)
1142 wt_longstatus_print_trailer(s);
1143 status_printf_ln(s, c, _("Changes to be committed:"));
1144 rev.diffopt.a_prefix = "c/";
1145 rev.diffopt.b_prefix = "i/";
1146 } /* else use prefix as per user config */
1147 run_diff_index(&rev, 1);
1148 if (s->verbose > 1 &&
1149 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1150 status_printf_ln(s, c,
1151 "--------------------------------------------------");
1152 status_printf_ln(s, c, _("Changes not staged for commit:"));
1153 setup_work_tree();
1154 rev.diffopt.a_prefix = "i/";
1155 rev.diffopt.b_prefix = "w/";
1156 run_diff_files(&rev, 0);
1158 release_revisions(&rev);
1161 static void wt_longstatus_print_tracking(struct wt_status *s)
1163 struct strbuf sb = STRBUF_INIT;
1164 const char *cp, *ep, *branch_name;
1165 struct branch *branch;
1166 char comment_line_string[3];
1167 int i;
1168 uint64_t t_begin = 0;
1170 assert(s->branch && !s->is_initial);
1171 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1172 return;
1173 branch = branch_get(branch_name);
1175 t_begin = getnanotime();
1177 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1178 return;
1180 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1181 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1182 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1183 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1184 strbuf_addf(&sb, _("\n"
1185 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1186 "You can use '--no-ahead-behind' to avoid this.\n"),
1187 t_delta_in_ms / 1000.0);
1191 i = 0;
1192 if (s->display_comment_prefix) {
1193 comment_line_string[i++] = comment_line_char;
1194 comment_line_string[i++] = ' ';
1196 comment_line_string[i] = '\0';
1198 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1199 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1200 "%s%.*s", comment_line_string,
1201 (int)(ep - cp), cp);
1202 if (s->display_comment_prefix)
1203 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1204 comment_line_char);
1205 else
1206 fputs("\n", s->fp);
1207 strbuf_release(&sb);
1210 static int uf_was_slow(struct wt_status *s)
1212 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1213 s->untracked_in_ms = 3250;
1214 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1217 static void show_merge_in_progress(struct wt_status *s,
1218 const char *color)
1220 if (has_unmerged(s)) {
1221 status_printf_ln(s, color, _("You have unmerged paths."));
1222 if (s->hints) {
1223 status_printf_ln(s, color,
1224 _(" (fix conflicts and run \"git commit\")"));
1225 status_printf_ln(s, color,
1226 _(" (use \"git merge --abort\" to abort the merge)"));
1228 } else {
1229 status_printf_ln(s, color,
1230 _("All conflicts fixed but you are still merging."));
1231 if (s->hints)
1232 status_printf_ln(s, color,
1233 _(" (use \"git commit\" to conclude merge)"));
1235 wt_longstatus_print_trailer(s);
1238 static void show_am_in_progress(struct wt_status *s,
1239 const char *color)
1241 int am_empty_patch;
1243 status_printf_ln(s, color,
1244 _("You are in the middle of an am session."));
1245 if (s->state.am_empty_patch)
1246 status_printf_ln(s, color,
1247 _("The current patch is empty."));
1248 if (s->hints) {
1249 am_empty_patch = s->state.am_empty_patch;
1250 if (!am_empty_patch)
1251 status_printf_ln(s, color,
1252 _(" (fix conflicts and then run \"git am --continue\")"));
1253 status_printf_ln(s, color,
1254 _(" (use \"git am --skip\" to skip this patch)"));
1255 if (am_empty_patch)
1256 status_printf_ln(s, color,
1257 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1258 status_printf_ln(s, color,
1259 _(" (use \"git am --abort\" to restore the original branch)"));
1261 wt_longstatus_print_trailer(s);
1264 static char *read_line_from_git_path(const char *filename)
1266 struct strbuf buf = STRBUF_INIT;
1267 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1269 if (!fp) {
1270 strbuf_release(&buf);
1271 return NULL;
1273 strbuf_getline_lf(&buf, fp);
1274 if (!fclose(fp)) {
1275 return strbuf_detach(&buf, NULL);
1276 } else {
1277 strbuf_release(&buf);
1278 return NULL;
1282 static int split_commit_in_progress(struct wt_status *s)
1284 int split_in_progress = 0;
1285 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1287 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1288 !s->branch || strcmp(s->branch, "HEAD"))
1289 return 0;
1291 head = read_line_from_git_path("HEAD");
1292 orig_head = read_line_from_git_path("ORIG_HEAD");
1293 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1294 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1296 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1297 ; /* fall through, no split in progress */
1298 else if (!strcmp(rebase_amend, rebase_orig_head))
1299 split_in_progress = !!strcmp(head, rebase_amend);
1300 else if (strcmp(orig_head, rebase_orig_head))
1301 split_in_progress = 1;
1303 free(head);
1304 free(orig_head);
1305 free(rebase_amend);
1306 free(rebase_orig_head);
1308 return split_in_progress;
1312 * Turn
1313 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1314 * into
1315 * "pick d6a2f03 some message"
1317 * The function assumes that the line does not contain useless spaces
1318 * before or after the command.
1320 static void abbrev_oid_in_line(struct strbuf *line)
1322 struct strbuf **split;
1323 int i;
1325 if (starts_with(line->buf, "exec ") ||
1326 starts_with(line->buf, "x ") ||
1327 starts_with(line->buf, "label ") ||
1328 starts_with(line->buf, "l "))
1329 return;
1331 split = strbuf_split_max(line, ' ', 3);
1332 if (split[0] && split[1]) {
1333 struct object_id oid;
1336 * strbuf_split_max left a space. Trim it and re-add
1337 * it after abbreviation.
1339 strbuf_trim(split[1]);
1340 if (!get_oid(split[1]->buf, &oid)) {
1341 strbuf_reset(split[1]);
1342 strbuf_add_unique_abbrev(split[1], &oid,
1343 DEFAULT_ABBREV);
1344 strbuf_addch(split[1], ' ');
1345 strbuf_reset(line);
1346 for (i = 0; split[i]; i++)
1347 strbuf_addbuf(line, split[i]);
1350 strbuf_list_free(split);
1353 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1355 struct strbuf line = STRBUF_INIT;
1356 FILE *f = fopen(git_path("%s", fname), "r");
1358 if (!f) {
1359 if (errno == ENOENT)
1360 return -1;
1361 die_errno("Could not open file %s for reading",
1362 git_path("%s", fname));
1364 while (!strbuf_getline_lf(&line, f)) {
1365 if (line.len && line.buf[0] == comment_line_char)
1366 continue;
1367 strbuf_trim(&line);
1368 if (!line.len)
1369 continue;
1370 abbrev_oid_in_line(&line);
1371 string_list_append(lines, line.buf);
1373 fclose(f);
1374 strbuf_release(&line);
1375 return 0;
1378 static void show_rebase_information(struct wt_status *s,
1379 const char *color)
1381 if (s->state.rebase_interactive_in_progress) {
1382 int i;
1383 int nr_lines_to_show = 2;
1385 struct string_list have_done = STRING_LIST_INIT_DUP;
1386 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1388 read_rebase_todolist("rebase-merge/done", &have_done);
1389 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1390 &yet_to_do))
1391 status_printf_ln(s, color,
1392 _("git-rebase-todo is missing."));
1393 if (have_done.nr == 0)
1394 status_printf_ln(s, color, _("No commands done."));
1395 else {
1396 status_printf_ln(s, color,
1397 Q_("Last command done (%"PRIuMAX" command done):",
1398 "Last commands done (%"PRIuMAX" commands done):",
1399 have_done.nr),
1400 (uintmax_t)have_done.nr);
1401 for (i = (have_done.nr > nr_lines_to_show)
1402 ? have_done.nr - nr_lines_to_show : 0;
1403 i < have_done.nr;
1404 i++)
1405 status_printf_ln(s, color, " %s", have_done.items[i].string);
1406 if (have_done.nr > nr_lines_to_show && s->hints)
1407 status_printf_ln(s, color,
1408 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1411 if (yet_to_do.nr == 0)
1412 status_printf_ln(s, color,
1413 _("No commands remaining."));
1414 else {
1415 status_printf_ln(s, color,
1416 Q_("Next command to do (%"PRIuMAX" remaining command):",
1417 "Next commands to do (%"PRIuMAX" remaining commands):",
1418 yet_to_do.nr),
1419 (uintmax_t)yet_to_do.nr);
1420 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1421 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1422 if (s->hints)
1423 status_printf_ln(s, color,
1424 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1426 string_list_clear(&yet_to_do, 0);
1427 string_list_clear(&have_done, 0);
1431 static void print_rebase_state(struct wt_status *s,
1432 const char *color)
1434 if (s->state.branch)
1435 status_printf_ln(s, color,
1436 _("You are currently rebasing branch '%s' on '%s'."),
1437 s->state.branch,
1438 s->state.onto);
1439 else
1440 status_printf_ln(s, color,
1441 _("You are currently rebasing."));
1444 static void show_rebase_in_progress(struct wt_status *s,
1445 const char *color)
1447 struct stat st;
1449 show_rebase_information(s, color);
1450 if (has_unmerged(s)) {
1451 print_rebase_state(s, color);
1452 if (s->hints) {
1453 status_printf_ln(s, color,
1454 _(" (fix conflicts and then run \"git rebase --continue\")"));
1455 status_printf_ln(s, color,
1456 _(" (use \"git rebase --skip\" to skip this patch)"));
1457 status_printf_ln(s, color,
1458 _(" (use \"git rebase --abort\" to check out the original branch)"));
1460 } else if (s->state.rebase_in_progress ||
1461 !stat(git_path_merge_msg(s->repo), &st)) {
1462 print_rebase_state(s, color);
1463 if (s->hints)
1464 status_printf_ln(s, color,
1465 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1466 } else if (split_commit_in_progress(s)) {
1467 if (s->state.branch)
1468 status_printf_ln(s, color,
1469 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1470 s->state.branch,
1471 s->state.onto);
1472 else
1473 status_printf_ln(s, color,
1474 _("You are currently splitting a commit during a rebase."));
1475 if (s->hints)
1476 status_printf_ln(s, color,
1477 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1478 } else {
1479 if (s->state.branch)
1480 status_printf_ln(s, color,
1481 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1482 s->state.branch,
1483 s->state.onto);
1484 else
1485 status_printf_ln(s, color,
1486 _("You are currently editing a commit during a rebase."));
1487 if (s->hints && !s->amend) {
1488 status_printf_ln(s, color,
1489 _(" (use \"git commit --amend\" to amend the current commit)"));
1490 status_printf_ln(s, color,
1491 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1494 wt_longstatus_print_trailer(s);
1497 static void show_cherry_pick_in_progress(struct wt_status *s,
1498 const char *color)
1500 if (is_null_oid(&s->state.cherry_pick_head_oid))
1501 status_printf_ln(s, color,
1502 _("Cherry-pick currently in progress."));
1503 else
1504 status_printf_ln(s, color,
1505 _("You are currently cherry-picking commit %s."),
1506 find_unique_abbrev(&s->state.cherry_pick_head_oid,
1507 DEFAULT_ABBREV));
1509 if (s->hints) {
1510 if (has_unmerged(s))
1511 status_printf_ln(s, color,
1512 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1513 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1514 status_printf_ln(s, color,
1515 _(" (run \"git cherry-pick --continue\" to continue)"));
1516 else
1517 status_printf_ln(s, color,
1518 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1519 status_printf_ln(s, color,
1520 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1521 status_printf_ln(s, color,
1522 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1524 wt_longstatus_print_trailer(s);
1527 static void show_revert_in_progress(struct wt_status *s,
1528 const char *color)
1530 if (is_null_oid(&s->state.revert_head_oid))
1531 status_printf_ln(s, color,
1532 _("Revert currently in progress."));
1533 else
1534 status_printf_ln(s, color,
1535 _("You are currently reverting commit %s."),
1536 find_unique_abbrev(&s->state.revert_head_oid,
1537 DEFAULT_ABBREV));
1538 if (s->hints) {
1539 if (has_unmerged(s))
1540 status_printf_ln(s, color,
1541 _(" (fix conflicts and run \"git revert --continue\")"));
1542 else if (is_null_oid(&s->state.revert_head_oid))
1543 status_printf_ln(s, color,
1544 _(" (run \"git revert --continue\" to continue)"));
1545 else
1546 status_printf_ln(s, color,
1547 _(" (all conflicts fixed: run \"git revert --continue\")"));
1548 status_printf_ln(s, color,
1549 _(" (use \"git revert --skip\" to skip this patch)"));
1550 status_printf_ln(s, color,
1551 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1553 wt_longstatus_print_trailer(s);
1556 static void show_bisect_in_progress(struct wt_status *s,
1557 const char *color)
1559 if (s->state.branch)
1560 status_printf_ln(s, color,
1561 _("You are currently bisecting, started from branch '%s'."),
1562 s->state.branch);
1563 else
1564 status_printf_ln(s, color,
1565 _("You are currently bisecting."));
1566 if (s->hints)
1567 status_printf_ln(s, color,
1568 _(" (use \"git bisect reset\" to get back to the original branch)"));
1569 wt_longstatus_print_trailer(s);
1572 static void show_sparse_checkout_in_use(struct wt_status *s,
1573 const char *color)
1575 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1576 return;
1578 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1579 status_printf_ln(s, color, _("You are in a sparse checkout."));
1580 else
1581 status_printf_ln(s, color,
1582 _("You are in a sparse checkout with %d%% of tracked files present."),
1583 s->state.sparse_checkout_percentage);
1584 wt_longstatus_print_trailer(s);
1588 * Extract branch information from rebase/bisect
1590 static char *get_branch(const struct worktree *wt, const char *path)
1592 struct strbuf sb = STRBUF_INIT;
1593 struct object_id oid;
1594 const char *branch_name;
1596 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1597 goto got_nothing;
1599 while (sb.len && sb.buf[sb.len - 1] == '\n')
1600 strbuf_setlen(&sb, sb.len - 1);
1601 if (!sb.len)
1602 goto got_nothing;
1603 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1604 strbuf_remove(&sb, 0, branch_name - sb.buf);
1605 else if (starts_with(sb.buf, "refs/"))
1607 else if (!get_oid_hex(sb.buf, &oid)) {
1608 strbuf_reset(&sb);
1609 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1610 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1611 goto got_nothing;
1612 else /* bisect */
1614 return strbuf_detach(&sb, NULL);
1616 got_nothing:
1617 strbuf_release(&sb);
1618 return NULL;
1621 struct grab_1st_switch_cbdata {
1622 struct strbuf buf;
1623 struct object_id noid;
1626 static int grab_1st_switch(struct object_id *ooid UNUSED,
1627 struct object_id *noid,
1628 const char *email UNUSED,
1629 timestamp_t timestamp UNUSED, int tz UNUSED,
1630 const char *message, void *cb_data)
1632 struct grab_1st_switch_cbdata *cb = cb_data;
1633 const char *target = NULL, *end;
1635 if (!skip_prefix(message, "checkout: moving from ", &message))
1636 return 0;
1637 target = strstr(message, " to ");
1638 if (!target)
1639 return 0;
1640 target += strlen(" to ");
1641 strbuf_reset(&cb->buf);
1642 oidcpy(&cb->noid, noid);
1643 end = strchrnul(target, '\n');
1644 strbuf_add(&cb->buf, target, end - target);
1645 if (!strcmp(cb->buf.buf, "HEAD")) {
1646 /* HEAD is relative. Resolve it to the right reflog entry. */
1647 strbuf_reset(&cb->buf);
1648 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1650 return 1;
1653 static void wt_status_get_detached_from(struct repository *r,
1654 struct wt_status_state *state)
1656 struct grab_1st_switch_cbdata cb;
1657 struct commit *commit;
1658 struct object_id oid;
1659 char *ref = NULL;
1661 strbuf_init(&cb.buf, 0);
1662 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1663 strbuf_release(&cb.buf);
1664 return;
1667 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref, 1) == 1 &&
1668 /* oid is a commit? match without further lookup */
1669 (oideq(&cb.noid, &oid) ||
1670 /* perhaps oid is a tag, try to dereference to a commit */
1671 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1672 oideq(&cb.noid, &commit->object.oid)))) {
1673 const char *from = ref;
1674 if (!skip_prefix(from, "refs/tags/", &from))
1675 skip_prefix(from, "refs/remotes/", &from);
1676 state->detached_from = xstrdup(from);
1677 } else
1678 state->detached_from =
1679 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
1680 oidcpy(&state->detached_oid, &cb.noid);
1681 state->detached_at = !get_oid("HEAD", &oid) &&
1682 oideq(&oid, &state->detached_oid);
1684 free(ref);
1685 strbuf_release(&cb.buf);
1688 int wt_status_check_rebase(const struct worktree *wt,
1689 struct wt_status_state *state)
1691 struct stat st;
1693 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1694 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1695 state->am_in_progress = 1;
1696 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1697 state->am_empty_patch = 1;
1698 } else {
1699 state->rebase_in_progress = 1;
1700 state->branch = get_branch(wt, "rebase-apply/head-name");
1701 state->onto = get_branch(wt, "rebase-apply/onto");
1703 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1704 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1705 state->rebase_interactive_in_progress = 1;
1706 else
1707 state->rebase_in_progress = 1;
1708 state->branch = get_branch(wt, "rebase-merge/head-name");
1709 state->onto = get_branch(wt, "rebase-merge/onto");
1710 } else
1711 return 0;
1712 return 1;
1715 int wt_status_check_bisect(const struct worktree *wt,
1716 struct wt_status_state *state)
1718 struct stat st;
1720 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1721 state->bisect_in_progress = 1;
1722 state->branch = get_branch(wt, "BISECT_START");
1723 return 1;
1725 return 0;
1728 static void wt_status_check_sparse_checkout(struct repository *r,
1729 struct wt_status_state *state)
1731 int skip_worktree = 0;
1732 int i;
1734 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1736 * Don't compute percentage of checked out files if we
1737 * aren't in a sparse checkout or would get division by 0.
1739 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1740 return;
1743 if (r->index->sparse_index) {
1744 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1745 return;
1748 for (i = 0; i < r->index->cache_nr; i++) {
1749 struct cache_entry *ce = r->index->cache[i];
1750 if (ce_skip_worktree(ce))
1751 skip_worktree++;
1754 state->sparse_checkout_percentage =
1755 100 - (100 * skip_worktree)/r->index->cache_nr;
1758 void wt_status_get_state(struct repository *r,
1759 struct wt_status_state *state,
1760 int get_detached_from)
1762 struct stat st;
1763 struct object_id oid;
1764 enum replay_action action;
1766 if (!stat(git_path_merge_head(r), &st)) {
1767 wt_status_check_rebase(NULL, state);
1768 state->merge_in_progress = 1;
1769 } else if (wt_status_check_rebase(NULL, state)) {
1770 ; /* all set */
1771 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1772 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1773 state->cherry_pick_in_progress = 1;
1774 oidcpy(&state->cherry_pick_head_oid, &oid);
1776 wt_status_check_bisect(NULL, state);
1777 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1778 !get_oid("REVERT_HEAD", &oid)) {
1779 state->revert_in_progress = 1;
1780 oidcpy(&state->revert_head_oid, &oid);
1782 if (!sequencer_get_last_command(r, &action)) {
1783 if (action == REPLAY_PICK) {
1784 state->cherry_pick_in_progress = 1;
1785 oidcpy(&state->cherry_pick_head_oid, null_oid());
1786 } else {
1787 state->revert_in_progress = 1;
1788 oidcpy(&state->revert_head_oid, null_oid());
1791 if (get_detached_from)
1792 wt_status_get_detached_from(r, state);
1793 wt_status_check_sparse_checkout(r, state);
1796 static void wt_longstatus_print_state(struct wt_status *s)
1798 const char *state_color = color(WT_STATUS_HEADER, s);
1799 struct wt_status_state *state = &s->state;
1801 if (state->merge_in_progress) {
1802 if (state->rebase_interactive_in_progress) {
1803 show_rebase_information(s, state_color);
1804 fputs("\n", s->fp);
1806 show_merge_in_progress(s, state_color);
1807 } else if (state->am_in_progress)
1808 show_am_in_progress(s, state_color);
1809 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1810 show_rebase_in_progress(s, state_color);
1811 else if (state->cherry_pick_in_progress)
1812 show_cherry_pick_in_progress(s, state_color);
1813 else if (state->revert_in_progress)
1814 show_revert_in_progress(s, state_color);
1815 if (state->bisect_in_progress)
1816 show_bisect_in_progress(s, state_color);
1818 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1819 show_sparse_checkout_in_use(s, state_color);
1822 static void wt_longstatus_print(struct wt_status *s)
1824 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1825 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1826 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1828 if (s->branch) {
1829 const char *on_what = _("On branch ");
1830 const char *branch_name = s->branch;
1831 if (!strcmp(branch_name, "HEAD")) {
1832 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1833 if (s->state.rebase_in_progress ||
1834 s->state.rebase_interactive_in_progress) {
1835 if (s->state.rebase_interactive_in_progress)
1836 on_what = _("interactive rebase in progress; onto ");
1837 else
1838 on_what = _("rebase in progress; onto ");
1839 branch_name = s->state.onto;
1840 } else if (s->state.detached_from) {
1841 branch_name = s->state.detached_from;
1842 if (s->state.detached_at)
1843 on_what = _("HEAD detached at ");
1844 else
1845 on_what = _("HEAD detached from ");
1846 } else {
1847 branch_name = "";
1848 on_what = _("Not currently on any branch.");
1850 } else
1851 skip_prefix(branch_name, "refs/heads/", &branch_name);
1852 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1853 status_printf_more(s, branch_status_color, "%s", on_what);
1854 status_printf_more(s, branch_color, "%s\n", branch_name);
1855 if (!s->is_initial)
1856 wt_longstatus_print_tracking(s);
1859 wt_longstatus_print_state(s);
1861 if (s->is_initial) {
1862 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1863 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1864 s->commit_template
1865 ? _("Initial commit")
1866 : _("No commits yet"));
1867 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1870 wt_longstatus_print_updated(s);
1871 wt_longstatus_print_unmerged(s);
1872 wt_longstatus_print_changed(s);
1873 if (s->submodule_summary &&
1874 (!s->ignore_submodule_arg ||
1875 strcmp(s->ignore_submodule_arg, "all"))) {
1876 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1877 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1879 if (s->show_untracked_files) {
1880 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1881 if (s->show_ignored_mode)
1882 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1883 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1884 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1885 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1886 status_printf_ln(s, GIT_COLOR_NORMAL,
1887 _("It took %.2f seconds to enumerate untracked files,\n"
1888 "but the results were cached, and subsequent runs may be faster."),
1889 s->untracked_in_ms / 1000.0);
1890 } else {
1891 status_printf_ln(s, GIT_COLOR_NORMAL,
1892 _("It took %.2f seconds to enumerate untracked files."),
1893 s->untracked_in_ms / 1000.0);
1895 status_printf_ln(s, GIT_COLOR_NORMAL,
1896 _("See 'git help status' for information on how to improve this."));
1897 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1899 } else if (s->committable)
1900 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1901 s->hints
1902 ? _(" (use -u option to show untracked files)") : "");
1904 if (s->verbose)
1905 wt_longstatus_print_verbose(s);
1906 if (!s->committable) {
1907 if (s->amend)
1908 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1909 else if (s->nowarn)
1910 ; /* nothing */
1911 else if (s->workdir_dirty) {
1912 if (s->hints)
1913 fprintf(s->fp, _("no changes added to commit "
1914 "(use \"git add\" and/or "
1915 "\"git commit -a\")\n"));
1916 else
1917 fprintf(s->fp, _("no changes added to "
1918 "commit\n"));
1919 } else if (s->untracked.nr) {
1920 if (s->hints)
1921 fprintf(s->fp, _("nothing added to commit but "
1922 "untracked files present (use "
1923 "\"git add\" to track)\n"));
1924 else
1925 fprintf(s->fp, _("nothing added to commit but "
1926 "untracked files present\n"));
1927 } else if (s->is_initial) {
1928 if (s->hints)
1929 fprintf(s->fp, _("nothing to commit (create/"
1930 "copy files and use \"git "
1931 "add\" to track)\n"));
1932 else
1933 fprintf(s->fp, _("nothing to commit\n"));
1934 } else if (!s->show_untracked_files) {
1935 if (s->hints)
1936 fprintf(s->fp, _("nothing to commit (use -u to "
1937 "show untracked files)\n"));
1938 else
1939 fprintf(s->fp, _("nothing to commit\n"));
1940 } else
1941 fprintf(s->fp, _("nothing to commit, working tree "
1942 "clean\n"));
1944 if(s->show_stash)
1945 wt_longstatus_print_stash_summary(s);
1948 static void wt_shortstatus_unmerged(struct string_list_item *it,
1949 struct wt_status *s)
1951 struct wt_status_change_data *d = it->util;
1952 const char *how = "??";
1954 switch (d->stagemask) {
1955 case 1: how = "DD"; break; /* both deleted */
1956 case 2: how = "AU"; break; /* added by us */
1957 case 3: how = "UD"; break; /* deleted by them */
1958 case 4: how = "UA"; break; /* added by them */
1959 case 5: how = "DU"; break; /* deleted by us */
1960 case 6: how = "AA"; break; /* both added */
1961 case 7: how = "UU"; break; /* both modified */
1963 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1964 if (s->null_termination) {
1965 fprintf(s->fp, " %s%c", it->string, 0);
1966 } else {
1967 struct strbuf onebuf = STRBUF_INIT;
1968 const char *one;
1969 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1970 fprintf(s->fp, " %s\n", one);
1971 strbuf_release(&onebuf);
1975 static void wt_shortstatus_status(struct string_list_item *it,
1976 struct wt_status *s)
1978 struct wt_status_change_data *d = it->util;
1980 if (d->index_status)
1981 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1982 else
1983 fputc(' ', s->fp);
1984 if (d->worktree_status)
1985 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1986 else
1987 fputc(' ', s->fp);
1988 fputc(' ', s->fp);
1989 if (s->null_termination) {
1990 fprintf(s->fp, "%s%c", it->string, 0);
1991 if (d->rename_source)
1992 fprintf(s->fp, "%s%c", d->rename_source, 0);
1993 } else {
1994 struct strbuf onebuf = STRBUF_INIT;
1995 const char *one;
1997 if (d->rename_source) {
1998 one = quote_path(d->rename_source, s->prefix, &onebuf,
1999 QUOTE_PATH_QUOTE_SP);
2000 fprintf(s->fp, "%s -> ", one);
2001 strbuf_release(&onebuf);
2003 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2004 fprintf(s->fp, "%s\n", one);
2005 strbuf_release(&onebuf);
2009 static void wt_shortstatus_other(struct string_list_item *it,
2010 struct wt_status *s, const char *sign)
2012 if (s->null_termination) {
2013 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2014 } else {
2015 struct strbuf onebuf = STRBUF_INIT;
2016 const char *one;
2017 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2018 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2019 fprintf(s->fp, " %s\n", one);
2020 strbuf_release(&onebuf);
2024 static void wt_shortstatus_print_tracking(struct wt_status *s)
2026 struct branch *branch;
2027 const char *header_color = color(WT_STATUS_HEADER, s);
2028 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2029 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2031 const char *base;
2032 char *short_base;
2033 const char *branch_name;
2034 int num_ours, num_theirs, sti;
2035 int upstream_is_gone = 0;
2037 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2039 if (!s->branch)
2040 return;
2041 branch_name = s->branch;
2043 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2045 if (s->is_initial)
2046 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2048 if (!strcmp(s->branch, "HEAD")) {
2049 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2050 LABEL(N_("HEAD (no branch)")));
2051 goto conclude;
2054 skip_prefix(branch_name, "refs/heads/", &branch_name);
2056 branch = branch_get(branch_name);
2058 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2060 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2061 0, s->ahead_behind_flags);
2062 if (sti < 0) {
2063 if (!base)
2064 goto conclude;
2066 upstream_is_gone = 1;
2069 short_base = shorten_unambiguous_ref(base, 0);
2070 color_fprintf(s->fp, header_color, "...");
2071 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2072 free(short_base);
2074 if (!upstream_is_gone && !sti)
2075 goto conclude;
2077 color_fprintf(s->fp, header_color, " [");
2078 if (upstream_is_gone) {
2079 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2080 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2081 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2082 } else if (!num_ours) {
2083 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2084 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2085 } else if (!num_theirs) {
2086 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2087 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2088 } else {
2089 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2090 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2091 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2092 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2095 color_fprintf(s->fp, header_color, "]");
2096 conclude:
2097 fputc(s->null_termination ? '\0' : '\n', s->fp);
2100 static void wt_shortstatus_print(struct wt_status *s)
2102 struct string_list_item *it;
2104 if (s->show_branch)
2105 wt_shortstatus_print_tracking(s);
2107 for_each_string_list_item(it, &s->change) {
2108 struct wt_status_change_data *d = it->util;
2110 if (d->stagemask)
2111 wt_shortstatus_unmerged(it, s);
2112 else
2113 wt_shortstatus_status(it, s);
2115 for_each_string_list_item(it, &s->untracked)
2116 wt_shortstatus_other(it, s, "??");
2118 for_each_string_list_item(it, &s->ignored)
2119 wt_shortstatus_other(it, s, "!!");
2122 static void wt_porcelain_print(struct wt_status *s)
2124 s->use_color = 0;
2125 s->relative_paths = 0;
2126 s->prefix = NULL;
2127 s->no_gettext = 1;
2128 wt_shortstatus_print(s);
2132 * Print branch information for porcelain v2 output. These lines
2133 * are printed when the '--branch' parameter is given.
2135 * # branch.oid <commit><eol>
2136 * # branch.head <head><eol>
2137 * [# branch.upstream <upstream><eol>
2138 * [# branch.ab +<ahead> -<behind><eol>]]
2140 * <commit> ::= the current commit hash or the literal
2141 * "(initial)" to indicate an initialized repo
2142 * with no commits.
2144 * <head> ::= <branch_name> the current branch name or
2145 * "(detached)" literal when detached head or
2146 * "(unknown)" when something is wrong.
2148 * <upstream> ::= the upstream branch name, when set.
2150 * <ahead> ::= integer ahead value or '?'.
2152 * <behind> ::= integer behind value or '?'.
2154 * The end-of-line is defined by the -z flag.
2156 * <eol> ::= NUL when -z,
2157 * LF when NOT -z.
2159 * When an upstream is set and present, the 'branch.ab' line will
2160 * be printed with the ahead/behind counts for the branch and the
2161 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2162 * are different, '?' will be substituted for the actual count.
2164 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2166 struct branch *branch;
2167 const char *base;
2168 const char *branch_name;
2169 int ab_info, nr_ahead, nr_behind;
2170 char eol = s->null_termination ? '\0' : '\n';
2172 fprintf(s->fp, "# branch.oid %s%c",
2173 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2174 eol);
2176 if (!s->branch)
2177 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2178 else {
2179 if (!strcmp(s->branch, "HEAD")) {
2180 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2182 if (s->state.rebase_in_progress ||
2183 s->state.rebase_interactive_in_progress)
2184 branch_name = s->state.onto;
2185 else if (s->state.detached_from)
2186 branch_name = s->state.detached_from;
2187 else
2188 branch_name = "";
2189 } else {
2190 branch_name = NULL;
2191 skip_prefix(s->branch, "refs/heads/", &branch_name);
2193 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2196 /* Lookup stats on the upstream tracking branch, if set. */
2197 branch = branch_get(branch_name);
2198 base = NULL;
2199 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2200 &base, 0, s->ahead_behind_flags);
2201 if (base) {
2202 base = shorten_unambiguous_ref(base, 0);
2203 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2204 free((char *)base);
2206 if (ab_info > 0) {
2207 /* different */
2208 if (nr_ahead || nr_behind)
2209 fprintf(s->fp, "# branch.ab +%d -%d%c",
2210 nr_ahead, nr_behind, eol);
2211 else
2212 fprintf(s->fp, "# branch.ab +? -?%c",
2213 eol);
2214 } else if (!ab_info) {
2215 /* same */
2216 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2223 * Print the stash count in a porcelain-friendly format
2225 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2227 int stash_count = count_stash_entries();
2228 char eol = s->null_termination ? '\0' : '\n';
2230 if (stash_count > 0)
2231 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2235 * Convert various submodule status values into a
2236 * fixed-length string of characters in the buffer provided.
2238 static void wt_porcelain_v2_submodule_state(
2239 struct wt_status_change_data *d,
2240 char sub[5])
2242 if (S_ISGITLINK(d->mode_head) ||
2243 S_ISGITLINK(d->mode_index) ||
2244 S_ISGITLINK(d->mode_worktree)) {
2245 sub[0] = 'S';
2246 sub[1] = d->new_submodule_commits ? 'C' : '.';
2247 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2248 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2249 } else {
2250 sub[0] = 'N';
2251 sub[1] = '.';
2252 sub[2] = '.';
2253 sub[3] = '.';
2255 sub[4] = 0;
2259 * Fix-up changed entries before we print them.
2261 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2263 struct wt_status_change_data *d = it->util;
2265 if (!d->index_status) {
2267 * This entry is unchanged in the index (relative to the head).
2268 * Therefore, the collect_updated_cb was never called for this
2269 * entry (during the head-vs-index scan) and so the head column
2270 * fields were never set.
2272 * We must have data for the index column (from the
2273 * index-vs-worktree scan (otherwise, this entry should not be
2274 * in the list of changes)).
2276 * Copy index column fields to the head column, so that our
2277 * output looks complete.
2279 assert(d->mode_head == 0);
2280 d->mode_head = d->mode_index;
2281 oidcpy(&d->oid_head, &d->oid_index);
2284 if (!d->worktree_status) {
2286 * This entry is unchanged in the worktree (relative to the index).
2287 * Therefore, the collect_changed_cb was never called for this entry
2288 * (during the index-vs-worktree scan) and so the worktree column
2289 * fields were never set.
2291 * We must have data for the index column (from the head-vs-index
2292 * scan).
2294 * Copy the index column fields to the worktree column so that
2295 * our output looks complete.
2297 * Note that we only have a mode field in the worktree column
2298 * because the scan code tries really hard to not have to compute it.
2300 assert(d->mode_worktree == 0);
2301 d->mode_worktree = d->mode_index;
2306 * Print porcelain v2 info for tracked entries with changes.
2308 static void wt_porcelain_v2_print_changed_entry(
2309 struct string_list_item *it,
2310 struct wt_status *s)
2312 struct wt_status_change_data *d = it->util;
2313 struct strbuf buf = STRBUF_INIT;
2314 struct strbuf buf_from = STRBUF_INIT;
2315 const char *path = NULL;
2316 const char *path_from = NULL;
2317 char key[3];
2318 char submodule_token[5];
2319 char sep_char, eol_char;
2321 wt_porcelain_v2_fix_up_changed(it);
2322 wt_porcelain_v2_submodule_state(d, submodule_token);
2324 key[0] = d->index_status ? d->index_status : '.';
2325 key[1] = d->worktree_status ? d->worktree_status : '.';
2326 key[2] = 0;
2328 if (s->null_termination) {
2330 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2331 * A single NUL character separates them.
2333 sep_char = '\0';
2334 eol_char = '\0';
2335 path = it->string;
2336 path_from = d->rename_source;
2337 } else {
2339 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2340 * The source path is only present when necessary.
2341 * A single TAB separates them (because paths can contain spaces
2342 * which are not escaped and C-quoting does escape TAB characters).
2344 sep_char = '\t';
2345 eol_char = '\n';
2346 path = quote_path(it->string, s->prefix, &buf, 0);
2347 if (d->rename_source)
2348 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2351 if (path_from)
2352 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2353 key, submodule_token,
2354 d->mode_head, d->mode_index, d->mode_worktree,
2355 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2356 d->rename_status, d->rename_score,
2357 path, sep_char, path_from, eol_char);
2358 else
2359 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2360 key, submodule_token,
2361 d->mode_head, d->mode_index, d->mode_worktree,
2362 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2363 path, eol_char);
2365 strbuf_release(&buf);
2366 strbuf_release(&buf_from);
2370 * Print porcelain v2 status info for unmerged entries.
2372 static void wt_porcelain_v2_print_unmerged_entry(
2373 struct string_list_item *it,
2374 struct wt_status *s)
2376 struct wt_status_change_data *d = it->util;
2377 struct index_state *istate = s->repo->index;
2378 const struct cache_entry *ce;
2379 struct strbuf buf_index = STRBUF_INIT;
2380 const char *path_index = NULL;
2381 int pos, stage, sum;
2382 struct {
2383 int mode;
2384 struct object_id oid;
2385 } stages[3];
2386 char *key;
2387 char submodule_token[5];
2388 char unmerged_prefix = 'u';
2389 char eol_char = s->null_termination ? '\0' : '\n';
2391 wt_porcelain_v2_submodule_state(d, submodule_token);
2393 switch (d->stagemask) {
2394 case 1: key = "DD"; break; /* both deleted */
2395 case 2: key = "AU"; break; /* added by us */
2396 case 3: key = "UD"; break; /* deleted by them */
2397 case 4: key = "UA"; break; /* added by them */
2398 case 5: key = "DU"; break; /* deleted by us */
2399 case 6: key = "AA"; break; /* both added */
2400 case 7: key = "UU"; break; /* both modified */
2401 default:
2402 BUG("unhandled unmerged status %x", d->stagemask);
2406 * Disregard d.aux.porcelain_v2 data that we accumulated
2407 * for the head and index columns during the scans and
2408 * replace with the actual stage data.
2410 * Note that this is a last-one-wins for each the individual
2411 * stage [123] columns in the event of multiple cache entries
2412 * for same stage.
2414 memset(stages, 0, sizeof(stages));
2415 sum = 0;
2416 pos = index_name_pos(istate, it->string, strlen(it->string));
2417 assert(pos < 0);
2418 pos = -pos-1;
2419 while (pos < istate->cache_nr) {
2420 ce = istate->cache[pos++];
2421 stage = ce_stage(ce);
2422 if (strcmp(ce->name, it->string) || !stage)
2423 break;
2424 stages[stage - 1].mode = ce->ce_mode;
2425 oidcpy(&stages[stage - 1].oid, &ce->oid);
2426 sum |= (1 << (stage - 1));
2428 if (sum != d->stagemask)
2429 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2431 if (s->null_termination)
2432 path_index = it->string;
2433 else
2434 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2436 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2437 unmerged_prefix, key, submodule_token,
2438 stages[0].mode, /* stage 1 */
2439 stages[1].mode, /* stage 2 */
2440 stages[2].mode, /* stage 3 */
2441 d->mode_worktree,
2442 oid_to_hex(&stages[0].oid), /* stage 1 */
2443 oid_to_hex(&stages[1].oid), /* stage 2 */
2444 oid_to_hex(&stages[2].oid), /* stage 3 */
2445 path_index,
2446 eol_char);
2448 strbuf_release(&buf_index);
2452 * Print porcelain V2 status info for untracked and ignored entries.
2454 static void wt_porcelain_v2_print_other(
2455 struct string_list_item *it,
2456 struct wt_status *s,
2457 char prefix)
2459 struct strbuf buf = STRBUF_INIT;
2460 const char *path;
2461 char eol_char;
2463 if (s->null_termination) {
2464 path = it->string;
2465 eol_char = '\0';
2466 } else {
2467 path = quote_path(it->string, s->prefix, &buf, 0);
2468 eol_char = '\n';
2471 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2473 strbuf_release(&buf);
2477 * Print porcelain V2 status.
2479 * [<v2_branch>]
2480 * [<v2_changed_items>]*
2481 * [<v2_unmerged_items>]*
2482 * [<v2_untracked_items>]*
2483 * [<v2_ignored_items>]*
2486 static void wt_porcelain_v2_print(struct wt_status *s)
2488 struct wt_status_change_data *d;
2489 struct string_list_item *it;
2490 int i;
2492 if (s->show_branch)
2493 wt_porcelain_v2_print_tracking(s);
2495 if (s->show_stash)
2496 wt_porcelain_v2_print_stash(s);
2498 for (i = 0; i < s->change.nr; i++) {
2499 it = &(s->change.items[i]);
2500 d = it->util;
2501 if (!d->stagemask)
2502 wt_porcelain_v2_print_changed_entry(it, s);
2505 for (i = 0; i < s->change.nr; i++) {
2506 it = &(s->change.items[i]);
2507 d = it->util;
2508 if (d->stagemask)
2509 wt_porcelain_v2_print_unmerged_entry(it, s);
2512 for (i = 0; i < s->untracked.nr; i++) {
2513 it = &(s->untracked.items[i]);
2514 wt_porcelain_v2_print_other(it, s, '?');
2517 for (i = 0; i < s->ignored.nr; i++) {
2518 it = &(s->ignored.items[i]);
2519 wt_porcelain_v2_print_other(it, s, '!');
2523 void wt_status_print(struct wt_status *s)
2525 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2526 trace2_data_intmax("status", s->repo, "count/untracked",
2527 s->untracked.nr);
2528 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2530 trace2_region_enter("status", "print", s->repo);
2532 switch (s->status_format) {
2533 case STATUS_FORMAT_SHORT:
2534 wt_shortstatus_print(s);
2535 break;
2536 case STATUS_FORMAT_PORCELAIN:
2537 wt_porcelain_print(s);
2538 break;
2539 case STATUS_FORMAT_PORCELAIN_V2:
2540 wt_porcelain_v2_print(s);
2541 break;
2542 case STATUS_FORMAT_UNSPECIFIED:
2543 BUG("finalize_deferred_config() should have been called");
2544 break;
2545 case STATUS_FORMAT_NONE:
2546 case STATUS_FORMAT_LONG:
2547 wt_longstatus_print(s);
2548 break;
2551 trace2_region_leave("status", "print", s->repo);
2555 * Returns 1 if there are unstaged changes, 0 otherwise.
2557 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2559 struct rev_info rev_info;
2560 int result;
2562 repo_init_revisions(r, &rev_info, NULL);
2563 if (ignore_submodules) {
2564 rev_info.diffopt.flags.ignore_submodules = 1;
2565 rev_info.diffopt.flags.override_submodule_config = 1;
2567 rev_info.diffopt.flags.quick = 1;
2568 diff_setup_done(&rev_info.diffopt);
2569 result = run_diff_files(&rev_info, 0);
2570 result = diff_result_code(&rev_info.diffopt, result);
2571 release_revisions(&rev_info);
2572 return result;
2576 * Returns 1 if there are uncommitted changes, 0 otherwise.
2578 int has_uncommitted_changes(struct repository *r,
2579 int ignore_submodules)
2581 struct rev_info rev_info;
2582 int result;
2584 if (is_index_unborn(r->index))
2585 return 0;
2587 repo_init_revisions(r, &rev_info, NULL);
2588 if (ignore_submodules)
2589 rev_info.diffopt.flags.ignore_submodules = 1;
2590 rev_info.diffopt.flags.quick = 1;
2592 add_head_to_pending(&rev_info);
2593 if (!rev_info.pending.nr) {
2595 * We have no head (or it's corrupt); use the empty tree,
2596 * which will complain if the index is non-empty.
2598 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2599 add_pending_object(&rev_info, &tree->object, "");
2602 diff_setup_done(&rev_info.diffopt);
2603 result = run_diff_index(&rev_info, 1);
2604 result = diff_result_code(&rev_info.diffopt, result);
2605 release_revisions(&rev_info);
2606 return result;
2610 * If the work tree has unstaged or uncommitted changes, dies with the
2611 * appropriate message.
2613 int require_clean_work_tree(struct repository *r,
2614 const char *action,
2615 const char *hint,
2616 int ignore_submodules,
2617 int gently)
2619 struct lock_file lock_file = LOCK_INIT;
2620 int err = 0, fd;
2622 fd = repo_hold_locked_index(r, &lock_file, 0);
2623 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2624 if (0 <= fd)
2625 repo_update_index_if_able(r, &lock_file);
2626 rollback_lock_file(&lock_file);
2628 if (has_unstaged_changes(r, ignore_submodules)) {
2629 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2630 error(_("cannot %s: You have unstaged changes."), _(action));
2631 err = 1;
2634 if (has_uncommitted_changes(r, ignore_submodules)) {
2635 if (err)
2636 error(_("additionally, your index contains uncommitted changes."));
2637 else
2638 error(_("cannot %s: Your index contains uncommitted changes."),
2639 _(action));
2640 err = 1;
2643 if (err) {
2644 if (hint)
2645 error("%s", hint);
2646 if (!gently)
2647 exit(128);
2650 return err;