The fifth batch
[alt-git.git] / wt-status.c
blobff4be071ca4f8366e4ddf926cf1fc94aea195eca
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "wt-status.h"
4 #include "object.h"
5 #include "dir.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "environment.h"
9 #include "gettext.h"
10 #include "hash.h"
11 #include "hex.h"
12 #include "object-name.h"
13 #include "path.h"
14 #include "revision.h"
15 #include "diffcore.h"
16 #include "quote.h"
17 #include "run-command.h"
18 #include "strvec.h"
19 #include "remote.h"
20 #include "refs.h"
21 #include "submodule.h"
22 #include "column.h"
23 #include "read-cache.h"
24 #include "setup.h"
25 #include "strbuf.h"
26 #include "trace.h"
27 #include "trace2.h"
28 #include "tree.h"
29 #include "utf8.h"
30 #include "worktree.h"
31 #include "lockfile.h"
32 #include "sequencer.h"
33 #include "fsmonitor-settings.h"
35 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
36 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
38 static const char cut_line[] =
39 "------------------------ >8 ------------------------\n";
41 static char default_wt_status_colors[][COLOR_MAXLEN] = {
42 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
43 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
44 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
45 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
46 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
47 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
48 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
49 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
50 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
53 static const char *color(int slot, struct wt_status *s)
55 const char *c = "";
56 if (want_color(s->use_color))
57 c = s->color_palette[slot];
58 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
59 c = s->color_palette[WT_STATUS_HEADER];
60 return c;
63 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
64 const char *fmt, va_list ap, const char *trail)
66 struct strbuf sb = STRBUF_INIT;
67 struct strbuf linebuf = STRBUF_INIT;
68 const char *line, *eol;
70 strbuf_vaddf(&sb, fmt, ap);
71 if (!sb.len) {
72 if (s->display_comment_prefix) {
73 strbuf_addstr(&sb, comment_line_str);
74 if (!trail)
75 strbuf_addch(&sb, ' ');
77 color_print_strbuf(s->fp, color, &sb);
78 if (trail)
79 fprintf(s->fp, "%s", trail);
80 strbuf_release(&sb);
81 return;
83 for (line = sb.buf; *line; line = eol + 1) {
84 eol = strchr(line, '\n');
86 strbuf_reset(&linebuf);
87 if (at_bol && s->display_comment_prefix) {
88 strbuf_addstr(&linebuf, comment_line_str);
89 if (*line != '\n' && *line != '\t')
90 strbuf_addch(&linebuf, ' ');
92 if (eol)
93 strbuf_add(&linebuf, line, eol - line);
94 else
95 strbuf_addstr(&linebuf, line);
96 color_print_strbuf(s->fp, color, &linebuf);
97 if (eol)
98 fprintf(s->fp, "\n");
99 else
100 break;
101 at_bol = 1;
103 if (trail)
104 fprintf(s->fp, "%s", trail);
105 strbuf_release(&linebuf);
106 strbuf_release(&sb);
109 void status_printf_ln(struct wt_status *s, const char *color,
110 const char *fmt, ...)
112 va_list ap;
114 va_start(ap, fmt);
115 status_vprintf(s, 1, color, fmt, ap, "\n");
116 va_end(ap);
119 void status_printf(struct wt_status *s, const char *color,
120 const char *fmt, ...)
122 va_list ap;
124 va_start(ap, fmt);
125 status_vprintf(s, 1, color, fmt, ap, NULL);
126 va_end(ap);
129 static void status_printf_more(struct wt_status *s, const char *color,
130 const char *fmt, ...)
132 va_list ap;
134 va_start(ap, fmt);
135 status_vprintf(s, 0, color, fmt, ap, NULL);
136 va_end(ap);
139 void wt_status_prepare(struct repository *r, struct wt_status *s)
141 memset(s, 0, sizeof(*s));
142 s->repo = r;
143 memcpy(s->color_palette, default_wt_status_colors,
144 sizeof(default_wt_status_colors));
145 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
146 s->use_color = -1;
147 s->relative_paths = 1;
148 s->branch = refs_resolve_refdup(get_main_ref_store(the_repository),
149 "HEAD", 0, NULL, NULL);
150 s->reference = "HEAD";
151 s->fp = stdout;
152 s->index_file = get_index_file();
153 s->change.strdup_strings = 1;
154 s->untracked.strdup_strings = 1;
155 s->ignored.strdup_strings = 1;
156 s->show_branch = -1; /* unspecified */
157 s->show_stash = 0;
158 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
159 s->display_comment_prefix = 0;
160 s->detect_rename = -1;
161 s->rename_score = -1;
162 s->rename_limit = -1;
165 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
167 int i;
168 int del_mod_conflict = 0;
169 int both_deleted = 0;
170 int not_deleted = 0;
171 const char *c = color(WT_STATUS_HEADER, s);
173 status_printf_ln(s, c, _("Unmerged paths:"));
175 for (i = 0; i < s->change.nr; i++) {
176 struct string_list_item *it = &(s->change.items[i]);
177 struct wt_status_change_data *d = it->util;
179 switch (d->stagemask) {
180 case 0:
181 break;
182 case 1:
183 both_deleted = 1;
184 break;
185 case 3:
186 case 5:
187 del_mod_conflict = 1;
188 break;
189 default:
190 not_deleted = 1;
191 break;
195 if (!s->hints)
196 return;
197 if (s->whence != FROM_COMMIT)
199 else if (!s->is_initial) {
200 if (!strcmp(s->reference, "HEAD"))
201 status_printf_ln(s, c,
202 _(" (use \"git restore --staged <file>...\" to unstage)"));
203 else
204 status_printf_ln(s, c,
205 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
206 s->reference);
207 } else
208 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
210 if (!both_deleted) {
211 if (!del_mod_conflict)
212 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
213 else
214 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
215 } else if (!del_mod_conflict && !not_deleted) {
216 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
217 } else {
218 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
222 static void wt_longstatus_print_cached_header(struct wt_status *s)
224 const char *c = color(WT_STATUS_HEADER, s);
226 status_printf_ln(s, c, _("Changes to be committed:"));
227 if (!s->hints)
228 return;
229 if (s->whence != FROM_COMMIT)
230 ; /* NEEDSWORK: use "git reset --unresolve"??? */
231 else if (!s->is_initial) {
232 if (!strcmp(s->reference, "HEAD"))
233 status_printf_ln(s, c
234 , _(" (use \"git restore --staged <file>...\" to unstage)"));
235 else
236 status_printf_ln(s, c,
237 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
238 s->reference);
239 } else
240 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
243 static void wt_longstatus_print_dirty_header(struct wt_status *s,
244 int has_deleted,
245 int has_dirty_submodules)
247 const char *c = color(WT_STATUS_HEADER, s);
249 status_printf_ln(s, c, _("Changes not staged for commit:"));
250 if (!s->hints)
251 return;
252 if (!has_deleted)
253 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
254 else
255 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
256 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
257 if (has_dirty_submodules)
258 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
261 static void wt_longstatus_print_other_header(struct wt_status *s,
262 const char *what,
263 const char *how)
265 const char *c = color(WT_STATUS_HEADER, s);
266 status_printf_ln(s, c, "%s:", what);
267 if (!s->hints)
268 return;
269 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
272 static void wt_longstatus_print_trailer(struct wt_status *s)
274 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
277 static const char *wt_status_unmerged_status_string(int stagemask)
279 switch (stagemask) {
280 case 1:
281 return _("both deleted:");
282 case 2:
283 return _("added by us:");
284 case 3:
285 return _("deleted by them:");
286 case 4:
287 return _("added by them:");
288 case 5:
289 return _("deleted by us:");
290 case 6:
291 return _("both added:");
292 case 7:
293 return _("both modified:");
294 default:
295 BUG("unhandled unmerged status %x", stagemask);
299 static const char *wt_status_diff_status_string(int status)
301 switch (status) {
302 case DIFF_STATUS_ADDED:
303 return _("new file:");
304 case DIFF_STATUS_COPIED:
305 return _("copied:");
306 case DIFF_STATUS_DELETED:
307 return _("deleted:");
308 case DIFF_STATUS_MODIFIED:
309 return _("modified:");
310 case DIFF_STATUS_RENAMED:
311 return _("renamed:");
312 case DIFF_STATUS_TYPE_CHANGED:
313 return _("typechange:");
314 case DIFF_STATUS_UNKNOWN:
315 return _("unknown:");
316 case DIFF_STATUS_UNMERGED:
317 return _("unmerged:");
318 default:
319 return NULL;
323 static int maxwidth(const char *(*label)(int), int minval, int maxval)
325 int result = 0, i;
327 for (i = minval; i <= maxval; i++) {
328 const char *s = label(i);
329 int len = s ? utf8_strwidth(s) : 0;
330 if (len > result)
331 result = len;
333 return result;
336 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
337 struct string_list_item *it)
339 const char *c = color(WT_STATUS_UNMERGED, s);
340 struct wt_status_change_data *d = it->util;
341 struct strbuf onebuf = STRBUF_INIT;
342 static char *padding;
343 static int label_width;
344 const char *one, *how;
345 int len;
347 if (!padding) {
348 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
349 label_width += strlen(" ");
350 padding = xmallocz(label_width);
351 memset(padding, ' ', label_width);
354 one = quote_path(it->string, s->prefix, &onebuf, 0);
355 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
357 how = wt_status_unmerged_status_string(d->stagemask);
358 len = label_width - utf8_strwidth(how);
359 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
360 strbuf_release(&onebuf);
363 static void wt_longstatus_print_change_data(struct wt_status *s,
364 int change_type,
365 struct string_list_item *it)
367 struct wt_status_change_data *d = it->util;
368 const char *c = color(change_type, s);
369 int status;
370 char *one_name;
371 char *two_name;
372 const char *one, *two;
373 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
374 struct strbuf extra = STRBUF_INIT;
375 static char *padding;
376 static int label_width;
377 const char *what;
378 int len;
380 if (!padding) {
381 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
382 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
383 label_width += strlen(" ");
384 padding = xmallocz(label_width);
385 memset(padding, ' ', label_width);
388 one_name = two_name = it->string;
389 switch (change_type) {
390 case WT_STATUS_UPDATED:
391 status = d->index_status;
392 break;
393 case WT_STATUS_CHANGED:
394 if (d->new_submodule_commits || d->dirty_submodule) {
395 strbuf_addstr(&extra, " (");
396 if (d->new_submodule_commits)
397 strbuf_addstr(&extra, _("new commits, "));
398 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
399 strbuf_addstr(&extra, _("modified content, "));
400 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
401 strbuf_addstr(&extra, _("untracked content, "));
402 strbuf_setlen(&extra, extra.len - 2);
403 strbuf_addch(&extra, ')');
405 status = d->worktree_status;
406 break;
407 default:
408 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
409 change_type);
413 * Only pick up the rename it's relevant. If the rename is for
414 * the changed section and we're printing the updated section,
415 * ignore it.
417 if (d->rename_status == status)
418 one_name = d->rename_source;
420 one = quote_path(one_name, s->prefix, &onebuf, 0);
421 two = quote_path(two_name, s->prefix, &twobuf, 0);
423 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
424 what = wt_status_diff_status_string(status);
425 if (!what)
426 BUG("unhandled diff status %c", status);
427 len = label_width - utf8_strwidth(what);
428 assert(len >= 0);
429 if (one_name != two_name)
430 status_printf_more(s, c, "%s%.*s%s -> %s",
431 what, len, padding, one, two);
432 else
433 status_printf_more(s, c, "%s%.*s%s",
434 what, len, padding, one);
435 if (extra.len) {
436 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
437 strbuf_release(&extra);
439 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
440 strbuf_release(&onebuf);
441 strbuf_release(&twobuf);
444 static char short_submodule_status(struct wt_status_change_data *d)
446 if (d->new_submodule_commits)
447 return 'M';
448 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
449 return 'm';
450 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
451 return '?';
452 return d->worktree_status;
455 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
456 struct diff_options *options UNUSED,
457 void *data)
459 struct wt_status *s = data;
460 int i;
462 if (!q->nr)
463 return;
464 s->workdir_dirty = 1;
465 for (i = 0; i < q->nr; i++) {
466 struct diff_filepair *p;
467 struct string_list_item *it;
468 struct wt_status_change_data *d;
470 p = q->queue[i];
471 it = string_list_insert(&s->change, p->two->path);
472 d = it->util;
473 if (!d) {
474 CALLOC_ARRAY(d, 1);
475 it->util = d;
477 if (!d->worktree_status)
478 d->worktree_status = p->status;
479 if (S_ISGITLINK(p->two->mode)) {
480 d->dirty_submodule = p->two->dirty_submodule;
481 d->new_submodule_commits = !oideq(&p->one->oid,
482 &p->two->oid);
483 if (s->status_format == STATUS_FORMAT_SHORT)
484 d->worktree_status = short_submodule_status(d);
487 switch (p->status) {
488 case DIFF_STATUS_ADDED:
489 d->mode_worktree = p->two->mode;
490 break;
492 case DIFF_STATUS_DELETED:
493 d->mode_index = p->one->mode;
494 oidcpy(&d->oid_index, &p->one->oid);
495 /* mode_worktree is zero for a delete. */
496 break;
498 case DIFF_STATUS_COPIED:
499 case DIFF_STATUS_RENAMED:
500 if (d->rename_status)
501 BUG("multiple renames on the same target? how?");
502 d->rename_source = xstrdup(p->one->path);
503 d->rename_score = p->score * 100 / MAX_SCORE;
504 d->rename_status = p->status;
505 /* fallthru */
506 case DIFF_STATUS_MODIFIED:
507 case DIFF_STATUS_TYPE_CHANGED:
508 case DIFF_STATUS_UNMERGED:
509 d->mode_index = p->one->mode;
510 d->mode_worktree = p->two->mode;
511 oidcpy(&d->oid_index, &p->one->oid);
512 break;
514 default:
515 BUG("unhandled diff-files status '%c'", p->status);
516 break;
522 static int unmerged_mask(struct index_state *istate, const char *path)
524 int pos, mask;
525 const struct cache_entry *ce;
527 pos = index_name_pos(istate, path, strlen(path));
528 if (0 <= pos)
529 return 0;
531 mask = 0;
532 pos = -pos-1;
533 while (pos < istate->cache_nr) {
534 ce = istate->cache[pos++];
535 if (strcmp(ce->name, path) || !ce_stage(ce))
536 break;
537 mask |= (1 << (ce_stage(ce) - 1));
539 return mask;
542 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
543 struct diff_options *options UNUSED,
544 void *data)
546 struct wt_status *s = data;
547 int i;
549 for (i = 0; i < q->nr; i++) {
550 struct diff_filepair *p;
551 struct string_list_item *it;
552 struct wt_status_change_data *d;
554 p = q->queue[i];
555 it = string_list_insert(&s->change, p->two->path);
556 d = it->util;
557 if (!d) {
558 CALLOC_ARRAY(d, 1);
559 it->util = d;
561 if (!d->index_status)
562 d->index_status = p->status;
563 switch (p->status) {
564 case DIFF_STATUS_ADDED:
565 /* Leave {mode,oid}_head zero for an add. */
566 d->mode_index = p->two->mode;
567 oidcpy(&d->oid_index, &p->two->oid);
568 s->committable = 1;
569 break;
570 case DIFF_STATUS_DELETED:
571 d->mode_head = p->one->mode;
572 oidcpy(&d->oid_head, &p->one->oid);
573 s->committable = 1;
574 /* Leave {mode,oid}_index zero for a delete. */
575 break;
577 case DIFF_STATUS_COPIED:
578 case DIFF_STATUS_RENAMED:
579 if (d->rename_status)
580 BUG("multiple renames on the same target? how?");
581 d->rename_source = xstrdup(p->one->path);
582 d->rename_score = p->score * 100 / MAX_SCORE;
583 d->rename_status = p->status;
584 /* fallthru */
585 case DIFF_STATUS_MODIFIED:
586 case DIFF_STATUS_TYPE_CHANGED:
587 d->mode_head = p->one->mode;
588 d->mode_index = p->two->mode;
589 oidcpy(&d->oid_head, &p->one->oid);
590 oidcpy(&d->oid_index, &p->two->oid);
591 s->committable = 1;
592 break;
593 case DIFF_STATUS_UNMERGED:
594 d->stagemask = unmerged_mask(s->repo->index,
595 p->two->path);
597 * Don't bother setting {mode,oid}_{head,index} since the print
598 * code will output the stage values directly and not use the
599 * values in these fields.
601 break;
603 default:
604 BUG("unhandled diff-index status '%c'", p->status);
605 break;
610 static void wt_status_collect_changes_worktree(struct wt_status *s)
612 struct rev_info rev;
614 repo_init_revisions(s->repo, &rev, NULL);
615 setup_revisions(0, NULL, &rev, NULL);
616 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
617 rev.diffopt.flags.dirty_submodules = 1;
618 rev.diffopt.ita_invisible_in_index = 1;
619 if (!s->show_untracked_files)
620 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
621 if (s->ignore_submodule_arg) {
622 rev.diffopt.flags.override_submodule_config = 1;
623 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
624 } else if (!rev.diffopt.flags.ignore_submodule_set &&
625 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
626 handle_ignore_submodules_arg(&rev.diffopt, "none");
627 rev.diffopt.format_callback = wt_status_collect_changed_cb;
628 rev.diffopt.format_callback_data = s;
629 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
630 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
631 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
632 copy_pathspec(&rev.prune_data, &s->pathspec);
633 run_diff_files(&rev, 0);
634 release_revisions(&rev);
637 static void wt_status_collect_changes_index(struct wt_status *s)
639 struct rev_info rev;
640 struct setup_revision_opt opt;
642 repo_init_revisions(s->repo, &rev, NULL);
643 memset(&opt, 0, sizeof(opt));
644 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
645 setup_revisions(0, NULL, &rev, &opt);
647 rev.diffopt.flags.override_submodule_config = 1;
648 rev.diffopt.ita_invisible_in_index = 1;
649 if (s->ignore_submodule_arg) {
650 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
651 } else {
653 * Unless the user did explicitly request a submodule ignore
654 * mode by passing a command line option we do not ignore any
655 * changed submodule SHA-1s when comparing index and HEAD, no
656 * matter what is configured. Otherwise the user won't be
657 * shown any submodules manually added (and which are
658 * staged to be committed), which would be really confusing.
660 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
663 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
664 rev.diffopt.format_callback = wt_status_collect_updated_cb;
665 rev.diffopt.format_callback_data = s;
666 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
667 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
668 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
671 * The `recursive` option must be enabled to allow the diff to recurse
672 * into subdirectories of sparse directory index entries. If it is not
673 * enabled, a subdirectory containing file(s) with changes is reported
674 * as "modified", rather than the modified files themselves.
676 rev.diffopt.flags.recursive = 1;
678 copy_pathspec(&rev.prune_data, &s->pathspec);
679 run_diff_index(&rev, DIFF_INDEX_CACHED);
680 release_revisions(&rev);
683 static int add_file_to_list(const struct object_id *oid,
684 struct strbuf *base, const char *path,
685 unsigned int mode, void *context)
687 struct string_list_item *it;
688 struct wt_status_change_data *d;
689 struct wt_status *s = context;
690 struct strbuf full_name = STRBUF_INIT;
692 if (S_ISDIR(mode))
693 return READ_TREE_RECURSIVE;
695 strbuf_add(&full_name, base->buf, base->len);
696 strbuf_addstr(&full_name, path);
697 it = string_list_insert(&s->change, full_name.buf);
698 d = it->util;
699 if (!d) {
700 CALLOC_ARRAY(d, 1);
701 it->util = d;
704 d->index_status = DIFF_STATUS_ADDED;
705 /* Leave {mode,oid}_head zero for adds. */
706 d->mode_index = mode;
707 oidcpy(&d->oid_index, oid);
708 s->committable = 1;
709 strbuf_release(&full_name);
710 return 0;
713 static void wt_status_collect_changes_initial(struct wt_status *s)
715 struct index_state *istate = s->repo->index;
716 int i;
718 for (i = 0; i < istate->cache_nr; i++) {
719 struct string_list_item *it;
720 struct wt_status_change_data *d;
721 const struct cache_entry *ce = istate->cache[i];
723 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
724 continue;
725 if (ce_intent_to_add(ce))
726 continue;
727 if (S_ISSPARSEDIR(ce->ce_mode)) {
729 * This is a sparse directory entry, so we want to collect all
730 * of the added files within the tree. This requires recursively
731 * expanding the trees to find the elements that are new in this
732 * tree and marking them with DIFF_STATUS_ADDED.
734 struct strbuf base = STRBUF_INIT;
735 struct pathspec ps = { 0 };
736 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
738 ps.recursive = 1;
739 ps.has_wildcard = 1;
740 ps.max_depth = -1;
742 strbuf_add(&base, ce->name, ce->ce_namelen);
743 read_tree_at(istate->repo, tree, &base, 0, &ps,
744 add_file_to_list, s);
745 continue;
748 it = string_list_insert(&s->change, ce->name);
749 d = it->util;
750 if (!d) {
751 CALLOC_ARRAY(d, 1);
752 it->util = d;
754 if (ce_stage(ce)) {
755 d->index_status = DIFF_STATUS_UNMERGED;
756 d->stagemask |= (1 << (ce_stage(ce) - 1));
758 * Don't bother setting {mode,oid}_{head,index} since the print
759 * code will output the stage values directly and not use the
760 * values in these fields.
762 s->committable = 1;
763 } else {
764 d->index_status = DIFF_STATUS_ADDED;
765 /* Leave {mode,oid}_head zero for adds. */
766 d->mode_index = ce->ce_mode;
767 oidcpy(&d->oid_index, &ce->oid);
768 s->committable = 1;
773 static void wt_status_collect_untracked(struct wt_status *s)
775 int i;
776 struct dir_struct dir = DIR_INIT;
777 uint64_t t_begin = getnanotime();
778 struct index_state *istate = s->repo->index;
780 if (!s->show_untracked_files)
781 return;
783 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
784 dir.flags |=
785 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
786 if (s->show_ignored_mode) {
787 dir.flags |= DIR_SHOW_IGNORED_TOO;
789 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
790 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
791 } else {
792 dir.untracked = istate->untracked;
795 setup_standard_excludes(&dir);
797 fill_directory(&dir, istate, &s->pathspec);
799 for (i = 0; i < dir.nr; i++) {
800 struct dir_entry *ent = dir.entries[i];
801 if (index_name_is_other(istate, ent->name, ent->len))
802 string_list_insert(&s->untracked, ent->name);
805 for (i = 0; i < dir.ignored_nr; i++) {
806 struct dir_entry *ent = dir.ignored[i];
807 if (index_name_is_other(istate, ent->name, ent->len))
808 string_list_insert(&s->ignored, ent->name);
811 dir_clear(&dir);
813 if (advice_enabled(ADVICE_STATUS_U_OPTION))
814 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
817 static int has_unmerged(struct wt_status *s)
819 int i;
821 for (i = 0; i < s->change.nr; i++) {
822 struct wt_status_change_data *d;
823 d = s->change.items[i].util;
824 if (d->stagemask)
825 return 1;
827 return 0;
830 void wt_status_collect(struct wt_status *s)
832 trace2_region_enter("status", "worktrees", s->repo);
833 wt_status_collect_changes_worktree(s);
834 trace2_region_leave("status", "worktrees", s->repo);
836 if (s->is_initial) {
837 trace2_region_enter("status", "initial", s->repo);
838 wt_status_collect_changes_initial(s);
839 trace2_region_leave("status", "initial", s->repo);
840 } else {
841 trace2_region_enter("status", "index", s->repo);
842 wt_status_collect_changes_index(s);
843 trace2_region_leave("status", "index", s->repo);
846 trace2_region_enter("status", "untracked", s->repo);
847 wt_status_collect_untracked(s);
848 trace2_region_leave("status", "untracked", s->repo);
850 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
851 if (s->state.merge_in_progress && !has_unmerged(s))
852 s->committable = 1;
855 void wt_status_collect_free_buffers(struct wt_status *s)
857 wt_status_state_free_buffers(&s->state);
860 void wt_status_state_free_buffers(struct wt_status_state *state)
862 FREE_AND_NULL(state->branch);
863 FREE_AND_NULL(state->onto);
864 FREE_AND_NULL(state->detached_from);
865 FREE_AND_NULL(state->bisecting_from);
868 static void wt_longstatus_print_unmerged(struct wt_status *s)
870 int shown_header = 0;
871 int i;
873 for (i = 0; i < s->change.nr; i++) {
874 struct wt_status_change_data *d;
875 struct string_list_item *it;
876 it = &(s->change.items[i]);
877 d = it->util;
878 if (!d->stagemask)
879 continue;
880 if (!shown_header) {
881 wt_longstatus_print_unmerged_header(s);
882 shown_header = 1;
884 wt_longstatus_print_unmerged_data(s, it);
886 if (shown_header)
887 wt_longstatus_print_trailer(s);
891 static void wt_longstatus_print_updated(struct wt_status *s)
893 int shown_header = 0;
894 int i;
896 for (i = 0; i < s->change.nr; i++) {
897 struct wt_status_change_data *d;
898 struct string_list_item *it;
899 it = &(s->change.items[i]);
900 d = it->util;
901 if (!d->index_status ||
902 d->index_status == DIFF_STATUS_UNMERGED)
903 continue;
904 if (!shown_header) {
905 wt_longstatus_print_cached_header(s);
906 shown_header = 1;
908 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
910 if (shown_header)
911 wt_longstatus_print_trailer(s);
915 * -1 : has delete
916 * 0 : no change
917 * 1 : some change but no delete
919 static int wt_status_check_worktree_changes(struct wt_status *s,
920 int *dirty_submodules)
922 int i;
923 int changes = 0;
925 *dirty_submodules = 0;
927 for (i = 0; i < s->change.nr; i++) {
928 struct wt_status_change_data *d;
929 d = s->change.items[i].util;
930 if (!d->worktree_status ||
931 d->worktree_status == DIFF_STATUS_UNMERGED)
932 continue;
933 if (!changes)
934 changes = 1;
935 if (d->dirty_submodule)
936 *dirty_submodules = 1;
937 if (d->worktree_status == DIFF_STATUS_DELETED)
938 changes = -1;
940 return changes;
943 static void wt_longstatus_print_changed(struct wt_status *s)
945 int i, dirty_submodules;
946 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
948 if (!worktree_changes)
949 return;
951 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
953 for (i = 0; i < s->change.nr; i++) {
954 struct wt_status_change_data *d;
955 struct string_list_item *it;
956 it = &(s->change.items[i]);
957 d = it->util;
958 if (!d->worktree_status ||
959 d->worktree_status == DIFF_STATUS_UNMERGED)
960 continue;
961 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
963 wt_longstatus_print_trailer(s);
966 static int stash_count_refs(struct object_id *ooid UNUSED,
967 struct object_id *noid UNUSED,
968 const char *email UNUSED,
969 timestamp_t timestamp UNUSED, int tz UNUSED,
970 const char *message UNUSED, void *cb_data)
972 int *c = cb_data;
973 (*c)++;
974 return 0;
977 static int count_stash_entries(void)
979 int n = 0;
980 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
981 "refs/stash", stash_count_refs, &n);
982 return n;
985 static void wt_longstatus_print_stash_summary(struct wt_status *s)
987 int stash_count = count_stash_entries();
989 if (stash_count > 0)
990 status_printf_ln(s, GIT_COLOR_NORMAL,
991 Q_("Your stash currently has %d entry",
992 "Your stash currently has %d entries", stash_count),
993 stash_count);
996 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
998 struct child_process sm_summary = CHILD_PROCESS_INIT;
999 struct strbuf cmd_stdout = STRBUF_INIT;
1000 struct strbuf summary = STRBUF_INIT;
1001 char *summary_content;
1003 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1005 strvec_push(&sm_summary.args, "submodule");
1006 strvec_push(&sm_summary.args, "summary");
1007 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1008 strvec_push(&sm_summary.args, "--for-status");
1009 strvec_push(&sm_summary.args, "--summary-limit");
1010 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1011 if (!uncommitted)
1012 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1014 sm_summary.git_cmd = 1;
1015 sm_summary.no_stdin = 1;
1017 capture_command(&sm_summary, &cmd_stdout, 1024);
1019 /* prepend header, only if there's an actual output */
1020 if (cmd_stdout.len) {
1021 if (uncommitted)
1022 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1023 else
1024 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1025 strbuf_addstr(&summary, "\n\n");
1027 strbuf_addbuf(&summary, &cmd_stdout);
1028 strbuf_release(&cmd_stdout);
1030 if (s->display_comment_prefix) {
1031 size_t len;
1032 summary_content = strbuf_detach(&summary, &len);
1033 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1034 free(summary_content);
1037 fputs(summary.buf, s->fp);
1038 strbuf_release(&summary);
1041 static void wt_longstatus_print_other(struct wt_status *s,
1042 struct string_list *l,
1043 const char *what,
1044 const char *how)
1046 int i;
1047 struct strbuf buf = STRBUF_INIT;
1048 static struct string_list output = STRING_LIST_INIT_DUP;
1049 struct column_options copts;
1051 if (!l->nr)
1052 return;
1054 wt_longstatus_print_other_header(s, what, how);
1056 for (i = 0; i < l->nr; i++) {
1057 struct string_list_item *it;
1058 const char *path;
1059 it = &(l->items[i]);
1060 path = quote_path(it->string, s->prefix, &buf, 0);
1061 if (column_active(s->colopts)) {
1062 string_list_append(&output, path);
1063 continue;
1065 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1066 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1067 "%s\n", path);
1070 strbuf_release(&buf);
1071 if (!column_active(s->colopts))
1072 goto conclude;
1074 strbuf_addf(&buf, "%s%s\t%s",
1075 color(WT_STATUS_HEADER, s),
1076 s->display_comment_prefix ? "#" : "",
1077 color(WT_STATUS_UNTRACKED, s));
1078 memset(&copts, 0, sizeof(copts));
1079 copts.padding = 1;
1080 copts.indent = buf.buf;
1081 if (want_color(s->use_color))
1082 copts.nl = GIT_COLOR_RESET "\n";
1083 print_columns(&output, s->colopts, &copts);
1084 string_list_clear(&output, 0);
1085 strbuf_release(&buf);
1086 conclude:
1087 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1090 size_t wt_status_locate_end(const char *s, size_t len)
1092 const char *p;
1093 struct strbuf pattern = STRBUF_INIT;
1095 strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line);
1096 if (starts_with(s, pattern.buf + 1))
1097 len = 0;
1098 else if ((p = strstr(s, pattern.buf))) {
1099 size_t newlen = p - s + 1;
1100 if (newlen < len)
1101 len = newlen;
1103 strbuf_release(&pattern);
1104 return len;
1107 void wt_status_append_cut_line(struct strbuf *buf)
1109 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1111 strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1112 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1115 void wt_status_add_cut_line(struct wt_status *s)
1117 struct strbuf buf = STRBUF_INIT;
1119 if (s->added_cut_line)
1120 return;
1121 s->added_cut_line = 1;
1122 wt_status_append_cut_line(&buf);
1123 fputs(buf.buf, s->fp);
1124 strbuf_release(&buf);
1127 static void wt_longstatus_print_verbose(struct wt_status *s)
1129 struct rev_info rev;
1130 struct setup_revision_opt opt;
1131 int dirty_submodules;
1132 const char *c = color(WT_STATUS_HEADER, s);
1134 repo_init_revisions(s->repo, &rev, NULL);
1135 rev.diffopt.flags.allow_textconv = 1;
1136 rev.diffopt.ita_invisible_in_index = 1;
1138 memset(&opt, 0, sizeof(opt));
1139 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
1140 setup_revisions(0, NULL, &rev, &opt);
1142 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1143 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1144 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1145 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1146 rev.diffopt.file = s->fp;
1147 rev.diffopt.close_file = 0;
1149 * If we're not going to stdout, then we definitely don't
1150 * want color, since we are going to the commit message
1151 * file (and even the "auto" setting won't work, since it
1152 * will have checked isatty on stdout). But we then do want
1153 * to insert the scissor line here to reliably remove the
1154 * diff before committing, if we didn't already include one
1155 * before.
1157 if (s->fp != stdout) {
1158 rev.diffopt.use_color = 0;
1159 wt_status_add_cut_line(s);
1161 if (s->verbose > 1 && s->committable) {
1162 /* print_updated() printed a header, so do we */
1163 if (s->fp != stdout)
1164 wt_longstatus_print_trailer(s);
1165 status_printf_ln(s, c, _("Changes to be committed:"));
1166 rev.diffopt.a_prefix = "c/";
1167 rev.diffopt.b_prefix = "i/";
1168 } /* else use prefix as per user config */
1169 run_diff_index(&rev, DIFF_INDEX_CACHED);
1170 if (s->verbose > 1 &&
1171 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1172 status_printf_ln(s, c,
1173 "--------------------------------------------------");
1174 status_printf_ln(s, c, _("Changes not staged for commit:"));
1175 setup_work_tree();
1176 rev.diffopt.a_prefix = "i/";
1177 rev.diffopt.b_prefix = "w/";
1178 run_diff_files(&rev, 0);
1180 release_revisions(&rev);
1183 static void wt_longstatus_print_tracking(struct wt_status *s)
1185 struct strbuf sb = STRBUF_INIT;
1186 const char *cp, *ep, *branch_name;
1187 struct branch *branch;
1188 uint64_t t_begin = 0;
1190 assert(s->branch && !s->is_initial);
1191 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1192 return;
1193 branch = branch_get(branch_name);
1195 t_begin = getnanotime();
1197 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1198 !s->commit_template))
1199 return;
1201 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1202 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1203 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1204 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1205 strbuf_addf(&sb, _("\n"
1206 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1207 "You can use '--no-ahead-behind' to avoid this.\n"),
1208 t_delta_in_ms / 1000.0);
1212 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1213 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1214 "%s%s%.*s",
1215 s->display_comment_prefix ? comment_line_str : "",
1216 s->display_comment_prefix ? " " : "",
1217 (int)(ep - cp), cp);
1218 if (s->display_comment_prefix)
1219 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
1220 comment_line_str);
1221 else
1222 fputs("\n", s->fp);
1223 strbuf_release(&sb);
1226 static int uf_was_slow(struct wt_status *s)
1228 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1229 s->untracked_in_ms = 3250;
1230 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1233 static void show_merge_in_progress(struct wt_status *s,
1234 const char *color)
1236 if (has_unmerged(s)) {
1237 status_printf_ln(s, color, _("You have unmerged paths."));
1238 if (s->hints) {
1239 status_printf_ln(s, color,
1240 _(" (fix conflicts and run \"git commit\")"));
1241 status_printf_ln(s, color,
1242 _(" (use \"git merge --abort\" to abort the merge)"));
1244 } else {
1245 status_printf_ln(s, color,
1246 _("All conflicts fixed but you are still merging."));
1247 if (s->hints)
1248 status_printf_ln(s, color,
1249 _(" (use \"git commit\" to conclude merge)"));
1251 wt_longstatus_print_trailer(s);
1254 static void show_am_in_progress(struct wt_status *s,
1255 const char *color)
1257 int am_empty_patch;
1259 status_printf_ln(s, color,
1260 _("You are in the middle of an am session."));
1261 if (s->state.am_empty_patch)
1262 status_printf_ln(s, color,
1263 _("The current patch is empty."));
1264 if (s->hints) {
1265 am_empty_patch = s->state.am_empty_patch;
1266 if (!am_empty_patch)
1267 status_printf_ln(s, color,
1268 _(" (fix conflicts and then run \"git am --continue\")"));
1269 status_printf_ln(s, color,
1270 _(" (use \"git am --skip\" to skip this patch)"));
1271 if (am_empty_patch)
1272 status_printf_ln(s, color,
1273 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1274 status_printf_ln(s, color,
1275 _(" (use \"git am --abort\" to restore the original branch)"));
1277 wt_longstatus_print_trailer(s);
1280 static char *read_line_from_git_path(const char *filename)
1282 struct strbuf buf = STRBUF_INIT;
1283 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1285 if (!fp) {
1286 strbuf_release(&buf);
1287 return NULL;
1289 strbuf_getline_lf(&buf, fp);
1290 if (!fclose(fp)) {
1291 return strbuf_detach(&buf, NULL);
1292 } else {
1293 strbuf_release(&buf);
1294 return NULL;
1298 static int split_commit_in_progress(struct wt_status *s)
1300 int split_in_progress = 0;
1301 struct object_id head_oid, orig_head_oid;
1302 char *rebase_amend, *rebase_orig_head;
1303 int head_flags, orig_head_flags;
1305 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1306 !s->branch || strcmp(s->branch, "HEAD"))
1307 return 0;
1309 if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1310 &head_oid, &head_flags) ||
1311 refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1312 &orig_head_oid, &orig_head_flags))
1313 return 0;
1314 if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1315 return 0;
1317 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1318 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1320 if (!rebase_amend || !rebase_orig_head)
1321 ; /* fall through, no split in progress */
1322 else if (!strcmp(rebase_amend, rebase_orig_head))
1323 split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1324 else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
1325 split_in_progress = 1;
1327 free(rebase_amend);
1328 free(rebase_orig_head);
1330 return split_in_progress;
1334 * Turn
1335 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1336 * into
1337 * "pick d6a2f03 some message"
1339 * The function assumes that the line does not contain useless spaces
1340 * before or after the command.
1342 static void abbrev_oid_in_line(struct strbuf *line)
1344 struct strbuf **split;
1345 int i;
1347 if (starts_with(line->buf, "exec ") ||
1348 starts_with(line->buf, "x ") ||
1349 starts_with(line->buf, "label ") ||
1350 starts_with(line->buf, "l "))
1351 return;
1353 split = strbuf_split_max(line, ' ', 3);
1354 if (split[0] && split[1]) {
1355 struct object_id oid;
1358 * strbuf_split_max left a space. Trim it and re-add
1359 * it after abbreviation.
1361 strbuf_trim(split[1]);
1362 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1363 strbuf_reset(split[1]);
1364 strbuf_add_unique_abbrev(split[1], &oid,
1365 DEFAULT_ABBREV);
1366 strbuf_addch(split[1], ' ');
1367 strbuf_reset(line);
1368 for (i = 0; split[i]; i++)
1369 strbuf_addbuf(line, split[i]);
1372 strbuf_list_free(split);
1375 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1377 struct strbuf line = STRBUF_INIT;
1378 FILE *f = fopen(git_path("%s", fname), "r");
1380 if (!f) {
1381 if (errno == ENOENT)
1382 return -1;
1383 die_errno("Could not open file %s for reading",
1384 git_path("%s", fname));
1386 while (!strbuf_getline_lf(&line, f)) {
1387 if (starts_with(line.buf, comment_line_str))
1388 continue;
1389 strbuf_trim(&line);
1390 if (!line.len)
1391 continue;
1392 abbrev_oid_in_line(&line);
1393 string_list_append(lines, line.buf);
1395 fclose(f);
1396 strbuf_release(&line);
1397 return 0;
1400 static void show_rebase_information(struct wt_status *s,
1401 const char *color)
1403 if (s->state.rebase_interactive_in_progress) {
1404 int i;
1405 int nr_lines_to_show = 2;
1407 struct string_list have_done = STRING_LIST_INIT_DUP;
1408 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1410 read_rebase_todolist("rebase-merge/done", &have_done);
1411 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1412 &yet_to_do))
1413 status_printf_ln(s, color,
1414 _("git-rebase-todo is missing."));
1415 if (have_done.nr == 0)
1416 status_printf_ln(s, color, _("No commands done."));
1417 else {
1418 status_printf_ln(s, color,
1419 Q_("Last command done (%"PRIuMAX" command done):",
1420 "Last commands done (%"PRIuMAX" commands done):",
1421 have_done.nr),
1422 (uintmax_t)have_done.nr);
1423 for (i = (have_done.nr > nr_lines_to_show)
1424 ? have_done.nr - nr_lines_to_show : 0;
1425 i < have_done.nr;
1426 i++)
1427 status_printf_ln(s, color, " %s", have_done.items[i].string);
1428 if (have_done.nr > nr_lines_to_show && s->hints)
1429 status_printf_ln(s, color,
1430 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1433 if (yet_to_do.nr == 0)
1434 status_printf_ln(s, color,
1435 _("No commands remaining."));
1436 else {
1437 status_printf_ln(s, color,
1438 Q_("Next command to do (%"PRIuMAX" remaining command):",
1439 "Next commands to do (%"PRIuMAX" remaining commands):",
1440 yet_to_do.nr),
1441 (uintmax_t)yet_to_do.nr);
1442 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1443 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1444 if (s->hints)
1445 status_printf_ln(s, color,
1446 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1448 string_list_clear(&yet_to_do, 0);
1449 string_list_clear(&have_done, 0);
1453 static void print_rebase_state(struct wt_status *s,
1454 const char *color)
1456 if (s->state.branch)
1457 status_printf_ln(s, color,
1458 _("You are currently rebasing branch '%s' on '%s'."),
1459 s->state.branch,
1460 s->state.onto);
1461 else
1462 status_printf_ln(s, color,
1463 _("You are currently rebasing."));
1466 static void show_rebase_in_progress(struct wt_status *s,
1467 const char *color)
1469 struct stat st;
1471 show_rebase_information(s, color);
1472 if (has_unmerged(s)) {
1473 print_rebase_state(s, color);
1474 if (s->hints) {
1475 status_printf_ln(s, color,
1476 _(" (fix conflicts and then run \"git rebase --continue\")"));
1477 status_printf_ln(s, color,
1478 _(" (use \"git rebase --skip\" to skip this patch)"));
1479 status_printf_ln(s, color,
1480 _(" (use \"git rebase --abort\" to check out the original branch)"));
1482 } else if (s->state.rebase_in_progress ||
1483 !stat(git_path_merge_msg(s->repo), &st)) {
1484 print_rebase_state(s, color);
1485 if (s->hints)
1486 status_printf_ln(s, color,
1487 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1488 } else if (split_commit_in_progress(s)) {
1489 if (s->state.branch)
1490 status_printf_ln(s, color,
1491 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1492 s->state.branch,
1493 s->state.onto);
1494 else
1495 status_printf_ln(s, color,
1496 _("You are currently splitting a commit during a rebase."));
1497 if (s->hints)
1498 status_printf_ln(s, color,
1499 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1500 } else {
1501 if (s->state.branch)
1502 status_printf_ln(s, color,
1503 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1504 s->state.branch,
1505 s->state.onto);
1506 else
1507 status_printf_ln(s, color,
1508 _("You are currently editing a commit during a rebase."));
1509 if (s->hints && !s->amend) {
1510 status_printf_ln(s, color,
1511 _(" (use \"git commit --amend\" to amend the current commit)"));
1512 status_printf_ln(s, color,
1513 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1516 wt_longstatus_print_trailer(s);
1519 static void show_cherry_pick_in_progress(struct wt_status *s,
1520 const char *color)
1522 if (is_null_oid(&s->state.cherry_pick_head_oid))
1523 status_printf_ln(s, color,
1524 _("Cherry-pick currently in progress."));
1525 else
1526 status_printf_ln(s, color,
1527 _("You are currently cherry-picking commit %s."),
1528 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1529 DEFAULT_ABBREV));
1531 if (s->hints) {
1532 if (has_unmerged(s))
1533 status_printf_ln(s, color,
1534 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1535 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1536 status_printf_ln(s, color,
1537 _(" (run \"git cherry-pick --continue\" to continue)"));
1538 else
1539 status_printf_ln(s, color,
1540 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1541 status_printf_ln(s, color,
1542 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1543 status_printf_ln(s, color,
1544 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1546 wt_longstatus_print_trailer(s);
1549 static void show_revert_in_progress(struct wt_status *s,
1550 const char *color)
1552 if (is_null_oid(&s->state.revert_head_oid))
1553 status_printf_ln(s, color,
1554 _("Revert currently in progress."));
1555 else
1556 status_printf_ln(s, color,
1557 _("You are currently reverting commit %s."),
1558 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1559 DEFAULT_ABBREV));
1560 if (s->hints) {
1561 if (has_unmerged(s))
1562 status_printf_ln(s, color,
1563 _(" (fix conflicts and run \"git revert --continue\")"));
1564 else if (is_null_oid(&s->state.revert_head_oid))
1565 status_printf_ln(s, color,
1566 _(" (run \"git revert --continue\" to continue)"));
1567 else
1568 status_printf_ln(s, color,
1569 _(" (all conflicts fixed: run \"git revert --continue\")"));
1570 status_printf_ln(s, color,
1571 _(" (use \"git revert --skip\" to skip this patch)"));
1572 status_printf_ln(s, color,
1573 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1575 wt_longstatus_print_trailer(s);
1578 static void show_bisect_in_progress(struct wt_status *s,
1579 const char *color)
1581 if (s->state.bisecting_from)
1582 status_printf_ln(s, color,
1583 _("You are currently bisecting, started from branch '%s'."),
1584 s->state.bisecting_from);
1585 else
1586 status_printf_ln(s, color,
1587 _("You are currently bisecting."));
1588 if (s->hints)
1589 status_printf_ln(s, color,
1590 _(" (use \"git bisect reset\" to get back to the original branch)"));
1591 wt_longstatus_print_trailer(s);
1594 static void show_sparse_checkout_in_use(struct wt_status *s,
1595 const char *color)
1597 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1598 return;
1600 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1601 status_printf_ln(s, color, _("You are in a sparse checkout."));
1602 else
1603 status_printf_ln(s, color,
1604 _("You are in a sparse checkout with %d%% of tracked files present."),
1605 s->state.sparse_checkout_percentage);
1606 wt_longstatus_print_trailer(s);
1610 * Extract branch information from rebase/bisect
1612 static char *get_branch(const struct worktree *wt, const char *path)
1614 struct strbuf sb = STRBUF_INIT;
1615 struct object_id oid;
1616 const char *branch_name;
1618 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1619 goto got_nothing;
1621 while (sb.len && sb.buf[sb.len - 1] == '\n')
1622 strbuf_setlen(&sb, sb.len - 1);
1623 if (!sb.len)
1624 goto got_nothing;
1625 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1626 strbuf_remove(&sb, 0, branch_name - sb.buf);
1627 else if (starts_with(sb.buf, "refs/"))
1629 else if (!get_oid_hex(sb.buf, &oid)) {
1630 strbuf_reset(&sb);
1631 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1632 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1633 goto got_nothing;
1634 else /* bisect */
1636 return strbuf_detach(&sb, NULL);
1638 got_nothing:
1639 strbuf_release(&sb);
1640 return NULL;
1643 struct grab_1st_switch_cbdata {
1644 struct strbuf buf;
1645 struct object_id noid;
1648 static int grab_1st_switch(struct object_id *ooid UNUSED,
1649 struct object_id *noid,
1650 const char *email UNUSED,
1651 timestamp_t timestamp UNUSED, int tz UNUSED,
1652 const char *message, void *cb_data)
1654 struct grab_1st_switch_cbdata *cb = cb_data;
1655 const char *target = NULL, *end;
1657 if (!skip_prefix(message, "checkout: moving from ", &message))
1658 return 0;
1659 target = strstr(message, " to ");
1660 if (!target)
1661 return 0;
1662 target += strlen(" to ");
1663 strbuf_reset(&cb->buf);
1664 oidcpy(&cb->noid, noid);
1665 end = strchrnul(target, '\n');
1666 strbuf_add(&cb->buf, target, end - target);
1667 if (!strcmp(cb->buf.buf, "HEAD")) {
1668 /* HEAD is relative. Resolve it to the right reflog entry. */
1669 strbuf_reset(&cb->buf);
1670 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1672 return 1;
1675 static void wt_status_get_detached_from(struct repository *r,
1676 struct wt_status_state *state)
1678 struct grab_1st_switch_cbdata cb;
1679 struct commit *commit;
1680 struct object_id oid;
1681 char *ref = NULL;
1683 strbuf_init(&cb.buf, 0);
1684 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) {
1685 strbuf_release(&cb.buf);
1686 return;
1689 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
1690 1) == 1 &&
1691 /* oid is a commit? match without further lookup */
1692 (oideq(&cb.noid, &oid) ||
1693 /* perhaps oid is a tag, try to dereference to a commit */
1694 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1695 oideq(&cb.noid, &commit->object.oid)))) {
1696 const char *from = ref;
1697 if (!skip_prefix(from, "refs/tags/", &from))
1698 skip_prefix(from, "refs/remotes/", &from);
1699 state->detached_from = xstrdup(from);
1700 } else
1701 state->detached_from =
1702 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1703 oidcpy(&state->detached_oid, &cb.noid);
1704 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1705 oideq(&oid, &state->detached_oid);
1707 free(ref);
1708 strbuf_release(&cb.buf);
1711 int wt_status_check_rebase(const struct worktree *wt,
1712 struct wt_status_state *state)
1714 struct stat st;
1716 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1717 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1718 state->am_in_progress = 1;
1719 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1720 state->am_empty_patch = 1;
1721 } else {
1722 state->rebase_in_progress = 1;
1723 state->branch = get_branch(wt, "rebase-apply/head-name");
1724 state->onto = get_branch(wt, "rebase-apply/onto");
1726 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1727 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1728 state->rebase_interactive_in_progress = 1;
1729 else
1730 state->rebase_in_progress = 1;
1731 state->branch = get_branch(wt, "rebase-merge/head-name");
1732 state->onto = get_branch(wt, "rebase-merge/onto");
1733 } else
1734 return 0;
1735 return 1;
1738 int wt_status_check_bisect(const struct worktree *wt,
1739 struct wt_status_state *state)
1741 struct stat st;
1743 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1744 state->bisect_in_progress = 1;
1745 state->bisecting_from = get_branch(wt, "BISECT_START");
1746 return 1;
1748 return 0;
1751 static void wt_status_check_sparse_checkout(struct repository *r,
1752 struct wt_status_state *state)
1754 int skip_worktree = 0;
1755 int i;
1757 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1759 * Don't compute percentage of checked out files if we
1760 * aren't in a sparse checkout or would get division by 0.
1762 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1763 return;
1766 if (r->index->sparse_index) {
1767 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1768 return;
1771 for (i = 0; i < r->index->cache_nr; i++) {
1772 struct cache_entry *ce = r->index->cache[i];
1773 if (ce_skip_worktree(ce))
1774 skip_worktree++;
1777 state->sparse_checkout_percentage =
1778 100 - (100 * skip_worktree)/r->index->cache_nr;
1781 void wt_status_get_state(struct repository *r,
1782 struct wt_status_state *state,
1783 int get_detached_from)
1785 struct stat st;
1786 struct object_id oid;
1787 enum replay_action action;
1789 if (!stat(git_path_merge_head(r), &st)) {
1790 wt_status_check_rebase(NULL, state);
1791 state->merge_in_progress = 1;
1792 } else if (wt_status_check_rebase(NULL, state)) {
1793 ; /* all set */
1794 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1795 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1796 state->cherry_pick_in_progress = 1;
1797 oidcpy(&state->cherry_pick_head_oid, &oid);
1799 wt_status_check_bisect(NULL, state);
1800 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1801 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
1802 state->revert_in_progress = 1;
1803 oidcpy(&state->revert_head_oid, &oid);
1805 if (!sequencer_get_last_command(r, &action)) {
1806 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1807 state->cherry_pick_in_progress = 1;
1808 oidcpy(&state->cherry_pick_head_oid, null_oid());
1809 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1810 state->revert_in_progress = 1;
1811 oidcpy(&state->revert_head_oid, null_oid());
1814 if (get_detached_from)
1815 wt_status_get_detached_from(r, state);
1816 wt_status_check_sparse_checkout(r, state);
1819 static void wt_longstatus_print_state(struct wt_status *s)
1821 const char *state_color = color(WT_STATUS_HEADER, s);
1822 struct wt_status_state *state = &s->state;
1824 if (state->merge_in_progress) {
1825 if (state->rebase_interactive_in_progress) {
1826 show_rebase_information(s, state_color);
1827 fputs("\n", s->fp);
1829 show_merge_in_progress(s, state_color);
1830 } else if (state->am_in_progress)
1831 show_am_in_progress(s, state_color);
1832 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1833 show_rebase_in_progress(s, state_color);
1834 else if (state->cherry_pick_in_progress)
1835 show_cherry_pick_in_progress(s, state_color);
1836 else if (state->revert_in_progress)
1837 show_revert_in_progress(s, state_color);
1838 if (state->bisect_in_progress)
1839 show_bisect_in_progress(s, state_color);
1841 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1842 show_sparse_checkout_in_use(s, state_color);
1845 static void wt_longstatus_print(struct wt_status *s)
1847 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1848 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1849 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1851 if (s->branch) {
1852 const char *on_what = _("On branch ");
1853 const char *branch_name = s->branch;
1854 if (!strcmp(branch_name, "HEAD")) {
1855 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1856 if (s->state.rebase_in_progress ||
1857 s->state.rebase_interactive_in_progress) {
1858 if (s->state.rebase_interactive_in_progress)
1859 on_what = _("interactive rebase in progress; onto ");
1860 else
1861 on_what = _("rebase in progress; onto ");
1862 branch_name = s->state.onto;
1863 } else if (s->state.detached_from) {
1864 branch_name = s->state.detached_from;
1865 if (s->state.detached_at)
1866 on_what = _("HEAD detached at ");
1867 else
1868 on_what = _("HEAD detached from ");
1869 } else {
1870 branch_name = "";
1871 on_what = _("Not currently on any branch.");
1873 } else
1874 skip_prefix(branch_name, "refs/heads/", &branch_name);
1875 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1876 status_printf_more(s, branch_status_color, "%s", on_what);
1877 status_printf_more(s, branch_color, "%s\n", branch_name);
1878 if (!s->is_initial)
1879 wt_longstatus_print_tracking(s);
1882 wt_longstatus_print_state(s);
1884 if (s->is_initial) {
1885 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1886 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1887 s->commit_template
1888 ? _("Initial commit")
1889 : _("No commits yet"));
1890 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1893 wt_longstatus_print_updated(s);
1894 wt_longstatus_print_unmerged(s);
1895 wt_longstatus_print_changed(s);
1896 if (s->submodule_summary &&
1897 (!s->ignore_submodule_arg ||
1898 strcmp(s->ignore_submodule_arg, "all"))) {
1899 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1900 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1902 if (s->show_untracked_files) {
1903 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1904 if (s->show_ignored_mode)
1905 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1906 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1907 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1908 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1909 status_printf_ln(s, GIT_COLOR_NORMAL,
1910 _("It took %.2f seconds to enumerate untracked files,\n"
1911 "but the results were cached, and subsequent runs may be faster."),
1912 s->untracked_in_ms / 1000.0);
1913 } else {
1914 status_printf_ln(s, GIT_COLOR_NORMAL,
1915 _("It took %.2f seconds to enumerate untracked files."),
1916 s->untracked_in_ms / 1000.0);
1918 status_printf_ln(s, GIT_COLOR_NORMAL,
1919 _("See 'git help status' for information on how to improve this."));
1920 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1922 } else if (s->committable)
1923 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1924 s->hints
1925 ? _(" (use -u option to show untracked files)") : "");
1927 if (s->verbose)
1928 wt_longstatus_print_verbose(s);
1929 if (!s->committable) {
1930 if (s->amend)
1931 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1932 else if (s->nowarn)
1933 ; /* nothing */
1934 else if (s->workdir_dirty) {
1935 if (s->hints)
1936 fprintf(s->fp, _("no changes added to commit "
1937 "(use \"git add\" and/or "
1938 "\"git commit -a\")\n"));
1939 else
1940 fprintf(s->fp, _("no changes added to "
1941 "commit\n"));
1942 } else if (s->untracked.nr) {
1943 if (s->hints)
1944 fprintf(s->fp, _("nothing added to commit but "
1945 "untracked files present (use "
1946 "\"git add\" to track)\n"));
1947 else
1948 fprintf(s->fp, _("nothing added to commit but "
1949 "untracked files present\n"));
1950 } else if (s->is_initial) {
1951 if (s->hints)
1952 fprintf(s->fp, _("nothing to commit (create/"
1953 "copy files and use \"git "
1954 "add\" to track)\n"));
1955 else
1956 fprintf(s->fp, _("nothing to commit\n"));
1957 } else if (!s->show_untracked_files) {
1958 if (s->hints)
1959 fprintf(s->fp, _("nothing to commit (use -u to "
1960 "show untracked files)\n"));
1961 else
1962 fprintf(s->fp, _("nothing to commit\n"));
1963 } else
1964 fprintf(s->fp, _("nothing to commit, working tree "
1965 "clean\n"));
1967 if(s->show_stash)
1968 wt_longstatus_print_stash_summary(s);
1971 static void wt_shortstatus_unmerged(struct string_list_item *it,
1972 struct wt_status *s)
1974 struct wt_status_change_data *d = it->util;
1975 const char *how = "??";
1977 switch (d->stagemask) {
1978 case 1: how = "DD"; break; /* both deleted */
1979 case 2: how = "AU"; break; /* added by us */
1980 case 3: how = "UD"; break; /* deleted by them */
1981 case 4: how = "UA"; break; /* added by them */
1982 case 5: how = "DU"; break; /* deleted by us */
1983 case 6: how = "AA"; break; /* both added */
1984 case 7: how = "UU"; break; /* both modified */
1986 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1987 if (s->null_termination) {
1988 fprintf(s->fp, " %s%c", it->string, 0);
1989 } else {
1990 struct strbuf onebuf = STRBUF_INIT;
1991 const char *one;
1992 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1993 fprintf(s->fp, " %s\n", one);
1994 strbuf_release(&onebuf);
1998 static void wt_shortstatus_status(struct string_list_item *it,
1999 struct wt_status *s)
2001 struct wt_status_change_data *d = it->util;
2003 if (d->index_status)
2004 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
2005 else
2006 fputc(' ', s->fp);
2007 if (d->worktree_status)
2008 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2009 else
2010 fputc(' ', s->fp);
2011 fputc(' ', s->fp);
2012 if (s->null_termination) {
2013 fprintf(s->fp, "%s%c", it->string, 0);
2014 if (d->rename_source)
2015 fprintf(s->fp, "%s%c", d->rename_source, 0);
2016 } else {
2017 struct strbuf onebuf = STRBUF_INIT;
2018 const char *one;
2020 if (d->rename_source) {
2021 one = quote_path(d->rename_source, s->prefix, &onebuf,
2022 QUOTE_PATH_QUOTE_SP);
2023 fprintf(s->fp, "%s -> ", one);
2024 strbuf_release(&onebuf);
2026 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2027 fprintf(s->fp, "%s\n", one);
2028 strbuf_release(&onebuf);
2032 static void wt_shortstatus_other(struct string_list_item *it,
2033 struct wt_status *s, const char *sign)
2035 if (s->null_termination) {
2036 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2037 } else {
2038 struct strbuf onebuf = STRBUF_INIT;
2039 const char *one;
2040 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2041 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2042 fprintf(s->fp, " %s\n", one);
2043 strbuf_release(&onebuf);
2047 static void wt_shortstatus_print_tracking(struct wt_status *s)
2049 struct branch *branch;
2050 const char *header_color = color(WT_STATUS_HEADER, s);
2051 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2052 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2054 const char *base;
2055 char *short_base;
2056 const char *branch_name;
2057 int num_ours, num_theirs, sti;
2058 int upstream_is_gone = 0;
2060 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2062 if (!s->branch)
2063 return;
2064 branch_name = s->branch;
2066 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2068 if (s->is_initial)
2069 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2071 if (!strcmp(s->branch, "HEAD")) {
2072 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2073 LABEL(N_("HEAD (no branch)")));
2074 goto conclude;
2077 skip_prefix(branch_name, "refs/heads/", &branch_name);
2079 branch = branch_get(branch_name);
2081 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2083 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2084 0, s->ahead_behind_flags);
2085 if (sti < 0) {
2086 if (!base)
2087 goto conclude;
2089 upstream_is_gone = 1;
2092 short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2093 base, 0);
2094 color_fprintf(s->fp, header_color, "...");
2095 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2096 free(short_base);
2098 if (!upstream_is_gone && !sti)
2099 goto conclude;
2101 color_fprintf(s->fp, header_color, " [");
2102 if (upstream_is_gone) {
2103 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2104 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2105 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2106 } else if (!num_ours) {
2107 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2108 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2109 } else if (!num_theirs) {
2110 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2111 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2112 } else {
2113 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2114 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2115 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2116 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2119 color_fprintf(s->fp, header_color, "]");
2120 conclude:
2121 fputc(s->null_termination ? '\0' : '\n', s->fp);
2124 static void wt_shortstatus_print(struct wt_status *s)
2126 struct string_list_item *it;
2128 if (s->show_branch)
2129 wt_shortstatus_print_tracking(s);
2131 for_each_string_list_item(it, &s->change) {
2132 struct wt_status_change_data *d = it->util;
2134 if (d->stagemask)
2135 wt_shortstatus_unmerged(it, s);
2136 else
2137 wt_shortstatus_status(it, s);
2139 for_each_string_list_item(it, &s->untracked)
2140 wt_shortstatus_other(it, s, "??");
2142 for_each_string_list_item(it, &s->ignored)
2143 wt_shortstatus_other(it, s, "!!");
2146 static void wt_porcelain_print(struct wt_status *s)
2148 s->use_color = 0;
2149 s->relative_paths = 0;
2150 s->prefix = NULL;
2151 s->no_gettext = 1;
2152 wt_shortstatus_print(s);
2156 * Print branch information for porcelain v2 output. These lines
2157 * are printed when the '--branch' parameter is given.
2159 * # branch.oid <commit><eol>
2160 * # branch.head <head><eol>
2161 * [# branch.upstream <upstream><eol>
2162 * [# branch.ab +<ahead> -<behind><eol>]]
2164 * <commit> ::= the current commit hash or the literal
2165 * "(initial)" to indicate an initialized repo
2166 * with no commits.
2168 * <head> ::= <branch_name> the current branch name or
2169 * "(detached)" literal when detached head or
2170 * "(unknown)" when something is wrong.
2172 * <upstream> ::= the upstream branch name, when set.
2174 * <ahead> ::= integer ahead value or '?'.
2176 * <behind> ::= integer behind value or '?'.
2178 * The end-of-line is defined by the -z flag.
2180 * <eol> ::= NUL when -z,
2181 * LF when NOT -z.
2183 * When an upstream is set and present, the 'branch.ab' line will
2184 * be printed with the ahead/behind counts for the branch and the
2185 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2186 * are different, '?' will be substituted for the actual count.
2188 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2190 struct branch *branch;
2191 const char *base;
2192 const char *branch_name;
2193 int ab_info, nr_ahead, nr_behind;
2194 char eol = s->null_termination ? '\0' : '\n';
2196 fprintf(s->fp, "# branch.oid %s%c",
2197 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2198 eol);
2200 if (!s->branch)
2201 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2202 else {
2203 if (!strcmp(s->branch, "HEAD")) {
2204 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2206 if (s->state.rebase_in_progress ||
2207 s->state.rebase_interactive_in_progress)
2208 branch_name = s->state.onto;
2209 else if (s->state.detached_from)
2210 branch_name = s->state.detached_from;
2211 else
2212 branch_name = "";
2213 } else {
2214 branch_name = NULL;
2215 skip_prefix(s->branch, "refs/heads/", &branch_name);
2217 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2220 /* Lookup stats on the upstream tracking branch, if set. */
2221 branch = branch_get(branch_name);
2222 base = NULL;
2223 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2224 &base, 0, s->ahead_behind_flags);
2225 if (base) {
2226 base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2227 base, 0);
2228 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2229 free((char *)base);
2231 if (ab_info > 0) {
2232 /* different */
2233 if (nr_ahead || nr_behind)
2234 fprintf(s->fp, "# branch.ab +%d -%d%c",
2235 nr_ahead, nr_behind, eol);
2236 else
2237 fprintf(s->fp, "# branch.ab +? -?%c",
2238 eol);
2239 } else if (!ab_info) {
2240 /* same */
2241 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2248 * Print the stash count in a porcelain-friendly format
2250 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2252 int stash_count = count_stash_entries();
2253 char eol = s->null_termination ? '\0' : '\n';
2255 if (stash_count > 0)
2256 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2260 * Convert various submodule status values into a
2261 * fixed-length string of characters in the buffer provided.
2263 static void wt_porcelain_v2_submodule_state(
2264 struct wt_status_change_data *d,
2265 char sub[5])
2267 if (S_ISGITLINK(d->mode_head) ||
2268 S_ISGITLINK(d->mode_index) ||
2269 S_ISGITLINK(d->mode_worktree)) {
2270 sub[0] = 'S';
2271 sub[1] = d->new_submodule_commits ? 'C' : '.';
2272 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2273 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2274 } else {
2275 sub[0] = 'N';
2276 sub[1] = '.';
2277 sub[2] = '.';
2278 sub[3] = '.';
2280 sub[4] = 0;
2284 * Fix-up changed entries before we print them.
2286 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2288 struct wt_status_change_data *d = it->util;
2290 if (!d->index_status) {
2292 * This entry is unchanged in the index (relative to the head).
2293 * Therefore, the collect_updated_cb was never called for this
2294 * entry (during the head-vs-index scan) and so the head column
2295 * fields were never set.
2297 * We must have data for the index column (from the
2298 * index-vs-worktree scan (otherwise, this entry should not be
2299 * in the list of changes)).
2301 * Copy index column fields to the head column, so that our
2302 * output looks complete.
2304 assert(d->mode_head == 0);
2305 d->mode_head = d->mode_index;
2306 oidcpy(&d->oid_head, &d->oid_index);
2309 if (!d->worktree_status) {
2311 * This entry is unchanged in the worktree (relative to the index).
2312 * Therefore, the collect_changed_cb was never called for this entry
2313 * (during the index-vs-worktree scan) and so the worktree column
2314 * fields were never set.
2316 * We must have data for the index column (from the head-vs-index
2317 * scan).
2319 * Copy the index column fields to the worktree column so that
2320 * our output looks complete.
2322 * Note that we only have a mode field in the worktree column
2323 * because the scan code tries really hard to not have to compute it.
2325 assert(d->mode_worktree == 0);
2326 d->mode_worktree = d->mode_index;
2331 * Print porcelain v2 info for tracked entries with changes.
2333 static void wt_porcelain_v2_print_changed_entry(
2334 struct string_list_item *it,
2335 struct wt_status *s)
2337 struct wt_status_change_data *d = it->util;
2338 struct strbuf buf = STRBUF_INIT;
2339 struct strbuf buf_from = STRBUF_INIT;
2340 const char *path = NULL;
2341 const char *path_from = NULL;
2342 char key[3];
2343 char submodule_token[5];
2344 char sep_char, eol_char;
2346 wt_porcelain_v2_fix_up_changed(it);
2347 wt_porcelain_v2_submodule_state(d, submodule_token);
2349 key[0] = d->index_status ? d->index_status : '.';
2350 key[1] = d->worktree_status ? d->worktree_status : '.';
2351 key[2] = 0;
2353 if (s->null_termination) {
2355 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2356 * A single NUL character separates them.
2358 sep_char = '\0';
2359 eol_char = '\0';
2360 path = it->string;
2361 path_from = d->rename_source;
2362 } else {
2364 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2365 * The source path is only present when necessary.
2366 * A single TAB separates them (because paths can contain spaces
2367 * which are not escaped and C-quoting does escape TAB characters).
2369 sep_char = '\t';
2370 eol_char = '\n';
2371 path = quote_path(it->string, s->prefix, &buf, 0);
2372 if (d->rename_source)
2373 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2376 if (path_from)
2377 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2378 key, submodule_token,
2379 d->mode_head, d->mode_index, d->mode_worktree,
2380 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2381 d->rename_status, d->rename_score,
2382 path, sep_char, path_from, eol_char);
2383 else
2384 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2385 key, submodule_token,
2386 d->mode_head, d->mode_index, d->mode_worktree,
2387 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2388 path, eol_char);
2390 strbuf_release(&buf);
2391 strbuf_release(&buf_from);
2395 * Print porcelain v2 status info for unmerged entries.
2397 static void wt_porcelain_v2_print_unmerged_entry(
2398 struct string_list_item *it,
2399 struct wt_status *s)
2401 struct wt_status_change_data *d = it->util;
2402 struct index_state *istate = s->repo->index;
2403 const struct cache_entry *ce;
2404 struct strbuf buf_index = STRBUF_INIT;
2405 const char *path_index = NULL;
2406 int pos, stage, sum;
2407 struct {
2408 int mode;
2409 struct object_id oid;
2410 } stages[3];
2411 char *key;
2412 char submodule_token[5];
2413 char unmerged_prefix = 'u';
2414 char eol_char = s->null_termination ? '\0' : '\n';
2416 wt_porcelain_v2_submodule_state(d, submodule_token);
2418 switch (d->stagemask) {
2419 case 1: key = "DD"; break; /* both deleted */
2420 case 2: key = "AU"; break; /* added by us */
2421 case 3: key = "UD"; break; /* deleted by them */
2422 case 4: key = "UA"; break; /* added by them */
2423 case 5: key = "DU"; break; /* deleted by us */
2424 case 6: key = "AA"; break; /* both added */
2425 case 7: key = "UU"; break; /* both modified */
2426 default:
2427 BUG("unhandled unmerged status %x", d->stagemask);
2431 * Disregard d.aux.porcelain_v2 data that we accumulated
2432 * for the head and index columns during the scans and
2433 * replace with the actual stage data.
2435 * Note that this is a last-one-wins for each the individual
2436 * stage [123] columns in the event of multiple cache entries
2437 * for same stage.
2439 memset(stages, 0, sizeof(stages));
2440 sum = 0;
2441 pos = index_name_pos(istate, it->string, strlen(it->string));
2442 assert(pos < 0);
2443 pos = -pos-1;
2444 while (pos < istate->cache_nr) {
2445 ce = istate->cache[pos++];
2446 stage = ce_stage(ce);
2447 if (strcmp(ce->name, it->string) || !stage)
2448 break;
2449 stages[stage - 1].mode = ce->ce_mode;
2450 oidcpy(&stages[stage - 1].oid, &ce->oid);
2451 sum |= (1 << (stage - 1));
2453 if (sum != d->stagemask)
2454 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2456 if (s->null_termination)
2457 path_index = it->string;
2458 else
2459 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2461 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2462 unmerged_prefix, key, submodule_token,
2463 stages[0].mode, /* stage 1 */
2464 stages[1].mode, /* stage 2 */
2465 stages[2].mode, /* stage 3 */
2466 d->mode_worktree,
2467 oid_to_hex(&stages[0].oid), /* stage 1 */
2468 oid_to_hex(&stages[1].oid), /* stage 2 */
2469 oid_to_hex(&stages[2].oid), /* stage 3 */
2470 path_index,
2471 eol_char);
2473 strbuf_release(&buf_index);
2477 * Print porcelain V2 status info for untracked and ignored entries.
2479 static void wt_porcelain_v2_print_other(
2480 struct string_list_item *it,
2481 struct wt_status *s,
2482 char prefix)
2484 struct strbuf buf = STRBUF_INIT;
2485 const char *path;
2486 char eol_char;
2488 if (s->null_termination) {
2489 path = it->string;
2490 eol_char = '\0';
2491 } else {
2492 path = quote_path(it->string, s->prefix, &buf, 0);
2493 eol_char = '\n';
2496 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2498 strbuf_release(&buf);
2502 * Print porcelain V2 status.
2504 * [<v2_branch>]
2505 * [<v2_changed_items>]*
2506 * [<v2_unmerged_items>]*
2507 * [<v2_untracked_items>]*
2508 * [<v2_ignored_items>]*
2511 static void wt_porcelain_v2_print(struct wt_status *s)
2513 struct wt_status_change_data *d;
2514 struct string_list_item *it;
2515 int i;
2517 if (s->show_branch)
2518 wt_porcelain_v2_print_tracking(s);
2520 if (s->show_stash)
2521 wt_porcelain_v2_print_stash(s);
2523 for (i = 0; i < s->change.nr; i++) {
2524 it = &(s->change.items[i]);
2525 d = it->util;
2526 if (!d->stagemask)
2527 wt_porcelain_v2_print_changed_entry(it, s);
2530 for (i = 0; i < s->change.nr; i++) {
2531 it = &(s->change.items[i]);
2532 d = it->util;
2533 if (d->stagemask)
2534 wt_porcelain_v2_print_unmerged_entry(it, s);
2537 for (i = 0; i < s->untracked.nr; i++) {
2538 it = &(s->untracked.items[i]);
2539 wt_porcelain_v2_print_other(it, s, '?');
2542 for (i = 0; i < s->ignored.nr; i++) {
2543 it = &(s->ignored.items[i]);
2544 wt_porcelain_v2_print_other(it, s, '!');
2548 void wt_status_print(struct wt_status *s)
2550 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2551 trace2_data_intmax("status", s->repo, "count/untracked",
2552 s->untracked.nr);
2553 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2555 trace2_region_enter("status", "print", s->repo);
2557 switch (s->status_format) {
2558 case STATUS_FORMAT_SHORT:
2559 wt_shortstatus_print(s);
2560 break;
2561 case STATUS_FORMAT_PORCELAIN:
2562 wt_porcelain_print(s);
2563 break;
2564 case STATUS_FORMAT_PORCELAIN_V2:
2565 wt_porcelain_v2_print(s);
2566 break;
2567 case STATUS_FORMAT_UNSPECIFIED:
2568 BUG("finalize_deferred_config() should have been called");
2569 break;
2570 case STATUS_FORMAT_NONE:
2571 case STATUS_FORMAT_LONG:
2572 wt_longstatus_print(s);
2573 break;
2576 trace2_region_leave("status", "print", s->repo);
2580 * Returns 1 if there are unstaged changes, 0 otherwise.
2582 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2584 struct rev_info rev_info;
2585 int result;
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.override_submodule_config = 1;
2592 rev_info.diffopt.flags.quick = 1;
2593 diff_setup_done(&rev_info.diffopt);
2594 run_diff_files(&rev_info, 0);
2595 result = diff_result_code(&rev_info.diffopt);
2596 release_revisions(&rev_info);
2597 return result;
2601 * Returns 1 if there are uncommitted changes, 0 otherwise.
2603 int has_uncommitted_changes(struct repository *r,
2604 int ignore_submodules)
2606 struct rev_info rev_info;
2607 int result;
2609 if (is_index_unborn(r->index))
2610 return 0;
2612 repo_init_revisions(r, &rev_info, NULL);
2613 if (ignore_submodules)
2614 rev_info.diffopt.flags.ignore_submodules = 1;
2615 rev_info.diffopt.flags.quick = 1;
2617 add_head_to_pending(&rev_info);
2618 if (!rev_info.pending.nr) {
2620 * We have no head (or it's corrupt); use the empty tree,
2621 * which will complain if the index is non-empty.
2623 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2624 add_pending_object(&rev_info, &tree->object, "");
2627 diff_setup_done(&rev_info.diffopt);
2628 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2629 result = diff_result_code(&rev_info.diffopt);
2630 release_revisions(&rev_info);
2631 return result;
2635 * If the work tree has unstaged or uncommitted changes, dies with the
2636 * appropriate message.
2638 int require_clean_work_tree(struct repository *r,
2639 const char *action,
2640 const char *hint,
2641 int ignore_submodules,
2642 int gently)
2644 struct lock_file lock_file = LOCK_INIT;
2645 int err = 0, fd;
2647 fd = repo_hold_locked_index(r, &lock_file, 0);
2648 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2649 if (0 <= fd)
2650 repo_update_index_if_able(r, &lock_file);
2651 rollback_lock_file(&lock_file);
2653 if (has_unstaged_changes(r, ignore_submodules)) {
2654 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2655 error(_("cannot %s: You have unstaged changes."), _(action));
2656 err = 1;
2659 if (has_uncommitted_changes(r, ignore_submodules)) {
2660 if (err)
2661 error(_("additionally, your index contains uncommitted changes."));
2662 else
2663 error(_("cannot %s: Your index contains uncommitted changes."),
2664 _(action));
2665 err = 1;
2668 if (err) {
2669 if (hint) {
2670 if (!*hint)
2671 BUG("empty hint passed to require_clean_work_tree();"
2672 " use NULL instead");
2673 error("%s", hint);
2675 if (!gently)
2676 exit(128);
2679 return err;