Merge branch 'ds/line-log-asan-fix'
[git/gitster.git] / wt-status.c
blob6a6397ca8f12ba7d6b11eb8e38a28f19d2767b8b
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 int i;
722 for (i = 0; i < istate->cache_nr; i++) {
723 struct string_list_item *it;
724 struct wt_status_change_data *d;
725 const struct cache_entry *ce = istate->cache[i];
727 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
728 continue;
729 if (ce_intent_to_add(ce))
730 continue;
731 if (S_ISSPARSEDIR(ce->ce_mode)) {
733 * This is a sparse directory entry, so we want to collect all
734 * of the added files within the tree. This requires recursively
735 * expanding the trees to find the elements that are new in this
736 * tree and marking them with DIFF_STATUS_ADDED.
738 struct strbuf base = STRBUF_INIT;
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_add(&base, ce->name, ce->ce_namelen);
747 read_tree_at(istate->repo, tree, &base, 0, &ps,
748 add_file_to_list, s);
749 continue;
752 it = string_list_insert(&s->change, ce->name);
753 d = it->util;
754 if (!d) {
755 CALLOC_ARRAY(d, 1);
756 it->util = d;
758 if (ce_stage(ce)) {
759 d->index_status = DIFF_STATUS_UNMERGED;
760 d->stagemask |= (1 << (ce_stage(ce) - 1));
762 * Don't bother setting {mode,oid}_{head,index} since the print
763 * code will output the stage values directly and not use the
764 * values in these fields.
766 s->committable = 1;
767 } else {
768 d->index_status = DIFF_STATUS_ADDED;
769 /* Leave {mode,oid}_head zero for adds. */
770 d->mode_index = ce->ce_mode;
771 oidcpy(&d->oid_index, &ce->oid);
772 s->committable = 1;
777 static void wt_status_collect_untracked(struct wt_status *s)
779 int i;
780 struct dir_struct dir = DIR_INIT;
781 uint64_t t_begin = getnanotime();
782 struct index_state *istate = s->repo->index;
784 if (!s->show_untracked_files)
785 return;
787 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
788 dir.flags |=
789 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
790 if (s->show_ignored_mode) {
791 dir.flags |= DIR_SHOW_IGNORED_TOO;
793 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
794 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
795 } else {
796 dir.untracked = istate->untracked;
799 setup_standard_excludes(&dir);
801 fill_directory(&dir, istate, &s->pathspec);
803 for (i = 0; i < dir.nr; i++) {
804 struct dir_entry *ent = dir.entries[i];
805 if (index_name_is_other(istate, ent->name, ent->len))
806 string_list_insert(&s->untracked, ent->name);
809 for (i = 0; i < dir.ignored_nr; i++) {
810 struct dir_entry *ent = dir.ignored[i];
811 if (index_name_is_other(istate, ent->name, ent->len))
812 string_list_insert(&s->ignored, ent->name);
815 dir_clear(&dir);
817 if (advice_enabled(ADVICE_STATUS_U_OPTION))
818 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
821 static int has_unmerged(struct wt_status *s)
823 int i;
825 for (i = 0; i < s->change.nr; i++) {
826 struct wt_status_change_data *d;
827 d = s->change.items[i].util;
828 if (d->stagemask)
829 return 1;
831 return 0;
834 void wt_status_collect(struct wt_status *s)
836 trace2_region_enter("status", "worktrees", s->repo);
837 wt_status_collect_changes_worktree(s);
838 trace2_region_leave("status", "worktrees", s->repo);
840 if (s->is_initial) {
841 trace2_region_enter("status", "initial", s->repo);
842 wt_status_collect_changes_initial(s);
843 trace2_region_leave("status", "initial", s->repo);
844 } else {
845 trace2_region_enter("status", "index", s->repo);
846 wt_status_collect_changes_index(s);
847 trace2_region_leave("status", "index", s->repo);
850 trace2_region_enter("status", "untracked", s->repo);
851 wt_status_collect_untracked(s);
852 trace2_region_leave("status", "untracked", s->repo);
854 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
855 if (s->state.merge_in_progress && !has_unmerged(s))
856 s->committable = 1;
859 void wt_status_collect_free_buffers(struct wt_status *s)
861 wt_status_state_free_buffers(&s->state);
864 void wt_status_state_free_buffers(struct wt_status_state *state)
866 FREE_AND_NULL(state->branch);
867 FREE_AND_NULL(state->onto);
868 FREE_AND_NULL(state->detached_from);
869 FREE_AND_NULL(state->bisecting_from);
872 static void wt_longstatus_print_unmerged(struct wt_status *s)
874 int shown_header = 0;
875 int i;
877 for (i = 0; i < s->change.nr; i++) {
878 struct wt_status_change_data *d;
879 struct string_list_item *it;
880 it = &(s->change.items[i]);
881 d = it->util;
882 if (!d->stagemask)
883 continue;
884 if (!shown_header) {
885 wt_longstatus_print_unmerged_header(s);
886 shown_header = 1;
888 wt_longstatus_print_unmerged_data(s, it);
890 if (shown_header)
891 wt_longstatus_print_trailer(s);
895 static void wt_longstatus_print_updated(struct wt_status *s)
897 int shown_header = 0;
898 int i;
900 for (i = 0; i < s->change.nr; i++) {
901 struct wt_status_change_data *d;
902 struct string_list_item *it;
903 it = &(s->change.items[i]);
904 d = it->util;
905 if (!d->index_status ||
906 d->index_status == DIFF_STATUS_UNMERGED)
907 continue;
908 if (!shown_header) {
909 wt_longstatus_print_cached_header(s);
910 shown_header = 1;
912 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
914 if (shown_header)
915 wt_longstatus_print_trailer(s);
919 * -1 : has delete
920 * 0 : no change
921 * 1 : some change but no delete
923 static int wt_status_check_worktree_changes(struct wt_status *s,
924 int *dirty_submodules)
926 int i;
927 int changes = 0;
929 *dirty_submodules = 0;
931 for (i = 0; i < s->change.nr; i++) {
932 struct wt_status_change_data *d;
933 d = s->change.items[i].util;
934 if (!d->worktree_status ||
935 d->worktree_status == DIFF_STATUS_UNMERGED)
936 continue;
937 if (!changes)
938 changes = 1;
939 if (d->dirty_submodule)
940 *dirty_submodules = 1;
941 if (d->worktree_status == DIFF_STATUS_DELETED)
942 changes = -1;
944 return changes;
947 static void wt_longstatus_print_changed(struct wt_status *s)
949 int i, dirty_submodules;
950 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
952 if (!worktree_changes)
953 return;
955 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
957 for (i = 0; i < s->change.nr; i++) {
958 struct wt_status_change_data *d;
959 struct string_list_item *it;
960 it = &(s->change.items[i]);
961 d = it->util;
962 if (!d->worktree_status ||
963 d->worktree_status == DIFF_STATUS_UNMERGED)
964 continue;
965 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
967 wt_longstatus_print_trailer(s);
970 static int stash_count_refs(struct object_id *ooid UNUSED,
971 struct object_id *noid UNUSED,
972 const char *email UNUSED,
973 timestamp_t timestamp UNUSED, int tz UNUSED,
974 const char *message UNUSED, void *cb_data)
976 int *c = cb_data;
977 (*c)++;
978 return 0;
981 static int count_stash_entries(void)
983 int n = 0;
984 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
985 "refs/stash", stash_count_refs, &n);
986 return n;
989 static void wt_longstatus_print_stash_summary(struct wt_status *s)
991 int stash_count = count_stash_entries();
993 if (stash_count > 0)
994 status_printf_ln(s, GIT_COLOR_NORMAL,
995 Q_("Your stash currently has %d entry",
996 "Your stash currently has %d entries", stash_count),
997 stash_count);
1000 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
1002 struct child_process sm_summary = CHILD_PROCESS_INIT;
1003 struct strbuf cmd_stdout = STRBUF_INIT;
1004 struct strbuf summary = STRBUF_INIT;
1005 char *summary_content;
1007 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1009 strvec_push(&sm_summary.args, "submodule");
1010 strvec_push(&sm_summary.args, "summary");
1011 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1012 strvec_push(&sm_summary.args, "--for-status");
1013 strvec_push(&sm_summary.args, "--summary-limit");
1014 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1015 if (!uncommitted)
1016 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1018 sm_summary.git_cmd = 1;
1019 sm_summary.no_stdin = 1;
1021 capture_command(&sm_summary, &cmd_stdout, 1024);
1023 /* prepend header, only if there's an actual output */
1024 if (cmd_stdout.len) {
1025 if (uncommitted)
1026 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1027 else
1028 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1029 strbuf_addstr(&summary, "\n\n");
1031 strbuf_addbuf(&summary, &cmd_stdout);
1032 strbuf_release(&cmd_stdout);
1034 if (s->display_comment_prefix) {
1035 size_t len;
1036 summary_content = strbuf_detach(&summary, &len);
1037 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1038 free(summary_content);
1041 fputs(summary.buf, s->fp);
1042 strbuf_release(&summary);
1045 static void wt_longstatus_print_other(struct wt_status *s,
1046 struct string_list *l,
1047 const char *what,
1048 const char *how)
1050 int i;
1051 struct strbuf buf = STRBUF_INIT;
1052 static struct string_list output = STRING_LIST_INIT_DUP;
1053 struct column_options copts;
1055 if (!l->nr)
1056 return;
1058 wt_longstatus_print_other_header(s, what, how);
1060 for (i = 0; i < l->nr; i++) {
1061 struct string_list_item *it;
1062 const char *path;
1063 it = &(l->items[i]);
1064 path = quote_path(it->string, s->prefix, &buf, 0);
1065 if (column_active(s->colopts)) {
1066 string_list_append(&output, path);
1067 continue;
1069 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1070 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1071 "%s\n", path);
1074 strbuf_release(&buf);
1075 if (!column_active(s->colopts))
1076 goto conclude;
1078 strbuf_addf(&buf, "%s%s\t%s",
1079 color(WT_STATUS_HEADER, s),
1080 s->display_comment_prefix ? "#" : "",
1081 color(WT_STATUS_UNTRACKED, s));
1082 memset(&copts, 0, sizeof(copts));
1083 copts.padding = 1;
1084 copts.indent = buf.buf;
1085 if (want_color(s->use_color))
1086 copts.nl = GIT_COLOR_RESET "\n";
1087 print_columns(&output, s->colopts, &copts);
1088 string_list_clear(&output, 0);
1089 strbuf_release(&buf);
1090 conclude:
1091 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1094 size_t wt_status_locate_end(const char *s, size_t len)
1096 const char *p;
1097 struct strbuf pattern = STRBUF_INIT;
1099 strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line);
1100 if (starts_with(s, pattern.buf + 1))
1101 len = 0;
1102 else if ((p = strstr(s, pattern.buf))) {
1103 size_t newlen = p - s + 1;
1104 if (newlen < len)
1105 len = newlen;
1107 strbuf_release(&pattern);
1108 return len;
1111 void wt_status_append_cut_line(struct strbuf *buf)
1113 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1115 strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1116 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1119 void wt_status_add_cut_line(struct wt_status *s)
1121 struct strbuf buf = STRBUF_INIT;
1123 if (s->added_cut_line)
1124 return;
1125 s->added_cut_line = 1;
1126 wt_status_append_cut_line(&buf);
1127 fputs(buf.buf, s->fp);
1128 strbuf_release(&buf);
1131 static void wt_longstatus_print_verbose(struct wt_status *s)
1133 struct rev_info rev;
1134 struct setup_revision_opt opt;
1135 int dirty_submodules;
1136 const char *c = color(WT_STATUS_HEADER, s);
1138 repo_init_revisions(s->repo, &rev, NULL);
1139 rev.diffopt.flags.allow_textconv = 1;
1140 rev.diffopt.ita_invisible_in_index = 1;
1142 memset(&opt, 0, sizeof(opt));
1143 opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
1144 setup_revisions(0, NULL, &rev, &opt);
1146 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1147 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1148 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1149 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1150 rev.diffopt.file = s->fp;
1151 rev.diffopt.close_file = 0;
1153 * If we're not going to stdout, then we definitely don't
1154 * want color, since we are going to the commit message
1155 * file (and even the "auto" setting won't work, since it
1156 * will have checked isatty on stdout). But we then do want
1157 * to insert the scissor line here to reliably remove the
1158 * diff before committing, if we didn't already include one
1159 * before.
1161 if (s->fp != stdout) {
1162 rev.diffopt.use_color = 0;
1163 wt_status_add_cut_line(s);
1165 if (s->verbose > 1 && s->committable) {
1166 /* print_updated() printed a header, so do we */
1167 if (s->fp != stdout)
1168 wt_longstatus_print_trailer(s);
1169 status_printf_ln(s, c, _("Changes to be committed:"));
1170 rev.diffopt.a_prefix = "c/";
1171 rev.diffopt.b_prefix = "i/";
1172 } /* else use prefix as per user config */
1173 run_diff_index(&rev, DIFF_INDEX_CACHED);
1174 if (s->verbose > 1 &&
1175 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1176 status_printf_ln(s, c,
1177 "--------------------------------------------------");
1178 status_printf_ln(s, c, _("Changes not staged for commit:"));
1179 setup_work_tree();
1180 rev.diffopt.a_prefix = "i/";
1181 rev.diffopt.b_prefix = "w/";
1182 run_diff_files(&rev, 0);
1184 release_revisions(&rev);
1187 static void wt_longstatus_print_tracking(struct wt_status *s)
1189 struct strbuf sb = STRBUF_INIT;
1190 const char *cp, *ep, *branch_name;
1191 struct branch *branch;
1192 uint64_t t_begin = 0;
1194 assert(s->branch && !s->is_initial);
1195 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1196 return;
1197 branch = branch_get(branch_name);
1199 t_begin = getnanotime();
1201 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1202 !s->commit_template))
1203 return;
1205 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1206 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1207 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1208 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1209 strbuf_addf(&sb, _("\n"
1210 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1211 "You can use '--no-ahead-behind' to avoid this.\n"),
1212 t_delta_in_ms / 1000.0);
1216 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1217 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1218 "%s%s%.*s",
1219 s->display_comment_prefix ? comment_line_str : "",
1220 s->display_comment_prefix ? " " : "",
1221 (int)(ep - cp), cp);
1222 if (s->display_comment_prefix)
1223 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
1224 comment_line_str);
1225 else
1226 fputs("\n", s->fp);
1227 strbuf_release(&sb);
1230 static int uf_was_slow(struct wt_status *s)
1232 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1233 s->untracked_in_ms = 3250;
1234 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1237 static void show_merge_in_progress(struct wt_status *s,
1238 const char *color)
1240 if (has_unmerged(s)) {
1241 status_printf_ln(s, color, _("You have unmerged paths."));
1242 if (s->hints) {
1243 status_printf_ln(s, color,
1244 _(" (fix conflicts and run \"git commit\")"));
1245 status_printf_ln(s, color,
1246 _(" (use \"git merge --abort\" to abort the merge)"));
1248 } else {
1249 status_printf_ln(s, color,
1250 _("All conflicts fixed but you are still merging."));
1251 if (s->hints)
1252 status_printf_ln(s, color,
1253 _(" (use \"git commit\" to conclude merge)"));
1255 wt_longstatus_print_trailer(s);
1258 static void show_am_in_progress(struct wt_status *s,
1259 const char *color)
1261 int am_empty_patch;
1263 status_printf_ln(s, color,
1264 _("You are in the middle of an am session."));
1265 if (s->state.am_empty_patch)
1266 status_printf_ln(s, color,
1267 _("The current patch is empty."));
1268 if (s->hints) {
1269 am_empty_patch = s->state.am_empty_patch;
1270 if (!am_empty_patch)
1271 status_printf_ln(s, color,
1272 _(" (fix conflicts and then run \"git am --continue\")"));
1273 status_printf_ln(s, color,
1274 _(" (use \"git am --skip\" to skip this patch)"));
1275 if (am_empty_patch)
1276 status_printf_ln(s, color,
1277 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1278 status_printf_ln(s, color,
1279 _(" (use \"git am --abort\" to restore the original branch)"));
1281 wt_longstatus_print_trailer(s);
1284 static char *read_line_from_git_path(const char *filename)
1286 struct strbuf buf = STRBUF_INIT;
1287 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1289 if (!fp) {
1290 strbuf_release(&buf);
1291 return NULL;
1293 strbuf_getline_lf(&buf, fp);
1294 if (!fclose(fp)) {
1295 return strbuf_detach(&buf, NULL);
1296 } else {
1297 strbuf_release(&buf);
1298 return NULL;
1302 static int split_commit_in_progress(struct wt_status *s)
1304 int split_in_progress = 0;
1305 struct object_id head_oid, orig_head_oid;
1306 char *rebase_amend, *rebase_orig_head;
1307 int head_flags, orig_head_flags;
1309 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1310 !s->branch || strcmp(s->branch, "HEAD"))
1311 return 0;
1313 if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1314 &head_oid, &head_flags) ||
1315 refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1316 &orig_head_oid, &orig_head_flags))
1317 return 0;
1318 if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1319 return 0;
1321 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1322 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1324 if (!rebase_amend || !rebase_orig_head)
1325 ; /* fall through, no split in progress */
1326 else if (!strcmp(rebase_amend, rebase_orig_head))
1327 split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1328 else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
1329 split_in_progress = 1;
1331 free(rebase_amend);
1332 free(rebase_orig_head);
1334 return split_in_progress;
1338 * Turn
1339 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1340 * into
1341 * "pick d6a2f03 some message"
1343 * The function assumes that the line does not contain useless spaces
1344 * before or after the command.
1346 static void abbrev_oid_in_line(struct strbuf *line)
1348 struct strbuf **split;
1349 int i;
1351 if (starts_with(line->buf, "exec ") ||
1352 starts_with(line->buf, "x ") ||
1353 starts_with(line->buf, "label ") ||
1354 starts_with(line->buf, "l "))
1355 return;
1357 split = strbuf_split_max(line, ' ', 3);
1358 if (split[0] && split[1]) {
1359 struct object_id oid;
1362 * strbuf_split_max left a space. Trim it and re-add
1363 * it after abbreviation.
1365 strbuf_trim(split[1]);
1366 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1367 strbuf_reset(split[1]);
1368 strbuf_add_unique_abbrev(split[1], &oid,
1369 DEFAULT_ABBREV);
1370 strbuf_addch(split[1], ' ');
1371 strbuf_reset(line);
1372 for (i = 0; split[i]; i++)
1373 strbuf_addbuf(line, split[i]);
1376 strbuf_list_free(split);
1379 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1381 struct strbuf line = STRBUF_INIT;
1382 FILE *f = fopen(git_path("%s", fname), "r");
1384 if (!f) {
1385 if (errno == ENOENT)
1386 return -1;
1387 die_errno("Could not open file %s for reading",
1388 git_path("%s", fname));
1390 while (!strbuf_getline_lf(&line, f)) {
1391 if (starts_with(line.buf, comment_line_str))
1392 continue;
1393 strbuf_trim(&line);
1394 if (!line.len)
1395 continue;
1396 abbrev_oid_in_line(&line);
1397 string_list_append(lines, line.buf);
1399 fclose(f);
1400 strbuf_release(&line);
1401 return 0;
1404 static void show_rebase_information(struct wt_status *s,
1405 const char *color)
1407 if (s->state.rebase_interactive_in_progress) {
1408 int i;
1409 int nr_lines_to_show = 2;
1411 struct string_list have_done = STRING_LIST_INIT_DUP;
1412 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1414 read_rebase_todolist("rebase-merge/done", &have_done);
1415 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1416 &yet_to_do))
1417 status_printf_ln(s, color,
1418 _("git-rebase-todo is missing."));
1419 if (have_done.nr == 0)
1420 status_printf_ln(s, color, _("No commands done."));
1421 else {
1422 status_printf_ln(s, color,
1423 Q_("Last command done (%"PRIuMAX" command done):",
1424 "Last commands done (%"PRIuMAX" commands done):",
1425 have_done.nr),
1426 (uintmax_t)have_done.nr);
1427 for (i = (have_done.nr > nr_lines_to_show)
1428 ? have_done.nr - nr_lines_to_show : 0;
1429 i < have_done.nr;
1430 i++)
1431 status_printf_ln(s, color, " %s", have_done.items[i].string);
1432 if (have_done.nr > nr_lines_to_show && s->hints)
1433 status_printf_ln(s, color,
1434 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1437 if (yet_to_do.nr == 0)
1438 status_printf_ln(s, color,
1439 _("No commands remaining."));
1440 else {
1441 status_printf_ln(s, color,
1442 Q_("Next command to do (%"PRIuMAX" remaining command):",
1443 "Next commands to do (%"PRIuMAX" remaining commands):",
1444 yet_to_do.nr),
1445 (uintmax_t)yet_to_do.nr);
1446 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1447 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1448 if (s->hints)
1449 status_printf_ln(s, color,
1450 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1452 string_list_clear(&yet_to_do, 0);
1453 string_list_clear(&have_done, 0);
1457 static void print_rebase_state(struct wt_status *s,
1458 const char *color)
1460 if (s->state.branch)
1461 status_printf_ln(s, color,
1462 _("You are currently rebasing branch '%s' on '%s'."),
1463 s->state.branch,
1464 s->state.onto);
1465 else
1466 status_printf_ln(s, color,
1467 _("You are currently rebasing."));
1470 static void show_rebase_in_progress(struct wt_status *s,
1471 const char *color)
1473 struct stat st;
1475 show_rebase_information(s, color);
1476 if (has_unmerged(s)) {
1477 print_rebase_state(s, color);
1478 if (s->hints) {
1479 status_printf_ln(s, color,
1480 _(" (fix conflicts and then run \"git rebase --continue\")"));
1481 status_printf_ln(s, color,
1482 _(" (use \"git rebase --skip\" to skip this patch)"));
1483 status_printf_ln(s, color,
1484 _(" (use \"git rebase --abort\" to check out the original branch)"));
1486 } else if (s->state.rebase_in_progress ||
1487 !stat(git_path_merge_msg(s->repo), &st)) {
1488 print_rebase_state(s, color);
1489 if (s->hints)
1490 status_printf_ln(s, color,
1491 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1492 } else if (split_commit_in_progress(s)) {
1493 if (s->state.branch)
1494 status_printf_ln(s, color,
1495 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1496 s->state.branch,
1497 s->state.onto);
1498 else
1499 status_printf_ln(s, color,
1500 _("You are currently splitting a commit during a rebase."));
1501 if (s->hints)
1502 status_printf_ln(s, color,
1503 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1504 } else {
1505 if (s->state.branch)
1506 status_printf_ln(s, color,
1507 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1508 s->state.branch,
1509 s->state.onto);
1510 else
1511 status_printf_ln(s, color,
1512 _("You are currently editing a commit during a rebase."));
1513 if (s->hints && !s->amend) {
1514 status_printf_ln(s, color,
1515 _(" (use \"git commit --amend\" to amend the current commit)"));
1516 status_printf_ln(s, color,
1517 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1520 wt_longstatus_print_trailer(s);
1523 static void show_cherry_pick_in_progress(struct wt_status *s,
1524 const char *color)
1526 if (is_null_oid(&s->state.cherry_pick_head_oid))
1527 status_printf_ln(s, color,
1528 _("Cherry-pick currently in progress."));
1529 else
1530 status_printf_ln(s, color,
1531 _("You are currently cherry-picking commit %s."),
1532 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1533 DEFAULT_ABBREV));
1535 if (s->hints) {
1536 if (has_unmerged(s))
1537 status_printf_ln(s, color,
1538 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1539 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1540 status_printf_ln(s, color,
1541 _(" (run \"git cherry-pick --continue\" to continue)"));
1542 else
1543 status_printf_ln(s, color,
1544 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1545 status_printf_ln(s, color,
1546 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1547 status_printf_ln(s, color,
1548 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1550 wt_longstatus_print_trailer(s);
1553 static void show_revert_in_progress(struct wt_status *s,
1554 const char *color)
1556 if (is_null_oid(&s->state.revert_head_oid))
1557 status_printf_ln(s, color,
1558 _("Revert currently in progress."));
1559 else
1560 status_printf_ln(s, color,
1561 _("You are currently reverting commit %s."),
1562 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1563 DEFAULT_ABBREV));
1564 if (s->hints) {
1565 if (has_unmerged(s))
1566 status_printf_ln(s, color,
1567 _(" (fix conflicts and run \"git revert --continue\")"));
1568 else if (is_null_oid(&s->state.revert_head_oid))
1569 status_printf_ln(s, color,
1570 _(" (run \"git revert --continue\" to continue)"));
1571 else
1572 status_printf_ln(s, color,
1573 _(" (all conflicts fixed: run \"git revert --continue\")"));
1574 status_printf_ln(s, color,
1575 _(" (use \"git revert --skip\" to skip this patch)"));
1576 status_printf_ln(s, color,
1577 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1579 wt_longstatus_print_trailer(s);
1582 static void show_bisect_in_progress(struct wt_status *s,
1583 const char *color)
1585 if (s->state.bisecting_from)
1586 status_printf_ln(s, color,
1587 _("You are currently bisecting, started from branch '%s'."),
1588 s->state.bisecting_from);
1589 else
1590 status_printf_ln(s, color,
1591 _("You are currently bisecting."));
1592 if (s->hints)
1593 status_printf_ln(s, color,
1594 _(" (use \"git bisect reset\" to get back to the original branch)"));
1595 wt_longstatus_print_trailer(s);
1598 static void show_sparse_checkout_in_use(struct wt_status *s,
1599 const char *color)
1601 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1602 return;
1604 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1605 status_printf_ln(s, color, _("You are in a sparse checkout."));
1606 else
1607 status_printf_ln(s, color,
1608 _("You are in a sparse checkout with %d%% of tracked files present."),
1609 s->state.sparse_checkout_percentage);
1610 wt_longstatus_print_trailer(s);
1614 * Extract branch information from rebase/bisect
1616 static char *get_branch(const struct worktree *wt, const char *path)
1618 struct strbuf sb = STRBUF_INIT;
1619 struct object_id oid;
1620 const char *branch_name;
1622 if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1623 goto got_nothing;
1625 while (sb.len && sb.buf[sb.len - 1] == '\n')
1626 strbuf_setlen(&sb, sb.len - 1);
1627 if (!sb.len)
1628 goto got_nothing;
1629 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1630 strbuf_remove(&sb, 0, branch_name - sb.buf);
1631 else if (starts_with(sb.buf, "refs/"))
1633 else if (!get_oid_hex(sb.buf, &oid)) {
1634 strbuf_reset(&sb);
1635 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1636 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1637 goto got_nothing;
1638 else /* bisect */
1640 return strbuf_detach(&sb, NULL);
1642 got_nothing:
1643 strbuf_release(&sb);
1644 return NULL;
1647 struct grab_1st_switch_cbdata {
1648 struct strbuf buf;
1649 struct object_id noid;
1652 static int grab_1st_switch(struct object_id *ooid UNUSED,
1653 struct object_id *noid,
1654 const char *email UNUSED,
1655 timestamp_t timestamp UNUSED, int tz UNUSED,
1656 const char *message, void *cb_data)
1658 struct grab_1st_switch_cbdata *cb = cb_data;
1659 const char *target = NULL, *end;
1661 if (!skip_prefix(message, "checkout: moving from ", &message))
1662 return 0;
1663 target = strstr(message, " to ");
1664 if (!target)
1665 return 0;
1666 target += strlen(" to ");
1667 strbuf_reset(&cb->buf);
1668 oidcpy(&cb->noid, noid);
1669 end = strchrnul(target, '\n');
1670 strbuf_add(&cb->buf, target, end - target);
1671 if (!strcmp(cb->buf.buf, "HEAD")) {
1672 /* HEAD is relative. Resolve it to the right reflog entry. */
1673 strbuf_reset(&cb->buf);
1674 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1676 return 1;
1679 static void wt_status_get_detached_from(struct repository *r,
1680 struct wt_status_state *state)
1682 struct grab_1st_switch_cbdata cb;
1683 struct commit *commit;
1684 struct object_id oid;
1685 char *ref = NULL;
1687 strbuf_init(&cb.buf, 0);
1688 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) {
1689 strbuf_release(&cb.buf);
1690 return;
1693 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
1694 1) == 1 &&
1695 /* oid is a commit? match without further lookup */
1696 (oideq(&cb.noid, &oid) ||
1697 /* perhaps oid is a tag, try to dereference to a commit */
1698 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1699 oideq(&cb.noid, &commit->object.oid)))) {
1700 const char *from = ref;
1701 if (!skip_prefix(from, "refs/tags/", &from))
1702 skip_prefix(from, "refs/remotes/", &from);
1703 state->detached_from = xstrdup(from);
1704 } else
1705 state->detached_from =
1706 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1707 oidcpy(&state->detached_oid, &cb.noid);
1708 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1709 oideq(&oid, &state->detached_oid);
1711 free(ref);
1712 strbuf_release(&cb.buf);
1715 int wt_status_check_rebase(const struct worktree *wt,
1716 struct wt_status_state *state)
1718 struct stat st;
1720 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1721 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1722 state->am_in_progress = 1;
1723 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1724 state->am_empty_patch = 1;
1725 } else {
1726 state->rebase_in_progress = 1;
1727 state->branch = get_branch(wt, "rebase-apply/head-name");
1728 state->onto = get_branch(wt, "rebase-apply/onto");
1730 } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1731 if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1732 state->rebase_interactive_in_progress = 1;
1733 else
1734 state->rebase_in_progress = 1;
1735 state->branch = get_branch(wt, "rebase-merge/head-name");
1736 state->onto = get_branch(wt, "rebase-merge/onto");
1737 } else
1738 return 0;
1739 return 1;
1742 int wt_status_check_bisect(const struct worktree *wt,
1743 struct wt_status_state *state)
1745 struct stat st;
1747 if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1748 state->bisect_in_progress = 1;
1749 state->bisecting_from = get_branch(wt, "BISECT_START");
1750 return 1;
1752 return 0;
1755 static void wt_status_check_sparse_checkout(struct repository *r,
1756 struct wt_status_state *state)
1758 int skip_worktree = 0;
1759 int i;
1761 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1763 * Don't compute percentage of checked out files if we
1764 * aren't in a sparse checkout or would get division by 0.
1766 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1767 return;
1770 if (r->index->sparse_index) {
1771 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1772 return;
1775 for (i = 0; i < r->index->cache_nr; i++) {
1776 struct cache_entry *ce = r->index->cache[i];
1777 if (ce_skip_worktree(ce))
1778 skip_worktree++;
1781 state->sparse_checkout_percentage =
1782 100 - (100 * skip_worktree)/r->index->cache_nr;
1785 void wt_status_get_state(struct repository *r,
1786 struct wt_status_state *state,
1787 int get_detached_from)
1789 struct stat st;
1790 struct object_id oid;
1791 enum replay_action action;
1793 if (!stat(git_path_merge_head(r), &st)) {
1794 wt_status_check_rebase(NULL, state);
1795 state->merge_in_progress = 1;
1796 } else if (wt_status_check_rebase(NULL, state)) {
1797 ; /* all set */
1798 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1799 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1800 state->cherry_pick_in_progress = 1;
1801 oidcpy(&state->cherry_pick_head_oid, &oid);
1803 wt_status_check_bisect(NULL, state);
1804 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1805 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
1806 state->revert_in_progress = 1;
1807 oidcpy(&state->revert_head_oid, &oid);
1809 if (!sequencer_get_last_command(r, &action)) {
1810 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1811 state->cherry_pick_in_progress = 1;
1812 oidcpy(&state->cherry_pick_head_oid, null_oid());
1813 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1814 state->revert_in_progress = 1;
1815 oidcpy(&state->revert_head_oid, null_oid());
1818 if (get_detached_from)
1819 wt_status_get_detached_from(r, state);
1820 wt_status_check_sparse_checkout(r, state);
1823 static void wt_longstatus_print_state(struct wt_status *s)
1825 const char *state_color = color(WT_STATUS_HEADER, s);
1826 struct wt_status_state *state = &s->state;
1828 if (state->merge_in_progress) {
1829 if (state->rebase_interactive_in_progress) {
1830 show_rebase_information(s, state_color);
1831 fputs("\n", s->fp);
1833 show_merge_in_progress(s, state_color);
1834 } else if (state->am_in_progress)
1835 show_am_in_progress(s, state_color);
1836 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1837 show_rebase_in_progress(s, state_color);
1838 else if (state->cherry_pick_in_progress)
1839 show_cherry_pick_in_progress(s, state_color);
1840 else if (state->revert_in_progress)
1841 show_revert_in_progress(s, state_color);
1842 if (state->bisect_in_progress)
1843 show_bisect_in_progress(s, state_color);
1845 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1846 show_sparse_checkout_in_use(s, state_color);
1849 static void wt_longstatus_print(struct wt_status *s)
1851 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1852 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1853 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1855 if (s->branch) {
1856 const char *on_what = _("On branch ");
1857 const char *branch_name = s->branch;
1858 if (!strcmp(branch_name, "HEAD")) {
1859 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1860 if (s->state.rebase_in_progress ||
1861 s->state.rebase_interactive_in_progress) {
1862 if (s->state.rebase_interactive_in_progress)
1863 on_what = _("interactive rebase in progress; onto ");
1864 else
1865 on_what = _("rebase in progress; onto ");
1866 branch_name = s->state.onto;
1867 } else if (s->state.detached_from) {
1868 branch_name = s->state.detached_from;
1869 if (s->state.detached_at)
1870 on_what = _("HEAD detached at ");
1871 else
1872 on_what = _("HEAD detached from ");
1873 } else {
1874 branch_name = "";
1875 on_what = _("Not currently on any branch.");
1877 } else
1878 skip_prefix(branch_name, "refs/heads/", &branch_name);
1879 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1880 status_printf_more(s, branch_status_color, "%s", on_what);
1881 status_printf_more(s, branch_color, "%s\n", branch_name);
1882 if (!s->is_initial)
1883 wt_longstatus_print_tracking(s);
1886 wt_longstatus_print_state(s);
1888 if (s->is_initial) {
1889 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1890 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1891 s->commit_template
1892 ? _("Initial commit")
1893 : _("No commits yet"));
1894 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1897 wt_longstatus_print_updated(s);
1898 wt_longstatus_print_unmerged(s);
1899 wt_longstatus_print_changed(s);
1900 if (s->submodule_summary &&
1901 (!s->ignore_submodule_arg ||
1902 strcmp(s->ignore_submodule_arg, "all"))) {
1903 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1904 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1906 if (s->show_untracked_files) {
1907 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1908 if (s->show_ignored_mode)
1909 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1910 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1911 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1912 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1913 status_printf_ln(s, GIT_COLOR_NORMAL,
1914 _("It took %.2f seconds to enumerate untracked files,\n"
1915 "but the results were cached, and subsequent runs may be faster."),
1916 s->untracked_in_ms / 1000.0);
1917 } else {
1918 status_printf_ln(s, GIT_COLOR_NORMAL,
1919 _("It took %.2f seconds to enumerate untracked files."),
1920 s->untracked_in_ms / 1000.0);
1922 status_printf_ln(s, GIT_COLOR_NORMAL,
1923 _("See 'git help status' for information on how to improve this."));
1924 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1926 } else if (s->committable)
1927 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1928 s->hints
1929 ? _(" (use -u option to show untracked files)") : "");
1931 if (s->verbose)
1932 wt_longstatus_print_verbose(s);
1933 if (!s->committable) {
1934 if (s->amend)
1935 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1936 else if (s->nowarn)
1937 ; /* nothing */
1938 else if (s->workdir_dirty) {
1939 if (s->hints)
1940 fprintf(s->fp, _("no changes added to commit "
1941 "(use \"git add\" and/or "
1942 "\"git commit -a\")\n"));
1943 else
1944 fprintf(s->fp, _("no changes added to "
1945 "commit\n"));
1946 } else if (s->untracked.nr) {
1947 if (s->hints)
1948 fprintf(s->fp, _("nothing added to commit but "
1949 "untracked files present (use "
1950 "\"git add\" to track)\n"));
1951 else
1952 fprintf(s->fp, _("nothing added to commit but "
1953 "untracked files present\n"));
1954 } else if (s->is_initial) {
1955 if (s->hints)
1956 fprintf(s->fp, _("nothing to commit (create/"
1957 "copy files and use \"git "
1958 "add\" to track)\n"));
1959 else
1960 fprintf(s->fp, _("nothing to commit\n"));
1961 } else if (!s->show_untracked_files) {
1962 if (s->hints)
1963 fprintf(s->fp, _("nothing to commit (use -u to "
1964 "show untracked files)\n"));
1965 else
1966 fprintf(s->fp, _("nothing to commit\n"));
1967 } else
1968 fprintf(s->fp, _("nothing to commit, working tree "
1969 "clean\n"));
1971 if(s->show_stash)
1972 wt_longstatus_print_stash_summary(s);
1975 static void wt_shortstatus_unmerged(struct string_list_item *it,
1976 struct wt_status *s)
1978 struct wt_status_change_data *d = it->util;
1979 const char *how = "??";
1981 switch (d->stagemask) {
1982 case 1: how = "DD"; break; /* both deleted */
1983 case 2: how = "AU"; break; /* added by us */
1984 case 3: how = "UD"; break; /* deleted by them */
1985 case 4: how = "UA"; break; /* added by them */
1986 case 5: how = "DU"; break; /* deleted by us */
1987 case 6: how = "AA"; break; /* both added */
1988 case 7: how = "UU"; break; /* both modified */
1990 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1991 if (s->null_termination) {
1992 fprintf(s->fp, " %s%c", it->string, 0);
1993 } else {
1994 struct strbuf onebuf = STRBUF_INIT;
1995 const char *one;
1996 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1997 fprintf(s->fp, " %s\n", one);
1998 strbuf_release(&onebuf);
2002 static void wt_shortstatus_status(struct string_list_item *it,
2003 struct wt_status *s)
2005 struct wt_status_change_data *d = it->util;
2007 if (d->index_status)
2008 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
2009 else
2010 fputc(' ', s->fp);
2011 if (d->worktree_status)
2012 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2013 else
2014 fputc(' ', s->fp);
2015 fputc(' ', s->fp);
2016 if (s->null_termination) {
2017 fprintf(s->fp, "%s%c", it->string, 0);
2018 if (d->rename_source)
2019 fprintf(s->fp, "%s%c", d->rename_source, 0);
2020 } else {
2021 struct strbuf onebuf = STRBUF_INIT;
2022 const char *one;
2024 if (d->rename_source) {
2025 one = quote_path(d->rename_source, s->prefix, &onebuf,
2026 QUOTE_PATH_QUOTE_SP);
2027 fprintf(s->fp, "%s -> ", one);
2028 strbuf_release(&onebuf);
2030 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2031 fprintf(s->fp, "%s\n", one);
2032 strbuf_release(&onebuf);
2036 static void wt_shortstatus_other(struct string_list_item *it,
2037 struct wt_status *s, const char *sign)
2039 if (s->null_termination) {
2040 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2041 } else {
2042 struct strbuf onebuf = STRBUF_INIT;
2043 const char *one;
2044 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2045 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2046 fprintf(s->fp, " %s\n", one);
2047 strbuf_release(&onebuf);
2051 static void wt_shortstatus_print_tracking(struct wt_status *s)
2053 struct branch *branch;
2054 const char *header_color = color(WT_STATUS_HEADER, s);
2055 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2056 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2058 const char *base;
2059 char *short_base;
2060 const char *branch_name;
2061 int num_ours, num_theirs, sti;
2062 int upstream_is_gone = 0;
2064 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2066 if (!s->branch)
2067 return;
2068 branch_name = s->branch;
2070 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2072 if (s->is_initial)
2073 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2075 if (!strcmp(s->branch, "HEAD")) {
2076 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2077 LABEL(N_("HEAD (no branch)")));
2078 goto conclude;
2081 skip_prefix(branch_name, "refs/heads/", &branch_name);
2083 branch = branch_get(branch_name);
2085 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2087 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2088 0, s->ahead_behind_flags);
2089 if (sti < 0) {
2090 if (!base)
2091 goto conclude;
2093 upstream_is_gone = 1;
2096 short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2097 base, 0);
2098 color_fprintf(s->fp, header_color, "...");
2099 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2100 free(short_base);
2102 if (!upstream_is_gone && !sti)
2103 goto conclude;
2105 color_fprintf(s->fp, header_color, " [");
2106 if (upstream_is_gone) {
2107 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2108 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2109 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2110 } else if (!num_ours) {
2111 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2112 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2113 } else if (!num_theirs) {
2114 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2115 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2116 } else {
2117 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2118 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2119 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2120 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2123 color_fprintf(s->fp, header_color, "]");
2124 conclude:
2125 fputc(s->null_termination ? '\0' : '\n', s->fp);
2128 static void wt_shortstatus_print(struct wt_status *s)
2130 struct string_list_item *it;
2132 if (s->show_branch)
2133 wt_shortstatus_print_tracking(s);
2135 for_each_string_list_item(it, &s->change) {
2136 struct wt_status_change_data *d = it->util;
2138 if (d->stagemask)
2139 wt_shortstatus_unmerged(it, s);
2140 else
2141 wt_shortstatus_status(it, s);
2143 for_each_string_list_item(it, &s->untracked)
2144 wt_shortstatus_other(it, s, "??");
2146 for_each_string_list_item(it, &s->ignored)
2147 wt_shortstatus_other(it, s, "!!");
2150 static void wt_porcelain_print(struct wt_status *s)
2152 s->use_color = 0;
2153 s->relative_paths = 0;
2154 s->prefix = NULL;
2155 s->no_gettext = 1;
2156 wt_shortstatus_print(s);
2160 * Print branch information for porcelain v2 output. These lines
2161 * are printed when the '--branch' parameter is given.
2163 * # branch.oid <commit><eol>
2164 * # branch.head <head><eol>
2165 * [# branch.upstream <upstream><eol>
2166 * [# branch.ab +<ahead> -<behind><eol>]]
2168 * <commit> ::= the current commit hash or the literal
2169 * "(initial)" to indicate an initialized repo
2170 * with no commits.
2172 * <head> ::= <branch_name> the current branch name or
2173 * "(detached)" literal when detached head or
2174 * "(unknown)" when something is wrong.
2176 * <upstream> ::= the upstream branch name, when set.
2178 * <ahead> ::= integer ahead value or '?'.
2180 * <behind> ::= integer behind value or '?'.
2182 * The end-of-line is defined by the -z flag.
2184 * <eol> ::= NUL when -z,
2185 * LF when NOT -z.
2187 * When an upstream is set and present, the 'branch.ab' line will
2188 * be printed with the ahead/behind counts for the branch and the
2189 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2190 * are different, '?' will be substituted for the actual count.
2192 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2194 struct branch *branch;
2195 const char *base;
2196 const char *branch_name;
2197 int ab_info, nr_ahead, nr_behind;
2198 char eol = s->null_termination ? '\0' : '\n';
2200 fprintf(s->fp, "# branch.oid %s%c",
2201 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2202 eol);
2204 if (!s->branch)
2205 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2206 else {
2207 if (!strcmp(s->branch, "HEAD")) {
2208 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2210 if (s->state.rebase_in_progress ||
2211 s->state.rebase_interactive_in_progress)
2212 branch_name = s->state.onto;
2213 else if (s->state.detached_from)
2214 branch_name = s->state.detached_from;
2215 else
2216 branch_name = "";
2217 } else {
2218 branch_name = NULL;
2219 skip_prefix(s->branch, "refs/heads/", &branch_name);
2221 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2224 /* Lookup stats on the upstream tracking branch, if set. */
2225 branch = branch_get(branch_name);
2226 base = NULL;
2227 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2228 &base, 0, s->ahead_behind_flags);
2229 if (base) {
2230 base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2231 base, 0);
2232 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2233 free((char *)base);
2235 if (ab_info > 0) {
2236 /* different */
2237 if (nr_ahead || nr_behind)
2238 fprintf(s->fp, "# branch.ab +%d -%d%c",
2239 nr_ahead, nr_behind, eol);
2240 else
2241 fprintf(s->fp, "# branch.ab +? -?%c",
2242 eol);
2243 } else if (!ab_info) {
2244 /* same */
2245 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2252 * Print the stash count in a porcelain-friendly format
2254 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2256 int stash_count = count_stash_entries();
2257 char eol = s->null_termination ? '\0' : '\n';
2259 if (stash_count > 0)
2260 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2264 * Convert various submodule status values into a
2265 * fixed-length string of characters in the buffer provided.
2267 static void wt_porcelain_v2_submodule_state(
2268 struct wt_status_change_data *d,
2269 char sub[5])
2271 if (S_ISGITLINK(d->mode_head) ||
2272 S_ISGITLINK(d->mode_index) ||
2273 S_ISGITLINK(d->mode_worktree)) {
2274 sub[0] = 'S';
2275 sub[1] = d->new_submodule_commits ? 'C' : '.';
2276 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2277 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2278 } else {
2279 sub[0] = 'N';
2280 sub[1] = '.';
2281 sub[2] = '.';
2282 sub[3] = '.';
2284 sub[4] = 0;
2288 * Fix-up changed entries before we print them.
2290 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2292 struct wt_status_change_data *d = it->util;
2294 if (!d->index_status) {
2296 * This entry is unchanged in the index (relative to the head).
2297 * Therefore, the collect_updated_cb was never called for this
2298 * entry (during the head-vs-index scan) and so the head column
2299 * fields were never set.
2301 * We must have data for the index column (from the
2302 * index-vs-worktree scan (otherwise, this entry should not be
2303 * in the list of changes)).
2305 * Copy index column fields to the head column, so that our
2306 * output looks complete.
2308 assert(d->mode_head == 0);
2309 d->mode_head = d->mode_index;
2310 oidcpy(&d->oid_head, &d->oid_index);
2313 if (!d->worktree_status) {
2315 * This entry is unchanged in the worktree (relative to the index).
2316 * Therefore, the collect_changed_cb was never called for this entry
2317 * (during the index-vs-worktree scan) and so the worktree column
2318 * fields were never set.
2320 * We must have data for the index column (from the head-vs-index
2321 * scan).
2323 * Copy the index column fields to the worktree column so that
2324 * our output looks complete.
2326 * Note that we only have a mode field in the worktree column
2327 * because the scan code tries really hard to not have to compute it.
2329 assert(d->mode_worktree == 0);
2330 d->mode_worktree = d->mode_index;
2335 * Print porcelain v2 info for tracked entries with changes.
2337 static void wt_porcelain_v2_print_changed_entry(
2338 struct string_list_item *it,
2339 struct wt_status *s)
2341 struct wt_status_change_data *d = it->util;
2342 struct strbuf buf = STRBUF_INIT;
2343 struct strbuf buf_from = STRBUF_INIT;
2344 const char *path = NULL;
2345 const char *path_from = NULL;
2346 char key[3];
2347 char submodule_token[5];
2348 char sep_char, eol_char;
2350 wt_porcelain_v2_fix_up_changed(it);
2351 wt_porcelain_v2_submodule_state(d, submodule_token);
2353 key[0] = d->index_status ? d->index_status : '.';
2354 key[1] = d->worktree_status ? d->worktree_status : '.';
2355 key[2] = 0;
2357 if (s->null_termination) {
2359 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2360 * A single NUL character separates them.
2362 sep_char = '\0';
2363 eol_char = '\0';
2364 path = it->string;
2365 path_from = d->rename_source;
2366 } else {
2368 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2369 * The source path is only present when necessary.
2370 * A single TAB separates them (because paths can contain spaces
2371 * which are not escaped and C-quoting does escape TAB characters).
2373 sep_char = '\t';
2374 eol_char = '\n';
2375 path = quote_path(it->string, s->prefix, &buf, 0);
2376 if (d->rename_source)
2377 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2380 if (path_from)
2381 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2382 key, submodule_token,
2383 d->mode_head, d->mode_index, d->mode_worktree,
2384 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2385 d->rename_status, d->rename_score,
2386 path, sep_char, path_from, eol_char);
2387 else
2388 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2389 key, submodule_token,
2390 d->mode_head, d->mode_index, d->mode_worktree,
2391 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2392 path, eol_char);
2394 strbuf_release(&buf);
2395 strbuf_release(&buf_from);
2399 * Print porcelain v2 status info for unmerged entries.
2401 static void wt_porcelain_v2_print_unmerged_entry(
2402 struct string_list_item *it,
2403 struct wt_status *s)
2405 struct wt_status_change_data *d = it->util;
2406 struct index_state *istate = s->repo->index;
2407 const struct cache_entry *ce;
2408 struct strbuf buf_index = STRBUF_INIT;
2409 const char *path_index = NULL;
2410 int pos, stage, sum;
2411 struct {
2412 int mode;
2413 struct object_id oid;
2414 } stages[3];
2415 const char *key;
2416 char submodule_token[5];
2417 char unmerged_prefix = 'u';
2418 char eol_char = s->null_termination ? '\0' : '\n';
2420 wt_porcelain_v2_submodule_state(d, submodule_token);
2422 switch (d->stagemask) {
2423 case 1: key = "DD"; break; /* both deleted */
2424 case 2: key = "AU"; break; /* added by us */
2425 case 3: key = "UD"; break; /* deleted by them */
2426 case 4: key = "UA"; break; /* added by them */
2427 case 5: key = "DU"; break; /* deleted by us */
2428 case 6: key = "AA"; break; /* both added */
2429 case 7: key = "UU"; break; /* both modified */
2430 default:
2431 BUG("unhandled unmerged status %x", d->stagemask);
2435 * Disregard d.aux.porcelain_v2 data that we accumulated
2436 * for the head and index columns during the scans and
2437 * replace with the actual stage data.
2439 * Note that this is a last-one-wins for each the individual
2440 * stage [123] columns in the event of multiple cache entries
2441 * for same stage.
2443 memset(stages, 0, sizeof(stages));
2444 sum = 0;
2445 pos = index_name_pos(istate, it->string, strlen(it->string));
2446 assert(pos < 0);
2447 pos = -pos-1;
2448 while (pos < istate->cache_nr) {
2449 ce = istate->cache[pos++];
2450 stage = ce_stage(ce);
2451 if (strcmp(ce->name, it->string) || !stage)
2452 break;
2453 stages[stage - 1].mode = ce->ce_mode;
2454 oidcpy(&stages[stage - 1].oid, &ce->oid);
2455 sum |= (1 << (stage - 1));
2457 if (sum != d->stagemask)
2458 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2460 if (s->null_termination)
2461 path_index = it->string;
2462 else
2463 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2465 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2466 unmerged_prefix, key, submodule_token,
2467 stages[0].mode, /* stage 1 */
2468 stages[1].mode, /* stage 2 */
2469 stages[2].mode, /* stage 3 */
2470 d->mode_worktree,
2471 oid_to_hex(&stages[0].oid), /* stage 1 */
2472 oid_to_hex(&stages[1].oid), /* stage 2 */
2473 oid_to_hex(&stages[2].oid), /* stage 3 */
2474 path_index,
2475 eol_char);
2477 strbuf_release(&buf_index);
2481 * Print porcelain V2 status info for untracked and ignored entries.
2483 static void wt_porcelain_v2_print_other(
2484 struct string_list_item *it,
2485 struct wt_status *s,
2486 char prefix)
2488 struct strbuf buf = STRBUF_INIT;
2489 const char *path;
2490 char eol_char;
2492 if (s->null_termination) {
2493 path = it->string;
2494 eol_char = '\0';
2495 } else {
2496 path = quote_path(it->string, s->prefix, &buf, 0);
2497 eol_char = '\n';
2500 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2502 strbuf_release(&buf);
2506 * Print porcelain V2 status.
2508 * [<v2_branch>]
2509 * [<v2_changed_items>]*
2510 * [<v2_unmerged_items>]*
2511 * [<v2_untracked_items>]*
2512 * [<v2_ignored_items>]*
2515 static void wt_porcelain_v2_print(struct wt_status *s)
2517 struct wt_status_change_data *d;
2518 struct string_list_item *it;
2519 int i;
2521 if (s->show_branch)
2522 wt_porcelain_v2_print_tracking(s);
2524 if (s->show_stash)
2525 wt_porcelain_v2_print_stash(s);
2527 for (i = 0; i < s->change.nr; i++) {
2528 it = &(s->change.items[i]);
2529 d = it->util;
2530 if (!d->stagemask)
2531 wt_porcelain_v2_print_changed_entry(it, s);
2534 for (i = 0; i < s->change.nr; i++) {
2535 it = &(s->change.items[i]);
2536 d = it->util;
2537 if (d->stagemask)
2538 wt_porcelain_v2_print_unmerged_entry(it, s);
2541 for (i = 0; i < s->untracked.nr; i++) {
2542 it = &(s->untracked.items[i]);
2543 wt_porcelain_v2_print_other(it, s, '?');
2546 for (i = 0; i < s->ignored.nr; i++) {
2547 it = &(s->ignored.items[i]);
2548 wt_porcelain_v2_print_other(it, s, '!');
2552 void wt_status_print(struct wt_status *s)
2554 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2555 trace2_data_intmax("status", s->repo, "count/untracked",
2556 s->untracked.nr);
2557 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2559 trace2_region_enter("status", "print", s->repo);
2561 switch (s->status_format) {
2562 case STATUS_FORMAT_SHORT:
2563 wt_shortstatus_print(s);
2564 break;
2565 case STATUS_FORMAT_PORCELAIN:
2566 wt_porcelain_print(s);
2567 break;
2568 case STATUS_FORMAT_PORCELAIN_V2:
2569 wt_porcelain_v2_print(s);
2570 break;
2571 case STATUS_FORMAT_UNSPECIFIED:
2572 BUG("finalize_deferred_config() should have been called");
2573 break;
2574 case STATUS_FORMAT_NONE:
2575 case STATUS_FORMAT_LONG:
2576 wt_longstatus_print(s);
2577 break;
2580 trace2_region_leave("status", "print", s->repo);
2584 * Returns 1 if there are unstaged changes, 0 otherwise.
2586 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2588 struct rev_info rev_info;
2589 int result;
2591 repo_init_revisions(r, &rev_info, NULL);
2592 if (ignore_submodules) {
2593 rev_info.diffopt.flags.ignore_submodules = 1;
2594 rev_info.diffopt.flags.override_submodule_config = 1;
2596 rev_info.diffopt.flags.quick = 1;
2597 diff_setup_done(&rev_info.diffopt);
2598 run_diff_files(&rev_info, 0);
2599 result = diff_result_code(&rev_info);
2600 release_revisions(&rev_info);
2601 return result;
2605 * Returns 1 if there are uncommitted changes, 0 otherwise.
2607 int has_uncommitted_changes(struct repository *r,
2608 int ignore_submodules)
2610 struct rev_info rev_info;
2611 int result;
2613 if (is_index_unborn(r->index))
2614 return 0;
2616 repo_init_revisions(r, &rev_info, NULL);
2617 if (ignore_submodules)
2618 rev_info.diffopt.flags.ignore_submodules = 1;
2619 rev_info.diffopt.flags.quick = 1;
2621 add_head_to_pending(&rev_info);
2622 if (!rev_info.pending.nr) {
2624 * We have no head (or it's corrupt); use the empty tree,
2625 * which will complain if the index is non-empty.
2627 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2628 add_pending_object(&rev_info, &tree->object, "");
2631 diff_setup_done(&rev_info.diffopt);
2632 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2633 result = diff_result_code(&rev_info);
2634 release_revisions(&rev_info);
2635 return result;
2639 * If the work tree has unstaged or uncommitted changes, dies with the
2640 * appropriate message.
2642 int require_clean_work_tree(struct repository *r,
2643 const char *action,
2644 const char *hint,
2645 int ignore_submodules,
2646 int gently)
2648 struct lock_file lock_file = LOCK_INIT;
2649 int err = 0, fd;
2651 fd = repo_hold_locked_index(r, &lock_file, 0);
2652 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2653 if (0 <= fd)
2654 repo_update_index_if_able(r, &lock_file);
2655 rollback_lock_file(&lock_file);
2657 if (has_unstaged_changes(r, ignore_submodules)) {
2658 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2659 error(_("cannot %s: You have unstaged changes."), _(action));
2660 err = 1;
2663 if (has_uncommitted_changes(r, ignore_submodules)) {
2664 if (err)
2665 error(_("additionally, your index contains uncommitted changes."));
2666 else
2667 error(_("cannot %s: Your index contains uncommitted changes."),
2668 _(action));
2669 err = 1;
2672 if (err) {
2673 if (hint) {
2674 if (!*hint)
2675 BUG("empty hint passed to require_clean_work_tree();"
2676 " use NULL instead");
2677 error("%s", hint);
2679 if (!gently)
2680 exit(128);
2683 return err;