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