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