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