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