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