Sync with 'maint'
[git/gitster.git] / wt-status.c
blob6a8c05d1cff165dda8f42b843bf78490ac624d4e
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "advice.h"
5 #include "wt-status.h"
6 #include "object.h"
7 #include "dir.h"
8 #include "commit.h"
9 #include "diff.h"
10 #include "environment.h"
11 #include "gettext.h"
12 #include "hash.h"
13 #include "hex.h"
14 #include "object-name.h"
15 #include "path.h"
16 #include "revision.h"
17 #include "diffcore.h"
18 #include "quote.h"
19 #include "repository.h"
20 #include "run-command.h"
21 #include "strvec.h"
22 #include "remote.h"
23 #include "refs.h"
24 #include "submodule.h"
25 #include "column.h"
26 #include "read-cache.h"
27 #include "setup.h"
28 #include "strbuf.h"
29 #include "trace.h"
30 #include "trace2.h"
31 #include "tree.h"
32 #include "utf8.h"
33 #include "worktree.h"
34 #include "lockfile.h"
35 #include "sequencer.h"
36 #include "fsmonitor-settings.h"
38 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
39 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
41 static const char cut_line[] =
42 "------------------------ >8 ------------------------\n";
44 static char default_wt_status_colors[][COLOR_MAXLEN] = {
45 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
46 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
47 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
48 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
49 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
50 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
51 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
52 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
53 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
56 static const char *color(int slot, struct wt_status *s)
58 const char *c = "";
59 if (want_color(s->use_color))
60 c = s->color_palette[slot];
61 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
62 c = s->color_palette[WT_STATUS_HEADER];
63 return c;
66 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
67 const char *fmt, va_list ap, const char *trail)
69 struct strbuf sb = STRBUF_INIT;
70 struct strbuf linebuf = STRBUF_INIT;
71 const char *line, *eol;
73 strbuf_vaddf(&sb, fmt, ap);
74 if (!sb.len) {
75 if (s->display_comment_prefix) {
76 strbuf_addstr(&sb, comment_line_str);
77 if (!trail)
78 strbuf_addch(&sb, ' ');
80 color_print_strbuf(s->fp, color, &sb);
81 if (trail)
82 fprintf(s->fp, "%s", trail);
83 strbuf_release(&sb);
84 return;
86 for (line = sb.buf; *line; line = eol + 1) {
87 eol = strchr(line, '\n');
89 strbuf_reset(&linebuf);
90 if (at_bol && s->display_comment_prefix) {
91 strbuf_addstr(&linebuf, comment_line_str);
92 if (*line != '\n' && *line != '\t')
93 strbuf_addch(&linebuf, ' ');
95 if (eol)
96 strbuf_add(&linebuf, line, eol - line);
97 else
98 strbuf_addstr(&linebuf, line);
99 color_print_strbuf(s->fp, color, &linebuf);
100 if (eol)
101 fprintf(s->fp, "\n");
102 else
103 break;
104 at_bol = 1;
106 if (trail)
107 fprintf(s->fp, "%s", trail);
108 strbuf_release(&linebuf);
109 strbuf_release(&sb);
112 void status_printf_ln(struct wt_status *s, const char *color,
113 const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 status_vprintf(s, 1, color, fmt, ap, "\n");
119 va_end(ap);
122 void status_printf(struct wt_status *s, const char *color,
123 const char *fmt, ...)
125 va_list ap;
127 va_start(ap, fmt);
128 status_vprintf(s, 1, color, fmt, ap, NULL);
129 va_end(ap);
132 __attribute__((format (printf, 3, 4)))
133 static void status_printf_more(struct wt_status *s, const char *color,
134 const char *fmt, ...)
136 va_list ap;
138 va_start(ap, fmt);
139 status_vprintf(s, 0, color, fmt, ap, NULL);
140 va_end(ap);
143 void wt_status_prepare(struct repository *r, struct wt_status *s)
145 memset(s, 0, sizeof(*s));
146 s->repo = r;
147 memcpy(s->color_palette, default_wt_status_colors,
148 sizeof(default_wt_status_colors));
149 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
150 s->use_color = -1;
151 s->relative_paths = 1;
152 s->branch = refs_resolve_refdup(get_main_ref_store(the_repository),
153 "HEAD", 0, NULL, NULL);
154 s->reference = "HEAD";
155 s->fp = stdout;
156 s->index_file = repo_get_index_file(the_repository);
157 s->change.strdup_strings = 1;
158 s->untracked.strdup_strings = 1;
159 s->ignored.strdup_strings = 1;
160 s->show_branch = -1; /* unspecified */
161 s->show_stash = 0;
162 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
163 s->display_comment_prefix = 0;
164 s->detect_rename = -1;
165 s->rename_score = -1;
166 s->rename_limit = -1;
169 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
171 int i;
172 int del_mod_conflict = 0;
173 int both_deleted = 0;
174 int not_deleted = 0;
175 const char *c = color(WT_STATUS_HEADER, s);
177 status_printf_ln(s, c, _("Unmerged paths:"));
179 for (i = 0; i < s->change.nr; i++) {
180 struct string_list_item *it = &(s->change.items[i]);
181 struct wt_status_change_data *d = it->util;
183 switch (d->stagemask) {
184 case 0:
185 break;
186 case 1:
187 both_deleted = 1;
188 break;
189 case 3:
190 case 5:
191 del_mod_conflict = 1;
192 break;
193 default:
194 not_deleted = 1;
195 break;
199 if (!s->hints)
200 return;
201 if (s->whence != FROM_COMMIT)
203 else if (!s->is_initial) {
204 if (!strcmp(s->reference, "HEAD"))
205 status_printf_ln(s, c,
206 _(" (use \"git restore --staged <file>...\" to unstage)"));
207 else
208 status_printf_ln(s, c,
209 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
210 s->reference);
211 } else
212 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
214 if (!both_deleted) {
215 if (!del_mod_conflict)
216 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
217 else
218 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
219 } else if (!del_mod_conflict && !not_deleted) {
220 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
221 } else {
222 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
226 static void wt_longstatus_print_cached_header(struct wt_status *s)
228 const char *c = color(WT_STATUS_HEADER, s);
230 status_printf_ln(s, c, _("Changes to be committed:"));
231 if (!s->hints)
232 return;
233 if (s->whence != FROM_COMMIT)
234 ; /* NEEDSWORK: use "git reset --unresolve"??? */
235 else if (!s->is_initial) {
236 if (!strcmp(s->reference, "HEAD"))
237 status_printf_ln(s, c
238 , _(" (use \"git restore --staged <file>...\" to unstage)"));
239 else
240 status_printf_ln(s, c,
241 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
242 s->reference);
243 } else
244 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
247 static void wt_longstatus_print_dirty_header(struct wt_status *s,
248 int has_deleted,
249 int has_dirty_submodules)
251 const char *c = color(WT_STATUS_HEADER, s);
253 status_printf_ln(s, c, _("Changes not staged for commit:"));
254 if (!s->hints)
255 return;
256 if (!has_deleted)
257 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
258 else
259 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
260 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
261 if (has_dirty_submodules)
262 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
265 static void wt_longstatus_print_other_header(struct wt_status *s,
266 const char *what,
267 const char *how)
269 const char *c = color(WT_STATUS_HEADER, s);
270 status_printf_ln(s, c, "%s:", what);
271 if (!s->hints)
272 return;
273 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
276 static void wt_longstatus_print_trailer(struct wt_status *s)
278 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
281 static const char *wt_status_unmerged_status_string(int stagemask)
283 switch (stagemask) {
284 case 1:
285 return _("both deleted:");
286 case 2:
287 return _("added by us:");
288 case 3:
289 return _("deleted by them:");
290 case 4:
291 return _("added by them:");
292 case 5:
293 return _("deleted by us:");
294 case 6:
295 return _("both added:");
296 case 7:
297 return _("both modified:");
298 default:
299 BUG("unhandled unmerged status %x", stagemask);
303 static const char *wt_status_diff_status_string(int status)
305 switch (status) {
306 case DIFF_STATUS_ADDED:
307 return _("new file:");
308 case DIFF_STATUS_COPIED:
309 return _("copied:");
310 case DIFF_STATUS_DELETED:
311 return _("deleted:");
312 case DIFF_STATUS_MODIFIED:
313 return _("modified:");
314 case DIFF_STATUS_RENAMED:
315 return _("renamed:");
316 case DIFF_STATUS_TYPE_CHANGED:
317 return _("typechange:");
318 case DIFF_STATUS_UNKNOWN:
319 return _("unknown:");
320 case DIFF_STATUS_UNMERGED:
321 return _("unmerged:");
322 default:
323 return NULL;
327 static int maxwidth(const char *(*label)(int), int minval, int maxval)
329 int result = 0, i;
331 for (i = minval; i <= maxval; i++) {
332 const char *s = label(i);
333 int len = s ? utf8_strwidth(s) : 0;
334 if (len > result)
335 result = len;
337 return result;
340 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
341 struct string_list_item *it)
343 const char *c = color(WT_STATUS_UNMERGED, s);
344 struct wt_status_change_data *d = it->util;
345 struct strbuf onebuf = STRBUF_INIT;
346 static char *padding;
347 static int label_width;
348 const char *one, *how;
349 int len;
351 if (!padding) {
352 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
353 label_width += strlen(" ");
354 padding = xmallocz(label_width);
355 memset(padding, ' ', label_width);
358 one = quote_path(it->string, s->prefix, &onebuf, 0);
359 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
361 how = wt_status_unmerged_status_string(d->stagemask);
362 len = label_width - utf8_strwidth(how);
363 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
364 strbuf_release(&onebuf);
367 static void wt_longstatus_print_change_data(struct wt_status *s,
368 int change_type,
369 struct string_list_item *it)
371 struct wt_status_change_data *d = it->util;
372 const char *c = color(change_type, s);
373 int status;
374 char *one_name;
375 char *two_name;
376 const char *one, *two;
377 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
378 struct strbuf extra = STRBUF_INIT;
379 static char *padding;
380 static int label_width;
381 const char *what;
382 int len;
384 if (!padding) {
385 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
386 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
387 label_width += strlen(" ");
388 padding = xmallocz(label_width);
389 memset(padding, ' ', label_width);
392 one_name = two_name = it->string;
393 switch (change_type) {
394 case WT_STATUS_UPDATED:
395 status = d->index_status;
396 break;
397 case WT_STATUS_CHANGED:
398 if (d->new_submodule_commits || d->dirty_submodule) {
399 strbuf_addstr(&extra, " (");
400 if (d->new_submodule_commits)
401 strbuf_addstr(&extra, _("new commits, "));
402 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
403 strbuf_addstr(&extra, _("modified content, "));
404 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
405 strbuf_addstr(&extra, _("untracked content, "));
406 strbuf_setlen(&extra, extra.len - 2);
407 strbuf_addch(&extra, ')');
409 status = d->worktree_status;
410 break;
411 default:
412 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
413 change_type);
417 * Only pick up the rename it's relevant. If the rename is for
418 * the changed section and we're printing the updated section,
419 * ignore it.
421 if (d->rename_status == status)
422 one_name = d->rename_source;
424 one = quote_path(one_name, s->prefix, &onebuf, 0);
425 two = quote_path(two_name, s->prefix, &twobuf, 0);
427 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
428 what = wt_status_diff_status_string(status);
429 if (!what)
430 BUG("unhandled diff status %c", status);
431 len = label_width - utf8_strwidth(what);
432 assert(len >= 0);
433 if (one_name != two_name)
434 status_printf_more(s, c, "%s%.*s%s -> %s",
435 what, len, padding, one, two);
436 else
437 status_printf_more(s, c, "%s%.*s%s",
438 what, len, padding, one);
439 if (extra.len) {
440 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
441 strbuf_release(&extra);
443 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
444 strbuf_release(&onebuf);
445 strbuf_release(&twobuf);
448 static char short_submodule_status(struct wt_status_change_data *d)
450 if (d->new_submodule_commits)
451 return 'M';
452 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
453 return 'm';
454 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
455 return '?';
456 return d->worktree_status;
459 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
460 struct diff_options *options UNUSED,
461 void *data)
463 struct wt_status *s = data;
464 int i;
466 if (!q->nr)
467 return;
468 s->workdir_dirty = 1;
469 for (i = 0; i < q->nr; i++) {
470 struct diff_filepair *p;
471 struct string_list_item *it;
472 struct wt_status_change_data *d;
474 p = q->queue[i];
475 it = string_list_insert(&s->change, p->two->path);
476 d = it->util;
477 if (!d) {
478 CALLOC_ARRAY(d, 1);
479 it->util = d;
481 if (!d->worktree_status)
482 d->worktree_status = p->status;
483 if (S_ISGITLINK(p->two->mode)) {
484 d->dirty_submodule = p->two->dirty_submodule;
485 d->new_submodule_commits = !oideq(&p->one->oid,
486 &p->two->oid);
487 if (s->status_format == STATUS_FORMAT_SHORT)
488 d->worktree_status = short_submodule_status(d);
491 switch (p->status) {
492 case DIFF_STATUS_ADDED:
493 d->mode_worktree = p->two->mode;
494 break;
496 case DIFF_STATUS_DELETED:
497 d->mode_index = p->one->mode;
498 oidcpy(&d->oid_index, &p->one->oid);
499 /* mode_worktree is zero for a delete. */
500 break;
502 case DIFF_STATUS_COPIED:
503 case DIFF_STATUS_RENAMED:
504 if (d->rename_status)
505 BUG("multiple renames on the same target? how?");
506 d->rename_source = xstrdup(p->one->path);
507 d->rename_score = p->score * 100 / MAX_SCORE;
508 d->rename_status = p->status;
509 /* fallthru */
510 case DIFF_STATUS_MODIFIED:
511 case DIFF_STATUS_TYPE_CHANGED:
512 case DIFF_STATUS_UNMERGED:
513 d->mode_index = p->one->mode;
514 d->mode_worktree = p->two->mode;
515 oidcpy(&d->oid_index, &p->one->oid);
516 break;
518 default:
519 BUG("unhandled diff-files status '%c'", p->status);
520 break;
526 static int unmerged_mask(struct index_state *istate, const char *path)
528 int pos, mask;
529 const struct cache_entry *ce;
531 pos = index_name_pos(istate, path, strlen(path));
532 if (0 <= pos)
533 return 0;
535 mask = 0;
536 pos = -pos-1;
537 while (pos < istate->cache_nr) {
538 ce = istate->cache[pos++];
539 if (strcmp(ce->name, path) || !ce_stage(ce))
540 break;
541 mask |= (1 << (ce_stage(ce) - 1));
543 return mask;
546 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
547 struct diff_options *options UNUSED,
548 void *data)
550 struct wt_status *s = data;
551 int i;
553 for (i = 0; i < q->nr; i++) {
554 struct diff_filepair *p;
555 struct string_list_item *it;
556 struct wt_status_change_data *d;
558 p = q->queue[i];
559 it = string_list_insert(&s->change, p->two->path);
560 d = it->util;
561 if (!d) {
562 CALLOC_ARRAY(d, 1);
563 it->util = d;
565 if (!d->index_status)
566 d->index_status = p->status;
567 switch (p->status) {
568 case DIFF_STATUS_ADDED:
569 /* Leave {mode,oid}_head zero for an add. */
570 d->mode_index = p->two->mode;
571 oidcpy(&d->oid_index, &p->two->oid);
572 s->committable = 1;
573 break;
574 case DIFF_STATUS_DELETED:
575 d->mode_head = p->one->mode;
576 oidcpy(&d->oid_head, &p->one->oid);
577 s->committable = 1;
578 /* Leave {mode,oid}_index zero for a delete. */
579 break;
581 case DIFF_STATUS_COPIED:
582 case DIFF_STATUS_RENAMED:
583 if (d->rename_status)
584 BUG("multiple renames on the same target? how?");
585 d->rename_source = xstrdup(p->one->path);
586 d->rename_score = p->score * 100 / MAX_SCORE;
587 d->rename_status = p->status;
588 /* fallthru */
589 case DIFF_STATUS_MODIFIED:
590 case DIFF_STATUS_TYPE_CHANGED:
591 d->mode_head = p->one->mode;
592 d->mode_index = p->two->mode;
593 oidcpy(&d->oid_head, &p->one->oid);
594 oidcpy(&d->oid_index, &p->two->oid);
595 s->committable = 1;
596 break;
597 case DIFF_STATUS_UNMERGED:
598 d->stagemask = unmerged_mask(s->repo->index,
599 p->two->path);
601 * Don't bother setting {mode,oid}_{head,index} since the print
602 * code will output the stage values directly and not use the
603 * values in these fields.
605 break;
607 default:
608 BUG("unhandled diff-index status '%c'", p->status);
609 break;
614 static void wt_status_collect_changes_worktree(struct wt_status *s)
616 struct rev_info rev;
618 repo_init_revisions(s->repo, &rev, NULL);
619 setup_revisions(0, NULL, &rev, NULL);
620 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
621 rev.diffopt.flags.dirty_submodules = 1;
622 rev.diffopt.ita_invisible_in_index = 1;
623 if (!s->show_untracked_files)
624 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
625 if (s->ignore_submodule_arg) {
626 rev.diffopt.flags.override_submodule_config = 1;
627 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
628 } else if (!rev.diffopt.flags.ignore_submodule_set &&
629 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
630 handle_ignore_submodules_arg(&rev.diffopt, "none");
631 rev.diffopt.format_callback = wt_status_collect_changed_cb;
632 rev.diffopt.format_callback_data = s;
633 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
634 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
635 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
636 copy_pathspec(&rev.prune_data, &s->pathspec);
637 run_diff_files(&rev, 0);
638 release_revisions(&rev);
641 static void wt_status_collect_changes_index(struct wt_status *s)
643 struct rev_info rev;
644 struct setup_revision_opt opt;
646 repo_init_revisions(s->repo, &rev, NULL);
647 memset(&opt, 0, sizeof(opt));
648 opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
649 setup_revisions(0, NULL, &rev, &opt);
651 rev.diffopt.flags.override_submodule_config = 1;
652 rev.diffopt.ita_invisible_in_index = 1;
653 if (s->ignore_submodule_arg) {
654 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
655 } else {
657 * Unless the user did explicitly request a submodule ignore
658 * mode by passing a command line option we do not ignore any
659 * changed submodule SHA-1s when comparing index and HEAD, no
660 * matter what is configured. Otherwise the user won't be
661 * shown any submodules manually added (and which are
662 * staged to be committed), which would be really confusing.
664 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
667 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
668 rev.diffopt.format_callback = wt_status_collect_updated_cb;
669 rev.diffopt.format_callback_data = s;
670 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
671 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
672 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
675 * The `recursive` option must be enabled to allow the diff to recurse
676 * into subdirectories of sparse directory index entries. If it is not
677 * enabled, a subdirectory containing file(s) with changes is reported
678 * as "modified", rather than the modified files themselves.
680 rev.diffopt.flags.recursive = 1;
682 copy_pathspec(&rev.prune_data, &s->pathspec);
683 run_diff_index(&rev, DIFF_INDEX_CACHED);
684 release_revisions(&rev);
687 static int add_file_to_list(const struct object_id *oid,
688 struct strbuf *base, const char *path,
689 unsigned int mode, void *context)
691 struct string_list_item *it;
692 struct wt_status_change_data *d;
693 struct wt_status *s = context;
694 struct strbuf full_name = STRBUF_INIT;
696 if (S_ISDIR(mode))
697 return READ_TREE_RECURSIVE;
699 strbuf_add(&full_name, base->buf, base->len);
700 strbuf_addstr(&full_name, path);
701 it = string_list_insert(&s->change, full_name.buf);
702 d = it->util;
703 if (!d) {
704 CALLOC_ARRAY(d, 1);
705 it->util = d;
708 d->index_status = DIFF_STATUS_ADDED;
709 /* Leave {mode,oid}_head zero for adds. */
710 d->mode_index = mode;
711 oidcpy(&d->oid_index, oid);
712 s->committable = 1;
713 strbuf_release(&full_name);
714 return 0;
717 static void wt_status_collect_changes_initial(struct wt_status *s)
719 struct index_state *istate = s->repo->index;
720 struct strbuf base = STRBUF_INIT;
721 int i;
723 for (i = 0; i < istate->cache_nr; i++) {
724 struct string_list_item *it;
725 struct wt_status_change_data *d;
726 const struct cache_entry *ce = istate->cache[i];
728 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
729 continue;
730 if (ce_intent_to_add(ce))
731 continue;
732 if (S_ISSPARSEDIR(ce->ce_mode)) {
734 * This is a sparse directory entry, so we want to collect all
735 * of the added files within the tree. This requires recursively
736 * expanding the trees to find the elements that are new in this
737 * tree and marking them with DIFF_STATUS_ADDED.
739 struct pathspec ps = { 0 };
740 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
742 ps.recursive = 1;
743 ps.has_wildcard = 1;
744 ps.max_depth = -1;
746 strbuf_reset(&base);
747 strbuf_add(&base, ce->name, ce->ce_namelen);
748 read_tree_at(istate->repo, tree, &base, 0, &ps,
749 add_file_to_list, s);
751 continue;
754 it = string_list_insert(&s->change, ce->name);
755 d = it->util;
756 if (!d) {
757 CALLOC_ARRAY(d, 1);
758 it->util = d;
760 if (ce_stage(ce)) {
761 d->index_status = DIFF_STATUS_UNMERGED;
762 d->stagemask |= (1 << (ce_stage(ce) - 1));
764 * Don't bother setting {mode,oid}_{head,index} since the print
765 * code will output the stage values directly and not use the
766 * values in these fields.
768 s->committable = 1;
769 } else {
770 d->index_status = DIFF_STATUS_ADDED;
771 /* Leave {mode,oid}_head zero for adds. */
772 d->mode_index = ce->ce_mode;
773 oidcpy(&d->oid_index, &ce->oid);
774 s->committable = 1;
778 strbuf_release(&base);
781 static void wt_status_collect_untracked(struct wt_status *s)
783 int i;
784 struct dir_struct dir = DIR_INIT;
785 uint64_t t_begin = getnanotime();
786 struct index_state *istate = s->repo->index;
788 if (!s->show_untracked_files)
789 return;
791 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
792 dir.flags |=
793 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
794 if (s->show_ignored_mode) {
795 dir.flags |= DIR_SHOW_IGNORED_TOO;
797 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
798 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
799 } else {
800 dir.untracked = istate->untracked;
803 setup_standard_excludes(&dir);
805 fill_directory(&dir, istate, &s->pathspec);
807 for (i = 0; i < dir.nr; i++) {
808 struct dir_entry *ent = dir.entries[i];
809 if (index_name_is_other(istate, ent->name, ent->len))
810 string_list_insert(&s->untracked, ent->name);
813 for (i = 0; i < dir.ignored_nr; i++) {
814 struct dir_entry *ent = dir.ignored[i];
815 if (index_name_is_other(istate, ent->name, ent->len))
816 string_list_insert(&s->ignored, ent->name);
819 dir_clear(&dir);
821 if (advice_enabled(ADVICE_STATUS_U_OPTION))
822 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
825 static int has_unmerged(struct wt_status *s)
827 int i;
829 for (i = 0; i < s->change.nr; i++) {
830 struct wt_status_change_data *d;
831 d = s->change.items[i].util;
832 if (d->stagemask)
833 return 1;
835 return 0;
838 void wt_status_collect(struct wt_status *s)
840 trace2_region_enter("status", "worktrees", s->repo);
841 wt_status_collect_changes_worktree(s);
842 trace2_region_leave("status", "worktrees", s->repo);
844 if (s->is_initial) {
845 trace2_region_enter("status", "initial", s->repo);
846 wt_status_collect_changes_initial(s);
847 trace2_region_leave("status", "initial", s->repo);
848 } else {
849 trace2_region_enter("status", "index", s->repo);
850 wt_status_collect_changes_index(s);
851 trace2_region_leave("status", "index", s->repo);
854 trace2_region_enter("status", "untracked", s->repo);
855 wt_status_collect_untracked(s);
856 trace2_region_leave("status", "untracked", s->repo);
858 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
859 if (s->state.merge_in_progress && !has_unmerged(s))
860 s->committable = 1;
863 void wt_status_collect_free_buffers(struct wt_status *s)
865 wt_status_state_free_buffers(&s->state);
868 void wt_status_state_free_buffers(struct wt_status_state *state)
870 FREE_AND_NULL(state->branch);
871 FREE_AND_NULL(state->onto);
872 FREE_AND_NULL(state->detached_from);
873 FREE_AND_NULL(state->bisecting_from);
876 static void wt_longstatus_print_unmerged(struct wt_status *s)
878 int shown_header = 0;
879 int i;
881 for (i = 0; i < s->change.nr; i++) {
882 struct wt_status_change_data *d;
883 struct string_list_item *it;
884 it = &(s->change.items[i]);
885 d = it->util;
886 if (!d->stagemask)
887 continue;
888 if (!shown_header) {
889 wt_longstatus_print_unmerged_header(s);
890 shown_header = 1;
892 wt_longstatus_print_unmerged_data(s, it);
894 if (shown_header)
895 wt_longstatus_print_trailer(s);
899 static void wt_longstatus_print_updated(struct wt_status *s)
901 int shown_header = 0;
902 int i;
904 for (i = 0; i < s->change.nr; i++) {
905 struct wt_status_change_data *d;
906 struct string_list_item *it;
907 it = &(s->change.items[i]);
908 d = it->util;
909 if (!d->index_status ||
910 d->index_status == DIFF_STATUS_UNMERGED)
911 continue;
912 if (!shown_header) {
913 wt_longstatus_print_cached_header(s);
914 shown_header = 1;
916 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
918 if (shown_header)
919 wt_longstatus_print_trailer(s);
923 * -1 : has delete
924 * 0 : no change
925 * 1 : some change but no delete
927 static int wt_status_check_worktree_changes(struct wt_status *s,
928 int *dirty_submodules)
930 int i;
931 int changes = 0;
933 *dirty_submodules = 0;
935 for (i = 0; i < s->change.nr; i++) {
936 struct wt_status_change_data *d;
937 d = s->change.items[i].util;
938 if (!d->worktree_status ||
939 d->worktree_status == DIFF_STATUS_UNMERGED)
940 continue;
941 if (!changes)
942 changes = 1;
943 if (d->dirty_submodule)
944 *dirty_submodules = 1;
945 if (d->worktree_status == DIFF_STATUS_DELETED)
946 changes = -1;
948 return changes;
951 static void wt_longstatus_print_changed(struct wt_status *s)
953 int i, dirty_submodules;
954 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
956 if (!worktree_changes)
957 return;
959 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
961 for (i = 0; i < s->change.nr; i++) {
962 struct wt_status_change_data *d;
963 struct string_list_item *it;
964 it = &(s->change.items[i]);
965 d = it->util;
966 if (!d->worktree_status ||
967 d->worktree_status == DIFF_STATUS_UNMERGED)
968 continue;
969 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
971 wt_longstatus_print_trailer(s);
974 static int stash_count_refs(struct object_id *ooid UNUSED,
975 struct object_id *noid UNUSED,
976 const char *email UNUSED,
977 timestamp_t timestamp UNUSED, int tz UNUSED,
978 const char *message UNUSED, void *cb_data)
980 int *c = cb_data;
981 (*c)++;
982 return 0;
985 static int count_stash_entries(void)
987 int n = 0;
988 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
989 "refs/stash", stash_count_refs, &n);
990 return n;
993 static void wt_longstatus_print_stash_summary(struct wt_status *s)
995 int stash_count = count_stash_entries();
997 if (stash_count > 0)
998 status_printf_ln(s, GIT_COLOR_NORMAL,
999 Q_("Your stash currently has %d entry",
1000 "Your stash currently has %d entries", stash_count),
1001 stash_count);
1004 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
1006 struct child_process sm_summary = CHILD_PROCESS_INIT;
1007 struct strbuf cmd_stdout = STRBUF_INIT;
1008 struct strbuf summary = STRBUF_INIT;
1009 char *summary_content;
1011 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1013 strvec_push(&sm_summary.args, "submodule");
1014 strvec_push(&sm_summary.args, "summary");
1015 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1016 strvec_push(&sm_summary.args, "--for-status");
1017 strvec_push(&sm_summary.args, "--summary-limit");
1018 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1019 if (!uncommitted)
1020 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1022 sm_summary.git_cmd = 1;
1023 sm_summary.no_stdin = 1;
1025 capture_command(&sm_summary, &cmd_stdout, 1024);
1027 /* prepend header, only if there's an actual output */
1028 if (cmd_stdout.len) {
1029 if (uncommitted)
1030 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1031 else
1032 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1033 strbuf_addstr(&summary, "\n\n");
1035 strbuf_addbuf(&summary, &cmd_stdout);
1036 strbuf_release(&cmd_stdout);
1038 if (s->display_comment_prefix) {
1039 size_t len;
1040 summary_content = strbuf_detach(&summary, &len);
1041 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1042 free(summary_content);
1045 fputs(summary.buf, s->fp);
1046 strbuf_release(&summary);
1049 static void wt_longstatus_print_other(struct wt_status *s,
1050 struct string_list *l,
1051 const char *what,
1052 const char *how)
1054 int i;
1055 struct strbuf buf = STRBUF_INIT;
1056 static struct string_list output = STRING_LIST_INIT_DUP;
1057 struct column_options copts;
1059 if (!l->nr)
1060 return;
1062 wt_longstatus_print_other_header(s, what, how);
1064 for (i = 0; i < l->nr; i++) {
1065 struct string_list_item *it;
1066 const char *path;
1067 it = &(l->items[i]);
1068 path = quote_path(it->string, s->prefix, &buf, 0);
1069 if (column_active(s->colopts)) {
1070 string_list_append(&output, path);
1071 continue;
1073 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1074 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1075 "%s\n", path);
1078 strbuf_release(&buf);
1079 if (!column_active(s->colopts))
1080 goto conclude;
1082 strbuf_addf(&buf, "%s%s\t%s",
1083 color(WT_STATUS_HEADER, s),
1084 s->display_comment_prefix ? "#" : "",
1085 color(WT_STATUS_UNTRACKED, s));
1086 memset(&copts, 0, sizeof(copts));
1087 copts.padding = 1;
1088 copts.indent = buf.buf;
1089 if (want_color(s->use_color))
1090 copts.nl = GIT_COLOR_RESET "\n";
1091 print_columns(&output, s->colopts, &copts);
1092 string_list_clear(&output, 0);
1093 strbuf_release(&buf);
1094 conclude:
1095 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1098 size_t wt_status_locate_end(const char *s, size_t len)
1100 const char *p;
1101 struct strbuf pattern = STRBUF_INIT;
1103 strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line);
1104 if (starts_with(s, pattern.buf + 1))
1105 len = 0;
1106 else if ((p = strstr(s, pattern.buf))) {
1107 size_t newlen = p - s + 1;
1108 if (newlen < len)
1109 len = newlen;
1111 strbuf_release(&pattern);
1112 return len;
1115 void wt_status_append_cut_line(struct strbuf *buf)
1117 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1119 strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1120 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1123 void wt_status_add_cut_line(struct wt_status *s)
1125 struct strbuf buf = STRBUF_INIT;
1127 if (s->added_cut_line)
1128 return;
1129 s->added_cut_line = 1;
1130 wt_status_append_cut_line(&buf);
1131 fputs(buf.buf, s->fp);
1132 strbuf_release(&buf);
1135 static void wt_longstatus_print_verbose(struct wt_status *s)
1137 struct rev_info rev;
1138 struct setup_revision_opt opt;
1139 int dirty_submodules;
1140 const char *c = color(WT_STATUS_HEADER, s);
1142 repo_init_revisions(s->repo, &rev, NULL);
1143 rev.diffopt.flags.allow_textconv = 1;
1144 rev.diffopt.ita_invisible_in_index = 1;
1146 memset(&opt, 0, sizeof(opt));
1147 opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
1148 setup_revisions(0, NULL, &rev, &opt);
1150 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1151 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1152 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1153 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1154 rev.diffopt.file = s->fp;
1155 rev.diffopt.close_file = 0;
1157 * If we're not going to stdout, then we definitely don't
1158 * want color, since we are going to the commit message
1159 * file (and even the "auto" setting won't work, since it
1160 * will have checked isatty on stdout). But we then do want
1161 * to insert the scissor line here to reliably remove the
1162 * diff before committing, if we didn't already include one
1163 * before.
1165 if (s->fp != stdout) {
1166 rev.diffopt.use_color = 0;
1167 wt_status_add_cut_line(s);
1169 if (s->verbose > 1 && s->committable) {
1170 /* print_updated() printed a header, so do we */
1171 if (s->fp != stdout)
1172 wt_longstatus_print_trailer(s);
1173 status_printf_ln(s, c, _("Changes to be committed:"));
1174 rev.diffopt.a_prefix = "c/";
1175 rev.diffopt.b_prefix = "i/";
1176 } /* else use prefix as per user config */
1177 run_diff_index(&rev, DIFF_INDEX_CACHED);
1178 if (s->verbose > 1 &&
1179 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1180 status_printf_ln(s, c,
1181 "--------------------------------------------------");
1182 status_printf_ln(s, c, _("Changes not staged for commit:"));
1183 setup_work_tree();
1184 rev.diffopt.a_prefix = "i/";
1185 rev.diffopt.b_prefix = "w/";
1186 run_diff_files(&rev, 0);
1188 release_revisions(&rev);
1191 static void wt_longstatus_print_tracking(struct wt_status *s)
1193 struct strbuf sb = STRBUF_INIT;
1194 const char *cp, *ep, *branch_name;
1195 struct branch *branch;
1196 uint64_t t_begin = 0;
1198 assert(s->branch && !s->is_initial);
1199 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1200 return;
1201 branch = branch_get(branch_name);
1203 t_begin = getnanotime();
1205 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1206 !s->commit_template))
1207 return;
1209 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1210 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1211 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1212 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1213 strbuf_addf(&sb, _("\n"
1214 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1215 "You can use '--no-ahead-behind' to avoid this.\n"),
1216 t_delta_in_ms / 1000.0);
1220 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1221 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1222 "%s%s%.*s",
1223 s->display_comment_prefix ? comment_line_str : "",
1224 s->display_comment_prefix ? " " : "",
1225 (int)(ep - cp), cp);
1226 if (s->display_comment_prefix)
1227 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
1228 comment_line_str);
1229 else
1230 fputs("\n", s->fp);
1231 strbuf_release(&sb);
1234 static int uf_was_slow(struct wt_status *s)
1236 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1237 s->untracked_in_ms = 3250;
1238 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1241 static void show_merge_in_progress(struct wt_status *s,
1242 const char *color)
1244 if (has_unmerged(s)) {
1245 status_printf_ln(s, color, _("You have unmerged paths."));
1246 if (s->hints) {
1247 status_printf_ln(s, color,
1248 _(" (fix conflicts and run \"git commit\")"));
1249 status_printf_ln(s, color,
1250 _(" (use \"git merge --abort\" to abort the merge)"));
1252 } else {
1253 status_printf_ln(s, color,
1254 _("All conflicts fixed but you are still merging."));
1255 if (s->hints)
1256 status_printf_ln(s, color,
1257 _(" (use \"git commit\" to conclude merge)"));
1259 wt_longstatus_print_trailer(s);
1262 static void show_am_in_progress(struct wt_status *s,
1263 const char *color)
1265 int am_empty_patch;
1267 status_printf_ln(s, color,
1268 _("You are in the middle of an am session."));
1269 if (s->state.am_empty_patch)
1270 status_printf_ln(s, color,
1271 _("The current patch is empty."));
1272 if (s->hints) {
1273 am_empty_patch = s->state.am_empty_patch;
1274 if (!am_empty_patch)
1275 status_printf_ln(s, color,
1276 _(" (fix conflicts and then run \"git am --continue\")"));
1277 status_printf_ln(s, color,
1278 _(" (use \"git am --skip\" to skip this patch)"));
1279 if (am_empty_patch)
1280 status_printf_ln(s, color,
1281 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1282 status_printf_ln(s, color,
1283 _(" (use \"git am --abort\" to restore the original branch)"));
1285 wt_longstatus_print_trailer(s);
1288 static char *read_line_from_git_path(const char *filename)
1290 struct strbuf buf = STRBUF_INIT;
1291 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1293 if (!fp) {
1294 strbuf_release(&buf);
1295 return NULL;
1297 strbuf_getline_lf(&buf, fp);
1298 if (!fclose(fp)) {
1299 return strbuf_detach(&buf, NULL);
1300 } else {
1301 strbuf_release(&buf);
1302 return NULL;
1306 static int split_commit_in_progress(struct wt_status *s)
1308 int split_in_progress = 0;
1309 struct object_id head_oid, orig_head_oid;
1310 char *rebase_amend, *rebase_orig_head;
1311 int head_flags, orig_head_flags;
1313 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1314 !s->branch || strcmp(s->branch, "HEAD"))
1315 return 0;
1317 if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1318 &head_oid, &head_flags) ||
1319 refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1320 &orig_head_oid, &orig_head_flags))
1321 return 0;
1322 if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1323 return 0;
1325 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1326 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1328 if (!rebase_amend || !rebase_orig_head)
1329 ; /* fall through, no split in progress */
1330 else if (!strcmp(rebase_amend, rebase_orig_head))
1331 split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1332 else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
1333 split_in_progress = 1;
1335 free(rebase_amend);
1336 free(rebase_orig_head);
1338 return split_in_progress;
1342 * Turn
1343 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1344 * into
1345 * "pick d6a2f03 some message"
1347 * The function assumes that the line does not contain useless spaces
1348 * before or after the command.
1350 static void abbrev_oid_in_line(struct strbuf *line)
1352 struct strbuf **split;
1353 int i;
1355 if (starts_with(line->buf, "exec ") ||
1356 starts_with(line->buf, "x ") ||
1357 starts_with(line->buf, "label ") ||
1358 starts_with(line->buf, "l "))
1359 return;
1361 split = strbuf_split_max(line, ' ', 3);
1362 if (split[0] && split[1]) {
1363 struct object_id oid;
1366 * strbuf_split_max left a space. Trim it and re-add
1367 * it after abbreviation.
1369 strbuf_trim(split[1]);
1370 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1371 strbuf_reset(split[1]);
1372 strbuf_add_unique_abbrev(split[1], &oid,
1373 DEFAULT_ABBREV);
1374 strbuf_addch(split[1], ' ');
1375 strbuf_reset(line);
1376 for (i = 0; split[i]; i++)
1377 strbuf_addbuf(line, split[i]);
1380 strbuf_list_free(split);
1383 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1385 struct strbuf line = STRBUF_INIT;
1386 FILE *f = fopen(git_path("%s", fname), "r");
1388 if (!f) {
1389 if (errno == ENOENT)
1390 return -1;
1391 die_errno("Could not open file %s for reading",
1392 git_path("%s", fname));
1394 while (!strbuf_getline_lf(&line, f)) {
1395 if (starts_with(line.buf, comment_line_str))
1396 continue;
1397 strbuf_trim(&line);
1398 if (!line.len)
1399 continue;
1400 abbrev_oid_in_line(&line);
1401 string_list_append(lines, line.buf);
1403 fclose(f);
1404 strbuf_release(&line);
1405 return 0;
1408 static void show_rebase_information(struct wt_status *s,
1409 const char *color)
1411 if (s->state.rebase_interactive_in_progress) {
1412 int i;
1413 int nr_lines_to_show = 2;
1415 struct string_list have_done = STRING_LIST_INIT_DUP;
1416 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1418 read_rebase_todolist("rebase-merge/done", &have_done);
1419 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1420 &yet_to_do))
1421 status_printf_ln(s, color,
1422 _("git-rebase-todo is missing."));
1423 if (have_done.nr == 0)
1424 status_printf_ln(s, color, _("No commands done."));
1425 else {
1426 status_printf_ln(s, color,
1427 Q_("Last command done (%"PRIuMAX" command done):",
1428 "Last commands done (%"PRIuMAX" commands done):",
1429 have_done.nr),
1430 (uintmax_t)have_done.nr);
1431 for (i = (have_done.nr > nr_lines_to_show)
1432 ? have_done.nr - nr_lines_to_show : 0;
1433 i < have_done.nr;
1434 i++)
1435 status_printf_ln(s, color, " %s", have_done.items[i].string);
1436 if (have_done.nr > nr_lines_to_show && s->hints)
1437 status_printf_ln(s, color,
1438 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1441 if (yet_to_do.nr == 0)
1442 status_printf_ln(s, color,
1443 _("No commands remaining."));
1444 else {
1445 status_printf_ln(s, color,
1446 Q_("Next command to do (%"PRIuMAX" remaining command):",
1447 "Next commands to do (%"PRIuMAX" remaining commands):",
1448 yet_to_do.nr),
1449 (uintmax_t)yet_to_do.nr);
1450 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1451 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1452 if (s->hints)
1453 status_printf_ln(s, color,
1454 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1456 string_list_clear(&yet_to_do, 0);
1457 string_list_clear(&have_done, 0);
1461 static void print_rebase_state(struct wt_status *s,
1462 const char *color)
1464 if (s->state.branch)
1465 status_printf_ln(s, color,
1466 _("You are currently rebasing branch '%s' on '%s'."),
1467 s->state.branch,
1468 s->state.onto);
1469 else
1470 status_printf_ln(s, color,
1471 _("You are currently rebasing."));
1474 static void show_rebase_in_progress(struct wt_status *s,
1475 const char *color)
1477 struct stat st;
1479 show_rebase_information(s, color);
1480 if (has_unmerged(s)) {
1481 print_rebase_state(s, color);
1482 if (s->hints) {
1483 status_printf_ln(s, color,
1484 _(" (fix conflicts and then run \"git rebase --continue\")"));
1485 status_printf_ln(s, color,
1486 _(" (use \"git rebase --skip\" to skip this patch)"));
1487 status_printf_ln(s, color,
1488 _(" (use \"git rebase --abort\" to check out the original branch)"));
1490 } else if (s->state.rebase_in_progress ||
1491 !stat(git_path_merge_msg(s->repo), &st)) {
1492 print_rebase_state(s, color);
1493 if (s->hints)
1494 status_printf_ln(s, color,
1495 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1496 } else if (split_commit_in_progress(s)) {
1497 if (s->state.branch)
1498 status_printf_ln(s, color,
1499 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1500 s->state.branch,
1501 s->state.onto);
1502 else
1503 status_printf_ln(s, color,
1504 _("You are currently splitting a commit during a rebase."));
1505 if (s->hints)
1506 status_printf_ln(s, color,
1507 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1508 } else {
1509 if (s->state.branch)
1510 status_printf_ln(s, color,
1511 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1512 s->state.branch,
1513 s->state.onto);
1514 else
1515 status_printf_ln(s, color,
1516 _("You are currently editing a commit during a rebase."));
1517 if (s->hints && !s->amend) {
1518 status_printf_ln(s, color,
1519 _(" (use \"git commit --amend\" to amend the current commit)"));
1520 status_printf_ln(s, color,
1521 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1524 wt_longstatus_print_trailer(s);
1527 static void show_cherry_pick_in_progress(struct wt_status *s,
1528 const char *color)
1530 if (is_null_oid(&s->state.cherry_pick_head_oid))
1531 status_printf_ln(s, color,
1532 _("Cherry-pick currently in progress."));
1533 else
1534 status_printf_ln(s, color,
1535 _("You are currently cherry-picking commit %s."),
1536 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1537 DEFAULT_ABBREV));
1539 if (s->hints) {
1540 if (has_unmerged(s))
1541 status_printf_ln(s, color,
1542 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1543 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1544 status_printf_ln(s, color,
1545 _(" (run \"git cherry-pick --continue\" to continue)"));
1546 else
1547 status_printf_ln(s, color,
1548 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1549 status_printf_ln(s, color,
1550 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1551 status_printf_ln(s, color,
1552 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1554 wt_longstatus_print_trailer(s);
1557 static void show_revert_in_progress(struct wt_status *s,
1558 const char *color)
1560 if (is_null_oid(&s->state.revert_head_oid))
1561 status_printf_ln(s, color,
1562 _("Revert currently in progress."));
1563 else
1564 status_printf_ln(s, color,
1565 _("You are currently reverting commit %s."),
1566 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1567 DEFAULT_ABBREV));
1568 if (s->hints) {
1569 if (has_unmerged(s))
1570 status_printf_ln(s, color,
1571 _(" (fix conflicts and run \"git revert --continue\")"));
1572 else if (is_null_oid(&s->state.revert_head_oid))
1573 status_printf_ln(s, color,
1574 _(" (run \"git revert --continue\" to continue)"));
1575 else
1576 status_printf_ln(s, color,
1577 _(" (all conflicts fixed: run \"git revert --continue\")"));
1578 status_printf_ln(s, color,
1579 _(" (use \"git revert --skip\" to skip this patch)"));
1580 status_printf_ln(s, color,
1581 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1583 wt_longstatus_print_trailer(s);
1586 static void show_bisect_in_progress(struct wt_status *s,
1587 const char *color)
1589 if (s->state.bisecting_from)
1590 status_printf_ln(s, color,
1591 _("You are currently bisecting, started from branch '%s'."),
1592 s->state.bisecting_from);
1593 else
1594 status_printf_ln(s, color,
1595 _("You are currently bisecting."));
1596 if (s->hints)
1597 status_printf_ln(s, color,
1598 _(" (use \"git bisect reset\" to get back to the original branch)"));
1599 wt_longstatus_print_trailer(s);
1602 static void show_sparse_checkout_in_use(struct wt_status *s,
1603 const char *color)
1605 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1606 return;
1608 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1609 status_printf_ln(s, color, _("You are in a sparse checkout."));
1610 else
1611 status_printf_ln(s, color,
1612 _("You are in a sparse checkout with %d%% of tracked files present."),
1613 s->state.sparse_checkout_percentage);
1614 wt_longstatus_print_trailer(s);
1618 * Extract branch information from rebase/bisect
1620 static char *get_branch(const struct worktree *wt, const char *path)
1622 struct strbuf sb = STRBUF_INIT;
1623 struct object_id oid;
1624 const char *branch_name;
1626 if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1627 goto got_nothing;
1629 while (sb.len && sb.buf[sb.len - 1] == '\n')
1630 strbuf_setlen(&sb, sb.len - 1);
1631 if (!sb.len)
1632 goto got_nothing;
1633 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1634 strbuf_remove(&sb, 0, branch_name - sb.buf);
1635 else if (starts_with(sb.buf, "refs/"))
1637 else if (!get_oid_hex(sb.buf, &oid)) {
1638 strbuf_reset(&sb);
1639 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1640 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1641 goto got_nothing;
1642 else /* bisect */
1644 return strbuf_detach(&sb, NULL);
1646 got_nothing:
1647 strbuf_release(&sb);
1648 return NULL;
1651 struct grab_1st_switch_cbdata {
1652 struct strbuf buf;
1653 struct object_id noid;
1656 static int grab_1st_switch(struct object_id *ooid UNUSED,
1657 struct object_id *noid,
1658 const char *email UNUSED,
1659 timestamp_t timestamp UNUSED, int tz UNUSED,
1660 const char *message, void *cb_data)
1662 struct grab_1st_switch_cbdata *cb = cb_data;
1663 const char *target = NULL, *end;
1665 if (!skip_prefix(message, "checkout: moving from ", &message))
1666 return 0;
1667 target = strstr(message, " to ");
1668 if (!target)
1669 return 0;
1670 target += strlen(" to ");
1671 strbuf_reset(&cb->buf);
1672 oidcpy(&cb->noid, noid);
1673 end = strchrnul(target, '\n');
1674 strbuf_add(&cb->buf, target, end - target);
1675 if (!strcmp(cb->buf.buf, "HEAD")) {
1676 /* HEAD is relative. Resolve it to the right reflog entry. */
1677 strbuf_reset(&cb->buf);
1678 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1680 return 1;
1683 static void wt_status_get_detached_from(struct repository *r,
1684 struct wt_status_state *state)
1686 struct grab_1st_switch_cbdata cb;
1687 struct commit *commit;
1688 struct object_id oid;
1689 char *ref = NULL;
1691 strbuf_init(&cb.buf, 0);
1692 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) {
1693 strbuf_release(&cb.buf);
1694 return;
1697 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
1698 1) == 1 &&
1699 /* oid is a commit? match without further lookup */
1700 (oideq(&cb.noid, &oid) ||
1701 /* perhaps oid is a tag, try to dereference to a commit */
1702 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1703 oideq(&cb.noid, &commit->object.oid)))) {
1704 const char *from = ref;
1705 if (!skip_prefix(from, "refs/tags/", &from))
1706 skip_prefix(from, "refs/remotes/", &from);
1707 state->detached_from = xstrdup(from);
1708 } else
1709 state->detached_from =
1710 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1711 oidcpy(&state->detached_oid, &cb.noid);
1712 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1713 oideq(&oid, &state->detached_oid);
1715 free(ref);
1716 strbuf_release(&cb.buf);
1719 int wt_status_check_rebase(const struct worktree *wt,
1720 struct wt_status_state *state)
1722 struct stat st;
1724 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1725 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1726 state->am_in_progress = 1;
1727 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1728 state->am_empty_patch = 1;
1729 } else {
1730 state->rebase_in_progress = 1;
1731 state->branch = get_branch(wt, "rebase-apply/head-name");
1732 state->onto = get_branch(wt, "rebase-apply/onto");
1734 } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1735 if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1736 state->rebase_interactive_in_progress = 1;
1737 else
1738 state->rebase_in_progress = 1;
1739 state->branch = get_branch(wt, "rebase-merge/head-name");
1740 state->onto = get_branch(wt, "rebase-merge/onto");
1741 } else
1742 return 0;
1743 return 1;
1746 int wt_status_check_bisect(const struct worktree *wt,
1747 struct wt_status_state *state)
1749 struct stat st;
1751 if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1752 state->bisect_in_progress = 1;
1753 state->bisecting_from = get_branch(wt, "BISECT_START");
1754 return 1;
1756 return 0;
1759 static void wt_status_check_sparse_checkout(struct repository *r,
1760 struct wt_status_state *state)
1762 int skip_worktree = 0;
1763 int i;
1765 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1767 * Don't compute percentage of checked out files if we
1768 * aren't in a sparse checkout or would get division by 0.
1770 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1771 return;
1774 if (r->index->sparse_index) {
1775 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1776 return;
1779 for (i = 0; i < r->index->cache_nr; i++) {
1780 struct cache_entry *ce = r->index->cache[i];
1781 if (ce_skip_worktree(ce))
1782 skip_worktree++;
1785 state->sparse_checkout_percentage =
1786 100 - (100 * skip_worktree)/r->index->cache_nr;
1789 void wt_status_get_state(struct repository *r,
1790 struct wt_status_state *state,
1791 int get_detached_from)
1793 struct stat st;
1794 struct object_id oid;
1795 enum replay_action action;
1797 if (!stat(git_path_merge_head(r), &st)) {
1798 wt_status_check_rebase(NULL, state);
1799 state->merge_in_progress = 1;
1800 } else if (wt_status_check_rebase(NULL, state)) {
1801 ; /* all set */
1802 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1803 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1804 state->cherry_pick_in_progress = 1;
1805 oidcpy(&state->cherry_pick_head_oid, &oid);
1807 wt_status_check_bisect(NULL, state);
1808 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1809 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
1810 state->revert_in_progress = 1;
1811 oidcpy(&state->revert_head_oid, &oid);
1813 if (!sequencer_get_last_command(r, &action)) {
1814 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1815 state->cherry_pick_in_progress = 1;
1816 oidcpy(&state->cherry_pick_head_oid, null_oid());
1817 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1818 state->revert_in_progress = 1;
1819 oidcpy(&state->revert_head_oid, null_oid());
1822 if (get_detached_from)
1823 wt_status_get_detached_from(r, state);
1824 wt_status_check_sparse_checkout(r, state);
1827 static void wt_longstatus_print_state(struct wt_status *s)
1829 const char *state_color = color(WT_STATUS_HEADER, s);
1830 struct wt_status_state *state = &s->state;
1832 if (state->merge_in_progress) {
1833 if (state->rebase_interactive_in_progress) {
1834 show_rebase_information(s, state_color);
1835 fputs("\n", s->fp);
1837 show_merge_in_progress(s, state_color);
1838 } else if (state->am_in_progress)
1839 show_am_in_progress(s, state_color);
1840 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1841 show_rebase_in_progress(s, state_color);
1842 else if (state->cherry_pick_in_progress)
1843 show_cherry_pick_in_progress(s, state_color);
1844 else if (state->revert_in_progress)
1845 show_revert_in_progress(s, state_color);
1846 if (state->bisect_in_progress)
1847 show_bisect_in_progress(s, state_color);
1849 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1850 show_sparse_checkout_in_use(s, state_color);
1853 static void wt_longstatus_print(struct wt_status *s)
1855 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1856 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1857 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1859 if (s->branch) {
1860 const char *on_what = _("On branch ");
1861 const char *branch_name = s->branch;
1862 if (!strcmp(branch_name, "HEAD")) {
1863 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1864 if (s->state.rebase_in_progress ||
1865 s->state.rebase_interactive_in_progress) {
1866 if (s->state.rebase_interactive_in_progress)
1867 on_what = _("interactive rebase in progress; onto ");
1868 else
1869 on_what = _("rebase in progress; onto ");
1870 branch_name = s->state.onto;
1871 } else if (s->state.detached_from) {
1872 branch_name = s->state.detached_from;
1873 if (s->state.detached_at)
1874 on_what = _("HEAD detached at ");
1875 else
1876 on_what = _("HEAD detached from ");
1877 } else {
1878 branch_name = "";
1879 on_what = _("Not currently on any branch.");
1881 } else
1882 skip_prefix(branch_name, "refs/heads/", &branch_name);
1883 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1884 status_printf_more(s, branch_status_color, "%s", on_what);
1885 status_printf_more(s, branch_color, "%s\n", branch_name);
1886 if (!s->is_initial)
1887 wt_longstatus_print_tracking(s);
1890 wt_longstatus_print_state(s);
1892 if (s->is_initial) {
1893 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1894 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1895 s->commit_template
1896 ? _("Initial commit")
1897 : _("No commits yet"));
1898 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1901 wt_longstatus_print_updated(s);
1902 wt_longstatus_print_unmerged(s);
1903 wt_longstatus_print_changed(s);
1904 if (s->submodule_summary &&
1905 (!s->ignore_submodule_arg ||
1906 strcmp(s->ignore_submodule_arg, "all"))) {
1907 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1908 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1910 if (s->show_untracked_files) {
1911 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1912 if (s->show_ignored_mode)
1913 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1914 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1915 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1916 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1917 status_printf_ln(s, GIT_COLOR_NORMAL,
1918 _("It took %.2f seconds to enumerate untracked files,\n"
1919 "but the results were cached, and subsequent runs may be faster."),
1920 s->untracked_in_ms / 1000.0);
1921 } else {
1922 status_printf_ln(s, GIT_COLOR_NORMAL,
1923 _("It took %.2f seconds to enumerate untracked files."),
1924 s->untracked_in_ms / 1000.0);
1926 status_printf_ln(s, GIT_COLOR_NORMAL,
1927 _("See 'git help status' for information on how to improve this."));
1928 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1930 } else if (s->committable)
1931 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1932 s->hints
1933 ? _(" (use -u option to show untracked files)") : "");
1935 if (s->verbose)
1936 wt_longstatus_print_verbose(s);
1937 if (!s->committable) {
1938 if (s->amend)
1939 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1940 else if (s->nowarn)
1941 ; /* nothing */
1942 else if (s->workdir_dirty) {
1943 if (s->hints)
1944 fprintf(s->fp, _("no changes added to commit "
1945 "(use \"git add\" and/or "
1946 "\"git commit -a\")\n"));
1947 else
1948 fprintf(s->fp, _("no changes added to "
1949 "commit\n"));
1950 } else if (s->untracked.nr) {
1951 if (s->hints)
1952 fprintf(s->fp, _("nothing added to commit but "
1953 "untracked files present (use "
1954 "\"git add\" to track)\n"));
1955 else
1956 fprintf(s->fp, _("nothing added to commit but "
1957 "untracked files present\n"));
1958 } else if (s->is_initial) {
1959 if (s->hints)
1960 fprintf(s->fp, _("nothing to commit (create/"
1961 "copy files and use \"git "
1962 "add\" to track)\n"));
1963 else
1964 fprintf(s->fp, _("nothing to commit\n"));
1965 } else if (!s->show_untracked_files) {
1966 if (s->hints)
1967 fprintf(s->fp, _("nothing to commit (use -u to "
1968 "show untracked files)\n"));
1969 else
1970 fprintf(s->fp, _("nothing to commit\n"));
1971 } else
1972 fprintf(s->fp, _("nothing to commit, working tree "
1973 "clean\n"));
1975 if(s->show_stash)
1976 wt_longstatus_print_stash_summary(s);
1979 static void wt_shortstatus_unmerged(struct string_list_item *it,
1980 struct wt_status *s)
1982 struct wt_status_change_data *d = it->util;
1983 const char *how = "??";
1985 switch (d->stagemask) {
1986 case 1: how = "DD"; break; /* both deleted */
1987 case 2: how = "AU"; break; /* added by us */
1988 case 3: how = "UD"; break; /* deleted by them */
1989 case 4: how = "UA"; break; /* added by them */
1990 case 5: how = "DU"; break; /* deleted by us */
1991 case 6: how = "AA"; break; /* both added */
1992 case 7: how = "UU"; break; /* both modified */
1994 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1995 if (s->null_termination) {
1996 fprintf(s->fp, " %s%c", it->string, 0);
1997 } else {
1998 struct strbuf onebuf = STRBUF_INIT;
1999 const char *one;
2000 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2001 fprintf(s->fp, " %s\n", one);
2002 strbuf_release(&onebuf);
2006 static void wt_shortstatus_status(struct string_list_item *it,
2007 struct wt_status *s)
2009 struct wt_status_change_data *d = it->util;
2011 if (d->index_status)
2012 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
2013 else
2014 fputc(' ', s->fp);
2015 if (d->worktree_status)
2016 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2017 else
2018 fputc(' ', s->fp);
2019 fputc(' ', s->fp);
2020 if (s->null_termination) {
2021 fprintf(s->fp, "%s%c", it->string, 0);
2022 if (d->rename_source)
2023 fprintf(s->fp, "%s%c", d->rename_source, 0);
2024 } else {
2025 struct strbuf onebuf = STRBUF_INIT;
2026 const char *one;
2028 if (d->rename_source) {
2029 one = quote_path(d->rename_source, s->prefix, &onebuf,
2030 QUOTE_PATH_QUOTE_SP);
2031 fprintf(s->fp, "%s -> ", one);
2032 strbuf_release(&onebuf);
2034 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2035 fprintf(s->fp, "%s\n", one);
2036 strbuf_release(&onebuf);
2040 static void wt_shortstatus_other(struct string_list_item *it,
2041 struct wt_status *s, const char *sign)
2043 if (s->null_termination) {
2044 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2045 } else {
2046 struct strbuf onebuf = STRBUF_INIT;
2047 const char *one;
2048 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2049 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2050 fprintf(s->fp, " %s\n", one);
2051 strbuf_release(&onebuf);
2055 static void wt_shortstatus_print_tracking(struct wt_status *s)
2057 struct branch *branch;
2058 const char *header_color = color(WT_STATUS_HEADER, s);
2059 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2060 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2062 const char *base;
2063 char *short_base;
2064 const char *branch_name;
2065 int num_ours, num_theirs, sti;
2066 int upstream_is_gone = 0;
2068 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2070 if (!s->branch)
2071 return;
2072 branch_name = s->branch;
2074 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2076 if (s->is_initial)
2077 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2079 if (!strcmp(s->branch, "HEAD")) {
2080 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2081 LABEL(N_("HEAD (no branch)")));
2082 goto conclude;
2085 skip_prefix(branch_name, "refs/heads/", &branch_name);
2087 branch = branch_get(branch_name);
2089 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2091 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2092 0, s->ahead_behind_flags);
2093 if (sti < 0) {
2094 if (!base)
2095 goto conclude;
2097 upstream_is_gone = 1;
2100 short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2101 base, 0);
2102 color_fprintf(s->fp, header_color, "...");
2103 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2104 free(short_base);
2106 if (!upstream_is_gone && !sti)
2107 goto conclude;
2109 color_fprintf(s->fp, header_color, " [");
2110 if (upstream_is_gone) {
2111 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2112 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2113 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2114 } else if (!num_ours) {
2115 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2116 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2117 } else if (!num_theirs) {
2118 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2119 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2120 } else {
2121 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2122 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2123 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2124 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2127 color_fprintf(s->fp, header_color, "]");
2128 conclude:
2129 fputc(s->null_termination ? '\0' : '\n', s->fp);
2132 static void wt_shortstatus_print(struct wt_status *s)
2134 struct string_list_item *it;
2136 if (s->show_branch)
2137 wt_shortstatus_print_tracking(s);
2139 for_each_string_list_item(it, &s->change) {
2140 struct wt_status_change_data *d = it->util;
2142 if (d->stagemask)
2143 wt_shortstatus_unmerged(it, s);
2144 else
2145 wt_shortstatus_status(it, s);
2147 for_each_string_list_item(it, &s->untracked)
2148 wt_shortstatus_other(it, s, "??");
2150 for_each_string_list_item(it, &s->ignored)
2151 wt_shortstatus_other(it, s, "!!");
2154 static void wt_porcelain_print(struct wt_status *s)
2156 s->use_color = 0;
2157 s->relative_paths = 0;
2158 s->prefix = NULL;
2159 s->no_gettext = 1;
2160 wt_shortstatus_print(s);
2164 * Print branch information for porcelain v2 output. These lines
2165 * are printed when the '--branch' parameter is given.
2167 * # branch.oid <commit><eol>
2168 * # branch.head <head><eol>
2169 * [# branch.upstream <upstream><eol>
2170 * [# branch.ab +<ahead> -<behind><eol>]]
2172 * <commit> ::= the current commit hash or the literal
2173 * "(initial)" to indicate an initialized repo
2174 * with no commits.
2176 * <head> ::= <branch_name> the current branch name or
2177 * "(detached)" literal when detached head or
2178 * "(unknown)" when something is wrong.
2180 * <upstream> ::= the upstream branch name, when set.
2182 * <ahead> ::= integer ahead value or '?'.
2184 * <behind> ::= integer behind value or '?'.
2186 * The end-of-line is defined by the -z flag.
2188 * <eol> ::= NUL when -z,
2189 * LF when NOT -z.
2191 * When an upstream is set and present, the 'branch.ab' line will
2192 * be printed with the ahead/behind counts for the branch and the
2193 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2194 * are different, '?' will be substituted for the actual count.
2196 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2198 struct branch *branch;
2199 const char *base;
2200 const char *branch_name;
2201 int ab_info, nr_ahead, nr_behind;
2202 char eol = s->null_termination ? '\0' : '\n';
2204 fprintf(s->fp, "# branch.oid %s%c",
2205 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2206 eol);
2208 if (!s->branch)
2209 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2210 else {
2211 if (!strcmp(s->branch, "HEAD")) {
2212 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2214 if (s->state.rebase_in_progress ||
2215 s->state.rebase_interactive_in_progress)
2216 branch_name = s->state.onto;
2217 else if (s->state.detached_from)
2218 branch_name = s->state.detached_from;
2219 else
2220 branch_name = "";
2221 } else {
2222 branch_name = NULL;
2223 skip_prefix(s->branch, "refs/heads/", &branch_name);
2225 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2228 /* Lookup stats on the upstream tracking branch, if set. */
2229 branch = branch_get(branch_name);
2230 base = NULL;
2231 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2232 &base, 0, s->ahead_behind_flags);
2233 if (base) {
2234 base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2235 base, 0);
2236 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2237 free((char *)base);
2239 if (ab_info > 0) {
2240 /* different */
2241 if (nr_ahead || nr_behind)
2242 fprintf(s->fp, "# branch.ab +%d -%d%c",
2243 nr_ahead, nr_behind, eol);
2244 else
2245 fprintf(s->fp, "# branch.ab +? -?%c",
2246 eol);
2247 } else if (!ab_info) {
2248 /* same */
2249 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2256 * Print the stash count in a porcelain-friendly format
2258 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2260 int stash_count = count_stash_entries();
2261 char eol = s->null_termination ? '\0' : '\n';
2263 if (stash_count > 0)
2264 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2268 * Convert various submodule status values into a
2269 * fixed-length string of characters in the buffer provided.
2271 static void wt_porcelain_v2_submodule_state(
2272 struct wt_status_change_data *d,
2273 char sub[5])
2275 if (S_ISGITLINK(d->mode_head) ||
2276 S_ISGITLINK(d->mode_index) ||
2277 S_ISGITLINK(d->mode_worktree)) {
2278 sub[0] = 'S';
2279 sub[1] = d->new_submodule_commits ? 'C' : '.';
2280 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2281 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2282 } else {
2283 sub[0] = 'N';
2284 sub[1] = '.';
2285 sub[2] = '.';
2286 sub[3] = '.';
2288 sub[4] = 0;
2292 * Fix-up changed entries before we print them.
2294 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2296 struct wt_status_change_data *d = it->util;
2298 if (!d->index_status) {
2300 * This entry is unchanged in the index (relative to the head).
2301 * Therefore, the collect_updated_cb was never called for this
2302 * entry (during the head-vs-index scan) and so the head column
2303 * fields were never set.
2305 * We must have data for the index column (from the
2306 * index-vs-worktree scan (otherwise, this entry should not be
2307 * in the list of changes)).
2309 * Copy index column fields to the head column, so that our
2310 * output looks complete.
2312 assert(d->mode_head == 0);
2313 d->mode_head = d->mode_index;
2314 oidcpy(&d->oid_head, &d->oid_index);
2317 if (!d->worktree_status) {
2319 * This entry is unchanged in the worktree (relative to the index).
2320 * Therefore, the collect_changed_cb was never called for this entry
2321 * (during the index-vs-worktree scan) and so the worktree column
2322 * fields were never set.
2324 * We must have data for the index column (from the head-vs-index
2325 * scan).
2327 * Copy the index column fields to the worktree column so that
2328 * our output looks complete.
2330 * Note that we only have a mode field in the worktree column
2331 * because the scan code tries really hard to not have to compute it.
2333 assert(d->mode_worktree == 0);
2334 d->mode_worktree = d->mode_index;
2339 * Print porcelain v2 info for tracked entries with changes.
2341 static void wt_porcelain_v2_print_changed_entry(
2342 struct string_list_item *it,
2343 struct wt_status *s)
2345 struct wt_status_change_data *d = it->util;
2346 struct strbuf buf = STRBUF_INIT;
2347 struct strbuf buf_from = STRBUF_INIT;
2348 const char *path = NULL;
2349 const char *path_from = NULL;
2350 char key[3];
2351 char submodule_token[5];
2352 char sep_char, eol_char;
2354 wt_porcelain_v2_fix_up_changed(it);
2355 wt_porcelain_v2_submodule_state(d, submodule_token);
2357 key[0] = d->index_status ? d->index_status : '.';
2358 key[1] = d->worktree_status ? d->worktree_status : '.';
2359 key[2] = 0;
2361 if (s->null_termination) {
2363 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2364 * A single NUL character separates them.
2366 sep_char = '\0';
2367 eol_char = '\0';
2368 path = it->string;
2369 path_from = d->rename_source;
2370 } else {
2372 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2373 * The source path is only present when necessary.
2374 * A single TAB separates them (because paths can contain spaces
2375 * which are not escaped and C-quoting does escape TAB characters).
2377 sep_char = '\t';
2378 eol_char = '\n';
2379 path = quote_path(it->string, s->prefix, &buf, 0);
2380 if (d->rename_source)
2381 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2384 if (path_from)
2385 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2386 key, submodule_token,
2387 d->mode_head, d->mode_index, d->mode_worktree,
2388 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2389 d->rename_status, d->rename_score,
2390 path, sep_char, path_from, eol_char);
2391 else
2392 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2393 key, submodule_token,
2394 d->mode_head, d->mode_index, d->mode_worktree,
2395 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2396 path, eol_char);
2398 strbuf_release(&buf);
2399 strbuf_release(&buf_from);
2403 * Print porcelain v2 status info for unmerged entries.
2405 static void wt_porcelain_v2_print_unmerged_entry(
2406 struct string_list_item *it,
2407 struct wt_status *s)
2409 struct wt_status_change_data *d = it->util;
2410 struct index_state *istate = s->repo->index;
2411 const struct cache_entry *ce;
2412 struct strbuf buf_index = STRBUF_INIT;
2413 const char *path_index = NULL;
2414 int pos, stage, sum;
2415 struct {
2416 int mode;
2417 struct object_id oid;
2418 } stages[3];
2419 const char *key;
2420 char submodule_token[5];
2421 char unmerged_prefix = 'u';
2422 char eol_char = s->null_termination ? '\0' : '\n';
2424 wt_porcelain_v2_submodule_state(d, submodule_token);
2426 switch (d->stagemask) {
2427 case 1: key = "DD"; break; /* both deleted */
2428 case 2: key = "AU"; break; /* added by us */
2429 case 3: key = "UD"; break; /* deleted by them */
2430 case 4: key = "UA"; break; /* added by them */
2431 case 5: key = "DU"; break; /* deleted by us */
2432 case 6: key = "AA"; break; /* both added */
2433 case 7: key = "UU"; break; /* both modified */
2434 default:
2435 BUG("unhandled unmerged status %x", d->stagemask);
2439 * Disregard d.aux.porcelain_v2 data that we accumulated
2440 * for the head and index columns during the scans and
2441 * replace with the actual stage data.
2443 * Note that this is a last-one-wins for each the individual
2444 * stage [123] columns in the event of multiple cache entries
2445 * for same stage.
2447 memset(stages, 0, sizeof(stages));
2448 sum = 0;
2449 pos = index_name_pos(istate, it->string, strlen(it->string));
2450 assert(pos < 0);
2451 pos = -pos-1;
2452 while (pos < istate->cache_nr) {
2453 ce = istate->cache[pos++];
2454 stage = ce_stage(ce);
2455 if (strcmp(ce->name, it->string) || !stage)
2456 break;
2457 stages[stage - 1].mode = ce->ce_mode;
2458 oidcpy(&stages[stage - 1].oid, &ce->oid);
2459 sum |= (1 << (stage - 1));
2461 if (sum != d->stagemask)
2462 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2464 if (s->null_termination)
2465 path_index = it->string;
2466 else
2467 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2469 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2470 unmerged_prefix, key, submodule_token,
2471 stages[0].mode, /* stage 1 */
2472 stages[1].mode, /* stage 2 */
2473 stages[2].mode, /* stage 3 */
2474 d->mode_worktree,
2475 oid_to_hex(&stages[0].oid), /* stage 1 */
2476 oid_to_hex(&stages[1].oid), /* stage 2 */
2477 oid_to_hex(&stages[2].oid), /* stage 3 */
2478 path_index,
2479 eol_char);
2481 strbuf_release(&buf_index);
2485 * Print porcelain V2 status info for untracked and ignored entries.
2487 static void wt_porcelain_v2_print_other(
2488 struct string_list_item *it,
2489 struct wt_status *s,
2490 char prefix)
2492 struct strbuf buf = STRBUF_INIT;
2493 const char *path;
2494 char eol_char;
2496 if (s->null_termination) {
2497 path = it->string;
2498 eol_char = '\0';
2499 } else {
2500 path = quote_path(it->string, s->prefix, &buf, 0);
2501 eol_char = '\n';
2504 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2506 strbuf_release(&buf);
2510 * Print porcelain V2 status.
2512 * [<v2_branch>]
2513 * [<v2_changed_items>]*
2514 * [<v2_unmerged_items>]*
2515 * [<v2_untracked_items>]*
2516 * [<v2_ignored_items>]*
2519 static void wt_porcelain_v2_print(struct wt_status *s)
2521 struct wt_status_change_data *d;
2522 struct string_list_item *it;
2523 int i;
2525 if (s->show_branch)
2526 wt_porcelain_v2_print_tracking(s);
2528 if (s->show_stash)
2529 wt_porcelain_v2_print_stash(s);
2531 for (i = 0; i < s->change.nr; i++) {
2532 it = &(s->change.items[i]);
2533 d = it->util;
2534 if (!d->stagemask)
2535 wt_porcelain_v2_print_changed_entry(it, s);
2538 for (i = 0; i < s->change.nr; i++) {
2539 it = &(s->change.items[i]);
2540 d = it->util;
2541 if (d->stagemask)
2542 wt_porcelain_v2_print_unmerged_entry(it, s);
2545 for (i = 0; i < s->untracked.nr; i++) {
2546 it = &(s->untracked.items[i]);
2547 wt_porcelain_v2_print_other(it, s, '?');
2550 for (i = 0; i < s->ignored.nr; i++) {
2551 it = &(s->ignored.items[i]);
2552 wt_porcelain_v2_print_other(it, s, '!');
2556 void wt_status_print(struct wt_status *s)
2558 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2559 trace2_data_intmax("status", s->repo, "count/untracked",
2560 s->untracked.nr);
2561 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2563 trace2_region_enter("status", "print", s->repo);
2565 switch (s->status_format) {
2566 case STATUS_FORMAT_SHORT:
2567 wt_shortstatus_print(s);
2568 break;
2569 case STATUS_FORMAT_PORCELAIN:
2570 wt_porcelain_print(s);
2571 break;
2572 case STATUS_FORMAT_PORCELAIN_V2:
2573 wt_porcelain_v2_print(s);
2574 break;
2575 case STATUS_FORMAT_UNSPECIFIED:
2576 BUG("finalize_deferred_config() should have been called");
2577 break;
2578 case STATUS_FORMAT_NONE:
2579 case STATUS_FORMAT_LONG:
2580 wt_longstatus_print(s);
2581 break;
2584 trace2_region_leave("status", "print", s->repo);
2588 * Returns 1 if there are unstaged changes, 0 otherwise.
2590 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2592 struct rev_info rev_info;
2593 int result;
2595 repo_init_revisions(r, &rev_info, NULL);
2596 if (ignore_submodules) {
2597 rev_info.diffopt.flags.ignore_submodules = 1;
2598 rev_info.diffopt.flags.override_submodule_config = 1;
2600 rev_info.diffopt.flags.quick = 1;
2601 diff_setup_done(&rev_info.diffopt);
2602 run_diff_files(&rev_info, 0);
2603 result = diff_result_code(&rev_info);
2604 release_revisions(&rev_info);
2605 return result;
2609 * Returns 1 if there are uncommitted changes, 0 otherwise.
2611 int has_uncommitted_changes(struct repository *r,
2612 int ignore_submodules)
2614 struct rev_info rev_info;
2615 int result;
2617 if (is_index_unborn(r->index))
2618 return 0;
2620 repo_init_revisions(r, &rev_info, NULL);
2621 if (ignore_submodules)
2622 rev_info.diffopt.flags.ignore_submodules = 1;
2623 rev_info.diffopt.flags.quick = 1;
2625 add_head_to_pending(&rev_info);
2626 if (!rev_info.pending.nr) {
2628 * We have no head (or it's corrupt); use the empty tree,
2629 * which will complain if the index is non-empty.
2631 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2632 add_pending_object(&rev_info, &tree->object, "");
2635 diff_setup_done(&rev_info.diffopt);
2636 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2637 result = diff_result_code(&rev_info);
2638 release_revisions(&rev_info);
2639 return result;
2643 * If the work tree has unstaged or uncommitted changes, dies with the
2644 * appropriate message.
2646 int require_clean_work_tree(struct repository *r,
2647 const char *action,
2648 const char *hint,
2649 int ignore_submodules,
2650 int gently)
2652 struct lock_file lock_file = LOCK_INIT;
2653 int err = 0, fd;
2655 fd = repo_hold_locked_index(r, &lock_file, 0);
2656 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2657 if (0 <= fd)
2658 repo_update_index_if_able(r, &lock_file);
2659 rollback_lock_file(&lock_file);
2661 if (has_unstaged_changes(r, ignore_submodules)) {
2662 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2663 error(_("cannot %s: You have unstaged changes."), _(action));
2664 err = 1;
2667 if (has_uncommitted_changes(r, ignore_submodules)) {
2668 if (err)
2669 error(_("additionally, your index contains uncommitted changes."));
2670 else
2671 error(_("cannot %s: Your index contains uncommitted changes."),
2672 _(action));
2673 err = 1;
2676 if (err) {
2677 if (hint) {
2678 if (!*hint)
2679 BUG("empty hint passed to require_clean_work_tree();"
2680 " use NULL instead");
2681 error("%s", hint);
2683 if (!gently)
2684 exit(128);
2687 return err;