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