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