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