]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
wildmatch: rename constants and update prototype
[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"
c91f0d92 15
23900a96 16static char default_wt_status_colors[][COLOR_MAXLEN] = {
dc6ebd4c
AL
17 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
18 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
19 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
20 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
21 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
4d4d5726 22 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
05a59a08
DKF
23 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
24 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
148135fc 25 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
c91f0d92 26};
4d229653 27
d249b098 28static const char *color(int slot, struct wt_status *s)
c91f0d92 29{
daa0c3d9
JK
30 const char *c = "";
31 if (want_color(s->use_color))
32 c = s->color_palette[slot];
148135fc
JK
33 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
34 c = s->color_palette[WT_STATUS_HEADER];
35 return c;
c91f0d92
JK
36}
37
becbdae8
JN
38static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
39 const char *fmt, va_list ap, const char *trail)
40{
41 struct strbuf sb = STRBUF_INIT;
42 struct strbuf linebuf = STRBUF_INIT;
43 const char *line, *eol;
44
45 strbuf_vaddf(&sb, fmt, ap);
46 if (!sb.len) {
47 strbuf_addch(&sb, '#');
48 if (!trail)
49 strbuf_addch(&sb, ' ');
50 color_print_strbuf(s->fp, color, &sb);
51 if (trail)
52 fprintf(s->fp, "%s", trail);
53 strbuf_release(&sb);
54 return;
55 }
56 for (line = sb.buf; *line; line = eol + 1) {
57 eol = strchr(line, '\n');
58
59 strbuf_reset(&linebuf);
60 if (at_bol) {
61 strbuf_addch(&linebuf, '#');
62 if (*line != '\n' && *line != '\t')
63 strbuf_addch(&linebuf, ' ');
64 }
65 if (eol)
66 strbuf_add(&linebuf, line, eol - line);
67 else
68 strbuf_addstr(&linebuf, line);
69 color_print_strbuf(s->fp, color, &linebuf);
70 if (eol)
71 fprintf(s->fp, "\n");
72 else
73 break;
74 at_bol = 1;
75 }
76 if (trail)
77 fprintf(s->fp, "%s", trail);
78 strbuf_release(&linebuf);
79 strbuf_release(&sb);
80}
81
82void status_printf_ln(struct wt_status *s, const char *color,
83 const char *fmt, ...)
84{
85 va_list ap;
86
87 va_start(ap, fmt);
88 status_vprintf(s, 1, color, fmt, ap, "\n");
89 va_end(ap);
90}
91
92void status_printf(struct wt_status *s, const char *color,
93 const char *fmt, ...)
94{
95 va_list ap;
96
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, NULL);
99 va_end(ap);
100}
101
102void status_printf_more(struct wt_status *s, const char *color,
103 const char *fmt, ...)
104{
105 va_list ap;
106
107 va_start(ap, fmt);
108 status_vprintf(s, 0, color, fmt, ap, NULL);
109 va_end(ap);
110}
111
c91f0d92
JK
112void wt_status_prepare(struct wt_status *s)
113{
114 unsigned char sha1[20];
c91f0d92 115
cc46a743 116 memset(s, 0, sizeof(*s));
23900a96
JH
117 memcpy(s->color_palette, default_wt_status_colors,
118 sizeof(default_wt_status_colors));
d249b098
JH
119 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
120 s->use_color = -1;
121 s->relative_paths = 1;
96ec7b1e 122 s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
c91f0d92 123 s->reference = "HEAD";
f26a0012 124 s->fp = stdout;
0f729f21 125 s->index_file = get_index_file();
50b7e70f 126 s->change.strdup_strings = 1;
76378683 127 s->untracked.strdup_strings = 1;
6cb3f6b2 128 s->ignored.strdup_strings = 1;
c91f0d92
JK
129}
130
4d4d5726
JH
131static void wt_status_print_unmerged_header(struct wt_status *s)
132{
d249b098 133 const char *c = color(WT_STATUS_HEADER, s);
3c588453 134
355ec7a1 135 status_printf_ln(s, c, _("Unmerged paths:"));
edf563fb
JK
136 if (!advice_status_hints)
137 return;
37f7a857 138 if (s->whence != FROM_COMMIT)
3c588453
JH
139 ;
140 else if (!s->is_initial)
355ec7a1 141 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
4d4d5726 142 else
355ec7a1
ÆAB
143 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
144 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
b926c0d1 145 status_printf_ln(s, c, "");
4d4d5726
JH
146}
147
f26a0012 148static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb 149{
d249b098 150 const char *c = color(WT_STATUS_HEADER, s);
3c588453 151
919a4ce0 152 status_printf_ln(s, c, _("Changes to be committed:"));
edf563fb
JK
153 if (!advice_status_hints)
154 return;
37f7a857 155 if (s->whence != FROM_COMMIT)
3c588453
JH
156 ; /* NEEDSWORK: use "git reset --unresolve"??? */
157 else if (!s->is_initial)
355ec7a1 158 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
3c588453 159 else
355ec7a1 160 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
b926c0d1 161 status_printf_ln(s, c, "");
3c1eb9cb
JR
162}
163
bb914b14 164static void wt_status_print_dirty_header(struct wt_status *s,
9297f77e
JL
165 int has_deleted,
166 int has_dirty_submodules)
c91f0d92 167{
d249b098 168 const char *c = color(WT_STATUS_HEADER, s);
3c588453 169
355ec7a1 170 status_printf_ln(s, c, _("Changes not staged for commit:"));
edf563fb
JK
171 if (!advice_status_hints)
172 return;
bb914b14 173 if (!has_deleted)
355ec7a1 174 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
bb914b14 175 else
355ec7a1
ÆAB
176 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
177 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
9297f77e 178 if (has_dirty_submodules)
355ec7a1 179 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
b926c0d1 180 status_printf_ln(s, c, "");
bb914b14
AM
181}
182
1b908b6f
JH
183static void wt_status_print_other_header(struct wt_status *s,
184 const char *what,
185 const char *how)
bb914b14 186{
d249b098 187 const char *c = color(WT_STATUS_HEADER, s);
355ec7a1 188 status_printf_ln(s, c, _("%s files:"), what);
edf563fb
JK
189 if (!advice_status_hints)
190 return;
355ec7a1 191 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
b926c0d1 192 status_printf_ln(s, c, "");
c91f0d92
JK
193}
194
f26a0012 195static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 196{
b926c0d1 197 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
c91f0d92
JK
198}
199
a734d0b1 200#define quote_path quote_path_relative
3a946802 201
4d4d5726
JH
202static void wt_status_print_unmerged_data(struct wt_status *s,
203 struct string_list_item *it)
204{
d249b098 205 const char *c = color(WT_STATUS_UNMERGED, s);
4d4d5726
JH
206 struct wt_status_change_data *d = it->util;
207 struct strbuf onebuf = STRBUF_INIT;
355ec7a1 208 const char *one, *how = _("bug");
4d4d5726
JH
209
210 one = quote_path(it->string, -1, &onebuf, s->prefix);
b926c0d1 211 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
4d4d5726 212 switch (d->stagemask) {
355ec7a1
ÆAB
213 case 1: how = _("both deleted:"); break;
214 case 2: how = _("added by us:"); break;
215 case 3: how = _("deleted by them:"); break;
216 case 4: how = _("added by them:"); break;
217 case 5: how = _("deleted by us:"); break;
218 case 6: how = _("both added:"); break;
219 case 7: how = _("both modified:"); break;
4d4d5726 220 }
b926c0d1 221 status_printf_more(s, c, "%-20s%s\n", how, one);
4d4d5726
JH
222 strbuf_release(&onebuf);
223}
224
50b7e70f
JH
225static void wt_status_print_change_data(struct wt_status *s,
226 int change_type,
227 struct string_list_item *it)
c91f0d92 228{
50b7e70f 229 struct wt_status_change_data *d = it->util;
d249b098 230 const char *c = color(change_type, s);
50b7e70f
JH
231 int status = status;
232 char *one_name;
233 char *two_name;
3a946802 234 const char *one, *two;
f285a2d7 235 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
9297f77e 236 struct strbuf extra = STRBUF_INIT;
3a946802 237
50b7e70f
JH
238 one_name = two_name = it->string;
239 switch (change_type) {
240 case WT_STATUS_UPDATED:
241 status = d->index_status;
242 if (d->head_path)
243 one_name = d->head_path;
244 break;
245 case WT_STATUS_CHANGED:
9297f77e
JL
246 if (d->new_submodule_commits || d->dirty_submodule) {
247 strbuf_addstr(&extra, " (");
248 if (d->new_submodule_commits)
355ec7a1 249 strbuf_addf(&extra, _("new commits, "));
9297f77e 250 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
355ec7a1 251 strbuf_addf(&extra, _("modified content, "));
9297f77e 252 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
355ec7a1 253 strbuf_addf(&extra, _("untracked content, "));
9297f77e
JL
254 strbuf_setlen(&extra, extra.len - 2);
255 strbuf_addch(&extra, ')');
256 }
50b7e70f
JH
257 status = d->worktree_status;
258 break;
259 }
260
261 one = quote_path(one_name, -1, &onebuf, s->prefix);
262 two = quote_path(two_name, -1, &twobuf, s->prefix);
3a946802 263
b926c0d1 264 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
50b7e70f 265 switch (status) {
c91f0d92 266 case DIFF_STATUS_ADDED:
355ec7a1 267 status_printf_more(s, c, _("new file: %s"), one);
3a946802 268 break;
c91f0d92 269 case DIFF_STATUS_COPIED:
355ec7a1 270 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
c91f0d92
JK
271 break;
272 case DIFF_STATUS_DELETED:
355ec7a1 273 status_printf_more(s, c, _("deleted: %s"), one);
3a946802 274 break;
c91f0d92 275 case DIFF_STATUS_MODIFIED:
355ec7a1 276 status_printf_more(s, c, _("modified: %s"), one);
3a946802 277 break;
c91f0d92 278 case DIFF_STATUS_RENAMED:
d2b044be 279 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
c91f0d92
JK
280 break;
281 case DIFF_STATUS_TYPE_CHANGED:
355ec7a1 282 status_printf_more(s, c, _("typechange: %s"), one);
3a946802 283 break;
c91f0d92 284 case DIFF_STATUS_UNKNOWN:
355ec7a1 285 status_printf_more(s, c, _("unknown: %s"), one);
3a946802 286 break;
c91f0d92 287 case DIFF_STATUS_UNMERGED:
355ec7a1 288 status_printf_more(s, c, _("unmerged: %s"), one);
3a946802 289 break;
c91f0d92 290 default:
355ec7a1 291 die(_("bug: unhandled diff status %c"), status);
c91f0d92 292 }
9297f77e 293 if (extra.len) {
b926c0d1 294 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
9297f77e
JL
295 strbuf_release(&extra);
296 }
b926c0d1 297 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
367c9886
JS
298 strbuf_release(&onebuf);
299 strbuf_release(&twobuf);
c91f0d92
JK
300}
301
50b7e70f
JH
302static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
303 struct diff_options *options,
304 void *data)
c91f0d92
JK
305{
306 struct wt_status *s = data;
c91f0d92 307 int i;
50b7e70f
JH
308
309 if (!q->nr)
310 return;
311 s->workdir_dirty = 1;
c91f0d92 312 for (i = 0; i < q->nr; i++) {
50b7e70f
JH
313 struct diff_filepair *p;
314 struct string_list_item *it;
315 struct wt_status_change_data *d;
316
317 p = q->queue[i];
78a395d3 318 it = string_list_insert(&s->change, p->one->path);
50b7e70f
JH
319 d = it->util;
320 if (!d) {
321 d = xcalloc(1, sizeof(*d));
322 it->util = d;
c91f0d92 323 }
50b7e70f
JH
324 if (!d->worktree_status)
325 d->worktree_status = p->status;
9297f77e
JL
326 d->dirty_submodule = p->two->dirty_submodule;
327 if (S_ISGITLINK(p->two->mode))
328 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
c91f0d92 329 }
c91f0d92
JK
330}
331
4d4d5726
JH
332static int unmerged_mask(const char *path)
333{
334 int pos, mask;
335 struct cache_entry *ce;
336
337 pos = cache_name_pos(path, strlen(path));
338 if (0 <= pos)
339 return 0;
340
341 mask = 0;
342 pos = -pos-1;
343 while (pos < active_nr) {
344 ce = active_cache[pos++];
345 if (strcmp(ce->name, path) || !ce_stage(ce))
346 break;
347 mask |= (1 << (ce_stage(ce) - 1));
348 }
349 return mask;
350}
351
50b7e70f
JH
352static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
353 struct diff_options *options,
354 void *data)
c91f0d92 355{
6e458bf6 356 struct wt_status *s = data;
c91f0d92 357 int i;
50b7e70f
JH
358
359 for (i = 0; i < q->nr; i++) {
360 struct diff_filepair *p;
361 struct string_list_item *it;
362 struct wt_status_change_data *d;
363
364 p = q->queue[i];
78a395d3 365 it = string_list_insert(&s->change, p->two->path);
50b7e70f
JH
366 d = it->util;
367 if (!d) {
368 d = xcalloc(1, sizeof(*d));
369 it->util = d;
370 }
371 if (!d->index_status)
372 d->index_status = p->status;
373 switch (p->status) {
374 case DIFF_STATUS_COPIED:
375 case DIFF_STATUS_RENAMED:
376 d->head_path = xstrdup(p->one->path);
377 break;
4d4d5726
JH
378 case DIFF_STATUS_UNMERGED:
379 d->stagemask = unmerged_mask(p->two->path);
380 break;
50b7e70f 381 }
6e458bf6 382 }
c91f0d92
JK
383}
384
50b7e70f 385static void wt_status_collect_changes_worktree(struct wt_status *s)
c91f0d92
JK
386{
387 struct rev_info rev;
50b7e70f
JH
388
389 init_revisions(&rev, NULL);
390 setup_revisions(0, NULL, &rev, NULL);
391 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
9297f77e 392 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
3bfc4504
JL
393 if (!s->show_untracked_files)
394 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
aee9c7d6
JL
395 if (s->ignore_submodule_arg) {
396 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
46a958b3 397 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
c4c42f2c 398 }
50b7e70f
JH
399 rev.diffopt.format_callback = wt_status_collect_changed_cb;
400 rev.diffopt.format_callback_data = s;
afe069d1 401 init_pathspec(&rev.prune_data, s->pathspec);
50b7e70f
JH
402 run_diff_files(&rev, 0);
403}
404
405static void wt_status_collect_changes_index(struct wt_status *s)
406{
407 struct rev_info rev;
32962c9b 408 struct setup_revision_opt opt;
50b7e70f 409
c91f0d92 410 init_revisions(&rev, NULL);
32962c9b
JH
411 memset(&opt, 0, sizeof(opt));
412 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
413 setup_revisions(0, NULL, &rev, &opt);
414
aee9c7d6
JL
415 if (s->ignore_submodule_arg) {
416 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
46a958b3 417 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
aee9c7d6 418 }
46a958b3 419
c91f0d92 420 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
50b7e70f 421 rev.diffopt.format_callback = wt_status_collect_updated_cb;
c91f0d92
JK
422 rev.diffopt.format_callback_data = s;
423 rev.diffopt.detect_rename = 1;
50705915 424 rev.diffopt.rename_limit = 200;
f714fb84 425 rev.diffopt.break_opt = 0;
afe069d1 426 init_pathspec(&rev.prune_data, s->pathspec);
c91f0d92
JK
427 run_diff_index(&rev, 1);
428}
429
50b7e70f
JH
430static void wt_status_collect_changes_initial(struct wt_status *s)
431{
eb9cb55b 432 struct pathspec pathspec;
50b7e70f
JH
433 int i;
434
eb9cb55b 435 init_pathspec(&pathspec, s->pathspec);
50b7e70f
JH
436 for (i = 0; i < active_nr; i++) {
437 struct string_list_item *it;
438 struct wt_status_change_data *d;
439 struct cache_entry *ce = active_cache[i];
440
eb9cb55b 441 if (!ce_path_match(ce, &pathspec))
76e2f7ce 442 continue;
78a395d3 443 it = string_list_insert(&s->change, ce->name);
50b7e70f
JH
444 d = it->util;
445 if (!d) {
446 d = xcalloc(1, sizeof(*d));
447 it->util = d;
448 }
4d4d5726 449 if (ce_stage(ce)) {
50b7e70f 450 d->index_status = DIFF_STATUS_UNMERGED;
4d4d5726
JH
451 d->stagemask |= (1 << (ce_stage(ce) - 1));
452 }
50b7e70f
JH
453 else
454 d->index_status = DIFF_STATUS_ADDED;
455 }
eb9cb55b 456 free_pathspec(&pathspec);
50b7e70f
JH
457}
458
76378683
JH
459static void wt_status_collect_untracked(struct wt_status *s)
460{
461 int i;
462 struct dir_struct dir;
463
464 if (!s->show_untracked_files)
465 return;
466 memset(&dir, 0, sizeof(dir));
467 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
468 dir.flags |=
469 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
470 setup_standard_excludes(&dir);
471
688cd6d2 472 fill_directory(&dir, s->pathspec);
eeefa7c9 473 for (i = 0; i < dir.nr; i++) {
76378683 474 struct dir_entry *ent = dir.entries[i];
b822423e
BC
475 if (cache_name_is_other(ent->name, ent->len) &&
476 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
477 string_list_insert(&s->untracked, ent->name);
f5b26b1d 478 free(ent);
76378683 479 }
f5b26b1d 480
6cb3f6b2
JH
481 if (s->show_ignored_files) {
482 dir.nr = 0;
483 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
484 fill_directory(&dir, s->pathspec);
485 for (i = 0; i < dir.nr; i++) {
486 struct dir_entry *ent = dir.entries[i];
b822423e
BC
487 if (cache_name_is_other(ent->name, ent->len) &&
488 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
489 string_list_insert(&s->ignored, ent->name);
6cb3f6b2
JH
490 free(ent);
491 }
492 }
493
f5b26b1d 494 free(dir.entries);
76378683
JH
495}
496
497void wt_status_collect(struct wt_status *s)
50b7e70f
JH
498{
499 wt_status_collect_changes_worktree(s);
500
501 if (s->is_initial)
502 wt_status_collect_changes_initial(s);
503 else
504 wt_status_collect_changes_index(s);
76378683 505 wt_status_collect_untracked(s);
50b7e70f
JH
506}
507
4d4d5726
JH
508static void wt_status_print_unmerged(struct wt_status *s)
509{
510 int shown_header = 0;
511 int i;
512
513 for (i = 0; i < s->change.nr; i++) {
514 struct wt_status_change_data *d;
515 struct string_list_item *it;
516 it = &(s->change.items[i]);
517 d = it->util;
518 if (!d->stagemask)
519 continue;
520 if (!shown_header) {
521 wt_status_print_unmerged_header(s);
522 shown_header = 1;
523 }
524 wt_status_print_unmerged_data(s, it);
525 }
526 if (shown_header)
527 wt_status_print_trailer(s);
528
529}
530
50b7e70f
JH
531static void wt_status_print_updated(struct wt_status *s)
532{
533 int shown_header = 0;
534 int i;
535
536 for (i = 0; i < s->change.nr; i++) {
537 struct wt_status_change_data *d;
538 struct string_list_item *it;
539 it = &(s->change.items[i]);
540 d = it->util;
541 if (!d->index_status ||
542 d->index_status == DIFF_STATUS_UNMERGED)
543 continue;
544 if (!shown_header) {
545 wt_status_print_cached_header(s);
546 s->commitable = 1;
547 shown_header = 1;
548 }
549 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
550 }
551 if (shown_header)
552 wt_status_print_trailer(s);
553}
554
555/*
556 * -1 : has delete
557 * 0 : no change
558 * 1 : some change but no delete
559 */
9297f77e
JL
560static int wt_status_check_worktree_changes(struct wt_status *s,
561 int *dirty_submodules)
50b7e70f
JH
562{
563 int i;
564 int changes = 0;
565
9297f77e
JL
566 *dirty_submodules = 0;
567
50b7e70f
JH
568 for (i = 0; i < s->change.nr; i++) {
569 struct wt_status_change_data *d;
570 d = s->change.items[i].util;
4d4d5726
JH
571 if (!d->worktree_status ||
572 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f 573 continue;
9297f77e
JL
574 if (!changes)
575 changes = 1;
576 if (d->dirty_submodule)
577 *dirty_submodules = 1;
50b7e70f 578 if (d->worktree_status == DIFF_STATUS_DELETED)
9297f77e 579 changes = -1;
50b7e70f
JH
580 }
581 return changes;
582}
583
c91f0d92
JK
584static void wt_status_print_changed(struct wt_status *s)
585{
9297f77e
JL
586 int i, dirty_submodules;
587 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
50b7e70f
JH
588
589 if (!worktree_changes)
590 return;
591
9297f77e 592 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
50b7e70f
JH
593
594 for (i = 0; i < s->change.nr; i++) {
595 struct wt_status_change_data *d;
596 struct string_list_item *it;
597 it = &(s->change.items[i]);
598 d = it->util;
4d4d5726
JH
599 if (!d->worktree_status ||
600 d->worktree_status == DIFF_STATUS_UNMERGED)
50b7e70f
JH
601 continue;
602 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
603 }
604 wt_status_print_trailer(s);
c91f0d92
JK
605}
606
f17a5d34 607static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
ac8d5afc
PY
608{
609 struct child_process sm_summary;
610 char summary_limit[64];
611 char index[PATH_MAX];
66dbfd55
GV
612 const char *env[] = { NULL, NULL };
613 const char *argv[8];
614
615 env[0] = index;
616 argv[0] = "submodule";
617 argv[1] = "summary";
618 argv[2] = uncommitted ? "--files" : "--cached";
619 argv[3] = "--for-status";
620 argv[4] = "--summary-limit";
621 argv[5] = summary_limit;
622 argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
623 argv[7] = NULL;
ac8d5afc 624
d249b098 625 sprintf(summary_limit, "%d", s->submodule_summary);
ac8d5afc
PY
626 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
627
628 memset(&sm_summary, 0, sizeof(sm_summary));
629 sm_summary.argv = argv;
630 sm_summary.env = env;
631 sm_summary.git_cmd = 1;
632 sm_summary.no_stdin = 1;
633 fflush(s->fp);
634 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
635 run_command(&sm_summary);
636}
637
1b908b6f
JH
638static void wt_status_print_other(struct wt_status *s,
639 struct string_list *l,
640 const char *what,
641 const char *how)
c91f0d92 642{
c91f0d92 643 int i;
f285a2d7 644 struct strbuf buf = STRBUF_INIT;
323d0530
NTND
645 static struct string_list output = STRING_LIST_INIT_DUP;
646 struct column_options copts;
c91f0d92 647
1282988b 648 if (!l->nr)
76378683 649 return;
c91f0d92 650
1b908b6f
JH
651 wt_status_print_other_header(s, what, how);
652
653 for (i = 0; i < l->nr; i++) {
76378683 654 struct string_list_item *it;
323d0530 655 const char *path;
1b908b6f 656 it = &(l->items[i]);
323d0530
NTND
657 path = quote_path(it->string, strlen(it->string),
658 &buf, s->prefix);
659 if (column_active(s->colopts)) {
660 string_list_append(&output, path);
661 continue;
662 }
b926c0d1
JN
663 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
664 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
323d0530 665 "%s\n", path);
c91f0d92 666 }
323d0530
NTND
667
668 strbuf_release(&buf);
669 if (!column_active(s->colopts))
670 return;
671
672 strbuf_addf(&buf, "%s#\t%s",
673 color(WT_STATUS_HEADER, s),
674 color(WT_STATUS_UNTRACKED, s));
675 memset(&copts, 0, sizeof(copts));
676 copts.padding = 1;
677 copts.indent = buf.buf;
678 if (want_color(s->use_color))
679 copts.nl = GIT_COLOR_RESET "\n";
680 print_columns(&output, s->colopts, &copts);
681 string_list_clear(&output, 0);
367c9886 682 strbuf_release(&buf);
c91f0d92
JK
683}
684
685static void wt_status_print_verbose(struct wt_status *s)
686{
687 struct rev_info rev;
32962c9b 688 struct setup_revision_opt opt;
99a12694 689
c91f0d92 690 init_revisions(&rev, NULL);
5ec11af6 691 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
32962c9b
JH
692
693 memset(&opt, 0, sizeof(opt));
694 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
695 setup_revisions(0, NULL, &rev, &opt);
696
c91f0d92
JK
697 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
698 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
699 rev.diffopt.file = s->fp;
700 rev.diffopt.close_file = 0;
4f672ad6
JK
701 /*
702 * If we're not going to stdout, then we definitely don't
703 * want color, since we are going to the commit message
704 * file (and even the "auto" setting won't work, since it
705 * will have checked isatty on stdout).
706 */
707 if (s->fp != stdout)
f1c96261 708 rev.diffopt.use_color = 0;
c91f0d92
JK
709 run_diff_index(&rev, 1);
710}
711
b6975ab5
JH
712static void wt_status_print_tracking(struct wt_status *s)
713{
714 struct strbuf sb = STRBUF_INIT;
715 const char *cp, *ep;
716 struct branch *branch;
717
718 assert(s->branch && !s->is_initial);
719 if (prefixcmp(s->branch, "refs/heads/"))
720 return;
721 branch = branch_get(s->branch + 11);
722 if (!format_tracking_info(branch, &sb))
723 return;
724
725 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
d249b098 726 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
b6975ab5 727 "# %.*s", (int)(ep - cp), cp);
d249b098 728 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
b6975ab5
JH
729}
730
c91f0d92
JK
731void wt_status_print(struct wt_status *s)
732{
1d282327
AA
733 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
734 const char *branch_status_color = color(WT_STATUS_HEADER, s);
98bf8a47 735
bda324cf 736 if (s->branch) {
355ec7a1 737 const char *on_what = _("On branch ");
bda324cf 738 const char *branch_name = s->branch;
cc44c765 739 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
740 branch_name += 11;
741 else if (!strcmp(branch_name, "HEAD")) {
742 branch_name = "";
1d282327 743 branch_status_color = color(WT_STATUS_NOBRANCH, s);
355ec7a1 744 on_what = _("Not currently on any branch.");
bda324cf 745 }
b926c0d1
JN
746 status_printf(s, color(WT_STATUS_HEADER, s), "");
747 status_printf_more(s, branch_status_color, "%s", on_what);
748 status_printf_more(s, branch_color, "%s\n", branch_name);
b6975ab5
JH
749 if (!s->is_initial)
750 wt_status_print_tracking(s);
bda324cf 751 }
c91f0d92
JK
752
753 if (s->is_initial) {
b926c0d1 754 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
b3b298af 755 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
b926c0d1 756 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
c91f0d92
JK
757 }
758
c1e255b7 759 wt_status_print_updated(s);
228e7b5d 760 wt_status_print_unmerged(s);
c91f0d92 761 wt_status_print_changed(s);
46a958b3
JL
762 if (s->submodule_summary &&
763 (!s->ignore_submodule_arg ||
764 strcmp(s->ignore_submodule_arg, "all"))) {
f17a5d34
JL
765 wt_status_print_submodule_summary(s, 0); /* staged */
766 wt_status_print_submodule_summary(s, 1); /* unstaged */
767 }
2381e39e 768 if (s->show_untracked_files) {
355ec7a1 769 wt_status_print_other(s, &s->untracked, _("Untracked"), "add");
2381e39e 770 if (s->show_ignored_files)
355ec7a1 771 wt_status_print_other(s, &s->ignored, _("Ignored"), "add -f");
2381e39e 772 } else if (s->commitable)
355ec7a1 773 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
980bde38 774 advice_status_hints
355ec7a1 775 ? _(" (use -u option to show untracked files)") : "");
c91f0d92 776
1324fb6f 777 if (s->verbose)
c91f0d92 778 wt_status_print_verbose(s);
6e458bf6
JR
779 if (!s->commitable) {
780 if (s->amend)
355ec7a1 781 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
37d07f8f
JH
782 else if (s->nowarn)
783 ; /* nothing */
2a3a3c24 784 else if (s->workdir_dirty)
355ec7a1 785 printf(_("no changes added to commit%s\n"),
980bde38 786 advice_status_hints
355ec7a1 787 ? _(" (use \"git add\" and/or \"git commit -a\")") : "");
76378683 788 else if (s->untracked.nr)
355ec7a1 789 printf(_("nothing added to commit but untracked files present%s\n"),
980bde38 790 advice_status_hints
355ec7a1 791 ? _(" (use \"git add\" to track)") : "");
2a3a3c24 792 else if (s->is_initial)
8ec9bc0d
ÆAB
793 printf(_("nothing to commit%s\n"), advice_status_hints
794 ? _(" (create/copy files and use \"git add\" to track)") : "");
d249b098 795 else if (!s->show_untracked_files)
8ec9bc0d
ÆAB
796 printf(_("nothing to commit%s\n"), advice_status_hints
797 ? _(" (use -u to show untracked files)") : "");
2a3a3c24 798 else
8ec9bc0d
ÆAB
799 printf(_("nothing to commit%s\n"), advice_status_hints
800 ? _(" (working directory clean)") : "");
6e458bf6 801 }
c91f0d92 802}
84dbe7b8 803
3207a3a2 804static void wt_shortstatus_unmerged(struct string_list_item *it,
84dbe7b8
MG
805 struct wt_status *s)
806{
807 struct wt_status_change_data *d = it->util;
808 const char *how = "??";
809
810 switch (d->stagemask) {
811 case 1: how = "DD"; break; /* both deleted */
812 case 2: how = "AU"; break; /* added by us */
813 case 3: how = "UD"; break; /* deleted by them */
814 case 4: how = "UA"; break; /* added by them */
815 case 5: how = "DU"; break; /* deleted by us */
816 case 6: how = "AA"; break; /* both added */
817 case 7: how = "UU"; break; /* both modified */
818 }
3fe2a894 819 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
3207a3a2 820 if (s->null_termination) {
3fe2a894 821 fprintf(stdout, " %s%c", it->string, 0);
84dbe7b8
MG
822 } else {
823 struct strbuf onebuf = STRBUF_INIT;
824 const char *one;
825 one = quote_path(it->string, -1, &onebuf, s->prefix);
3fe2a894 826 printf(" %s\n", one);
84dbe7b8
MG
827 strbuf_release(&onebuf);
828 }
829}
830
3207a3a2 831static void wt_shortstatus_status(struct string_list_item *it,
84dbe7b8
MG
832 struct wt_status *s)
833{
834 struct wt_status_change_data *d = it->util;
835
3fe2a894
MG
836 if (d->index_status)
837 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
838 else
839 putchar(' ');
840 if (d->worktree_status)
841 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
842 else
843 putchar(' ');
844 putchar(' ');
3207a3a2 845 if (s->null_termination) {
84dbe7b8
MG
846 fprintf(stdout, "%s%c", it->string, 0);
847 if (d->head_path)
848 fprintf(stdout, "%s%c", d->head_path, 0);
849 } else {
850 struct strbuf onebuf = STRBUF_INIT;
851 const char *one;
852 if (d->head_path) {
853 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
dbfdc625
KB
854 if (*one != '"' && strchr(one, ' ') != NULL) {
855 putchar('"');
856 strbuf_addch(&onebuf, '"');
857 one = onebuf.buf;
858 }
84dbe7b8
MG
859 printf("%s -> ", one);
860 strbuf_release(&onebuf);
861 }
862 one = quote_path(it->string, -1, &onebuf, s->prefix);
dbfdc625
KB
863 if (*one != '"' && strchr(one, ' ') != NULL) {
864 putchar('"');
865 strbuf_addch(&onebuf, '"');
866 one = onebuf.buf;
867 }
84dbe7b8
MG
868 printf("%s\n", one);
869 strbuf_release(&onebuf);
870 }
871}
872
3207a3a2 873static void wt_shortstatus_other(struct string_list_item *it,
2381e39e 874 struct wt_status *s, const char *sign)
84dbe7b8 875{
3207a3a2 876 if (s->null_termination) {
2381e39e 877 fprintf(stdout, "%s %s%c", sign, it->string, 0);
84dbe7b8
MG
878 } else {
879 struct strbuf onebuf = STRBUF_INIT;
880 const char *one;
881 one = quote_path(it->string, -1, &onebuf, s->prefix);
c1909e72 882 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
3fe2a894 883 printf(" %s\n", one);
84dbe7b8
MG
884 strbuf_release(&onebuf);
885 }
886}
887
05a59a08
DKF
888static void wt_shortstatus_print_tracking(struct wt_status *s)
889{
890 struct branch *branch;
891 const char *header_color = color(WT_STATUS_HEADER, s);
892 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
893 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
894
895 const char *base;
896 const char *branch_name;
897 int num_ours, num_theirs;
898
899 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
900
901 if (!s->branch)
902 return;
903 branch_name = s->branch;
904
905 if (!prefixcmp(branch_name, "refs/heads/"))
906 branch_name += 11;
907 else if (!strcmp(branch_name, "HEAD")) {
98f5e24b 908 branch_name = _("HEAD (no branch)");
05a59a08
DKF
909 branch_color_local = color(WT_STATUS_NOBRANCH, s);
910 }
911
912 branch = branch_get(s->branch + 11);
913 if (s->is_initial)
98f5e24b 914 color_fprintf(s->fp, header_color, _("Initial commit on "));
05a59a08 915 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
a5985237
JK
916 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
917 fputc(s->null_termination ? '\0' : '\n', s->fp);
05a59a08
DKF
918 return;
919 }
920
921 base = branch->merge[0]->dst;
922 base = shorten_unambiguous_ref(base, 0);
923 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
924 color_fprintf(s->fp, header_color, "...");
925 color_fprintf(s->fp, branch_color_remote, "%s", base);
926
927 color_fprintf(s->fp, header_color, " [");
928 if (!num_ours) {
98f5e24b 929 color_fprintf(s->fp, header_color, _("behind "));
05a59a08
DKF
930 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
931 } else if (!num_theirs) {
98f5e24b 932 color_fprintf(s->fp, header_color, _("ahead "));
05a59a08
DKF
933 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
934 } else {
98f5e24b 935 color_fprintf(s->fp, header_color, _("ahead "));
05a59a08 936 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
98f5e24b 937 color_fprintf(s->fp, header_color, _(", behind "));
05a59a08
DKF
938 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
939 }
940
a5985237
JK
941 color_fprintf(s->fp, header_color, "]");
942 fputc(s->null_termination ? '\0' : '\n', s->fp);
05a59a08
DKF
943}
944
d4a6bf1f 945void wt_shortstatus_print(struct wt_status *s)
84dbe7b8
MG
946{
947 int i;
05a59a08 948
d4a6bf1f 949 if (s->show_branch)
05a59a08
DKF
950 wt_shortstatus_print_tracking(s);
951
84dbe7b8
MG
952 for (i = 0; i < s->change.nr; i++) {
953 struct wt_status_change_data *d;
954 struct string_list_item *it;
955
956 it = &(s->change.items[i]);
957 d = it->util;
958 if (d->stagemask)
3207a3a2 959 wt_shortstatus_unmerged(it, s);
84dbe7b8 960 else
3207a3a2 961 wt_shortstatus_status(it, s);
84dbe7b8
MG
962 }
963 for (i = 0; i < s->untracked.nr; i++) {
964 struct string_list_item *it;
965
966 it = &(s->untracked.items[i]);
3207a3a2 967 wt_shortstatus_other(it, s, "??");
2381e39e
JH
968 }
969 for (i = 0; i < s->ignored.nr; i++) {
970 struct string_list_item *it;
971
972 it = &(s->ignored.items[i]);
3207a3a2 973 wt_shortstatus_other(it, s, "!!");
84dbe7b8
MG
974 }
975}
4a7cc2fd 976
3207a3a2 977void wt_porcelain_print(struct wt_status *s)
4a7cc2fd
JK
978{
979 s->use_color = 0;
8661768f
JK
980 s->relative_paths = 0;
981 s->prefix = NULL;
d4a6bf1f 982 wt_shortstatus_print(s);
4a7cc2fd 983}