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