submodule summary: ignore --for-status option
[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"
c91f0d92 17
23900a96 18static char default_wt_status_colors[][COLOR_MAXLEN] = {
dc6ebd4c
AL
19 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
20 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
21 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
22 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
23 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
4d4d5726 24 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
05a59a08
DKF
25 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
26 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
148135fc 27 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
c91f0d92 28};
4d229653 29
d249b098 30static const char *color(int slot, struct wt_status *s)
c91f0d92 31{
daa0c3d9
JK
32 const char *c = "";
33 if (want_color(s->use_color))
34 c = s->color_palette[slot];
148135fc
JK
35 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
36 c = s->color_palette[WT_STATUS_HEADER];
37 return c;
c91f0d92
JK
38}
39
becbdae8
JN
40static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
41 const char *fmt, va_list ap, const char *trail)
42{
43 struct strbuf sb = STRBUF_INIT;
44 struct strbuf linebuf = STRBUF_INIT;
45 const char *line, *eol;
46
47 strbuf_vaddf(&sb, fmt, ap);
48 if (!sb.len) {
eff80a9f 49 strbuf_addch(&sb, comment_line_char);
becbdae8
JN
50 if (!trail)
51 strbuf_addch(&sb, ' ');
52 color_print_strbuf(s->fp, color, &sb);
53 if (trail)
54 fprintf(s->fp, "%s", trail);
55 strbuf_release(&sb);
56 return;
57 }
58 for (line = sb.buf; *line; line = eol + 1) {
59 eol = strchr(line, '\n');
60
61 strbuf_reset(&linebuf);
62 if (at_bol) {
eff80a9f 63 strbuf_addch(&linebuf, comment_line_char);
becbdae8
JN
64 if (*line != '\n' && *line != '\t')
65 strbuf_addch(&linebuf, ' ');
66 }
67 if (eol)
68 strbuf_add(&linebuf, line, eol - line);
69 else
70 strbuf_addstr(&linebuf, line);
71 color_print_strbuf(s->fp, color, &linebuf);
72 if (eol)
73 fprintf(s->fp, "\n");
74 else
75 break;
76 at_bol = 1;
77 }
78 if (trail)
79 fprintf(s->fp, "%s", trail);
80 strbuf_release(&linebuf);
81 strbuf_release(&sb);
82}
83
84void status_printf_ln(struct wt_status *s, const char *color,
85 const char *fmt, ...)
86{
87 va_list ap;
88
89 va_start(ap, fmt);
90 status_vprintf(s, 1, color, fmt, ap, "\n");
91 va_end(ap);
92}
93
94void status_printf(struct wt_status *s, const char *color,
95 const char *fmt, ...)
96{
97 va_list ap;
98
99 va_start(ap, fmt);
100 status_vprintf(s, 1, color, fmt, ap, NULL);
101 va_end(ap);
102}
103
1e24845c
JH
104static void status_printf_more(struct wt_status *s, const char *color,
105 const char *fmt, ...)
becbdae8
JN
106{
107 va_list ap;
108
109 va_start(ap, fmt);
110 status_vprintf(s, 0, color, fmt, ap, NULL);
111 va_end(ap);
112}
113
c91f0d92
JK
114void wt_status_prepare(struct wt_status *s)
115{
116 unsigned char sha1[20];
c91f0d92 117
cc46a743 118 memset(s, 0, sizeof(*s));
23900a96
JH
119 memcpy(s->color_palette, default_wt_status_colors,
120 sizeof(default_wt_status_colors));
d249b098
JH
121 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
122 s->use_color = -1;
123 s->relative_paths = 1;
96ec7b1e 124 s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
c91f0d92 125 s->reference = "HEAD";
f26a0012 126 s->fp = stdout;
0f729f21 127 s->index_file = get_index_file();
50b7e70f 128 s->change.strdup_strings = 1;
76378683 129 s->untracked.strdup_strings = 1;
6cb3f6b2 130 s->ignored.strdup_strings = 1;
84b4202d 131 s->show_branch = -1; /* unspecified */
c91f0d92
JK
132}
133
4d4d5726
JH
134static void wt_status_print_unmerged_header(struct wt_status *s)
135{
96b0ec1a
LK
136 int i;
137 int del_mod_conflict = 0;
138 int both_deleted = 0;
139 int not_deleted = 0;
d249b098 140 const char *c = color(WT_STATUS_HEADER, s);
3c588453 141
355ec7a1 142 status_printf_ln(s, c, _("Unmerged paths:"));
96b0ec1a
LK
143
144 for (i = 0; i < s->change.nr; i++) {
145 struct string_list_item *it = &(s->change.items[i]);
146 struct wt_status_change_data *d = it->util;
147
148 switch (d->stagemask) {
149 case 0:
150 break;
151 case 1:
152 both_deleted = 1;
153 break;
154 case 3:
155 case 5:
156 del_mod_conflict = 1;
157 break;
158 default:
159 not_deleted = 1;
160 break;
161 }
162 }
163
edf563fb
JK
164 if (!advice_status_hints)
165 return;
37f7a857 166 if (s->whence != FROM_COMMIT)
3c588453
JH
167 ;
168 else if (!s->is_initial)
355ec7a1 169 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
4d4d5726 170 else
355ec7a1 171 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
96b0ec1a
LK
172
173 if (!both_deleted) {
174 if (!del_mod_conflict)
175 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
176 else
177 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
178 } else if (!del_mod_conflict && !not_deleted) {
179 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
180 } else {
181 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
182 }
b926c0d1 183 status_printf_ln(s, c, "");
4d4d5726
JH
184}
185
f26a0012 186static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb 187{
d249b098 188 const char *c = color(WT_STATUS_HEADER, s);
3c588453 189
919a4ce0 190 status_printf_ln(s, c, _("Changes to be committed:"));
edf563fb
JK
191 if (!advice_status_hints)
192 return;
37f7a857 193 if (s->whence != FROM_COMMIT)
3c588453
JH
194 ; /* NEEDSWORK: use "git reset --unresolve"??? */
195 else if (!s->is_initial)
355ec7a1 196 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
3c588453 197 else
355ec7a1 198 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
b926c0d1 199 status_printf_ln(s, c, "");
3c1eb9cb
JR
200}
201
bb914b14 202static void wt_status_print_dirty_header(struct wt_status *s,
9297f77e
JL
203 int has_deleted,
204 int has_dirty_submodules)
c91f0d92 205{
d249b098 206 const char *c = color(WT_STATUS_HEADER, s);
3c588453 207
355ec7a1 208 status_printf_ln(s, c, _("Changes not staged for commit:"));
edf563fb
JK
209 if (!advice_status_hints)
210 return;
bb914b14 211 if (!has_deleted)
355ec7a1 212 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
bb914b14 213 else
355ec7a1
ÆAB
214 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
215 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
9297f77e 216 if (has_dirty_submodules)
355ec7a1 217 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
b926c0d1 218 status_printf_ln(s, c, "");
bb914b14
AM
219}
220
1b908b6f
JH
221static void wt_status_print_other_header(struct wt_status *s,
222 const char *what,
223 const char *how)
bb914b14 224{
d249b098 225 const char *c = color(WT_STATUS_HEADER, s);
50bd8b7e 226 status_printf_ln(s, c, "%s:", what);
edf563fb
JK
227 if (!advice_status_hints)
228 return;
355ec7a1 229 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
b926c0d1 230 status_printf_ln(s, c, "");
c91f0d92
JK
231}
232
f26a0012 233static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 234{
b926c0d1 235 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
c91f0d92
JK
236}
237
a734d0b1 238#define quote_path quote_path_relative
3a946802 239
4d4d5726
JH
240static void wt_status_print_unmerged_data(struct wt_status *s,
241 struct string_list_item *it)
242{
d249b098 243 const char *c = color(WT_STATUS_UNMERGED, s);
4d4d5726
JH
244 struct wt_status_change_data *d = it->util;
245 struct strbuf onebuf = STRBUF_INIT;
355ec7a1 246 const char *one, *how = _("bug");
4d4d5726 247
39598f99 248 one = quote_path(it->string, s->prefix, &onebuf);
b926c0d1 249 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
4d4d5726 250 switch (d->stagemask) {
355ec7a1
ÆAB
251 case 1: how = _("both deleted:"); break;
252 case 2: how = _("added by us:"); break;
253 case 3: how = _("deleted by them:"); break;
254 case 4: how = _("added by them:"); break;
255 case 5: how = _("deleted by us:"); break;
256 case 6: how = _("both added:"); break;
257 case 7: how = _("both modified:"); break;
4d4d5726 258 }
b926c0d1 259 status_printf_more(s, c, "%-20s%s\n", how, one);
4d4d5726
JH
260 strbuf_release(&onebuf);
261}
262
50b7e70f
JH
263static void wt_status_print_change_data(struct wt_status *s,
264 int change_type,
265 struct string_list_item *it)
c91f0d92 266{
50b7e70f 267 struct wt_status_change_data *d = it->util;
d249b098 268 const char *c = color(change_type, s);
b8527d5f 269 int status;
50b7e70f
JH
270 char *one_name;
271 char *two_name;
3a946802 272 const char *one, *two;
f285a2d7 273 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
9297f77e 274 struct strbuf extra = STRBUF_INIT;
3a946802 275
50b7e70f
JH
276 one_name = two_name = it->string;
277 switch (change_type) {
278 case WT_STATUS_UPDATED:
279 status = d->index_status;
280 if (d->head_path)
281 one_name = d->head_path;
282 break;
283 case WT_STATUS_CHANGED:
9297f77e
JL
284 if (d->new_submodule_commits || d->dirty_submodule) {
285 strbuf_addstr(&extra, " (");
286 if (d->new_submodule_commits)
355ec7a1 287 strbuf_addf(&extra, _("new commits, "));
9297f77e 288 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
355ec7a1 289 strbuf_addf(&extra, _("modified content, "));
9297f77e 290 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
355ec7a1 291 strbuf_addf(&extra, _("untracked content, "));
9297f77e
JL
292 strbuf_setlen(&extra, extra.len - 2);
293 strbuf_addch(&extra, ')');
294 }
50b7e70f
JH
295 status = d->worktree_status;
296 break;
b8527d5f
JK
297 default:
298 die("BUG: unhandled change_type %d in wt_status_print_change_data",
299 change_type);
50b7e70f
JH
300 }
301
39598f99
JX
302 one = quote_path(one_name, s->prefix, &onebuf);
303 two = quote_path(two_name, s->prefix, &twobuf);
3a946802 304
b926c0d1 305 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
50b7e70f 306 switch (status) {
c91f0d92 307 case DIFF_STATUS_ADDED:
355ec7a1 308 status_printf_more(s, c, _("new file: %s"), one);
3a946802 309 break;
c91f0d92 310 case DIFF_STATUS_COPIED:
355ec7a1 311 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
c91f0d92
JK
312 break;
313 case DIFF_STATUS_DELETED:
355ec7a1 314 status_printf_more(s, c, _("deleted: %s"), one);
3a946802 315 break;
c91f0d92 316 case DIFF_STATUS_MODIFIED:
355ec7a1 317 status_printf_more(s, c, _("modified: %s"), one);
3a946802 318 break;
c91f0d92 319 case DIFF_STATUS_RENAMED:
d2b044be 320 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
c91f0d92
JK
321 break;
322 case DIFF_STATUS_TYPE_CHANGED:
355ec7a1 323 status_printf_more(s, c, _("typechange: %s"), one);
3a946802 324 break;
c91f0d92 325 case DIFF_STATUS_UNKNOWN:
355ec7a1 326 status_printf_more(s, c, _("unknown: %s"), one);
3a946802 327 break;
c91f0d92 328 case DIFF_STATUS_UNMERGED:
355ec7a1 329 status_printf_more(s, c, _("unmerged: %s"), one);
3a946802 330 break;
c91f0d92 331 default:
355ec7a1 332 die(_("bug: unhandled diff status %c"), status);
c91f0d92 333 }
9297f77e 334 if (extra.len) {
b926c0d1 335 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
9297f77e
JL
336 strbuf_release(&extra);
337 }
b926c0d1 338 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
367c9886
JS
339 strbuf_release(&onebuf);
340 strbuf_release(&twobuf);
c91f0d92
JK
341}
342
50b7e70f
JH
343static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
344 struct diff_options *options,
345 void *data)
c91f0d92
JK
346{
347 struct wt_status *s = data;
c91f0d92 348 int i;
50b7e70f
JH
349
350 if (!q->nr)
351 return;
352 s->workdir_dirty = 1;
c91f0d92 353 for (i = 0; i < q->nr; i++) {
50b7e70f
JH
354 struct diff_filepair *p;
355 struct string_list_item *it;
356 struct wt_status_change_data *d;
357
358 p = q->queue[i];
78a395d3 359 it = string_list_insert(&s->change, p->one->path);
50b7e70f
JH
360 d = it->util;
361 if (!d) {
362 d = xcalloc(1, sizeof(*d));
363 it->util = d;
c91f0d92 364 }
50b7e70f
JH
365 if (!d->worktree_status)
366 d->worktree_status = p->status;
9297f77e
JL
367 d->dirty_submodule = p->two->dirty_submodule;
368 if (S_ISGITLINK(p->two->mode))
369 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
c91f0d92 370 }
c91f0d92
JK
371}
372
4d4d5726
JH
373static int unmerged_mask(const char *path)
374{
375 int pos, mask;
9c5e6c80 376 const struct cache_entry *ce;
4d4d5726
JH
377
378 pos = cache_name_pos(path, strlen(path));
379 if (0 <= pos)
380 return 0;
381
382 mask = 0;
383 pos = -pos-1;
384 while (pos < active_nr) {
385 ce = active_cache[pos++];
386 if (strcmp(ce->name, path) || !ce_stage(ce))
387 break;
388 mask |= (1 << (ce_stage(ce) - 1));
389 }
390 return mask;
391}
392
50b7e70f
JH
393static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
394 struct diff_options *options,
395 void *data)
c91f0d92 396{
6e458bf6 397 struct wt_status *s = data;
c91f0d92 398 int i;
50b7e70f
JH
399
400 for (i = 0; i < q->nr; i++) {
401 struct diff_filepair *p;
402 struct string_list_item *it;
403 struct wt_status_change_data *d;
404
405 p = q->queue[i];
78a395d3 406 it = string_list_insert(&s->change, p->two->path);
50b7e70f
JH
407 d = it->util;
408 if (!d) {
409 d = xcalloc(1, sizeof(*d));
410 it->util = d;
411 }
412 if (!d->index_status)
413 d->index_status = p->status;
414 switch (p->status) {
415 case DIFF_STATUS_COPIED:
416 case DIFF_STATUS_RENAMED:
417 d->head_path = xstrdup(p->one->path);
418 break;
4d4d5726
JH
419 case DIFF_STATUS_UNMERGED:
420 d->stagemask = unmerged_mask(p->two->path);
421 break;
50b7e70f 422 }
6e458bf6 423 }
c91f0d92
JK
424}
425
50b7e70f 426static void wt_status_collect_changes_worktree(struct wt_status *s)
c91f0d92
JK
427{
428 struct rev_info rev;
50b7e70f
JH
429
430 init_revisions(&rev, NULL);
431 setup_revisions(0, NULL, &rev, NULL);
432 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
9297f77e 433 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
3bfc4504
JL
434 if (!s->show_untracked_files)
435 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
aee9c7d6
JL
436 if (s->ignore_submodule_arg) {
437 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
46a958b3 438 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
c4c42f2c 439 }
50b7e70f
JH
440 rev.diffopt.format_callback = wt_status_collect_changed_cb;
441 rev.diffopt.format_callback_data = s;
afe069d1 442 init_pathspec(&rev.prune_data, s->pathspec);
50b7e70f
JH
443 run_diff_files(&rev, 0);
444}
445
446static void wt_status_collect_changes_index(struct wt_status *s)
447{
448 struct rev_info rev;
32962c9b 449 struct setup_revision_opt opt;
50b7e70f 450
c91f0d92 451 init_revisions(&rev, NULL);
32962c9b
JH
452 memset(&opt, 0, sizeof(opt));
453 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
454 setup_revisions(0, NULL, &rev, &opt);
455
aee9c7d6
JL
456 if (s->ignore_submodule_arg) {
457 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
46a958b3 458 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
aee9c7d6 459 }
46a958b3 460
c91f0d92 461 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
50b7e70f 462 rev.diffopt.format_callback = wt_status_collect_updated_cb;
c91f0d92
JK
463 rev.diffopt.format_callback_data = s;
464 rev.diffopt.detect_rename = 1;
50705915 465 rev.diffopt.rename_limit = 200;
f714fb84 466 rev.diffopt.break_opt = 0;
afe069d1 467 init_pathspec(&rev.prune_data, s->pathspec);
c91f0d92
JK
468 run_diff_index(&rev, 1);
469}
470
50b7e70f
JH
471static void wt_status_collect_changes_initial(struct wt_status *s)
472{
eb9cb55b 473 struct pathspec pathspec;
50b7e70f
JH
474 int i;
475
eb9cb55b 476 init_pathspec(&pathspec, s->pathspec);
50b7e70f
JH
477 for (i = 0; i < active_nr; i++) {
478 struct string_list_item *it;
479 struct wt_status_change_data *d;
9c5e6c80 480 const struct cache_entry *ce = active_cache[i];
50b7e70f 481
eb9cb55b 482 if (!ce_path_match(ce, &pathspec))
76e2f7ce 483 continue;
78a395d3 484 it = string_list_insert(&s->change, ce->name);
50b7e70f
JH
485 d = it->util;
486 if (!d) {
487 d = xcalloc(1, sizeof(*d));
488 it->util = d;
489 }
4d4d5726 490 if (ce_stage(ce)) {
50b7e70f 491 d->index_status = DIFF_STATUS_UNMERGED;
4d4d5726
JH
492 d->stagemask |= (1 << (ce_stage(ce) - 1));
493 }
50b7e70f
JH
494 else
495 d->index_status = DIFF_STATUS_ADDED;
496 }
eb9cb55b 497 free_pathspec(&pathspec);
50b7e70f
JH
498}
499
76378683
JH
500static void wt_status_collect_untracked(struct wt_status *s)
501{
502 int i;
503 struct dir_struct dir;
6a38ef2c 504 struct timeval t_begin;
76378683
JH
505
506 if (!s->show_untracked_files)
507 return;
6a38ef2c
NTND
508
509 if (advice_status_u_option)
510 gettimeofday(&t_begin, NULL);
511
76378683
JH
512 memset(&dir, 0, sizeof(dir));
513 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
514 dir.flags |=
515 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
0aaf62b6
KB
516 if (s->show_ignored_files)
517 dir.flags |= DIR_SHOW_IGNORED_TOO;
76378683
JH
518 setup_standard_excludes(&dir);
519
688cd6d2 520 fill_directory(&dir, s->pathspec);
0aaf62b6 521
eeefa7c9 522 for (i = 0; i < dir.nr; i++) {
76378683 523 struct dir_entry *ent = dir.entries[i];
b822423e
BC
524 if (cache_name_is_other(ent->name, ent->len) &&
525 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
526 string_list_insert(&s->untracked, ent->name);
f5b26b1d 527 free(ent);
76378683 528 }
f5b26b1d 529
0aaf62b6
KB
530 for (i = 0; i < dir.ignored_nr; i++) {
531 struct dir_entry *ent = dir.ignored[i];
532 if (cache_name_is_other(ent->name, ent->len) &&
533 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
534 string_list_insert(&s->ignored, ent->name);
535 free(ent);
6cb3f6b2
JH
536 }
537
f5b26b1d 538 free(dir.entries);
0aaf62b6
KB
539 free(dir.ignored);
540 clear_directory(&dir);
6a38ef2c
NTND
541
542 if (advice_status_u_option) {
543 struct timeval t_end;
544 gettimeofday(&t_end, NULL);
545 s->untracked_in_ms =
546 (uint64_t)t_end.tv_sec * 1000 + t_end.tv_usec / 1000 -
547 ((uint64_t)t_begin.tv_sec * 1000 + t_begin.tv_usec / 1000);
548 }
76378683
JH
549}
550
551void wt_status_collect(struct wt_status *s)
50b7e70f
JH
552{
553 wt_status_collect_changes_worktree(s);
554
555 if (s->is_initial)
556 wt_status_collect_changes_initial(s);
557 else
558 wt_status_collect_changes_index(s);
76378683 559 wt_status_collect_untracked(s);
50b7e70f
JH
560}
561
4d4d5726
JH
562static void wt_status_print_unmerged(struct wt_status *s)
563{
564 int shown_header = 0;
565 int i;
566
567 for (i = 0; i < s->change.nr; i++) {
568 struct wt_status_change_data *d;
569 struct string_list_item *it;
570 it = &(s->change.items[i]);
571 d = it->util;
572 if (!d->stagemask)
573 continue;
574 if (!shown_header) {
575 wt_status_print_unmerged_header(s);
576 shown_header = 1;
577 }
578 wt_status_print_unmerged_data(s, it);
579 }
580 if (shown_header)
581 wt_status_print_trailer(s);
582
583}
584
50b7e70f
JH
585static void wt_status_print_updated(struct wt_status *s)
586{
587 int shown_header = 0;
588 int i;
589
590 for (i = 0; i < s->change.nr; i++) {
591 struct wt_status_change_data *d;
592 struct string_list_item *it;
593 it = &(s->change.items[i]);
594 d = it->util;
595 if (!d->index_status ||
596 d->index_status == DIFF_STATUS_UNMERGED)
597 continue;
598 if (!shown_header) {
599 wt_status_print_cached_header(s);
600 s->commitable = 1;
601 shown_header = 1;
602 }
603 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
604 }
605 if (shown_header)
606 wt_status_print_trailer(s);
607}
608
609/*
610 * -1 : has delete
611 * 0 : no change
612 * 1 : some change but no delete
613 */
9297f77e
JL
614static int wt_status_check_worktree_changes(struct wt_status *s,
615 int *dirty_submodules)
50b7e70f
JH
616{
617 int i;
618 int changes = 0;
619
9297f77e
JL
620 *dirty_submodules = 0;
621
50b7e70f
JH
622 for (i = 0; i < s->change.nr; i++) {
623 struct wt_status_change_data *d;
624 d = s->change.items[i].util;
4d4d5726
JH
625 if (!d->worktree_status ||
626 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f 627 continue;
9297f77e
JL
628 if (!changes)
629 changes = 1;
630 if (d->dirty_submodule)
631 *dirty_submodules = 1;
50b7e70f 632 if (d->worktree_status == DIFF_STATUS_DELETED)
9297f77e 633 changes = -1;
50b7e70f
JH
634 }
635 return changes;
636}
637
c91f0d92
JK
638static void wt_status_print_changed(struct wt_status *s)
639{
9297f77e
JL
640 int i, dirty_submodules;
641 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
50b7e70f
JH
642
643 if (!worktree_changes)
644 return;
645
9297f77e 646 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
50b7e70f
JH
647
648 for (i = 0; i < s->change.nr; i++) {
649 struct wt_status_change_data *d;
650 struct string_list_item *it;
651 it = &(s->change.items[i]);
652 d = it->util;
4d4d5726
JH
653 if (!d->worktree_status ||
654 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f
JH
655 continue;
656 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
657 }
658 wt_status_print_trailer(s);
c91f0d92
JK
659}
660
f17a5d34 661static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
ac8d5afc
PY
662{
663 struct child_process sm_summary;
664 char summary_limit[64];
665 char index[PATH_MAX];
66dbfd55 666 const char *env[] = { NULL, NULL };
bb7e32e3 667 struct argv_array argv = ARGV_ARRAY_INIT;
3ba7407b
MM
668 struct strbuf cmd_stdout = STRBUF_INIT;
669 struct strbuf summary = STRBUF_INIT;
670 char *summary_content;
671 size_t len;
ac8d5afc 672
d249b098 673 sprintf(summary_limit, "%d", s->submodule_summary);
ac8d5afc
PY
674 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
675
bb7e32e3
MM
676 env[0] = index;
677 argv_array_push(&argv, "submodule");
678 argv_array_push(&argv, "summary");
679 argv_array_push(&argv, uncommitted ? "--files" : "--cached");
680 argv_array_push(&argv, "--for-status");
681 argv_array_push(&argv, "--summary-limit");
682 argv_array_push(&argv, summary_limit);
683 if (!uncommitted)
684 argv_array_push(&argv, s->amend ? "HEAD^" : "HEAD");
685
ac8d5afc 686 memset(&sm_summary, 0, sizeof(sm_summary));
bb7e32e3 687 sm_summary.argv = argv.argv;
ac8d5afc
PY
688 sm_summary.env = env;
689 sm_summary.git_cmd = 1;
690 sm_summary.no_stdin = 1;
691 fflush(s->fp);
3ba7407b
MM
692 sm_summary.out = -1;
693
ac8d5afc 694 run_command(&sm_summary);
bb7e32e3 695 argv_array_clear(&argv);
3ba7407b
MM
696
697 len = strbuf_read(&cmd_stdout, sm_summary.out, 1024);
698
699 /* prepend header, only if there's an actual output */
700 if (len) {
701 if (uncommitted)
702 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
703 else
704 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
705 strbuf_addstr(&summary, "\n\n");
706 }
707 strbuf_addbuf(&summary, &cmd_stdout);
708 strbuf_release(&cmd_stdout);
709
710 summary_content = strbuf_detach(&summary, &len);
711 strbuf_add_commented_lines(&summary, summary_content, len);
712 free(summary_content);
713
714 fputs(summary.buf, s->fp);
715 strbuf_release(&summary);
ac8d5afc
PY
716}
717
1b908b6f
JH
718static void wt_status_print_other(struct wt_status *s,
719 struct string_list *l,
720 const char *what,
721 const char *how)
c91f0d92 722{
c91f0d92 723 int i;
f285a2d7 724 struct strbuf buf = STRBUF_INIT;
323d0530
NTND
725 static struct string_list output = STRING_LIST_INIT_DUP;
726 struct column_options copts;
c91f0d92 727
1282988b 728 if (!l->nr)
76378683 729 return;
c91f0d92 730
1b908b6f
JH
731 wt_status_print_other_header(s, what, how);
732
733 for (i = 0; i < l->nr; i++) {
76378683 734 struct string_list_item *it;
323d0530 735 const char *path;
1b908b6f 736 it = &(l->items[i]);
39598f99 737 path = quote_path(it->string, s->prefix, &buf);
323d0530
NTND
738 if (column_active(s->colopts)) {
739 string_list_append(&output, path);
740 continue;
741 }
b926c0d1
JN
742 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
743 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
323d0530 744 "%s\n", path);
c91f0d92 745 }
323d0530
NTND
746
747 strbuf_release(&buf);
748 if (!column_active(s->colopts))
749 return;
750
751 strbuf_addf(&buf, "%s#\t%s",
752 color(WT_STATUS_HEADER, s),
753 color(WT_STATUS_UNTRACKED, s));
754 memset(&copts, 0, sizeof(copts));
755 copts.padding = 1;
756 copts.indent = buf.buf;
757 if (want_color(s->use_color))
758 copts.nl = GIT_COLOR_RESET "\n";
759 print_columns(&output, s->colopts, &copts);
760 string_list_clear(&output, 0);
367c9886 761 strbuf_release(&buf);
c91f0d92
JK
762}
763
764static void wt_status_print_verbose(struct wt_status *s)
765{
766 struct rev_info rev;
32962c9b 767 struct setup_revision_opt opt;
99a12694 768
c91f0d92 769 init_revisions(&rev, NULL);
5ec11af6 770 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
32962c9b
JH
771
772 memset(&opt, 0, sizeof(opt));
773 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
774 setup_revisions(0, NULL, &rev, &opt);
775
c91f0d92
JK
776 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
777 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
778 rev.diffopt.file = s->fp;
779 rev.diffopt.close_file = 0;
4f672ad6
JK
780 /*
781 * If we're not going to stdout, then we definitely don't
782 * want color, since we are going to the commit message
783 * file (and even the "auto" setting won't work, since it
784 * will have checked isatty on stdout).
785 */
786 if (s->fp != stdout)
f1c96261 787 rev.diffopt.use_color = 0;
c91f0d92
JK
788 run_diff_index(&rev, 1);
789}
790
b6975ab5
JH
791static void wt_status_print_tracking(struct wt_status *s)
792{
793 struct strbuf sb = STRBUF_INIT;
794 const char *cp, *ep;
795 struct branch *branch;
796
797 assert(s->branch && !s->is_initial);
798 if (prefixcmp(s->branch, "refs/heads/"))
799 return;
800 branch = branch_get(s->branch + 11);
801 if (!format_tracking_info(branch, &sb))
802 return;
803
804 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
d249b098 805 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
eff80a9f
JH
806 "%c %.*s", comment_line_char,
807 (int)(ep - cp), cp);
808 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
809 comment_line_char);
b6975ab5
JH
810}
811
83c750ac
LK
812static int has_unmerged(struct wt_status *s)
813{
814 int i;
815
816 for (i = 0; i < s->change.nr; i++) {
817 struct wt_status_change_data *d;
818 d = s->change.items[i].util;
819 if (d->stagemask)
820 return 1;
821 }
822 return 0;
823}
824
825static void show_merge_in_progress(struct wt_status *s,
826 struct wt_status_state *state,
827 const char *color)
828{
829 if (has_unmerged(s)) {
830 status_printf_ln(s, color, _("You have unmerged paths."));
831 if (advice_status_hints)
832 status_printf_ln(s, color,
833 _(" (fix conflicts and run \"git commit\")"));
834 } else {
835 status_printf_ln(s, color,
836 _("All conflicts fixed but you are still merging."));
837 if (advice_status_hints)
838 status_printf_ln(s, color,
839 _(" (use \"git commit\" to conclude merge)"));
840 }
841 wt_status_print_trailer(s);
842}
843
844static void show_am_in_progress(struct wt_status *s,
845 struct wt_status_state *state,
846 const char *color)
847{
848 status_printf_ln(s, color,
849 _("You are in the middle of an am session."));
850 if (state->am_empty_patch)
851 status_printf_ln(s, color,
852 _("The current patch is empty."));
853 if (advice_status_hints) {
854 if (!state->am_empty_patch)
855 status_printf_ln(s, color,
8ceb6fbd 856 _(" (fix conflicts and then run \"git am --continue\")"));
83c750ac
LK
857 status_printf_ln(s, color,
858 _(" (use \"git am --skip\" to skip this patch)"));
859 status_printf_ln(s, color,
860 _(" (use \"git am --abort\" to restore the original branch)"));
861 }
862 wt_status_print_trailer(s);
863}
864
2d1cceba
LK
865static char *read_line_from_git_path(const char *filename)
866{
867 struct strbuf buf = STRBUF_INIT;
868 FILE *fp = fopen(git_path("%s", filename), "r");
869 if (!fp) {
870 strbuf_release(&buf);
871 return NULL;
872 }
873 strbuf_getline(&buf, fp, '\n');
874 if (!fclose(fp)) {
875 return strbuf_detach(&buf, NULL);
876 } else {
877 strbuf_release(&buf);
878 return NULL;
879 }
880}
881
882static int split_commit_in_progress(struct wt_status *s)
883{
884 int split_in_progress = 0;
885 char *head = read_line_from_git_path("HEAD");
886 char *orig_head = read_line_from_git_path("ORIG_HEAD");
887 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
888 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
889
890 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
891 !s->branch || strcmp(s->branch, "HEAD"))
892 return split_in_progress;
893
894 if (!strcmp(rebase_amend, rebase_orig_head)) {
895 if (strcmp(head, rebase_amend))
896 split_in_progress = 1;
897 } else if (strcmp(orig_head, rebase_orig_head)) {
898 split_in_progress = 1;
899 }
900
901 if (!s->amend && !s->nowarn && !s->workdir_dirty)
902 split_in_progress = 0;
903
904 free(head);
905 free(orig_head);
906 free(rebase_amend);
907 free(rebase_orig_head);
908 return split_in_progress;
909}
910
83c750ac
LK
911static void show_rebase_in_progress(struct wt_status *s,
912 struct wt_status_state *state,
913 const char *color)
914{
915 struct stat st;
916
917 if (has_unmerged(s)) {
0722c805
NTND
918 if (state->branch)
919 status_printf_ln(s, color,
920 _("You are currently rebasing branch '%s' on '%s'."),
921 state->branch,
922 state->onto);
923 else
924 status_printf_ln(s, color,
925 _("You are currently rebasing."));
83c750ac
LK
926 if (advice_status_hints) {
927 status_printf_ln(s, color,
928 _(" (fix conflicts and then run \"git rebase --continue\")"));
929 status_printf_ln(s, color,
930 _(" (use \"git rebase --skip\" to skip this patch)"));
931 status_printf_ln(s, color,
932 _(" (use \"git rebase --abort\" to check out the original branch)"));
933 }
934 } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
0722c805
NTND
935 if (state->branch)
936 status_printf_ln(s, color,
937 _("You are currently rebasing branch '%s' on '%s'."),
938 state->branch,
939 state->onto);
940 else
941 status_printf_ln(s, color,
942 _("You are currently rebasing."));
83c750ac
LK
943 if (advice_status_hints)
944 status_printf_ln(s, color,
945 _(" (all conflicts fixed: run \"git rebase --continue\")"));
2d1cceba 946 } else if (split_commit_in_progress(s)) {
0722c805
NTND
947 if (state->branch)
948 status_printf_ln(s, color,
949 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
950 state->branch,
951 state->onto);
952 else
953 status_printf_ln(s, color,
954 _("You are currently splitting a commit during a rebase."));
2d1cceba
LK
955 if (advice_status_hints)
956 status_printf_ln(s, color,
957 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
83c750ac 958 } else {
0722c805
NTND
959 if (state->branch)
960 status_printf_ln(s, color,
961 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
962 state->branch,
963 state->onto);
964 else
965 status_printf_ln(s, color,
966 _("You are currently editing a commit during a rebase."));
83c750ac
LK
967 if (advice_status_hints && !s->amend) {
968 status_printf_ln(s, color,
969 _(" (use \"git commit --amend\" to amend the current commit)"));
970 status_printf_ln(s, color,
971 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
972 }
973 }
974 wt_status_print_trailer(s);
975}
976
977static void show_cherry_pick_in_progress(struct wt_status *s,
978 struct wt_status_state *state,
979 const char *color)
980{
981 status_printf_ln(s, color, _("You are currently cherry-picking."));
982 if (advice_status_hints) {
983 if (has_unmerged(s))
984 status_printf_ln(s, color,
b95e66f5 985 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
83c750ac
LK
986 else
987 status_printf_ln(s, color,
b95e66f5
RT
988 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
989 status_printf_ln(s, color,
990 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
83c750ac
LK
991 }
992 wt_status_print_trailer(s);
993}
994
db4ef449
MM
995static void show_revert_in_progress(struct wt_status *s,
996 struct wt_status_state *state,
997 const char *color)
998{
87e139c0
MM
999 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1000 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
db4ef449
MM
1001 if (advice_status_hints) {
1002 if (has_unmerged(s))
1003 status_printf_ln(s, color,
1004 _(" (fix conflicts and run \"git revert --continue\")"));
1005 else
1006 status_printf_ln(s, color,
1007 _(" (all conflicts fixed: run \"git revert --continue\")"));
1008 status_printf_ln(s, color,
1009 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1010 }
1011 wt_status_print_trailer(s);
1012}
1013
83c750ac
LK
1014static void show_bisect_in_progress(struct wt_status *s,
1015 struct wt_status_state *state,
1016 const char *color)
1017{
0722c805
NTND
1018 if (state->branch)
1019 status_printf_ln(s, color,
6deab24d 1020 _("You are currently bisecting, started from branch '%s'."),
0722c805
NTND
1021 state->branch);
1022 else
1023 status_printf_ln(s, color,
1024 _("You are currently bisecting."));
83c750ac
LK
1025 if (advice_status_hints)
1026 status_printf_ln(s, color,
1027 _(" (use \"git bisect reset\" to get back to the original branch)"));
1028 wt_status_print_trailer(s);
1029}
1030
0722c805
NTND
1031/*
1032 * Extract branch information from rebase/bisect
1033 */
8b87cfd0 1034static char *read_and_strip_branch(const char *path)
0722c805 1035{
8b87cfd0 1036 struct strbuf sb = STRBUF_INIT;
0722c805
NTND
1037 unsigned char sha1[20];
1038
8b87cfd0
NTND
1039 if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1040 goto got_nothing;
0722c805 1041
8b87cfd0
NTND
1042 while (&sb.len && sb.buf[sb.len - 1] == '\n')
1043 strbuf_setlen(&sb, sb.len - 1);
1044 if (!sb.len)
1045 goto got_nothing;
1046 if (!prefixcmp(sb.buf, "refs/heads/"))
1047 strbuf_remove(&sb,0, strlen("refs/heads/"));
1048 else if (!prefixcmp(sb.buf, "refs/"))
1049 ;
1050 else if (!get_sha1_hex(sb.buf, sha1)) {
0722c805
NTND
1051 const char *abbrev;
1052 abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
8b87cfd0
NTND
1053 strbuf_reset(&sb);
1054 strbuf_addstr(&sb, abbrev);
1055 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1056 goto got_nothing;
0722c805 1057 else /* bisect */
8b87cfd0
NTND
1058 ;
1059 return strbuf_detach(&sb, NULL);
1060
1061got_nothing:
1062 strbuf_release(&sb);
1063 return NULL;
0722c805
NTND
1064}
1065
b397ea48 1066struct grab_1st_switch_cbdata {
b397ea48
NTND
1067 struct strbuf buf;
1068 unsigned char nsha1[20];
1069};
1070
1071static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
1072 const char *email, unsigned long timestamp, int tz,
1073 const char *message, void *cb_data)
83c750ac 1074{
b397ea48
NTND
1075 struct grab_1st_switch_cbdata *cb = cb_data;
1076 const char *target = NULL, *end;
83c750ac 1077
b397ea48
NTND
1078 if (prefixcmp(message, "checkout: moving from "))
1079 return 0;
1080 message += strlen("checkout: moving from ");
1081 target = strstr(message, " to ");
1082 if (!target)
1083 return 0;
1084 target += strlen(" to ");
1085 strbuf_reset(&cb->buf);
1086 hashcpy(cb->nsha1, nsha1);
1087 for (end = target; *end && *end != '\n'; end++)
1088 ;
1089 strbuf_add(&cb->buf, target, end - target);
b397ea48
NTND
1090 return 1;
1091}
1092
1093static void wt_status_get_detached_from(struct wt_status_state *state)
1094{
1095 struct grab_1st_switch_cbdata cb;
1096 struct commit *commit;
1097 unsigned char sha1[20];
1098 char *ref = NULL;
1099
1100 strbuf_init(&cb.buf, 0);
1101 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1102 strbuf_release(&cb.buf);
1103 return;
1104 }
1105
1106 if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 &&
1107 /* sha1 is a commit? match without further lookup */
1108 (!hashcmp(cb.nsha1, sha1) ||
1109 /* perhaps sha1 is a tag, try to dereference to a commit */
1110 ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
1111 !hashcmp(cb.nsha1, commit->object.sha1)))) {
1112 int ofs;
1113 if (!prefixcmp(ref, "refs/tags/"))
1114 ofs = strlen("refs/tags/");
1115 else if (!prefixcmp(ref, "refs/remotes/"))
1116 ofs = strlen("refs/remotes/");
1117 else
1118 ofs = 0;
1119 state->detached_from = xstrdup(ref + ofs);
1120 } else
1121 state->detached_from =
1122 xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
1123 hashcpy(state->detached_sha1, cb.nsha1);
1124
1125 free(ref);
1126 strbuf_release(&cb.buf);
1127}
1128
1129void wt_status_get_state(struct wt_status_state *state,
1130 int get_detached_from)
83c750ac 1131{
83c750ac 1132 struct stat st;
87e139c0 1133 unsigned char sha1[20];
83c750ac
LK
1134
1135 if (!stat(git_path("MERGE_HEAD"), &st)) {
b9691db4 1136 state->merge_in_progress = 1;
83c750ac
LK
1137 } else if (!stat(git_path("rebase-apply"), &st)) {
1138 if (!stat(git_path("rebase-apply/applying"), &st)) {
b9691db4 1139 state->am_in_progress = 1;
83c750ac 1140 if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
b9691db4 1141 state->am_empty_patch = 1;
83c750ac 1142 } else {
b9691db4
NTND
1143 state->rebase_in_progress = 1;
1144 state->branch = read_and_strip_branch("rebase-apply/head-name");
1145 state->onto = read_and_strip_branch("rebase-apply/onto");
83c750ac
LK
1146 }
1147 } else if (!stat(git_path("rebase-merge"), &st)) {
1148 if (!stat(git_path("rebase-merge/interactive"), &st))
b9691db4 1149 state->rebase_interactive_in_progress = 1;
83c750ac 1150 else
b9691db4
NTND
1151 state->rebase_in_progress = 1;
1152 state->branch = read_and_strip_branch("rebase-merge/head-name");
1153 state->onto = read_and_strip_branch("rebase-merge/onto");
83c750ac 1154 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
b9691db4 1155 state->cherry_pick_in_progress = 1;
83c750ac 1156 }
0722c805 1157 if (!stat(git_path("BISECT_LOG"), &st)) {
b9691db4
NTND
1158 state->bisect_in_progress = 1;
1159 state->branch = read_and_strip_branch("BISECT_START");
0722c805 1160 }
87e139c0
MM
1161 if (!stat(git_path("REVERT_HEAD"), &st) &&
1162 !get_sha1("REVERT_HEAD", sha1)) {
db4ef449 1163 state->revert_in_progress = 1;
87e139c0 1164 hashcpy(state->revert_head_sha1, sha1);
db4ef449 1165 }
83c750ac 1166
b397ea48
NTND
1167 if (get_detached_from)
1168 wt_status_get_detached_from(state);
b9691db4
NTND
1169}
1170
3b691ccc
NTND
1171static void wt_status_print_state(struct wt_status *s,
1172 struct wt_status_state *state)
b9691db4
NTND
1173{
1174 const char *state_color = color(WT_STATUS_HEADER, s);
3b691ccc
NTND
1175 if (state->merge_in_progress)
1176 show_merge_in_progress(s, state, state_color);
1177 else if (state->am_in_progress)
1178 show_am_in_progress(s, state, state_color);
1179 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1180 show_rebase_in_progress(s, state, state_color);
1181 else if (state->cherry_pick_in_progress)
1182 show_cherry_pick_in_progress(s, state, state_color);
db4ef449
MM
1183 else if (state->revert_in_progress)
1184 show_revert_in_progress(s, state, state_color);
3b691ccc
NTND
1185 if (state->bisect_in_progress)
1186 show_bisect_in_progress(s, state, state_color);
83c750ac
LK
1187}
1188
c91f0d92
JK
1189void wt_status_print(struct wt_status *s)
1190{
1d282327
AA
1191 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1192 const char *branch_status_color = color(WT_STATUS_HEADER, s);
3b691ccc
NTND
1193 struct wt_status_state state;
1194
1195 memset(&state, 0, sizeof(state));
b397ea48
NTND
1196 wt_status_get_state(&state,
1197 s->branch && !strcmp(s->branch, "HEAD"));
98bf8a47 1198
bda324cf 1199 if (s->branch) {
355ec7a1 1200 const char *on_what = _("On branch ");
bda324cf 1201 const char *branch_name = s->branch;
cc44c765 1202 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
1203 branch_name += 11;
1204 else if (!strcmp(branch_name, "HEAD")) {
1d282327 1205 branch_status_color = color(WT_STATUS_NOBRANCH, s);
ec506310
RR
1206 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1207 on_what = _("rebase in progress; onto ");
1208 branch_name = state.onto;
1209 } else if (state.detached_from) {
b397ea48
NTND
1210 unsigned char sha1[20];
1211 branch_name = state.detached_from;
1212 if (!get_sha1("HEAD", sha1) &&
1213 !hashcmp(sha1, state.detached_sha1))
1214 on_what = _("HEAD detached at ");
1215 else
1216 on_what = _("HEAD detached from ");
1217 } else {
1218 branch_name = "";
1219 on_what = _("Not currently on any branch.");
1220 }
bda324cf 1221 }
b926c0d1
JN
1222 status_printf(s, color(WT_STATUS_HEADER, s), "");
1223 status_printf_more(s, branch_status_color, "%s", on_what);
1224 status_printf_more(s, branch_color, "%s\n", branch_name);
b6975ab5
JH
1225 if (!s->is_initial)
1226 wt_status_print_tracking(s);
bda324cf 1227 }
c91f0d92 1228
3b691ccc
NTND
1229 wt_status_print_state(s, &state);
1230 free(state.branch);
1231 free(state.onto);
b397ea48 1232 free(state.detached_from);
3b691ccc 1233
c91f0d92 1234 if (s->is_initial) {
b926c0d1 1235 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
b3b298af 1236 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
b926c0d1 1237 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
c91f0d92
JK
1238 }
1239
c1e255b7 1240 wt_status_print_updated(s);
228e7b5d 1241 wt_status_print_unmerged(s);
c91f0d92 1242 wt_status_print_changed(s);
46a958b3
JL
1243 if (s->submodule_summary &&
1244 (!s->ignore_submodule_arg ||
1245 strcmp(s->ignore_submodule_arg, "all"))) {
f17a5d34
JL
1246 wt_status_print_submodule_summary(s, 0); /* staged */
1247 wt_status_print_submodule_summary(s, 1); /* unstaged */
1248 }
2381e39e 1249 if (s->show_untracked_files) {
50bd8b7e 1250 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
2381e39e 1251 if (s->show_ignored_files)
50bd8b7e 1252 wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
6a38ef2c
NTND
1253 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1254 status_printf_ln(s, GIT_COLOR_NORMAL, "");
1255 status_printf_ln(s, GIT_COLOR_NORMAL,
62901179
JX
1256 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1257 "may speed it up, but you have to be careful not to forget to add\n"
1258 "new files yourself (see 'git help status')."),
1259 s->untracked_in_ms / 1000.0);
6a38ef2c 1260 }
2381e39e 1261 } else if (s->commitable)
355ec7a1 1262 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
980bde38 1263 advice_status_hints
355ec7a1 1264 ? _(" (use -u option to show untracked files)") : "");
c91f0d92 1265
1324fb6f 1266 if (s->verbose)
c91f0d92 1267 wt_status_print_verbose(s);
6e458bf6
JR
1268 if (!s->commitable) {
1269 if (s->amend)
355ec7a1 1270 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
37d07f8f
JH
1271 else if (s->nowarn)
1272 ; /* nothing */
50bd8b7e
NTND
1273 else if (s->workdir_dirty) {
1274 if (advice_status_hints)
1275 printf(_("no changes added to commit "
1276 "(use \"git add\" and/or \"git commit -a\")\n"));
1277 else
1278 printf(_("no changes added to commit\n"));
1279 } else if (s->untracked.nr) {
1280 if (advice_status_hints)
1281 printf(_("nothing added to commit but untracked files "
1282 "present (use \"git add\" to track)\n"));
1283 else
1284 printf(_("nothing added to commit but untracked files present\n"));
1285 } else if (s->is_initial) {
1286 if (advice_status_hints)
1287 printf(_("nothing to commit (create/copy files "
1288 "and use \"git add\" to track)\n"));
1289 else
1290 printf(_("nothing to commit\n"));
1291 } else if (!s->show_untracked_files) {
1292 if (advice_status_hints)
1293 printf(_("nothing to commit (use -u to show untracked files)\n"));
1294 else
1295 printf(_("nothing to commit\n"));
1296 } else
1297 printf(_("nothing to commit, working directory clean\n"));
6e458bf6 1298 }
c91f0d92 1299}
84dbe7b8 1300
3207a3a2 1301static void wt_shortstatus_unmerged(struct string_list_item *it,
84dbe7b8
MG
1302 struct wt_status *s)
1303{
1304 struct wt_status_change_data *d = it->util;
1305 const char *how = "??";
1306
1307 switch (d->stagemask) {
1308 case 1: how = "DD"; break; /* both deleted */
1309 case 2: how = "AU"; break; /* added by us */
1310 case 3: how = "UD"; break; /* deleted by them */
1311 case 4: how = "UA"; break; /* added by them */
1312 case 5: how = "DU"; break; /* deleted by us */
1313 case 6: how = "AA"; break; /* both added */
1314 case 7: how = "UU"; break; /* both modified */
1315 }
3fe2a894 1316 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
3207a3a2 1317 if (s->null_termination) {
3fe2a894 1318 fprintf(stdout, " %s%c", it->string, 0);
84dbe7b8
MG
1319 } else {
1320 struct strbuf onebuf = STRBUF_INIT;
1321 const char *one;
39598f99 1322 one = quote_path(it->string, s->prefix, &onebuf);
3fe2a894 1323 printf(" %s\n", one);
84dbe7b8
MG
1324 strbuf_release(&onebuf);
1325 }
1326}
1327
3207a3a2 1328static void wt_shortstatus_status(struct string_list_item *it,
84dbe7b8
MG
1329 struct wt_status *s)
1330{
1331 struct wt_status_change_data *d = it->util;
1332
3fe2a894
MG
1333 if (d->index_status)
1334 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1335 else
1336 putchar(' ');
1337 if (d->worktree_status)
1338 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1339 else
1340 putchar(' ');
1341 putchar(' ');
3207a3a2 1342 if (s->null_termination) {
84dbe7b8
MG
1343 fprintf(stdout, "%s%c", it->string, 0);
1344 if (d->head_path)
1345 fprintf(stdout, "%s%c", d->head_path, 0);
1346 } else {
1347 struct strbuf onebuf = STRBUF_INIT;
1348 const char *one;
1349 if (d->head_path) {
39598f99 1350 one = quote_path(d->head_path, s->prefix, &onebuf);
dbfdc625
KB
1351 if (*one != '"' && strchr(one, ' ') != NULL) {
1352 putchar('"');
1353 strbuf_addch(&onebuf, '"');
1354 one = onebuf.buf;
1355 }
84dbe7b8
MG
1356 printf("%s -> ", one);
1357 strbuf_release(&onebuf);
1358 }
39598f99 1359 one = quote_path(it->string, s->prefix, &onebuf);
dbfdc625
KB
1360 if (*one != '"' && strchr(one, ' ') != NULL) {
1361 putchar('"');
1362 strbuf_addch(&onebuf, '"');
1363 one = onebuf.buf;
1364 }
84dbe7b8
MG
1365 printf("%s\n", one);
1366 strbuf_release(&onebuf);
1367 }
1368}
1369
3207a3a2 1370static void wt_shortstatus_other(struct string_list_item *it,
2381e39e 1371 struct wt_status *s, const char *sign)
84dbe7b8 1372{
3207a3a2 1373 if (s->null_termination) {
2381e39e 1374 fprintf(stdout, "%s %s%c", sign, it->string, 0);
84dbe7b8
MG
1375 } else {
1376 struct strbuf onebuf = STRBUF_INIT;
1377 const char *one;
39598f99 1378 one = quote_path(it->string, s->prefix, &onebuf);
c1909e72 1379 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
3fe2a894 1380 printf(" %s\n", one);
84dbe7b8
MG
1381 strbuf_release(&onebuf);
1382 }
1383}
1384
05a59a08
DKF
1385static void wt_shortstatus_print_tracking(struct wt_status *s)
1386{
1387 struct branch *branch;
1388 const char *header_color = color(WT_STATUS_HEADER, s);
1389 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1390 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1391
1392 const char *base;
1393 const char *branch_name;
1394 int num_ours, num_theirs;
1395
1396 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1397
1398 if (!s->branch)
1399 return;
1400 branch_name = s->branch;
1401
1402 if (!prefixcmp(branch_name, "refs/heads/"))
1403 branch_name += 11;
1404 else if (!strcmp(branch_name, "HEAD")) {
98f5e24b 1405 branch_name = _("HEAD (no branch)");
05a59a08
DKF
1406 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1407 }
1408
1409 branch = branch_get(s->branch + 11);
1410 if (s->is_initial)
98f5e24b 1411 color_fprintf(s->fp, header_color, _("Initial commit on "));
05a59a08 1412 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
a5985237
JK
1413 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1414 fputc(s->null_termination ? '\0' : '\n', s->fp);
05a59a08
DKF
1415 return;
1416 }
1417
1418 base = branch->merge[0]->dst;
1419 base = shorten_unambiguous_ref(base, 0);
1420 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1421 color_fprintf(s->fp, header_color, "...");
1422 color_fprintf(s->fp, branch_color_remote, "%s", base);
1423
1424 color_fprintf(s->fp, header_color, " [");
1425 if (!num_ours) {
98f5e24b 1426 color_fprintf(s->fp, header_color, _("behind "));
05a59a08
DKF
1427 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1428 } else if (!num_theirs) {
98f5e24b 1429 color_fprintf(s->fp, header_color, _("ahead "));
05a59a08
DKF
1430 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1431 } else {
98f5e24b 1432 color_fprintf(s->fp, header_color, _("ahead "));
05a59a08 1433 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
98f5e24b 1434 color_fprintf(s->fp, header_color, _(", behind "));
05a59a08
DKF
1435 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1436 }
1437
a5985237
JK
1438 color_fprintf(s->fp, header_color, "]");
1439 fputc(s->null_termination ? '\0' : '\n', s->fp);
05a59a08
DKF
1440}
1441
d4a6bf1f 1442void wt_shortstatus_print(struct wt_status *s)
84dbe7b8
MG
1443{
1444 int i;
05a59a08 1445
d4a6bf1f 1446 if (s->show_branch)
05a59a08
DKF
1447 wt_shortstatus_print_tracking(s);
1448
84dbe7b8
MG
1449 for (i = 0; i < s->change.nr; i++) {
1450 struct wt_status_change_data *d;
1451 struct string_list_item *it;
1452
1453 it = &(s->change.items[i]);
1454 d = it->util;
1455 if (d->stagemask)
3207a3a2 1456 wt_shortstatus_unmerged(it, s);
84dbe7b8 1457 else
3207a3a2 1458 wt_shortstatus_status(it, s);
84dbe7b8
MG
1459 }
1460 for (i = 0; i < s->untracked.nr; i++) {
1461 struct string_list_item *it;
1462
1463 it = &(s->untracked.items[i]);
3207a3a2 1464 wt_shortstatus_other(it, s, "??");
2381e39e
JH
1465 }
1466 for (i = 0; i < s->ignored.nr; i++) {
1467 struct string_list_item *it;
1468
1469 it = &(s->ignored.items[i]);
3207a3a2 1470 wt_shortstatus_other(it, s, "!!");
84dbe7b8
MG
1471 }
1472}
4a7cc2fd 1473
3207a3a2 1474void wt_porcelain_print(struct wt_status *s)
4a7cc2fd
JK
1475{
1476 s->use_color = 0;
8661768f
JK
1477 s->relative_paths = 0;
1478 s->prefix = NULL;
d4a6bf1f 1479 wt_shortstatus_print(s);
4a7cc2fd 1480}