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