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