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