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