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