log -L: check range set invariants when we look it up
[alt-git.git] / wt-status.c
blobef405d03d928c34fe90e27b188bb7a942e3f834c
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 "revision.h"
8 #include "diffcore.h"
9 #include "quote.h"
10 #include "run-command.h"
11 #include "remote.h"
12 #include "refs.h"
13 #include "submodule.h"
14 #include "column.h"
15 #include "strbuf.h"
17 static char default_wt_status_colors[][COLOR_MAXLEN] = {
18 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
23 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
24 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
25 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
26 GIT_COLOR_NIL, /* WT_STATUS_ONBRANCH */
29 static const char *color(int slot, struct wt_status *s)
31 const char *c = "";
32 if (want_color(s->use_color))
33 c = s->color_palette[slot];
34 if (slot == WT_STATUS_ONBRANCH && color_is_nil(c))
35 c = s->color_palette[WT_STATUS_HEADER];
36 return c;
39 static void status_vprintf(struct wt_status *s, int at_bol, const char *color,
40 const char *fmt, va_list ap, const char *trail)
42 struct strbuf sb = STRBUF_INIT;
43 struct strbuf linebuf = STRBUF_INIT;
44 const char *line, *eol;
46 strbuf_vaddf(&sb, fmt, ap);
47 if (!sb.len) {
48 strbuf_addch(&sb, comment_line_char);
49 if (!trail)
50 strbuf_addch(&sb, ' ');
51 color_print_strbuf(s->fp, color, &sb);
52 if (trail)
53 fprintf(s->fp, "%s", trail);
54 strbuf_release(&sb);
55 return;
57 for (line = sb.buf; *line; line = eol + 1) {
58 eol = strchr(line, '\n');
60 strbuf_reset(&linebuf);
61 if (at_bol) {
62 strbuf_addch(&linebuf, comment_line_char);
63 if (*line != '\n' && *line != '\t')
64 strbuf_addch(&linebuf, ' ');
66 if (eol)
67 strbuf_add(&linebuf, line, eol - line);
68 else
69 strbuf_addstr(&linebuf, line);
70 color_print_strbuf(s->fp, color, &linebuf);
71 if (eol)
72 fprintf(s->fp, "\n");
73 else
74 break;
75 at_bol = 1;
77 if (trail)
78 fprintf(s->fp, "%s", trail);
79 strbuf_release(&linebuf);
80 strbuf_release(&sb);
83 void status_printf_ln(struct wt_status *s, const char *color,
84 const char *fmt, ...)
86 va_list ap;
88 va_start(ap, fmt);
89 status_vprintf(s, 1, color, fmt, ap, "\n");
90 va_end(ap);
93 void status_printf(struct wt_status *s, const char *color,
94 const char *fmt, ...)
96 va_list ap;
98 va_start(ap, fmt);
99 status_vprintf(s, 1, color, fmt, ap, NULL);
100 va_end(ap);
103 static void status_printf_more(struct wt_status *s, const char *color,
104 const char *fmt, ...)
106 va_list ap;
108 va_start(ap, fmt);
109 status_vprintf(s, 0, color, fmt, ap, NULL);
110 va_end(ap);
113 void wt_status_prepare(struct wt_status *s)
115 unsigned char sha1[20];
117 memset(s, 0, sizeof(*s));
118 memcpy(s->color_palette, default_wt_status_colors,
119 sizeof(default_wt_status_colors));
120 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
121 s->use_color = -1;
122 s->relative_paths = 1;
123 s->branch = resolve_refdup("HEAD", sha1, 0, NULL);
124 s->reference = "HEAD";
125 s->fp = stdout;
126 s->index_file = get_index_file();
127 s->change.strdup_strings = 1;
128 s->untracked.strdup_strings = 1;
129 s->ignored.strdup_strings = 1;
132 static void wt_status_print_unmerged_header(struct wt_status *s)
134 int i;
135 int del_mod_conflict = 0;
136 int both_deleted = 0;
137 int not_deleted = 0;
138 const char *c = color(WT_STATUS_HEADER, s);
140 status_printf_ln(s, c, _("Unmerged paths:"));
142 for (i = 0; i < s->change.nr; i++) {
143 struct string_list_item *it = &(s->change.items[i]);
144 struct wt_status_change_data *d = it->util;
146 switch (d->stagemask) {
147 case 0:
148 break;
149 case 1:
150 both_deleted = 1;
151 break;
152 case 3:
153 case 5:
154 del_mod_conflict = 1;
155 break;
156 default:
157 not_deleted = 1;
158 break;
162 if (!advice_status_hints)
163 return;
164 if (s->whence != FROM_COMMIT)
166 else if (!s->is_initial)
167 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
168 else
169 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
171 if (!both_deleted) {
172 if (!del_mod_conflict)
173 status_printf_ln(s, c, _(" (use \"git add <file>...\" to mark resolution)"));
174 else
175 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
176 } else if (!del_mod_conflict && !not_deleted) {
177 status_printf_ln(s, c, _(" (use \"git rm <file>...\" to mark resolution)"));
178 } else {
179 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
181 status_printf_ln(s, c, "");
184 static void wt_status_print_cached_header(struct wt_status *s)
186 const char *c = color(WT_STATUS_HEADER, s);
188 status_printf_ln(s, c, _("Changes to be committed:"));
189 if (!advice_status_hints)
190 return;
191 if (s->whence != FROM_COMMIT)
192 ; /* NEEDSWORK: use "git reset --unresolve"??? */
193 else if (!s->is_initial)
194 status_printf_ln(s, c, _(" (use \"git reset %s <file>...\" to unstage)"), s->reference);
195 else
196 status_printf_ln(s, c, _(" (use \"git rm --cached <file>...\" to unstage)"));
197 status_printf_ln(s, c, "");
200 static void wt_status_print_dirty_header(struct wt_status *s,
201 int has_deleted,
202 int has_dirty_submodules)
204 const char *c = color(WT_STATUS_HEADER, s);
206 status_printf_ln(s, c, _("Changes not staged for commit:"));
207 if (!advice_status_hints)
208 return;
209 if (!has_deleted)
210 status_printf_ln(s, c, _(" (use \"git add <file>...\" to update what will be committed)"));
211 else
212 status_printf_ln(s, c, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
213 status_printf_ln(s, c, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
214 if (has_dirty_submodules)
215 status_printf_ln(s, c, _(" (commit or discard the untracked or modified content in submodules)"));
216 status_printf_ln(s, c, "");
219 static void wt_status_print_other_header(struct wt_status *s,
220 const char *what,
221 const char *how)
223 const char *c = color(WT_STATUS_HEADER, s);
224 status_printf_ln(s, c, "%s:", what);
225 if (!advice_status_hints)
226 return;
227 status_printf_ln(s, c, _(" (use \"git %s <file>...\" to include in what will be committed)"), how);
228 status_printf_ln(s, c, "");
231 static void wt_status_print_trailer(struct wt_status *s)
233 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
236 #define quote_path quote_path_relative
238 static void wt_status_print_unmerged_data(struct wt_status *s,
239 struct string_list_item *it)
241 const char *c = color(WT_STATUS_UNMERGED, s);
242 struct wt_status_change_data *d = it->util;
243 struct strbuf onebuf = STRBUF_INIT;
244 const char *one, *how = _("bug");
246 one = quote_path(it->string, -1, &onebuf, s->prefix);
247 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
248 switch (d->stagemask) {
249 case 1: how = _("both deleted:"); break;
250 case 2: how = _("added by us:"); break;
251 case 3: how = _("deleted by them:"); break;
252 case 4: how = _("added by them:"); break;
253 case 5: how = _("deleted by us:"); break;
254 case 6: how = _("both added:"); break;
255 case 7: how = _("both modified:"); break;
257 status_printf_more(s, c, "%-20s%s\n", how, one);
258 strbuf_release(&onebuf);
261 static void wt_status_print_change_data(struct wt_status *s,
262 int change_type,
263 struct string_list_item *it)
265 struct wt_status_change_data *d = it->util;
266 const char *c = color(change_type, s);
267 int status = status;
268 char *one_name;
269 char *two_name;
270 const char *one, *two;
271 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
272 struct strbuf extra = STRBUF_INIT;
274 one_name = two_name = it->string;
275 switch (change_type) {
276 case WT_STATUS_UPDATED:
277 status = d->index_status;
278 if (d->head_path)
279 one_name = d->head_path;
280 break;
281 case WT_STATUS_CHANGED:
282 if (d->new_submodule_commits || d->dirty_submodule) {
283 strbuf_addstr(&extra, " (");
284 if (d->new_submodule_commits)
285 strbuf_addf(&extra, _("new commits, "));
286 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
287 strbuf_addf(&extra, _("modified content, "));
288 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
289 strbuf_addf(&extra, _("untracked content, "));
290 strbuf_setlen(&extra, extra.len - 2);
291 strbuf_addch(&extra, ')');
293 status = d->worktree_status;
294 break;
297 one = quote_path(one_name, -1, &onebuf, s->prefix);
298 two = quote_path(two_name, -1, &twobuf, s->prefix);
300 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
301 switch (status) {
302 case DIFF_STATUS_ADDED:
303 status_printf_more(s, c, _("new file: %s"), one);
304 break;
305 case DIFF_STATUS_COPIED:
306 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
307 break;
308 case DIFF_STATUS_DELETED:
309 status_printf_more(s, c, _("deleted: %s"), one);
310 break;
311 case DIFF_STATUS_MODIFIED:
312 status_printf_more(s, c, _("modified: %s"), one);
313 break;
314 case DIFF_STATUS_RENAMED:
315 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
316 break;
317 case DIFF_STATUS_TYPE_CHANGED:
318 status_printf_more(s, c, _("typechange: %s"), one);
319 break;
320 case DIFF_STATUS_UNKNOWN:
321 status_printf_more(s, c, _("unknown: %s"), one);
322 break;
323 case DIFF_STATUS_UNMERGED:
324 status_printf_more(s, c, _("unmerged: %s"), one);
325 break;
326 default:
327 die(_("bug: unhandled diff status %c"), status);
329 if (extra.len) {
330 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
331 strbuf_release(&extra);
333 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
334 strbuf_release(&onebuf);
335 strbuf_release(&twobuf);
338 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
339 struct diff_options *options,
340 void *data)
342 struct wt_status *s = data;
343 int i;
345 if (!q->nr)
346 return;
347 s->workdir_dirty = 1;
348 for (i = 0; i < q->nr; i++) {
349 struct diff_filepair *p;
350 struct string_list_item *it;
351 struct wt_status_change_data *d;
353 p = q->queue[i];
354 it = string_list_insert(&s->change, p->one->path);
355 d = it->util;
356 if (!d) {
357 d = xcalloc(1, sizeof(*d));
358 it->util = d;
360 if (!d->worktree_status)
361 d->worktree_status = p->status;
362 d->dirty_submodule = p->two->dirty_submodule;
363 if (S_ISGITLINK(p->two->mode))
364 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
368 static int unmerged_mask(const char *path)
370 int pos, mask;
371 struct cache_entry *ce;
373 pos = cache_name_pos(path, strlen(path));
374 if (0 <= pos)
375 return 0;
377 mask = 0;
378 pos = -pos-1;
379 while (pos < active_nr) {
380 ce = active_cache[pos++];
381 if (strcmp(ce->name, path) || !ce_stage(ce))
382 break;
383 mask |= (1 << (ce_stage(ce) - 1));
385 return mask;
388 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
389 struct diff_options *options,
390 void *data)
392 struct wt_status *s = data;
393 int i;
395 for (i = 0; i < q->nr; i++) {
396 struct diff_filepair *p;
397 struct string_list_item *it;
398 struct wt_status_change_data *d;
400 p = q->queue[i];
401 it = string_list_insert(&s->change, p->two->path);
402 d = it->util;
403 if (!d) {
404 d = xcalloc(1, sizeof(*d));
405 it->util = d;
407 if (!d->index_status)
408 d->index_status = p->status;
409 switch (p->status) {
410 case DIFF_STATUS_COPIED:
411 case DIFF_STATUS_RENAMED:
412 d->head_path = xstrdup(p->one->path);
413 break;
414 case DIFF_STATUS_UNMERGED:
415 d->stagemask = unmerged_mask(p->two->path);
416 break;
421 static void wt_status_collect_changes_worktree(struct wt_status *s)
423 struct rev_info rev;
425 init_revisions(&rev, NULL);
426 setup_revisions(0, NULL, &rev, NULL);
427 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
428 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
429 if (!s->show_untracked_files)
430 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
431 if (s->ignore_submodule_arg) {
432 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
433 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
435 rev.diffopt.format_callback = wt_status_collect_changed_cb;
436 rev.diffopt.format_callback_data = s;
437 init_pathspec(&rev.prune_data, s->pathspec);
438 run_diff_files(&rev, 0);
441 static void wt_status_collect_changes_index(struct wt_status *s)
443 struct rev_info rev;
444 struct setup_revision_opt opt;
446 init_revisions(&rev, NULL);
447 memset(&opt, 0, sizeof(opt));
448 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
449 setup_revisions(0, NULL, &rev, &opt);
451 if (s->ignore_submodule_arg) {
452 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
453 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
456 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
457 rev.diffopt.format_callback = wt_status_collect_updated_cb;
458 rev.diffopt.format_callback_data = s;
459 rev.diffopt.detect_rename = 1;
460 rev.diffopt.rename_limit = 200;
461 rev.diffopt.break_opt = 0;
462 init_pathspec(&rev.prune_data, s->pathspec);
463 run_diff_index(&rev, 1);
466 static void wt_status_collect_changes_initial(struct wt_status *s)
468 struct pathspec pathspec;
469 int i;
471 init_pathspec(&pathspec, s->pathspec);
472 for (i = 0; i < active_nr; i++) {
473 struct string_list_item *it;
474 struct wt_status_change_data *d;
475 struct cache_entry *ce = active_cache[i];
477 if (!ce_path_match(ce, &pathspec))
478 continue;
479 it = string_list_insert(&s->change, ce->name);
480 d = it->util;
481 if (!d) {
482 d = xcalloc(1, sizeof(*d));
483 it->util = d;
485 if (ce_stage(ce)) {
486 d->index_status = DIFF_STATUS_UNMERGED;
487 d->stagemask |= (1 << (ce_stage(ce) - 1));
489 else
490 d->index_status = DIFF_STATUS_ADDED;
492 free_pathspec(&pathspec);
495 static void wt_status_collect_untracked(struct wt_status *s)
497 int i;
498 struct dir_struct dir;
500 if (!s->show_untracked_files)
501 return;
502 memset(&dir, 0, sizeof(dir));
503 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
504 dir.flags |=
505 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
506 setup_standard_excludes(&dir);
508 fill_directory(&dir, s->pathspec);
509 for (i = 0; i < dir.nr; i++) {
510 struct dir_entry *ent = dir.entries[i];
511 if (cache_name_is_other(ent->name, ent->len) &&
512 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
513 string_list_insert(&s->untracked, ent->name);
514 free(ent);
517 if (s->show_ignored_files) {
518 dir.nr = 0;
519 dir.flags = DIR_SHOW_IGNORED;
520 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
521 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
522 fill_directory(&dir, s->pathspec);
523 for (i = 0; i < dir.nr; i++) {
524 struct dir_entry *ent = dir.entries[i];
525 if (cache_name_is_other(ent->name, ent->len) &&
526 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
527 string_list_insert(&s->ignored, ent->name);
528 free(ent);
532 free(dir.entries);
535 void wt_status_collect(struct wt_status *s)
537 wt_status_collect_changes_worktree(s);
539 if (s->is_initial)
540 wt_status_collect_changes_initial(s);
541 else
542 wt_status_collect_changes_index(s);
543 wt_status_collect_untracked(s);
546 static void wt_status_print_unmerged(struct wt_status *s)
548 int shown_header = 0;
549 int i;
551 for (i = 0; i < s->change.nr; i++) {
552 struct wt_status_change_data *d;
553 struct string_list_item *it;
554 it = &(s->change.items[i]);
555 d = it->util;
556 if (!d->stagemask)
557 continue;
558 if (!shown_header) {
559 wt_status_print_unmerged_header(s);
560 shown_header = 1;
562 wt_status_print_unmerged_data(s, it);
564 if (shown_header)
565 wt_status_print_trailer(s);
569 static void wt_status_print_updated(struct wt_status *s)
571 int shown_header = 0;
572 int i;
574 for (i = 0; i < s->change.nr; i++) {
575 struct wt_status_change_data *d;
576 struct string_list_item *it;
577 it = &(s->change.items[i]);
578 d = it->util;
579 if (!d->index_status ||
580 d->index_status == DIFF_STATUS_UNMERGED)
581 continue;
582 if (!shown_header) {
583 wt_status_print_cached_header(s);
584 s->commitable = 1;
585 shown_header = 1;
587 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
589 if (shown_header)
590 wt_status_print_trailer(s);
594 * -1 : has delete
595 * 0 : no change
596 * 1 : some change but no delete
598 static int wt_status_check_worktree_changes(struct wt_status *s,
599 int *dirty_submodules)
601 int i;
602 int changes = 0;
604 *dirty_submodules = 0;
606 for (i = 0; i < s->change.nr; i++) {
607 struct wt_status_change_data *d;
608 d = s->change.items[i].util;
609 if (!d->worktree_status ||
610 d->worktree_status == DIFF_STATUS_UNMERGED)
611 continue;
612 if (!changes)
613 changes = 1;
614 if (d->dirty_submodule)
615 *dirty_submodules = 1;
616 if (d->worktree_status == DIFF_STATUS_DELETED)
617 changes = -1;
619 return changes;
622 static void wt_status_print_changed(struct wt_status *s)
624 int i, dirty_submodules;
625 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
627 if (!worktree_changes)
628 return;
630 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
632 for (i = 0; i < s->change.nr; i++) {
633 struct wt_status_change_data *d;
634 struct string_list_item *it;
635 it = &(s->change.items[i]);
636 d = it->util;
637 if (!d->worktree_status ||
638 d->worktree_status == DIFF_STATUS_UNMERGED)
639 continue;
640 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
642 wt_status_print_trailer(s);
645 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
647 struct child_process sm_summary;
648 char summary_limit[64];
649 char index[PATH_MAX];
650 const char *env[] = { NULL, NULL };
651 const char *argv[8];
653 env[0] = index;
654 argv[0] = "submodule";
655 argv[1] = "summary";
656 argv[2] = uncommitted ? "--files" : "--cached";
657 argv[3] = "--for-status";
658 argv[4] = "--summary-limit";
659 argv[5] = summary_limit;
660 argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
661 argv[7] = NULL;
663 sprintf(summary_limit, "%d", s->submodule_summary);
664 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
666 memset(&sm_summary, 0, sizeof(sm_summary));
667 sm_summary.argv = argv;
668 sm_summary.env = env;
669 sm_summary.git_cmd = 1;
670 sm_summary.no_stdin = 1;
671 fflush(s->fp);
672 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
673 run_command(&sm_summary);
676 static void wt_status_print_other(struct wt_status *s,
677 struct string_list *l,
678 const char *what,
679 const char *how)
681 int i;
682 struct strbuf buf = STRBUF_INIT;
683 static struct string_list output = STRING_LIST_INIT_DUP;
684 struct column_options copts;
686 if (!l->nr)
687 return;
689 wt_status_print_other_header(s, what, how);
691 for (i = 0; i < l->nr; i++) {
692 struct string_list_item *it;
693 const char *path;
694 it = &(l->items[i]);
695 path = quote_path(it->string, strlen(it->string),
696 &buf, s->prefix);
697 if (column_active(s->colopts)) {
698 string_list_append(&output, path);
699 continue;
701 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
702 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
703 "%s\n", path);
706 strbuf_release(&buf);
707 if (!column_active(s->colopts))
708 return;
710 strbuf_addf(&buf, "%s#\t%s",
711 color(WT_STATUS_HEADER, s),
712 color(WT_STATUS_UNTRACKED, s));
713 memset(&copts, 0, sizeof(copts));
714 copts.padding = 1;
715 copts.indent = buf.buf;
716 if (want_color(s->use_color))
717 copts.nl = GIT_COLOR_RESET "\n";
718 print_columns(&output, s->colopts, &copts);
719 string_list_clear(&output, 0);
720 strbuf_release(&buf);
723 static void wt_status_print_verbose(struct wt_status *s)
725 struct rev_info rev;
726 struct setup_revision_opt opt;
728 init_revisions(&rev, NULL);
729 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
731 memset(&opt, 0, sizeof(opt));
732 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
733 setup_revisions(0, NULL, &rev, &opt);
735 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
736 rev.diffopt.detect_rename = 1;
737 rev.diffopt.file = s->fp;
738 rev.diffopt.close_file = 0;
740 * If we're not going to stdout, then we definitely don't
741 * want color, since we are going to the commit message
742 * file (and even the "auto" setting won't work, since it
743 * will have checked isatty on stdout).
745 if (s->fp != stdout)
746 rev.diffopt.use_color = 0;
747 run_diff_index(&rev, 1);
750 static void wt_status_print_tracking(struct wt_status *s)
752 struct strbuf sb = STRBUF_INIT;
753 const char *cp, *ep;
754 struct branch *branch;
756 assert(s->branch && !s->is_initial);
757 if (prefixcmp(s->branch, "refs/heads/"))
758 return;
759 branch = branch_get(s->branch + 11);
760 if (!format_tracking_info(branch, &sb))
761 return;
763 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
764 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
765 "%c %.*s", comment_line_char,
766 (int)(ep - cp), cp);
767 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
768 comment_line_char);
771 static int has_unmerged(struct wt_status *s)
773 int i;
775 for (i = 0; i < s->change.nr; i++) {
776 struct wt_status_change_data *d;
777 d = s->change.items[i].util;
778 if (d->stagemask)
779 return 1;
781 return 0;
784 static void show_merge_in_progress(struct wt_status *s,
785 struct wt_status_state *state,
786 const char *color)
788 if (has_unmerged(s)) {
789 status_printf_ln(s, color, _("You have unmerged paths."));
790 if (advice_status_hints)
791 status_printf_ln(s, color,
792 _(" (fix conflicts and run \"git commit\")"));
793 } else {
794 status_printf_ln(s, color,
795 _("All conflicts fixed but you are still merging."));
796 if (advice_status_hints)
797 status_printf_ln(s, color,
798 _(" (use \"git commit\" to conclude merge)"));
800 wt_status_print_trailer(s);
803 static void show_am_in_progress(struct wt_status *s,
804 struct wt_status_state *state,
805 const char *color)
807 status_printf_ln(s, color,
808 _("You are in the middle of an am session."));
809 if (state->am_empty_patch)
810 status_printf_ln(s, color,
811 _("The current patch is empty."));
812 if (advice_status_hints) {
813 if (!state->am_empty_patch)
814 status_printf_ln(s, color,
815 _(" (fix conflicts and then run \"git am --resolved\")"));
816 status_printf_ln(s, color,
817 _(" (use \"git am --skip\" to skip this patch)"));
818 status_printf_ln(s, color,
819 _(" (use \"git am --abort\" to restore the original branch)"));
821 wt_status_print_trailer(s);
824 static char *read_line_from_git_path(const char *filename)
826 struct strbuf buf = STRBUF_INIT;
827 FILE *fp = fopen(git_path("%s", filename), "r");
828 if (!fp) {
829 strbuf_release(&buf);
830 return NULL;
832 strbuf_getline(&buf, fp, '\n');
833 if (!fclose(fp)) {
834 return strbuf_detach(&buf, NULL);
835 } else {
836 strbuf_release(&buf);
837 return NULL;
841 static int split_commit_in_progress(struct wt_status *s)
843 int split_in_progress = 0;
844 char *head = read_line_from_git_path("HEAD");
845 char *orig_head = read_line_from_git_path("ORIG_HEAD");
846 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
847 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
849 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
850 !s->branch || strcmp(s->branch, "HEAD"))
851 return split_in_progress;
853 if (!strcmp(rebase_amend, rebase_orig_head)) {
854 if (strcmp(head, rebase_amend))
855 split_in_progress = 1;
856 } else if (strcmp(orig_head, rebase_orig_head)) {
857 split_in_progress = 1;
860 if (!s->amend && !s->nowarn && !s->workdir_dirty)
861 split_in_progress = 0;
863 free(head);
864 free(orig_head);
865 free(rebase_amend);
866 free(rebase_orig_head);
867 return split_in_progress;
870 static void show_rebase_in_progress(struct wt_status *s,
871 struct wt_status_state *state,
872 const char *color)
874 struct stat st;
876 if (has_unmerged(s)) {
877 if (state->branch)
878 status_printf_ln(s, color,
879 _("You are currently rebasing branch '%s' on '%s'."),
880 state->branch,
881 state->onto);
882 else
883 status_printf_ln(s, color,
884 _("You are currently rebasing."));
885 if (advice_status_hints) {
886 status_printf_ln(s, color,
887 _(" (fix conflicts and then run \"git rebase --continue\")"));
888 status_printf_ln(s, color,
889 _(" (use \"git rebase --skip\" to skip this patch)"));
890 status_printf_ln(s, color,
891 _(" (use \"git rebase --abort\" to check out the original branch)"));
893 } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
894 if (state->branch)
895 status_printf_ln(s, color,
896 _("You are currently rebasing branch '%s' on '%s'."),
897 state->branch,
898 state->onto);
899 else
900 status_printf_ln(s, color,
901 _("You are currently rebasing."));
902 if (advice_status_hints)
903 status_printf_ln(s, color,
904 _(" (all conflicts fixed: run \"git rebase --continue\")"));
905 } else if (split_commit_in_progress(s)) {
906 if (state->branch)
907 status_printf_ln(s, color,
908 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
909 state->branch,
910 state->onto);
911 else
912 status_printf_ln(s, color,
913 _("You are currently splitting a commit during a rebase."));
914 if (advice_status_hints)
915 status_printf_ln(s, color,
916 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
917 } else {
918 if (state->branch)
919 status_printf_ln(s, color,
920 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
921 state->branch,
922 state->onto);
923 else
924 status_printf_ln(s, color,
925 _("You are currently editing a commit during a rebase."));
926 if (advice_status_hints && !s->amend) {
927 status_printf_ln(s, color,
928 _(" (use \"git commit --amend\" to amend the current commit)"));
929 status_printf_ln(s, color,
930 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
933 wt_status_print_trailer(s);
936 static void show_cherry_pick_in_progress(struct wt_status *s,
937 struct wt_status_state *state,
938 const char *color)
940 status_printf_ln(s, color, _("You are currently cherry-picking."));
941 if (advice_status_hints) {
942 if (has_unmerged(s))
943 status_printf_ln(s, color,
944 _(" (fix conflicts and run \"git commit\")"));
945 else
946 status_printf_ln(s, color,
947 _(" (all conflicts fixed: run \"git commit\")"));
949 wt_status_print_trailer(s);
952 static void show_bisect_in_progress(struct wt_status *s,
953 struct wt_status_state *state,
954 const char *color)
956 if (state->branch)
957 status_printf_ln(s, color,
958 _("You are currently bisecting branch '%s'."),
959 state->branch);
960 else
961 status_printf_ln(s, color,
962 _("You are currently bisecting."));
963 if (advice_status_hints)
964 status_printf_ln(s, color,
965 _(" (use \"git bisect reset\" to get back to the original branch)"));
966 wt_status_print_trailer(s);
970 * Extract branch information from rebase/bisect
972 static void read_and_strip_branch(struct strbuf *sb,
973 const char **branch,
974 const char *path)
976 unsigned char sha1[20];
978 strbuf_reset(sb);
979 if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
980 return;
982 while (sb->len && sb->buf[sb->len - 1] == '\n')
983 strbuf_setlen(sb, sb->len - 1);
984 if (!sb->len)
985 return;
986 if (!prefixcmp(sb->buf, "refs/heads/"))
987 *branch = sb->buf + strlen("refs/heads/");
988 else if (!prefixcmp(sb->buf, "refs/"))
989 *branch = sb->buf;
990 else if (!get_sha1_hex(sb->buf, sha1)) {
991 const char *abbrev;
992 abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
993 strbuf_reset(sb);
994 strbuf_addstr(sb, abbrev);
995 *branch = sb->buf;
996 } else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
998 else /* bisect */
999 *branch = sb->buf;
1002 static void wt_status_print_state(struct wt_status *s)
1004 const char *state_color = color(WT_STATUS_HEADER, s);
1005 struct strbuf branch = STRBUF_INIT;
1006 struct strbuf onto = STRBUF_INIT;
1007 struct wt_status_state state;
1008 struct stat st;
1010 memset(&state, 0, sizeof(state));
1012 if (!stat(git_path("MERGE_HEAD"), &st)) {
1013 state.merge_in_progress = 1;
1014 } else if (!stat(git_path("rebase-apply"), &st)) {
1015 if (!stat(git_path("rebase-apply/applying"), &st)) {
1016 state.am_in_progress = 1;
1017 if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1018 state.am_empty_patch = 1;
1019 } else {
1020 state.rebase_in_progress = 1;
1021 read_and_strip_branch(&branch, &state.branch,
1022 "rebase-apply/head-name");
1023 read_and_strip_branch(&onto, &state.onto,
1024 "rebase-apply/onto");
1026 } else if (!stat(git_path("rebase-merge"), &st)) {
1027 if (!stat(git_path("rebase-merge/interactive"), &st))
1028 state.rebase_interactive_in_progress = 1;
1029 else
1030 state.rebase_in_progress = 1;
1031 read_and_strip_branch(&branch, &state.branch,
1032 "rebase-merge/head-name");
1033 read_and_strip_branch(&onto, &state.onto,
1034 "rebase-merge/onto");
1035 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1036 state.cherry_pick_in_progress = 1;
1038 if (!stat(git_path("BISECT_LOG"), &st)) {
1039 state.bisect_in_progress = 1;
1040 read_and_strip_branch(&branch, &state.branch,
1041 "BISECT_START");
1044 if (state.merge_in_progress)
1045 show_merge_in_progress(s, &state, state_color);
1046 else if (state.am_in_progress)
1047 show_am_in_progress(s, &state, state_color);
1048 else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1049 show_rebase_in_progress(s, &state, state_color);
1050 else if (state.cherry_pick_in_progress)
1051 show_cherry_pick_in_progress(s, &state, state_color);
1052 if (state.bisect_in_progress)
1053 show_bisect_in_progress(s, &state, state_color);
1054 strbuf_release(&branch);
1055 strbuf_release(&onto);
1058 void wt_status_print(struct wt_status *s)
1060 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1061 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1063 if (s->branch) {
1064 const char *on_what = _("On branch ");
1065 const char *branch_name = s->branch;
1066 if (!prefixcmp(branch_name, "refs/heads/"))
1067 branch_name += 11;
1068 else if (!strcmp(branch_name, "HEAD")) {
1069 branch_name = "";
1070 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1071 on_what = _("Not currently on any branch.");
1073 status_printf(s, color(WT_STATUS_HEADER, s), "");
1074 status_printf_more(s, branch_status_color, "%s", on_what);
1075 status_printf_more(s, branch_color, "%s\n", branch_name);
1076 if (!s->is_initial)
1077 wt_status_print_tracking(s);
1080 wt_status_print_state(s);
1081 if (s->is_initial) {
1082 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1083 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1084 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1087 wt_status_print_updated(s);
1088 wt_status_print_unmerged(s);
1089 wt_status_print_changed(s);
1090 if (s->submodule_summary &&
1091 (!s->ignore_submodule_arg ||
1092 strcmp(s->ignore_submodule_arg, "all"))) {
1093 wt_status_print_submodule_summary(s, 0); /* staged */
1094 wt_status_print_submodule_summary(s, 1); /* unstaged */
1096 if (s->show_untracked_files) {
1097 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1098 if (s->show_ignored_files)
1099 wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1100 } else if (s->commitable)
1101 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1102 advice_status_hints
1103 ? _(" (use -u option to show untracked files)") : "");
1105 if (s->verbose)
1106 wt_status_print_verbose(s);
1107 if (!s->commitable) {
1108 if (s->amend)
1109 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1110 else if (s->nowarn)
1111 ; /* nothing */
1112 else if (s->workdir_dirty) {
1113 if (advice_status_hints)
1114 printf(_("no changes added to commit "
1115 "(use \"git add\" and/or \"git commit -a\")\n"));
1116 else
1117 printf(_("no changes added to commit\n"));
1118 } else if (s->untracked.nr) {
1119 if (advice_status_hints)
1120 printf(_("nothing added to commit but untracked files "
1121 "present (use \"git add\" to track)\n"));
1122 else
1123 printf(_("nothing added to commit but untracked files present\n"));
1124 } else if (s->is_initial) {
1125 if (advice_status_hints)
1126 printf(_("nothing to commit (create/copy files "
1127 "and use \"git add\" to track)\n"));
1128 else
1129 printf(_("nothing to commit\n"));
1130 } else if (!s->show_untracked_files) {
1131 if (advice_status_hints)
1132 printf(_("nothing to commit (use -u to show untracked files)\n"));
1133 else
1134 printf(_("nothing to commit\n"));
1135 } else
1136 printf(_("nothing to commit, working directory clean\n"));
1140 static void wt_shortstatus_unmerged(struct string_list_item *it,
1141 struct wt_status *s)
1143 struct wt_status_change_data *d = it->util;
1144 const char *how = "??";
1146 switch (d->stagemask) {
1147 case 1: how = "DD"; break; /* both deleted */
1148 case 2: how = "AU"; break; /* added by us */
1149 case 3: how = "UD"; break; /* deleted by them */
1150 case 4: how = "UA"; break; /* added by them */
1151 case 5: how = "DU"; break; /* deleted by us */
1152 case 6: how = "AA"; break; /* both added */
1153 case 7: how = "UU"; break; /* both modified */
1155 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1156 if (s->null_termination) {
1157 fprintf(stdout, " %s%c", it->string, 0);
1158 } else {
1159 struct strbuf onebuf = STRBUF_INIT;
1160 const char *one;
1161 one = quote_path(it->string, -1, &onebuf, s->prefix);
1162 printf(" %s\n", one);
1163 strbuf_release(&onebuf);
1167 static void wt_shortstatus_status(struct string_list_item *it,
1168 struct wt_status *s)
1170 struct wt_status_change_data *d = it->util;
1172 if (d->index_status)
1173 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1174 else
1175 putchar(' ');
1176 if (d->worktree_status)
1177 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1178 else
1179 putchar(' ');
1180 putchar(' ');
1181 if (s->null_termination) {
1182 fprintf(stdout, "%s%c", it->string, 0);
1183 if (d->head_path)
1184 fprintf(stdout, "%s%c", d->head_path, 0);
1185 } else {
1186 struct strbuf onebuf = STRBUF_INIT;
1187 const char *one;
1188 if (d->head_path) {
1189 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
1190 if (*one != '"' && strchr(one, ' ') != NULL) {
1191 putchar('"');
1192 strbuf_addch(&onebuf, '"');
1193 one = onebuf.buf;
1195 printf("%s -> ", one);
1196 strbuf_release(&onebuf);
1198 one = quote_path(it->string, -1, &onebuf, s->prefix);
1199 if (*one != '"' && strchr(one, ' ') != NULL) {
1200 putchar('"');
1201 strbuf_addch(&onebuf, '"');
1202 one = onebuf.buf;
1204 printf("%s\n", one);
1205 strbuf_release(&onebuf);
1209 static void wt_shortstatus_other(struct string_list_item *it,
1210 struct wt_status *s, const char *sign)
1212 if (s->null_termination) {
1213 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1214 } else {
1215 struct strbuf onebuf = STRBUF_INIT;
1216 const char *one;
1217 one = quote_path(it->string, -1, &onebuf, s->prefix);
1218 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1219 printf(" %s\n", one);
1220 strbuf_release(&onebuf);
1224 static void wt_shortstatus_print_tracking(struct wt_status *s)
1226 struct branch *branch;
1227 const char *header_color = color(WT_STATUS_HEADER, s);
1228 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1229 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1231 const char *base;
1232 const char *branch_name;
1233 int num_ours, num_theirs;
1235 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1237 if (!s->branch)
1238 return;
1239 branch_name = s->branch;
1241 if (!prefixcmp(branch_name, "refs/heads/"))
1242 branch_name += 11;
1243 else if (!strcmp(branch_name, "HEAD")) {
1244 branch_name = _("HEAD (no branch)");
1245 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1248 branch = branch_get(s->branch + 11);
1249 if (s->is_initial)
1250 color_fprintf(s->fp, header_color, _("Initial commit on "));
1251 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1252 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1253 fputc(s->null_termination ? '\0' : '\n', s->fp);
1254 return;
1257 base = branch->merge[0]->dst;
1258 base = shorten_unambiguous_ref(base, 0);
1259 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1260 color_fprintf(s->fp, header_color, "...");
1261 color_fprintf(s->fp, branch_color_remote, "%s", base);
1263 color_fprintf(s->fp, header_color, " [");
1264 if (!num_ours) {
1265 color_fprintf(s->fp, header_color, _("behind "));
1266 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1267 } else if (!num_theirs) {
1268 color_fprintf(s->fp, header_color, _("ahead "));
1269 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1270 } else {
1271 color_fprintf(s->fp, header_color, _("ahead "));
1272 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1273 color_fprintf(s->fp, header_color, _(", behind "));
1274 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1277 color_fprintf(s->fp, header_color, "]");
1278 fputc(s->null_termination ? '\0' : '\n', s->fp);
1281 void wt_shortstatus_print(struct wt_status *s)
1283 int i;
1285 if (s->show_branch)
1286 wt_shortstatus_print_tracking(s);
1288 for (i = 0; i < s->change.nr; i++) {
1289 struct wt_status_change_data *d;
1290 struct string_list_item *it;
1292 it = &(s->change.items[i]);
1293 d = it->util;
1294 if (d->stagemask)
1295 wt_shortstatus_unmerged(it, s);
1296 else
1297 wt_shortstatus_status(it, s);
1299 for (i = 0; i < s->untracked.nr; i++) {
1300 struct string_list_item *it;
1302 it = &(s->untracked.items[i]);
1303 wt_shortstatus_other(it, s, "??");
1305 for (i = 0; i < s->ignored.nr; i++) {
1306 struct string_list_item *it;
1308 it = &(s->ignored.items[i]);
1309 wt_shortstatus_other(it, s, "!!");
1313 void wt_porcelain_print(struct wt_status *s)
1315 s->use_color = 0;
1316 s->relative_paths = 0;
1317 s->prefix = NULL;
1318 wt_shortstatus_print(s);