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