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