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