mingw: simplify PATH handling
[git/git-svn.git] / wt-status.c
blob7daa5320ac7538835285495782e74be5423ed76b
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 "revision.h"
8 #include "diffcore.h"
9 #include "quote.h"
10 #include "run-command.h"
11 #include "argv-array.h"
12 #include "remote.h"
13 #include "refs.h"
14 #include "submodule.h"
15 #include "column.h"
16 #include "strbuf.h"
17 #include "utf8.h"
18 #include "worktree.h"
19 #include "lockfile.h"
21 static const char cut_line[] =
22 "------------------------ >8 ------------------------\n";
24 static char default_wt_status_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
30 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
31 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
33 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
36 static const char *color(int slot, struct wt_status *s)
38 const char *c = "";
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
41 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
43 return c;
46 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
53 strbuf_vaddf(&sb, fmt, ap);
54 if (!sb.len) {
55 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
57 if (!trail)
58 strbuf_addch(&sb, ' ');
60 color_print_strbuf(s->fp, color, &sb);
61 if (trail)
62 fprintf(s->fp, "%s", trail);
63 strbuf_release(&sb);
64 return;
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
69 strbuf_reset(&linebuf);
70 if (at_bol && s->display_comment_prefix) {
71 strbuf_addch(&linebuf, comment_line_char);
72 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
75 if (eol)
76 strbuf_add(&linebuf, line, eol - line);
77 else
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
80 if (eol)
81 fprintf(s->fp, "\n");
82 else
83 break;
84 at_bol = 1;
86 if (trail)
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
89 strbuf_release(&sb);
92 void status_printf_ln(struct wt_status *s, const char *color,
93 const char *fmt, ...)
95 va_list ap;
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, "\n");
99 va_end(ap);
102 void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
105 va_list ap;
107 va_start(ap, fmt);
108 status_vprintf(s, 1, color, fmt, ap, NULL);
109 va_end(ap);
112 static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
115 va_list ap;
117 va_start(ap, fmt);
118 status_vprintf(s, 0, color, fmt, ap, NULL);
119 va_end(ap);
122 void wt_status_prepare(struct wt_status *s)
124 struct object_id oid;
126 memset(s, 0, sizeof(*s));
127 memcpy(s->color_palette, default_wt_status_colors,
128 sizeof(default_wt_status_colors));
129 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
130 s->use_color = -1;
131 s->relative_paths = 1;
132 s->branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
133 s->reference = "HEAD";
134 s->fp = stdout;
135 s->index_file = get_index_file();
136 s->change.strdup_strings = 1;
137 s->untracked.strdup_strings = 1;
138 s->ignored.strdup_strings = 1;
139 s->show_branch = -1; /* unspecified */
140 s->display_comment_prefix = 0;
143 static void wt_longstatus_print_unmerged_header(struct wt_status *s)
145 int i;
146 int del_mod_conflict = 0;
147 int both_deleted = 0;
148 int not_deleted = 0;
149 const char *c = color(WT_STATUS_HEADER, s);
151 status_printf_ln(s, c, _("Unmerged paths:"));
153 for (i = 0; i < s->change.nr; i++) {
154 struct string_list_item *it = &(s->change.items[i]);
155 struct wt_status_change_data *d = it->util;
157 switch (d->stagemask) {
158 case 0:
159 break;
160 case 1:
161 both_deleted = 1;
162 break;
163 case 3:
164 case 5:
165 del_mod_conflict = 1;
166 break;
167 default:
168 not_deleted = 1;
169 break;
173 if (!s->hints)
174 return;
175 if (s->whence != FROM_COMMIT)
177 else if (!s->is_initial)
178 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
179 else
180 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
182 if (!both_deleted) {
183 if (!del_mod_conflict)
184 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
185 else
186 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
187 } else if (!del_mod_conflict && !not_deleted) {
188 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
189 } else {
190 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
192 status_printf_ln(s, c, "%s", "");
195 static void wt_longstatus_print_cached_header(struct wt_status *s)
197 const char *c = color(WT_STATUS_HEADER, s);
199 status_printf_ln(s, c, _("Changes to be committed:"));
200 if (!s->hints)
201 return;
202 if (s->whence != FROM_COMMIT)
203 ; /* NEEDSWORK: use "git reset --unresolve"??? */
204 else if (!s->is_initial)
205 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
206 else
207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
208 status_printf_ln(s, c, "%s", "");
211 static void wt_longstatus_print_dirty_header(struct wt_status *s,
212 int has_deleted,
213 int has_dirty_submodules)
215 const char *c = color(WT_STATUS_HEADER, s);
217 status_printf_ln(s, c, _("Changes not staged for commit:"));
218 if (!s->hints)
219 return;
220 if (!has_deleted)
221 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
222 else
223 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
224 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
225 if (has_dirty_submodules)
226 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
227 status_printf_ln(s, c, "%s", "");
230 static void wt_longstatus_print_other_header(struct wt_status *s,
231 const char *what,
232 const char *how)
234 const char *c = color(WT_STATUS_HEADER, s);
235 status_printf_ln(s, c, "%s:", what);
236 if (!s->hints)
237 return;
238 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
239 status_printf_ln(s, c, "%s", "");
242 static void wt_longstatus_print_trailer(struct wt_status *s)
244 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
247 #define quote_path quote_path_relative
249 static const char *wt_status_unmerged_status_string(int stagemask)
251 switch (stagemask) {
252 case 1:
253 return _("both deleted:");
254 case 2:
255 return _("added by us:");
256 case 3:
257 return _("deleted by them:");
258 case 4:
259 return _("added by them:");
260 case 5:
261 return _("deleted by us:");
262 case 6:
263 return _("both added:");
264 case 7:
265 return _("both modified:");
266 default:
267 die("BUG: unhandled unmerged status %x", stagemask);
271 static const char *wt_status_diff_status_string(int status)
273 switch (status) {
274 case DIFF_STATUS_ADDED:
275 return _("new file:");
276 case DIFF_STATUS_COPIED:
277 return _("copied:");
278 case DIFF_STATUS_DELETED:
279 return _("deleted:");
280 case DIFF_STATUS_MODIFIED:
281 return _("modified:");
282 case DIFF_STATUS_RENAMED:
283 return _("renamed:");
284 case DIFF_STATUS_TYPE_CHANGED:
285 return _("typechange:");
286 case DIFF_STATUS_UNKNOWN:
287 return _("unknown:");
288 case DIFF_STATUS_UNMERGED:
289 return _("unmerged:");
290 default:
291 return NULL;
295 static int maxwidth(const char *(*label)(int), int minval, int maxval)
297 int result = 0, i;
299 for (i = minval; i <= maxval; i++) {
300 const char *s = label(i);
301 int len = s ? utf8_strwidth(s) : 0;
302 if (len > result)
303 result = len;
305 return result;
308 static void wt_longstatus_print_unmerged_data(struct wt_status *s,
309 struct string_list_item *it)
311 const char *c = color(WT_STATUS_UNMERGED, s);
312 struct wt_status_change_data *d = it->util;
313 struct strbuf onebuf = STRBUF_INIT;
314 static char *padding;
315 static int label_width;
316 const char *one, *how;
317 int len;
319 if (!padding) {
320 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
321 label_width += strlen(" ");
322 padding = xmallocz(label_width);
323 memset(padding, ' ', label_width);
326 one = quote_path(it->string, s->prefix, &onebuf);
327 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
329 how = wt_status_unmerged_status_string(d->stagemask);
330 len = label_width - utf8_strwidth(how);
331 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
332 strbuf_release(&onebuf);
335 static void wt_longstatus_print_change_data(struct wt_status *s,
336 int change_type,
337 struct string_list_item *it)
339 struct wt_status_change_data *d = it->util;
340 const char *c = color(change_type, s);
341 int status;
342 char *one_name;
343 char *two_name;
344 const char *one, *two;
345 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
346 struct strbuf extra = STRBUF_INIT;
347 static char *padding;
348 static int label_width;
349 const char *what;
350 int len;
352 if (!padding) {
353 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
354 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
355 label_width += strlen(" ");
356 padding = xmallocz(label_width);
357 memset(padding, ' ', label_width);
360 one_name = two_name = it->string;
361 switch (change_type) {
362 case WT_STATUS_UPDATED:
363 status = d->index_status;
364 if (d->head_path)
365 one_name = d->head_path;
366 break;
367 case WT_STATUS_CHANGED:
368 if (d->new_submodule_commits || d->dirty_submodule) {
369 strbuf_addstr(&extra, " (");
370 if (d->new_submodule_commits)
371 strbuf_addstr(&extra, _("new commits, "));
372 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
373 strbuf_addstr(&extra, _("modified content, "));
374 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
375 strbuf_addstr(&extra, _("untracked content, "));
376 strbuf_setlen(&extra, extra.len - 2);
377 strbuf_addch(&extra, ')');
379 status = d->worktree_status;
380 break;
381 default:
382 die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
383 change_type);
386 one = quote_path(one_name, s->prefix, &onebuf);
387 two = quote_path(two_name, s->prefix, &twobuf);
389 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
390 what = wt_status_diff_status_string(status);
391 if (!what)
392 die("BUG: unhandled diff status %c", status);
393 len = label_width - utf8_strwidth(what);
394 assert(len >= 0);
395 if (status == DIFF_STATUS_COPIED || status == DIFF_STATUS_RENAMED)
396 status_printf_more(s, c, "%s%.*s%s -> %s",
397 what, len, padding, one, two);
398 else
399 status_printf_more(s, c, "%s%.*s%s",
400 what, len, padding, one);
401 if (extra.len) {
402 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
403 strbuf_release(&extra);
405 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
406 strbuf_release(&onebuf);
407 strbuf_release(&twobuf);
410 static char short_submodule_status(struct wt_status_change_data *d) {
411 if (d->new_submodule_commits)
412 return 'M';
413 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
414 return 'm';
415 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
416 return '?';
417 return d->worktree_status;
420 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
421 struct diff_options *options,
422 void *data)
424 struct wt_status *s = data;
425 int i;
427 if (!q->nr)
428 return;
429 s->workdir_dirty = 1;
430 for (i = 0; i < q->nr; i++) {
431 struct diff_filepair *p;
432 struct string_list_item *it;
433 struct wt_status_change_data *d;
435 p = q->queue[i];
436 it = string_list_insert(&s->change, p->one->path);
437 d = it->util;
438 if (!d) {
439 d = xcalloc(1, sizeof(*d));
440 it->util = d;
442 if (!d->worktree_status)
443 d->worktree_status = p->status;
444 if (S_ISGITLINK(p->two->mode)) {
445 d->dirty_submodule = p->two->dirty_submodule;
446 d->new_submodule_commits = !!oidcmp(&p->one->oid,
447 &p->two->oid);
448 if (s->status_format == STATUS_FORMAT_SHORT)
449 d->worktree_status = short_submodule_status(d);
452 switch (p->status) {
453 case DIFF_STATUS_ADDED:
454 d->mode_worktree = p->two->mode;
455 break;
457 case DIFF_STATUS_DELETED:
458 d->mode_index = p->one->mode;
459 oidcpy(&d->oid_index, &p->one->oid);
460 /* mode_worktree is zero for a delete. */
461 break;
463 case DIFF_STATUS_MODIFIED:
464 case DIFF_STATUS_TYPE_CHANGED:
465 case DIFF_STATUS_UNMERGED:
466 d->mode_index = p->one->mode;
467 d->mode_worktree = p->two->mode;
468 oidcpy(&d->oid_index, &p->one->oid);
469 break;
471 case DIFF_STATUS_UNKNOWN:
472 die("BUG: worktree status unknown???");
473 break;
479 static int unmerged_mask(const char *path)
481 int pos, mask;
482 const struct cache_entry *ce;
484 pos = cache_name_pos(path, strlen(path));
485 if (0 <= pos)
486 return 0;
488 mask = 0;
489 pos = -pos-1;
490 while (pos < active_nr) {
491 ce = active_cache[pos++];
492 if (strcmp(ce->name, path) || !ce_stage(ce))
493 break;
494 mask |= (1 << (ce_stage(ce) - 1));
496 return mask;
499 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
500 struct diff_options *options,
501 void *data)
503 struct wt_status *s = data;
504 int i;
506 for (i = 0; i < q->nr; i++) {
507 struct diff_filepair *p;
508 struct string_list_item *it;
509 struct wt_status_change_data *d;
511 p = q->queue[i];
512 it = string_list_insert(&s->change, p->two->path);
513 d = it->util;
514 if (!d) {
515 d = xcalloc(1, sizeof(*d));
516 it->util = d;
518 if (!d->index_status)
519 d->index_status = p->status;
520 switch (p->status) {
521 case DIFF_STATUS_ADDED:
522 /* Leave {mode,oid}_head zero for an add. */
523 d->mode_index = p->two->mode;
524 oidcpy(&d->oid_index, &p->two->oid);
525 break;
526 case DIFF_STATUS_DELETED:
527 d->mode_head = p->one->mode;
528 oidcpy(&d->oid_head, &p->one->oid);
529 /* Leave {mode,oid}_index zero for a delete. */
530 break;
532 case DIFF_STATUS_COPIED:
533 case DIFF_STATUS_RENAMED:
534 d->head_path = xstrdup(p->one->path);
535 d->score = p->score * 100 / MAX_SCORE;
536 /* fallthru */
537 case DIFF_STATUS_MODIFIED:
538 case DIFF_STATUS_TYPE_CHANGED:
539 d->mode_head = p->one->mode;
540 d->mode_index = p->two->mode;
541 oidcpy(&d->oid_head, &p->one->oid);
542 oidcpy(&d->oid_index, &p->two->oid);
543 break;
544 case DIFF_STATUS_UNMERGED:
545 d->stagemask = unmerged_mask(p->two->path);
547 * Don't bother setting {mode,oid}_{head,index} since the print
548 * code will output the stage values directly and not use the
549 * values in these fields.
551 break;
556 static void wt_status_collect_changes_worktree(struct wt_status *s)
558 struct rev_info rev;
560 init_revisions(&rev, NULL);
561 setup_revisions(0, NULL, &rev, NULL);
562 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
563 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
564 rev.diffopt.ita_invisible_in_index = 1;
565 if (!s->show_untracked_files)
566 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
567 if (s->ignore_submodule_arg) {
568 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
569 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
571 rev.diffopt.format_callback = wt_status_collect_changed_cb;
572 rev.diffopt.format_callback_data = s;
573 copy_pathspec(&rev.prune_data, &s->pathspec);
574 run_diff_files(&rev, 0);
577 static void wt_status_collect_changes_index(struct wt_status *s)
579 struct rev_info rev;
580 struct setup_revision_opt opt;
582 init_revisions(&rev, NULL);
583 memset(&opt, 0, sizeof(opt));
584 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
585 setup_revisions(0, NULL, &rev, &opt);
587 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
588 rev.diffopt.ita_invisible_in_index = 1;
589 if (s->ignore_submodule_arg) {
590 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
591 } else {
593 * Unless the user did explicitly request a submodule ignore
594 * mode by passing a command line option we do not ignore any
595 * changed submodule SHA-1s when comparing index and HEAD, no
596 * matter what is configured. Otherwise the user won't be
597 * shown any submodules she manually added (and which are
598 * staged to be committed), which would be really confusing.
600 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
603 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
604 rev.diffopt.format_callback = wt_status_collect_updated_cb;
605 rev.diffopt.format_callback_data = s;
606 rev.diffopt.detect_rename = 1;
607 rev.diffopt.rename_limit = 200;
608 rev.diffopt.break_opt = 0;
609 copy_pathspec(&rev.prune_data, &s->pathspec);
610 run_diff_index(&rev, 1);
613 static void wt_status_collect_changes_initial(struct wt_status *s)
615 int i;
617 for (i = 0; i < active_nr; i++) {
618 struct string_list_item *it;
619 struct wt_status_change_data *d;
620 const struct cache_entry *ce = active_cache[i];
622 if (!ce_path_match(ce, &s->pathspec, NULL))
623 continue;
624 if (ce_intent_to_add(ce))
625 continue;
626 it = string_list_insert(&s->change, ce->name);
627 d = it->util;
628 if (!d) {
629 d = xcalloc(1, sizeof(*d));
630 it->util = d;
632 if (ce_stage(ce)) {
633 d->index_status = DIFF_STATUS_UNMERGED;
634 d->stagemask |= (1 << (ce_stage(ce) - 1));
636 * Don't bother setting {mode,oid}_{head,index} since the print
637 * code will output the stage values directly and not use the
638 * values in these fields.
640 } else {
641 d->index_status = DIFF_STATUS_ADDED;
642 /* Leave {mode,oid}_head zero for adds. */
643 d->mode_index = ce->ce_mode;
644 oidcpy(&d->oid_index, &ce->oid);
649 static void wt_status_collect_untracked(struct wt_status *s)
651 int i;
652 struct dir_struct dir;
653 uint64_t t_begin = getnanotime();
655 if (!s->show_untracked_files)
656 return;
658 memset(&dir, 0, sizeof(dir));
659 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
660 dir.flags |=
661 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
662 if (s->show_ignored_files)
663 dir.flags |= DIR_SHOW_IGNORED_TOO;
664 else
665 dir.untracked = the_index.untracked;
666 setup_standard_excludes(&dir);
668 fill_directory(&dir, &s->pathspec);
670 for (i = 0; i < dir.nr; i++) {
671 struct dir_entry *ent = dir.entries[i];
672 if (cache_name_is_other(ent->name, ent->len) &&
673 dir_path_match(ent, &s->pathspec, 0, NULL))
674 string_list_insert(&s->untracked, ent->name);
675 free(ent);
678 for (i = 0; i < dir.ignored_nr; i++) {
679 struct dir_entry *ent = dir.ignored[i];
680 if (cache_name_is_other(ent->name, ent->len) &&
681 dir_path_match(ent, &s->pathspec, 0, NULL))
682 string_list_insert(&s->ignored, ent->name);
683 free(ent);
686 free(dir.entries);
687 free(dir.ignored);
688 clear_directory(&dir);
690 if (advice_status_u_option)
691 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
694 void wt_status_collect(struct wt_status *s)
696 wt_status_collect_changes_worktree(s);
698 if (s->is_initial)
699 wt_status_collect_changes_initial(s);
700 else
701 wt_status_collect_changes_index(s);
702 wt_status_collect_untracked(s);
705 static void wt_longstatus_print_unmerged(struct wt_status *s)
707 int shown_header = 0;
708 int i;
710 for (i = 0; i < s->change.nr; i++) {
711 struct wt_status_change_data *d;
712 struct string_list_item *it;
713 it = &(s->change.items[i]);
714 d = it->util;
715 if (!d->stagemask)
716 continue;
717 if (!shown_header) {
718 wt_longstatus_print_unmerged_header(s);
719 shown_header = 1;
721 wt_longstatus_print_unmerged_data(s, it);
723 if (shown_header)
724 wt_longstatus_print_trailer(s);
728 static void wt_longstatus_print_updated(struct wt_status *s)
730 int shown_header = 0;
731 int i;
733 for (i = 0; i < s->change.nr; i++) {
734 struct wt_status_change_data *d;
735 struct string_list_item *it;
736 it = &(s->change.items[i]);
737 d = it->util;
738 if (!d->index_status ||
739 d->index_status == DIFF_STATUS_UNMERGED)
740 continue;
741 if (!shown_header) {
742 wt_longstatus_print_cached_header(s);
743 s->commitable = 1;
744 shown_header = 1;
746 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
748 if (shown_header)
749 wt_longstatus_print_trailer(s);
753 * -1 : has delete
754 * 0 : no change
755 * 1 : some change but no delete
757 static int wt_status_check_worktree_changes(struct wt_status *s,
758 int *dirty_submodules)
760 int i;
761 int changes = 0;
763 *dirty_submodules = 0;
765 for (i = 0; i < s->change.nr; i++) {
766 struct wt_status_change_data *d;
767 d = s->change.items[i].util;
768 if (!d->worktree_status ||
769 d->worktree_status == DIFF_STATUS_UNMERGED)
770 continue;
771 if (!changes)
772 changes = 1;
773 if (d->dirty_submodule)
774 *dirty_submodules = 1;
775 if (d->worktree_status == DIFF_STATUS_DELETED)
776 changes = -1;
778 return changes;
781 static void wt_longstatus_print_changed(struct wt_status *s)
783 int i, dirty_submodules;
784 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
786 if (!worktree_changes)
787 return;
789 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
791 for (i = 0; i < s->change.nr; i++) {
792 struct wt_status_change_data *d;
793 struct string_list_item *it;
794 it = &(s->change.items[i]);
795 d = it->util;
796 if (!d->worktree_status ||
797 d->worktree_status == DIFF_STATUS_UNMERGED)
798 continue;
799 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
801 wt_longstatus_print_trailer(s);
804 static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
806 struct child_process sm_summary = CHILD_PROCESS_INIT;
807 struct strbuf cmd_stdout = STRBUF_INIT;
808 struct strbuf summary = STRBUF_INIT;
809 char *summary_content;
811 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
812 s->index_file);
814 argv_array_push(&sm_summary.args, "submodule");
815 argv_array_push(&sm_summary.args, "summary");
816 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
817 argv_array_push(&sm_summary.args, "--for-status");
818 argv_array_push(&sm_summary.args, "--summary-limit");
819 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
820 if (!uncommitted)
821 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
823 sm_summary.git_cmd = 1;
824 sm_summary.no_stdin = 1;
826 capture_command(&sm_summary, &cmd_stdout, 1024);
828 /* prepend header, only if there's an actual output */
829 if (cmd_stdout.len) {
830 if (uncommitted)
831 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
832 else
833 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
834 strbuf_addstr(&summary, "\n\n");
836 strbuf_addbuf(&summary, &cmd_stdout);
837 strbuf_release(&cmd_stdout);
839 if (s->display_comment_prefix) {
840 size_t len;
841 summary_content = strbuf_detach(&summary, &len);
842 strbuf_add_commented_lines(&summary, summary_content, len);
843 free(summary_content);
846 fputs(summary.buf, s->fp);
847 strbuf_release(&summary);
850 static void wt_longstatus_print_other(struct wt_status *s,
851 struct string_list *l,
852 const char *what,
853 const char *how)
855 int i;
856 struct strbuf buf = STRBUF_INIT;
857 static struct string_list output = STRING_LIST_INIT_DUP;
858 struct column_options copts;
860 if (!l->nr)
861 return;
863 wt_longstatus_print_other_header(s, what, how);
865 for (i = 0; i < l->nr; i++) {
866 struct string_list_item *it;
867 const char *path;
868 it = &(l->items[i]);
869 path = quote_path(it->string, s->prefix, &buf);
870 if (column_active(s->colopts)) {
871 string_list_append(&output, path);
872 continue;
874 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
875 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
876 "%s\n", path);
879 strbuf_release(&buf);
880 if (!column_active(s->colopts))
881 goto conclude;
883 strbuf_addf(&buf, "%s%s\t%s",
884 color(WT_STATUS_HEADER, s),
885 s->display_comment_prefix ? "#" : "",
886 color(WT_STATUS_UNTRACKED, s));
887 memset(&copts, 0, sizeof(copts));
888 copts.padding = 1;
889 copts.indent = buf.buf;
890 if (want_color(s->use_color))
891 copts.nl = GIT_COLOR_RESET "\n";
892 print_columns(&output, s->colopts, &copts);
893 string_list_clear(&output, 0);
894 strbuf_release(&buf);
895 conclude:
896 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
899 void wt_status_truncate_message_at_cut_line(struct strbuf *buf)
901 const char *p;
902 struct strbuf pattern = STRBUF_INIT;
904 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
905 if (starts_with(buf->buf, pattern.buf + 1))
906 strbuf_setlen(buf, 0);
907 else if ((p = strstr(buf->buf, pattern.buf)))
908 strbuf_setlen(buf, p - buf->buf + 1);
909 strbuf_release(&pattern);
912 void wt_status_add_cut_line(FILE *fp)
914 const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
915 struct strbuf buf = STRBUF_INIT;
917 fprintf(fp, "%c %s", comment_line_char, cut_line);
918 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
919 fputs(buf.buf, fp);
920 strbuf_release(&buf);
923 static void wt_longstatus_print_verbose(struct wt_status *s)
925 struct rev_info rev;
926 struct setup_revision_opt opt;
927 int dirty_submodules;
928 const char *c = color(WT_STATUS_HEADER, s);
930 init_revisions(&rev, NULL);
931 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
932 rev.diffopt.ita_invisible_in_index = 1;
934 memset(&opt, 0, sizeof(opt));
935 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
936 setup_revisions(0, NULL, &rev, &opt);
938 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
939 rev.diffopt.detect_rename = 1;
940 rev.diffopt.file = s->fp;
941 rev.diffopt.close_file = 0;
943 * If we're not going to stdout, then we definitely don't
944 * want color, since we are going to the commit message
945 * file (and even the "auto" setting won't work, since it
946 * will have checked isatty on stdout). But we then do want
947 * to insert the scissor line here to reliably remove the
948 * diff before committing.
950 if (s->fp != stdout) {
951 rev.diffopt.use_color = 0;
952 wt_status_add_cut_line(s->fp);
954 if (s->verbose > 1 && s->commitable) {
955 /* print_updated() printed a header, so do we */
956 if (s->fp != stdout)
957 wt_longstatus_print_trailer(s);
958 status_printf_ln(s, c, _("Changes to be committed:"));
959 rev.diffopt.a_prefix = "c/";
960 rev.diffopt.b_prefix = "i/";
961 } /* else use prefix as per user config */
962 run_diff_index(&rev, 1);
963 if (s->verbose > 1 &&
964 wt_status_check_worktree_changes(s, &dirty_submodules)) {
965 status_printf_ln(s, c,
966 "--------------------------------------------------");
967 status_printf_ln(s, c, _("Changes not staged for commit:"));
968 setup_work_tree();
969 rev.diffopt.a_prefix = "i/";
970 rev.diffopt.b_prefix = "w/";
971 run_diff_files(&rev, 0);
975 static void wt_longstatus_print_tracking(struct wt_status *s)
977 struct strbuf sb = STRBUF_INIT;
978 const char *cp, *ep, *branch_name;
979 struct branch *branch;
980 char comment_line_string[3];
981 int i;
983 assert(s->branch && !s->is_initial);
984 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
985 return;
986 branch = branch_get(branch_name);
987 if (!format_tracking_info(branch, &sb))
988 return;
990 i = 0;
991 if (s->display_comment_prefix) {
992 comment_line_string[i++] = comment_line_char;
993 comment_line_string[i++] = ' ';
995 comment_line_string[i] = '\0';
997 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
998 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
999 "%s%.*s", comment_line_string,
1000 (int)(ep - cp), cp);
1001 if (s->display_comment_prefix)
1002 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1003 comment_line_char);
1004 else
1005 fputs("\n", s->fp);
1008 static int has_unmerged(struct wt_status *s)
1010 int i;
1012 for (i = 0; i < s->change.nr; i++) {
1013 struct wt_status_change_data *d;
1014 d = s->change.items[i].util;
1015 if (d->stagemask)
1016 return 1;
1018 return 0;
1021 static void show_merge_in_progress(struct wt_status *s,
1022 struct wt_status_state *state,
1023 const char *color)
1025 if (has_unmerged(s)) {
1026 status_printf_ln(s, color, _("You have unmerged paths."));
1027 if (s->hints) {
1028 status_printf_ln(s, color,
1029 _(" (fix conflicts and run \"git commit\")"));
1030 status_printf_ln(s, color,
1031 _(" (use \"git merge --abort\" to abort the merge)"));
1033 } else {
1034 s-> commitable = 1;
1035 status_printf_ln(s, color,
1036 _("All conflicts fixed but you are still merging."));
1037 if (s->hints)
1038 status_printf_ln(s, color,
1039 _(" (use \"git commit\" to conclude merge)"));
1041 wt_longstatus_print_trailer(s);
1044 static void show_am_in_progress(struct wt_status *s,
1045 struct wt_status_state *state,
1046 const char *color)
1048 status_printf_ln(s, color,
1049 _("You are in the middle of an am session."));
1050 if (state->am_empty_patch)
1051 status_printf_ln(s, color,
1052 _("The current patch is empty."));
1053 if (s->hints) {
1054 if (!state->am_empty_patch)
1055 status_printf_ln(s, color,
1056 _(" (fix conflicts and then run \"git am --continue\")"));
1057 status_printf_ln(s, color,
1058 _(" (use \"git am --skip\" to skip this patch)"));
1059 status_printf_ln(s, color,
1060 _(" (use \"git am --abort\" to restore the original branch)"));
1062 wt_longstatus_print_trailer(s);
1065 static char *read_line_from_git_path(const char *filename)
1067 struct strbuf buf = STRBUF_INIT;
1068 FILE *fp = fopen(git_path("%s", filename), "r");
1069 if (!fp) {
1070 strbuf_release(&buf);
1071 return NULL;
1073 strbuf_getline_lf(&buf, fp);
1074 if (!fclose(fp)) {
1075 return strbuf_detach(&buf, NULL);
1076 } else {
1077 strbuf_release(&buf);
1078 return NULL;
1082 static int split_commit_in_progress(struct wt_status *s)
1084 int split_in_progress = 0;
1085 char *head = read_line_from_git_path("HEAD");
1086 char *orig_head = read_line_from_git_path("ORIG_HEAD");
1087 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
1088 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1090 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
1091 !s->branch || strcmp(s->branch, "HEAD"))
1092 return split_in_progress;
1094 if (!strcmp(rebase_amend, rebase_orig_head)) {
1095 if (strcmp(head, rebase_amend))
1096 split_in_progress = 1;
1097 } else if (strcmp(orig_head, rebase_orig_head)) {
1098 split_in_progress = 1;
1101 if (!s->amend && !s->nowarn && !s->workdir_dirty)
1102 split_in_progress = 0;
1104 free(head);
1105 free(orig_head);
1106 free(rebase_amend);
1107 free(rebase_orig_head);
1108 return split_in_progress;
1112 * Turn
1113 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1114 * into
1115 * "pick d6a2f03 some message"
1117 * The function assumes that the line does not contain useless spaces
1118 * before or after the command.
1120 static void abbrev_sha1_in_line(struct strbuf *line)
1122 struct strbuf **split;
1123 int i;
1125 if (starts_with(line->buf, "exec ") ||
1126 starts_with(line->buf, "x "))
1127 return;
1129 split = strbuf_split_max(line, ' ', 3);
1130 if (split[0] && split[1]) {
1131 struct object_id oid;
1134 * strbuf_split_max left a space. Trim it and re-add
1135 * it after abbreviation.
1137 strbuf_trim(split[1]);
1138 if (!get_oid(split[1]->buf, &oid)) {
1139 strbuf_reset(split[1]);
1140 strbuf_add_unique_abbrev(split[1], oid.hash,
1141 DEFAULT_ABBREV);
1142 strbuf_addch(split[1], ' ');
1143 strbuf_reset(line);
1144 for (i = 0; split[i]; i++)
1145 strbuf_addbuf(line, split[i]);
1148 strbuf_list_free(split);
1151 static int read_rebase_todolist(const char *fname, struct string_list *lines)
1153 struct strbuf line = STRBUF_INIT;
1154 FILE *f = fopen(git_path("%s", fname), "r");
1156 if (!f) {
1157 if (errno == ENOENT)
1158 return -1;
1159 die_errno("Could not open file %s for reading",
1160 git_path("%s", fname));
1162 while (!strbuf_getline_lf(&line, f)) {
1163 if (line.len && line.buf[0] == comment_line_char)
1164 continue;
1165 strbuf_trim(&line);
1166 if (!line.len)
1167 continue;
1168 abbrev_sha1_in_line(&line);
1169 string_list_append(lines, line.buf);
1171 return 0;
1174 static void show_rebase_information(struct wt_status *s,
1175 struct wt_status_state *state,
1176 const char *color)
1178 if (state->rebase_interactive_in_progress) {
1179 int i;
1180 int nr_lines_to_show = 2;
1182 struct string_list have_done = STRING_LIST_INIT_DUP;
1183 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1185 read_rebase_todolist("rebase-merge/done", &have_done);
1186 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1187 &yet_to_do))
1188 status_printf_ln(s, color,
1189 _("git-rebase-todo is missing."));
1190 if (have_done.nr == 0)
1191 status_printf_ln(s, color, _("No commands done."));
1192 else {
1193 status_printf_ln(s, color,
1194 Q_("Last command done (%d command done):",
1195 "Last commands done (%d commands done):",
1196 have_done.nr),
1197 have_done.nr);
1198 for (i = (have_done.nr > nr_lines_to_show)
1199 ? have_done.nr - nr_lines_to_show : 0;
1200 i < have_done.nr;
1201 i++)
1202 status_printf_ln(s, color, " %s", have_done.items[i].string);
1203 if (have_done.nr > nr_lines_to_show && s->hints)
1204 status_printf_ln(s, color,
1205 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1208 if (yet_to_do.nr == 0)
1209 status_printf_ln(s, color,
1210 _("No commands remaining."));
1211 else {
1212 status_printf_ln(s, color,
1213 Q_("Next command to do (%d remaining command):",
1214 "Next commands to do (%d remaining commands):",
1215 yet_to_do.nr),
1216 yet_to_do.nr);
1217 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1218 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1219 if (s->hints)
1220 status_printf_ln(s, color,
1221 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1223 string_list_clear(&yet_to_do, 0);
1224 string_list_clear(&have_done, 0);
1228 static void print_rebase_state(struct wt_status *s,
1229 struct wt_status_state *state,
1230 const char *color)
1232 if (state->branch)
1233 status_printf_ln(s, color,
1234 _("You are currently rebasing branch '%s' on '%s'."),
1235 state->branch,
1236 state->onto);
1237 else
1238 status_printf_ln(s, color,
1239 _("You are currently rebasing."));
1242 static void show_rebase_in_progress(struct wt_status *s,
1243 struct wt_status_state *state,
1244 const char *color)
1246 struct stat st;
1248 show_rebase_information(s, state, color);
1249 if (has_unmerged(s)) {
1250 print_rebase_state(s, state, color);
1251 if (s->hints) {
1252 status_printf_ln(s, color,
1253 _(" (fix conflicts and then run \"git rebase --continue\")"));
1254 status_printf_ln(s, color,
1255 _(" (use \"git rebase --skip\" to skip this patch)"));
1256 status_printf_ln(s, color,
1257 _(" (use \"git rebase --abort\" to check out the original branch)"));
1259 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
1260 print_rebase_state(s, state, color);
1261 if (s->hints)
1262 status_printf_ln(s, color,
1263 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1264 } else if (split_commit_in_progress(s)) {
1265 if (state->branch)
1266 status_printf_ln(s, color,
1267 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1268 state->branch,
1269 state->onto);
1270 else
1271 status_printf_ln(s, color,
1272 _("You are currently splitting a commit during a rebase."));
1273 if (s->hints)
1274 status_printf_ln(s, color,
1275 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1276 } else {
1277 if (state->branch)
1278 status_printf_ln(s, color,
1279 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1280 state->branch,
1281 state->onto);
1282 else
1283 status_printf_ln(s, color,
1284 _("You are currently editing a commit during a rebase."));
1285 if (s->hints && !s->amend) {
1286 status_printf_ln(s, color,
1287 _(" (use \"git commit --amend\" to amend the current commit)"));
1288 status_printf_ln(s, color,
1289 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1292 wt_longstatus_print_trailer(s);
1295 static void show_cherry_pick_in_progress(struct wt_status *s,
1296 struct wt_status_state *state,
1297 const char *color)
1299 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1300 find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
1301 if (s->hints) {
1302 if (has_unmerged(s))
1303 status_printf_ln(s, color,
1304 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1305 else
1306 status_printf_ln(s, color,
1307 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1308 status_printf_ln(s, color,
1309 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1311 wt_longstatus_print_trailer(s);
1314 static void show_revert_in_progress(struct wt_status *s,
1315 struct wt_status_state *state,
1316 const char *color)
1318 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1319 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
1320 if (s->hints) {
1321 if (has_unmerged(s))
1322 status_printf_ln(s, color,
1323 _(" (fix conflicts and run \"git revert --continue\")"));
1324 else
1325 status_printf_ln(s, color,
1326 _(" (all conflicts fixed: run \"git revert --continue\")"));
1327 status_printf_ln(s, color,
1328 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1330 wt_longstatus_print_trailer(s);
1333 static void show_bisect_in_progress(struct wt_status *s,
1334 struct wt_status_state *state,
1335 const char *color)
1337 if (state->branch)
1338 status_printf_ln(s, color,
1339 _("You are currently bisecting, started from branch '%s'."),
1340 state->branch);
1341 else
1342 status_printf_ln(s, color,
1343 _("You are currently bisecting."));
1344 if (s->hints)
1345 status_printf_ln(s, color,
1346 _(" (use \"git bisect reset\" to get back to the original branch)"));
1347 wt_longstatus_print_trailer(s);
1351 * Extract branch information from rebase/bisect
1353 static char *get_branch(const struct worktree *wt, const char *path)
1355 struct strbuf sb = STRBUF_INIT;
1356 struct object_id oid;
1357 const char *branch_name;
1359 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1360 goto got_nothing;
1362 while (sb.len && sb.buf[sb.len - 1] == '\n')
1363 strbuf_setlen(&sb, sb.len - 1);
1364 if (!sb.len)
1365 goto got_nothing;
1366 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1367 strbuf_remove(&sb, 0, branch_name - sb.buf);
1368 else if (starts_with(sb.buf, "refs/"))
1370 else if (!get_oid_hex(sb.buf, &oid)) {
1371 strbuf_reset(&sb);
1372 strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
1373 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1374 goto got_nothing;
1375 else /* bisect */
1377 return strbuf_detach(&sb, NULL);
1379 got_nothing:
1380 strbuf_release(&sb);
1381 return NULL;
1384 struct grab_1st_switch_cbdata {
1385 struct strbuf buf;
1386 struct object_id noid;
1389 static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
1390 const char *email, timestamp_t timestamp, int tz,
1391 const char *message, void *cb_data)
1393 struct grab_1st_switch_cbdata *cb = cb_data;
1394 const char *target = NULL, *end;
1396 if (!skip_prefix(message, "checkout: moving from ", &message))
1397 return 0;
1398 target = strstr(message, " to ");
1399 if (!target)
1400 return 0;
1401 target += strlen(" to ");
1402 strbuf_reset(&cb->buf);
1403 oidcpy(&cb->noid, noid);
1404 end = strchrnul(target, '\n');
1405 strbuf_add(&cb->buf, target, end - target);
1406 if (!strcmp(cb->buf.buf, "HEAD")) {
1407 /* HEAD is relative. Resolve it to the right reflog entry. */
1408 strbuf_reset(&cb->buf);
1409 strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
1411 return 1;
1414 static void wt_status_get_detached_from(struct wt_status_state *state)
1416 struct grab_1st_switch_cbdata cb;
1417 struct commit *commit;
1418 struct object_id oid;
1419 char *ref = NULL;
1421 strbuf_init(&cb.buf, 0);
1422 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1423 strbuf_release(&cb.buf);
1424 return;
1427 if (dwim_ref(cb.buf.buf, cb.buf.len, oid.hash, &ref) == 1 &&
1428 /* sha1 is a commit? match without further lookup */
1429 (!oidcmp(&cb.noid, &oid) ||
1430 /* perhaps sha1 is a tag, try to dereference to a commit */
1431 ((commit = lookup_commit_reference_gently(oid.hash, 1)) != NULL &&
1432 !oidcmp(&cb.noid, &commit->object.oid)))) {
1433 const char *from = ref;
1434 if (!skip_prefix(from, "refs/tags/", &from))
1435 skip_prefix(from, "refs/remotes/", &from);
1436 state->detached_from = xstrdup(from);
1437 } else
1438 state->detached_from =
1439 xstrdup(find_unique_abbrev(cb.noid.hash, DEFAULT_ABBREV));
1440 hashcpy(state->detached_sha1, cb.noid.hash);
1441 state->detached_at = !get_oid("HEAD", &oid) &&
1442 !hashcmp(oid.hash, state->detached_sha1);
1444 free(ref);
1445 strbuf_release(&cb.buf);
1448 int wt_status_check_rebase(const struct worktree *wt,
1449 struct wt_status_state *state)
1451 struct stat st;
1453 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1454 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1455 state->am_in_progress = 1;
1456 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1457 state->am_empty_patch = 1;
1458 } else {
1459 state->rebase_in_progress = 1;
1460 state->branch = get_branch(wt, "rebase-apply/head-name");
1461 state->onto = get_branch(wt, "rebase-apply/onto");
1463 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1464 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1465 state->rebase_interactive_in_progress = 1;
1466 else
1467 state->rebase_in_progress = 1;
1468 state->branch = get_branch(wt, "rebase-merge/head-name");
1469 state->onto = get_branch(wt, "rebase-merge/onto");
1470 } else
1471 return 0;
1472 return 1;
1475 int wt_status_check_bisect(const struct worktree *wt,
1476 struct wt_status_state *state)
1478 struct stat st;
1480 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1481 state->bisect_in_progress = 1;
1482 state->branch = get_branch(wt, "BISECT_START");
1483 return 1;
1485 return 0;
1488 void wt_status_get_state(struct wt_status_state *state,
1489 int get_detached_from)
1491 struct stat st;
1492 struct object_id oid;
1494 if (!stat(git_path_merge_head(), &st)) {
1495 state->merge_in_progress = 1;
1496 } else if (wt_status_check_rebase(NULL, state)) {
1497 ; /* all set */
1498 } else if (!stat(git_path_cherry_pick_head(), &st) &&
1499 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1500 state->cherry_pick_in_progress = 1;
1501 hashcpy(state->cherry_pick_head_sha1, oid.hash);
1503 wt_status_check_bisect(NULL, state);
1504 if (!stat(git_path_revert_head(), &st) &&
1505 !get_oid("REVERT_HEAD", &oid)) {
1506 state->revert_in_progress = 1;
1507 hashcpy(state->revert_head_sha1, oid.hash);
1510 if (get_detached_from)
1511 wt_status_get_detached_from(state);
1514 static void wt_longstatus_print_state(struct wt_status *s,
1515 struct wt_status_state *state)
1517 const char *state_color = color(WT_STATUS_HEADER, s);
1518 if (state->merge_in_progress)
1519 show_merge_in_progress(s, state, state_color);
1520 else if (state->am_in_progress)
1521 show_am_in_progress(s, state, state_color);
1522 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1523 show_rebase_in_progress(s, state, state_color);
1524 else if (state->cherry_pick_in_progress)
1525 show_cherry_pick_in_progress(s, state, state_color);
1526 else if (state->revert_in_progress)
1527 show_revert_in_progress(s, state, state_color);
1528 if (state->bisect_in_progress)
1529 show_bisect_in_progress(s, state, state_color);
1532 static void wt_longstatus_print(struct wt_status *s)
1534 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1535 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1536 struct wt_status_state state;
1538 memset(&state, 0, sizeof(state));
1539 wt_status_get_state(&state,
1540 s->branch && !strcmp(s->branch, "HEAD"));
1542 if (s->branch) {
1543 const char *on_what = _("On branch ");
1544 const char *branch_name = s->branch;
1545 if (!strcmp(branch_name, "HEAD")) {
1546 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1547 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1548 if (state.rebase_interactive_in_progress)
1549 on_what = _("interactive rebase in progress; onto ");
1550 else
1551 on_what = _("rebase in progress; onto ");
1552 branch_name = state.onto;
1553 } else if (state.detached_from) {
1554 branch_name = state.detached_from;
1555 if (state.detached_at)
1556 on_what = _("HEAD detached at ");
1557 else
1558 on_what = _("HEAD detached from ");
1559 } else {
1560 branch_name = "";
1561 on_what = _("Not currently on any branch.");
1563 } else
1564 skip_prefix(branch_name, "refs/heads/", &branch_name);
1565 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1566 status_printf_more(s, branch_status_color, "%s", on_what);
1567 status_printf_more(s, branch_color, "%s\n", branch_name);
1568 if (!s->is_initial)
1569 wt_longstatus_print_tracking(s);
1572 wt_longstatus_print_state(s, &state);
1573 free(state.branch);
1574 free(state.onto);
1575 free(state.detached_from);
1577 if (s->is_initial) {
1578 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1579 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1580 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1583 wt_longstatus_print_updated(s);
1584 wt_longstatus_print_unmerged(s);
1585 wt_longstatus_print_changed(s);
1586 if (s->submodule_summary &&
1587 (!s->ignore_submodule_arg ||
1588 strcmp(s->ignore_submodule_arg, "all"))) {
1589 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1590 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1592 if (s->show_untracked_files) {
1593 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1594 if (s->show_ignored_files)
1595 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1596 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1597 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1598 status_printf_ln(s, GIT_COLOR_NORMAL,
1599 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1600 "may speed it up, but you have to be careful not to forget to add\n"
1601 "new files yourself (see 'git help status')."),
1602 s->untracked_in_ms / 1000.0);
1604 } else if (s->commitable)
1605 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1606 s->hints
1607 ? _(" (use -u option to show untracked files)") : "");
1609 if (s->verbose)
1610 wt_longstatus_print_verbose(s);
1611 if (!s->commitable) {
1612 if (s->amend)
1613 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1614 else if (s->nowarn)
1615 ; /* nothing */
1616 else if (s->workdir_dirty) {
1617 if (s->hints)
1618 printf(_("no changes added to commit "
1619 "(use \"git add\" and/or \"git commit -a\")\n"));
1620 else
1621 printf(_("no changes added to commit\n"));
1622 } else if (s->untracked.nr) {
1623 if (s->hints)
1624 printf(_("nothing added to commit but untracked files "
1625 "present (use \"git add\" to track)\n"));
1626 else
1627 printf(_("nothing added to commit but untracked files present\n"));
1628 } else if (s->is_initial) {
1629 if (s->hints)
1630 printf(_("nothing to commit (create/copy files "
1631 "and use \"git add\" to track)\n"));
1632 else
1633 printf(_("nothing to commit\n"));
1634 } else if (!s->show_untracked_files) {
1635 if (s->hints)
1636 printf(_("nothing to commit (use -u to show untracked files)\n"));
1637 else
1638 printf(_("nothing to commit\n"));
1639 } else
1640 printf(_("nothing to commit, working tree clean\n"));
1644 static void wt_shortstatus_unmerged(struct string_list_item *it,
1645 struct wt_status *s)
1647 struct wt_status_change_data *d = it->util;
1648 const char *how = "??";
1650 switch (d->stagemask) {
1651 case 1: how = "DD"; break; /* both deleted */
1652 case 2: how = "AU"; break; /* added by us */
1653 case 3: how = "UD"; break; /* deleted by them */
1654 case 4: how = "UA"; break; /* added by them */
1655 case 5: how = "DU"; break; /* deleted by us */
1656 case 6: how = "AA"; break; /* both added */
1657 case 7: how = "UU"; break; /* both modified */
1659 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1660 if (s->null_termination) {
1661 fprintf(stdout, " %s%c", it->string, 0);
1662 } else {
1663 struct strbuf onebuf = STRBUF_INIT;
1664 const char *one;
1665 one = quote_path(it->string, s->prefix, &onebuf);
1666 printf(" %s\n", one);
1667 strbuf_release(&onebuf);
1671 static void wt_shortstatus_status(struct string_list_item *it,
1672 struct wt_status *s)
1674 struct wt_status_change_data *d = it->util;
1676 if (d->index_status)
1677 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1678 else
1679 putchar(' ');
1680 if (d->worktree_status)
1681 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1682 else
1683 putchar(' ');
1684 putchar(' ');
1685 if (s->null_termination) {
1686 fprintf(stdout, "%s%c", it->string, 0);
1687 if (d->head_path)
1688 fprintf(stdout, "%s%c", d->head_path, 0);
1689 } else {
1690 struct strbuf onebuf = STRBUF_INIT;
1691 const char *one;
1692 if (d->head_path) {
1693 one = quote_path(d->head_path, s->prefix, &onebuf);
1694 if (*one != '"' && strchr(one, ' ') != NULL) {
1695 putchar('"');
1696 strbuf_addch(&onebuf, '"');
1697 one = onebuf.buf;
1699 printf("%s -> ", one);
1700 strbuf_release(&onebuf);
1702 one = quote_path(it->string, s->prefix, &onebuf);
1703 if (*one != '"' && strchr(one, ' ') != NULL) {
1704 putchar('"');
1705 strbuf_addch(&onebuf, '"');
1706 one = onebuf.buf;
1708 printf("%s\n", one);
1709 strbuf_release(&onebuf);
1713 static void wt_shortstatus_other(struct string_list_item *it,
1714 struct wt_status *s, const char *sign)
1716 if (s->null_termination) {
1717 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1718 } else {
1719 struct strbuf onebuf = STRBUF_INIT;
1720 const char *one;
1721 one = quote_path(it->string, s->prefix, &onebuf);
1722 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1723 printf(" %s\n", one);
1724 strbuf_release(&onebuf);
1728 static void wt_shortstatus_print_tracking(struct wt_status *s)
1730 struct branch *branch;
1731 const char *header_color = color(WT_STATUS_HEADER, s);
1732 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1733 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1735 const char *base;
1736 const char *branch_name;
1737 int num_ours, num_theirs;
1738 int upstream_is_gone = 0;
1740 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1742 if (!s->branch)
1743 return;
1744 branch_name = s->branch;
1746 #define LABEL(string) (s->no_gettext ? (string) : _(string))
1748 if (s->is_initial)
1749 color_fprintf(s->fp, header_color, LABEL(N_("Initial commit on ")));
1751 if (!strcmp(s->branch, "HEAD")) {
1752 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1753 LABEL(N_("HEAD (no branch)")));
1754 goto conclude;
1757 skip_prefix(branch_name, "refs/heads/", &branch_name);
1759 branch = branch_get(branch_name);
1761 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1763 if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
1764 if (!base)
1765 goto conclude;
1767 upstream_is_gone = 1;
1770 base = shorten_unambiguous_ref(base, 0);
1771 color_fprintf(s->fp, header_color, "...");
1772 color_fprintf(s->fp, branch_color_remote, "%s", base);
1773 free((char *)base);
1775 if (!upstream_is_gone && !num_ours && !num_theirs)
1776 goto conclude;
1778 color_fprintf(s->fp, header_color, " [");
1779 if (upstream_is_gone) {
1780 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
1781 } else if (!num_ours) {
1782 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
1783 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1784 } else if (!num_theirs) {
1785 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1786 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1787 } else {
1788 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1789 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1790 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
1791 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1794 color_fprintf(s->fp, header_color, "]");
1795 conclude:
1796 fputc(s->null_termination ? '\0' : '\n', s->fp);
1799 static void wt_shortstatus_print(struct wt_status *s)
1801 struct string_list_item *it;
1803 if (s->show_branch)
1804 wt_shortstatus_print_tracking(s);
1806 for_each_string_list_item(it, &s->change) {
1807 struct wt_status_change_data *d = it->util;
1809 if (d->stagemask)
1810 wt_shortstatus_unmerged(it, s);
1811 else
1812 wt_shortstatus_status(it, s);
1814 for_each_string_list_item(it, &s->untracked)
1815 wt_shortstatus_other(it, s, "??");
1817 for_each_string_list_item(it, &s->ignored)
1818 wt_shortstatus_other(it, s, "!!");
1821 static void wt_porcelain_print(struct wt_status *s)
1823 s->use_color = 0;
1824 s->relative_paths = 0;
1825 s->prefix = NULL;
1826 s->no_gettext = 1;
1827 wt_shortstatus_print(s);
1831 * Print branch information for porcelain v2 output. These lines
1832 * are printed when the '--branch' parameter is given.
1834 * # branch.oid <commit><eol>
1835 * # branch.head <head><eol>
1836 * [# branch.upstream <upstream><eol>
1837 * [# branch.ab +<ahead> -<behind><eol>]]
1839 * <commit> ::= the current commit hash or the the literal
1840 * "(initial)" to indicate an initialized repo
1841 * with no commits.
1843 * <head> ::= <branch_name> the current branch name or
1844 * "(detached)" literal when detached head or
1845 * "(unknown)" when something is wrong.
1847 * <upstream> ::= the upstream branch name, when set.
1849 * <ahead> ::= integer ahead value, when upstream set
1850 * and the commit is present (not gone).
1852 * <behind> ::= integer behind value, when upstream set
1853 * and commit is present.
1856 * The end-of-line is defined by the -z flag.
1858 * <eol> ::= NUL when -z,
1859 * LF when NOT -z.
1862 static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1864 struct branch *branch;
1865 const char *base;
1866 const char *branch_name;
1867 struct wt_status_state state;
1868 int ab_info, nr_ahead, nr_behind;
1869 char eol = s->null_termination ? '\0' : '\n';
1871 memset(&state, 0, sizeof(state));
1872 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1874 fprintf(s->fp, "# branch.oid %s%c",
1875 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1876 eol);
1878 if (!s->branch)
1879 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1880 else {
1881 if (!strcmp(s->branch, "HEAD")) {
1882 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1884 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1885 branch_name = state.onto;
1886 else if (state.detached_from)
1887 branch_name = state.detached_from;
1888 else
1889 branch_name = "";
1890 } else {
1891 branch_name = NULL;
1892 skip_prefix(s->branch, "refs/heads/", &branch_name);
1894 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1897 /* Lookup stats on the upstream tracking branch, if set. */
1898 branch = branch_get(branch_name);
1899 base = NULL;
1900 ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0);
1901 if (base) {
1902 base = shorten_unambiguous_ref(base, 0);
1903 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1904 free((char *)base);
1906 if (ab_info)
1907 fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
1911 free(state.branch);
1912 free(state.onto);
1913 free(state.detached_from);
1917 * Convert various submodule status values into a
1918 * fixed-length string of characters in the buffer provided.
1920 static void wt_porcelain_v2_submodule_state(
1921 struct wt_status_change_data *d,
1922 char sub[5])
1924 if (S_ISGITLINK(d->mode_head) ||
1925 S_ISGITLINK(d->mode_index) ||
1926 S_ISGITLINK(d->mode_worktree)) {
1927 sub[0] = 'S';
1928 sub[1] = d->new_submodule_commits ? 'C' : '.';
1929 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
1930 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
1931 } else {
1932 sub[0] = 'N';
1933 sub[1] = '.';
1934 sub[2] = '.';
1935 sub[3] = '.';
1937 sub[4] = 0;
1941 * Fix-up changed entries before we print them.
1943 static void wt_porcelain_v2_fix_up_changed(
1944 struct string_list_item *it,
1945 struct wt_status *s)
1947 struct wt_status_change_data *d = it->util;
1949 if (!d->index_status) {
1951 * This entry is unchanged in the index (relative to the head).
1952 * Therefore, the collect_updated_cb was never called for this
1953 * entry (during the head-vs-index scan) and so the head column
1954 * fields were never set.
1956 * We must have data for the index column (from the
1957 * index-vs-worktree scan (otherwise, this entry should not be
1958 * in the list of changes)).
1960 * Copy index column fields to the head column, so that our
1961 * output looks complete.
1963 assert(d->mode_head == 0);
1964 d->mode_head = d->mode_index;
1965 oidcpy(&d->oid_head, &d->oid_index);
1968 if (!d->worktree_status) {
1970 * This entry is unchanged in the worktree (relative to the index).
1971 * Therefore, the collect_changed_cb was never called for this entry
1972 * (during the index-vs-worktree scan) and so the worktree column
1973 * fields were never set.
1975 * We must have data for the index column (from the head-vs-index
1976 * scan).
1978 * Copy the index column fields to the worktree column so that
1979 * our output looks complete.
1981 * Note that we only have a mode field in the worktree column
1982 * because the scan code tries really hard to not have to compute it.
1984 assert(d->mode_worktree == 0);
1985 d->mode_worktree = d->mode_index;
1990 * Print porcelain v2 info for tracked entries with changes.
1992 static void wt_porcelain_v2_print_changed_entry(
1993 struct string_list_item *it,
1994 struct wt_status *s)
1996 struct wt_status_change_data *d = it->util;
1997 struct strbuf buf_index = STRBUF_INIT;
1998 struct strbuf buf_head = STRBUF_INIT;
1999 const char *path_index = NULL;
2000 const char *path_head = NULL;
2001 char key[3];
2002 char submodule_token[5];
2003 char sep_char, eol_char;
2005 wt_porcelain_v2_fix_up_changed(it, s);
2006 wt_porcelain_v2_submodule_state(d, submodule_token);
2008 key[0] = d->index_status ? d->index_status : '.';
2009 key[1] = d->worktree_status ? d->worktree_status : '.';
2010 key[2] = 0;
2012 if (s->null_termination) {
2014 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2015 * A single NUL character separates them.
2017 sep_char = '\0';
2018 eol_char = '\0';
2019 path_index = it->string;
2020 path_head = d->head_path;
2021 } else {
2023 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2024 * The source path is only present when necessary.
2025 * A single TAB separates them (because paths can contain spaces
2026 * which are not escaped and C-quoting does escape TAB characters).
2028 sep_char = '\t';
2029 eol_char = '\n';
2030 path_index = quote_path(it->string, s->prefix, &buf_index);
2031 if (d->head_path)
2032 path_head = quote_path(d->head_path, s->prefix, &buf_head);
2035 if (path_head)
2036 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2037 key, submodule_token,
2038 d->mode_head, d->mode_index, d->mode_worktree,
2039 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2040 key[0], d->score,
2041 path_index, sep_char, path_head, eol_char);
2042 else
2043 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2044 key, submodule_token,
2045 d->mode_head, d->mode_index, d->mode_worktree,
2046 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2047 path_index, eol_char);
2049 strbuf_release(&buf_index);
2050 strbuf_release(&buf_head);
2054 * Print porcelain v2 status info for unmerged entries.
2056 static void wt_porcelain_v2_print_unmerged_entry(
2057 struct string_list_item *it,
2058 struct wt_status *s)
2060 struct wt_status_change_data *d = it->util;
2061 const struct cache_entry *ce;
2062 struct strbuf buf_index = STRBUF_INIT;
2063 const char *path_index = NULL;
2064 int pos, stage, sum;
2065 struct {
2066 int mode;
2067 struct object_id oid;
2068 } stages[3];
2069 char *key;
2070 char submodule_token[5];
2071 char unmerged_prefix = 'u';
2072 char eol_char = s->null_termination ? '\0' : '\n';
2074 wt_porcelain_v2_submodule_state(d, submodule_token);
2076 switch (d->stagemask) {
2077 case 1: key = "DD"; break; /* both deleted */
2078 case 2: key = "AU"; break; /* added by us */
2079 case 3: key = "UD"; break; /* deleted by them */
2080 case 4: key = "UA"; break; /* added by them */
2081 case 5: key = "DU"; break; /* deleted by us */
2082 case 6: key = "AA"; break; /* both added */
2083 case 7: key = "UU"; break; /* both modified */
2084 default:
2085 die("BUG: unhandled unmerged status %x", d->stagemask);
2089 * Disregard d.aux.porcelain_v2 data that we accumulated
2090 * for the head and index columns during the scans and
2091 * replace with the actual stage data.
2093 * Note that this is a last-one-wins for each the individual
2094 * stage [123] columns in the event of multiple cache entries
2095 * for same stage.
2097 memset(stages, 0, sizeof(stages));
2098 sum = 0;
2099 pos = cache_name_pos(it->string, strlen(it->string));
2100 assert(pos < 0);
2101 pos = -pos-1;
2102 while (pos < active_nr) {
2103 ce = active_cache[pos++];
2104 stage = ce_stage(ce);
2105 if (strcmp(ce->name, it->string) || !stage)
2106 break;
2107 stages[stage - 1].mode = ce->ce_mode;
2108 oidcpy(&stages[stage - 1].oid, &ce->oid);
2109 sum |= (1 << (stage - 1));
2111 if (sum != d->stagemask)
2112 die("BUG: observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2114 if (s->null_termination)
2115 path_index = it->string;
2116 else
2117 path_index = quote_path(it->string, s->prefix, &buf_index);
2119 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2120 unmerged_prefix, key, submodule_token,
2121 stages[0].mode, /* stage 1 */
2122 stages[1].mode, /* stage 2 */
2123 stages[2].mode, /* stage 3 */
2124 d->mode_worktree,
2125 oid_to_hex(&stages[0].oid), /* stage 1 */
2126 oid_to_hex(&stages[1].oid), /* stage 2 */
2127 oid_to_hex(&stages[2].oid), /* stage 3 */
2128 path_index,
2129 eol_char);
2131 strbuf_release(&buf_index);
2135 * Print porcelain V2 status info for untracked and ignored entries.
2137 static void wt_porcelain_v2_print_other(
2138 struct string_list_item *it,
2139 struct wt_status *s,
2140 char prefix)
2142 struct strbuf buf = STRBUF_INIT;
2143 const char *path;
2144 char eol_char;
2146 if (s->null_termination) {
2147 path = it->string;
2148 eol_char = '\0';
2149 } else {
2150 path = quote_path(it->string, s->prefix, &buf);
2151 eol_char = '\n';
2154 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2156 strbuf_release(&buf);
2160 * Print porcelain V2 status.
2162 * [<v2_branch>]
2163 * [<v2_changed_items>]*
2164 * [<v2_unmerged_items>]*
2165 * [<v2_untracked_items>]*
2166 * [<v2_ignored_items>]*
2169 static void wt_porcelain_v2_print(struct wt_status *s)
2171 struct wt_status_change_data *d;
2172 struct string_list_item *it;
2173 int i;
2175 if (s->show_branch)
2176 wt_porcelain_v2_print_tracking(s);
2178 for (i = 0; i < s->change.nr; i++) {
2179 it = &(s->change.items[i]);
2180 d = it->util;
2181 if (!d->stagemask)
2182 wt_porcelain_v2_print_changed_entry(it, s);
2185 for (i = 0; i < s->change.nr; i++) {
2186 it = &(s->change.items[i]);
2187 d = it->util;
2188 if (d->stagemask)
2189 wt_porcelain_v2_print_unmerged_entry(it, s);
2192 for (i = 0; i < s->untracked.nr; i++) {
2193 it = &(s->untracked.items[i]);
2194 wt_porcelain_v2_print_other(it, s, '?');
2197 for (i = 0; i < s->ignored.nr; i++) {
2198 it = &(s->ignored.items[i]);
2199 wt_porcelain_v2_print_other(it, s, '!');
2203 void wt_status_print(struct wt_status *s)
2205 switch (s->status_format) {
2206 case STATUS_FORMAT_SHORT:
2207 wt_shortstatus_print(s);
2208 break;
2209 case STATUS_FORMAT_PORCELAIN:
2210 wt_porcelain_print(s);
2211 break;
2212 case STATUS_FORMAT_PORCELAIN_V2:
2213 wt_porcelain_v2_print(s);
2214 break;
2215 case STATUS_FORMAT_UNSPECIFIED:
2216 die("BUG: finalize_deferred_config() should have been called");
2217 break;
2218 case STATUS_FORMAT_NONE:
2219 case STATUS_FORMAT_LONG:
2220 wt_longstatus_print(s);
2221 break;
2226 * Returns 1 if there are unstaged changes, 0 otherwise.
2228 int has_unstaged_changes(int ignore_submodules)
2230 struct rev_info rev_info;
2231 int result;
2233 init_revisions(&rev_info, NULL);
2234 if (ignore_submodules)
2235 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
2236 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2237 diff_setup_done(&rev_info.diffopt);
2238 result = run_diff_files(&rev_info, 0);
2239 return diff_result_code(&rev_info.diffopt, result);
2243 * Returns 1 if there are uncommitted changes, 0 otherwise.
2245 int has_uncommitted_changes(int ignore_submodules)
2247 struct rev_info rev_info;
2248 int result;
2250 if (is_cache_unborn())
2251 return 0;
2253 init_revisions(&rev_info, NULL);
2254 if (ignore_submodules)
2255 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
2256 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2257 add_head_to_pending(&rev_info);
2258 diff_setup_done(&rev_info.diffopt);
2259 result = run_diff_index(&rev_info, 1);
2260 return diff_result_code(&rev_info.diffopt, result);
2264 * If the work tree has unstaged or uncommitted changes, dies with the
2265 * appropriate message.
2267 int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2269 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
2270 int err = 0, fd;
2272 fd = hold_locked_index(lock_file, 0);
2273 refresh_cache(REFRESH_QUIET);
2274 if (0 <= fd)
2275 update_index_if_able(&the_index, lock_file);
2276 rollback_lock_file(lock_file);
2278 if (has_unstaged_changes(ignore_submodules)) {
2279 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2280 error(_("cannot %s: You have unstaged changes."), _(action));
2281 err = 1;
2284 if (has_uncommitted_changes(ignore_submodules)) {
2285 if (err)
2286 error(_("additionally, your index contains uncommitted changes."));
2287 else
2288 error(_("cannot %s: Your index contains uncommitted changes."),
2289 _(action));
2290 err = 1;
2293 if (err) {
2294 if (hint)
2295 error("%s", hint);
2296 if (!gently)
2297 exit(128);
2300 return err;