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