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