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