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