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