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