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