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