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