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