add-patch: handle splitting hunks with diff.suppressBlankEmpty
[git.git] / wt-status.c
blob9e8c08003b990c44ca17dc1e778bf572eb694786
1 #include "git-compat-util.h"
2 #include "advice.h"
3 #include "wt-status.h"
4 #include "object.h"
5 #include "dir.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "environment.h"
9 #include "gettext.h"
10 #include "hash.h"
11 #include "hex.h"
12 #include "object-name.h"
13 #include "path.h"
14 #include "revision.h"
15 #include "diffcore.h"
16 #include "quote.h"
17 #include "run-command.h"
18 #include "strvec.h"
19 #include "remote.h"
20 #include "refs.h"
21 #include "submodule.h"
22 #include "column.h"
23 #include "read-cache.h"
24 #include "setup.h"
25 #include "strbuf.h"
26 #include "trace.h"
27 #include "trace2.h"
28 #include "tree.h"
29 #include "utf8.h"
30 #include "worktree.h"
31 #include "lockfile.h"
32 #include "sequencer.h"
33 #include "fsmonitor-settings.h"
35 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
36 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
38 static const char cut_line[] =
39 "------------------------ >8 ------------------------\n";
41 static char default_wt_status_colors[][COLOR_MAXLEN] = {
42 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
43 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
44 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
45 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
46 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
47 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
48 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
49 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
50 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
53 static const char *color(int slot, struct wt_status *s)
55 const char *c = "";
56 if (want_color(s->use_color))
57 c = s->color_palette[slot];
58 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
59 c = s->color_palette[WT_STATUS_HEADER];
60 return c;
63 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
64 const char *fmt, va_list ap, const char *trail)
66 struct strbuf sb = STRBUF_INIT;
67 struct strbuf linebuf = STRBUF_INIT;
68 const char *line, *eol;
70 strbuf_vaddf(&sb, fmt, ap);
71 if (!sb.len) {
72 if (s->display_comment_prefix) {
73 strbuf_addch(&sb, comment_line_char);
74 if (!trail)
75 strbuf_addch(&sb, ' ');
77 color_print_strbuf(s->fp, color, &sb);
78 if (trail)
79 fprintf(s->fp, "%s", trail);
80 strbuf_release(&sb);
81 return;
83 for (line = sb.buf; *line; line = eol + 1) {
84 eol = strchr(line, '\n');
86 strbuf_reset(&linebuf);
87 if (at_bol && s->display_comment_prefix) {
88 strbuf_addch(&linebuf, comment_line_char);
89 if (*line != '\n' && *line != '\t')
90 strbuf_addch(&linebuf, ' ');
92 if (eol)
93 strbuf_add(&linebuf, line, eol - line);
94 else
95 strbuf_addstr(&linebuf, line);
96 color_print_strbuf(s->fp, color, &linebuf);
97 if (eol)
98 fprintf(s->fp, "\n");
99 else
100 break;
101 at_bol = 1;
103 if (trail)
104 fprintf(s->fp, "%s", trail);
105 strbuf_release(&linebuf);
106 strbuf_release(&sb);
109 void status_printf_ln(struct wt_status *s, const char *color,
110 const char *fmt, ...)
112 va_list ap;
114 va_start(ap, fmt);
115 status_vprintf(s, 1, color, fmt, ap, "\n");
116 va_end(ap);
119 void status_printf(struct wt_status *s, const char *color,
120 const char *fmt, ...)
122 va_list ap;
124 va_start(ap, fmt);
125 status_vprintf(s, 1, color, fmt, ap, NULL);
126 va_end(ap);
129 static void status_printf_more(struct wt_status *s, const char *color,
130 const char *fmt, ...)
132 va_list ap;
134 va_start(ap, fmt);
135 status_vprintf(s, 0, color, fmt, ap, NULL);
136 va_end(ap);
139 void wt_status_prepare(struct repository *r, struct wt_status *s)
141 memset(s, 0, sizeof(*s));
142 s->repo = r;
143 memcpy(s->color_palette, default_wt_status_colors,
144 sizeof(default_wt_status_colors));
145 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
146 s->use_color = -1;
147 s->relative_paths = 1;
148 s->branch = resolve_refdup("HEAD", 0, NULL, NULL);
149 s->reference = "HEAD";
150 s->fp = stdout;
151 s->index_file = get_index_file();
152 s->change.strdup_strings = 1;
153 s->untracked.strdup_strings = 1;
154 s->ignored.strdup_strings = 1;
155 s->show_branch = -1; /* unspecified */
156 s->show_stash = 0;
157 s->ahead_behind_flags = AHEAD_BEHIND_UNSPECIFIED;
158 s->display_comment_prefix = 0;
159 s->detect_rename = -1;
160 s->rename_score = -1;
161 s->rename_limit = -1;
164 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
166 int i;
167 int del_mod_conflict = 0;
168 int both_deleted = 0;
169 int not_deleted = 0;
170 const char *c = color(WT_STATUS_HEADER, s);
172 status_printf_ln(s, c, _("Unmerged paths:"));
174 for (i = 0; i < s->change.nr; i++) {
175 struct string_list_item *it = &(s->change.items[i]);
176 struct wt_status_change_data *d = it->util;
178 switch (d->stagemask) {
179 case 0:
180 break;
181 case 1:
182 both_deleted = 1;
183 break;
184 case 3:
185 case 5:
186 del_mod_conflict = 1;
187 break;
188 default:
189 not_deleted = 1;
190 break;
194 if (!s->hints)
195 return;
196 if (s->whence != FROM_COMMIT)
198 else if (!s->is_initial) {
199 if (!strcmp(s->reference, "HEAD"))
200 status_printf_ln(s, c,
201 _(" (use \"git restore --staged <file>...\" to unstage)"));
202 else
203 status_printf_ln(s, c,
204 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
205 s->reference);
206 } else
207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
209 if (!both_deleted) {
210 if (!del_mod_conflict)
211 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
212 else
213 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
214 } else if (!del_mod_conflict && !not_deleted) {
215 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
216 } else {
217 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
221 static void wt_longstatus_print_cached_header(struct wt_status *s)
223 const char *c = color(WT_STATUS_HEADER, s);
225 status_printf_ln(s, c, _("Changes to be committed:"));
226 if (!s->hints)
227 return;
228 if (s->whence != FROM_COMMIT)
229 ; /* NEEDSWORK: use "git reset --unresolve"??? */
230 else if (!s->is_initial) {
231 if (!strcmp(s->reference, "HEAD"))
232 status_printf_ln(s, c
233 , _(" (use \"git restore --staged <file>...\" to unstage)"));
234 else
235 status_printf_ln(s, c,
236 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
237 s->reference);
238 } else
239 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
242 static void wt_longstatus_print_dirty_header(struct wt_status *s,
243 int has_deleted,
244 int has_dirty_submodules)
246 const char *c = color(WT_STATUS_HEADER, s);
248 status_printf_ln(s, c, _("Changes not staged for commit:"));
249 if (!s->hints)
250 return;
251 if (!has_deleted)
252 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
253 else
254 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
255 status_printf_ln(s, c, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
256 if (has_dirty_submodules)
257 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
260 static void wt_longstatus_print_other_header(struct wt_status *s,
261 const char *what,
262 const char *how)
264 const char *c = color(WT_STATUS_HEADER, s);
265 status_printf_ln(s, c, "%s:", what);
266 if (!s->hints)
267 return;
268 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
271 static void wt_longstatus_print_trailer(struct wt_status *s)
273 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
276 static const char *wt_status_unmerged_status_string(int stagemask)
278 switch (stagemask) {
279 case 1:
280 return _("both deleted:");
281 case 2:
282 return _("added by us:");
283 case 3:
284 return _("deleted by them:");
285 case 4:
286 return _("added by them:");
287 case 5:
288 return _("deleted by us:");
289 case 6:
290 return _("both added:");
291 case 7:
292 return _("both modified:");
293 default:
294 BUG("unhandled unmerged status %x", stagemask);
298 static const char *wt_status_diff_status_string(int status)
300 switch (status) {
301 case DIFF_STATUS_ADDED:
302 return _("new file:");
303 case DIFF_STATUS_COPIED:
304 return _("copied:");
305 case DIFF_STATUS_DELETED:
306 return _("deleted:");
307 case DIFF_STATUS_MODIFIED:
308 return _("modified:");
309 case DIFF_STATUS_RENAMED:
310 return _("renamed:");
311 case DIFF_STATUS_TYPE_CHANGED:
312 return _("typechange:");
313 case DIFF_STATUS_UNKNOWN:
314 return _("unknown:");
315 case DIFF_STATUS_UNMERGED:
316 return _("unmerged:");
317 default:
318 return NULL;
322 static int maxwidth(const char *(*label)(int), int minval, int maxval)
324 int result = 0, i;
326 for (i = minval; i <= maxval; i++) {
327 const char *s = label(i);
328 int len = s ? utf8_strwidth(s) : 0;
329 if (len > result)
330 result = len;
332 return result;
335 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
336 struct string_list_item *it)
338 const char *c = color(WT_STATUS_UNMERGED, s);
339 struct wt_status_change_data *d = it->util;
340 struct strbuf onebuf = STRBUF_INIT;
341 static char *padding;
342 static int label_width;
343 const char *one, *how;
344 int len;
346 if (!padding) {
347 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
348 label_width += strlen(" ");
349 padding = xmallocz(label_width);
350 memset(padding, ' ', label_width);
353 one = quote_path(it->string, s->prefix, &onebuf, 0);
354 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
356 how = wt_status_unmerged_status_string(d->stagemask);
357 len = label_width - utf8_strwidth(how);
358 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
359 strbuf_release(&onebuf);
362 static void wt_longstatus_print_change_data(struct wt_status *s,
363 int change_type,
364 struct string_list_item *it)
366 struct wt_status_change_data *d = it->util;
367 const char *c = color(change_type, s);
368 int status;
369 char *one_name;
370 char *two_name;
371 const char *one, *two;
372 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
373 struct strbuf extra = STRBUF_INIT;
374 static char *padding;
375 static int label_width;
376 const char *what;
377 int len;
379 if (!padding) {
380 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
381 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
382 label_width += strlen(" ");
383 padding = xmallocz(label_width);
384 memset(padding, ' ', label_width);
387 one_name = two_name = it->string;
388 switch (change_type) {
389 case WT_STATUS_UPDATED:
390 status = d->index_status;
391 break;
392 case WT_STATUS_CHANGED:
393 if (d->new_submodule_commits || d->dirty_submodule) {
394 strbuf_addstr(&extra, " (");
395 if (d->new_submodule_commits)
396 strbuf_addstr(&extra, _("new commits, "));
397 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
398 strbuf_addstr(&extra, _("modified content, "));
399 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
400 strbuf_addstr(&extra, _("untracked content, "));
401 strbuf_setlen(&extra, extra.len - 2);
402 strbuf_addch(&extra, ')');
404 status = d->worktree_status;
405 break;
406 default:
407 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
408 change_type);
412 * Only pick up the rename it's relevant. If the rename is for
413 * the changed section and we're printing the updated section,
414 * ignore it.
416 if (d->rename_status == status)
417 one_name = d->rename_source;
419 one = quote_path(one_name, s->prefix, &onebuf, 0);
420 two = quote_path(two_name, s->prefix, &twobuf, 0);
422 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
423 what = wt_status_diff_status_string(status);
424 if (!what)
425 BUG("unhandled diff status %c", status);
426 len = label_width - utf8_strwidth(what);
427 assert(len >= 0);
428 if (one_name != two_name)
429 status_printf_more(s, c, "%s%.*s%s -> %s",
430 what, len, padding, one, two);
431 else
432 status_printf_more(s, c, "%s%.*s%s",
433 what, len, padding, one);
434 if (extra.len) {
435 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
436 strbuf_release(&extra);
438 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
439 strbuf_release(&onebuf);
440 strbuf_release(&twobuf);
443 static char short_submodule_status(struct wt_status_change_data *d)
445 if (d->new_submodule_commits)
446 return 'M';
447 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
448 return 'm';
449 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
450 return '?';
451 return d->worktree_status;
454 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
455 struct diff_options *options UNUSED,
456 void *data)
458 struct wt_status *s = data;
459 int i;
461 if (!q->nr)
462 return;
463 s->workdir_dirty = 1;
464 for (i = 0; i < q->nr; i++) {
465 struct diff_filepair *p;
466 struct string_list_item *it;
467 struct wt_status_change_data *d;
469 p = q->queue[i];
470 it = string_list_insert(&s->change, p->two->path);
471 d = it->util;
472 if (!d) {
473 CALLOC_ARRAY(d, 1);
474 it->util = d;
476 if (!d->worktree_status)
477 d->worktree_status = p->status;
478 if (S_ISGITLINK(p->two->mode)) {
479 d->dirty_submodule = p->two->dirty_submodule;
480 d->new_submodule_commits = !oideq(&p->one->oid,
481 &p->two->oid);
482 if (s->status_format == STATUS_FORMAT_SHORT)
483 d->worktree_status = short_submodule_status(d);
486 switch (p->status) {
487 case DIFF_STATUS_ADDED:
488 d->mode_worktree = p->two->mode;
489 break;
491 case DIFF_STATUS_DELETED:
492 d->mode_index = p->one->mode;
493 oidcpy(&d->oid_index, &p->one->oid);
494 /* mode_worktree is zero for a delete. */
495 break;
497 case DIFF_STATUS_COPIED:
498 case DIFF_STATUS_RENAMED:
499 if (d->rename_status)
500 BUG("multiple renames on the same target? how?");
501 d->rename_source = xstrdup(p->one->path);
502 d->rename_score = p->score * 100 / MAX_SCORE;
503 d->rename_status = p->status;
504 /* fallthru */
505 case DIFF_STATUS_MODIFIED:
506 case DIFF_STATUS_TYPE_CHANGED:
507 case DIFF_STATUS_UNMERGED:
508 d->mode_index = p->one->mode;
509 d->mode_worktree = p->two->mode;
510 oidcpy(&d->oid_index, &p->one->oid);
511 break;
513 default:
514 BUG("unhandled diff-files status '%c'", p->status);
515 break;
521 static int unmerged_mask(struct index_state *istate, const char *path)
523 int pos, mask;
524 const struct cache_entry *ce;
526 pos = index_name_pos(istate, path, strlen(path));
527 if (0 <= pos)
528 return 0;
530 mask = 0;
531 pos = -pos-1;
532 while (pos < istate->cache_nr) {
533 ce = istate->cache[pos++];
534 if (strcmp(ce->name, path) || !ce_stage(ce))
535 break;
536 mask |= (1 << (ce_stage(ce) - 1));
538 return mask;
541 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
542 struct diff_options *options UNUSED,
543 void *data)
545 struct wt_status *s = data;
546 int i;
548 for (i = 0; i < q->nr; i++) {
549 struct diff_filepair *p;
550 struct string_list_item *it;
551 struct wt_status_change_data *d;
553 p = q->queue[i];
554 it = string_list_insert(&s->change, p->two->path);
555 d = it->util;
556 if (!d) {
557 CALLOC_ARRAY(d, 1);
558 it->util = d;
560 if (!d->index_status)
561 d->index_status = p->status;
562 switch (p->status) {
563 case DIFF_STATUS_ADDED:
564 /* Leave {mode,oid}_head zero for an add. */
565 d->mode_index = p->two->mode;
566 oidcpy(&d->oid_index, &p->two->oid);
567 s->committable = 1;
568 break;
569 case DIFF_STATUS_DELETED:
570 d->mode_head = p->one->mode;
571 oidcpy(&d->oid_head, &p->one->oid);
572 s->committable = 1;
573 /* Leave {mode,oid}_index zero for a delete. */
574 break;
576 case DIFF_STATUS_COPIED:
577 case DIFF_STATUS_RENAMED:
578 if (d->rename_status)
579 BUG("multiple renames on the same target? how?");
580 d->rename_source = xstrdup(p->one->path);
581 d->rename_score = p->score * 100 / MAX_SCORE;
582 d->rename_status = p->status;
583 /* fallthru */
584 case DIFF_STATUS_MODIFIED:
585 case DIFF_STATUS_TYPE_CHANGED:
586 d->mode_head = p->one->mode;
587 d->mode_index = p->two->mode;
588 oidcpy(&d->oid_head, &p->one->oid);
589 oidcpy(&d->oid_index, &p->two->oid);
590 s->committable = 1;
591 break;
592 case DIFF_STATUS_UNMERGED:
593 d->stagemask = unmerged_mask(s->repo->index,
594 p->two->path);
596 * Don't bother setting {mode,oid}_{head,index} since the print
597 * code will output the stage values directly and not use the
598 * values in these fields.
600 break;
602 default:
603 BUG("unhandled diff-index status '%c'", p->status);
604 break;
609 static void wt_status_collect_changes_worktree(struct wt_status *s)
611 struct rev_info rev;
613 repo_init_revisions(s->repo, &rev, NULL);
614 setup_revisions(0, NULL, &rev, NULL);
615 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
616 rev.diffopt.flags.dirty_submodules = 1;
617 rev.diffopt.ita_invisible_in_index = 1;
618 if (!s->show_untracked_files)
619 rev.diffopt.flags.ignore_untracked_in_submodules = 1;
620 if (s->ignore_submodule_arg) {
621 rev.diffopt.flags.override_submodule_config = 1;
622 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
623 } else if (!rev.diffopt.flags.ignore_submodule_set &&
624 s->show_untracked_files != SHOW_NO_UNTRACKED_FILES)
625 handle_ignore_submodules_arg(&rev.diffopt, "none");
626 rev.diffopt.format_callback = wt_status_collect_changed_cb;
627 rev.diffopt.format_callback_data = s;
628 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
629 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
630 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
631 copy_pathspec(&rev.prune_data, &s->pathspec);
632 run_diff_files(&rev, 0);
633 release_revisions(&rev);
636 static void wt_status_collect_changes_index(struct wt_status *s)
638 struct rev_info rev;
639 struct setup_revision_opt opt;
641 repo_init_revisions(s->repo, &rev, NULL);
642 memset(&opt, 0, sizeof(opt));
643 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
644 setup_revisions(0, NULL, &rev, &opt);
646 rev.diffopt.flags.override_submodule_config = 1;
647 rev.diffopt.ita_invisible_in_index = 1;
648 if (s->ignore_submodule_arg) {
649 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
650 } else {
652 * Unless the user did explicitly request a submodule ignore
653 * mode by passing a command line option we do not ignore any
654 * changed submodule SHA-1s when comparing index and HEAD, no
655 * matter what is configured. Otherwise the user won't be
656 * shown any submodules manually added (and which are
657 * staged to be committed), which would be really confusing.
659 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
662 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
663 rev.diffopt.format_callback = wt_status_collect_updated_cb;
664 rev.diffopt.format_callback_data = s;
665 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
666 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
667 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
670 * The `recursive` option must be enabled to allow the diff to recurse
671 * into subdirectories of sparse directory index entries. If it is not
672 * enabled, a subdirectory containing file(s) with changes is reported
673 * as "modified", rather than the modified files themselves.
675 rev.diffopt.flags.recursive = 1;
677 copy_pathspec(&rev.prune_data, &s->pathspec);
678 run_diff_index(&rev, DIFF_INDEX_CACHED);
679 release_revisions(&rev);
682 static int add_file_to_list(const struct object_id *oid,
683 struct strbuf *base, const char *path,
684 unsigned int mode, void *context)
686 struct string_list_item *it;
687 struct wt_status_change_data *d;
688 struct wt_status *s = context;
689 struct strbuf full_name = STRBUF_INIT;
691 if (S_ISDIR(mode))
692 return READ_TREE_RECURSIVE;
694 strbuf_add(&full_name, base->buf, base->len);
695 strbuf_addstr(&full_name, path);
696 it = string_list_insert(&s->change, full_name.buf);
697 d = it->util;
698 if (!d) {
699 CALLOC_ARRAY(d, 1);
700 it->util = d;
703 d->index_status = DIFF_STATUS_ADDED;
704 /* Leave {mode,oid}_head zero for adds. */
705 d->mode_index = mode;
706 oidcpy(&d->oid_index, oid);
707 s->committable = 1;
708 strbuf_release(&full_name);
709 return 0;
712 static void wt_status_collect_changes_initial(struct wt_status *s)
714 struct index_state *istate = s->repo->index;
715 int i;
717 for (i = 0; i < istate->cache_nr; i++) {
718 struct string_list_item *it;
719 struct wt_status_change_data *d;
720 const struct cache_entry *ce = istate->cache[i];
722 if (!ce_path_match(istate, ce, &s->pathspec, NULL))
723 continue;
724 if (ce_intent_to_add(ce))
725 continue;
726 if (S_ISSPARSEDIR(ce->ce_mode)) {
728 * This is a sparse directory entry, so we want to collect all
729 * of the added files within the tree. This requires recursively
730 * expanding the trees to find the elements that are new in this
731 * tree and marking them with DIFF_STATUS_ADDED.
733 struct strbuf base = STRBUF_INIT;
734 struct pathspec ps = { 0 };
735 struct tree *tree = lookup_tree(istate->repo, &ce->oid);
737 ps.recursive = 1;
738 ps.has_wildcard = 1;
739 ps.max_depth = -1;
741 strbuf_add(&base, ce->name, ce->ce_namelen);
742 read_tree_at(istate->repo, tree, &base, 0, &ps,
743 add_file_to_list, s);
744 continue;
747 it = string_list_insert(&s->change, ce->name);
748 d = it->util;
749 if (!d) {
750 CALLOC_ARRAY(d, 1);
751 it->util = d;
753 if (ce_stage(ce)) {
754 d->index_status = DIFF_STATUS_UNMERGED;
755 d->stagemask |= (1 << (ce_stage(ce) - 1));
757 * Don't bother setting {mode,oid}_{head,index} since the print
758 * code will output the stage values directly and not use the
759 * values in these fields.
761 s->committable = 1;
762 } else {
763 d->index_status = DIFF_STATUS_ADDED;
764 /* Leave {mode,oid}_head zero for adds. */
765 d->mode_index = ce->ce_mode;
766 oidcpy(&d->oid_index, &ce->oid);
767 s->committable = 1;
772 static void wt_status_collect_untracked(struct wt_status *s)
774 int i;
775 struct dir_struct dir = DIR_INIT;
776 uint64_t t_begin = getnanotime();
777 struct index_state *istate = s->repo->index;
779 if (!s->show_untracked_files)
780 return;
782 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
783 dir.flags |=
784 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
785 if (s->show_ignored_mode) {
786 dir.flags |= DIR_SHOW_IGNORED_TOO;
788 if (s->show_ignored_mode == SHOW_MATCHING_IGNORED)
789 dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;
790 } else {
791 dir.untracked = istate->untracked;
794 setup_standard_excludes(&dir);
796 fill_directory(&dir, istate, &s->pathspec);
798 for (i = 0; i < dir.nr; i++) {
799 struct dir_entry *ent = dir.entries[i];
800 if (index_name_is_other(istate, ent->name, ent->len))
801 string_list_insert(&s->untracked, ent->name);
804 for (i = 0; i < dir.ignored_nr; i++) {
805 struct dir_entry *ent = dir.ignored[i];
806 if (index_name_is_other(istate, ent->name, ent->len))
807 string_list_insert(&s->ignored, ent->name);
810 dir_clear(&dir);
812 if (advice_enabled(ADVICE_STATUS_U_OPTION))
813 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
816 static int has_unmerged(struct wt_status *s)
818 int i;
820 for (i = 0; i < s->change.nr; i++) {
821 struct wt_status_change_data *d;
822 d = s->change.items[i].util;
823 if (d->stagemask)
824 return 1;
826 return 0;
829 void wt_status_collect(struct wt_status *s)
831 trace2_region_enter("status", "worktrees", s->repo);
832 wt_status_collect_changes_worktree(s);
833 trace2_region_leave("status", "worktrees", s->repo);
835 if (s->is_initial) {
836 trace2_region_enter("status", "initial", s->repo);
837 wt_status_collect_changes_initial(s);
838 trace2_region_leave("status", "initial", s->repo);
839 } else {
840 trace2_region_enter("status", "index", s->repo);
841 wt_status_collect_changes_index(s);
842 trace2_region_leave("status", "index", s->repo);
845 trace2_region_enter("status", "untracked", s->repo);
846 wt_status_collect_untracked(s);
847 trace2_region_leave("status", "untracked", s->repo);
849 wt_status_get_state(s->repo, &s->state, s->branch && !strcmp(s->branch, "HEAD"));
850 if (s->state.merge_in_progress && !has_unmerged(s))
851 s->committable = 1;
854 void wt_status_collect_free_buffers(struct wt_status *s)
856 wt_status_state_free_buffers(&s->state);
859 void wt_status_state_free_buffers(struct wt_status_state *state)
861 FREE_AND_NULL(state->branch);
862 FREE_AND_NULL(state->onto);
863 FREE_AND_NULL(state->detached_from);
864 FREE_AND_NULL(state->bisecting_from);
867 static void wt_longstatus_print_unmerged(struct wt_status *s)
869 int shown_header = 0;
870 int i;
872 for (i = 0; i < s->change.nr; i++) {
873 struct wt_status_change_data *d;
874 struct string_list_item *it;
875 it = &(s->change.items[i]);
876 d = it->util;
877 if (!d->stagemask)
878 continue;
879 if (!shown_header) {
880 wt_longstatus_print_unmerged_header(s);
881 shown_header = 1;
883 wt_longstatus_print_unmerged_data(s, it);
885 if (shown_header)
886 wt_longstatus_print_trailer(s);
890 static void wt_longstatus_print_updated(struct wt_status *s)
892 int shown_header = 0;
893 int i;
895 for (i = 0; i < s->change.nr; i++) {
896 struct wt_status_change_data *d;
897 struct string_list_item *it;
898 it = &(s->change.items[i]);
899 d = it->util;
900 if (!d->index_status ||
901 d->index_status == DIFF_STATUS_UNMERGED)
902 continue;
903 if (!shown_header) {
904 wt_longstatus_print_cached_header(s);
905 shown_header = 1;
907 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
909 if (shown_header)
910 wt_longstatus_print_trailer(s);
914 * -1 : has delete
915 * 0 : no change
916 * 1 : some change but no delete
918 static int wt_status_check_worktree_changes(struct wt_status *s,
919 int *dirty_submodules)
921 int i;
922 int changes = 0;
924 *dirty_submodules = 0;
926 for (i = 0; i < s->change.nr; i++) {
927 struct wt_status_change_data *d;
928 d = s->change.items[i].util;
929 if (!d->worktree_status ||
930 d->worktree_status == DIFF_STATUS_UNMERGED)
931 continue;
932 if (!changes)
933 changes = 1;
934 if (d->dirty_submodule)
935 *dirty_submodules = 1;
936 if (d->worktree_status == DIFF_STATUS_DELETED)
937 changes = -1;
939 return changes;
942 static void wt_longstatus_print_changed(struct wt_status *s)
944 int i, dirty_submodules;
945 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
947 if (!worktree_changes)
948 return;
950 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
952 for (i = 0; i < s->change.nr; i++) {
953 struct wt_status_change_data *d;
954 struct string_list_item *it;
955 it = &(s->change.items[i]);
956 d = it->util;
957 if (!d->worktree_status ||
958 d->worktree_status == DIFF_STATUS_UNMERGED)
959 continue;
960 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
962 wt_longstatus_print_trailer(s);
965 static int stash_count_refs(struct object_id *ooid UNUSED,
966 struct object_id *noid UNUSED,
967 const char *email UNUSED,
968 timestamp_t timestamp UNUSED, int tz UNUSED,
969 const char *message UNUSED, void *cb_data)
971 int *c = cb_data;
972 (*c)++;
973 return 0;
976 static int count_stash_entries(void)
978 int n = 0;
979 for_each_reflog_ent("refs/stash", stash_count_refs, &n);
980 return n;
983 static void wt_longstatus_print_stash_summary(struct wt_status *s)
985 int stash_count = count_stash_entries();
987 if (stash_count > 0)
988 status_printf_ln(s, GIT_COLOR_NORMAL,
989 Q_("Your stash currently has %d entry",
990 "Your stash currently has %d entries", stash_count),
991 stash_count);
994 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
996 struct child_process sm_summary = CHILD_PROCESS_INIT;
997 struct strbuf cmd_stdout = STRBUF_INIT;
998 struct strbuf summary = STRBUF_INIT;
999 char *summary_content;
1001 strvec_pushf(&sm_summary.env, "GIT_INDEX_FILE=%s", s->index_file);
1003 strvec_push(&sm_summary.args, "submodule");
1004 strvec_push(&sm_summary.args, "summary");
1005 strvec_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
1006 strvec_push(&sm_summary.args, "--for-status");
1007 strvec_push(&sm_summary.args, "--summary-limit");
1008 strvec_pushf(&sm_summary.args, "%d", s->submodule_summary);
1009 if (!uncommitted)
1010 strvec_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
1012 sm_summary.git_cmd = 1;
1013 sm_summary.no_stdin = 1;
1015 capture_command(&sm_summary, &cmd_stdout, 1024);
1017 /* prepend header, only if there's an actual output */
1018 if (cmd_stdout.len) {
1019 if (uncommitted)
1020 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
1021 else
1022 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
1023 strbuf_addstr(&summary, "\n\n");
1025 strbuf_addbuf(&summary, &cmd_stdout);
1026 strbuf_release(&cmd_stdout);
1028 if (s->display_comment_prefix) {
1029 size_t len;
1030 summary_content = strbuf_detach(&summary, &len);
1031 strbuf_add_commented_lines(&summary, summary_content, len, comment_line_char);
1032 free(summary_content);
1035 fputs(summary.buf, s->fp);
1036 strbuf_release(&summary);
1039 static void wt_longstatus_print_other(struct wt_status *s,
1040 struct string_list *l,
1041 const char *what,
1042 const char *how)
1044 int i;
1045 struct strbuf buf = STRBUF_INIT;
1046 static struct string_list output = STRING_LIST_INIT_DUP;
1047 struct column_options copts;
1049 if (!l->nr)
1050 return;
1052 wt_longstatus_print_other_header(s, what, how);
1054 for (i = 0; i < l->nr; i++) {
1055 struct string_list_item *it;
1056 const char *path;
1057 it = &(l->items[i]);
1058 path = quote_path(it->string, s->prefix, &buf, 0);
1059 if (column_active(s->colopts)) {
1060 string_list_append(&output, path);
1061 continue;
1063 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
1064 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
1065 "%s\n", path);
1068 strbuf_release(&buf);
1069 if (!column_active(s->colopts))
1070 goto conclude;
1072 strbuf_addf(&buf, "%s%s\t%s",
1073 color(WT_STATUS_HEADER, s),
1074 s->display_comment_prefix ? "#" : "",
1075 color(WT_STATUS_UNTRACKED, s));
1076 memset(&copts, 0, sizeof(copts));
1077 copts.padding = 1;
1078 copts.indent = buf.buf;
1079 if (want_color(s->use_color))
1080 copts.nl = GIT_COLOR_RESET "\n";
1081 print_columns(&output, s->colopts, &copts);
1082 string_list_clear(&output, 0);
1083 strbuf_release(&buf);
1084 conclude:
1085 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1088 size_t wt_status_locate_end(const char *s, size_t len)
1090 const char *p;
1091 struct strbuf pattern = STRBUF_INIT;
1093 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
1094 if (starts_with(s, pattern.buf + 1))
1095 len = 0;
1096 else if ((p = strstr(s, pattern.buf)))
1097 len = p - s + 1;
1098 strbuf_release(&pattern);
1099 return len;
1102 void wt_status_append_cut_line(struct strbuf *buf)
1104 const char *explanation = _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1106 strbuf_commented_addf(buf, comment_line_char, "%s", cut_line);
1107 strbuf_add_commented_lines(buf, explanation, strlen(explanation), comment_line_char);
1110 void wt_status_add_cut_line(FILE *fp)
1112 struct strbuf buf = STRBUF_INIT;
1114 wt_status_append_cut_line(&buf);
1115 fputs(buf.buf, fp);
1116 strbuf_release(&buf);
1119 static void wt_longstatus_print_verbose(struct wt_status *s)
1121 struct rev_info rev;
1122 struct setup_revision_opt opt;
1123 int dirty_submodules;
1124 const char *c = color(WT_STATUS_HEADER, s);
1126 repo_init_revisions(s->repo, &rev, NULL);
1127 rev.diffopt.flags.allow_textconv = 1;
1128 rev.diffopt.ita_invisible_in_index = 1;
1130 memset(&opt, 0, sizeof(opt));
1131 opt.def = s->is_initial ? empty_tree_oid_hex() : s->reference;
1132 setup_revisions(0, NULL, &rev, &opt);
1134 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
1135 rev.diffopt.detect_rename = s->detect_rename >= 0 ? s->detect_rename : rev.diffopt.detect_rename;
1136 rev.diffopt.rename_limit = s->rename_limit >= 0 ? s->rename_limit : rev.diffopt.rename_limit;
1137 rev.diffopt.rename_score = s->rename_score >= 0 ? s->rename_score : rev.diffopt.rename_score;
1138 rev.diffopt.file = s->fp;
1139 rev.diffopt.close_file = 0;
1141 * If we're not going to stdout, then we definitely don't
1142 * want color, since we are going to the commit message
1143 * file (and even the "auto" setting won't work, since it
1144 * will have checked isatty on stdout). But we then do want
1145 * to insert the scissor line here to reliably remove the
1146 * diff before committing.
1148 if (s->fp != stdout) {
1149 rev.diffopt.use_color = 0;
1150 wt_status_add_cut_line(s->fp);
1152 if (s->verbose > 1 && s->committable) {
1153 /* print_updated() printed a header, so do we */
1154 if (s->fp != stdout)
1155 wt_longstatus_print_trailer(s);
1156 status_printf_ln(s, c, _("Changes to be committed:"));
1157 rev.diffopt.a_prefix = "c/";
1158 rev.diffopt.b_prefix = "i/";
1159 } /* else use prefix as per user config */
1160 run_diff_index(&rev, DIFF_INDEX_CACHED);
1161 if (s->verbose > 1 &&
1162 wt_status_check_worktree_changes(s, &dirty_submodules)) {
1163 status_printf_ln(s, c,
1164 "--------------------------------------------------");
1165 status_printf_ln(s, c, _("Changes not staged for commit:"));
1166 setup_work_tree();
1167 rev.diffopt.a_prefix = "i/";
1168 rev.diffopt.b_prefix = "w/";
1169 run_diff_files(&rev, 0);
1171 release_revisions(&rev);
1174 static void wt_longstatus_print_tracking(struct wt_status *s)
1176 struct strbuf sb = STRBUF_INIT;
1177 const char *cp, *ep, *branch_name;
1178 struct branch *branch;
1179 char comment_line_string[3];
1180 int i;
1181 uint64_t t_begin = 0;
1183 assert(s->branch && !s->is_initial);
1184 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1185 return;
1186 branch = branch_get(branch_name);
1188 t_begin = getnanotime();
1190 if (!format_tracking_info(branch, &sb, s->ahead_behind_flags,
1191 !s->commit_template))
1192 return;
1194 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING) &&
1195 s->ahead_behind_flags == AHEAD_BEHIND_FULL) {
1196 uint64_t t_delta_in_ms = (getnanotime() - t_begin) / 1000000;
1197 if (t_delta_in_ms > AB_DELAY_WARNING_IN_MS) {
1198 strbuf_addf(&sb, _("\n"
1199 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1200 "You can use '--no-ahead-behind' to avoid this.\n"),
1201 t_delta_in_ms / 1000.0);
1205 i = 0;
1206 if (s->display_comment_prefix) {
1207 comment_line_string[i++] = comment_line_char;
1208 comment_line_string[i++] = ' ';
1210 comment_line_string[i] = '\0';
1212 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1213 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1214 "%s%.*s", comment_line_string,
1215 (int)(ep - cp), cp);
1216 if (s->display_comment_prefix)
1217 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1218 comment_line_char);
1219 else
1220 fputs("\n", s->fp);
1221 strbuf_release(&sb);
1224 static int uf_was_slow(struct wt_status *s)
1226 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1227 s->untracked_in_ms = 3250;
1228 return UF_DELAY_WARNING_IN_MS < s->untracked_in_ms;
1231 static void show_merge_in_progress(struct wt_status *s,
1232 const char *color)
1234 if (has_unmerged(s)) {
1235 status_printf_ln(s, color, _("You have unmerged paths."));
1236 if (s->hints) {
1237 status_printf_ln(s, color,
1238 _(" (fix conflicts and run \"git commit\")"));
1239 status_printf_ln(s, color,
1240 _(" (use \"git merge --abort\" to abort the merge)"));
1242 } else {
1243 status_printf_ln(s, color,
1244 _("All conflicts fixed but you are still merging."));
1245 if (s->hints)
1246 status_printf_ln(s, color,
1247 _(" (use \"git commit\" to conclude merge)"));
1249 wt_longstatus_print_trailer(s);
1252 static void show_am_in_progress(struct wt_status *s,
1253 const char *color)
1255 int am_empty_patch;
1257 status_printf_ln(s, color,
1258 _("You are in the middle of an am session."));
1259 if (s->state.am_empty_patch)
1260 status_printf_ln(s, color,
1261 _("The current patch is empty."));
1262 if (s->hints) {
1263 am_empty_patch = s->state.am_empty_patch;
1264 if (!am_empty_patch)
1265 status_printf_ln(s, color,
1266 _(" (fix conflicts and then run \"git am --continue\")"));
1267 status_printf_ln(s, color,
1268 _(" (use \"git am --skip\" to skip this patch)"));
1269 if (am_empty_patch)
1270 status_printf_ln(s, color,
1271 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1272 status_printf_ln(s, color,
1273 _(" (use \"git am --abort\" to restore the original branch)"));
1275 wt_longstatus_print_trailer(s);
1278 static char *read_line_from_git_path(const char *filename)
1280 struct strbuf buf = STRBUF_INIT;
1281 FILE *fp = fopen_or_warn(git_path("%s", filename), "r");
1283 if (!fp) {
1284 strbuf_release(&buf);
1285 return NULL;
1287 strbuf_getline_lf(&buf, fp);
1288 if (!fclose(fp)) {
1289 return strbuf_detach(&buf, NULL);
1290 } else {
1291 strbuf_release(&buf);
1292 return NULL;
1296 static int split_commit_in_progress(struct wt_status *s)
1298 int split_in_progress = 0;
1299 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1301 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1302 !s->branch || strcmp(s->branch, "HEAD"))
1303 return 0;
1305 head = read_line_from_git_path("HEAD");
1306 orig_head = read_line_from_git_path("ORIG_HEAD");
1307 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1308 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1310 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1311 ; /* fall through, no split in progress */
1312 else if (!strcmp(rebase_amend, rebase_orig_head))
1313 split_in_progress = !!strcmp(head, rebase_amend);
1314 else if (strcmp(orig_head, rebase_orig_head))
1315 split_in_progress = 1;
1317 free(head);
1318 free(orig_head);
1319 free(rebase_amend);
1320 free(rebase_orig_head);
1322 return split_in_progress;
1326 * Turn
1327 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1328 * into
1329 * "pick d6a2f03 some message"
1331 * The function assumes that the line does not contain useless spaces
1332 * before or after the command.
1334 static void abbrev_oid_in_line(struct strbuf *line)
1336 struct strbuf **split;
1337 int i;
1339 if (starts_with(line->buf, "exec ") ||
1340 starts_with(line->buf, "x ") ||
1341 starts_with(line->buf, "label ") ||
1342 starts_with(line->buf, "l "))
1343 return;
1345 split = strbuf_split_max(line, ' ', 3);
1346 if (split[0] && split[1]) {
1347 struct object_id oid;
1350 * strbuf_split_max left a space. Trim it and re-add
1351 * it after abbreviation.
1353 strbuf_trim(split[1]);
1354 if (!repo_get_oid(the_repository, split[1]->buf, &oid)) {
1355 strbuf_reset(split[1]);
1356 strbuf_add_unique_abbrev(split[1], &oid,
1357 DEFAULT_ABBREV);
1358 strbuf_addch(split[1], ' ');
1359 strbuf_reset(line);
1360 for (i = 0; split[i]; i++)
1361 strbuf_addbuf(line, split[i]);
1364 strbuf_list_free(split);
1367 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1369 struct strbuf line = STRBUF_INIT;
1370 FILE *f = fopen(git_path("%s", fname), "r");
1372 if (!f) {
1373 if (errno == ENOENT)
1374 return -1;
1375 die_errno("Could not open file %s for reading",
1376 git_path("%s", fname));
1378 while (!strbuf_getline_lf(&line, f)) {
1379 if (line.len && line.buf[0] == comment_line_char)
1380 continue;
1381 strbuf_trim(&line);
1382 if (!line.len)
1383 continue;
1384 abbrev_oid_in_line(&line);
1385 string_list_append(lines, line.buf);
1387 fclose(f);
1388 strbuf_release(&line);
1389 return 0;
1392 static void show_rebase_information(struct wt_status *s,
1393 const char *color)
1395 if (s->state.rebase_interactive_in_progress) {
1396 int i;
1397 int nr_lines_to_show = 2;
1399 struct string_list have_done = STRING_LIST_INIT_DUP;
1400 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1402 read_rebase_todolist("rebase-merge/done", &have_done);
1403 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1404 &yet_to_do))
1405 status_printf_ln(s, color,
1406 _("git-rebase-todo is missing."));
1407 if (have_done.nr == 0)
1408 status_printf_ln(s, color, _("No commands done."));
1409 else {
1410 status_printf_ln(s, color,
1411 Q_("Last command done (%"PRIuMAX" command done):",
1412 "Last commands done (%"PRIuMAX" commands done):",
1413 have_done.nr),
1414 (uintmax_t)have_done.nr);
1415 for (i = (have_done.nr > nr_lines_to_show)
1416 ? have_done.nr - nr_lines_to_show : 0;
1417 i < have_done.nr;
1418 i++)
1419 status_printf_ln(s, color, " %s", have_done.items[i].string);
1420 if (have_done.nr > nr_lines_to_show && s->hints)
1421 status_printf_ln(s, color,
1422 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1425 if (yet_to_do.nr == 0)
1426 status_printf_ln(s, color,
1427 _("No commands remaining."));
1428 else {
1429 status_printf_ln(s, color,
1430 Q_("Next command to do (%"PRIuMAX" remaining command):",
1431 "Next commands to do (%"PRIuMAX" remaining commands):",
1432 yet_to_do.nr),
1433 (uintmax_t)yet_to_do.nr);
1434 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1435 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1436 if (s->hints)
1437 status_printf_ln(s, color,
1438 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1440 string_list_clear(&yet_to_do, 0);
1441 string_list_clear(&have_done, 0);
1445 static void print_rebase_state(struct wt_status *s,
1446 const char *color)
1448 if (s->state.branch)
1449 status_printf_ln(s, color,
1450 _("You are currently rebasing branch '%s' on '%s'."),
1451 s->state.branch,
1452 s->state.onto);
1453 else
1454 status_printf_ln(s, color,
1455 _("You are currently rebasing."));
1458 static void show_rebase_in_progress(struct wt_status *s,
1459 const char *color)
1461 struct stat st;
1463 show_rebase_information(s, color);
1464 if (has_unmerged(s)) {
1465 print_rebase_state(s, color);
1466 if (s->hints) {
1467 status_printf_ln(s, color,
1468 _(" (fix conflicts and then run \"git rebase --continue\")"));
1469 status_printf_ln(s, color,
1470 _(" (use \"git rebase --skip\" to skip this patch)"));
1471 status_printf_ln(s, color,
1472 _(" (use \"git rebase --abort\" to check out the original branch)"));
1474 } else if (s->state.rebase_in_progress ||
1475 !stat(git_path_merge_msg(s->repo), &st)) {
1476 print_rebase_state(s, color);
1477 if (s->hints)
1478 status_printf_ln(s, color,
1479 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1480 } else if (split_commit_in_progress(s)) {
1481 if (s->state.branch)
1482 status_printf_ln(s, color,
1483 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1484 s->state.branch,
1485 s->state.onto);
1486 else
1487 status_printf_ln(s, color,
1488 _("You are currently splitting a commit during a rebase."));
1489 if (s->hints)
1490 status_printf_ln(s, color,
1491 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1492 } else {
1493 if (s->state.branch)
1494 status_printf_ln(s, color,
1495 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1496 s->state.branch,
1497 s->state.onto);
1498 else
1499 status_printf_ln(s, color,
1500 _("You are currently editing a commit during a rebase."));
1501 if (s->hints && !s->amend) {
1502 status_printf_ln(s, color,
1503 _(" (use \"git commit --amend\" to amend the current commit)"));
1504 status_printf_ln(s, color,
1505 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1508 wt_longstatus_print_trailer(s);
1511 static void show_cherry_pick_in_progress(struct wt_status *s,
1512 const char *color)
1514 if (is_null_oid(&s->state.cherry_pick_head_oid))
1515 status_printf_ln(s, color,
1516 _("Cherry-pick currently in progress."));
1517 else
1518 status_printf_ln(s, color,
1519 _("You are currently cherry-picking commit %s."),
1520 repo_find_unique_abbrev(the_repository, &s->state.cherry_pick_head_oid,
1521 DEFAULT_ABBREV));
1523 if (s->hints) {
1524 if (has_unmerged(s))
1525 status_printf_ln(s, color,
1526 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1527 else if (is_null_oid(&s->state.cherry_pick_head_oid))
1528 status_printf_ln(s, color,
1529 _(" (run \"git cherry-pick --continue\" to continue)"));
1530 else
1531 status_printf_ln(s, color,
1532 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1533 status_printf_ln(s, color,
1534 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1535 status_printf_ln(s, color,
1536 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1538 wt_longstatus_print_trailer(s);
1541 static void show_revert_in_progress(struct wt_status *s,
1542 const char *color)
1544 if (is_null_oid(&s->state.revert_head_oid))
1545 status_printf_ln(s, color,
1546 _("Revert currently in progress."));
1547 else
1548 status_printf_ln(s, color,
1549 _("You are currently reverting commit %s."),
1550 repo_find_unique_abbrev(the_repository, &s->state.revert_head_oid,
1551 DEFAULT_ABBREV));
1552 if (s->hints) {
1553 if (has_unmerged(s))
1554 status_printf_ln(s, color,
1555 _(" (fix conflicts and run \"git revert --continue\")"));
1556 else if (is_null_oid(&s->state.revert_head_oid))
1557 status_printf_ln(s, color,
1558 _(" (run \"git revert --continue\" to continue)"));
1559 else
1560 status_printf_ln(s, color,
1561 _(" (all conflicts fixed: run \"git revert --continue\")"));
1562 status_printf_ln(s, color,
1563 _(" (use \"git revert --skip\" to skip this patch)"));
1564 status_printf_ln(s, color,
1565 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1567 wt_longstatus_print_trailer(s);
1570 static void show_bisect_in_progress(struct wt_status *s,
1571 const char *color)
1573 if (s->state.bisecting_from)
1574 status_printf_ln(s, color,
1575 _("You are currently bisecting, started from branch '%s'."),
1576 s->state.bisecting_from);
1577 else
1578 status_printf_ln(s, color,
1579 _("You are currently bisecting."));
1580 if (s->hints)
1581 status_printf_ln(s, color,
1582 _(" (use \"git bisect reset\" to get back to the original branch)"));
1583 wt_longstatus_print_trailer(s);
1586 static void show_sparse_checkout_in_use(struct wt_status *s,
1587 const char *color)
1589 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_DISABLED)
1590 return;
1592 if (s->state.sparse_checkout_percentage == SPARSE_CHECKOUT_SPARSE_INDEX)
1593 status_printf_ln(s, color, _("You are in a sparse checkout."));
1594 else
1595 status_printf_ln(s, color,
1596 _("You are in a sparse checkout with %d%% of tracked files present."),
1597 s->state.sparse_checkout_percentage);
1598 wt_longstatus_print_trailer(s);
1602 * Extract branch information from rebase/bisect
1604 static char *get_branch(const struct worktree *wt, const char *path)
1606 struct strbuf sb = STRBUF_INIT;
1607 struct object_id oid;
1608 const char *branch_name;
1610 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1611 goto got_nothing;
1613 while (sb.len && sb.buf[sb.len - 1] == '\n')
1614 strbuf_setlen(&sb, sb.len - 1);
1615 if (!sb.len)
1616 goto got_nothing;
1617 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1618 strbuf_remove(&sb, 0, branch_name - sb.buf);
1619 else if (starts_with(sb.buf, "refs/"))
1621 else if (!get_oid_hex(sb.buf, &oid)) {
1622 strbuf_reset(&sb);
1623 strbuf_add_unique_abbrev(&sb, &oid, DEFAULT_ABBREV);
1624 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1625 goto got_nothing;
1626 else /* bisect */
1628 return strbuf_detach(&sb, NULL);
1630 got_nothing:
1631 strbuf_release(&sb);
1632 return NULL;
1635 struct grab_1st_switch_cbdata {
1636 struct strbuf buf;
1637 struct object_id noid;
1640 static int grab_1st_switch(struct object_id *ooid UNUSED,
1641 struct object_id *noid,
1642 const char *email UNUSED,
1643 timestamp_t timestamp UNUSED, int tz UNUSED,
1644 const char *message, void *cb_data)
1646 struct grab_1st_switch_cbdata *cb = cb_data;
1647 const char *target = NULL, *end;
1649 if (!skip_prefix(message, "checkout: moving from ", &message))
1650 return 0;
1651 target = strstr(message, " to ");
1652 if (!target)
1653 return 0;
1654 target += strlen(" to ");
1655 strbuf_reset(&cb->buf);
1656 oidcpy(&cb->noid, noid);
1657 end = strchrnul(target, '\n');
1658 strbuf_add(&cb->buf, target, end - target);
1659 if (!strcmp(cb->buf.buf, "HEAD")) {
1660 /* HEAD is relative. Resolve it to the right reflog entry. */
1661 strbuf_reset(&cb->buf);
1662 strbuf_add_unique_abbrev(&cb->buf, noid, DEFAULT_ABBREV);
1664 return 1;
1667 static void wt_status_get_detached_from(struct repository *r,
1668 struct wt_status_state *state)
1670 struct grab_1st_switch_cbdata cb;
1671 struct commit *commit;
1672 struct object_id oid;
1673 char *ref = NULL;
1675 strbuf_init(&cb.buf, 0);
1676 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1677 strbuf_release(&cb.buf);
1678 return;
1681 if (repo_dwim_ref(r, cb.buf.buf, cb.buf.len, &oid, &ref,
1682 1) == 1 &&
1683 /* oid is a commit? match without further lookup */
1684 (oideq(&cb.noid, &oid) ||
1685 /* perhaps oid is a tag, try to dereference to a commit */
1686 ((commit = lookup_commit_reference_gently(r, &oid, 1)) != NULL &&
1687 oideq(&cb.noid, &commit->object.oid)))) {
1688 const char *from = ref;
1689 if (!skip_prefix(from, "refs/tags/", &from))
1690 skip_prefix(from, "refs/remotes/", &from);
1691 state->detached_from = xstrdup(from);
1692 } else
1693 state->detached_from =
1694 xstrdup(repo_find_unique_abbrev(r, &cb.noid, DEFAULT_ABBREV));
1695 oidcpy(&state->detached_oid, &cb.noid);
1696 state->detached_at = !repo_get_oid(r, "HEAD", &oid) &&
1697 oideq(&oid, &state->detached_oid);
1699 free(ref);
1700 strbuf_release(&cb.buf);
1703 int wt_status_check_rebase(const struct worktree *wt,
1704 struct wt_status_state *state)
1706 struct stat st;
1708 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1709 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1710 state->am_in_progress = 1;
1711 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1712 state->am_empty_patch = 1;
1713 } else {
1714 state->rebase_in_progress = 1;
1715 state->branch = get_branch(wt, "rebase-apply/head-name");
1716 state->onto = get_branch(wt, "rebase-apply/onto");
1718 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1719 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1720 state->rebase_interactive_in_progress = 1;
1721 else
1722 state->rebase_in_progress = 1;
1723 state->branch = get_branch(wt, "rebase-merge/head-name");
1724 state->onto = get_branch(wt, "rebase-merge/onto");
1725 } else
1726 return 0;
1727 return 1;
1730 int wt_status_check_bisect(const struct worktree *wt,
1731 struct wt_status_state *state)
1733 struct stat st;
1735 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1736 state->bisect_in_progress = 1;
1737 state->bisecting_from = get_branch(wt, "BISECT_START");
1738 return 1;
1740 return 0;
1743 static void wt_status_check_sparse_checkout(struct repository *r,
1744 struct wt_status_state *state)
1746 int skip_worktree = 0;
1747 int i;
1749 if (!core_apply_sparse_checkout || r->index->cache_nr == 0) {
1751 * Don't compute percentage of checked out files if we
1752 * aren't in a sparse checkout or would get division by 0.
1754 state->sparse_checkout_percentage = SPARSE_CHECKOUT_DISABLED;
1755 return;
1758 if (r->index->sparse_index) {
1759 state->sparse_checkout_percentage = SPARSE_CHECKOUT_SPARSE_INDEX;
1760 return;
1763 for (i = 0; i < r->index->cache_nr; i++) {
1764 struct cache_entry *ce = r->index->cache[i];
1765 if (ce_skip_worktree(ce))
1766 skip_worktree++;
1769 state->sparse_checkout_percentage =
1770 100 - (100 * skip_worktree)/r->index->cache_nr;
1773 void wt_status_get_state(struct repository *r,
1774 struct wt_status_state *state,
1775 int get_detached_from)
1777 struct stat st;
1778 struct object_id oid;
1779 enum replay_action action;
1781 if (!stat(git_path_merge_head(r), &st)) {
1782 wt_status_check_rebase(NULL, state);
1783 state->merge_in_progress = 1;
1784 } else if (wt_status_check_rebase(NULL, state)) {
1785 ; /* all set */
1786 } else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
1787 !repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
1788 state->cherry_pick_in_progress = 1;
1789 oidcpy(&state->cherry_pick_head_oid, &oid);
1791 wt_status_check_bisect(NULL, state);
1792 if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
1793 !repo_get_oid(r, "REVERT_HEAD", &oid)) {
1794 state->revert_in_progress = 1;
1795 oidcpy(&state->revert_head_oid, &oid);
1797 if (!sequencer_get_last_command(r, &action)) {
1798 if (action == REPLAY_PICK && !state->cherry_pick_in_progress) {
1799 state->cherry_pick_in_progress = 1;
1800 oidcpy(&state->cherry_pick_head_oid, null_oid());
1801 } else if (action == REPLAY_REVERT && !state->revert_in_progress) {
1802 state->revert_in_progress = 1;
1803 oidcpy(&state->revert_head_oid, null_oid());
1806 if (get_detached_from)
1807 wt_status_get_detached_from(r, state);
1808 wt_status_check_sparse_checkout(r, state);
1811 static void wt_longstatus_print_state(struct wt_status *s)
1813 const char *state_color = color(WT_STATUS_HEADER, s);
1814 struct wt_status_state *state = &s->state;
1816 if (state->merge_in_progress) {
1817 if (state->rebase_interactive_in_progress) {
1818 show_rebase_information(s, state_color);
1819 fputs("\n", s->fp);
1821 show_merge_in_progress(s, state_color);
1822 } else if (state->am_in_progress)
1823 show_am_in_progress(s, state_color);
1824 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1825 show_rebase_in_progress(s, state_color);
1826 else if (state->cherry_pick_in_progress)
1827 show_cherry_pick_in_progress(s, state_color);
1828 else if (state->revert_in_progress)
1829 show_revert_in_progress(s, state_color);
1830 if (state->bisect_in_progress)
1831 show_bisect_in_progress(s, state_color);
1833 if (state->sparse_checkout_percentage != SPARSE_CHECKOUT_DISABLED)
1834 show_sparse_checkout_in_use(s, state_color);
1837 static void wt_longstatus_print(struct wt_status *s)
1839 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1840 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1841 enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(s->repo);
1843 if (s->branch) {
1844 const char *on_what = _("On branch ");
1845 const char *branch_name = s->branch;
1846 if (!strcmp(branch_name, "HEAD")) {
1847 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1848 if (s->state.rebase_in_progress ||
1849 s->state.rebase_interactive_in_progress) {
1850 if (s->state.rebase_interactive_in_progress)
1851 on_what = _("interactive rebase in progress; onto ");
1852 else
1853 on_what = _("rebase in progress; onto ");
1854 branch_name = s->state.onto;
1855 } else if (s->state.detached_from) {
1856 branch_name = s->state.detached_from;
1857 if (s->state.detached_at)
1858 on_what = _("HEAD detached at ");
1859 else
1860 on_what = _("HEAD detached from ");
1861 } else {
1862 branch_name = "";
1863 on_what = _("Not currently on any branch.");
1865 } else
1866 skip_prefix(branch_name, "refs/heads/", &branch_name);
1867 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1868 status_printf_more(s, branch_status_color, "%s", on_what);
1869 status_printf_more(s, branch_color, "%s\n", branch_name);
1870 if (!s->is_initial)
1871 wt_longstatus_print_tracking(s);
1874 wt_longstatus_print_state(s);
1876 if (s->is_initial) {
1877 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1878 status_printf_ln(s, color(WT_STATUS_HEADER, s),
1879 s->commit_template
1880 ? _("Initial commit")
1881 : _("No commits yet"));
1882 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1885 wt_longstatus_print_updated(s);
1886 wt_longstatus_print_unmerged(s);
1887 wt_longstatus_print_changed(s);
1888 if (s->submodule_summary &&
1889 (!s->ignore_submodule_arg ||
1890 strcmp(s->ignore_submodule_arg, "all"))) {
1891 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1892 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1894 if (s->show_untracked_files) {
1895 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1896 if (s->show_ignored_mode)
1897 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1898 if (advice_enabled(ADVICE_STATUS_U_OPTION) && uf_was_slow(s)) {
1899 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1900 if (fsm_mode > FSMONITOR_MODE_DISABLED) {
1901 status_printf_ln(s, GIT_COLOR_NORMAL,
1902 _("It took %.2f seconds to enumerate untracked files,\n"
1903 "but the results were cached, and subsequent runs may be faster."),
1904 s->untracked_in_ms / 1000.0);
1905 } else {
1906 status_printf_ln(s, GIT_COLOR_NORMAL,
1907 _("It took %.2f seconds to enumerate untracked files."),
1908 s->untracked_in_ms / 1000.0);
1910 status_printf_ln(s, GIT_COLOR_NORMAL,
1911 _("See 'git help status' for information on how to improve this."));
1912 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1914 } else if (s->committable)
1915 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1916 s->hints
1917 ? _(" (use -u option to show untracked files)") : "");
1919 if (s->verbose)
1920 wt_longstatus_print_verbose(s);
1921 if (!s->committable) {
1922 if (s->amend)
1923 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1924 else if (s->nowarn)
1925 ; /* nothing */
1926 else if (s->workdir_dirty) {
1927 if (s->hints)
1928 fprintf(s->fp, _("no changes added to commit "
1929 "(use \"git add\" and/or "
1930 "\"git commit -a\")\n"));
1931 else
1932 fprintf(s->fp, _("no changes added to "
1933 "commit\n"));
1934 } else if (s->untracked.nr) {
1935 if (s->hints)
1936 fprintf(s->fp, _("nothing added to commit but "
1937 "untracked files present (use "
1938 "\"git add\" to track)\n"));
1939 else
1940 fprintf(s->fp, _("nothing added to commit but "
1941 "untracked files present\n"));
1942 } else if (s->is_initial) {
1943 if (s->hints)
1944 fprintf(s->fp, _("nothing to commit (create/"
1945 "copy files and use \"git "
1946 "add\" to track)\n"));
1947 else
1948 fprintf(s->fp, _("nothing to commit\n"));
1949 } else if (!s->show_untracked_files) {
1950 if (s->hints)
1951 fprintf(s->fp, _("nothing to commit (use -u to "
1952 "show untracked files)\n"));
1953 else
1954 fprintf(s->fp, _("nothing to commit\n"));
1955 } else
1956 fprintf(s->fp, _("nothing to commit, working tree "
1957 "clean\n"));
1959 if(s->show_stash)
1960 wt_longstatus_print_stash_summary(s);
1963 static void wt_shortstatus_unmerged(struct string_list_item *it,
1964 struct wt_status *s)
1966 struct wt_status_change_data *d = it->util;
1967 const char *how = "??";
1969 switch (d->stagemask) {
1970 case 1: how = "DD"; break; /* both deleted */
1971 case 2: how = "AU"; break; /* added by us */
1972 case 3: how = "UD"; break; /* deleted by them */
1973 case 4: how = "UA"; break; /* added by them */
1974 case 5: how = "DU"; break; /* deleted by us */
1975 case 6: how = "AA"; break; /* both added */
1976 case 7: how = "UU"; break; /* both modified */
1978 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1979 if (s->null_termination) {
1980 fprintf(s->fp, " %s%c", it->string, 0);
1981 } else {
1982 struct strbuf onebuf = STRBUF_INIT;
1983 const char *one;
1984 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
1985 fprintf(s->fp, " %s\n", one);
1986 strbuf_release(&onebuf);
1990 static void wt_shortstatus_status(struct string_list_item *it,
1991 struct wt_status *s)
1993 struct wt_status_change_data *d = it->util;
1995 if (d->index_status)
1996 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1997 else
1998 fputc(' ', s->fp);
1999 if (d->worktree_status)
2000 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
2001 else
2002 fputc(' ', s->fp);
2003 fputc(' ', s->fp);
2004 if (s->null_termination) {
2005 fprintf(s->fp, "%s%c", it->string, 0);
2006 if (d->rename_source)
2007 fprintf(s->fp, "%s%c", d->rename_source, 0);
2008 } else {
2009 struct strbuf onebuf = STRBUF_INIT;
2010 const char *one;
2012 if (d->rename_source) {
2013 one = quote_path(d->rename_source, s->prefix, &onebuf,
2014 QUOTE_PATH_QUOTE_SP);
2015 fprintf(s->fp, "%s -> ", one);
2016 strbuf_release(&onebuf);
2018 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2019 fprintf(s->fp, "%s\n", one);
2020 strbuf_release(&onebuf);
2024 static void wt_shortstatus_other(struct string_list_item *it,
2025 struct wt_status *s, const char *sign)
2027 if (s->null_termination) {
2028 fprintf(s->fp, "%s %s%c", sign, it->string, 0);
2029 } else {
2030 struct strbuf onebuf = STRBUF_INIT;
2031 const char *one;
2032 one = quote_path(it->string, s->prefix, &onebuf, QUOTE_PATH_QUOTE_SP);
2033 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
2034 fprintf(s->fp, " %s\n", one);
2035 strbuf_release(&onebuf);
2039 static void wt_shortstatus_print_tracking(struct wt_status *s)
2041 struct branch *branch;
2042 const char *header_color = color(WT_STATUS_HEADER, s);
2043 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
2044 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
2046 const char *base;
2047 char *short_base;
2048 const char *branch_name;
2049 int num_ours, num_theirs, sti;
2050 int upstream_is_gone = 0;
2052 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
2054 if (!s->branch)
2055 return;
2056 branch_name = s->branch;
2058 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2060 if (s->is_initial)
2061 color_fprintf(s->fp, header_color, LABEL(N_("No commits yet on ")));
2063 if (!strcmp(s->branch, "HEAD")) {
2064 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
2065 LABEL(N_("HEAD (no branch)")));
2066 goto conclude;
2069 skip_prefix(branch_name, "refs/heads/", &branch_name);
2071 branch = branch_get(branch_name);
2073 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
2075 sti = stat_tracking_info(branch, &num_ours, &num_theirs, &base,
2076 0, s->ahead_behind_flags);
2077 if (sti < 0) {
2078 if (!base)
2079 goto conclude;
2081 upstream_is_gone = 1;
2084 short_base = shorten_unambiguous_ref(base, 0);
2085 color_fprintf(s->fp, header_color, "...");
2086 color_fprintf(s->fp, branch_color_remote, "%s", short_base);
2087 free(short_base);
2089 if (!upstream_is_gone && !sti)
2090 goto conclude;
2092 color_fprintf(s->fp, header_color, " [");
2093 if (upstream_is_gone) {
2094 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
2095 } else if (s->ahead_behind_flags == AHEAD_BEHIND_QUICK) {
2096 color_fprintf(s->fp, header_color, LABEL(N_("different")));
2097 } else if (!num_ours) {
2098 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
2099 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2100 } else if (!num_theirs) {
2101 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2102 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2103 } else {
2104 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
2105 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
2106 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
2107 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
2110 color_fprintf(s->fp, header_color, "]");
2111 conclude:
2112 fputc(s->null_termination ? '\0' : '\n', s->fp);
2115 static void wt_shortstatus_print(struct wt_status *s)
2117 struct string_list_item *it;
2119 if (s->show_branch)
2120 wt_shortstatus_print_tracking(s);
2122 for_each_string_list_item(it, &s->change) {
2123 struct wt_status_change_data *d = it->util;
2125 if (d->stagemask)
2126 wt_shortstatus_unmerged(it, s);
2127 else
2128 wt_shortstatus_status(it, s);
2130 for_each_string_list_item(it, &s->untracked)
2131 wt_shortstatus_other(it, s, "??");
2133 for_each_string_list_item(it, &s->ignored)
2134 wt_shortstatus_other(it, s, "!!");
2137 static void wt_porcelain_print(struct wt_status *s)
2139 s->use_color = 0;
2140 s->relative_paths = 0;
2141 s->prefix = NULL;
2142 s->no_gettext = 1;
2143 wt_shortstatus_print(s);
2147 * Print branch information for porcelain v2 output. These lines
2148 * are printed when the '--branch' parameter is given.
2150 * # branch.oid <commit><eol>
2151 * # branch.head <head><eol>
2152 * [# branch.upstream <upstream><eol>
2153 * [# branch.ab +<ahead> -<behind><eol>]]
2155 * <commit> ::= the current commit hash or the literal
2156 * "(initial)" to indicate an initialized repo
2157 * with no commits.
2159 * <head> ::= <branch_name> the current branch name or
2160 * "(detached)" literal when detached head or
2161 * "(unknown)" when something is wrong.
2163 * <upstream> ::= the upstream branch name, when set.
2165 * <ahead> ::= integer ahead value or '?'.
2167 * <behind> ::= integer behind value or '?'.
2169 * The end-of-line is defined by the -z flag.
2171 * <eol> ::= NUL when -z,
2172 * LF when NOT -z.
2174 * When an upstream is set and present, the 'branch.ab' line will
2175 * be printed with the ahead/behind counts for the branch and the
2176 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2177 * are different, '?' will be substituted for the actual count.
2179 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
2181 struct branch *branch;
2182 const char *base;
2183 const char *branch_name;
2184 int ab_info, nr_ahead, nr_behind;
2185 char eol = s->null_termination ? '\0' : '\n';
2187 fprintf(s->fp, "# branch.oid %s%c",
2188 (s->is_initial ? "(initial)" : oid_to_hex(&s->oid_commit)),
2189 eol);
2191 if (!s->branch)
2192 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
2193 else {
2194 if (!strcmp(s->branch, "HEAD")) {
2195 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
2197 if (s->state.rebase_in_progress ||
2198 s->state.rebase_interactive_in_progress)
2199 branch_name = s->state.onto;
2200 else if (s->state.detached_from)
2201 branch_name = s->state.detached_from;
2202 else
2203 branch_name = "";
2204 } else {
2205 branch_name = NULL;
2206 skip_prefix(s->branch, "refs/heads/", &branch_name);
2208 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
2211 /* Lookup stats on the upstream tracking branch, if set. */
2212 branch = branch_get(branch_name);
2213 base = NULL;
2214 ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind,
2215 &base, 0, s->ahead_behind_flags);
2216 if (base) {
2217 base = shorten_unambiguous_ref(base, 0);
2218 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
2219 free((char *)base);
2221 if (ab_info > 0) {
2222 /* different */
2223 if (nr_ahead || nr_behind)
2224 fprintf(s->fp, "# branch.ab +%d -%d%c",
2225 nr_ahead, nr_behind, eol);
2226 else
2227 fprintf(s->fp, "# branch.ab +? -?%c",
2228 eol);
2229 } else if (!ab_info) {
2230 /* same */
2231 fprintf(s->fp, "# branch.ab +0 -0%c", eol);
2238 * Print the stash count in a porcelain-friendly format
2240 static void wt_porcelain_v2_print_stash(struct wt_status *s)
2242 int stash_count = count_stash_entries();
2243 char eol = s->null_termination ? '\0' : '\n';
2245 if (stash_count > 0)
2246 fprintf(s->fp, "# stash %d%c", stash_count, eol);
2250 * Convert various submodule status values into a
2251 * fixed-length string of characters in the buffer provided.
2253 static void wt_porcelain_v2_submodule_state(
2254 struct wt_status_change_data *d,
2255 char sub[5])
2257 if (S_ISGITLINK(d->mode_head) ||
2258 S_ISGITLINK(d->mode_index) ||
2259 S_ISGITLINK(d->mode_worktree)) {
2260 sub[0] = 'S';
2261 sub[1] = d->new_submodule_commits ? 'C' : '.';
2262 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
2263 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
2264 } else {
2265 sub[0] = 'N';
2266 sub[1] = '.';
2267 sub[2] = '.';
2268 sub[3] = '.';
2270 sub[4] = 0;
2274 * Fix-up changed entries before we print them.
2276 static void wt_porcelain_v2_fix_up_changed(struct string_list_item *it)
2278 struct wt_status_change_data *d = it->util;
2280 if (!d->index_status) {
2282 * This entry is unchanged in the index (relative to the head).
2283 * Therefore, the collect_updated_cb was never called for this
2284 * entry (during the head-vs-index scan) and so the head column
2285 * fields were never set.
2287 * We must have data for the index column (from the
2288 * index-vs-worktree scan (otherwise, this entry should not be
2289 * in the list of changes)).
2291 * Copy index column fields to the head column, so that our
2292 * output looks complete.
2294 assert(d->mode_head == 0);
2295 d->mode_head = d->mode_index;
2296 oidcpy(&d->oid_head, &d->oid_index);
2299 if (!d->worktree_status) {
2301 * This entry is unchanged in the worktree (relative to the index).
2302 * Therefore, the collect_changed_cb was never called for this entry
2303 * (during the index-vs-worktree scan) and so the worktree column
2304 * fields were never set.
2306 * We must have data for the index column (from the head-vs-index
2307 * scan).
2309 * Copy the index column fields to the worktree column so that
2310 * our output looks complete.
2312 * Note that we only have a mode field in the worktree column
2313 * because the scan code tries really hard to not have to compute it.
2315 assert(d->mode_worktree == 0);
2316 d->mode_worktree = d->mode_index;
2321 * Print porcelain v2 info for tracked entries with changes.
2323 static void wt_porcelain_v2_print_changed_entry(
2324 struct string_list_item *it,
2325 struct wt_status *s)
2327 struct wt_status_change_data *d = it->util;
2328 struct strbuf buf = STRBUF_INIT;
2329 struct strbuf buf_from = STRBUF_INIT;
2330 const char *path = NULL;
2331 const char *path_from = NULL;
2332 char key[3];
2333 char submodule_token[5];
2334 char sep_char, eol_char;
2336 wt_porcelain_v2_fix_up_changed(it);
2337 wt_porcelain_v2_submodule_state(d, submodule_token);
2339 key[0] = d->index_status ? d->index_status : '.';
2340 key[1] = d->worktree_status ? d->worktree_status : '.';
2341 key[2] = 0;
2343 if (s->null_termination) {
2345 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2346 * A single NUL character separates them.
2348 sep_char = '\0';
2349 eol_char = '\0';
2350 path = it->string;
2351 path_from = d->rename_source;
2352 } else {
2354 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2355 * The source path is only present when necessary.
2356 * A single TAB separates them (because paths can contain spaces
2357 * which are not escaped and C-quoting does escape TAB characters).
2359 sep_char = '\t';
2360 eol_char = '\n';
2361 path = quote_path(it->string, s->prefix, &buf, 0);
2362 if (d->rename_source)
2363 path_from = quote_path(d->rename_source, s->prefix, &buf_from, 0);
2366 if (path_from)
2367 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2368 key, submodule_token,
2369 d->mode_head, d->mode_index, d->mode_worktree,
2370 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2371 d->rename_status, d->rename_score,
2372 path, sep_char, path_from, eol_char);
2373 else
2374 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2375 key, submodule_token,
2376 d->mode_head, d->mode_index, d->mode_worktree,
2377 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2378 path, eol_char);
2380 strbuf_release(&buf);
2381 strbuf_release(&buf_from);
2385 * Print porcelain v2 status info for unmerged entries.
2387 static void wt_porcelain_v2_print_unmerged_entry(
2388 struct string_list_item *it,
2389 struct wt_status *s)
2391 struct wt_status_change_data *d = it->util;
2392 struct index_state *istate = s->repo->index;
2393 const struct cache_entry *ce;
2394 struct strbuf buf_index = STRBUF_INIT;
2395 const char *path_index = NULL;
2396 int pos, stage, sum;
2397 struct {
2398 int mode;
2399 struct object_id oid;
2400 } stages[3];
2401 char *key;
2402 char submodule_token[5];
2403 char unmerged_prefix = 'u';
2404 char eol_char = s->null_termination ? '\0' : '\n';
2406 wt_porcelain_v2_submodule_state(d, submodule_token);
2408 switch (d->stagemask) {
2409 case 1: key = "DD"; break; /* both deleted */
2410 case 2: key = "AU"; break; /* added by us */
2411 case 3: key = "UD"; break; /* deleted by them */
2412 case 4: key = "UA"; break; /* added by them */
2413 case 5: key = "DU"; break; /* deleted by us */
2414 case 6: key = "AA"; break; /* both added */
2415 case 7: key = "UU"; break; /* both modified */
2416 default:
2417 BUG("unhandled unmerged status %x", d->stagemask);
2421 * Disregard d.aux.porcelain_v2 data that we accumulated
2422 * for the head and index columns during the scans and
2423 * replace with the actual stage data.
2425 * Note that this is a last-one-wins for each the individual
2426 * stage [123] columns in the event of multiple cache entries
2427 * for same stage.
2429 memset(stages, 0, sizeof(stages));
2430 sum = 0;
2431 pos = index_name_pos(istate, it->string, strlen(it->string));
2432 assert(pos < 0);
2433 pos = -pos-1;
2434 while (pos < istate->cache_nr) {
2435 ce = istate->cache[pos++];
2436 stage = ce_stage(ce);
2437 if (strcmp(ce->name, it->string) || !stage)
2438 break;
2439 stages[stage - 1].mode = ce->ce_mode;
2440 oidcpy(&stages[stage - 1].oid, &ce->oid);
2441 sum |= (1 << (stage - 1));
2443 if (sum != d->stagemask)
2444 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2446 if (s->null_termination)
2447 path_index = it->string;
2448 else
2449 path_index = quote_path(it->string, s->prefix, &buf_index, 0);
2451 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2452 unmerged_prefix, key, submodule_token,
2453 stages[0].mode, /* stage 1 */
2454 stages[1].mode, /* stage 2 */
2455 stages[2].mode, /* stage 3 */
2456 d->mode_worktree,
2457 oid_to_hex(&stages[0].oid), /* stage 1 */
2458 oid_to_hex(&stages[1].oid), /* stage 2 */
2459 oid_to_hex(&stages[2].oid), /* stage 3 */
2460 path_index,
2461 eol_char);
2463 strbuf_release(&buf_index);
2467 * Print porcelain V2 status info for untracked and ignored entries.
2469 static void wt_porcelain_v2_print_other(
2470 struct string_list_item *it,
2471 struct wt_status *s,
2472 char prefix)
2474 struct strbuf buf = STRBUF_INIT;
2475 const char *path;
2476 char eol_char;
2478 if (s->null_termination) {
2479 path = it->string;
2480 eol_char = '\0';
2481 } else {
2482 path = quote_path(it->string, s->prefix, &buf, 0);
2483 eol_char = '\n';
2486 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2488 strbuf_release(&buf);
2492 * Print porcelain V2 status.
2494 * [<v2_branch>]
2495 * [<v2_changed_items>]*
2496 * [<v2_unmerged_items>]*
2497 * [<v2_untracked_items>]*
2498 * [<v2_ignored_items>]*
2501 static void wt_porcelain_v2_print(struct wt_status *s)
2503 struct wt_status_change_data *d;
2504 struct string_list_item *it;
2505 int i;
2507 if (s->show_branch)
2508 wt_porcelain_v2_print_tracking(s);
2510 if (s->show_stash)
2511 wt_porcelain_v2_print_stash(s);
2513 for (i = 0; i < s->change.nr; i++) {
2514 it = &(s->change.items[i]);
2515 d = it->util;
2516 if (!d->stagemask)
2517 wt_porcelain_v2_print_changed_entry(it, s);
2520 for (i = 0; i < s->change.nr; i++) {
2521 it = &(s->change.items[i]);
2522 d = it->util;
2523 if (d->stagemask)
2524 wt_porcelain_v2_print_unmerged_entry(it, s);
2527 for (i = 0; i < s->untracked.nr; i++) {
2528 it = &(s->untracked.items[i]);
2529 wt_porcelain_v2_print_other(it, s, '?');
2532 for (i = 0; i < s->ignored.nr; i++) {
2533 it = &(s->ignored.items[i]);
2534 wt_porcelain_v2_print_other(it, s, '!');
2538 void wt_status_print(struct wt_status *s)
2540 trace2_data_intmax("status", s->repo, "count/changed", s->change.nr);
2541 trace2_data_intmax("status", s->repo, "count/untracked",
2542 s->untracked.nr);
2543 trace2_data_intmax("status", s->repo, "count/ignored", s->ignored.nr);
2545 trace2_region_enter("status", "print", s->repo);
2547 switch (s->status_format) {
2548 case STATUS_FORMAT_SHORT:
2549 wt_shortstatus_print(s);
2550 break;
2551 case STATUS_FORMAT_PORCELAIN:
2552 wt_porcelain_print(s);
2553 break;
2554 case STATUS_FORMAT_PORCELAIN_V2:
2555 wt_porcelain_v2_print(s);
2556 break;
2557 case STATUS_FORMAT_UNSPECIFIED:
2558 BUG("finalize_deferred_config() should have been called");
2559 break;
2560 case STATUS_FORMAT_NONE:
2561 case STATUS_FORMAT_LONG:
2562 wt_longstatus_print(s);
2563 break;
2566 trace2_region_leave("status", "print", s->repo);
2570 * Returns 1 if there are unstaged changes, 0 otherwise.
2572 int has_unstaged_changes(struct repository *r, int ignore_submodules)
2574 struct rev_info rev_info;
2575 int result;
2577 repo_init_revisions(r, &rev_info, NULL);
2578 if (ignore_submodules) {
2579 rev_info.diffopt.flags.ignore_submodules = 1;
2580 rev_info.diffopt.flags.override_submodule_config = 1;
2582 rev_info.diffopt.flags.quick = 1;
2583 diff_setup_done(&rev_info.diffopt);
2584 run_diff_files(&rev_info, 0);
2585 result = diff_result_code(&rev_info.diffopt);
2586 release_revisions(&rev_info);
2587 return result;
2591 * Returns 1 if there are uncommitted changes, 0 otherwise.
2593 int has_uncommitted_changes(struct repository *r,
2594 int ignore_submodules)
2596 struct rev_info rev_info;
2597 int result;
2599 if (is_index_unborn(r->index))
2600 return 0;
2602 repo_init_revisions(r, &rev_info, NULL);
2603 if (ignore_submodules)
2604 rev_info.diffopt.flags.ignore_submodules = 1;
2605 rev_info.diffopt.flags.quick = 1;
2607 add_head_to_pending(&rev_info);
2608 if (!rev_info.pending.nr) {
2610 * We have no head (or it's corrupt); use the empty tree,
2611 * which will complain if the index is non-empty.
2613 struct tree *tree = lookup_tree(r, the_hash_algo->empty_tree);
2614 add_pending_object(&rev_info, &tree->object, "");
2617 diff_setup_done(&rev_info.diffopt);
2618 run_diff_index(&rev_info, DIFF_INDEX_CACHED);
2619 result = diff_result_code(&rev_info.diffopt);
2620 release_revisions(&rev_info);
2621 return result;
2625 * If the work tree has unstaged or uncommitted changes, dies with the
2626 * appropriate message.
2628 int require_clean_work_tree(struct repository *r,
2629 const char *action,
2630 const char *hint,
2631 int ignore_submodules,
2632 int gently)
2634 struct lock_file lock_file = LOCK_INIT;
2635 int err = 0, fd;
2637 fd = repo_hold_locked_index(r, &lock_file, 0);
2638 refresh_index(r->index, REFRESH_QUIET, NULL, NULL, NULL);
2639 if (0 <= fd)
2640 repo_update_index_if_able(r, &lock_file);
2641 rollback_lock_file(&lock_file);
2643 if (has_unstaged_changes(r, ignore_submodules)) {
2644 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2645 error(_("cannot %s: You have unstaged changes."), _(action));
2646 err = 1;
2649 if (has_uncommitted_changes(r, ignore_submodules)) {
2650 if (err)
2651 error(_("additionally, your index contains uncommitted changes."));
2652 else
2653 error(_("cannot %s: Your index contains uncommitted changes."),
2654 _(action));
2655 err = 1;
2658 if (err) {
2659 if (hint) {
2660 if (!*hint)
2661 BUG("empty hint passed to require_clean_work_tree();"
2662 " use NULL instead");
2663 error("%s", hint);
2665 if (!gently)
2666 exit(128);
2669 return err;