cache.h: remove expand_user_path()
[git/debian.git] / wt-status.c
blob106e46480a177d23cdd8be2e81e9e527cca78400
1 #include "cache.h"
2 #include "wt-status.h"
3 #include "object.h"
4 #include "dir.h"
5 #include "commit.h"
6 #include "diff.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "revision.h"
11 #include "diffcore.h"
12 #include "quote.h"
13 #include "run-command.h"
14 #include "strvec.h"
15 #include "remote.h"
16 #include "refs.h"
17 #include "submodule.h"
18 #include "column.h"
19 #include "strbuf.h"
20 #include "utf8.h"
21 #include "worktree.h"
22 #include "lockfile.h"
23 #include "sequencer.h"
24 #include "fsmonitor-settings.h"
26 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
27 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
29 static const char cut_line[] =
30 "------------------------ >8 ------------------------\n";
32 static char default_wt_status_colors[][COLOR_MAXLEN] = {
33 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
34 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
35 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
36 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
37 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
38 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
39 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
40 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
41 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
44 static const char *color(int slot, struct wt_status *s)
46 const char *c = "";
47 if (want_color(s->use_color))
48 c = s->color_palette[slot];
49 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
50 c = s->color_palette[WT_STATUS_HEADER];
51 return c;
54 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
55 const char *fmt, va_list ap, const char *trail)
57 struct strbuf sb = STRBUF_INIT;
58 struct strbuf linebuf = STRBUF_INIT;
59 const char *line, *eol;
61 strbuf_vaddf(&sb, fmt, ap);
62 if (!sb.len) {
63 if (s->display_comment_prefix) {
64 strbuf_addch(&sb, comment_line_char);
65 if (!trail)
66 strbuf_addch(&sb, ' ');
68 color_print_strbuf(s->fp, color, &sb);
69 if (trail)
70 fprintf(s->fp, "%s", trail);
71 strbuf_release(&sb);
72 return;
74 for (line = sb.buf; *line; line = eol + 1) {
75 eol = strchr(line, '\n');
77 strbuf_reset(&linebuf);
78 if (at_bol && s->display_comment_prefix) {
79 strbuf_addch(&linebuf, comment_line_char);
80 if (*line != '\n' && *line != '\t')
81 strbuf_addch(&linebuf, ' ');
83 if (eol)
84 strbuf_add(&linebuf, line, eol - line);
85 else
86 strbuf_addstr(&linebuf, line);
87 color_print_strbuf(s->fp, color, &linebuf);
88 if (eol)
89 fprintf(s->fp, "\n");
90 else
91 break;
92 at_bol = 1;
94 if (trail)
95 fprintf(s->fp, "%s", trail);
96 strbuf_release(&linebuf);
97 strbuf_release(&sb);
100 void status_printf_ln(struct wt_status *s, const char *color,
101 const char *fmt, ...)
103 va_list ap;
105 va_start(ap, fmt);
106 status_vprintf(s, 1, color, fmt, ap, "\n");
107 va_end(ap);
110 void status_printf(struct wt_status *s, const char *color,
111 const char *fmt, ...)
113 va_list ap;
115 va_start(ap, fmt);
116 status_vprintf(s, 1, color, fmt, ap, NULL);
117 va_end(ap);
120 static void status_printf_more(struct wt_status *s, const char *color,
121 const char *fmt, ...)
123 va_list ap;
125 va_start(ap, fmt);
126 status_vprintf(s, 0, color, fmt, ap, NULL);
127 va_end(ap);
130 void wt_status_prepare(struct repository *r, struct wt_status *s)
132 memset(s, 0, sizeof(*s));
133 s->repo = r;
134 memcpy(s->color_palette, default_wt_status_colors,
135 sizeof(default_wt_status_colors));
136 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
137 s->use_color = -1;
138 s->relative_paths = 1;
139 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
140 s->reference = "HEAD";
141 s->fp = stdout;
142 s->index_file = get_index_file();
143 s->change.strdup_strings = 1;
144 s->untracked.strdup_strings = 1;
145 s->ignored.strdup_strings = 1;
146 s->show_branch = -1; /* unspecified */
147 s->show_stash = 0;
148 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
149 s->display_comment_prefix = 0;
150 s->detect_rename = -1;
151 s->rename_score = -1;
152 s->rename_limit = -1;
155 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
157 int i;
158 int del_mod_conflict = 0;
159 int both_deleted = 0;
160 int not_deleted = 0;
161 const char *c = color(WT_STATUS_HEADER, s);
163 status_printf_ln(s, c, _("Unmerged paths:"));
165 for (i = 0; i < s->change.nr; i++) {
166 struct string_list_item *it = &(s->change.items[i]);
167 struct wt_status_change_data *d = it->util;
169 switch (d->stagemask) {
170 case 0:
171 break;
172 case 1:
173 both_deleted = 1;
174 break;
175 case 3:
176 case 5:
177 del_mod_conflict = 1;
178 break;
179 default:
180 not_deleted = 1;
181 break;
185 if (!s->hints)
186 return;
187 if (s->whence != FROM_COMMIT)
189 else if (!s->is_initial) {
190 if (!strcmp(s->reference, "HEAD"))
191 status_printf_ln(s, c,
192 _(" (use \"git restore --staged <file>...\" to unstage)"));
193 else
194 status_printf_ln(s, c,
195 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
196 s->reference);
197 } else
198 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
200 if (!both_deleted) {
201 if (!del_mod_conflict)
202 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
203 else
204 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
205 } else if (!del_mod_conflict && !not_deleted) {
206 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
207 } else {
208 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
212 static void wt_longstatus_print_cached_header(struct wt_status *s)
214 const char *c = color(WT_STATUS_HEADER, s);
216 status_printf_ln(s, c, _("Changes to be committed:"));
217 if (!s->hints)
218 return;
219 if (s->whence != FROM_COMMIT)
220 ; /* NEEDSWORK: use "git reset --unresolve"??? */
221 else if (!s->is_initial) {
222 if (!strcmp(s->reference, "HEAD"))
223 status_printf_ln(s, c
224 , _(" (use \"git restore --staged <file>...\" to unstage)"));
225 else
226 status_printf_ln(s, c,
227 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
228 s->reference);
229 } else
230 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
233 static void wt_longstatus_print_dirty_header(struct wt_status *s,
234 int has_deleted,
235 int has_dirty_submodules)
237 const char *c = color(WT_STATUS_HEADER, s);
239 status_printf_ln(s, c, _("Changes not staged for commit:"));
240 if (!s->hints)
241 return;
242 if (!has_deleted)
243 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
244 else
245 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
246 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
247 if (has_dirty_submodules)
248 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
251 static void wt_longstatus_print_other_header(struct wt_status *s,
252 const char *what,
253 const char *how)
255 const char *c = color(WT_STATUS_HEADER, s);
256 status_printf_ln(s, c, "%s:", what);
257 if (!s->hints)
258 return;
259 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
262 static void wt_longstatus_print_trailer(struct wt_status *s)
264 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
267 static const char *wt_status_unmerged_status_string(int stagemask)
269 switch (stagemask) {
270 case 1:
271 return _("both deleted:");
272 case 2:
273 return _("added by us:");
274 case 3:
275 return _("deleted by them:");
276 case 4:
277 return _("added by them:");
278 case 5:
279 return _("deleted by us:");
280 case 6:
281 return _("both added:");
282 case 7:
283 return _("both modified:");
284 default:
285 BUG("unhandled unmerged status %x", stagemask);
289 static const char *wt_status_diff_status_string(int status)
291 switch (status) {
292 case DIFF_STATUS_ADDED:
293 return _("new file:");
294 case DIFF_STATUS_COPIED:
295 return _("copied:");
296 case DIFF_STATUS_DELETED:
297 return _("deleted:");
298 case DIFF_STATUS_MODIFIED:
299 return _("modified:");
300 case DIFF_STATUS_RENAMED:
301 return _("renamed:");
302 case DIFF_STATUS_TYPE_CHANGED:
303 return _("typechange:");
304 case DIFF_STATUS_UNKNOWN:
305 return _("unknown:");
306 case DIFF_STATUS_UNMERGED:
307 return _("unmerged:");
308 default:
309 return NULL;
313 static int maxwidth(const char *(*label)(int), int minval, int maxval)
315 int result = 0, i;
317 for (i = minval; i <= maxval; i++) {
318 const char *s = label(i);
319 int len = s ? utf8_strwidth(s) : 0;
320 if (len > result)
321 result = len;
323 return result;
326 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
327 struct string_list_item *it)
329 const char *c = color(WT_STATUS_UNMERGED, s);
330 struct wt_status_change_data *d = it->util;
331 struct strbuf onebuf = STRBUF_INIT;
332 static char *padding;
333 static int label_width;
334 const char *one, *how;
335 int len;
337 if (!padding) {
338 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
339 label_width += strlen(" ");
340 padding = xmallocz(label_width);
341 memset(padding, ' ', label_width);
344 one = quote_path(it->string, s->prefix, &onebuf, 0);
345 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
347 how = wt_status_unmerged_status_string(d->stagemask);
348 len = label_width - utf8_strwidth(how);
349 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
350 strbuf_release(&onebuf);
353 static void wt_longstatus_print_change_data(struct wt_status *s,
354 int change_type,
355 struct string_list_item *it)
357 struct wt_status_change_data *d = it->util;
358 const char *c = color(change_type, s);
359 int status;
360 char *one_name;
361 char *two_name;
362 const char *one, *two;
363 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
364 struct strbuf extra = STRBUF_INIT;
365 static char *padding;
366 static int label_width;
367 const char *what;
368 int len;
370 if (!padding) {
371 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
372 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
373 label_width += strlen(" ");
374 padding = xmallocz(label_width);
375 memset(padding, ' ', label_width);
378 one_name = two_name = it->string;
379 switch (change_type) {
380 case WT_STATUS_UPDATED:
381 status = d->index_status;
382 break;
383 case WT_STATUS_CHANGED:
384 if (d->new_submodule_commits || d->dirty_submodule) {
385 strbuf_addstr(&extra, " (");
386 if (d->new_submodule_commits)
387 strbuf_addstr(&extra, _("new commits, "));
388 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
389 strbuf_addstr(&extra, _("modified content, "));
390 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
391 strbuf_addstr(&extra, _("untracked content, "));
392 strbuf_setlen(&extra, extra.len - 2);
393 strbuf_addch(&extra, ')');
395 status = d->worktree_status;
396 break;
397 default:
398 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
399 change_type);
403 * Only pick up the rename it's relevant. If the rename is for
404 * the changed section and we're printing the updated section,
405 * ignore it.
407 if (d->rename_status == status)
408 one_name = d->rename_source;
410 one = quote_path(one_name, s->prefix, &onebuf, 0);
411 two = quote_path(two_name, s->prefix, &twobuf, 0);
413 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
414 what = wt_status_diff_status_string(status);
415 if (!what)
416 BUG("unhandled diff status %c", status);
417 len = label_width - utf8_strwidth(what);
418 assert(len >= 0);
419 if (one_name != two_name)
420 status_printf_more(s, c, "%s%.*s%s -> %s",
421 what, len, padding, one, two);
422 else
423 status_printf_more(s, c, "%s%.*s%s",
424 what, len, padding, one);
425 if (extra.len) {
426 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
427 strbuf_release(&extra);
429 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
430 strbuf_release(&onebuf);
431 strbuf_release(&twobuf);
434 static char short_submodule_status(struct wt_status_change_data *d)
436 if (d->new_submodule_commits)
437 return 'M';
438 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
439 return 'm';
440 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
441 return '?';
442 return d->worktree_status;
445 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
446 struct diff_options *options UNUSED,
447 void *data)
449 struct wt_status *s = data;
450 int i;
452 if (!q->nr)
453 return;
454 s->workdir_dirty = 1;
455 for (i = 0; i < q->nr; i++) {
456 struct diff_filepair *p;
457 struct string_list_item *it;
458 struct wt_status_change_data *d;
460 p = q->queue[i];
461 it = string_list_insert(&s->change, p->two->path);
462 d = it->util;
463 if (!d) {
464 CALLOC_ARRAY(d, 1);
465 it->util = d;
467 if (!d->worktree_status)
468 d->worktree_status = p->status;
469 if (S_ISGITLINK(p->two->mode)) {
470 d->dirty_submodule = p->two->dirty_submodule;
471 d->new_submodule_commits = !oideq(&p->one->oid,
472 &p->two->oid);
473 if (s->status_format == STATUS_FORMAT_SHORT)
474 d->worktree_status = short_submodule_status(d);
477 switch (p->status) {
478 case DIFF_STATUS_ADDED:
479 d->mode_worktree = p->two->mode;
480 break;
482 case DIFF_STATUS_DELETED:
483 d->mode_index = p->one->mode;
484 oidcpy(&d->oid_index, &p->one->oid);
485 /* mode_worktree is zero for a delete. */
486 break;
488 case DIFF_STATUS_COPIED:
489 case DIFF_STATUS_RENAMED:
490 if (d->rename_status)
491 BUG("multiple renames on the same target? how?");
492 d->rename_source = xstrdup(p->one->path);
493 d->rename_score = p->score * 100 / MAX_SCORE;
494 d->rename_status = p->status;
495 /* fallthru */
496 case DIFF_STATUS_MODIFIED:
497 case DIFF_STATUS_TYPE_CHANGED:
498 case DIFF_STATUS_UNMERGED:
499 d->mode_index = p->one->mode;
500 d->mode_worktree = p->two->mode;
501 oidcpy(&d->oid_index, &p->one->oid);
502 break;
504 default:
505 BUG("unhandled diff-files status '%c'", p->status);
506 break;
512 static int unmerged_mask(struct index_state *istate, const char *path)
514 int pos, mask;
515 const struct cache_entry *ce;
517 pos = index_name_pos(istate, path, strlen(path));
518 if (0 <= pos)
519 return 0;
521 mask = 0;
522 pos = -pos-1;
523 while (pos < istate->cache_nr) {
524 ce = istate->cache[pos++];
525 if (strcmp(ce->name, path) || !ce_stage(ce))
526 break;
527 mask |= (1 << (ce_stage(ce) - 1));
529 return mask;
532 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
533 struct diff_options *options UNUSED,
534 void *data)
536 struct wt_status *s = data;
537 int i;
539 for (i = 0; i < q->nr; i++) {
540 struct diff_filepair *p;
541 struct string_list_item *it;
542 struct wt_status_change_data *d;
544 p = q->queue[i];
545 it = string_list_insert(&s->change, p->two->path);
546 d = it->util;
547 if (!d) {
548 CALLOC_ARRAY(d, 1);
549 it->util = d;
551 if (!d->index_status)
552 d->index_status = p->status;
553 switch (p->status) {
554 case DIFF_STATUS_ADDED:
555 /* Leave {mode,oid}_head zero for an add. */
556 d->mode_index = p->two->mode;
557 oidcpy(&d->oid_index, &p->two->oid);
558 s->committable = 1;
559 break;
560 case DIFF_STATUS_DELETED:
561 d->mode_head = p->one->mode;
562 oidcpy(&d->oid_head, &p->one->oid);
563 s->committable = 1;
564 /* Leave {mode,oid}_index zero for a delete. */
565 break;
567 case DIFF_STATUS_COPIED:
568 case DIFF_STATUS_RENAMED:
569 if (d->rename_status)
570 BUG("multiple renames on the same target? how?");
571 d->rename_source = xstrdup(p->one->path);
572 d->rename_score = p->score * 100 / MAX_SCORE;
573 d->rename_status = p->status;
574 /* fallthru */
575 case DIFF_STATUS_MODIFIED:
576 case DIFF_STATUS_TYPE_CHANGED:
577 d->mode_head = p->one->mode;
578 d->mode_index = p->two->mode;
579 oidcpy(&d->oid_head, &p->one->oid);
580 oidcpy(&d->oid_index, &p->two->oid);
581 s->committable = 1;
582 break;
583 case DIFF_STATUS_UNMERGED:
584 d->stagemask = unmerged_mask(s->repo->index,
585 p->two->path);
587 * Don't bother setting {mode,oid}_{head,index} since the print
588 * code will output the stage values directly and not use the
589 * values in these fields.
591 break;
593 default:
594 BUG("unhandled diff-index status '%c'", p->status);
595 break;
600 static void wt_status_collect_changes_worktree(struct wt_status *s)
602 struct rev_info rev;
604 repo_init_revisions(s->repo, &rev, NULL);
605 setup_revisions(0, NULL, &rev, NULL);
606 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
607 rev.diffopt.flags.dirty_submodules = 1;
608 rev.diffopt.ita_invisible_in_index = 1;
609 if (!s->show_untracked_files)
610 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
611 if (s->ignore_submodule_arg) {
612 rev.diffopt.flags.override_submodule_config = 1;
613 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
614 } else if (!rev.diffopt.flags.ignore_submodule_set &&
615 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
616 handle_ignore_submodules_arg(&rev.diffopt, "none");
617 rev.diffopt.format_callback = wt_status_collect_changed_cb;
618 rev.diffopt.format_callback_data = s;
619 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
620 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
621 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
622 copy_pathspec(&rev.prune_data, &s->pathspec);
623 run_diff_files(&rev, 0);
624 release_revisions(&rev);
627 static void wt_status_collect_changes_index(struct wt_status *s)
629 struct rev_info rev;
630 struct setup_revision_opt opt;
632 repo_init_revisions(s->repo, &rev, NULL);
633 memset(&opt, 0, sizeof(opt));
634 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
635 setup_revisions(0, NULL, &rev, &opt);
637 rev.diffopt.flags.override_submodule_config = 1;
638 rev.diffopt.ita_invisible_in_index = 1;
639 if (s->ignore_submodule_arg) {
640 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
641 } else {
643 * Unless the user did explicitly request a submodule ignore
644 * mode by passing a command line option we do not ignore any
645 * changed submodule SHA-1s when comparing index and HEAD, no
646 * matter what is configured. Otherwise the user won't be
647 * shown any submodules manually added (and which are
648 * staged to be committed), which would be really confusing.
650 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
653 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
654 rev.diffopt.format_callback = wt_status_collect_updated_cb;
655 rev.diffopt.format_callback_data = s;
656 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
657 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
658 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
661 * The `recursive` option must be enabled to allow the diff to recurse
662 * into subdirectories of sparse directory index entries. If it is not
663 * enabled, a subdirectory containing file(s) with changes is reported
664 * as "modified", rather than the modified files themselves.
666 rev.diffopt.flags.recursive = 1;
668 copy_pathspec(&rev.prune_data, &s->pathspec);
669 run_diff_index(&rev, 1);
670 release_revisions(&rev);
673 static int add_file_to_list(const struct object_id *oid,
674 struct strbuf *base, const char *path,
675 unsigned int mode, void *context)
677 struct string_list_item *it;
678 struct wt_status_change_data *d;
679 struct wt_status *s = context;
680 struct strbuf full_name = STRBUF_INIT;
682 if (S_ISDIR(mode))
683 return READ_TREE_RECURSIVE;
685 strbuf_add(&full_name, base->buf, base->len);
686 strbuf_addstr(&full_name, path);
687 it = string_list_insert(&s->change, full_name.buf);
688 d = it->util;
689 if (!d) {
690 CALLOC_ARRAY(d, 1);
691 it->util = d;
694 d->index_status = DIFF_STATUS_ADDED;
695 /* Leave {mode,oid}_head zero for adds. */
696 d->mode_index = mode;
697 oidcpy(&d->oid_index, oid);
698 s->committable = 1;
699 strbuf_release(&full_name);
700 return 0;
703 static void wt_status_collect_changes_initial(struct wt_status *s)
705 struct index_state *istate = s->repo->index;
706 int i;
708 for (i = 0; i < istate->cache_nr; i++) {
709 struct string_list_item *it;
710 struct wt_status_change_data *d;
711 const struct cache_entry *ce = istate->cache[i];
713 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
714 continue;
715 if (ce_intent_to_add(ce))
716 continue;
717 if (S_ISSPARSEDIR(ce->ce_mode)) {
719 * This is a sparse directory entry, so we want to collect all
720 * of the added files within the tree. This requires recursively
721 * expanding the trees to find the elements that are new in this
722 * tree and marking them with DIFF_STATUS_ADDED.
724 struct strbuf base = STRBUF_INIT;
725 struct pathspec ps = { 0 };
726 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
728 ps.recursive = 1;
729 ps.has_wildcard = 1;
730 ps.max_depth = -1;
732 strbuf_add(&base, ce->name, ce->ce_namelen);
733 read_tree_at(istate->repo, tree, &base, &ps,
734 add_file_to_list, s);
735 continue;
738 it = string_list_insert(&s->change, ce->name);
739 d = it->util;
740 if (!d) {
741 CALLOC_ARRAY(d, 1);
742 it->util = d;
744 if (ce_stage(ce)) {
745 d->index_status = DIFF_STATUS_UNMERGED;
746 d->stagemask |= (1 << (ce_stage(ce) - 1));
748 * Don't bother setting {mode,oid}_{head,index} since the print
749 * code will output the stage values directly and not use the
750 * values in these fields.
752 s->committable = 1;
753 } else {
754 d->index_status = DIFF_STATUS_ADDED;
755 /* Leave {mode,oid}_head zero for adds. */
756 d->mode_index = ce->ce_mode;
757 oidcpy(&d->oid_index, &ce->oid);
758 s->committable = 1;
763 static void wt_status_collect_untracked(struct wt_status *s)
765 int i;
766 struct dir_struct dir = DIR_INIT;
767 uint64_t t_begin = getnanotime();
768 struct index_state *istate = s->repo->index;
770 if (!s->show_untracked_files)
771 return;
773 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
774 dir.flags |=
775 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
776 if (s->show_ignored_mode) {
777 dir.flags |= DIR_SHOW_IGNORED_TOO;
779 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
780 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
781 } else {
782 dir.untracked = istate->untracked;
785 setup_standard_excludes(&dir);
787 fill_directory(&dir, istate, &s->pathspec);
789 for (i = 0; i < dir.nr; i++) {
790 struct dir_entry *ent = dir.entries[i];
791 if (index_name_is_other(istate, ent->name, ent->len))
792 string_list_insert(&s->untracked, ent->name);
795 for (i = 0; i < dir.ignored_nr; i++) {
796 struct dir_entry *ent = dir.ignored[i];
797 if (index_name_is_other(istate, ent->name, ent->len))
798 string_list_insert(&s->ignored, ent->name);
801 dir_clear(&dir);
803 if (advice_enabled(ADVICE_STATUS_U_OPTION))
804 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
807 static int has_unmerged(struct wt_status *s)
809 int i;
811 for (i = 0; i < s->change.nr; i++) {
812 struct wt_status_change_data *d;
813 d = s->change.items[i].util;
814 if (d->stagemask)
815 return 1;
817 return 0;
820 void wt_status_collect(struct wt_status *s)
822 trace2_region_enter("status", "worktrees", s->repo);
823 wt_status_collect_changes_worktree(s);
824 trace2_region_leave("status", "worktrees", s->repo);
826 if (s->is_initial) {
827 trace2_region_enter("status", "initial", s->repo);
828 wt_status_collect_changes_initial(s);
829 trace2_region_leave("status", "initial", s->repo);
830 } else {
831 trace2_region_enter("status", "index", s->repo);
832 wt_status_collect_changes_index(s);
833 trace2_region_leave("status", "index", s->repo);
836 trace2_region_enter("status", "untracked", s->repo);
837 wt_status_collect_untracked(s);
838 trace2_region_leave("status", "untracked", s->repo);
840 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
841 if (s->state.merge_in_progress && !has_unmerged(s))
842 s->committable = 1;
845 void wt_status_collect_free_buffers(struct wt_status *s)
847 wt_status_state_free_buffers(&s->state);
850 void wt_status_state_free_buffers(struct wt_status_state *state)
852 FREE_AND_NULL(state->branch);
853 FREE_AND_NULL(state->onto);
854 FREE_AND_NULL(state->detached_from);
857 static void wt_longstatus_print_unmerged(struct wt_status *s)
859 int shown_header = 0;
860 int i;
862 for (i = 0; i < s->change.nr; i++) {
863 struct wt_status_change_data *d;
864 struct string_list_item *it;
865 it = &(s->change.items[i]);
866 d = it->util;
867 if (!d->stagemask)
868 continue;
869 if (!shown_header) {
870 wt_longstatus_print_unmerged_header(s);
871 shown_header = 1;
873 wt_longstatus_print_unmerged_data(s, it);
875 if (shown_header)
876 wt_longstatus_print_trailer(s);
880 static void wt_longstatus_print_updated(struct wt_status *s)
882 int shown_header = 0;
883 int i;
885 for (i = 0; i < s->change.nr; i++) {
886 struct wt_status_change_data *d;
887 struct string_list_item *it;
888 it = &(s->change.items[i]);
889 d = it->util;
890 if (!d->index_status ||
891 d->index_status == DIFF_STATUS_UNMERGED)
892 continue;
893 if (!shown_header) {
894 wt_longstatus_print_cached_header(s);
895 shown_header = 1;
897 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
899 if (shown_header)
900 wt_longstatus_print_trailer(s);
904 * -1 : has delete
905 * 0 : no change
906 * 1 : some change but no delete
908 static int wt_status_check_worktree_changes(struct wt_status *s,
909 int *dirty_submodules)
911 int i;
912 int changes = 0;
914 *dirty_submodules = 0;
916 for (i = 0; i < s->change.nr; i++) {
917 struct wt_status_change_data *d;
918 d = s->change.items[i].util;
919 if (!d->worktree_status ||
920 d->worktree_status == DIFF_STATUS_UNMERGED)
921 continue;
922 if (!changes)
923 changes = 1;
924 if (d->dirty_submodule)
925 *dirty_submodules = 1;
926 if (d->worktree_status == DIFF_STATUS_DELETED)
927 changes = -1;
929 return changes;
932 static void wt_longstatus_print_changed(struct wt_status *s)
934 int i, dirty_submodules;
935 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
937 if (!worktree_changes)
938 return;
940 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
942 for (i = 0; i < s->change.nr; i++) {
943 struct wt_status_change_data *d;
944 struct string_list_item *it;
945 it = &(s->change.items[i]);
946 d = it->util;
947 if (!d->worktree_status ||
948 d->worktree_status == DIFF_STATUS_UNMERGED)
949 continue;
950 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
952 wt_longstatus_print_trailer(s);
955 static int stash_count_refs(struct object_id *ooid UNUSED,
956 struct object_id *noid UNUSED,
957 const char *email UNUSED,
958 timestamp_t timestamp UNUSED, int tz UNUSED,
959 const char *message UNUSED, void *cb_data)
961 int *c = cb_data;
962 (*c)++;
963 return 0;
966 static int count_stash_entries(void)
968 int n = 0;
969 for_each_reflog_ent("refs/stash", stash_count_refs, &n);
970 return n;
973 static void wt_longstatus_print_stash_summary(struct wt_status *s)
975 int stash_count = count_stash_entries();
977 if (stash_count > 0)
978 status_printf_ln(s, GIT_COLOR_NORMAL,
979 Q_("Your stash currently has %d entry",
980 "Your stash currently has %d entries", stash_count),
981 stash_count);
984 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
986 struct child_process sm_summary = CHILD_PROCESS_INIT;
987 struct strbuf cmd_stdout = STRBUF_INIT;
988 struct strbuf summary = STRBUF_INIT;
989 char *summary_content;
991 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
993 strvec_push(&sm_summary.args, "submodule");
994 strvec_push(&sm_summary.args, "summary");
995 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
996 strvec_push(&sm_summary.args, "--for-status");
997 strvec_push(&sm_summary.args, "--summary-limit");
998 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
999 if (!uncommitted)
1000 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1002 sm_summary.git_cmd = 1;
1003 sm_summary.no_stdin = 1;
1005 capture_command(&sm_summary, &cmd_stdout, 1024);
1007 /* prepend header, only if there's an actual output */
1008 if (cmd_stdout.len) {
1009 if (uncommitted)
1010 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1011 else
1012 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1013 strbuf_addstr(&summary, "\n\n");
1015 strbuf_addbuf(&summary, &cmd_stdout);
1016 strbuf_release(&cmd_stdout);
1018 if (s->display_comment_prefix) {
1019 size_t len;
1020 summary_content = strbuf_detach(&summary, &len);
1021 strbuf_add_commented_lines(&summary, summary_content, len);
1022 free(summary_content);
1025 fputs(summary.buf, s->fp);
1026 strbuf_release(&summary);
1029 static void wt_longstatus_print_other(struct wt_status *s,
1030 struct string_list *l,
1031 const char *what,
1032 const char *how)
1034 int i;
1035 struct strbuf buf = STRBUF_INIT;
1036 static struct string_list output = STRING_LIST_INIT_DUP;
1037 struct column_options copts;
1039 if (!l->nr)
1040 return;
1042 wt_longstatus_print_other_header(s, what, how);
1044 for (i = 0; i < l->nr; i++) {
1045 struct string_list_item *it;
1046 const char *path;
1047 it = &(l->items[i]);
1048 path = quote_path(it->string, s->prefix, &buf, 0);
1049 if (column_active(s->colopts)) {
1050 string_list_append(&output, path);
1051 continue;
1053 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1054 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1055 "%s\n", path);
1058 strbuf_release(&buf);
1059 if (!column_active(s->colopts))
1060 goto conclude;
1062 strbuf_addf(&buf, "%s%s\t%s",
1063 color(WT_STATUS_HEADER, s),
1064 s->display_comment_prefix ? "#" : "",
1065 color(WT_STATUS_UNTRACKED, s));
1066 memset(&copts, 0, sizeof(copts));
1067 copts.padding = 1;
1068 copts.indent = buf.buf;
1069 if (want_color(s->use_color))
1070 copts.nl = GIT_COLOR_RESET "\n";
1071 print_columns(&output, s->colopts, &copts);
1072 string_list_clear(&output, 0);
1073 strbuf_release(&buf);
1074 conclude:
1075 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1078 size_t wt_status_locate_end(const char *s, size_t len)
1080 const char *p;
1081 struct strbuf pattern = STRBUF_INIT;
1083 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
1084 if (starts_with(s, pattern.buf + 1))
1085 len = 0;
1086 else if ((p = strstr(s, pattern.buf)))
1087 len = p - s + 1;
1088 strbuf_release(&pattern);
1089 return len;
1092 void wt_status_append_cut_line(struct strbuf *buf)
1094 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1096 strbuf_commented_addf(buf, "%s", cut_line);
1097 strbuf_add_commented_lines(buf, explanation, strlen(explanation));
1100 void wt_status_add_cut_line(FILE *fp)
1102 struct strbuf buf = STRBUF_INIT;
1104 wt_status_append_cut_line(&buf);
1105 fputs(buf.buf, fp);
1106 strbuf_release(&buf);
1109 static void wt_longstatus_print_verbose(struct wt_status *s)
1111 struct rev_info rev;
1112 struct setup_revision_opt opt;
1113 int dirty_submodules;
1114 const char *c = color(WT_STATUS_HEADER, s);
1116 repo_init_revisions(s->repo, &rev, NULL);
1117 rev.diffopt.flags.allow_textconv = 1;
1118 rev.diffopt.ita_invisible_in_index = 1;
1120 memset(&opt, 0, sizeof(opt));
1121 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
1122 setup_revisions(0, NULL, &rev, &opt);
1124 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1125 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1126 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1127 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1128 rev.diffopt.file = s->fp;
1129 rev.diffopt.close_file = 0;
1131 * If we're not going to stdout, then we definitely don't
1132 * want color, since we are going to the commit message
1133 * file (and even the "auto" setting won't work, since it
1134 * will have checked isatty on stdout). But we then do want
1135 * to insert the scissor line here to reliably remove the
1136 * diff before committing.
1138 if (s->fp != stdout) {
1139 rev.diffopt.use_color = 0;
1140 wt_status_add_cut_line(s->fp);
1142 if (s->verbose > 1 && s->committable) {
1143 /* print_updated() printed a header, so do we */
1144 if (s->fp != stdout)
1145 wt_longstatus_print_trailer(s);
1146 status_printf_ln(s, c, _("Changes to be committed:"));
1147 rev.diffopt.a_prefix = "c/";
1148 rev.diffopt.b_prefix = "i/";
1149 } /* else use prefix as per user config */
1150 run_diff_index(&rev, 1);
1151 if (s->verbose > 1 &&
1152 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1153 status_printf_ln(s, c,
1154 "--------------------------------------------------");
1155 status_printf_ln(s, c, _("Changes not staged for commit:"));
1156 setup_work_tree();
1157 rev.diffopt.a_prefix = "i/";
1158 rev.diffopt.b_prefix = "w/";
1159 run_diff_files(&rev, 0);
1161 release_revisions(&rev);
1164 static void wt_longstatus_print_tracking(struct wt_status *s)
1166 struct strbuf sb = STRBUF_INIT;
1167 const char *cp, *ep, *branch_name;
1168 struct branch *branch;
1169 char comment_line_string[3];
1170 int i;
1171 uint64_t t_begin = 0;
1173 assert(s->branch && !s->is_initial);
1174 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1175 return;
1176 branch = branch_get(branch_name);
1178 t_begin = getnanotime();
1180 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags))
1181 return;
1183 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1184 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1185 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1186 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1187 strbuf_addf(&sb, _("\n"
1188 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1189 "You can use '--no-ahead-behind' to avoid this.\n"),
1190 t_delta_in_ms / 1000.0);
1194 i = 0;
1195 if (s->display_comment_prefix) {
1196 comment_line_string[i++] = comment_line_char;
1197 comment_line_string[i++] = ' ';
1199 comment_line_string[i] = '\0';
1201 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1202 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1203 "%s%.*s", comment_line_string,
1204 (int)(ep - cp), cp);
1205 if (s->display_comment_prefix)
1206 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1207 comment_line_char);
1208 else
1209 fputs("\n", s->fp);
1210 strbuf_release(&sb);
1213 static int uf_was_slow(struct wt_status *s)
1215 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1216 s->untracked_in_ms = 3250;
1217 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1220 static void show_merge_in_progress(struct wt_status *s,
1221 const char *color)
1223 if (has_unmerged(s)) {
1224 status_printf_ln(s, color, _("You have unmerged paths."));
1225 if (s->hints) {
1226 status_printf_ln(s, color,
1227 _(" (fix conflicts and run \"git commit\")"));
1228 status_printf_ln(s, color,
1229 _(" (use \"git merge --abort\" to abort the merge)"));
1231 } else {
1232 status_printf_ln(s, color,
1233 _("All conflicts fixed but you are still merging."));
1234 if (s->hints)
1235 status_printf_ln(s, color,
1236 _(" (use \"git commit\" to conclude merge)"));
1238 wt_longstatus_print_trailer(s);
1241 static void show_am_in_progress(struct wt_status *s,
1242 const char *color)
1244 int am_empty_patch;
1246 status_printf_ln(s, color,
1247 _("You are in the middle of an am session."));
1248 if (s->state.am_empty_patch)
1249 status_printf_ln(s, color,
1250 _("The current patch is empty."));
1251 if (s->hints) {
1252 am_empty_patch = s->state.am_empty_patch;
1253 if (!am_empty_patch)
1254 status_printf_ln(s, color,
1255 _(" (fix conflicts and then run \"git am --continue\")"));
1256 status_printf_ln(s, color,
1257 _(" (use \"git am --skip\" to skip this patch)"));
1258 if (am_empty_patch)
1259 status_printf_ln(s, color,
1260 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1261 status_printf_ln(s, color,
1262 _(" (use \"git am --abort\" to restore the original branch)"));
1264 wt_longstatus_print_trailer(s);
1267 static char *read_line_from_git_path(const char *filename)
1269 struct strbuf buf = STRBUF_INIT;
1270 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1272 if (!fp) {
1273 strbuf_release(&buf);
1274 return NULL;
1276 strbuf_getline_lf(&buf, fp);
1277 if (!fclose(fp)) {
1278 return strbuf_detach(&buf, NULL);
1279 } else {
1280 strbuf_release(&buf);
1281 return NULL;
1285 static int split_commit_in_progress(struct wt_status *s)
1287 int split_in_progress = 0;
1288 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1290 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1291 !s->branch || strcmp(s->branch, "HEAD"))
1292 return 0;
1294 head = read_line_from_git_path("HEAD");
1295 orig_head = read_line_from_git_path("ORIG_HEAD");
1296 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1297 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1299 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1300 ; /* fall through, no split in progress */
1301 else if (!strcmp(rebase_amend, rebase_orig_head))
1302 split_in_progress = !!strcmp(head, rebase_amend);
1303 else if (strcmp(orig_head, rebase_orig_head))
1304 split_in_progress = 1;
1306 free(head);
1307 free(orig_head);
1308 free(rebase_amend);
1309 free(rebase_orig_head);
1311 return split_in_progress;
1315 * Turn
1316 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1317 * into
1318 * "pick d6a2f03 some message"
1320 * The function assumes that the line does not contain useless spaces
1321 * before or after the command.
1323 static void abbrev_oid_in_line(struct strbuf *line)
1325 struct strbuf **split;
1326 int i;
1328 if (starts_with(line->buf, "exec ") ||
1329 starts_with(line->buf, "x ") ||
1330 starts_with(line->buf, "label ") ||
1331 starts_with(line->buf, "l "))
1332 return;
1334 split = strbuf_split_max(line, ' ', 3);
1335 if (split[0] && split[1]) {
1336 struct object_id oid;
1339 * strbuf_split_max left a space. Trim it and re-add
1340 * it after abbreviation.
1342 strbuf_trim(split[1]);
1343 if (!get_oid(split[1]->buf, &oid)) {
1344 strbuf_reset(split[1]);
1345 strbuf_add_unique_abbrev(split[1], &oid,
1346 DEFAULT_ABBREV);
1347 strbuf_addch(split[1], ' ');
1348 strbuf_reset(line);
1349 for (i = 0; split[i]; i++)
1350 strbuf_addbuf(line, split[i]);
1353 strbuf_list_free(split);
1356 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1358 struct strbuf line = STRBUF_INIT;
1359 FILE *f = fopen(git_path("%s", fname), "r");
1361 if (!f) {
1362 if (errno == ENOENT)
1363 return -1;
1364 die_errno("Could not open file %s for reading",
1365 git_path("%s", fname));
1367 while (!strbuf_getline_lf(&line, f)) {
1368 if (line.len && line.buf[0] == comment_line_char)
1369 continue;
1370 strbuf_trim(&line);
1371 if (!line.len)
1372 continue;
1373 abbrev_oid_in_line(&line);
1374 string_list_append(lines, line.buf);
1376 fclose(f);
1377 strbuf_release(&line);
1378 return 0;
1381 static void show_rebase_information(struct wt_status *s,
1382 const char *color)
1384 if (s->state.rebase_interactive_in_progress) {
1385 int i;
1386 int nr_lines_to_show = 2;
1388 struct string_list have_done = STRING_LIST_INIT_DUP;
1389 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1391 read_rebase_todolist("rebase-merge/done", &have_done);
1392 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1393 &yet_to_do))
1394 status_printf_ln(s, color,
1395 _("git-rebase-todo is missing."));
1396 if (have_done.nr == 0)
1397 status_printf_ln(s, color, _("No commands done."));
1398 else {
1399 status_printf_ln(s, color,
1400 Q_("Last command done (%"PRIuMAX" command done):",
1401 "Last commands done (%"PRIuMAX" commands done):",
1402 have_done.nr),
1403 (uintmax_t)have_done.nr);
1404 for (i = (have_done.nr > nr_lines_to_show)
1405 ? have_done.nr - nr_lines_to_show : 0;
1406 i < have_done.nr;
1407 i++)
1408 status_printf_ln(s, color, " %s", have_done.items[i].string);
1409 if (have_done.nr > nr_lines_to_show && s->hints)
1410 status_printf_ln(s, color,
1411 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1414 if (yet_to_do.nr == 0)
1415 status_printf_ln(s, color,
1416 _("No commands remaining."));
1417 else {
1418 status_printf_ln(s, color,
1419 Q_("Next command to do (%"PRIuMAX" remaining command):",
1420 "Next commands to do (%"PRIuMAX" remaining commands):",
1421 yet_to_do.nr),
1422 (uintmax_t)yet_to_do.nr);
1423 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1424 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1425 if (s->hints)
1426 status_printf_ln(s, color,
1427 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1429 string_list_clear(&yet_to_do, 0);
1430 string_list_clear(&have_done, 0);
1434 static void print_rebase_state(struct wt_status *s,
1435 const char *color)
1437 if (s->state.branch)
1438 status_printf_ln(s, color,
1439 _("You are currently rebasing branch '%s' on '%s'."),
1440 s->state.branch,
1441 s->state.onto);
1442 else
1443 status_printf_ln(s, color,
1444 _("You are currently rebasing."));
1447 static void show_rebase_in_progress(struct wt_status *s,
1448 const char *color)
1450 struct stat st;
1452 show_rebase_information(s, color);
1453 if (has_unmerged(s)) {
1454 print_rebase_state(s, color);
1455 if (s->hints) {
1456 status_printf_ln(s, color,
1457 _(" (fix conflicts and then run \"git rebase --continue\")"));
1458 status_printf_ln(s, color,
1459 _(" (use \"git rebase --skip\" to skip this patch)"));
1460 status_printf_ln(s, color,
1461 _(" (use \"git rebase --abort\" to check out the original branch)"));
1463 } else if (s->state.rebase_in_progress ||
1464 !stat(git_path_merge_msg(s->repo), &st)) {
1465 print_rebase_state(s, color);
1466 if (s->hints)
1467 status_printf_ln(s, color,
1468 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1469 } else if (split_commit_in_progress(s)) {
1470 if (s->state.branch)
1471 status_printf_ln(s, color,
1472 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1473 s->state.branch,
1474 s->state.onto);
1475 else
1476 status_printf_ln(s, color,
1477 _("You are currently splitting a commit during a rebase."));
1478 if (s->hints)
1479 status_printf_ln(s, color,
1480 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1481 } else {
1482 if (s->state.branch)
1483 status_printf_ln(s, color,
1484 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1485 s->state.branch,
1486 s->state.onto);
1487 else
1488 status_printf_ln(s, color,
1489 _("You are currently editing a commit during a rebase."));
1490 if (s->hints && !s->amend) {
1491 status_printf_ln(s, color,
1492 _(" (use \"git commit --amend\" to amend the current commit)"));
1493 status_printf_ln(s, color,
1494 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1497 wt_longstatus_print_trailer(s);
1500 static void show_cherry_pick_in_progress(struct wt_status *s,
1501 const char *color)
1503 if (is_null_oid(&s->state.cherry_pick_head_oid))
1504 status_printf_ln(s, color,
1505 _("Cherry-pick currently in progress."));
1506 else
1507 status_printf_ln(s, color,
1508 _("You are currently cherry-picking commit %s."),
1509 find_unique_abbrev(&s->state.cherry_pick_head_oid,
1510 DEFAULT_ABBREV));
1512 if (s->hints) {
1513 if (has_unmerged(s))
1514 status_printf_ln(s, color,
1515 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1516 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1517 status_printf_ln(s, color,
1518 _(" (run \"git cherry-pick --continue\" to continue)"));
1519 else
1520 status_printf_ln(s, color,
1521 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1522 status_printf_ln(s, color,
1523 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1524 status_printf_ln(s, color,
1525 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1527 wt_longstatus_print_trailer(s);
1530 static void show_revert_in_progress(struct wt_status *s,
1531 const char *color)
1533 if (is_null_oid(&s->state.revert_head_oid))
1534 status_printf_ln(s, color,
1535 _("Revert currently in progress."));
1536 else
1537 status_printf_ln(s, color,
1538 _("You are currently reverting commit %s."),
1539 find_unique_abbrev(&s->state.revert_head_oid,
1540 DEFAULT_ABBREV));
1541 if (s->hints) {
1542 if (has_unmerged(s))
1543 status_printf_ln(s, color,
1544 _(" (fix conflicts and run \"git revert --continue\")"));
1545 else if (is_null_oid(&s->state.revert_head_oid))
1546 status_printf_ln(s, color,
1547 _(" (run \"git revert --continue\" to continue)"));
1548 else
1549 status_printf_ln(s, color,
1550 _(" (all conflicts fixed: run \"git revert --continue\")"));
1551 status_printf_ln(s, color,
1552 _(" (use \"git revert --skip\" to skip this patch)"));
1553 status_printf_ln(s, color,
1554 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1556 wt_longstatus_print_trailer(s);
1559 static void show_bisect_in_progress(struct wt_status *s,
1560 const char *color)
1562 if (s->state.branch)
1563 status_printf_ln(s, color,
1564 _("You are currently bisecting, started from branch '%s'."),
1565 s->state.branch);
1566 else
1567 status_printf_ln(s, color,
1568 _("You are currently bisecting."));
1569 if (s->hints)
1570 status_printf_ln(s, color,
1571 _(" (use \"git bisect reset\" to get back to the original branch)"));
1572 wt_longstatus_print_trailer(s);
1575 static void show_sparse_checkout_in_use(struct wt_status *s,
1576 const char *color)
1578 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1579 return;
1581 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1582 status_printf_ln(s, color, _("You are in a sparse checkout."));
1583 else
1584 status_printf_ln(s, color,
1585 _("You are in a sparse checkout with %d%% of tracked files present."),
1586 s->state.sparse_checkout_percentage);
1587 wt_longstatus_print_trailer(s);
1591 * Extract branch information from rebase/bisect
1593 static char *get_branch(const struct worktree *wt, const char *path)
1595 struct strbuf sb = STRBUF_INIT;
1596 struct object_id oid;
1597 const char *branch_name;
1599 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1600 goto got_nothing;
1602 while (sb.len && sb.buf[sb.len - 1] == '\n')
1603 strbuf_setlen(&sb, sb.len - 1);
1604 if (!sb.len)
1605 goto got_nothing;
1606 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1607 strbuf_remove(&sb, 0, branch_name - sb.buf);
1608 else if (starts_with(sb.buf, "refs/"))
1610 else if (!get_oid_hex(sb.buf, &oid)) {
1611 strbuf_reset(&sb);
1612 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1613 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1614 goto got_nothing;
1615 else /* bisect */
1617 return strbuf_detach(&sb, NULL);
1619 got_nothing:
1620 strbuf_release(&sb);
1621 return NULL;
1624 struct grab_1st_switch_cbdata {
1625 struct strbuf buf;
1626 struct object_id noid;
1629 static int grab_1st_switch(struct object_id *ooid UNUSED,
1630 struct object_id *noid,
1631 const char *email UNUSED,
1632 timestamp_t timestamp UNUSED, int tz UNUSED,
1633 const char *message, void *cb_data)
1635 struct grab_1st_switch_cbdata *cb = cb_data;
1636 const char *target = NULL, *end;
1638 if (!skip_prefix(message, "checkout: moving from ", &message))
1639 return 0;
1640 target = strstr(message, " to ");
1641 if (!target)
1642 return 0;
1643 target += strlen(" to ");
1644 strbuf_reset(&cb->buf);
1645 oidcpy(&cb->noid, noid);
1646 end = strchrnul(target, '\n');
1647 strbuf_add(&cb->buf, target, end - target);
1648 if (!strcmp(cb->buf.buf, "HEAD")) {
1649 /* HEAD is relative. Resolve it to the right reflog entry. */
1650 strbuf_reset(&cb->buf);
1651 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1653 return 1;
1656 static void wt_status_get_detached_from(struct repository *r,
1657 struct wt_status_state *state)
1659 struct grab_1st_switch_cbdata cb;
1660 struct commit *commit;
1661 struct object_id oid;
1662 char *ref = NULL;
1664 strbuf_init(&cb.buf, 0);
1665 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1666 strbuf_release(&cb.buf);
1667 return;
1670 if (dwim_ref(cb.buf.buf, cb.buf.len, &oid, &ref, 1) == 1 &&
1671 /* oid is a commit? match without further lookup */
1672 (oideq(&cb.noid, &oid) ||
1673 /* perhaps oid is a tag, try to dereference to a commit */
1674 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1675 oideq(&cb.noid, &commit->object.oid)))) {
1676 const char *from = ref;
1677 if (!skip_prefix(from, "refs/tags/", &from))
1678 skip_prefix(from, "refs/remotes/", &from);
1679 state->detached_from = xstrdup(from);
1680 } else
1681 state->detached_from =
1682 xstrdup(find_unique_abbrev(&cb.noid, DEFAULT_ABBREV));
1683 oidcpy(&state->detached_oid, &cb.noid);
1684 state->detached_at = !get_oid("HEAD", &oid) &&
1685 oideq(&oid, &state->detached_oid);
1687 free(ref);
1688 strbuf_release(&cb.buf);
1691 int wt_status_check_rebase(const struct worktree *wt,
1692 struct wt_status_state *state)
1694 struct stat st;
1696 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1697 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1698 state->am_in_progress = 1;
1699 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1700 state->am_empty_patch = 1;
1701 } else {
1702 state->rebase_in_progress = 1;
1703 state->branch = get_branch(wt, "rebase-apply/head-name");
1704 state->onto = get_branch(wt, "rebase-apply/onto");
1706 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1707 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1708 state->rebase_interactive_in_progress = 1;
1709 else
1710 state->rebase_in_progress = 1;
1711 state->branch = get_branch(wt, "rebase-merge/head-name");
1712 state->onto = get_branch(wt, "rebase-merge/onto");
1713 } else
1714 return 0;
1715 return 1;
1718 int wt_status_check_bisect(const struct worktree *wt,
1719 struct wt_status_state *state)
1721 struct stat st;
1723 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1724 state->bisect_in_progress = 1;
1725 state->branch = get_branch(wt, "BISECT_START");
1726 return 1;
1728 return 0;
1731 static void wt_status_check_sparse_checkout(struct repository *r,
1732 struct wt_status_state *state)
1734 int skip_worktree = 0;
1735 int i;
1737 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1739 * Don't compute percentage of checked out files if we
1740 * aren't in a sparse checkout or would get division by 0.
1742 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1743 return;
1746 if (r->index->sparse_index) {
1747 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1748 return;
1751 for (i = 0; i < r->index->cache_nr; i++) {
1752 struct cache_entry *ce = r->index->cache[i];
1753 if (ce_skip_worktree(ce))
1754 skip_worktree++;
1757 state->sparse_checkout_percentage =
1758 100 - (100 * skip_worktree)/r->index->cache_nr;
1761 void wt_status_get_state(struct repository *r,
1762 struct wt_status_state *state,
1763 int get_detached_from)
1765 struct stat st;
1766 struct object_id oid;
1767 enum replay_action action;
1769 if (!stat(git_path_merge_head(r), &st)) {
1770 wt_status_check_rebase(NULL, state);
1771 state->merge_in_progress = 1;
1772 } else if (wt_status_check_rebase(NULL, state)) {
1773 ; /* all set */
1774 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1775 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1776 state->cherry_pick_in_progress = 1;
1777 oidcpy(&state->cherry_pick_head_oid, &oid);
1779 wt_status_check_bisect(NULL, state);
1780 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1781 !get_oid("REVERT_HEAD", &oid)) {
1782 state->revert_in_progress = 1;
1783 oidcpy(&state->revert_head_oid, &oid);
1785 if (!sequencer_get_last_command(r, &action)) {
1786 if (action == REPLAY_PICK) {
1787 state->cherry_pick_in_progress = 1;
1788 oidcpy(&state->cherry_pick_head_oid, null_oid());
1789 } else {
1790 state->revert_in_progress = 1;
1791 oidcpy(&state->revert_head_oid, null_oid());
1794 if (get_detached_from)
1795 wt_status_get_detached_from(r, state);
1796 wt_status_check_sparse_checkout(r, state);
1799 static void wt_longstatus_print_state(struct wt_status *s)
1801 const char *state_color = color(WT_STATUS_HEADER, s);
1802 struct wt_status_state *state = &s->state;
1804 if (state->merge_in_progress) {
1805 if (state->rebase_interactive_in_progress) {
1806 show_rebase_information(s, state_color);
1807 fputs("\n", s->fp);
1809 show_merge_in_progress(s, state_color);
1810 } else if (state->am_in_progress)
1811 show_am_in_progress(s, state_color);
1812 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1813 show_rebase_in_progress(s, state_color);
1814 else if (state->cherry_pick_in_progress)
1815 show_cherry_pick_in_progress(s, state_color);
1816 else if (state->revert_in_progress)
1817 show_revert_in_progress(s, state_color);
1818 if (state->bisect_in_progress)
1819 show_bisect_in_progress(s, state_color);
1821 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1822 show_sparse_checkout_in_use(s, state_color);
1825 static void wt_longstatus_print(struct wt_status *s)
1827 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1828 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1829 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1831 if (s->branch) {
1832 const char *on_what = _("On branch ");
1833 const char *branch_name = s->branch;
1834 if (!strcmp(branch_name, "HEAD")) {
1835 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1836 if (s->state.rebase_in_progress ||
1837 s->state.rebase_interactive_in_progress) {
1838 if (s->state.rebase_interactive_in_progress)
1839 on_what = _("interactive rebase in progress; onto ");
1840 else
1841 on_what = _("rebase in progress; onto ");
1842 branch_name = s->state.onto;
1843 } else if (s->state.detached_from) {
1844 branch_name = s->state.detached_from;
1845 if (s->state.detached_at)
1846 on_what = _("HEAD detached at ");
1847 else
1848 on_what = _("HEAD detached from ");
1849 } else {
1850 branch_name = "";
1851 on_what = _("Not currently on any branch.");
1853 } else
1854 skip_prefix(branch_name, "refs/heads/", &branch_name);
1855 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1856 status_printf_more(s, branch_status_color, "%s", on_what);
1857 status_printf_more(s, branch_color, "%s\n", branch_name);
1858 if (!s->is_initial)
1859 wt_longstatus_print_tracking(s);
1862 wt_longstatus_print_state(s);
1864 if (s->is_initial) {
1865 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1866 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1867 s->commit_template
1868 ? _("Initial commit")
1869 : _("No commits yet"));
1870 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1873 wt_longstatus_print_updated(s);
1874 wt_longstatus_print_unmerged(s);
1875 wt_longstatus_print_changed(s);
1876 if (s->submodule_summary &&
1877 (!s->ignore_submodule_arg ||
1878 strcmp(s->ignore_submodule_arg, "all"))) {
1879 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1880 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1882 if (s->show_untracked_files) {
1883 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1884 if (s->show_ignored_mode)
1885 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1886 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1887 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1888 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1889 status_printf_ln(s, GIT_COLOR_NORMAL,
1890 _("It took %.2f seconds to enumerate untracked files,\n"
1891 "but the results were cached, and subsequent runs may be faster."),
1892 s->untracked_in_ms / 1000.0);
1893 } else {
1894 status_printf_ln(s, GIT_COLOR_NORMAL,
1895 _("It took %.2f seconds to enumerate untracked files."),
1896 s->untracked_in_ms / 1000.0);
1898 status_printf_ln(s, GIT_COLOR_NORMAL,
1899 _("See 'git help status' for information on how to improve this."));
1900 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1902 } else if (s->committable)
1903 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1904 s->hints
1905 ? _(" (use -u option to show untracked files)") : "");
1907 if (s->verbose)
1908 wt_longstatus_print_verbose(s);
1909 if (!s->committable) {
1910 if (s->amend)
1911 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1912 else if (s->nowarn)
1913 ; /* nothing */
1914 else if (s->workdir_dirty) {
1915 if (s->hints)
1916 fprintf(s->fp, _("no changes added to commit "
1917 "(use \"git add\" and/or "
1918 "\"git commit -a\")\n"));
1919 else
1920 fprintf(s->fp, _("no changes added to "
1921 "commit\n"));
1922 } else if (s->untracked.nr) {
1923 if (s->hints)
1924 fprintf(s->fp, _("nothing added to commit but "
1925 "untracked files present (use "
1926 "\"git add\" to track)\n"));
1927 else
1928 fprintf(s->fp, _("nothing added to commit but "
1929 "untracked files present\n"));
1930 } else if (s->is_initial) {
1931 if (s->hints)
1932 fprintf(s->fp, _("nothing to commit (create/"
1933 "copy files and use \"git "
1934 "add\" to track)\n"));
1935 else
1936 fprintf(s->fp, _("nothing to commit\n"));
1937 } else if (!s->show_untracked_files) {
1938 if (s->hints)
1939 fprintf(s->fp, _("nothing to commit (use -u to "
1940 "show untracked files)\n"));
1941 else
1942 fprintf(s->fp, _("nothing to commit\n"));
1943 } else
1944 fprintf(s->fp, _("nothing to commit, working tree "
1945 "clean\n"));
1947 if(s->show_stash)
1948 wt_longstatus_print_stash_summary(s);
1951 static void wt_shortstatus_unmerged(struct string_list_item *it,
1952 struct wt_status *s)
1954 struct wt_status_change_data *d = it->util;
1955 const char *how = "??";
1957 switch (d->stagemask) {
1958 case 1: how = "DD"; break; /* both deleted */
1959 case 2: how = "AU"; break; /* added by us */
1960 case 3: how = "UD"; break; /* deleted by them */
1961 case 4: how = "UA"; break; /* added by them */
1962 case 5: how = "DU"; break; /* deleted by us */
1963 case 6: how = "AA"; break; /* both added */
1964 case 7: how = "UU"; break; /* both modified */
1966 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1967 if (s->null_termination) {
1968 fprintf(s->fp, " %s%c", it->string, 0);
1969 } else {
1970 struct strbuf onebuf = STRBUF_INIT;
1971 const char *one;
1972 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1973 fprintf(s->fp, " %s\n", one);
1974 strbuf_release(&onebuf);
1978 static void wt_shortstatus_status(struct string_list_item *it,
1979 struct wt_status *s)
1981 struct wt_status_change_data *d = it->util;
1983 if (d->index_status)
1984 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1985 else
1986 fputc(' ', s->fp);
1987 if (d->worktree_status)
1988 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1989 else
1990 fputc(' ', s->fp);
1991 fputc(' ', s->fp);
1992 if (s->null_termination) {
1993 fprintf(s->fp, "%s%c", it->string, 0);
1994 if (d->rename_source)
1995 fprintf(s->fp, "%s%c", d->rename_source, 0);
1996 } else {
1997 struct strbuf onebuf = STRBUF_INIT;
1998 const char *one;
2000 if (d->rename_source) {
2001 one = quote_path(d->rename_source, s->prefix, &onebuf,
2002 QUOTE_PATH_QUOTE_SP);
2003 fprintf(s->fp, "%s -> ", one);
2004 strbuf_release(&onebuf);
2006 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2007 fprintf(s->fp, "%s\n", one);
2008 strbuf_release(&onebuf);
2012 static void wt_shortstatus_other(struct string_list_item *it,
2013 struct wt_status *s, const char *sign)
2015 if (s->null_termination) {
2016 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2017 } else {
2018 struct strbuf onebuf = STRBUF_INIT;
2019 const char *one;
2020 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2021 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2022 fprintf(s->fp, " %s\n", one);
2023 strbuf_release(&onebuf);
2027 static void wt_shortstatus_print_tracking(struct wt_status *s)
2029 struct branch *branch;
2030 const char *header_color = color(WT_STATUS_HEADER, s);
2031 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2032 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2034 const char *base;
2035 char *short_base;
2036 const char *branch_name;
2037 int num_ours, num_theirs, sti;
2038 int upstream_is_gone = 0;
2040 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2042 if (!s->branch)
2043 return;
2044 branch_name = s->branch;
2046 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2048 if (s->is_initial)
2049 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2051 if (!strcmp(s->branch, "HEAD")) {
2052 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2053 LABEL(N_("HEAD (no branch)")));
2054 goto conclude;
2057 skip_prefix(branch_name, "refs/heads/", &branch_name);
2059 branch = branch_get(branch_name);
2061 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2063 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2064 0, s->ahead_behind_flags);
2065 if (sti < 0) {
2066 if (!base)
2067 goto conclude;
2069 upstream_is_gone = 1;
2072 short_base = shorten_unambiguous_ref(base, 0);
2073 color_fprintf(s->fp, header_color, "...");
2074 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2075 free(short_base);
2077 if (!upstream_is_gone && !sti)
2078 goto conclude;
2080 color_fprintf(s->fp, header_color, " [");
2081 if (upstream_is_gone) {
2082 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2083 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2084 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2085 } else if (!num_ours) {
2086 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2087 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2088 } else if (!num_theirs) {
2089 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2090 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2091 } else {
2092 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2093 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2094 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2095 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2098 color_fprintf(s->fp, header_color, "]");
2099 conclude:
2100 fputc(s->null_termination ? '\0' : '\n', s->fp);
2103 static void wt_shortstatus_print(struct wt_status *s)
2105 struct string_list_item *it;
2107 if (s->show_branch)
2108 wt_shortstatus_print_tracking(s);
2110 for_each_string_list_item(it, &s->change) {
2111 struct wt_status_change_data *d = it->util;
2113 if (d->stagemask)
2114 wt_shortstatus_unmerged(it, s);
2115 else
2116 wt_shortstatus_status(it, s);
2118 for_each_string_list_item(it, &s->untracked)
2119 wt_shortstatus_other(it, s, "??");
2121 for_each_string_list_item(it, &s->ignored)
2122 wt_shortstatus_other(it, s, "!!");
2125 static void wt_porcelain_print(struct wt_status *s)
2127 s->use_color = 0;
2128 s->relative_paths = 0;
2129 s->prefix = NULL;
2130 s->no_gettext = 1;
2131 wt_shortstatus_print(s);
2135 * Print branch information for porcelain v2 output. These lines
2136 * are printed when the '--branch' parameter is given.
2138 * # branch.oid <commit><eol>
2139 * # branch.head <head><eol>
2140 * [# branch.upstream <upstream><eol>
2141 * [# branch.ab +<ahead> -<behind><eol>]]
2143 * <commit> ::= the current commit hash or the literal
2144 * "(initial)" to indicate an initialized repo
2145 * with no commits.
2147 * <head> ::= <branch_name> the current branch name or
2148 * "(detached)" literal when detached head or
2149 * "(unknown)" when something is wrong.
2151 * <upstream> ::= the upstream branch name, when set.
2153 * <ahead> ::= integer ahead value or '?'.
2155 * <behind> ::= integer behind value or '?'.
2157 * The end-of-line is defined by the -z flag.
2159 * <eol> ::= NUL when -z,
2160 * LF when NOT -z.
2162 * When an upstream is set and present, the 'branch.ab' line will
2163 * be printed with the ahead/behind counts for the branch and the
2164 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2165 * are different, '?' will be substituted for the actual count.
2167 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2169 struct branch *branch;
2170 const char *base;
2171 const char *branch_name;
2172 int ab_info, nr_ahead, nr_behind;
2173 char eol = s->null_termination ? '\0' : '\n';
2175 fprintf(s->fp, "# branch.oid %s%c",
2176 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2177 eol);
2179 if (!s->branch)
2180 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2181 else {
2182 if (!strcmp(s->branch, "HEAD")) {
2183 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2185 if (s->state.rebase_in_progress ||
2186 s->state.rebase_interactive_in_progress)
2187 branch_name = s->state.onto;
2188 else if (s->state.detached_from)
2189 branch_name = s->state.detached_from;
2190 else
2191 branch_name = "";
2192 } else {
2193 branch_name = NULL;
2194 skip_prefix(s->branch, "refs/heads/", &branch_name);
2196 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2199 /* Lookup stats on the upstream tracking branch, if set. */
2200 branch = branch_get(branch_name);
2201 base = NULL;
2202 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2203 &base, 0, s->ahead_behind_flags);
2204 if (base) {
2205 base = shorten_unambiguous_ref(base, 0);
2206 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2207 free((char *)base);
2209 if (ab_info > 0) {
2210 /* different */
2211 if (nr_ahead || nr_behind)
2212 fprintf(s->fp, "# branch.ab +%d -%d%c",
2213 nr_ahead, nr_behind, eol);
2214 else
2215 fprintf(s->fp, "# branch.ab +? -?%c",
2216 eol);
2217 } else if (!ab_info) {
2218 /* same */
2219 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2226 * Print the stash count in a porcelain-friendly format
2228 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2230 int stash_count = count_stash_entries();
2231 char eol = s->null_termination ? '\0' : '\n';
2233 if (stash_count > 0)
2234 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2238 * Convert various submodule status values into a
2239 * fixed-length string of characters in the buffer provided.
2241 static void wt_porcelain_v2_submodule_state(
2242 struct wt_status_change_data *d,
2243 char sub[5])
2245 if (S_ISGITLINK(d->mode_head) ||
2246 S_ISGITLINK(d->mode_index) ||
2247 S_ISGITLINK(d->mode_worktree)) {
2248 sub[0] = 'S';
2249 sub[1] = d->new_submodule_commits ? 'C' : '.';
2250 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2251 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2252 } else {
2253 sub[0] = 'N';
2254 sub[1] = '.';
2255 sub[2] = '.';
2256 sub[3] = '.';
2258 sub[4] = 0;
2262 * Fix-up changed entries before we print them.
2264 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2266 struct wt_status_change_data *d = it->util;
2268 if (!d->index_status) {
2270 * This entry is unchanged in the index (relative to the head).
2271 * Therefore, the collect_updated_cb was never called for this
2272 * entry (during the head-vs-index scan) and so the head column
2273 * fields were never set.
2275 * We must have data for the index column (from the
2276 * index-vs-worktree scan (otherwise, this entry should not be
2277 * in the list of changes)).
2279 * Copy index column fields to the head column, so that our
2280 * output looks complete.
2282 assert(d->mode_head == 0);
2283 d->mode_head = d->mode_index;
2284 oidcpy(&d->oid_head, &d->oid_index);
2287 if (!d->worktree_status) {
2289 * This entry is unchanged in the worktree (relative to the index).
2290 * Therefore, the collect_changed_cb was never called for this entry
2291 * (during the index-vs-worktree scan) and so the worktree column
2292 * fields were never set.
2294 * We must have data for the index column (from the head-vs-index
2295 * scan).
2297 * Copy the index column fields to the worktree column so that
2298 * our output looks complete.
2300 * Note that we only have a mode field in the worktree column
2301 * because the scan code tries really hard to not have to compute it.
2303 assert(d->mode_worktree == 0);
2304 d->mode_worktree = d->mode_index;
2309 * Print porcelain v2 info for tracked entries with changes.
2311 static void wt_porcelain_v2_print_changed_entry(
2312 struct string_list_item *it,
2313 struct wt_status *s)
2315 struct wt_status_change_data *d = it->util;
2316 struct strbuf buf = STRBUF_INIT;
2317 struct strbuf buf_from = STRBUF_INIT;
2318 const char *path = NULL;
2319 const char *path_from = NULL;
2320 char key[3];
2321 char submodule_token[5];
2322 char sep_char, eol_char;
2324 wt_porcelain_v2_fix_up_changed(it);
2325 wt_porcelain_v2_submodule_state(d, submodule_token);
2327 key[0] = d->index_status ? d->index_status : '.';
2328 key[1] = d->worktree_status ? d->worktree_status : '.';
2329 key[2] = 0;
2331 if (s->null_termination) {
2333 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2334 * A single NUL character separates them.
2336 sep_char = '\0';
2337 eol_char = '\0';
2338 path = it->string;
2339 path_from = d->rename_source;
2340 } else {
2342 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2343 * The source path is only present when necessary.
2344 * A single TAB separates them (because paths can contain spaces
2345 * which are not escaped and C-quoting does escape TAB characters).
2347 sep_char = '\t';
2348 eol_char = '\n';
2349 path = quote_path(it->string, s->prefix, &buf, 0);
2350 if (d->rename_source)
2351 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2354 if (path_from)
2355 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2356 key, submodule_token,
2357 d->mode_head, d->mode_index, d->mode_worktree,
2358 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2359 d->rename_status, d->rename_score,
2360 path, sep_char, path_from, eol_char);
2361 else
2362 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2363 key, submodule_token,
2364 d->mode_head, d->mode_index, d->mode_worktree,
2365 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2366 path, eol_char);
2368 strbuf_release(&buf);
2369 strbuf_release(&buf_from);
2373 * Print porcelain v2 status info for unmerged entries.
2375 static void wt_porcelain_v2_print_unmerged_entry(
2376 struct string_list_item *it,
2377 struct wt_status *s)
2379 struct wt_status_change_data *d = it->util;
2380 struct index_state *istate = s->repo->index;
2381 const struct cache_entry *ce;
2382 struct strbuf buf_index = STRBUF_INIT;
2383 const char *path_index = NULL;
2384 int pos, stage, sum;
2385 struct {
2386 int mode;
2387 struct object_id oid;
2388 } stages[3];
2389 char *key;
2390 char submodule_token[5];
2391 char unmerged_prefix = 'u';
2392 char eol_char = s->null_termination ? '\0' : '\n';
2394 wt_porcelain_v2_submodule_state(d, submodule_token);
2396 switch (d->stagemask) {
2397 case 1: key = "DD"; break; /* both deleted */
2398 case 2: key = "AU"; break; /* added by us */
2399 case 3: key = "UD"; break; /* deleted by them */
2400 case 4: key = "UA"; break; /* added by them */
2401 case 5: key = "DU"; break; /* deleted by us */
2402 case 6: key = "AA"; break; /* both added */
2403 case 7: key = "UU"; break; /* both modified */
2404 default:
2405 BUG("unhandled unmerged status %x", d->stagemask);
2409 * Disregard d.aux.porcelain_v2 data that we accumulated
2410 * for the head and index columns during the scans and
2411 * replace with the actual stage data.
2413 * Note that this is a last-one-wins for each the individual
2414 * stage [123] columns in the event of multiple cache entries
2415 * for same stage.
2417 memset(stages, 0, sizeof(stages));
2418 sum = 0;
2419 pos = index_name_pos(istate, it->string, strlen(it->string));
2420 assert(pos < 0);
2421 pos = -pos-1;
2422 while (pos < istate->cache_nr) {
2423 ce = istate->cache[pos++];
2424 stage = ce_stage(ce);
2425 if (strcmp(ce->name, it->string) || !stage)
2426 break;
2427 stages[stage - 1].mode = ce->ce_mode;
2428 oidcpy(&stages[stage - 1].oid, &ce->oid);
2429 sum |= (1 << (stage - 1));
2431 if (sum != d->stagemask)
2432 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2434 if (s->null_termination)
2435 path_index = it->string;
2436 else
2437 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2439 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2440 unmerged_prefix, key, submodule_token,
2441 stages[0].mode, /* stage 1 */
2442 stages[1].mode, /* stage 2 */
2443 stages[2].mode, /* stage 3 */
2444 d->mode_worktree,
2445 oid_to_hex(&stages[0].oid), /* stage 1 */
2446 oid_to_hex(&stages[1].oid), /* stage 2 */
2447 oid_to_hex(&stages[2].oid), /* stage 3 */
2448 path_index,
2449 eol_char);
2451 strbuf_release(&buf_index);
2455 * Print porcelain V2 status info for untracked and ignored entries.
2457 static void wt_porcelain_v2_print_other(
2458 struct string_list_item *it,
2459 struct wt_status *s,
2460 char prefix)
2462 struct strbuf buf = STRBUF_INIT;
2463 const char *path;
2464 char eol_char;
2466 if (s->null_termination) {
2467 path = it->string;
2468 eol_char = '\0';
2469 } else {
2470 path = quote_path(it->string, s->prefix, &buf, 0);
2471 eol_char = '\n';
2474 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2476 strbuf_release(&buf);
2480 * Print porcelain V2 status.
2482 * [<v2_branch>]
2483 * [<v2_changed_items>]*
2484 * [<v2_unmerged_items>]*
2485 * [<v2_untracked_items>]*
2486 * [<v2_ignored_items>]*
2489 static void wt_porcelain_v2_print(struct wt_status *s)
2491 struct wt_status_change_data *d;
2492 struct string_list_item *it;
2493 int i;
2495 if (s->show_branch)
2496 wt_porcelain_v2_print_tracking(s);
2498 if (s->show_stash)
2499 wt_porcelain_v2_print_stash(s);
2501 for (i = 0; i < s->change.nr; i++) {
2502 it = &(s->change.items[i]);
2503 d = it->util;
2504 if (!d->stagemask)
2505 wt_porcelain_v2_print_changed_entry(it, s);
2508 for (i = 0; i < s->change.nr; i++) {
2509 it = &(s->change.items[i]);
2510 d = it->util;
2511 if (d->stagemask)
2512 wt_porcelain_v2_print_unmerged_entry(it, s);
2515 for (i = 0; i < s->untracked.nr; i++) {
2516 it = &(s->untracked.items[i]);
2517 wt_porcelain_v2_print_other(it, s, '?');
2520 for (i = 0; i < s->ignored.nr; i++) {
2521 it = &(s->ignored.items[i]);
2522 wt_porcelain_v2_print_other(it, s, '!');
2526 void wt_status_print(struct wt_status *s)
2528 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2529 trace2_data_intmax("status", s->repo, "count/untracked",
2530 s->untracked.nr);
2531 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2533 trace2_region_enter("status", "print", s->repo);
2535 switch (s->status_format) {
2536 case STATUS_FORMAT_SHORT:
2537 wt_shortstatus_print(s);
2538 break;
2539 case STATUS_FORMAT_PORCELAIN:
2540 wt_porcelain_print(s);
2541 break;
2542 case STATUS_FORMAT_PORCELAIN_V2:
2543 wt_porcelain_v2_print(s);
2544 break;
2545 case STATUS_FORMAT_UNSPECIFIED:
2546 BUG("finalize_deferred_config() should have been called");
2547 break;
2548 case STATUS_FORMAT_NONE:
2549 case STATUS_FORMAT_LONG:
2550 wt_longstatus_print(s);
2551 break;
2554 trace2_region_leave("status", "print", s->repo);
2558 * Returns 1 if there are unstaged changes, 0 otherwise.
2560 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2562 struct rev_info rev_info;
2563 int result;
2565 repo_init_revisions(r, &rev_info, NULL);
2566 if (ignore_submodules) {
2567 rev_info.diffopt.flags.ignore_submodules = 1;
2568 rev_info.diffopt.flags.override_submodule_config = 1;
2570 rev_info.diffopt.flags.quick = 1;
2571 diff_setup_done(&rev_info.diffopt);
2572 result = run_diff_files(&rev_info, 0);
2573 result = diff_result_code(&rev_info.diffopt, result);
2574 release_revisions(&rev_info);
2575 return result;
2579 * Returns 1 if there are uncommitted changes, 0 otherwise.
2581 int has_uncommitted_changes(struct repository *r,
2582 int ignore_submodules)
2584 struct rev_info rev_info;
2585 int result;
2587 if (is_index_unborn(r->index))
2588 return 0;
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.quick = 1;
2595 add_head_to_pending(&rev_info);
2596 if (!rev_info.pending.nr) {
2598 * We have no head (or it's corrupt); use the empty tree,
2599 * which will complain if the index is non-empty.
2601 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2602 add_pending_object(&rev_info, &tree->object, "");
2605 diff_setup_done(&rev_info.diffopt);
2606 result = run_diff_index(&rev_info, 1);
2607 result = diff_result_code(&rev_info.diffopt, result);
2608 release_revisions(&rev_info);
2609 return result;
2613 * If the work tree has unstaged or uncommitted changes, dies with the
2614 * appropriate message.
2616 int require_clean_work_tree(struct repository *r,
2617 const char *action,
2618 const char *hint,
2619 int ignore_submodules,
2620 int gently)
2622 struct lock_file lock_file = LOCK_INIT;
2623 int err = 0, fd;
2625 fd = repo_hold_locked_index(r, &lock_file, 0);
2626 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2627 if (0 <= fd)
2628 repo_update_index_if_able(r, &lock_file);
2629 rollback_lock_file(&lock_file);
2631 if (has_unstaged_changes(r, ignore_submodules)) {
2632 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2633 error(_("cannot %s: You have unstaged changes."), _(action));
2634 err = 1;
2637 if (has_uncommitted_changes(r, ignore_submodules)) {
2638 if (err)
2639 error(_("additionally, your index contains uncommitted changes."));
2640 else
2641 error(_("cannot %s: Your index contains uncommitted changes."),
2642 _(action));
2643 err = 1;
2646 if (err) {
2647 if (hint)
2648 error("%s", hint);
2649 if (!gently)
2650 exit(128);
2653 return err;