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