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