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