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