Merge branch 'ps/reftable-drop-generic'
[git/gitster.git] / wt-status.c
blobb477239039dd93ce70a58c56c0f5e070aa166cf0
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 "run-command.h"
20 #include "strvec.h"
21 #include "remote.h"
22 #include "refs.h"
23 #include "submodule.h"
24 #include "column.h"
25 #include "read-cache.h"
26 #include "setup.h"
27 #include "strbuf.h"
28 #include "trace.h"
29 #include "trace2.h"
30 #include "tree.h"
31 #include "utf8.h"
32 #include "worktree.h"
33 #include "lockfile.h"
34 #include "sequencer.h"
35 #include "fsmonitor-settings.h"
37 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
38 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
40 static const char cut_line[] =
41 "------------------------ >8 ------------------------\n";
43 static char default_wt_status_colors[][COLOR_MAXLEN] = {
44 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
45 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
46 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
47 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
48 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
49 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
50 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
51 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
52 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
55 static const char *color(int slot, struct wt_status *s)
57 const char *c = "";
58 if (want_color(s->use_color))
59 c = s->color_palette[slot];
60 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
61 c = s->color_palette[WT_STATUS_HEADER];
62 return c;
65 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
66 const char *fmt, va_list ap, const char *trail)
68 struct strbuf sb = STRBUF_INIT;
69 struct strbuf linebuf = STRBUF_INIT;
70 const char *line, *eol;
72 strbuf_vaddf(&sb, fmt, ap);
73 if (!sb.len) {
74 if (s->display_comment_prefix) {
75 strbuf_addstr(&sb, comment_line_str);
76 if (!trail)
77 strbuf_addch(&sb, ' ');
79 color_print_strbuf(s->fp, color, &sb);
80 if (trail)
81 fprintf(s->fp, "%s", trail);
82 strbuf_release(&sb);
83 return;
85 for (line = sb.buf; *line; line = eol + 1) {
86 eol = strchr(line, '\n');
88 strbuf_reset(&linebuf);
89 if (at_bol && s->display_comment_prefix) {
90 strbuf_addstr(&linebuf, comment_line_str);
91 if (*line != '\n' && *line != '\t')
92 strbuf_addch(&linebuf, ' ');
94 if (eol)
95 strbuf_add(&linebuf, line, eol - line);
96 else
97 strbuf_addstr(&linebuf, line);
98 color_print_strbuf(s->fp, color, &linebuf);
99 if (eol)
100 fprintf(s->fp, "\n");
101 else
102 break;
103 at_bol = 1;
105 if (trail)
106 fprintf(s->fp, "%s", trail);
107 strbuf_release(&linebuf);
108 strbuf_release(&sb);
111 void status_printf_ln(struct wt_status *s, const char *color,
112 const char *fmt, ...)
114 va_list ap;
116 va_start(ap, fmt);
117 status_vprintf(s, 1, color, fmt, ap, "\n");
118 va_end(ap);
121 void status_printf(struct wt_status *s, const char *color,
122 const char *fmt, ...)
124 va_list ap;
126 va_start(ap, fmt);
127 status_vprintf(s, 1, color, fmt, ap, NULL);
128 va_end(ap);
131 __attribute__((format (printf, 3, 4)))
132 static void status_printf_more(struct wt_status *s, const char *color,
133 const char *fmt, ...)
135 va_list ap;
137 va_start(ap, fmt);
138 status_vprintf(s, 0, color, fmt, ap, NULL);
139 va_end(ap);
142 void wt_status_prepare(struct repository *r, struct wt_status *s)
144 memset(s, 0, sizeof(*s));
145 s->repo = r;
146 memcpy(s->color_palette, default_wt_status_colors,
147 sizeof(default_wt_status_colors));
148 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
149 s->use_color = -1;
150 s->relative_paths = 1;
151 s->branch = refs_resolve_refdup(get_main_ref_store(the_repository),
152 "HEAD", 0, NULL, NULL);
153 s->reference = "HEAD";
154 s->fp = stdout;
155 s->index_file = get_index_file();
156 s->change.strdup_strings = 1;
157 s->untracked.strdup_strings = 1;
158 s->ignored.strdup_strings = 1;
159 s->show_branch = -1; /* unspecified */
160 s->show_stash = 0;
161 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
162 s->display_comment_prefix = 0;
163 s->detect_rename = -1;
164 s->rename_score = -1;
165 s->rename_limit = -1;
168 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
170 int i;
171 int del_mod_conflict = 0;
172 int both_deleted = 0;
173 int not_deleted = 0;
174 const char *c = color(WT_STATUS_HEADER, s);
176 status_printf_ln(s, c, _("Unmerged paths:"));
178 for (i = 0; i < s->change.nr; i++) {
179 struct string_list_item *it = &(s->change.items[i]);
180 struct wt_status_change_data *d = it->util;
182 switch (d->stagemask) {
183 case 0:
184 break;
185 case 1:
186 both_deleted = 1;
187 break;
188 case 3:
189 case 5:
190 del_mod_conflict = 1;
191 break;
192 default:
193 not_deleted = 1;
194 break;
198 if (!s->hints)
199 return;
200 if (s->whence != FROM_COMMIT)
202 else if (!s->is_initial) {
203 if (!strcmp(s->reference, "HEAD"))
204 status_printf_ln(s, c,
205 _(" (use \"git restore --staged <file>...\" to unstage)"));
206 else
207 status_printf_ln(s, c,
208 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
209 s->reference);
210 } else
211 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
213 if (!both_deleted) {
214 if (!del_mod_conflict)
215 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
216 else
217 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
218 } else if (!del_mod_conflict && !not_deleted) {
219 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
220 } else {
221 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
225 static void wt_longstatus_print_cached_header(struct wt_status *s)
227 const char *c = color(WT_STATUS_HEADER, s);
229 status_printf_ln(s, c, _("Changes to be committed:"));
230 if (!s->hints)
231 return;
232 if (s->whence != FROM_COMMIT)
233 ; /* NEEDSWORK: use "git reset --unresolve"??? */
234 else if (!s->is_initial) {
235 if (!strcmp(s->reference, "HEAD"))
236 status_printf_ln(s, c
237 , _(" (use \"git restore --staged <file>...\" to unstage)"));
238 else
239 status_printf_ln(s, c,
240 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
241 s->reference);
242 } else
243 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
246 static void wt_longstatus_print_dirty_header(struct wt_status *s,
247 int has_deleted,
248 int has_dirty_submodules)
250 const char *c = color(WT_STATUS_HEADER, s);
252 status_printf_ln(s, c, _("Changes not staged for commit:"));
253 if (!s->hints)
254 return;
255 if (!has_deleted)
256 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
257 else
258 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
259 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
260 if (has_dirty_submodules)
261 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
264 static void wt_longstatus_print_other_header(struct wt_status *s,
265 const char *what,
266 const char *how)
268 const char *c = color(WT_STATUS_HEADER, s);
269 status_printf_ln(s, c, "%s:", what);
270 if (!s->hints)
271 return;
272 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
275 static void wt_longstatus_print_trailer(struct wt_status *s)
277 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
280 static const char *wt_status_unmerged_status_string(int stagemask)
282 switch (stagemask) {
283 case 1:
284 return _("both deleted:");
285 case 2:
286 return _("added by us:");
287 case 3:
288 return _("deleted by them:");
289 case 4:
290 return _("added by them:");
291 case 5:
292 return _("deleted by us:");
293 case 6:
294 return _("both added:");
295 case 7:
296 return _("both modified:");
297 default:
298 BUG("unhandled unmerged status %x", stagemask);
302 static const char *wt_status_diff_status_string(int status)
304 switch (status) {
305 case DIFF_STATUS_ADDED:
306 return _("new file:");
307 case DIFF_STATUS_COPIED:
308 return _("copied:");
309 case DIFF_STATUS_DELETED:
310 return _("deleted:");
311 case DIFF_STATUS_MODIFIED:
312 return _("modified:");
313 case DIFF_STATUS_RENAMED:
314 return _("renamed:");
315 case DIFF_STATUS_TYPE_CHANGED:
316 return _("typechange:");
317 case DIFF_STATUS_UNKNOWN:
318 return _("unknown:");
319 case DIFF_STATUS_UNMERGED:
320 return _("unmerged:");
321 default:
322 return NULL;
326 static int maxwidth(const char *(*label)(int), int minval, int maxval)
328 int result = 0, i;
330 for (i = minval; i <= maxval; i++) {
331 const char *s = label(i);
332 int len = s ? utf8_strwidth(s) : 0;
333 if (len > result)
334 result = len;
336 return result;
339 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
340 struct string_list_item *it)
342 const char *c = color(WT_STATUS_UNMERGED, s);
343 struct wt_status_change_data *d = it->util;
344 struct strbuf onebuf = STRBUF_INIT;
345 static char *padding;
346 static int label_width;
347 const char *one, *how;
348 int len;
350 if (!padding) {
351 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
352 label_width += strlen(" ");
353 padding = xmallocz(label_width);
354 memset(padding, ' ', label_width);
357 one = quote_path(it->string, s->prefix, &onebuf, 0);
358 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
360 how = wt_status_unmerged_status_string(d->stagemask);
361 len = label_width - utf8_strwidth(how);
362 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
363 strbuf_release(&onebuf);
366 static void wt_longstatus_print_change_data(struct wt_status *s,
367 int change_type,
368 struct string_list_item *it)
370 struct wt_status_change_data *d = it->util;
371 const char *c = color(change_type, s);
372 int status;
373 char *one_name;
374 char *two_name;
375 const char *one, *two;
376 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
377 struct strbuf extra = STRBUF_INIT;
378 static char *padding;
379 static int label_width;
380 const char *what;
381 int len;
383 if (!padding) {
384 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
385 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
386 label_width += strlen(" ");
387 padding = xmallocz(label_width);
388 memset(padding, ' ', label_width);
391 one_name = two_name = it->string;
392 switch (change_type) {
393 case WT_STATUS_UPDATED:
394 status = d->index_status;
395 break;
396 case WT_STATUS_CHANGED:
397 if (d->new_submodule_commits || d->dirty_submodule) {
398 strbuf_addstr(&extra, " (");
399 if (d->new_submodule_commits)
400 strbuf_addstr(&extra, _("new commits, "));
401 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
402 strbuf_addstr(&extra, _("modified content, "));
403 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
404 strbuf_addstr(&extra, _("untracked content, "));
405 strbuf_setlen(&extra, extra.len - 2);
406 strbuf_addch(&extra, ')');
408 status = d->worktree_status;
409 break;
410 default:
411 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
412 change_type);
416 * Only pick up the rename it's relevant. If the rename is for
417 * the changed section and we're printing the updated section,
418 * ignore it.
420 if (d->rename_status == status)
421 one_name = d->rename_source;
423 one = quote_path(one_name, s->prefix, &onebuf, 0);
424 two = quote_path(two_name, s->prefix, &twobuf, 0);
426 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
427 what = wt_status_diff_status_string(status);
428 if (!what)
429 BUG("unhandled diff status %c", status);
430 len = label_width - utf8_strwidth(what);
431 assert(len >= 0);
432 if (one_name != two_name)
433 status_printf_more(s, c, "%s%.*s%s -> %s",
434 what, len, padding, one, two);
435 else
436 status_printf_more(s, c, "%s%.*s%s",
437 what, len, padding, one);
438 if (extra.len) {
439 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
440 strbuf_release(&extra);
442 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
443 strbuf_release(&onebuf);
444 strbuf_release(&twobuf);
447 static char short_submodule_status(struct wt_status_change_data *d)
449 if (d->new_submodule_commits)
450 return 'M';
451 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
452 return 'm';
453 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
454 return '?';
455 return d->worktree_status;
458 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
459 struct diff_options *options UNUSED,
460 void *data)
462 struct wt_status *s = data;
463 int i;
465 if (!q->nr)
466 return;
467 s->workdir_dirty = 1;
468 for (i = 0; i < q->nr; i++) {
469 struct diff_filepair *p;
470 struct string_list_item *it;
471 struct wt_status_change_data *d;
473 p = q->queue[i];
474 it = string_list_insert(&s->change, p->two->path);
475 d = it->util;
476 if (!d) {
477 CALLOC_ARRAY(d, 1);
478 it->util = d;
480 if (!d->worktree_status)
481 d->worktree_status = p->status;
482 if (S_ISGITLINK(p->two->mode)) {
483 d->dirty_submodule = p->two->dirty_submodule;
484 d->new_submodule_commits = !oideq(&p->one->oid,
485 &p->two->oid);
486 if (s->status_format == STATUS_FORMAT_SHORT)
487 d->worktree_status = short_submodule_status(d);
490 switch (p->status) {
491 case DIFF_STATUS_ADDED:
492 d->mode_worktree = p->two->mode;
493 break;
495 case DIFF_STATUS_DELETED:
496 d->mode_index = p->one->mode;
497 oidcpy(&d->oid_index, &p->one->oid);
498 /* mode_worktree is zero for a delete. */
499 break;
501 case DIFF_STATUS_COPIED:
502 case DIFF_STATUS_RENAMED:
503 if (d->rename_status)
504 BUG("multiple renames on the same target? how?");
505 d->rename_source = xstrdup(p->one->path);
506 d->rename_score = p->score * 100 / MAX_SCORE;
507 d->rename_status = p->status;
508 /* fallthru */
509 case DIFF_STATUS_MODIFIED:
510 case DIFF_STATUS_TYPE_CHANGED:
511 case DIFF_STATUS_UNMERGED:
512 d->mode_index = p->one->mode;
513 d->mode_worktree = p->two->mode;
514 oidcpy(&d->oid_index, &p->one->oid);
515 break;
517 default:
518 BUG("unhandled diff-files status '%c'", p->status);
519 break;
525 static int unmerged_mask(struct index_state *istate, const char *path)
527 int pos, mask;
528 const struct cache_entry *ce;
530 pos = index_name_pos(istate, path, strlen(path));
531 if (0 <= pos)
532 return 0;
534 mask = 0;
535 pos = -pos-1;
536 while (pos < istate->cache_nr) {
537 ce = istate->cache[pos++];
538 if (strcmp(ce->name, path) || !ce_stage(ce))
539 break;
540 mask |= (1 << (ce_stage(ce) - 1));
542 return mask;
545 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
546 struct diff_options *options UNUSED,
547 void *data)
549 struct wt_status *s = data;
550 int i;
552 for (i = 0; i < q->nr; i++) {
553 struct diff_filepair *p;
554 struct string_list_item *it;
555 struct wt_status_change_data *d;
557 p = q->queue[i];
558 it = string_list_insert(&s->change, p->two->path);
559 d = it->util;
560 if (!d) {
561 CALLOC_ARRAY(d, 1);
562 it->util = d;
564 if (!d->index_status)
565 d->index_status = p->status;
566 switch (p->status) {
567 case DIFF_STATUS_ADDED:
568 /* Leave {mode,oid}_head zero for an add. */
569 d->mode_index = p->two->mode;
570 oidcpy(&d->oid_index, &p->two->oid);
571 s->committable = 1;
572 break;
573 case DIFF_STATUS_DELETED:
574 d->mode_head = p->one->mode;
575 oidcpy(&d->oid_head, &p->one->oid);
576 s->committable = 1;
577 /* Leave {mode,oid}_index zero for a delete. */
578 break;
580 case DIFF_STATUS_COPIED:
581 case DIFF_STATUS_RENAMED:
582 if (d->rename_status)
583 BUG("multiple renames on the same target? how?");
584 d->rename_source = xstrdup(p->one->path);
585 d->rename_score = p->score * 100 / MAX_SCORE;
586 d->rename_status = p->status;
587 /* fallthru */
588 case DIFF_STATUS_MODIFIED:
589 case DIFF_STATUS_TYPE_CHANGED:
590 d->mode_head = p->one->mode;
591 d->mode_index = p->two->mode;
592 oidcpy(&d->oid_head, &p->one->oid);
593 oidcpy(&d->oid_index, &p->two->oid);
594 s->committable = 1;
595 break;
596 case DIFF_STATUS_UNMERGED:
597 d->stagemask = unmerged_mask(s->repo->index,
598 p->two->path);
600 * Don't bother setting {mode,oid}_{head,index} since the print
601 * code will output the stage values directly and not use the
602 * values in these fields.
604 break;
606 default:
607 BUG("unhandled diff-index status '%c'", p->status);
608 break;
613 static void wt_status_collect_changes_worktree(struct wt_status *s)
615 struct rev_info rev;
617 repo_init_revisions(s->repo, &rev, NULL);
618 setup_revisions(0, NULL, &rev, NULL);
619 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
620 rev.diffopt.flags.dirty_submodules = 1;
621 rev.diffopt.ita_invisible_in_index = 1;
622 if (!s->show_untracked_files)
623 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
624 if (s->ignore_submodule_arg) {
625 rev.diffopt.flags.override_submodule_config = 1;
626 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
627 } else if (!rev.diffopt.flags.ignore_submodule_set &&
628 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
629 handle_ignore_submodules_arg(&rev.diffopt, "none");
630 rev.diffopt.format_callback = wt_status_collect_changed_cb;
631 rev.diffopt.format_callback_data = s;
632 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
633 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
634 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
635 copy_pathspec(&rev.prune_data, &s->pathspec);
636 run_diff_files(&rev, 0);
637 release_revisions(&rev);
640 static void wt_status_collect_changes_index(struct wt_status *s)
642 struct rev_info rev;
643 struct setup_revision_opt opt;
645 repo_init_revisions(s->repo, &rev, NULL);
646 memset(&opt, 0, sizeof(opt));
647 opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
648 setup_revisions(0, NULL, &rev, &opt);
650 rev.diffopt.flags.override_submodule_config = 1;
651 rev.diffopt.ita_invisible_in_index = 1;
652 if (s->ignore_submodule_arg) {
653 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
654 } else {
656 * Unless the user did explicitly request a submodule ignore
657 * mode by passing a command line option we do not ignore any
658 * changed submodule SHA-1s when comparing index and HEAD, no
659 * matter what is configured. Otherwise the user won't be
660 * shown any submodules manually added (and which are
661 * staged to be committed), which would be really confusing.
663 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
666 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
667 rev.diffopt.format_callback = wt_status_collect_updated_cb;
668 rev.diffopt.format_callback_data = s;
669 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
670 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
671 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
674 * The `recursive` option must be enabled to allow the diff to recurse
675 * into subdirectories of sparse directory index entries. If it is not
676 * enabled, a subdirectory containing file(s) with changes is reported
677 * as "modified", rather than the modified files themselves.
679 rev.diffopt.flags.recursive = 1;
681 copy_pathspec(&rev.prune_data, &s->pathspec);
682 run_diff_index(&rev, DIFF_INDEX_CACHED);
683 release_revisions(&rev);
686 static int add_file_to_list(const struct object_id *oid,
687 struct strbuf *base, const char *path,
688 unsigned int mode, void *context)
690 struct string_list_item *it;
691 struct wt_status_change_data *d;
692 struct wt_status *s = context;
693 struct strbuf full_name = STRBUF_INIT;
695 if (S_ISDIR(mode))
696 return READ_TREE_RECURSIVE;
698 strbuf_add(&full_name, base->buf, base->len);
699 strbuf_addstr(&full_name, path);
700 it = string_list_insert(&s->change, full_name.buf);
701 d = it->util;
702 if (!d) {
703 CALLOC_ARRAY(d, 1);
704 it->util = d;
707 d->index_status = DIFF_STATUS_ADDED;
708 /* Leave {mode,oid}_head zero for adds. */
709 d->mode_index = mode;
710 oidcpy(&d->oid_index, oid);
711 s->committable = 1;
712 strbuf_release(&full_name);
713 return 0;
716 static void wt_status_collect_changes_initial(struct wt_status *s)
718 struct index_state *istate = s->repo->index;
719 int i;
721 for (i = 0; i < istate->cache_nr; i++) {
722 struct string_list_item *it;
723 struct wt_status_change_data *d;
724 const struct cache_entry *ce = istate->cache[i];
726 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
727 continue;
728 if (ce_intent_to_add(ce))
729 continue;
730 if (S_ISSPARSEDIR(ce->ce_mode)) {
732 * This is a sparse directory entry, so we want to collect all
733 * of the added files within the tree. This requires recursively
734 * expanding the trees to find the elements that are new in this
735 * tree and marking them with DIFF_STATUS_ADDED.
737 struct strbuf base = STRBUF_INIT;
738 struct pathspec ps = { 0 };
739 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
741 ps.recursive = 1;
742 ps.has_wildcard = 1;
743 ps.max_depth = -1;
745 strbuf_add(&base, ce->name, ce->ce_namelen);
746 read_tree_at(istate->repo, tree, &base, 0, &ps,
747 add_file_to_list, s);
748 continue;
751 it = string_list_insert(&s->change, ce->name);
752 d = it->util;
753 if (!d) {
754 CALLOC_ARRAY(d, 1);
755 it->util = d;
757 if (ce_stage(ce)) {
758 d->index_status = DIFF_STATUS_UNMERGED;
759 d->stagemask |= (1 << (ce_stage(ce) - 1));
761 * Don't bother setting {mode,oid}_{head,index} since the print
762 * code will output the stage values directly and not use the
763 * values in these fields.
765 s->committable = 1;
766 } else {
767 d->index_status = DIFF_STATUS_ADDED;
768 /* Leave {mode,oid}_head zero for adds. */
769 d->mode_index = ce->ce_mode;
770 oidcpy(&d->oid_index, &ce->oid);
771 s->committable = 1;
776 static void wt_status_collect_untracked(struct wt_status *s)
778 int i;
779 struct dir_struct dir = DIR_INIT;
780 uint64_t t_begin = getnanotime();
781 struct index_state *istate = s->repo->index;
783 if (!s->show_untracked_files)
784 return;
786 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
787 dir.flags |=
788 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
789 if (s->show_ignored_mode) {
790 dir.flags |= DIR_SHOW_IGNORED_TOO;
792 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
793 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
794 } else {
795 dir.untracked = istate->untracked;
798 setup_standard_excludes(&dir);
800 fill_directory(&dir, istate, &s->pathspec);
802 for (i = 0; i < dir.nr; i++) {
803 struct dir_entry *ent = dir.entries[i];
804 if (index_name_is_other(istate, ent->name, ent->len))
805 string_list_insert(&s->untracked, ent->name);
808 for (i = 0; i < dir.ignored_nr; i++) {
809 struct dir_entry *ent = dir.ignored[i];
810 if (index_name_is_other(istate, ent->name, ent->len))
811 string_list_insert(&s->ignored, ent->name);
814 dir_clear(&dir);
816 if (advice_enabled(ADVICE_STATUS_U_OPTION))
817 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
820 static int has_unmerged(struct wt_status *s)
822 int i;
824 for (i = 0; i < s->change.nr; i++) {
825 struct wt_status_change_data *d;
826 d = s->change.items[i].util;
827 if (d->stagemask)
828 return 1;
830 return 0;
833 void wt_status_collect(struct wt_status *s)
835 trace2_region_enter("status", "worktrees", s->repo);
836 wt_status_collect_changes_worktree(s);
837 trace2_region_leave("status", "worktrees", s->repo);
839 if (s->is_initial) {
840 trace2_region_enter("status", "initial", s->repo);
841 wt_status_collect_changes_initial(s);
842 trace2_region_leave("status", "initial", s->repo);
843 } else {
844 trace2_region_enter("status", "index", s->repo);
845 wt_status_collect_changes_index(s);
846 trace2_region_leave("status", "index", s->repo);
849 trace2_region_enter("status", "untracked", s->repo);
850 wt_status_collect_untracked(s);
851 trace2_region_leave("status", "untracked", s->repo);
853 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
854 if (s->state.merge_in_progress && !has_unmerged(s))
855 s->committable = 1;
858 void wt_status_collect_free_buffers(struct wt_status *s)
860 wt_status_state_free_buffers(&s->state);
863 void wt_status_state_free_buffers(struct wt_status_state *state)
865 FREE_AND_NULL(state->branch);
866 FREE_AND_NULL(state->onto);
867 FREE_AND_NULL(state->detached_from);
868 FREE_AND_NULL(state->bisecting_from);
871 static void wt_longstatus_print_unmerged(struct wt_status *s)
873 int shown_header = 0;
874 int i;
876 for (i = 0; i < s->change.nr; i++) {
877 struct wt_status_change_data *d;
878 struct string_list_item *it;
879 it = &(s->change.items[i]);
880 d = it->util;
881 if (!d->stagemask)
882 continue;
883 if (!shown_header) {
884 wt_longstatus_print_unmerged_header(s);
885 shown_header = 1;
887 wt_longstatus_print_unmerged_data(s, it);
889 if (shown_header)
890 wt_longstatus_print_trailer(s);
894 static void wt_longstatus_print_updated(struct wt_status *s)
896 int shown_header = 0;
897 int i;
899 for (i = 0; i < s->change.nr; i++) {
900 struct wt_status_change_data *d;
901 struct string_list_item *it;
902 it = &(s->change.items[i]);
903 d = it->util;
904 if (!d->index_status ||
905 d->index_status == DIFF_STATUS_UNMERGED)
906 continue;
907 if (!shown_header) {
908 wt_longstatus_print_cached_header(s);
909 shown_header = 1;
911 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
913 if (shown_header)
914 wt_longstatus_print_trailer(s);
918 * -1 : has delete
919 * 0 : no change
920 * 1 : some change but no delete
922 static int wt_status_check_worktree_changes(struct wt_status *s,
923 int *dirty_submodules)
925 int i;
926 int changes = 0;
928 *dirty_submodules = 0;
930 for (i = 0; i < s->change.nr; i++) {
931 struct wt_status_change_data *d;
932 d = s->change.items[i].util;
933 if (!d->worktree_status ||
934 d->worktree_status == DIFF_STATUS_UNMERGED)
935 continue;
936 if (!changes)
937 changes = 1;
938 if (d->dirty_submodule)
939 *dirty_submodules = 1;
940 if (d->worktree_status == DIFF_STATUS_DELETED)
941 changes = -1;
943 return changes;
946 static void wt_longstatus_print_changed(struct wt_status *s)
948 int i, dirty_submodules;
949 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
951 if (!worktree_changes)
952 return;
954 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
956 for (i = 0; i < s->change.nr; i++) {
957 struct wt_status_change_data *d;
958 struct string_list_item *it;
959 it = &(s->change.items[i]);
960 d = it->util;
961 if (!d->worktree_status ||
962 d->worktree_status == DIFF_STATUS_UNMERGED)
963 continue;
964 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
966 wt_longstatus_print_trailer(s);
969 static int stash_count_refs(struct object_id *ooid UNUSED,
970 struct object_id *noid UNUSED,
971 const char *email UNUSED,
972 timestamp_t timestamp UNUSED, int tz UNUSED,
973 const char *message UNUSED, void *cb_data)
975 int *c = cb_data;
976 (*c)++;
977 return 0;
980 static int count_stash_entries(void)
982 int n = 0;
983 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
984 "refs/stash", stash_count_refs, &n);
985 return n;
988 static void wt_longstatus_print_stash_summary(struct wt_status *s)
990 int stash_count = count_stash_entries();
992 if (stash_count > 0)
993 status_printf_ln(s, GIT_COLOR_NORMAL,
994 Q_("Your stash currently has %d entry",
995 "Your stash currently has %d entries", stash_count),
996 stash_count);
999 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
1001 struct child_process sm_summary = CHILD_PROCESS_INIT;
1002 struct strbuf cmd_stdout = STRBUF_INIT;
1003 struct strbuf summary = STRBUF_INIT;
1004 char *summary_content;
1006 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1008 strvec_push(&sm_summary.args, "submodule");
1009 strvec_push(&sm_summary.args, "summary");
1010 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1011 strvec_push(&sm_summary.args, "--for-status");
1012 strvec_push(&sm_summary.args, "--summary-limit");
1013 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1014 if (!uncommitted)
1015 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1017 sm_summary.git_cmd = 1;
1018 sm_summary.no_stdin = 1;
1020 capture_command(&sm_summary, &cmd_stdout, 1024);
1022 /* prepend header, only if there's an actual output */
1023 if (cmd_stdout.len) {
1024 if (uncommitted)
1025 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1026 else
1027 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1028 strbuf_addstr(&summary, "\n\n");
1030 strbuf_addbuf(&summary, &cmd_stdout);
1031 strbuf_release(&cmd_stdout);
1033 if (s->display_comment_prefix) {
1034 size_t len;
1035 summary_content = strbuf_detach(&summary, &len);
1036 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_str);
1037 free(summary_content);
1040 fputs(summary.buf, s->fp);
1041 strbuf_release(&summary);
1044 static void wt_longstatus_print_other(struct wt_status *s,
1045 struct string_list *l,
1046 const char *what,
1047 const char *how)
1049 int i;
1050 struct strbuf buf = STRBUF_INIT;
1051 static struct string_list output = STRING_LIST_INIT_DUP;
1052 struct column_options copts;
1054 if (!l->nr)
1055 return;
1057 wt_longstatus_print_other_header(s, what, how);
1059 for (i = 0; i < l->nr; i++) {
1060 struct string_list_item *it;
1061 const char *path;
1062 it = &(l->items[i]);
1063 path = quote_path(it->string, s->prefix, &buf, 0);
1064 if (column_active(s->colopts)) {
1065 string_list_append(&output, path);
1066 continue;
1068 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1069 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1070 "%s\n", path);
1073 strbuf_release(&buf);
1074 if (!column_active(s->colopts))
1075 goto conclude;
1077 strbuf_addf(&buf, "%s%s\t%s",
1078 color(WT_STATUS_HEADER, s),
1079 s->display_comment_prefix ? "#" : "",
1080 color(WT_STATUS_UNTRACKED, s));
1081 memset(&copts, 0, sizeof(copts));
1082 copts.padding = 1;
1083 copts.indent = buf.buf;
1084 if (want_color(s->use_color))
1085 copts.nl = GIT_COLOR_RESET "\n";
1086 print_columns(&output, s->colopts, &copts);
1087 string_list_clear(&output, 0);
1088 strbuf_release(&buf);
1089 conclude:
1090 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1093 size_t wt_status_locate_end(const char *s, size_t len)
1095 const char *p;
1096 struct strbuf pattern = STRBUF_INIT;
1098 strbuf_addf(&pattern, "\n%s %s", comment_line_str, cut_line);
1099 if (starts_with(s, pattern.buf + 1))
1100 len = 0;
1101 else if ((p = strstr(s, pattern.buf))) {
1102 size_t newlen = p - s + 1;
1103 if (newlen < len)
1104 len = newlen;
1106 strbuf_release(&pattern);
1107 return len;
1110 void wt_status_append_cut_line(struct strbuf *buf)
1112 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1114 strbuf_commented_addf(buf, comment_line_str, "%s", cut_line);
1115 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_str);
1118 void wt_status_add_cut_line(struct wt_status *s)
1120 struct strbuf buf = STRBUF_INIT;
1122 if (s->added_cut_line)
1123 return;
1124 s->added_cut_line = 1;
1125 wt_status_append_cut_line(&buf);
1126 fputs(buf.buf, s->fp);
1127 strbuf_release(&buf);
1130 static void wt_longstatus_print_verbose(struct wt_status *s)
1132 struct rev_info rev;
1133 struct setup_revision_opt opt;
1134 int dirty_submodules;
1135 const char *c = color(WT_STATUS_HEADER, s);
1137 repo_init_revisions(s->repo, &rev, NULL);
1138 rev.diffopt.flags.allow_textconv = 1;
1139 rev.diffopt.ita_invisible_in_index = 1;
1141 memset(&opt, 0, sizeof(opt));
1142 opt.def = s->is_initial ? empty_tree_oid_hex(the_repository->hash_algo) : s->reference;
1143 setup_revisions(0, NULL, &rev, &opt);
1145 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1146 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1147 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1148 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1149 rev.diffopt.file = s->fp;
1150 rev.diffopt.close_file = 0;
1152 * If we're not going to stdout, then we definitely don't
1153 * want color, since we are going to the commit message
1154 * file (and even the "auto" setting won't work, since it
1155 * will have checked isatty on stdout). But we then do want
1156 * to insert the scissor line here to reliably remove the
1157 * diff before committing, if we didn't already include one
1158 * before.
1160 if (s->fp != stdout) {
1161 rev.diffopt.use_color = 0;
1162 wt_status_add_cut_line(s);
1164 if (s->verbose > 1 && s->committable) {
1165 /* print_updated() printed a header, so do we */
1166 if (s->fp != stdout)
1167 wt_longstatus_print_trailer(s);
1168 status_printf_ln(s, c, _("Changes to be committed:"));
1169 rev.diffopt.a_prefix = "c/";
1170 rev.diffopt.b_prefix = "i/";
1171 } /* else use prefix as per user config */
1172 run_diff_index(&rev, DIFF_INDEX_CACHED);
1173 if (s->verbose > 1 &&
1174 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1175 status_printf_ln(s, c,
1176 "--------------------------------------------------");
1177 status_printf_ln(s, c, _("Changes not staged for commit:"));
1178 setup_work_tree();
1179 rev.diffopt.a_prefix = "i/";
1180 rev.diffopt.b_prefix = "w/";
1181 run_diff_files(&rev, 0);
1183 release_revisions(&rev);
1186 static void wt_longstatus_print_tracking(struct wt_status *s)
1188 struct strbuf sb = STRBUF_INIT;
1189 const char *cp, *ep, *branch_name;
1190 struct branch *branch;
1191 uint64_t t_begin = 0;
1193 assert(s->branch && !s->is_initial);
1194 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1195 return;
1196 branch = branch_get(branch_name);
1198 t_begin = getnanotime();
1200 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1201 !s->commit_template))
1202 return;
1204 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1205 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1206 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1207 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1208 strbuf_addf(&sb, _("\n"
1209 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1210 "You can use '--no-ahead-behind' to avoid this.\n"),
1211 t_delta_in_ms / 1000.0);
1215 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1216 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1217 "%s%s%.*s",
1218 s->display_comment_prefix ? comment_line_str : "",
1219 s->display_comment_prefix ? " " : "",
1220 (int)(ep - cp), cp);
1221 if (s->display_comment_prefix)
1222 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%s",
1223 comment_line_str);
1224 else
1225 fputs("\n", s->fp);
1226 strbuf_release(&sb);
1229 static int uf_was_slow(struct wt_status *s)
1231 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1232 s->untracked_in_ms = 3250;
1233 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1236 static void show_merge_in_progress(struct wt_status *s,
1237 const char *color)
1239 if (has_unmerged(s)) {
1240 status_printf_ln(s, color, _("You have unmerged paths."));
1241 if (s->hints) {
1242 status_printf_ln(s, color,
1243 _(" (fix conflicts and run \"git commit\")"));
1244 status_printf_ln(s, color,
1245 _(" (use \"git merge --abort\" to abort the merge)"));
1247 } else {
1248 status_printf_ln(s, color,
1249 _("All conflicts fixed but you are still merging."));
1250 if (s->hints)
1251 status_printf_ln(s, color,
1252 _(" (use \"git commit\" to conclude merge)"));
1254 wt_longstatus_print_trailer(s);
1257 static void show_am_in_progress(struct wt_status *s,
1258 const char *color)
1260 int am_empty_patch;
1262 status_printf_ln(s, color,
1263 _("You are in the middle of an am session."));
1264 if (s->state.am_empty_patch)
1265 status_printf_ln(s, color,
1266 _("The current patch is empty."));
1267 if (s->hints) {
1268 am_empty_patch = s->state.am_empty_patch;
1269 if (!am_empty_patch)
1270 status_printf_ln(s, color,
1271 _(" (fix conflicts and then run \"git am --continue\")"));
1272 status_printf_ln(s, color,
1273 _(" (use \"git am --skip\" to skip this patch)"));
1274 if (am_empty_patch)
1275 status_printf_ln(s, color,
1276 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1277 status_printf_ln(s, color,
1278 _(" (use \"git am --abort\" to restore the original branch)"));
1280 wt_longstatus_print_trailer(s);
1283 static char *read_line_from_git_path(const char *filename)
1285 struct strbuf buf = STRBUF_INIT;
1286 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1288 if (!fp) {
1289 strbuf_release(&buf);
1290 return NULL;
1292 strbuf_getline_lf(&buf, fp);
1293 if (!fclose(fp)) {
1294 return strbuf_detach(&buf, NULL);
1295 } else {
1296 strbuf_release(&buf);
1297 return NULL;
1301 static int split_commit_in_progress(struct wt_status *s)
1303 int split_in_progress = 0;
1304 struct object_id head_oid, orig_head_oid;
1305 char *rebase_amend, *rebase_orig_head;
1306 int head_flags, orig_head_flags;
1308 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1309 !s->branch || strcmp(s->branch, "HEAD"))
1310 return 0;
1312 if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1313 &head_oid, &head_flags) ||
1314 refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1315 &orig_head_oid, &orig_head_flags))
1316 return 0;
1317 if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1318 return 0;
1320 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1321 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1323 if (!rebase_amend || !rebase_orig_head)
1324 ; /* fall through, no split in progress */
1325 else if (!strcmp(rebase_amend, rebase_orig_head))
1326 split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1327 else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
1328 split_in_progress = 1;
1330 free(rebase_amend);
1331 free(rebase_orig_head);
1333 return split_in_progress;
1337 * Turn
1338 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1339 * into
1340 * "pick d6a2f03 some message"
1342 * The function assumes that the line does not contain useless spaces
1343 * before or after the command.
1345 static void abbrev_oid_in_line(struct strbuf *line)
1347 struct strbuf **split;
1348 int i;
1350 if (starts_with(line->buf, "exec ") ||
1351 starts_with(line->buf, "x ") ||
1352 starts_with(line->buf, "label ") ||
1353 starts_with(line->buf, "l "))
1354 return;
1356 split = strbuf_split_max(line, ' ', 3);
1357 if (split[0] && split[1]) {
1358 struct object_id oid;
1361 * strbuf_split_max left a space. Trim it and re-add
1362 * it after abbreviation.
1364 strbuf_trim(split[1]);
1365 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1366 strbuf_reset(split[1]);
1367 strbuf_add_unique_abbrev(split[1], &oid,
1368 DEFAULT_ABBREV);
1369 strbuf_addch(split[1], ' ');
1370 strbuf_reset(line);
1371 for (i = 0; split[i]; i++)
1372 strbuf_addbuf(line, split[i]);
1375 strbuf_list_free(split);
1378 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1380 struct strbuf line = STRBUF_INIT;
1381 FILE *f = fopen(git_path("%s", fname), "r");
1383 if (!f) {
1384 if (errno == ENOENT)
1385 return -1;
1386 die_errno("Could not open file %s for reading",
1387 git_path("%s", fname));
1389 while (!strbuf_getline_lf(&line, f)) {
1390 if (starts_with(line.buf, comment_line_str))
1391 continue;
1392 strbuf_trim(&line);
1393 if (!line.len)
1394 continue;
1395 abbrev_oid_in_line(&line);
1396 string_list_append(lines, line.buf);
1398 fclose(f);
1399 strbuf_release(&line);
1400 return 0;
1403 static void show_rebase_information(struct wt_status *s,
1404 const char *color)
1406 if (s->state.rebase_interactive_in_progress) {
1407 int i;
1408 int nr_lines_to_show = 2;
1410 struct string_list have_done = STRING_LIST_INIT_DUP;
1411 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1413 read_rebase_todolist("rebase-merge/done", &have_done);
1414 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1415 &yet_to_do))
1416 status_printf_ln(s, color,
1417 _("git-rebase-todo is missing."));
1418 if (have_done.nr == 0)
1419 status_printf_ln(s, color, _("No commands done."));
1420 else {
1421 status_printf_ln(s, color,
1422 Q_("Last command done (%"PRIuMAX" command done):",
1423 "Last commands done (%"PRIuMAX" commands done):",
1424 have_done.nr),
1425 (uintmax_t)have_done.nr);
1426 for (i = (have_done.nr > nr_lines_to_show)
1427 ? have_done.nr - nr_lines_to_show : 0;
1428 i < have_done.nr;
1429 i++)
1430 status_printf_ln(s, color, " %s", have_done.items[i].string);
1431 if (have_done.nr > nr_lines_to_show && s->hints)
1432 status_printf_ln(s, color,
1433 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1436 if (yet_to_do.nr == 0)
1437 status_printf_ln(s, color,
1438 _("No commands remaining."));
1439 else {
1440 status_printf_ln(s, color,
1441 Q_("Next command to do (%"PRIuMAX" remaining command):",
1442 "Next commands to do (%"PRIuMAX" remaining commands):",
1443 yet_to_do.nr),
1444 (uintmax_t)yet_to_do.nr);
1445 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1446 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1447 if (s->hints)
1448 status_printf_ln(s, color,
1449 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1451 string_list_clear(&yet_to_do, 0);
1452 string_list_clear(&have_done, 0);
1456 static void print_rebase_state(struct wt_status *s,
1457 const char *color)
1459 if (s->state.branch)
1460 status_printf_ln(s, color,
1461 _("You are currently rebasing branch '%s' on '%s'."),
1462 s->state.branch,
1463 s->state.onto);
1464 else
1465 status_printf_ln(s, color,
1466 _("You are currently rebasing."));
1469 static void show_rebase_in_progress(struct wt_status *s,
1470 const char *color)
1472 struct stat st;
1474 show_rebase_information(s, color);
1475 if (has_unmerged(s)) {
1476 print_rebase_state(s, color);
1477 if (s->hints) {
1478 status_printf_ln(s, color,
1479 _(" (fix conflicts and then run \"git rebase --continue\")"));
1480 status_printf_ln(s, color,
1481 _(" (use \"git rebase --skip\" to skip this patch)"));
1482 status_printf_ln(s, color,
1483 _(" (use \"git rebase --abort\" to check out the original branch)"));
1485 } else if (s->state.rebase_in_progress ||
1486 !stat(git_path_merge_msg(s->repo), &st)) {
1487 print_rebase_state(s, color);
1488 if (s->hints)
1489 status_printf_ln(s, color,
1490 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1491 } else if (split_commit_in_progress(s)) {
1492 if (s->state.branch)
1493 status_printf_ln(s, color,
1494 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1495 s->state.branch,
1496 s->state.onto);
1497 else
1498 status_printf_ln(s, color,
1499 _("You are currently splitting a commit during a rebase."));
1500 if (s->hints)
1501 status_printf_ln(s, color,
1502 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1503 } else {
1504 if (s->state.branch)
1505 status_printf_ln(s, color,
1506 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1507 s->state.branch,
1508 s->state.onto);
1509 else
1510 status_printf_ln(s, color,
1511 _("You are currently editing a commit during a rebase."));
1512 if (s->hints && !s->amend) {
1513 status_printf_ln(s, color,
1514 _(" (use \"git commit --amend\" to amend the current commit)"));
1515 status_printf_ln(s, color,
1516 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1519 wt_longstatus_print_trailer(s);
1522 static void show_cherry_pick_in_progress(struct wt_status *s,
1523 const char *color)
1525 if (is_null_oid(&s->state.cherry_pick_head_oid))
1526 status_printf_ln(s, color,
1527 _("Cherry-pick currently in progress."));
1528 else
1529 status_printf_ln(s, color,
1530 _("You are currently cherry-picking commit %s."),
1531 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1532 DEFAULT_ABBREV));
1534 if (s->hints) {
1535 if (has_unmerged(s))
1536 status_printf_ln(s, color,
1537 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1538 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1539 status_printf_ln(s, color,
1540 _(" (run \"git cherry-pick --continue\" to continue)"));
1541 else
1542 status_printf_ln(s, color,
1543 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1544 status_printf_ln(s, color,
1545 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1546 status_printf_ln(s, color,
1547 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1549 wt_longstatus_print_trailer(s);
1552 static void show_revert_in_progress(struct wt_status *s,
1553 const char *color)
1555 if (is_null_oid(&s->state.revert_head_oid))
1556 status_printf_ln(s, color,
1557 _("Revert currently in progress."));
1558 else
1559 status_printf_ln(s, color,
1560 _("You are currently reverting commit %s."),
1561 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1562 DEFAULT_ABBREV));
1563 if (s->hints) {
1564 if (has_unmerged(s))
1565 status_printf_ln(s, color,
1566 _(" (fix conflicts and run \"git revert --continue\")"));
1567 else if (is_null_oid(&s->state.revert_head_oid))
1568 status_printf_ln(s, color,
1569 _(" (run \"git revert --continue\" to continue)"));
1570 else
1571 status_printf_ln(s, color,
1572 _(" (all conflicts fixed: run \"git revert --continue\")"));
1573 status_printf_ln(s, color,
1574 _(" (use \"git revert --skip\" to skip this patch)"));
1575 status_printf_ln(s, color,
1576 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1578 wt_longstatus_print_trailer(s);
1581 static void show_bisect_in_progress(struct wt_status *s,
1582 const char *color)
1584 if (s->state.bisecting_from)
1585 status_printf_ln(s, color,
1586 _("You are currently bisecting, started from branch '%s'."),
1587 s->state.bisecting_from);
1588 else
1589 status_printf_ln(s, color,
1590 _("You are currently bisecting."));
1591 if (s->hints)
1592 status_printf_ln(s, color,
1593 _(" (use \"git bisect reset\" to get back to the original branch)"));
1594 wt_longstatus_print_trailer(s);
1597 static void show_sparse_checkout_in_use(struct wt_status *s,
1598 const char *color)
1600 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1601 return;
1603 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1604 status_printf_ln(s, color, _("You are in a sparse checkout."));
1605 else
1606 status_printf_ln(s, color,
1607 _("You are in a sparse checkout with %d%% of tracked files present."),
1608 s->state.sparse_checkout_percentage);
1609 wt_longstatus_print_trailer(s);
1613 * Extract branch information from rebase/bisect
1615 static char *get_branch(const struct worktree *wt, const char *path)
1617 struct strbuf sb = STRBUF_INIT;
1618 struct object_id oid;
1619 const char *branch_name;
1621 if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1622 goto got_nothing;
1624 while (sb.len && sb.buf[sb.len - 1] == '\n')
1625 strbuf_setlen(&sb, sb.len - 1);
1626 if (!sb.len)
1627 goto got_nothing;
1628 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1629 strbuf_remove(&sb, 0, branch_name - sb.buf);
1630 else if (starts_with(sb.buf, "refs/"))
1632 else if (!get_oid_hex(sb.buf, &oid)) {
1633 strbuf_reset(&sb);
1634 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1635 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1636 goto got_nothing;
1637 else /* bisect */
1639 return strbuf_detach(&sb, NULL);
1641 got_nothing:
1642 strbuf_release(&sb);
1643 return NULL;
1646 struct grab_1st_switch_cbdata {
1647 struct strbuf buf;
1648 struct object_id noid;
1651 static int grab_1st_switch(struct object_id *ooid UNUSED,
1652 struct object_id *noid,
1653 const char *email UNUSED,
1654 timestamp_t timestamp UNUSED, int tz UNUSED,
1655 const char *message, void *cb_data)
1657 struct grab_1st_switch_cbdata *cb = cb_data;
1658 const char *target = NULL, *end;
1660 if (!skip_prefix(message, "checkout: moving from ", &message))
1661 return 0;
1662 target = strstr(message, " to ");
1663 if (!target)
1664 return 0;
1665 target += strlen(" to ");
1666 strbuf_reset(&cb->buf);
1667 oidcpy(&cb->noid, noid);
1668 end = strchrnul(target, '\n');
1669 strbuf_add(&cb->buf, target, end - target);
1670 if (!strcmp(cb->buf.buf, "HEAD")) {
1671 /* HEAD is relative. Resolve it to the right reflog entry. */
1672 strbuf_reset(&cb->buf);
1673 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1675 return 1;
1678 static void wt_status_get_detached_from(struct repository *r,
1679 struct wt_status_state *state)
1681 struct grab_1st_switch_cbdata cb;
1682 struct commit *commit;
1683 struct object_id oid;
1684 char *ref = NULL;
1686 strbuf_init(&cb.buf, 0);
1687 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) {
1688 strbuf_release(&cb.buf);
1689 return;
1692 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
1693 1) == 1 &&
1694 /* oid is a commit? match without further lookup */
1695 (oideq(&cb.noid, &oid) ||
1696 /* perhaps oid is a tag, try to dereference to a commit */
1697 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1698 oideq(&cb.noid, &commit->object.oid)))) {
1699 const char *from = ref;
1700 if (!skip_prefix(from, "refs/tags/", &from))
1701 skip_prefix(from, "refs/remotes/", &from);
1702 state->detached_from = xstrdup(from);
1703 } else
1704 state->detached_from =
1705 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1706 oidcpy(&state->detached_oid, &cb.noid);
1707 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1708 oideq(&oid, &state->detached_oid);
1710 free(ref);
1711 strbuf_release(&cb.buf);
1714 int wt_status_check_rebase(const struct worktree *wt,
1715 struct wt_status_state *state)
1717 struct stat st;
1719 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1720 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1721 state->am_in_progress = 1;
1722 if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1723 state->am_empty_patch = 1;
1724 } else {
1725 state->rebase_in_progress = 1;
1726 state->branch = get_branch(wt, "rebase-apply/head-name");
1727 state->onto = get_branch(wt, "rebase-apply/onto");
1729 } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1730 if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1731 state->rebase_interactive_in_progress = 1;
1732 else
1733 state->rebase_in_progress = 1;
1734 state->branch = get_branch(wt, "rebase-merge/head-name");
1735 state->onto = get_branch(wt, "rebase-merge/onto");
1736 } else
1737 return 0;
1738 return 1;
1741 int wt_status_check_bisect(const struct worktree *wt,
1742 struct wt_status_state *state)
1744 struct stat st;
1746 if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1747 state->bisect_in_progress = 1;
1748 state->bisecting_from = get_branch(wt, "BISECT_START");
1749 return 1;
1751 return 0;
1754 static void wt_status_check_sparse_checkout(struct repository *r,
1755 struct wt_status_state *state)
1757 int skip_worktree = 0;
1758 int i;
1760 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1762 * Don't compute percentage of checked out files if we
1763 * aren't in a sparse checkout or would get division by 0.
1765 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1766 return;
1769 if (r->index->sparse_index) {
1770 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1771 return;
1774 for (i = 0; i < r->index->cache_nr; i++) {
1775 struct cache_entry *ce = r->index->cache[i];
1776 if (ce_skip_worktree(ce))
1777 skip_worktree++;
1780 state->sparse_checkout_percentage =
1781 100 - (100 * skip_worktree)/r->index->cache_nr;
1784 void wt_status_get_state(struct repository *r,
1785 struct wt_status_state *state,
1786 int get_detached_from)
1788 struct stat st;
1789 struct object_id oid;
1790 enum replay_action action;
1792 if (!stat(git_path_merge_head(r), &st)) {
1793 wt_status_check_rebase(NULL, state);
1794 state->merge_in_progress = 1;
1795 } else if (wt_status_check_rebase(NULL, state)) {
1796 ; /* all set */
1797 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1798 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1799 state->cherry_pick_in_progress = 1;
1800 oidcpy(&state->cherry_pick_head_oid, &oid);
1802 wt_status_check_bisect(NULL, state);
1803 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1804 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
1805 state->revert_in_progress = 1;
1806 oidcpy(&state->revert_head_oid, &oid);
1808 if (!sequencer_get_last_command(r, &action)) {
1809 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1810 state->cherry_pick_in_progress = 1;
1811 oidcpy(&state->cherry_pick_head_oid, null_oid());
1812 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1813 state->revert_in_progress = 1;
1814 oidcpy(&state->revert_head_oid, null_oid());
1817 if (get_detached_from)
1818 wt_status_get_detached_from(r, state);
1819 wt_status_check_sparse_checkout(r, state);
1822 static void wt_longstatus_print_state(struct wt_status *s)
1824 const char *state_color = color(WT_STATUS_HEADER, s);
1825 struct wt_status_state *state = &s->state;
1827 if (state->merge_in_progress) {
1828 if (state->rebase_interactive_in_progress) {
1829 show_rebase_information(s, state_color);
1830 fputs("\n", s->fp);
1832 show_merge_in_progress(s, state_color);
1833 } else if (state->am_in_progress)
1834 show_am_in_progress(s, state_color);
1835 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1836 show_rebase_in_progress(s, state_color);
1837 else if (state->cherry_pick_in_progress)
1838 show_cherry_pick_in_progress(s, state_color);
1839 else if (state->revert_in_progress)
1840 show_revert_in_progress(s, state_color);
1841 if (state->bisect_in_progress)
1842 show_bisect_in_progress(s, state_color);
1844 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1845 show_sparse_checkout_in_use(s, state_color);
1848 static void wt_longstatus_print(struct wt_status *s)
1850 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1851 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1852 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1854 if (s->branch) {
1855 const char *on_what = _("On branch ");
1856 const char *branch_name = s->branch;
1857 if (!strcmp(branch_name, "HEAD")) {
1858 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1859 if (s->state.rebase_in_progress ||
1860 s->state.rebase_interactive_in_progress) {
1861 if (s->state.rebase_interactive_in_progress)
1862 on_what = _("interactive rebase in progress; onto ");
1863 else
1864 on_what = _("rebase in progress; onto ");
1865 branch_name = s->state.onto;
1866 } else if (s->state.detached_from) {
1867 branch_name = s->state.detached_from;
1868 if (s->state.detached_at)
1869 on_what = _("HEAD detached at ");
1870 else
1871 on_what = _("HEAD detached from ");
1872 } else {
1873 branch_name = "";
1874 on_what = _("Not currently on any branch.");
1876 } else
1877 skip_prefix(branch_name, "refs/heads/", &branch_name);
1878 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1879 status_printf_more(s, branch_status_color, "%s", on_what);
1880 status_printf_more(s, branch_color, "%s\n", branch_name);
1881 if (!s->is_initial)
1882 wt_longstatus_print_tracking(s);
1885 wt_longstatus_print_state(s);
1887 if (s->is_initial) {
1888 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1889 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1890 s->commit_template
1891 ? _("Initial commit")
1892 : _("No commits yet"));
1893 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1896 wt_longstatus_print_updated(s);
1897 wt_longstatus_print_unmerged(s);
1898 wt_longstatus_print_changed(s);
1899 if (s->submodule_summary &&
1900 (!s->ignore_submodule_arg ||
1901 strcmp(s->ignore_submodule_arg, "all"))) {
1902 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1903 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1905 if (s->show_untracked_files) {
1906 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1907 if (s->show_ignored_mode)
1908 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1909 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1910 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1911 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1912 status_printf_ln(s, GIT_COLOR_NORMAL,
1913 _("It took %.2f seconds to enumerate untracked files,\n"
1914 "but the results were cached, and subsequent runs may be faster."),
1915 s->untracked_in_ms / 1000.0);
1916 } else {
1917 status_printf_ln(s, GIT_COLOR_NORMAL,
1918 _("It took %.2f seconds to enumerate untracked files."),
1919 s->untracked_in_ms / 1000.0);
1921 status_printf_ln(s, GIT_COLOR_NORMAL,
1922 _("See 'git help status' for information on how to improve this."));
1923 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1925 } else if (s->committable)
1926 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1927 s->hints
1928 ? _(" (use -u option to show untracked files)") : "");
1930 if (s->verbose)
1931 wt_longstatus_print_verbose(s);
1932 if (!s->committable) {
1933 if (s->amend)
1934 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1935 else if (s->nowarn)
1936 ; /* nothing */
1937 else if (s->workdir_dirty) {
1938 if (s->hints)
1939 fprintf(s->fp, _("no changes added to commit "
1940 "(use \"git add\" and/or "
1941 "\"git commit -a\")\n"));
1942 else
1943 fprintf(s->fp, _("no changes added to "
1944 "commit\n"));
1945 } else if (s->untracked.nr) {
1946 if (s->hints)
1947 fprintf(s->fp, _("nothing added to commit but "
1948 "untracked files present (use "
1949 "\"git add\" to track)\n"));
1950 else
1951 fprintf(s->fp, _("nothing added to commit but "
1952 "untracked files present\n"));
1953 } else if (s->is_initial) {
1954 if (s->hints)
1955 fprintf(s->fp, _("nothing to commit (create/"
1956 "copy files and use \"git "
1957 "add\" to track)\n"));
1958 else
1959 fprintf(s->fp, _("nothing to commit\n"));
1960 } else if (!s->show_untracked_files) {
1961 if (s->hints)
1962 fprintf(s->fp, _("nothing to commit (use -u to "
1963 "show untracked files)\n"));
1964 else
1965 fprintf(s->fp, _("nothing to commit\n"));
1966 } else
1967 fprintf(s->fp, _("nothing to commit, working tree "
1968 "clean\n"));
1970 if(s->show_stash)
1971 wt_longstatus_print_stash_summary(s);
1974 static void wt_shortstatus_unmerged(struct string_list_item *it,
1975 struct wt_status *s)
1977 struct wt_status_change_data *d = it->util;
1978 const char *how = "??";
1980 switch (d->stagemask) {
1981 case 1: how = "DD"; break; /* both deleted */
1982 case 2: how = "AU"; break; /* added by us */
1983 case 3: how = "UD"; break; /* deleted by them */
1984 case 4: how = "UA"; break; /* added by them */
1985 case 5: how = "DU"; break; /* deleted by us */
1986 case 6: how = "AA"; break; /* both added */
1987 case 7: how = "UU"; break; /* both modified */
1989 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1990 if (s->null_termination) {
1991 fprintf(s->fp, " %s%c", it->string, 0);
1992 } else {
1993 struct strbuf onebuf = STRBUF_INIT;
1994 const char *one;
1995 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1996 fprintf(s->fp, " %s\n", one);
1997 strbuf_release(&onebuf);
2001 static void wt_shortstatus_status(struct string_list_item *it,
2002 struct wt_status *s)
2004 struct wt_status_change_data *d = it->util;
2006 if (d->index_status)
2007 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
2008 else
2009 fputc(' ', s->fp);
2010 if (d->worktree_status)
2011 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2012 else
2013 fputc(' ', s->fp);
2014 fputc(' ', s->fp);
2015 if (s->null_termination) {
2016 fprintf(s->fp, "%s%c", it->string, 0);
2017 if (d->rename_source)
2018 fprintf(s->fp, "%s%c", d->rename_source, 0);
2019 } else {
2020 struct strbuf onebuf = STRBUF_INIT;
2021 const char *one;
2023 if (d->rename_source) {
2024 one = quote_path(d->rename_source, s->prefix, &onebuf,
2025 QUOTE_PATH_QUOTE_SP);
2026 fprintf(s->fp, "%s -> ", one);
2027 strbuf_release(&onebuf);
2029 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2030 fprintf(s->fp, "%s\n", one);
2031 strbuf_release(&onebuf);
2035 static void wt_shortstatus_other(struct string_list_item *it,
2036 struct wt_status *s, const char *sign)
2038 if (s->null_termination) {
2039 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2040 } else {
2041 struct strbuf onebuf = STRBUF_INIT;
2042 const char *one;
2043 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2044 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2045 fprintf(s->fp, " %s\n", one);
2046 strbuf_release(&onebuf);
2050 static void wt_shortstatus_print_tracking(struct wt_status *s)
2052 struct branch *branch;
2053 const char *header_color = color(WT_STATUS_HEADER, s);
2054 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2055 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2057 const char *base;
2058 char *short_base;
2059 const char *branch_name;
2060 int num_ours, num_theirs, sti;
2061 int upstream_is_gone = 0;
2063 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2065 if (!s->branch)
2066 return;
2067 branch_name = s->branch;
2069 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2071 if (s->is_initial)
2072 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2074 if (!strcmp(s->branch, "HEAD")) {
2075 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2076 LABEL(N_("HEAD (no branch)")));
2077 goto conclude;
2080 skip_prefix(branch_name, "refs/heads/", &branch_name);
2082 branch = branch_get(branch_name);
2084 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2086 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2087 0, s->ahead_behind_flags);
2088 if (sti < 0) {
2089 if (!base)
2090 goto conclude;
2092 upstream_is_gone = 1;
2095 short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2096 base, 0);
2097 color_fprintf(s->fp, header_color, "...");
2098 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2099 free(short_base);
2101 if (!upstream_is_gone && !sti)
2102 goto conclude;
2104 color_fprintf(s->fp, header_color, " [");
2105 if (upstream_is_gone) {
2106 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2107 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2108 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2109 } else if (!num_ours) {
2110 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2111 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2112 } else if (!num_theirs) {
2113 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2114 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2115 } else {
2116 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2117 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2118 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2119 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2122 color_fprintf(s->fp, header_color, "]");
2123 conclude:
2124 fputc(s->null_termination ? '\0' : '\n', s->fp);
2127 static void wt_shortstatus_print(struct wt_status *s)
2129 struct string_list_item *it;
2131 if (s->show_branch)
2132 wt_shortstatus_print_tracking(s);
2134 for_each_string_list_item(it, &s->change) {
2135 struct wt_status_change_data *d = it->util;
2137 if (d->stagemask)
2138 wt_shortstatus_unmerged(it, s);
2139 else
2140 wt_shortstatus_status(it, s);
2142 for_each_string_list_item(it, &s->untracked)
2143 wt_shortstatus_other(it, s, "??");
2145 for_each_string_list_item(it, &s->ignored)
2146 wt_shortstatus_other(it, s, "!!");
2149 static void wt_porcelain_print(struct wt_status *s)
2151 s->use_color = 0;
2152 s->relative_paths = 0;
2153 s->prefix = NULL;
2154 s->no_gettext = 1;
2155 wt_shortstatus_print(s);
2159 * Print branch information for porcelain v2 output. These lines
2160 * are printed when the '--branch' parameter is given.
2162 * # branch.oid <commit><eol>
2163 * # branch.head <head><eol>
2164 * [# branch.upstream <upstream><eol>
2165 * [# branch.ab +<ahead> -<behind><eol>]]
2167 * <commit> ::= the current commit hash or the literal
2168 * "(initial)" to indicate an initialized repo
2169 * with no commits.
2171 * <head> ::= <branch_name> the current branch name or
2172 * "(detached)" literal when detached head or
2173 * "(unknown)" when something is wrong.
2175 * <upstream> ::= the upstream branch name, when set.
2177 * <ahead> ::= integer ahead value or '?'.
2179 * <behind> ::= integer behind value or '?'.
2181 * The end-of-line is defined by the -z flag.
2183 * <eol> ::= NUL when -z,
2184 * LF when NOT -z.
2186 * When an upstream is set and present, the 'branch.ab' line will
2187 * be printed with the ahead/behind counts for the branch and the
2188 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2189 * are different, '?' will be substituted for the actual count.
2191 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2193 struct branch *branch;
2194 const char *base;
2195 const char *branch_name;
2196 int ab_info, nr_ahead, nr_behind;
2197 char eol = s->null_termination ? '\0' : '\n';
2199 fprintf(s->fp, "# branch.oid %s%c",
2200 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2201 eol);
2203 if (!s->branch)
2204 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2205 else {
2206 if (!strcmp(s->branch, "HEAD")) {
2207 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2209 if (s->state.rebase_in_progress ||
2210 s->state.rebase_interactive_in_progress)
2211 branch_name = s->state.onto;
2212 else if (s->state.detached_from)
2213 branch_name = s->state.detached_from;
2214 else
2215 branch_name = "";
2216 } else {
2217 branch_name = NULL;
2218 skip_prefix(s->branch, "refs/heads/", &branch_name);
2220 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2223 /* Lookup stats on the upstream tracking branch, if set. */
2224 branch = branch_get(branch_name);
2225 base = NULL;
2226 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2227 &base, 0, s->ahead_behind_flags);
2228 if (base) {
2229 base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2230 base, 0);
2231 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2232 free((char *)base);
2234 if (ab_info > 0) {
2235 /* different */
2236 if (nr_ahead || nr_behind)
2237 fprintf(s->fp, "# branch.ab +%d -%d%c",
2238 nr_ahead, nr_behind, eol);
2239 else
2240 fprintf(s->fp, "# branch.ab +? -?%c",
2241 eol);
2242 } else if (!ab_info) {
2243 /* same */
2244 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2251 * Print the stash count in a porcelain-friendly format
2253 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2255 int stash_count = count_stash_entries();
2256 char eol = s->null_termination ? '\0' : '\n';
2258 if (stash_count > 0)
2259 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2263 * Convert various submodule status values into a
2264 * fixed-length string of characters in the buffer provided.
2266 static void wt_porcelain_v2_submodule_state(
2267 struct wt_status_change_data *d,
2268 char sub[5])
2270 if (S_ISGITLINK(d->mode_head) ||
2271 S_ISGITLINK(d->mode_index) ||
2272 S_ISGITLINK(d->mode_worktree)) {
2273 sub[0] = 'S';
2274 sub[1] = d->new_submodule_commits ? 'C' : '.';
2275 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2276 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2277 } else {
2278 sub[0] = 'N';
2279 sub[1] = '.';
2280 sub[2] = '.';
2281 sub[3] = '.';
2283 sub[4] = 0;
2287 * Fix-up changed entries before we print them.
2289 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2291 struct wt_status_change_data *d = it->util;
2293 if (!d->index_status) {
2295 * This entry is unchanged in the index (relative to the head).
2296 * Therefore, the collect_updated_cb was never called for this
2297 * entry (during the head-vs-index scan) and so the head column
2298 * fields were never set.
2300 * We must have data for the index column (from the
2301 * index-vs-worktree scan (otherwise, this entry should not be
2302 * in the list of changes)).
2304 * Copy index column fields to the head column, so that our
2305 * output looks complete.
2307 assert(d->mode_head == 0);
2308 d->mode_head = d->mode_index;
2309 oidcpy(&d->oid_head, &d->oid_index);
2312 if (!d->worktree_status) {
2314 * This entry is unchanged in the worktree (relative to the index).
2315 * Therefore, the collect_changed_cb was never called for this entry
2316 * (during the index-vs-worktree scan) and so the worktree column
2317 * fields were never set.
2319 * We must have data for the index column (from the head-vs-index
2320 * scan).
2322 * Copy the index column fields to the worktree column so that
2323 * our output looks complete.
2325 * Note that we only have a mode field in the worktree column
2326 * because the scan code tries really hard to not have to compute it.
2328 assert(d->mode_worktree == 0);
2329 d->mode_worktree = d->mode_index;
2334 * Print porcelain v2 info for tracked entries with changes.
2336 static void wt_porcelain_v2_print_changed_entry(
2337 struct string_list_item *it,
2338 struct wt_status *s)
2340 struct wt_status_change_data *d = it->util;
2341 struct strbuf buf = STRBUF_INIT;
2342 struct strbuf buf_from = STRBUF_INIT;
2343 const char *path = NULL;
2344 const char *path_from = NULL;
2345 char key[3];
2346 char submodule_token[5];
2347 char sep_char, eol_char;
2349 wt_porcelain_v2_fix_up_changed(it);
2350 wt_porcelain_v2_submodule_state(d, submodule_token);
2352 key[0] = d->index_status ? d->index_status : '.';
2353 key[1] = d->worktree_status ? d->worktree_status : '.';
2354 key[2] = 0;
2356 if (s->null_termination) {
2358 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2359 * A single NUL character separates them.
2361 sep_char = '\0';
2362 eol_char = '\0';
2363 path = it->string;
2364 path_from = d->rename_source;
2365 } else {
2367 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2368 * The source path is only present when necessary.
2369 * A single TAB separates them (because paths can contain spaces
2370 * which are not escaped and C-quoting does escape TAB characters).
2372 sep_char = '\t';
2373 eol_char = '\n';
2374 path = quote_path(it->string, s->prefix, &buf, 0);
2375 if (d->rename_source)
2376 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2379 if (path_from)
2380 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2381 key, submodule_token,
2382 d->mode_head, d->mode_index, d->mode_worktree,
2383 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2384 d->rename_status, d->rename_score,
2385 path, sep_char, path_from, eol_char);
2386 else
2387 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2388 key, submodule_token,
2389 d->mode_head, d->mode_index, d->mode_worktree,
2390 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2391 path, eol_char);
2393 strbuf_release(&buf);
2394 strbuf_release(&buf_from);
2398 * Print porcelain v2 status info for unmerged entries.
2400 static void wt_porcelain_v2_print_unmerged_entry(
2401 struct string_list_item *it,
2402 struct wt_status *s)
2404 struct wt_status_change_data *d = it->util;
2405 struct index_state *istate = s->repo->index;
2406 const struct cache_entry *ce;
2407 struct strbuf buf_index = STRBUF_INIT;
2408 const char *path_index = NULL;
2409 int pos, stage, sum;
2410 struct {
2411 int mode;
2412 struct object_id oid;
2413 } stages[3];
2414 const char *key;
2415 char submodule_token[5];
2416 char unmerged_prefix = 'u';
2417 char eol_char = s->null_termination ? '\0' : '\n';
2419 wt_porcelain_v2_submodule_state(d, submodule_token);
2421 switch (d->stagemask) {
2422 case 1: key = "DD"; break; /* both deleted */
2423 case 2: key = "AU"; break; /* added by us */
2424 case 3: key = "UD"; break; /* deleted by them */
2425 case 4: key = "UA"; break; /* added by them */
2426 case 5: key = "DU"; break; /* deleted by us */
2427 case 6: key = "AA"; break; /* both added */
2428 case 7: key = "UU"; break; /* both modified */
2429 default:
2430 BUG("unhandled unmerged status %x", d->stagemask);
2434 * Disregard d.aux.porcelain_v2 data that we accumulated
2435 * for the head and index columns during the scans and
2436 * replace with the actual stage data.
2438 * Note that this is a last-one-wins for each the individual
2439 * stage [123] columns in the event of multiple cache entries
2440 * for same stage.
2442 memset(stages, 0, sizeof(stages));
2443 sum = 0;
2444 pos = index_name_pos(istate, it->string, strlen(it->string));
2445 assert(pos < 0);
2446 pos = -pos-1;
2447 while (pos < istate->cache_nr) {
2448 ce = istate->cache[pos++];
2449 stage = ce_stage(ce);
2450 if (strcmp(ce->name, it->string) || !stage)
2451 break;
2452 stages[stage - 1].mode = ce->ce_mode;
2453 oidcpy(&stages[stage - 1].oid, &ce->oid);
2454 sum |= (1 << (stage - 1));
2456 if (sum != d->stagemask)
2457 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2459 if (s->null_termination)
2460 path_index = it->string;
2461 else
2462 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2464 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2465 unmerged_prefix, key, submodule_token,
2466 stages[0].mode, /* stage 1 */
2467 stages[1].mode, /* stage 2 */
2468 stages[2].mode, /* stage 3 */
2469 d->mode_worktree,
2470 oid_to_hex(&stages[0].oid), /* stage 1 */
2471 oid_to_hex(&stages[1].oid), /* stage 2 */
2472 oid_to_hex(&stages[2].oid), /* stage 3 */
2473 path_index,
2474 eol_char);
2476 strbuf_release(&buf_index);
2480 * Print porcelain V2 status info for untracked and ignored entries.
2482 static void wt_porcelain_v2_print_other(
2483 struct string_list_item *it,
2484 struct wt_status *s,
2485 char prefix)
2487 struct strbuf buf = STRBUF_INIT;
2488 const char *path;
2489 char eol_char;
2491 if (s->null_termination) {
2492 path = it->string;
2493 eol_char = '\0';
2494 } else {
2495 path = quote_path(it->string, s->prefix, &buf, 0);
2496 eol_char = '\n';
2499 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2501 strbuf_release(&buf);
2505 * Print porcelain V2 status.
2507 * [<v2_branch>]
2508 * [<v2_changed_items>]*
2509 * [<v2_unmerged_items>]*
2510 * [<v2_untracked_items>]*
2511 * [<v2_ignored_items>]*
2514 static void wt_porcelain_v2_print(struct wt_status *s)
2516 struct wt_status_change_data *d;
2517 struct string_list_item *it;
2518 int i;
2520 if (s->show_branch)
2521 wt_porcelain_v2_print_tracking(s);
2523 if (s->show_stash)
2524 wt_porcelain_v2_print_stash(s);
2526 for (i = 0; i < s->change.nr; i++) {
2527 it = &(s->change.items[i]);
2528 d = it->util;
2529 if (!d->stagemask)
2530 wt_porcelain_v2_print_changed_entry(it, s);
2533 for (i = 0; i < s->change.nr; i++) {
2534 it = &(s->change.items[i]);
2535 d = it->util;
2536 if (d->stagemask)
2537 wt_porcelain_v2_print_unmerged_entry(it, s);
2540 for (i = 0; i < s->untracked.nr; i++) {
2541 it = &(s->untracked.items[i]);
2542 wt_porcelain_v2_print_other(it, s, '?');
2545 for (i = 0; i < s->ignored.nr; i++) {
2546 it = &(s->ignored.items[i]);
2547 wt_porcelain_v2_print_other(it, s, '!');
2551 void wt_status_print(struct wt_status *s)
2553 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2554 trace2_data_intmax("status", s->repo, "count/untracked",
2555 s->untracked.nr);
2556 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2558 trace2_region_enter("status", "print", s->repo);
2560 switch (s->status_format) {
2561 case STATUS_FORMAT_SHORT:
2562 wt_shortstatus_print(s);
2563 break;
2564 case STATUS_FORMAT_PORCELAIN:
2565 wt_porcelain_print(s);
2566 break;
2567 case STATUS_FORMAT_PORCELAIN_V2:
2568 wt_porcelain_v2_print(s);
2569 break;
2570 case STATUS_FORMAT_UNSPECIFIED:
2571 BUG("finalize_deferred_config() should have been called");
2572 break;
2573 case STATUS_FORMAT_NONE:
2574 case STATUS_FORMAT_LONG:
2575 wt_longstatus_print(s);
2576 break;
2579 trace2_region_leave("status", "print", s->repo);
2583 * Returns 1 if there are unstaged changes, 0 otherwise.
2585 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2587 struct rev_info rev_info;
2588 int result;
2590 repo_init_revisions(r, &rev_info, NULL);
2591 if (ignore_submodules) {
2592 rev_info.diffopt.flags.ignore_submodules = 1;
2593 rev_info.diffopt.flags.override_submodule_config = 1;
2595 rev_info.diffopt.flags.quick = 1;
2596 diff_setup_done(&rev_info.diffopt);
2597 run_diff_files(&rev_info, 0);
2598 result = diff_result_code(&rev_info.diffopt);
2599 release_revisions(&rev_info);
2600 return result;
2604 * Returns 1 if there are uncommitted changes, 0 otherwise.
2606 int has_uncommitted_changes(struct repository *r,
2607 int ignore_submodules)
2609 struct rev_info rev_info;
2610 int result;
2612 if (is_index_unborn(r->index))
2613 return 0;
2615 repo_init_revisions(r, &rev_info, NULL);
2616 if (ignore_submodules)
2617 rev_info.diffopt.flags.ignore_submodules = 1;
2618 rev_info.diffopt.flags.quick = 1;
2620 add_head_to_pending(&rev_info);
2621 if (!rev_info.pending.nr) {
2623 * We have no head (or it's corrupt); use the empty tree,
2624 * which will complain if the index is non-empty.
2626 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2627 add_pending_object(&rev_info, &tree->object, "");
2630 diff_setup_done(&rev_info.diffopt);
2631 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2632 result = diff_result_code(&rev_info.diffopt);
2633 release_revisions(&rev_info);
2634 return result;
2638 * If the work tree has unstaged or uncommitted changes, dies with the
2639 * appropriate message.
2641 int require_clean_work_tree(struct repository *r,
2642 const char *action,
2643 const char *hint,
2644 int ignore_submodules,
2645 int gently)
2647 struct lock_file lock_file = LOCK_INIT;
2648 int err = 0, fd;
2650 fd = repo_hold_locked_index(r, &lock_file, 0);
2651 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2652 if (0 <= fd)
2653 repo_update_index_if_able(r, &lock_file);
2654 rollback_lock_file(&lock_file);
2656 if (has_unstaged_changes(r, ignore_submodules)) {
2657 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2658 error(_("cannot %s: You have unstaged changes."), _(action));
2659 err = 1;
2662 if (has_uncommitted_changes(r, ignore_submodules)) {
2663 if (err)
2664 error(_("additionally, your index contains uncommitted changes."));
2665 else
2666 error(_("cannot %s: Your index contains uncommitted changes."),
2667 _(action));
2668 err = 1;
2671 if (err) {
2672 if (hint) {
2673 if (!*hint)
2674 BUG("empty hint passed to require_clean_work_tree();"
2675 " use NULL instead");
2676 error("%s", hint);
2678 if (!gently)
2679 exit(128);
2682 return err;