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