1#include "cache.h"
2#include "wt-status.h"
3#include "object.h"
4#include "dir.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
8#include "diffcore.h"
9#include "quote.h"
10#include "run-command.h"
11#include "argv-array.h"
12#include "remote.h"
13#include "refs.h"
14#include "submodule.h"
15#include "column.h"
16#include "strbuf.h"
17#include "utf8.h"
18#include "worktree.h"
19#include "lockfile.h"
20
21static const char cut_line[] =
22"------------------------ >8 ------------------------\n";
23
24static char default_wt_status_colors[][COLOR_MAXLEN] = {
25 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
30 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
31 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
33 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
34};
35
36static const char *color(int slot, struct wt_status *s)
37{
38 const char *c = "";
39 if (want_color(s->use_color))
40 c = s->color_palette[slot];
41 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
42 c = s->color_palette[WT_STATUS_HEADER];
43 return c;
44}
45
46static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
47 const char *fmt, va_list ap, const char *trail)
48{
49 struct strbuf sb = STRBUF_INIT;
50 struct strbuf linebuf = STRBUF_INIT;
51 const char *line, *eol;
52
53 strbuf_vaddf(&sb, fmt, ap);
54 if (!sb.len) {
55 if (s->display_comment_prefix) {
56 strbuf_addch(&sb, comment_line_char);
57 if (!trail)
58 strbuf_addch(&sb, ' ');
59 }
60 color_print_strbuf(s->fp, color, &sb);
61 if (trail)
62 fprintf(s->fp, "%s", trail);
63 strbuf_release(&sb);
64 return;
65 }
66 for (line = sb.buf; *line; line = eol + 1) {
67 eol = strchr(line, '\n');
68
69 strbuf_reset(&linebuf);
70 if (at_bol && s->display_comment_prefix) {
71 strbuf_addch(&linebuf, comment_line_char);
72 if (*line != '\n' && *line != '\t')
73 strbuf_addch(&linebuf, ' ');
74 }
75 if (eol)
76 strbuf_add(&linebuf, line, eol - line);
77 else
78 strbuf_addstr(&linebuf, line);
79 color_print_strbuf(s->fp, color, &linebuf);
80 if (eol)
81 fprintf(s->fp, "\n");
82 else
83 break;
84 at_bol = 1;
85 }
86 if (trail)
87 fprintf(s->fp, "%s", trail);
88 strbuf_release(&linebuf);
89 strbuf_release(&sb);
90}
91
92void status_printf_ln(struct wt_status *s, const char *color,
93 const char *fmt, ...)
94{
95 va_list ap;
96
97 va_start(ap, fmt);
98 status_vprintf(s, 1, color, fmt, ap, "\n");
99 va_end(ap);
100}
101
102void status_printf(struct wt_status *s, const char *color,
103 const char *fmt, ...)
104{
105 va_list ap;
106
107 va_start(ap, fmt);
108 status_vprintf(s, 1, color, fmt, ap, NULL);
109 va_end(ap);
110}
111
112static void status_printf_more(struct wt_status *s, const char *color,
113 const char *fmt, ...)
114{
115 va_list ap;
116
117 va_start(ap, fmt);
118 status_vprintf(s, 0, color, fmt, ap, NULL);
119 va_end(ap);
120}
121
122void wt_status_prepare(struct wt_status *s)
123{
124 struct object_id oid;
125
126 memset(s, 0, sizeof(*s));
127 memcpy(s->color_palette, default_wt_status_colors,
128 sizeof(default_wt_status_colors));
129 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
130 s->use_color = -1;
131 s->relative_paths = 1;
132 s->branch = resolve_refdup("HEAD", 0, oid.hash, NULL);
133 s->reference = "HEAD";
134 s->fp = stdout;
135 s->index_file = get_index_file();
136 s->change.strdup_strings = 1;
137 s->untracked.strdup_strings = 1;
138 s->ignored.strdup_strings = 1;
139 s->show_branch = -1; /* unspecified */
140 s->display_comment_prefix = 0;
141}
142
143static void wt_longstatus_print_unmerged_header(struct wt_status *s)
144{
145 int i;
146 int del_mod_conflict = 0;
147 int both_deleted = 0;
148 int not_deleted = 0;
149 const char *c = color(WT_STATUS_HEADER, s);
150
151 status_printf_ln(s, c, _("Unmerged paths:"));
152
153 for (i = 0; i < s->change.nr; i++) {
154 struct string_list_item *it = &(s->change.items[i]);
155 struct wt_status_change_data *d = it->util;
156
157 switch (d->stagemask) {
158 case 0:
159 break;
160 case 1:
161 both_deleted = 1;
162 break;
163 case 3:
164 case 5:
165 del_mod_conflict = 1;
166 break;
167 default:
168 not_deleted = 1;
169 break;
170 }
171 }
172
173 if (!s->hints)
174 return;
175 if (s->whence != FROM_COMMIT)
176 ;
177 else if (!s->is_initial)
178 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
179 else
180 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
181
182 if (!both_deleted) {
183 if (!del_mod_conflict)
184 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
185 else
186 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
187 } else if (!del_mod_conflict && !not_deleted) {
188 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
189 } else {
190 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
191 }
192 status_printf_ln(s, c, "%s", "");
193}
194
195static void wt_longstatus_print_cached_header(struct wt_status *s)
196{
197 const char *c = color(WT_STATUS_HEADER, s);
198
199 status_printf_ln(s, c, _("Changes to be committed:"));
200 if (!s->hints)
201 return;
202 if (s->whence != FROM_COMMIT)
203 ; /* NEEDSWORK: use "git reset --unresolve"??? */
204 else if (!s->is_initial)
205 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
206 else
207 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
208 status_printf_ln(s, c, "%s", "");
209}
210
211static void wt_longstatus_print_dirty_header(struct wt_status *s,
212 int has_deleted,
213 int has_dirty_submodules)
214{
215 const char *c = color(WT_STATUS_HEADER, s);
216
217 status_printf_ln(s, c, _("Changes not staged for commit:"));
218 if (!s->hints)
219 return;
220 if (!has_deleted)
221 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
222 else
223 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
224 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
225 if (has_dirty_submodules)
226 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
227 status_printf_ln(s, c, "%s", "");
228}
229
230static void wt_longstatus_print_other_header(struct wt_status *s,
231 const char *what,
232 const char *how)
233{
234 const char *c = color(WT_STATUS_HEADER, s);
235 status_printf_ln(s, c, "%s:", what);
236 if (!s->hints)
237 return;
238 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
239 status_printf_ln(s, c, "%s", "");
240}
241
242static void wt_longstatus_print_trailer(struct wt_status *s)
243{
244 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
245}
246
247#define quote_path quote_path_relative
248
249static const char *wt_status_unmerged_status_string(int stagemask)
250{
251 switch (stagemask) {
252 case 1:
253 return _("both deleted:");
254 case 2:
255 return _("added by us:");
256 case 3:
257 return _("deleted by them:");
258 case 4:
259 return _("added by them:");
260 case 5:
261 return _("deleted by us:");
262 case 6:
263 return _("both added:");
264 case 7:
265 return _("both modified:");
266 default:
267 die("BUG: unhandled unmerged status %x", stagemask);
268 }
269}
270
271static const char *wt_status_diff_status_string(int status)
272{
273 switch (status) {
274 case DIFF_STATUS_ADDED:
275 return _("new file:");
276 case DIFF_STATUS_COPIED:
277 return _("copied:");
278 case DIFF_STATUS_DELETED:
279 return _("deleted:");
280 case DIFF_STATUS_MODIFIED:
281 return _("modified:");
282 case DIFF_STATUS_RENAMED:
283 return _("renamed:");
284 case DIFF_STATUS_TYPE_CHANGED:
285 return _("typechange:");
286 case DIFF_STATUS_UNKNOWN:
287 return _("unknown:");
288 case DIFF_STATUS_UNMERGED:
289 return _("unmerged:");
290 default:
291 return NULL;
292 }
293}
294
295static int maxwidth(const char *(*label)(int), int minval, int maxval)
296{
297 int result = 0, i;
298
299 for (i = minval; i <= maxval; i++) {
300 const char *s = label(i);
301 int len = s ? utf8_strwidth(s) : 0;
302 if (len > result)
303 result = len;
304 }
305 return result;
306}
307
308static void wt_longstatus_print_unmerged_data(struct wt_status *s,
309 struct string_list_item *it)
310{
311 const char *c = color(WT_STATUS_UNMERGED, s);
312 struct wt_status_change_data *d = it->util;
313 struct strbuf onebuf = STRBUF_INIT;
314 static char *padding;
315 static int label_width;
316 const char *one, *how;
317 int len;
318
319 if (!padding) {
320 label_width = maxwidth(wt_status_unmerged_status_string, 1, 7);
321 label_width += strlen(" ");
322 padding = xmallocz(label_width);
323 memset(padding, ' ', label_width);
324 }
325
326 one = quote_path(it->string, s->prefix, &onebuf);
327 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
328
329 how = wt_status_unmerged_status_string(d->stagemask);
330 len = label_width - utf8_strwidth(how);
331 status_printf_more(s, c, "%s%.*s%s\n", how, len, padding, one);
332 strbuf_release(&onebuf);
333}
334
335static void wt_longstatus_print_change_data(struct wt_status *s,
336 int change_type,
337 struct string_list_item *it)
338{
339 struct wt_status_change_data *d = it->util;
340 const char *c = color(change_type, s);
341 int status;
342 char *one_name;
343 char *two_name;
344 const char *one, *two;
345 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
346 struct strbuf extra = STRBUF_INIT;
347 static char *padding;
348 static int label_width;
349 const char *what;
350 int len;
351
352 if (!padding) {
353 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
354 label_width = maxwidth(wt_status_diff_status_string, 'A', 'Z');
355 label_width += strlen(" ");
356 padding = xmallocz(label_width);
357 memset(padding, ' ', label_width);
358 }
359
360 one_name = two_name = it->string;
361 switch (change_type) {
362 case WT_STATUS_UPDATED:
363 status = d->index_status;
364 break;
365 case WT_STATUS_CHANGED:
366 if (d->new_submodule_commits || d->dirty_submodule) {
367 strbuf_addstr(&extra, " (");
368 if (d->new_submodule_commits)
369 strbuf_addstr(&extra, _("new commits, "));
370 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
371 strbuf_addstr(&extra, _("modified content, "));
372 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
373 strbuf_addstr(&extra, _("untracked content, "));
374 strbuf_setlen(&extra, extra.len - 2);
375 strbuf_addch(&extra, ')');
376 }
377 status = d->worktree_status;
378 break;
379 default:
380 die("BUG: unhandled change_type %d in wt_longstatus_print_change_data",
381 change_type);
382 }
383
384 /*
385 * Only pick up the rename it's relevant. If the rename is for
386 * the changed section and we're printing the updated section,
387 * ignore it.
388 */
389 if (d->rename_status == status)
390 one_name = d->rename_source;
391
392 one = quote_path(one_name, s->prefix, &onebuf);
393 two = quote_path(two_name, s->prefix, &twobuf);
394
395 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
396 what = wt_status_diff_status_string(status);
397 if (!what)
398 die("BUG: unhandled diff status %c", status);
399 len = label_width - utf8_strwidth(what);
400 assert(len >= 0);
401 if (one_name != two_name)
402 status_printf_more(s, c, "%s%.*s%s -> %s",
403 what, len, padding, one, two);
404 else
405 status_printf_more(s, c, "%s%.*s%s",
406 what, len, padding, one);
407 if (extra.len) {
408 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
409 strbuf_release(&extra);
410 }
411 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
412 strbuf_release(&onebuf);
413 strbuf_release(&twobuf);
414}
415
416static char short_submodule_status(struct wt_status_change_data *d)
417{
418 if (d->new_submodule_commits)
419 return 'M';
420 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
421 return 'm';
422 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
423 return '?';
424 return d->worktree_status;
425}
426
427static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
428 struct diff_options *options,
429 void *data)
430{
431 struct wt_status *s = data;
432 int i;
433
434 if (!q->nr)
435 return;
436 s->workdir_dirty = 1;
437 for (i = 0; i < q->nr; i++) {
438 struct diff_filepair *p;
439 struct string_list_item *it;
440 struct wt_status_change_data *d;
441
442 p = q->queue[i];
443 it = string_list_insert(&s->change, p->two->path);
444 d = it->util;
445 if (!d) {
446 d = xcalloc(1, sizeof(*d));
447 it->util = d;
448 }
449 if (!d->worktree_status)
450 d->worktree_status = p->status;
451 if (S_ISGITLINK(p->two->mode)) {
452 d->dirty_submodule = p->two->dirty_submodule;
453 d->new_submodule_commits = !!oidcmp(&p->one->oid,
454 &p->two->oid);
455 if (s->status_format == STATUS_FORMAT_SHORT)
456 d->worktree_status = short_submodule_status(d);
457 }
458
459 switch (p->status) {
460 case DIFF_STATUS_ADDED:
461 d->mode_worktree = p->two->mode;
462 break;
463
464 case DIFF_STATUS_DELETED:
465 d->mode_index = p->one->mode;
466 oidcpy(&d->oid_index, &p->one->oid);
467 /* mode_worktree is zero for a delete. */
468 break;
469
470 case DIFF_STATUS_COPIED:
471 case DIFF_STATUS_RENAMED:
472 if (d->rename_status)
473 die("BUG: multiple renames on the same target? how?");
474 d->rename_source = xstrdup(p->one->path);
475 d->rename_score = p->score * 100 / MAX_SCORE;
476 d->rename_status = p->status;
477 /* fallthru */
478 case DIFF_STATUS_MODIFIED:
479 case DIFF_STATUS_TYPE_CHANGED:
480 case DIFF_STATUS_UNMERGED:
481 d->mode_index = p->one->mode;
482 d->mode_worktree = p->two->mode;
483 oidcpy(&d->oid_index, &p->one->oid);
484 break;
485
486 default:
487 die("BUG: unhandled diff-files status '%c'", p->status);
488 break;
489 }
490
491 }
492}
493
494static int unmerged_mask(const char *path)
495{
496 int pos, mask;
497 const struct cache_entry *ce;
498
499 pos = cache_name_pos(path, strlen(path));
500 if (0 <= pos)
501 return 0;
502
503 mask = 0;
504 pos = -pos-1;
505 while (pos < active_nr) {
506 ce = active_cache[pos++];
507 if (strcmp(ce->name, path) || !ce_stage(ce))
508 break;
509 mask |= (1 << (ce_stage(ce) - 1));
510 }
511 return mask;
512}
513
514static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
515 struct diff_options *options,
516 void *data)
517{
518 struct wt_status *s = data;
519 int i;
520
521 for (i = 0; i < q->nr; i++) {
522 struct diff_filepair *p;
523 struct string_list_item *it;
524 struct wt_status_change_data *d;
525
526 p = q->queue[i];
527 it = string_list_insert(&s->change, p->two->path);
528 d = it->util;
529 if (!d) {
530 d = xcalloc(1, sizeof(*d));
531 it->util = d;
532 }
533 if (!d->index_status)
534 d->index_status = p->status;
535 switch (p->status) {
536 case DIFF_STATUS_ADDED:
537 /* Leave {mode,oid}_head zero for an add. */
538 d->mode_index = p->two->mode;
539 oidcpy(&d->oid_index, &p->two->oid);
540 break;
541 case DIFF_STATUS_DELETED:
542 d->mode_head = p->one->mode;
543 oidcpy(&d->oid_head, &p->one->oid);
544 /* Leave {mode,oid}_index zero for a delete. */
545 break;
546
547 case DIFF_STATUS_COPIED:
548 case DIFF_STATUS_RENAMED:
549 if (d->rename_status)
550 die("BUG: multiple renames on the same target? how?");
551 d->rename_source = xstrdup(p->one->path);
552 d->rename_score = p->score * 100 / MAX_SCORE;
553 d->rename_status = p->status;
554 /* fallthru */
555 case DIFF_STATUS_MODIFIED:
556 case DIFF_STATUS_TYPE_CHANGED:
557 d->mode_head = p->one->mode;
558 d->mode_index = p->two->mode;
559 oidcpy(&d->oid_head, &p->one->oid);
560 oidcpy(&d->oid_index, &p->two->oid);
561 break;
562 case DIFF_STATUS_UNMERGED:
563 d->stagemask = unmerged_mask(p->two->path);
564 /*
565 * Don't bother setting {mode,oid}_{head,index} since the print
566 * code will output the stage values directly and not use the
567 * values in these fields.
568 */
569 break;
570
571 default:
572 die("BUG: unhandled diff-index status '%c'", p->status);
573 break;
574 }
575 }
576}
577
578static void wt_status_collect_changes_worktree(struct wt_status *s)
579{
580 struct rev_info rev;
581
582 init_revisions(&rev, NULL);
583 setup_revisions(0, NULL, &rev, NULL);
584 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
585 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
586 rev.diffopt.ita_invisible_in_index = 1;
587 if (!s->show_untracked_files)
588 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
589 if (s->ignore_submodule_arg) {
590 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
591 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
592 }
593 rev.diffopt.format_callback = wt_status_collect_changed_cb;
594 rev.diffopt.format_callback_data = s;
595 copy_pathspec(&rev.prune_data, &s->pathspec);
596 run_diff_files(&rev, 0);
597}
598
599static void wt_status_collect_changes_index(struct wt_status *s)
600{
601 struct rev_info rev;
602 struct setup_revision_opt opt;
603
604 init_revisions(&rev, NULL);
605 memset(&opt, 0, sizeof(opt));
606 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
607 setup_revisions(0, NULL, &rev, &opt);
608
609 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
610 rev.diffopt.ita_invisible_in_index = 1;
611 if (s->ignore_submodule_arg) {
612 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
613 } else {
614 /*
615 * Unless the user did explicitly request a submodule ignore
616 * mode by passing a command line option we do not ignore any
617 * changed submodule SHA-1s when comparing index and HEAD, no
618 * matter what is configured. Otherwise the user won't be
619 * shown any submodules she manually added (and which are
620 * staged to be committed), which would be really confusing.
621 */
622 handle_ignore_submodules_arg(&rev.diffopt, "dirty");
623 }
624
625 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
626 rev.diffopt.format_callback = wt_status_collect_updated_cb;
627 rev.diffopt.format_callback_data = s;
628 rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
629 rev.diffopt.rename_limit = 200;
630 rev.diffopt.break_opt = 0;
631 copy_pathspec(&rev.prune_data, &s->pathspec);
632 run_diff_index(&rev, 1);
633}
634
635static void wt_status_collect_changes_initial(struct wt_status *s)
636{
637 int i;
638
639 for (i = 0; i < active_nr; i++) {
640 struct string_list_item *it;
641 struct wt_status_change_data *d;
642 const struct cache_entry *ce = active_cache[i];
643
644 if (!ce_path_match(ce, &s->pathspec, NULL))
645 continue;
646 if (ce_intent_to_add(ce))
647 continue;
648 it = string_list_insert(&s->change, ce->name);
649 d = it->util;
650 if (!d) {
651 d = xcalloc(1, sizeof(*d));
652 it->util = d;
653 }
654 if (ce_stage(ce)) {
655 d->index_status = DIFF_STATUS_UNMERGED;
656 d->stagemask |= (1 << (ce_stage(ce) - 1));
657 /*
658 * Don't bother setting {mode,oid}_{head,index} since the print
659 * code will output the stage values directly and not use the
660 * values in these fields.
661 */
662 } else {
663 d->index_status = DIFF_STATUS_ADDED;
664 /* Leave {mode,oid}_head zero for adds. */
665 d->mode_index = ce->ce_mode;
666 oidcpy(&d->oid_index, &ce->oid);
667 }
668 }
669}
670
671static void wt_status_collect_untracked(struct wt_status *s)
672{
673 int i;
674 struct dir_struct dir;
675 uint64_t t_begin = getnanotime();
676
677 if (!s->show_untracked_files)
678 return;
679
680 memset(&dir, 0, sizeof(dir));
681 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
682 dir.flags |=
683 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
684 if (s->show_ignored_files)
685 dir.flags |= DIR_SHOW_IGNORED_TOO;
686 else
687 dir.untracked = the_index.untracked;
688 setup_standard_excludes(&dir);
689
690 fill_directory(&dir, &s->pathspec);
691
692 for (i = 0; i < dir.nr; i++) {
693 struct dir_entry *ent = dir.entries[i];
694 if (cache_name_is_other(ent->name, ent->len) &&
695 dir_path_match(ent, &s->pathspec, 0, NULL))
696 string_list_insert(&s->untracked, ent->name);
697 free(ent);
698 }
699
700 for (i = 0; i < dir.ignored_nr; i++) {
701 struct dir_entry *ent = dir.ignored[i];
702 if (cache_name_is_other(ent->name, ent->len) &&
703 dir_path_match(ent, &s->pathspec, 0, NULL))
704 string_list_insert(&s->ignored, ent->name);
705 free(ent);
706 }
707
708 free(dir.entries);
709 free(dir.ignored);
710 clear_directory(&dir);
711
712 if (advice_status_u_option)
713 s->untracked_in_ms = (getnanotime() - t_begin) / 1000000;
714}
715
716void wt_status_collect(struct wt_status *s)
717{
718 wt_status_collect_changes_worktree(s);
719
720 if (s->is_initial)
721 wt_status_collect_changes_initial(s);
722 else
723 wt_status_collect_changes_index(s);
724 wt_status_collect_untracked(s);
725}
726
727static void wt_longstatus_print_unmerged(struct wt_status *s)
728{
729 int shown_header = 0;
730 int i;
731
732 for (i = 0; i < s->change.nr; i++) {
733 struct wt_status_change_data *d;
734 struct string_list_item *it;
735 it = &(s->change.items[i]);
736 d = it->util;
737 if (!d->stagemask)
738 continue;
739 if (!shown_header) {
740 wt_longstatus_print_unmerged_header(s);
741 shown_header = 1;
742 }
743 wt_longstatus_print_unmerged_data(s, it);
744 }
745 if (shown_header)
746 wt_longstatus_print_trailer(s);
747
748}
749
750static void wt_longstatus_print_updated(struct wt_status *s)
751{
752 int shown_header = 0;
753 int i;
754
755 for (i = 0; i < s->change.nr; i++) {
756 struct wt_status_change_data *d;
757 struct string_list_item *it;
758 it = &(s->change.items[i]);
759 d = it->util;
760 if (!d->index_status ||
761 d->index_status == DIFF_STATUS_UNMERGED)
762 continue;
763 if (!shown_header) {
764 wt_longstatus_print_cached_header(s);
765 s->commitable = 1;
766 shown_header = 1;
767 }
768 wt_longstatus_print_change_data(s, WT_STATUS_UPDATED, it);
769 }
770 if (shown_header)
771 wt_longstatus_print_trailer(s);
772}
773
774/*
775 * -1 : has delete
776 * 0 : no change
777 * 1 : some change but no delete
778 */
779static int wt_status_check_worktree_changes(struct wt_status *s,
780 int *dirty_submodules)
781{
782 int i;
783 int changes = 0;
784
785 *dirty_submodules = 0;
786
787 for (i = 0; i < s->change.nr; i++) {
788 struct wt_status_change_data *d;
789 d = s->change.items[i].util;
790 if (!d->worktree_status ||
791 d->worktree_status == DIFF_STATUS_UNMERGED)
792 continue;
793 if (!changes)
794 changes = 1;
795 if (d->dirty_submodule)
796 *dirty_submodules = 1;
797 if (d->worktree_status == DIFF_STATUS_DELETED)
798 changes = -1;
799 }
800 return changes;
801}
802
803static void wt_longstatus_print_changed(struct wt_status *s)
804{
805 int i, dirty_submodules;
806 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
807
808 if (!worktree_changes)
809 return;
810
811 wt_longstatus_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
812
813 for (i = 0; i < s->change.nr; i++) {
814 struct wt_status_change_data *d;
815 struct string_list_item *it;
816 it = &(s->change.items[i]);
817 d = it->util;
818 if (!d->worktree_status ||
819 d->worktree_status == DIFF_STATUS_UNMERGED)
820 continue;
821 wt_longstatus_print_change_data(s, WT_STATUS_CHANGED, it);
822 }
823 wt_longstatus_print_trailer(s);
824}
825
826static void wt_longstatus_print_submodule_summary(struct wt_status *s, int uncommitted)
827{
828 struct child_process sm_summary = CHILD_PROCESS_INIT;
829 struct strbuf cmd_stdout = STRBUF_INIT;
830 struct strbuf summary = STRBUF_INIT;
831 char *summary_content;
832
833 argv_array_pushf(&sm_summary.env_array, "GIT_INDEX_FILE=%s",
834 s->index_file);
835
836 argv_array_push(&sm_summary.args, "submodule");
837 argv_array_push(&sm_summary.args, "summary");
838 argv_array_push(&sm_summary.args, uncommitted ? "--files" : "--cached");
839 argv_array_push(&sm_summary.args, "--for-status");
840 argv_array_push(&sm_summary.args, "--summary-limit");
841 argv_array_pushf(&sm_summary.args, "%d", s->submodule_summary);
842 if (!uncommitted)
843 argv_array_push(&sm_summary.args, s->amend ? "HEAD^" : "HEAD");
844
845 sm_summary.git_cmd = 1;
846 sm_summary.no_stdin = 1;
847
848 capture_command(&sm_summary, &cmd_stdout, 1024);
849
850 /* prepend header, only if there's an actual output */
851 if (cmd_stdout.len) {
852 if (uncommitted)
853 strbuf_addstr(&summary, _("Submodules changed but not updated:"));
854 else
855 strbuf_addstr(&summary, _("Submodule changes to be committed:"));
856 strbuf_addstr(&summary, "\n\n");
857 }
858 strbuf_addbuf(&summary, &cmd_stdout);
859 strbuf_release(&cmd_stdout);
860
861 if (s->display_comment_prefix) {
862 size_t len;
863 summary_content = strbuf_detach(&summary, &len);
864 strbuf_add_commented_lines(&summary, summary_content, len);
865 free(summary_content);
866 }
867
868 fputs(summary.buf, s->fp);
869 strbuf_release(&summary);
870}
871
872static void wt_longstatus_print_other(struct wt_status *s,
873 struct string_list *l,
874 const char *what,
875 const char *how)
876{
877 int i;
878 struct strbuf buf = STRBUF_INIT;
879 static struct string_list output = STRING_LIST_INIT_DUP;
880 struct column_options copts;
881
882 if (!l->nr)
883 return;
884
885 wt_longstatus_print_other_header(s, what, how);
886
887 for (i = 0; i < l->nr; i++) {
888 struct string_list_item *it;
889 const char *path;
890 it = &(l->items[i]);
891 path = quote_path(it->string, s->prefix, &buf);
892 if (column_active(s->colopts)) {
893 string_list_append(&output, path);
894 continue;
895 }
896 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
897 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
898 "%s\n", path);
899 }
900
901 strbuf_release(&buf);
902 if (!column_active(s->colopts))
903 goto conclude;
904
905 strbuf_addf(&buf, "%s%s\t%s",
906 color(WT_STATUS_HEADER, s),
907 s->display_comment_prefix ? "#" : "",
908 color(WT_STATUS_UNTRACKED, s));
909 memset(&copts, 0, sizeof(copts));
910 copts.padding = 1;
911 copts.indent = buf.buf;
912 if (want_color(s->use_color))
913 copts.nl = GIT_COLOR_RESET "\n";
914 print_columns(&output, s->colopts, &copts);
915 string_list_clear(&output, 0);
916 strbuf_release(&buf);
917conclude:
918 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
919}
920
921size_t wt_status_locate_end(const char *s, size_t len)
922{
923 const char *p;
924 struct strbuf pattern = STRBUF_INIT;
925
926 strbuf_addf(&pattern, "\n%c %s", comment_line_char, cut_line);
927 if (starts_with(s, pattern.buf + 1))
928 len = 0;
929 else if ((p = strstr(s, pattern.buf)))
930 len = p - s + 1;
931 strbuf_release(&pattern);
932 return len;
933}
934
935void wt_status_add_cut_line(FILE *fp)
936{
937 const char *explanation = _("Do not touch the line above.\nEverything below will be removed.");
938 struct strbuf buf = STRBUF_INIT;
939
940 fprintf(fp, "%c %s", comment_line_char, cut_line);
941 strbuf_add_commented_lines(&buf, explanation, strlen(explanation));
942 fputs(buf.buf, fp);
943 strbuf_release(&buf);
944}
945
946static void wt_longstatus_print_verbose(struct wt_status *s)
947{
948 struct rev_info rev;
949 struct setup_revision_opt opt;
950 int dirty_submodules;
951 const char *c = color(WT_STATUS_HEADER, s);
952
953 init_revisions(&rev, NULL);
954 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
955 rev.diffopt.ita_invisible_in_index = 1;
956
957 memset(&opt, 0, sizeof(opt));
958 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
959 setup_revisions(0, NULL, &rev, &opt);
960
961 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
962 rev.diffopt.detect_rename = DIFF_DETECT_RENAME;
963 rev.diffopt.file = s->fp;
964 rev.diffopt.close_file = 0;
965 /*
966 * If we're not going to stdout, then we definitely don't
967 * want color, since we are going to the commit message
968 * file (and even the "auto" setting won't work, since it
969 * will have checked isatty on stdout). But we then do want
970 * to insert the scissor line here to reliably remove the
971 * diff before committing.
972 */
973 if (s->fp != stdout) {
974 rev.diffopt.use_color = 0;
975 wt_status_add_cut_line(s->fp);
976 }
977 if (s->verbose > 1 && s->commitable) {
978 /* print_updated() printed a header, so do we */
979 if (s->fp != stdout)
980 wt_longstatus_print_trailer(s);
981 status_printf_ln(s, c, _("Changes to be committed:"));
982 rev.diffopt.a_prefix = "c/";
983 rev.diffopt.b_prefix = "i/";
984 } /* else use prefix as per user config */
985 run_diff_index(&rev, 1);
986 if (s->verbose > 1 &&
987 wt_status_check_worktree_changes(s, &dirty_submodules)) {
988 status_printf_ln(s, c,
989 "--------------------------------------------------");
990 status_printf_ln(s, c, _("Changes not staged for commit:"));
991 setup_work_tree();
992 rev.diffopt.a_prefix = "i/";
993 rev.diffopt.b_prefix = "w/";
994 run_diff_files(&rev, 0);
995 }
996}
997
998static void wt_longstatus_print_tracking(struct wt_status *s)
999{
1000 struct strbuf sb = STRBUF_INIT;
1001 const char *cp, *ep, *branch_name;
1002 struct branch *branch;
1003 char comment_line_string[3];
1004 int i;
1005
1006 assert(s->branch && !s->is_initial);
1007 if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
1008 return;
1009 branch = branch_get(branch_name);
1010 if (!format_tracking_info(branch, &sb))
1011 return;
1012
1013 i = 0;
1014 if (s->display_comment_prefix) {
1015 comment_line_string[i++] = comment_line_char;
1016 comment_line_string[i++] = ' ';
1017 }
1018 comment_line_string[i] = '\0';
1019
1020 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
1021 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
1022 "%s%.*s", comment_line_string,
1023 (int)(ep - cp), cp);
1024 if (s->display_comment_prefix)
1025 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
1026 comment_line_char);
1027 else
1028 fputs("", s->fp);
1029}
1030
1031static int has_unmerged(struct wt_status *s)
1032{
1033 int i;
1034
1035 for (i = 0; i < s->change.nr; i++) {
1036 struct wt_status_change_data *d;
1037 d = s->change.items[i].util;
1038 if (d->stagemask)
1039 return 1;
1040 }
1041 return 0;
1042}
1043
1044static void show_merge_in_progress(struct wt_status *s,
1045 struct wt_status_state *state,
1046 const char *color)
1047{
1048 if (has_unmerged(s)) {
1049 status_printf_ln(s, color, _("You have unmerged paths."));
1050 if (s->hints) {
1051 status_printf_ln(s, color,
1052 _(" (fix conflicts and run \"git commit\")"));
1053 status_printf_ln(s, color,
1054 _(" (use \"git merge --abort\" to abort the merge)"));
1055 }
1056 } else {
1057 s-> commitable = 1;
1058 status_printf_ln(s, color,
1059 _("All conflicts fixed but you are still merging."));
1060 if (s->hints)
1061 status_printf_ln(s, color,
1062 _(" (use \"git commit\" to conclude merge)"));
1063 }
1064 wt_longstatus_print_trailer(s);
1065}
1066
1067static void show_am_in_progress(struct wt_status *s,
1068 struct wt_status_state *state,
1069 const char *color)
1070{
1071 status_printf_ln(s, color,
1072 _("You are in the middle of an am session."));
1073 if (state->am_empty_patch)
1074 status_printf_ln(s, color,
1075 _("The current patch is empty."));
1076 if (s->hints) {
1077 if (!state->am_empty_patch)
1078 status_printf_ln(s, color,
1079 _(" (fix conflicts and then run \"git am --continue\")"));
1080 status_printf_ln(s, color,
1081 _(" (use \"git am --skip\" to skip this patch)"));
1082 status_printf_ln(s, color,
1083 _(" (use \"git am --abort\" to restore the original branch)"));
1084 }
1085 wt_longstatus_print_trailer(s);
1086}
1087
1088static char *read_line_from_git_path(const char *filename)
1089{
1090 struct strbuf buf = STRBUF_INIT;
1091 FILE *fp = fopen(git_path("%s", filename), "r");
1092 if (!fp) {
1093 strbuf_release(&buf);
1094 return NULL;
1095 }
1096 strbuf_getline_lf(&buf, fp);
1097 if (!fclose(fp)) {
1098 return strbuf_detach(&buf, NULL);
1099 } else {
1100 strbuf_release(&buf);
1101 return NULL;
1102 }
1103}
1104
1105static int split_commit_in_progress(struct wt_status *s)
1106{
1107 int split_in_progress = 0;
1108 char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1109
1110 if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
1111 !s->branch || strcmp(s->branch, "HEAD"))
1112 return 0;
1113
1114 head = read_line_from_git_path("HEAD");
1115 orig_head = read_line_from_git_path("ORIG_HEAD");
1116 rebase_amend = read_line_from_git_path("rebase-merge/amend");
1117 rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
1118
1119 if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1120 ; /* fall through, no split in progress */
1121 else if (!strcmp(rebase_amend, rebase_orig_head))
1122 split_in_progress = !!strcmp(head, rebase_amend);
1123 else if (strcmp(orig_head, rebase_orig_head))
1124 split_in_progress = 1;
1125
1126 free(head);
1127 free(orig_head);
1128 free(rebase_amend);
1129 free(rebase_orig_head);
1130
1131 return split_in_progress;
1132}
1133
1134/*
1135 * Turn
1136 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1137 * into
1138 * "pick d6a2f03 some message"
1139 *
1140 * The function assumes that the line does not contain useless spaces
1141 * before or after the command.
1142 */
1143static void abbrev_sha1_in_line(struct strbuf *line)
1144{
1145 struct strbuf **split;
1146 int i;
1147
1148 if (starts_with(line->buf, "exec ") ||
1149 starts_with(line->buf, "x "))
1150 return;
1151
1152 split = strbuf_split_max(line, ' ', 3);
1153 if (split[0] && split[1]) {
1154 struct object_id oid;
1155
1156 /*
1157 * strbuf_split_max left a space. Trim it and re-add
1158 * it after abbreviation.
1159 */
1160 strbuf_trim(split[1]);
1161 if (!get_oid(split[1]->buf, &oid)) {
1162 strbuf_reset(split[1]);
1163 strbuf_add_unique_abbrev(split[1], oid.hash,
1164 DEFAULT_ABBREV);
1165 strbuf_addch(split[1], ' ');
1166 strbuf_reset(line);
1167 for (i = 0; split[i]; i++)
1168 strbuf_addbuf(line, split[i]);
1169 }
1170 }
1171 strbuf_list_free(split);
1172}
1173
1174static int read_rebase_todolist(const char *fname, struct string_list *lines)
1175{
1176 struct strbuf line = STRBUF_INIT;
1177 FILE *f = fopen(git_path("%s", fname), "r");
1178
1179 if (!f) {
1180 if (errno == ENOENT)
1181 return -1;
1182 die_errno("Could not open file %s for reading",
1183 git_path("%s", fname));
1184 }
1185 while (!strbuf_getline_lf(&line, f)) {
1186 if (line.len && line.buf[0] == comment_line_char)
1187 continue;
1188 strbuf_trim(&line);
1189 if (!line.len)
1190 continue;
1191 abbrev_sha1_in_line(&line);
1192 string_list_append(lines, line.buf);
1193 }
1194 fclose(f);
1195 return 0;
1196}
1197
1198static void show_rebase_information(struct wt_status *s,
1199 struct wt_status_state *state,
1200 const char *color)
1201{
1202 if (state->rebase_interactive_in_progress) {
1203 int i;
1204 int nr_lines_to_show = 2;
1205
1206 struct string_list have_done = STRING_LIST_INIT_DUP;
1207 struct string_list yet_to_do = STRING_LIST_INIT_DUP;
1208
1209 read_rebase_todolist("rebase-merge/done", &have_done);
1210 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1211 &yet_to_do))
1212 status_printf_ln(s, color,
1213 _("git-rebase-todo is missing."));
1214 if (have_done.nr == 0)
1215 status_printf_ln(s, color, _("No commands done."));
1216 else {
1217 status_printf_ln(s, color,
1218 Q_("Last command done (%d command done):",
1219 "Last commands done (%d commands done):",
1220 have_done.nr),
1221 have_done.nr);
1222 for (i = (have_done.nr > nr_lines_to_show)
1223 ? have_done.nr - nr_lines_to_show : 0;
1224 i < have_done.nr;
1225 i++)
1226 status_printf_ln(s, color, " %s", have_done.items[i].string);
1227 if (have_done.nr > nr_lines_to_show && s->hints)
1228 status_printf_ln(s, color,
1229 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1230 }
1231
1232 if (yet_to_do.nr == 0)
1233 status_printf_ln(s, color,
1234 _("No commands remaining."));
1235 else {
1236 status_printf_ln(s, color,
1237 Q_("Next command to do (%d remaining command):",
1238 "Next commands to do (%d remaining commands):",
1239 yet_to_do.nr),
1240 yet_to_do.nr);
1241 for (i = 0; i < nr_lines_to_show && i < yet_to_do.nr; i++)
1242 status_printf_ln(s, color, " %s", yet_to_do.items[i].string);
1243 if (s->hints)
1244 status_printf_ln(s, color,
1245 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1246 }
1247 string_list_clear(&yet_to_do, 0);
1248 string_list_clear(&have_done, 0);
1249 }
1250}
1251
1252static void print_rebase_state(struct wt_status *s,
1253 struct wt_status_state *state,
1254 const char *color)
1255{
1256 if (state->branch)
1257 status_printf_ln(s, color,
1258 _("You are currently rebasing branch '%s' on '%s'."),
1259 state->branch,
1260 state->onto);
1261 else
1262 status_printf_ln(s, color,
1263 _("You are currently rebasing."));
1264}
1265
1266static void show_rebase_in_progress(struct wt_status *s,
1267 struct wt_status_state *state,
1268 const char *color)
1269{
1270 struct stat st;
1271
1272 show_rebase_information(s, state, color);
1273 if (has_unmerged(s)) {
1274 print_rebase_state(s, state, color);
1275 if (s->hints) {
1276 status_printf_ln(s, color,
1277 _(" (fix conflicts and then run \"git rebase --continue\")"));
1278 status_printf_ln(s, color,
1279 _(" (use \"git rebase --skip\" to skip this patch)"));
1280 status_printf_ln(s, color,
1281 _(" (use \"git rebase --abort\" to check out the original branch)"));
1282 }
1283 } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
1284 print_rebase_state(s, state, color);
1285 if (s->hints)
1286 status_printf_ln(s, color,
1287 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1288 } else if (split_commit_in_progress(s)) {
1289 if (state->branch)
1290 status_printf_ln(s, color,
1291 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1292 state->branch,
1293 state->onto);
1294 else
1295 status_printf_ln(s, color,
1296 _("You are currently splitting a commit during a rebase."));
1297 if (s->hints)
1298 status_printf_ln(s, color,
1299 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1300 } else {
1301 if (state->branch)
1302 status_printf_ln(s, color,
1303 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1304 state->branch,
1305 state->onto);
1306 else
1307 status_printf_ln(s, color,
1308 _("You are currently editing a commit during a rebase."));
1309 if (s->hints && !s->amend) {
1310 status_printf_ln(s, color,
1311 _(" (use \"git commit --amend\" to amend the current commit)"));
1312 status_printf_ln(s, color,
1313 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1314 }
1315 }
1316 wt_longstatus_print_trailer(s);
1317}
1318
1319static void show_cherry_pick_in_progress(struct wt_status *s,
1320 struct wt_status_state *state,
1321 const char *color)
1322{
1323 status_printf_ln(s, color, _("You are currently cherry-picking commit %s."),
1324 find_unique_abbrev(state->cherry_pick_head_sha1, DEFAULT_ABBREV));
1325 if (s->hints) {
1326 if (has_unmerged(s))
1327 status_printf_ln(s, color,
1328 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1329 else
1330 status_printf_ln(s, color,
1331 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1332 status_printf_ln(s, color,
1333 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1334 }
1335 wt_longstatus_print_trailer(s);
1336}
1337
1338static void show_revert_in_progress(struct wt_status *s,
1339 struct wt_status_state *state,
1340 const char *color)
1341{
1342 status_printf_ln(s, color, _("You are currently reverting commit %s."),
1343 find_unique_abbrev(state->revert_head_sha1, DEFAULT_ABBREV));
1344 if (s->hints) {
1345 if (has_unmerged(s))
1346 status_printf_ln(s, color,
1347 _(" (fix conflicts and run \"git revert --continue\")"));
1348 else
1349 status_printf_ln(s, color,
1350 _(" (all conflicts fixed: run \"git revert --continue\")"));
1351 status_printf_ln(s, color,
1352 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1353 }
1354 wt_longstatus_print_trailer(s);
1355}
1356
1357static void show_bisect_in_progress(struct wt_status *s,
1358 struct wt_status_state *state,
1359 const char *color)
1360{
1361 if (state->branch)
1362 status_printf_ln(s, color,
1363 _("You are currently bisecting, started from branch '%s'."),
1364 state->branch);
1365 else
1366 status_printf_ln(s, color,
1367 _("You are currently bisecting."));
1368 if (s->hints)
1369 status_printf_ln(s, color,
1370 _(" (use \"git bisect reset\" to get back to the original branch)"));
1371 wt_longstatus_print_trailer(s);
1372}
1373
1374/*
1375 * Extract branch information from rebase/bisect
1376 */
1377static char *get_branch(const struct worktree *wt, const char *path)
1378{
1379 struct strbuf sb = STRBUF_INIT;
1380 struct object_id oid;
1381 const char *branch_name;
1382
1383 if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1384 goto got_nothing;
1385
1386 while (sb.len && sb.buf[sb.len - 1] == '\n')
1387 strbuf_setlen(&sb, sb.len - 1);
1388 if (!sb.len)
1389 goto got_nothing;
1390 if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1391 strbuf_remove(&sb, 0, branch_name - sb.buf);
1392 else if (starts_with(sb.buf, "refs/"))
1393 ;
1394 else if (!get_oid_hex(sb.buf, &oid)) {
1395 strbuf_reset(&sb);
1396 strbuf_add_unique_abbrev(&sb, oid.hash, DEFAULT_ABBREV);
1397 } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */
1398 goto got_nothing;
1399 else /* bisect */
1400 ;
1401 return strbuf_detach(&sb, NULL);
1402
1403got_nothing:
1404 strbuf_release(&sb);
1405 return NULL;
1406}
1407
1408struct grab_1st_switch_cbdata {
1409 struct strbuf buf;
1410 struct object_id noid;
1411};
1412
1413static int grab_1st_switch(struct object_id *ooid, struct object_id *noid,
1414 const char *email, unsigned long timestamp, int tz,
1415 const char *message, void *cb_data)
1416{
1417 struct grab_1st_switch_cbdata *cb = cb_data;
1418 const char *target = NULL, *end;
1419
1420 if (!skip_prefix(message, "checkout: moving from ", &message))
1421 return 0;
1422 target = strstr(message, " to ");
1423 if (!target)
1424 return 0;
1425 target += strlen(" to ");
1426 strbuf_reset(&cb->buf);
1427 oidcpy(&cb->noid, noid);
1428 end = strchrnul(target, '\n');
1429 strbuf_add(&cb->buf, target, end - target);
1430 if (!strcmp(cb->buf.buf, "HEAD")) {
1431 /* HEAD is relative. Resolve it to the right reflog entry. */
1432 strbuf_reset(&cb->buf);
1433 strbuf_add_unique_abbrev(&cb->buf, noid->hash, DEFAULT_ABBREV);
1434 }
1435 return 1;
1436}
1437
1438static void wt_status_get_detached_from(struct wt_status_state *state)
1439{
1440 struct grab_1st_switch_cbdata cb;
1441 struct commit *commit;
1442 struct object_id oid;
1443 char *ref = NULL;
1444
1445 strbuf_init(&cb.buf, 0);
1446 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) {
1447 strbuf_release(&cb.buf);
1448 return;
1449 }
1450
1451 if (dwim_ref(cb.buf.buf, cb.buf.len, oid.hash, &ref) == 1 &&
1452 /* sha1 is a commit? match without further lookup */
1453 (!oidcmp(&cb.noid, &oid) ||
1454 /* perhaps sha1 is a tag, try to dereference to a commit */
1455 ((commit = lookup_commit_reference_gently(oid.hash, 1)) != NULL &&
1456 !oidcmp(&cb.noid, &commit->object.oid)))) {
1457 const char *from = ref;
1458 if (!skip_prefix(from, "refs/tags/", &from))
1459 skip_prefix(from, "refs/remotes/", &from);
1460 state->detached_from = xstrdup(from);
1461 } else
1462 state->detached_from =
1463 xstrdup(find_unique_abbrev(cb.noid.hash, DEFAULT_ABBREV));
1464 hashcpy(state->detached_sha1, cb.noid.hash);
1465 state->detached_at = !get_oid("HEAD", &oid) &&
1466 !hashcmp(oid.hash, state->detached_sha1);
1467
1468 free(ref);
1469 strbuf_release(&cb.buf);
1470}
1471
1472int wt_status_check_rebase(const struct worktree *wt,
1473 struct wt_status_state *state)
1474{
1475 struct stat st;
1476
1477 if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1478 if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1479 state->am_in_progress = 1;
1480 if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1481 state->am_empty_patch = 1;
1482 } else {
1483 state->rebase_in_progress = 1;
1484 state->branch = get_branch(wt, "rebase-apply/head-name");
1485 state->onto = get_branch(wt, "rebase-apply/onto");
1486 }
1487 } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1488 if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1489 state->rebase_interactive_in_progress = 1;
1490 else
1491 state->rebase_in_progress = 1;
1492 state->branch = get_branch(wt, "rebase-merge/head-name");
1493 state->onto = get_branch(wt, "rebase-merge/onto");
1494 } else
1495 return 0;
1496 return 1;
1497}
1498
1499int wt_status_check_bisect(const struct worktree *wt,
1500 struct wt_status_state *state)
1501{
1502 struct stat st;
1503
1504 if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1505 state->bisect_in_progress = 1;
1506 state->branch = get_branch(wt, "BISECT_START");
1507 return 1;
1508 }
1509 return 0;
1510}
1511
1512void wt_status_get_state(struct wt_status_state *state,
1513 int get_detached_from)
1514{
1515 struct stat st;
1516 struct object_id oid;
1517
1518 if (!stat(git_path_merge_head(), &st)) {
1519 state->merge_in_progress = 1;
1520 } else if (wt_status_check_rebase(NULL, state)) {
1521 ; /* all set */
1522 } else if (!stat(git_path_cherry_pick_head(), &st) &&
1523 !get_oid("CHERRY_PICK_HEAD", &oid)) {
1524 state->cherry_pick_in_progress = 1;
1525 hashcpy(state->cherry_pick_head_sha1, oid.hash);
1526 }
1527 wt_status_check_bisect(NULL, state);
1528 if (!stat(git_path_revert_head(), &st) &&
1529 !get_oid("REVERT_HEAD", &oid)) {
1530 state->revert_in_progress = 1;
1531 hashcpy(state->revert_head_sha1, oid.hash);
1532 }
1533
1534 if (get_detached_from)
1535 wt_status_get_detached_from(state);
1536}
1537
1538static void wt_longstatus_print_state(struct wt_status *s,
1539 struct wt_status_state *state)
1540{
1541 const char *state_color = color(WT_STATUS_HEADER, s);
1542 if (state->merge_in_progress)
1543 show_merge_in_progress(s, state, state_color);
1544 else if (state->am_in_progress)
1545 show_am_in_progress(s, state, state_color);
1546 else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
1547 show_rebase_in_progress(s, state, state_color);
1548 else if (state->cherry_pick_in_progress)
1549 show_cherry_pick_in_progress(s, state, state_color);
1550 else if (state->revert_in_progress)
1551 show_revert_in_progress(s, state, state_color);
1552 if (state->bisect_in_progress)
1553 show_bisect_in_progress(s, state, state_color);
1554}
1555
1556static void wt_longstatus_print(struct wt_status *s)
1557{
1558 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1559 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1560 struct wt_status_state state;
1561
1562 memset(&state, 0, sizeof(state));
1563 wt_status_get_state(&state,
1564 s->branch && !strcmp(s->branch, "HEAD"));
1565
1566 if (s->branch) {
1567 const char *on_what = _("On branch ");
1568 const char *branch_name = s->branch;
1569 if (!strcmp(branch_name, "HEAD")) {
1570 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1571 if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1572 if (state.rebase_interactive_in_progress)
1573 on_what = _("interactive rebase in progress; onto ");
1574 else
1575 on_what = _("rebase in progress; onto ");
1576 branch_name = state.onto;
1577 } else if (state.detached_from) {
1578 branch_name = state.detached_from;
1579 if (state.detached_at)
1580 on_what = _("HEAD detached at ");
1581 else
1582 on_what = _("HEAD detached from ");
1583 } else {
1584 branch_name = "";
1585 on_what = _("Not currently on any branch.");
1586 }
1587 } else
1588 skip_prefix(branch_name, "refs/heads/", &branch_name);
1589 status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1590 status_printf_more(s, branch_status_color, "%s", on_what);
1591 status_printf_more(s, branch_color, "%s\n", branch_name);
1592 if (!s->is_initial)
1593 wt_longstatus_print_tracking(s);
1594 }
1595
1596 wt_longstatus_print_state(s, &state);
1597 free(state.branch);
1598 free(state.onto);
1599 free(state.detached_from);
1600
1601 if (s->is_initial) {
1602 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1603 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1604 status_printf_ln(s, color(WT_STATUS_HEADER, s), "%s", "");
1605 }
1606
1607 wt_longstatus_print_updated(s);
1608 wt_longstatus_print_unmerged(s);
1609 wt_longstatus_print_changed(s);
1610 if (s->submodule_summary &&
1611 (!s->ignore_submodule_arg ||
1612 strcmp(s->ignore_submodule_arg, "all"))) {
1613 wt_longstatus_print_submodule_summary(s, 0); /* staged */
1614 wt_longstatus_print_submodule_summary(s, 1); /* unstaged */
1615 }
1616 if (s->show_untracked_files) {
1617 wt_longstatus_print_other(s, &s->untracked, _("Untracked files"), "add");
1618 if (s->show_ignored_files)
1619 wt_longstatus_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1620 if (advice_status_u_option && 2000 < s->untracked_in_ms) {
1621 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
1622 status_printf_ln(s, GIT_COLOR_NORMAL,
1623 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1624 "may speed it up, but you have to be careful not to forget to add\n"
1625 "new files yourself (see 'git help status')."),
1626 s->untracked_in_ms / 1000.0);
1627 }
1628 } else if (s->commitable)
1629 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1630 s->hints
1631 ? _(" (use -u option to show untracked files)") : "");
1632
1633 if (s->verbose)
1634 wt_longstatus_print_verbose(s);
1635 if (!s->commitable) {
1636 if (s->amend)
1637 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1638 else if (s->nowarn)
1639 ; /* nothing */
1640 else if (s->workdir_dirty) {
1641 if (s->hints)
1642 printf(_("no changes added to commit "
1643 "(use \"git add\" and/or \"git commit -a\")\n"));
1644 else
1645 printf(_("no changes added to commit\n"));
1646 } else if (s->untracked.nr) {
1647 if (s->hints)
1648 printf(_("nothing added to commit but untracked files "
1649 "present (use \"git add\" to track)\n"));
1650 else
1651 printf(_("nothing added to commit but untracked files present\n"));
1652 } else if (s->is_initial) {
1653 if (s->hints)
1654 printf(_("nothing to commit (create/copy files "
1655 "and use \"git add\" to track)\n"));
1656 else
1657 printf(_("nothing to commit\n"));
1658 } else if (!s->show_untracked_files) {
1659 if (s->hints)
1660 printf(_("nothing to commit (use -u to show untracked files)\n"));
1661 else
1662 printf(_("nothing to commit\n"));
1663 } else
1664 printf(_("nothing to commit, working tree clean\n"));
1665 }
1666}
1667
1668static void wt_shortstatus_unmerged(struct string_list_item *it,
1669 struct wt_status *s)
1670{
1671 struct wt_status_change_data *d = it->util;
1672 const char *how = "??";
1673
1674 switch (d->stagemask) {
1675 case 1: how = "DD"; break; /* both deleted */
1676 case 2: how = "AU"; break; /* added by us */
1677 case 3: how = "UD"; break; /* deleted by them */
1678 case 4: how = "UA"; break; /* added by them */
1679 case 5: how = "DU"; break; /* deleted by us */
1680 case 6: how = "AA"; break; /* both added */
1681 case 7: how = "UU"; break; /* both modified */
1682 }
1683 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1684 if (s->null_termination) {
1685 fprintf(stdout, " %s%c", it->string, 0);
1686 } else {
1687 struct strbuf onebuf = STRBUF_INIT;
1688 const char *one;
1689 one = quote_path(it->string, s->prefix, &onebuf);
1690 printf(" %s\n", one);
1691 strbuf_release(&onebuf);
1692 }
1693}
1694
1695static void wt_shortstatus_status(struct string_list_item *it,
1696 struct wt_status *s)
1697{
1698 struct wt_status_change_data *d = it->util;
1699
1700 if (d->index_status)
1701 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1702 else
1703 putchar(' ');
1704 if (d->worktree_status)
1705 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1706 else
1707 putchar(' ');
1708 putchar(' ');
1709 if (s->null_termination) {
1710 fprintf(stdout, "%s%c", it->string, 0);
1711 if (d->rename_source)
1712 fprintf(stdout, "%s%c", d->rename_source, 0);
1713 } else {
1714 struct strbuf onebuf = STRBUF_INIT;
1715 const char *one;
1716
1717 if (d->rename_source) {
1718 one = quote_path(d->rename_source, s->prefix, &onebuf);
1719 if (*one != '"' && strchr(one, ' ') != NULL) {
1720 putchar('"');
1721 strbuf_addch(&onebuf, '"');
1722 one = onebuf.buf;
1723 }
1724 printf("%s -> ", one);
1725 strbuf_release(&onebuf);
1726 }
1727 one = quote_path(it->string, s->prefix, &onebuf);
1728 if (*one != '"' && strchr(one, ' ') != NULL) {
1729 putchar('"');
1730 strbuf_addch(&onebuf, '"');
1731 one = onebuf.buf;
1732 }
1733 printf("%s\n", one);
1734 strbuf_release(&onebuf);
1735 }
1736}
1737
1738static void wt_shortstatus_other(struct string_list_item *it,
1739 struct wt_status *s, const char *sign)
1740{
1741 if (s->null_termination) {
1742 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1743 } else {
1744 struct strbuf onebuf = STRBUF_INIT;
1745 const char *one;
1746 one = quote_path(it->string, s->prefix, &onebuf);
1747 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1748 printf(" %s\n", one);
1749 strbuf_release(&onebuf);
1750 }
1751}
1752
1753static void wt_shortstatus_print_tracking(struct wt_status *s)
1754{
1755 struct branch *branch;
1756 const char *header_color = color(WT_STATUS_HEADER, s);
1757 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1758 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1759
1760 const char *base;
1761 const char *branch_name;
1762 int num_ours, num_theirs;
1763 int upstream_is_gone = 0;
1764
1765 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1766
1767 if (!s->branch)
1768 return;
1769 branch_name = s->branch;
1770
1771#define LABEL(string) (s->no_gettext ? (string) : _(string))
1772
1773 if (s->is_initial)
1774 color_fprintf(s->fp, header_color, LABEL(N_("Initial commit on ")));
1775
1776 if (!strcmp(s->branch, "HEAD")) {
1777 color_fprintf(s->fp, color(WT_STATUS_NOBRANCH, s), "%s",
1778 LABEL(N_("HEAD (no branch)")));
1779 goto conclude;
1780 }
1781
1782 skip_prefix(branch_name, "refs/heads/", &branch_name);
1783
1784 branch = branch_get(branch_name);
1785
1786 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1787
1788 if (stat_tracking_info(branch, &num_ours, &num_theirs, &base) < 0) {
1789 if (!base)
1790 goto conclude;
1791
1792 upstream_is_gone = 1;
1793 }
1794
1795 base = shorten_unambiguous_ref(base, 0);
1796 color_fprintf(s->fp, header_color, "...");
1797 color_fprintf(s->fp, branch_color_remote, "%s", base);
1798 free((char *)base);
1799
1800 if (!upstream_is_gone && !num_ours && !num_theirs)
1801 goto conclude;
1802
1803 color_fprintf(s->fp, header_color, " [");
1804 if (upstream_is_gone) {
1805 color_fprintf(s->fp, header_color, LABEL(N_("gone")));
1806 } else if (!num_ours) {
1807 color_fprintf(s->fp, header_color, LABEL(N_("behind ")));
1808 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1809 } else if (!num_theirs) {
1810 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1811 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1812 } else {
1813 color_fprintf(s->fp, header_color, LABEL(N_("ahead ")));
1814 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1815 color_fprintf(s->fp, header_color, ", %s", LABEL(N_("behind ")));
1816 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1817 }
1818
1819 color_fprintf(s->fp, header_color, "]");
1820 conclude:
1821 fputc(s->null_termination ? '\0' : '\n', s->fp);
1822}
1823
1824static void wt_shortstatus_print(struct wt_status *s)
1825{
1826 struct string_list_item *it;
1827
1828 if (s->show_branch)
1829 wt_shortstatus_print_tracking(s);
1830
1831 for_each_string_list_item(it, &s->change) {
1832 struct wt_status_change_data *d = it->util;
1833
1834 if (d->stagemask)
1835 wt_shortstatus_unmerged(it, s);
1836 else
1837 wt_shortstatus_status(it, s);
1838 }
1839 for_each_string_list_item(it, &s->untracked)
1840 wt_shortstatus_other(it, s, "??");
1841
1842 for_each_string_list_item(it, &s->ignored)
1843 wt_shortstatus_other(it, s, "!!");
1844}
1845
1846static void wt_porcelain_print(struct wt_status *s)
1847{
1848 s->use_color = 0;
1849 s->relative_paths = 0;
1850 s->prefix = NULL;
1851 s->no_gettext = 1;
1852 wt_shortstatus_print(s);
1853}
1854
1855/*
1856 * Print branch information for porcelain v2 output. These lines
1857 * are printed when the '--branch' parameter is given.
1858 *
1859 * # branch.oid <commit><eol>
1860 * # branch.head <head><eol>
1861 * [# branch.upstream <upstream><eol>
1862 * [# branch.ab +<ahead> -<behind><eol>]]
1863 *
1864 * <commit> ::= the current commit hash or the the literal
1865 * "(initial)" to indicate an initialized repo
1866 * with no commits.
1867 *
1868 * <head> ::= <branch_name> the current branch name or
1869 * "(detached)" literal when detached head or
1870 * "(unknown)" when something is wrong.
1871 *
1872 * <upstream> ::= the upstream branch name, when set.
1873 *
1874 * <ahead> ::= integer ahead value, when upstream set
1875 * and the commit is present (not gone).
1876 *
1877 * <behind> ::= integer behind value, when upstream set
1878 * and commit is present.
1879 *
1880 *
1881 * The end-of-line is defined by the -z flag.
1882 *
1883 * <eol> ::= NUL when -z,
1884 * LF when NOT -z.
1885 *
1886 */
1887static void wt_porcelain_v2_print_tracking(struct wt_status *s)
1888{
1889 struct branch *branch;
1890 const char *base;
1891 const char *branch_name;
1892 struct wt_status_state state;
1893 int ab_info, nr_ahead, nr_behind;
1894 char eol = s->null_termination ? '\0' : '\n';
1895
1896 memset(&state, 0, sizeof(state));
1897 wt_status_get_state(&state, s->branch && !strcmp(s->branch, "HEAD"));
1898
1899 fprintf(s->fp, "# branch.oid %s%c",
1900 (s->is_initial ? "(initial)" : sha1_to_hex(s->sha1_commit)),
1901 eol);
1902
1903 if (!s->branch)
1904 fprintf(s->fp, "# branch.head %s%c", "(unknown)", eol);
1905 else {
1906 if (!strcmp(s->branch, "HEAD")) {
1907 fprintf(s->fp, "# branch.head %s%c", "(detached)", eol);
1908
1909 if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1910 branch_name = state.onto;
1911 else if (state.detached_from)
1912 branch_name = state.detached_from;
1913 else
1914 branch_name = "";
1915 } else {
1916 branch_name = NULL;
1917 skip_prefix(s->branch, "refs/heads/", &branch_name);
1918
1919 fprintf(s->fp, "# branch.head %s%c", branch_name, eol);
1920 }
1921
1922 /* Lookup stats on the upstream tracking branch, if set. */
1923 branch = branch_get(branch_name);
1924 base = NULL;
1925 ab_info = (stat_tracking_info(branch, &nr_ahead, &nr_behind, &base) == 0);
1926 if (base) {
1927 base = shorten_unambiguous_ref(base, 0);
1928 fprintf(s->fp, "# branch.upstream %s%c", base, eol);
1929 free((char *)base);
1930
1931 if (ab_info)
1932 fprintf(s->fp, "# branch.ab +%d -%d%c", nr_ahead, nr_behind, eol);
1933 }
1934 }
1935
1936 free(state.branch);
1937 free(state.onto);
1938 free(state.detached_from);
1939}
1940
1941/*
1942 * Convert various submodule status values into a
1943 * fixed-length string of characters in the buffer provided.
1944 */
1945static void wt_porcelain_v2_submodule_state(
1946 struct wt_status_change_data *d,
1947 char sub[5])
1948{
1949 if (S_ISGITLINK(d->mode_head) ||
1950 S_ISGITLINK(d->mode_index) ||
1951 S_ISGITLINK(d->mode_worktree)) {
1952 sub[0] = 'S';
1953 sub[1] = d->new_submodule_commits ? 'C' : '.';
1954 sub[2] = (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) ? 'M' : '.';
1955 sub[3] = (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ? 'U' : '.';
1956 } else {
1957 sub[0] = 'N';
1958 sub[1] = '.';
1959 sub[2] = '.';
1960 sub[3] = '.';
1961 }
1962 sub[4] = 0;
1963}
1964
1965/*
1966 * Fix-up changed entries before we print them.
1967 */
1968static void wt_porcelain_v2_fix_up_changed(
1969 struct string_list_item *it,
1970 struct wt_status *s)
1971{
1972 struct wt_status_change_data *d = it->util;
1973
1974 if (!d->index_status) {
1975 /*
1976 * This entry is unchanged in the index (relative to the head).
1977 * Therefore, the collect_updated_cb was never called for this
1978 * entry (during the head-vs-index scan) and so the head column
1979 * fields were never set.
1980 *
1981 * We must have data for the index column (from the
1982 * index-vs-worktree scan (otherwise, this entry should not be
1983 * in the list of changes)).
1984 *
1985 * Copy index column fields to the head column, so that our
1986 * output looks complete.
1987 */
1988 assert(d->mode_head == 0);
1989 d->mode_head = d->mode_index;
1990 oidcpy(&d->oid_head, &d->oid_index);
1991 }
1992
1993 if (!d->worktree_status) {
1994 /*
1995 * This entry is unchanged in the worktree (relative to the index).
1996 * Therefore, the collect_changed_cb was never called for this entry
1997 * (during the index-vs-worktree scan) and so the worktree column
1998 * fields were never set.
1999 *
2000 * We must have data for the index column (from the head-vs-index
2001 * scan).
2002 *
2003 * Copy the index column fields to the worktree column so that
2004 * our output looks complete.
2005 *
2006 * Note that we only have a mode field in the worktree column
2007 * because the scan code tries really hard to not have to compute it.
2008 */
2009 assert(d->mode_worktree == 0);
2010 d->mode_worktree = d->mode_index;
2011 }
2012}
2013
2014/*
2015 * Print porcelain v2 info for tracked entries with changes.
2016 */
2017static void wt_porcelain_v2_print_changed_entry(
2018 struct string_list_item *it,
2019 struct wt_status *s)
2020{
2021 struct wt_status_change_data *d = it->util;
2022 struct strbuf buf = STRBUF_INIT;
2023 struct strbuf buf_from = STRBUF_INIT;
2024 const char *path = NULL;
2025 const char *path_from = NULL;
2026 char key[3];
2027 char submodule_token[5];
2028 char sep_char, eol_char;
2029
2030 wt_porcelain_v2_fix_up_changed(it, s);
2031 wt_porcelain_v2_submodule_state(d, submodule_token);
2032
2033 key[0] = d->index_status ? d->index_status : '.';
2034 key[1] = d->worktree_status ? d->worktree_status : '.';
2035 key[2] = 0;
2036
2037 if (s->null_termination) {
2038 /*
2039 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2040 * A single NUL character separates them.
2041 */
2042 sep_char = '\0';
2043 eol_char = '\0';
2044 path = it->string;
2045 path_from = d->rename_source;
2046 } else {
2047 /*
2048 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2049 * The source path is only present when necessary.
2050 * A single TAB separates them (because paths can contain spaces
2051 * which are not escaped and C-quoting does escape TAB characters).
2052 */
2053 sep_char = '\t';
2054 eol_char = '\n';
2055 path = quote_path(it->string, s->prefix, &buf);
2056 if (d->rename_source)
2057 path_from = quote_path(d->rename_source, s->prefix, &buf_from);
2058 }
2059
2060 if (path_from)
2061 fprintf(s->fp, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2062 key, submodule_token,
2063 d->mode_head, d->mode_index, d->mode_worktree,
2064 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2065 d->rename_status, d->rename_score,
2066 path, sep_char, path_from, eol_char);
2067 else
2068 fprintf(s->fp, "1 %s %s %06o %06o %06o %s %s %s%c",
2069 key, submodule_token,
2070 d->mode_head, d->mode_index, d->mode_worktree,
2071 oid_to_hex(&d->oid_head), oid_to_hex(&d->oid_index),
2072 path, eol_char);
2073
2074 strbuf_release(&buf);
2075 strbuf_release(&buf_from);
2076}
2077
2078/*
2079 * Print porcelain v2 status info for unmerged entries.
2080 */
2081static void wt_porcelain_v2_print_unmerged_entry(
2082 struct string_list_item *it,
2083 struct wt_status *s)
2084{
2085 struct wt_status_change_data *d = it->util;
2086 const struct cache_entry *ce;
2087 struct strbuf buf_index = STRBUF_INIT;
2088 const char *path_index = NULL;
2089 int pos, stage, sum;
2090 struct {
2091 int mode;
2092 struct object_id oid;
2093 } stages[3];
2094 char *key;
2095 char submodule_token[5];
2096 char unmerged_prefix = 'u';
2097 char eol_char = s->null_termination ? '\0' : '\n';
2098
2099 wt_porcelain_v2_submodule_state(d, submodule_token);
2100
2101 switch (d->stagemask) {
2102 case 1: key = "DD"; break; /* both deleted */
2103 case 2: key = "AU"; break; /* added by us */
2104 case 3: key = "UD"; break; /* deleted by them */
2105 case 4: key = "UA"; break; /* added by them */
2106 case 5: key = "DU"; break; /* deleted by us */
2107 case 6: key = "AA"; break; /* both added */
2108 case 7: key = "UU"; break; /* both modified */
2109 default:
2110 die("BUG: unhandled unmerged status %x", d->stagemask);
2111 }
2112
2113 /*
2114 * Disregard d.aux.porcelain_v2 data that we accumulated
2115 * for the head and index columns during the scans and
2116 * replace with the actual stage data.
2117 *
2118 * Note that this is a last-one-wins for each the individual
2119 * stage [123] columns in the event of multiple cache entries
2120 * for same stage.
2121 */
2122 memset(stages, 0, sizeof(stages));
2123 sum = 0;
2124 pos = cache_name_pos(it->string, strlen(it->string));
2125 assert(pos < 0);
2126 pos = -pos-1;
2127 while (pos < active_nr) {
2128 ce = active_cache[pos++];
2129 stage = ce_stage(ce);
2130 if (strcmp(ce->name, it->string) || !stage)
2131 break;
2132 stages[stage - 1].mode = ce->ce_mode;
2133 oidcpy(&stages[stage - 1].oid, &ce->oid);
2134 sum |= (1 << (stage - 1));
2135 }
2136 if (sum != d->stagemask)
2137 die("BUG: observed stagemask 0x%x != expected stagemask 0x%x", sum, d->stagemask);
2138
2139 if (s->null_termination)
2140 path_index = it->string;
2141 else
2142 path_index = quote_path(it->string, s->prefix, &buf_index);
2143
2144 fprintf(s->fp, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2145 unmerged_prefix, key, submodule_token,
2146 stages[0].mode, /* stage 1 */
2147 stages[1].mode, /* stage 2 */
2148 stages[2].mode, /* stage 3 */
2149 d->mode_worktree,
2150 oid_to_hex(&stages[0].oid), /* stage 1 */
2151 oid_to_hex(&stages[1].oid), /* stage 2 */
2152 oid_to_hex(&stages[2].oid), /* stage 3 */
2153 path_index,
2154 eol_char);
2155
2156 strbuf_release(&buf_index);
2157}
2158
2159/*
2160 * Print porcelain V2 status info for untracked and ignored entries.
2161 */
2162static void wt_porcelain_v2_print_other(
2163 struct string_list_item *it,
2164 struct wt_status *s,
2165 char prefix)
2166{
2167 struct strbuf buf = STRBUF_INIT;
2168 const char *path;
2169 char eol_char;
2170
2171 if (s->null_termination) {
2172 path = it->string;
2173 eol_char = '\0';
2174 } else {
2175 path = quote_path(it->string, s->prefix, &buf);
2176 eol_char = '\n';
2177 }
2178
2179 fprintf(s->fp, "%c %s%c", prefix, path, eol_char);
2180
2181 strbuf_release(&buf);
2182}
2183
2184/*
2185 * Print porcelain V2 status.
2186 *
2187 * [<v2_branch>]
2188 * [<v2_changed_items>]*
2189 * [<v2_unmerged_items>]*
2190 * [<v2_untracked_items>]*
2191 * [<v2_ignored_items>]*
2192 *
2193 */
2194static void wt_porcelain_v2_print(struct wt_status *s)
2195{
2196 struct wt_status_change_data *d;
2197 struct string_list_item *it;
2198 int i;
2199
2200 if (s->show_branch)
2201 wt_porcelain_v2_print_tracking(s);
2202
2203 for (i = 0; i < s->change.nr; i++) {
2204 it = &(s->change.items[i]);
2205 d = it->util;
2206 if (!d->stagemask)
2207 wt_porcelain_v2_print_changed_entry(it, s);
2208 }
2209
2210 for (i = 0; i < s->change.nr; i++) {
2211 it = &(s->change.items[i]);
2212 d = it->util;
2213 if (d->stagemask)
2214 wt_porcelain_v2_print_unmerged_entry(it, s);
2215 }
2216
2217 for (i = 0; i < s->untracked.nr; i++) {
2218 it = &(s->untracked.items[i]);
2219 wt_porcelain_v2_print_other(it, s, '?');
2220 }
2221
2222 for (i = 0; i < s->ignored.nr; i++) {
2223 it = &(s->ignored.items[i]);
2224 wt_porcelain_v2_print_other(it, s, '!');
2225 }
2226}
2227
2228void wt_status_print(struct wt_status *s)
2229{
2230 switch (s->status_format) {
2231 case STATUS_FORMAT_SHORT:
2232 wt_shortstatus_print(s);
2233 break;
2234 case STATUS_FORMAT_PORCELAIN:
2235 wt_porcelain_print(s);
2236 break;
2237 case STATUS_FORMAT_PORCELAIN_V2:
2238 wt_porcelain_v2_print(s);
2239 break;
2240 case STATUS_FORMAT_UNSPECIFIED:
2241 die("BUG: finalize_deferred_config() should have been called");
2242 break;
2243 case STATUS_FORMAT_NONE:
2244 case STATUS_FORMAT_LONG:
2245 wt_longstatus_print(s);
2246 break;
2247 }
2248}
2249
2250/**
2251 * Returns 1 if there are unstaged changes, 0 otherwise.
2252 */
2253int has_unstaged_changes(int ignore_submodules)
2254{
2255 struct rev_info rev_info;
2256 int result;
2257
2258 init_revisions(&rev_info, NULL);
2259 if (ignore_submodules)
2260 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
2261 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2262 diff_setup_done(&rev_info.diffopt);
2263 result = run_diff_files(&rev_info, 0);
2264 return diff_result_code(&rev_info.diffopt, result);
2265}
2266
2267/**
2268 * Returns 1 if there are uncommitted changes, 0 otherwise.
2269 */
2270int has_uncommitted_changes(int ignore_submodules)
2271{
2272 struct rev_info rev_info;
2273 int result;
2274
2275 if (is_cache_unborn())
2276 return 0;
2277
2278 init_revisions(&rev_info, NULL);
2279 if (ignore_submodules)
2280 DIFF_OPT_SET(&rev_info.diffopt, IGNORE_SUBMODULES);
2281 DIFF_OPT_SET(&rev_info.diffopt, QUICK);
2282 add_head_to_pending(&rev_info);
2283 diff_setup_done(&rev_info.diffopt);
2284 result = run_diff_index(&rev_info, 1);
2285 return diff_result_code(&rev_info.diffopt, result);
2286}
2287
2288/**
2289 * If the work tree has unstaged or uncommitted changes, dies with the
2290 * appropriate message.
2291 */
2292int require_clean_work_tree(const char *action, const char *hint, int ignore_submodules, int gently)
2293{
2294 struct lock_file *lock_file = xcalloc(1, sizeof(*lock_file));
2295 int err = 0, fd;
2296
2297 fd = hold_locked_index(lock_file, 0);
2298 refresh_cache(REFRESH_QUIET);
2299 if (0 <= fd)
2300 update_index_if_able(&the_index, lock_file);
2301 rollback_lock_file(lock_file);
2302
2303 if (has_unstaged_changes(ignore_submodules)) {
2304 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2305 error(_("cannot %s: You have unstaged changes."), _(action));
2306 err = 1;
2307 }
2308
2309 if (has_uncommitted_changes(ignore_submodules)) {
2310 if (err)
2311 error(_("additionally, your index contains uncommitted changes."));
2312 else
2313 error(_("cannot %s: Your index contains uncommitted changes."),
2314 _(action));
2315 err = 1;
2316 }
2317
2318 if (err) {
2319 if (hint)
2320 error("%s", hint);
2321 if (!gently)
2322 exit(128);
2323 }
2324
2325 return err;
2326}