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