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