Merge branch 'maint-1.8.1' into maint
[git.git] / wt-status.c
blob75558177860d14120636859e2dcc29b48b72a721
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;
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;
295 default:
296 die("BUG: unhandled change_type %d in wt_status_print_change_data",
297 change_type);
300 one = quote_path(one_name, -1, &onebuf, s->prefix);
301 two = quote_path(two_name, -1, &twobuf, s->prefix);
303 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
304 switch (status) {
305 case DIFF_STATUS_ADDED:
306 status_printf_more(s, c, _("new file: %s"), one);
307 break;
308 case DIFF_STATUS_COPIED:
309 status_printf_more(s, c, _("copied: %s -> %s"), one, two);
310 break;
311 case DIFF_STATUS_DELETED:
312 status_printf_more(s, c, _("deleted: %s"), one);
313 break;
314 case DIFF_STATUS_MODIFIED:
315 status_printf_more(s, c, _("modified: %s"), one);
316 break;
317 case DIFF_STATUS_RENAMED:
318 status_printf_more(s, c, _("renamed: %s -> %s"), one, two);
319 break;
320 case DIFF_STATUS_TYPE_CHANGED:
321 status_printf_more(s, c, _("typechange: %s"), one);
322 break;
323 case DIFF_STATUS_UNKNOWN:
324 status_printf_more(s, c, _("unknown: %s"), one);
325 break;
326 case DIFF_STATUS_UNMERGED:
327 status_printf_more(s, c, _("unmerged: %s"), one);
328 break;
329 default:
330 die(_("bug: unhandled diff status %c"), status);
332 if (extra.len) {
333 status_printf_more(s, color(WT_STATUS_HEADER, s), "%s", extra.buf);
334 strbuf_release(&extra);
336 status_printf_more(s, GIT_COLOR_NORMAL, "\n");
337 strbuf_release(&onebuf);
338 strbuf_release(&twobuf);
341 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
342 struct diff_options *options,
343 void *data)
345 struct wt_status *s = data;
346 int i;
348 if (!q->nr)
349 return;
350 s->workdir_dirty = 1;
351 for (i = 0; i < q->nr; i++) {
352 struct diff_filepair *p;
353 struct string_list_item *it;
354 struct wt_status_change_data *d;
356 p = q->queue[i];
357 it = string_list_insert(&s->change, p->one->path);
358 d = it->util;
359 if (!d) {
360 d = xcalloc(1, sizeof(*d));
361 it->util = d;
363 if (!d->worktree_status)
364 d->worktree_status = p->status;
365 d->dirty_submodule = p->two->dirty_submodule;
366 if (S_ISGITLINK(p->two->mode))
367 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
371 static int unmerged_mask(const char *path)
373 int pos, mask;
374 struct cache_entry *ce;
376 pos = cache_name_pos(path, strlen(path));
377 if (0 <= pos)
378 return 0;
380 mask = 0;
381 pos = -pos-1;
382 while (pos < active_nr) {
383 ce = active_cache[pos++];
384 if (strcmp(ce->name, path) || !ce_stage(ce))
385 break;
386 mask |= (1 << (ce_stage(ce) - 1));
388 return mask;
391 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
392 struct diff_options *options,
393 void *data)
395 struct wt_status *s = data;
396 int i;
398 for (i = 0; i < q->nr; i++) {
399 struct diff_filepair *p;
400 struct string_list_item *it;
401 struct wt_status_change_data *d;
403 p = q->queue[i];
404 it = string_list_insert(&s->change, p->two->path);
405 d = it->util;
406 if (!d) {
407 d = xcalloc(1, sizeof(*d));
408 it->util = d;
410 if (!d->index_status)
411 d->index_status = p->status;
412 switch (p->status) {
413 case DIFF_STATUS_COPIED:
414 case DIFF_STATUS_RENAMED:
415 d->head_path = xstrdup(p->one->path);
416 break;
417 case DIFF_STATUS_UNMERGED:
418 d->stagemask = unmerged_mask(p->two->path);
419 break;
424 static void wt_status_collect_changes_worktree(struct wt_status *s)
426 struct rev_info rev;
428 init_revisions(&rev, NULL);
429 setup_revisions(0, NULL, &rev, NULL);
430 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
431 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
432 if (!s->show_untracked_files)
433 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
434 if (s->ignore_submodule_arg) {
435 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
436 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
438 rev.diffopt.format_callback = wt_status_collect_changed_cb;
439 rev.diffopt.format_callback_data = s;
440 init_pathspec(&rev.prune_data, s->pathspec);
441 run_diff_files(&rev, 0);
444 static void wt_status_collect_changes_index(struct wt_status *s)
446 struct rev_info rev;
447 struct setup_revision_opt opt;
449 init_revisions(&rev, NULL);
450 memset(&opt, 0, sizeof(opt));
451 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
452 setup_revisions(0, NULL, &rev, &opt);
454 if (s->ignore_submodule_arg) {
455 DIFF_OPT_SET(&rev.diffopt, OVERRIDE_SUBMODULE_CONFIG);
456 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
459 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
460 rev.diffopt.format_callback = wt_status_collect_updated_cb;
461 rev.diffopt.format_callback_data = s;
462 rev.diffopt.detect_rename = 1;
463 rev.diffopt.rename_limit = 200;
464 rev.diffopt.break_opt = 0;
465 init_pathspec(&rev.prune_data, s->pathspec);
466 run_diff_index(&rev, 1);
469 static void wt_status_collect_changes_initial(struct wt_status *s)
471 struct pathspec pathspec;
472 int i;
474 init_pathspec(&pathspec, s->pathspec);
475 for (i = 0; i < active_nr; i++) {
476 struct string_list_item *it;
477 struct wt_status_change_data *d;
478 struct cache_entry *ce = active_cache[i];
480 if (!ce_path_match(ce, &pathspec))
481 continue;
482 it = string_list_insert(&s->change, ce->name);
483 d = it->util;
484 if (!d) {
485 d = xcalloc(1, sizeof(*d));
486 it->util = d;
488 if (ce_stage(ce)) {
489 d->index_status = DIFF_STATUS_UNMERGED;
490 d->stagemask |= (1 << (ce_stage(ce) - 1));
492 else
493 d->index_status = DIFF_STATUS_ADDED;
495 free_pathspec(&pathspec);
498 static void wt_status_collect_untracked(struct wt_status *s)
500 int i;
501 struct dir_struct dir;
503 if (!s->show_untracked_files)
504 return;
505 memset(&dir, 0, sizeof(dir));
506 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
507 dir.flags |=
508 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
509 setup_standard_excludes(&dir);
511 fill_directory(&dir, s->pathspec);
512 for (i = 0; i < dir.nr; i++) {
513 struct dir_entry *ent = dir.entries[i];
514 if (cache_name_is_other(ent->name, ent->len) &&
515 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
516 string_list_insert(&s->untracked, ent->name);
517 free(ent);
520 if (s->show_ignored_files) {
521 dir.nr = 0;
522 dir.flags = DIR_SHOW_IGNORED;
523 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
524 dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
525 fill_directory(&dir, s->pathspec);
526 for (i = 0; i < dir.nr; i++) {
527 struct dir_entry *ent = dir.entries[i];
528 if (cache_name_is_other(ent->name, ent->len) &&
529 match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
530 string_list_insert(&s->ignored, ent->name);
531 free(ent);
535 free(dir.entries);
538 void wt_status_collect(struct wt_status *s)
540 wt_status_collect_changes_worktree(s);
542 if (s->is_initial)
543 wt_status_collect_changes_initial(s);
544 else
545 wt_status_collect_changes_index(s);
546 wt_status_collect_untracked(s);
549 static void wt_status_print_unmerged(struct wt_status *s)
551 int shown_header = 0;
552 int i;
554 for (i = 0; i < s->change.nr; i++) {
555 struct wt_status_change_data *d;
556 struct string_list_item *it;
557 it = &(s->change.items[i]);
558 d = it->util;
559 if (!d->stagemask)
560 continue;
561 if (!shown_header) {
562 wt_status_print_unmerged_header(s);
563 shown_header = 1;
565 wt_status_print_unmerged_data(s, it);
567 if (shown_header)
568 wt_status_print_trailer(s);
572 static void wt_status_print_updated(struct wt_status *s)
574 int shown_header = 0;
575 int i;
577 for (i = 0; i < s->change.nr; i++) {
578 struct wt_status_change_data *d;
579 struct string_list_item *it;
580 it = &(s->change.items[i]);
581 d = it->util;
582 if (!d->index_status ||
583 d->index_status == DIFF_STATUS_UNMERGED)
584 continue;
585 if (!shown_header) {
586 wt_status_print_cached_header(s);
587 s->commitable = 1;
588 shown_header = 1;
590 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
592 if (shown_header)
593 wt_status_print_trailer(s);
597 * -1 : has delete
598 * 0 : no change
599 * 1 : some change but no delete
601 static int wt_status_check_worktree_changes(struct wt_status *s,
602 int *dirty_submodules)
604 int i;
605 int changes = 0;
607 *dirty_submodules = 0;
609 for (i = 0; i < s->change.nr; i++) {
610 struct wt_status_change_data *d;
611 d = s->change.items[i].util;
612 if (!d->worktree_status ||
613 d->worktree_status == DIFF_STATUS_UNMERGED)
614 continue;
615 if (!changes)
616 changes = 1;
617 if (d->dirty_submodule)
618 *dirty_submodules = 1;
619 if (d->worktree_status == DIFF_STATUS_DELETED)
620 changes = -1;
622 return changes;
625 static void wt_status_print_changed(struct wt_status *s)
627 int i, dirty_submodules;
628 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
630 if (!worktree_changes)
631 return;
633 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
635 for (i = 0; i < s->change.nr; i++) {
636 struct wt_status_change_data *d;
637 struct string_list_item *it;
638 it = &(s->change.items[i]);
639 d = it->util;
640 if (!d->worktree_status ||
641 d->worktree_status == DIFF_STATUS_UNMERGED)
642 continue;
643 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
645 wt_status_print_trailer(s);
648 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
650 struct child_process sm_summary;
651 char summary_limit[64];
652 char index[PATH_MAX];
653 const char *env[] = { NULL, NULL };
654 const char *argv[8];
656 env[0] = index;
657 argv[0] = "submodule";
658 argv[1] = "summary";
659 argv[2] = uncommitted ? "--files" : "--cached";
660 argv[3] = "--for-status";
661 argv[4] = "--summary-limit";
662 argv[5] = summary_limit;
663 argv[6] = uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD");
664 argv[7] = NULL;
666 sprintf(summary_limit, "%d", s->submodule_summary);
667 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
669 memset(&sm_summary, 0, sizeof(sm_summary));
670 sm_summary.argv = argv;
671 sm_summary.env = env;
672 sm_summary.git_cmd = 1;
673 sm_summary.no_stdin = 1;
674 fflush(s->fp);
675 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
676 run_command(&sm_summary);
679 static void wt_status_print_other(struct wt_status *s,
680 struct string_list *l,
681 const char *what,
682 const char *how)
684 int i;
685 struct strbuf buf = STRBUF_INIT;
686 static struct string_list output = STRING_LIST_INIT_DUP;
687 struct column_options copts;
689 if (!l->nr)
690 return;
692 wt_status_print_other_header(s, what, how);
694 for (i = 0; i < l->nr; i++) {
695 struct string_list_item *it;
696 const char *path;
697 it = &(l->items[i]);
698 path = quote_path(it->string, strlen(it->string),
699 &buf, s->prefix);
700 if (column_active(s->colopts)) {
701 string_list_append(&output, path);
702 continue;
704 status_printf(s, color(WT_STATUS_HEADER, s), "\t");
705 status_printf_more(s, color(WT_STATUS_UNTRACKED, s),
706 "%s\n", path);
709 strbuf_release(&buf);
710 if (!column_active(s->colopts))
711 return;
713 strbuf_addf(&buf, "%s#\t%s",
714 color(WT_STATUS_HEADER, s),
715 color(WT_STATUS_UNTRACKED, s));
716 memset(&copts, 0, sizeof(copts));
717 copts.padding = 1;
718 copts.indent = buf.buf;
719 if (want_color(s->use_color))
720 copts.nl = GIT_COLOR_RESET "\n";
721 print_columns(&output, s->colopts, &copts);
722 string_list_clear(&output, 0);
723 strbuf_release(&buf);
726 static void wt_status_print_verbose(struct wt_status *s)
728 struct rev_info rev;
729 struct setup_revision_opt opt;
731 init_revisions(&rev, NULL);
732 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
734 memset(&opt, 0, sizeof(opt));
735 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
736 setup_revisions(0, NULL, &rev, &opt);
738 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
739 rev.diffopt.detect_rename = 1;
740 rev.diffopt.file = s->fp;
741 rev.diffopt.close_file = 0;
743 * If we're not going to stdout, then we definitely don't
744 * want color, since we are going to the commit message
745 * file (and even the "auto" setting won't work, since it
746 * will have checked isatty on stdout).
748 if (s->fp != stdout)
749 rev.diffopt.use_color = 0;
750 run_diff_index(&rev, 1);
753 static void wt_status_print_tracking(struct wt_status *s)
755 struct strbuf sb = STRBUF_INIT;
756 const char *cp, *ep;
757 struct branch *branch;
759 assert(s->branch && !s->is_initial);
760 if (prefixcmp(s->branch, "refs/heads/"))
761 return;
762 branch = branch_get(s->branch + 11);
763 if (!format_tracking_info(branch, &sb))
764 return;
766 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
767 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
768 "%c %.*s", comment_line_char,
769 (int)(ep - cp), cp);
770 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "%c",
771 comment_line_char);
774 static int has_unmerged(struct wt_status *s)
776 int i;
778 for (i = 0; i < s->change.nr; i++) {
779 struct wt_status_change_data *d;
780 d = s->change.items[i].util;
781 if (d->stagemask)
782 return 1;
784 return 0;
787 static void show_merge_in_progress(struct wt_status *s,
788 struct wt_status_state *state,
789 const char *color)
791 if (has_unmerged(s)) {
792 status_printf_ln(s, color, _("You have unmerged paths."));
793 if (advice_status_hints)
794 status_printf_ln(s, color,
795 _(" (fix conflicts and run \"git commit\")"));
796 } else {
797 status_printf_ln(s, color,
798 _("All conflicts fixed but you are still merging."));
799 if (advice_status_hints)
800 status_printf_ln(s, color,
801 _(" (use \"git commit\" to conclude merge)"));
803 wt_status_print_trailer(s);
806 static void show_am_in_progress(struct wt_status *s,
807 struct wt_status_state *state,
808 const char *color)
810 status_printf_ln(s, color,
811 _("You are in the middle of an am session."));
812 if (state->am_empty_patch)
813 status_printf_ln(s, color,
814 _("The current patch is empty."));
815 if (advice_status_hints) {
816 if (!state->am_empty_patch)
817 status_printf_ln(s, color,
818 _(" (fix conflicts and then run \"git am --resolved\")"));
819 status_printf_ln(s, color,
820 _(" (use \"git am --skip\" to skip this patch)"));
821 status_printf_ln(s, color,
822 _(" (use \"git am --abort\" to restore the original branch)"));
824 wt_status_print_trailer(s);
827 static char *read_line_from_git_path(const char *filename)
829 struct strbuf buf = STRBUF_INIT;
830 FILE *fp = fopen(git_path("%s", filename), "r");
831 if (!fp) {
832 strbuf_release(&buf);
833 return NULL;
835 strbuf_getline(&buf, fp, '\n');
836 if (!fclose(fp)) {
837 return strbuf_detach(&buf, NULL);
838 } else {
839 strbuf_release(&buf);
840 return NULL;
844 static int split_commit_in_progress(struct wt_status *s)
846 int split_in_progress = 0;
847 char *head = read_line_from_git_path("HEAD");
848 char *orig_head = read_line_from_git_path("ORIG_HEAD");
849 char *rebase_amend = read_line_from_git_path("rebase-merge/amend");
850 char *rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
852 if (!head || !orig_head || !rebase_amend || !rebase_orig_head ||
853 !s->branch || strcmp(s->branch, "HEAD"))
854 return split_in_progress;
856 if (!strcmp(rebase_amend, rebase_orig_head)) {
857 if (strcmp(head, rebase_amend))
858 split_in_progress = 1;
859 } else if (strcmp(orig_head, rebase_orig_head)) {
860 split_in_progress = 1;
863 if (!s->amend && !s->nowarn && !s->workdir_dirty)
864 split_in_progress = 0;
866 free(head);
867 free(orig_head);
868 free(rebase_amend);
869 free(rebase_orig_head);
870 return split_in_progress;
873 static void show_rebase_in_progress(struct wt_status *s,
874 struct wt_status_state *state,
875 const char *color)
877 struct stat st;
879 if (has_unmerged(s)) {
880 if (state->branch)
881 status_printf_ln(s, color,
882 _("You are currently rebasing branch '%s' on '%s'."),
883 state->branch,
884 state->onto);
885 else
886 status_printf_ln(s, color,
887 _("You are currently rebasing."));
888 if (advice_status_hints) {
889 status_printf_ln(s, color,
890 _(" (fix conflicts and then run \"git rebase --continue\")"));
891 status_printf_ln(s, color,
892 _(" (use \"git rebase --skip\" to skip this patch)"));
893 status_printf_ln(s, color,
894 _(" (use \"git rebase --abort\" to check out the original branch)"));
896 } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
897 if (state->branch)
898 status_printf_ln(s, color,
899 _("You are currently rebasing branch '%s' on '%s'."),
900 state->branch,
901 state->onto);
902 else
903 status_printf_ln(s, color,
904 _("You are currently rebasing."));
905 if (advice_status_hints)
906 status_printf_ln(s, color,
907 _(" (all conflicts fixed: run \"git rebase --continue\")"));
908 } else if (split_commit_in_progress(s)) {
909 if (state->branch)
910 status_printf_ln(s, color,
911 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
912 state->branch,
913 state->onto);
914 else
915 status_printf_ln(s, color,
916 _("You are currently splitting a commit during a rebase."));
917 if (advice_status_hints)
918 status_printf_ln(s, color,
919 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
920 } else {
921 if (state->branch)
922 status_printf_ln(s, color,
923 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
924 state->branch,
925 state->onto);
926 else
927 status_printf_ln(s, color,
928 _("You are currently editing a commit during a rebase."));
929 if (advice_status_hints && !s->amend) {
930 status_printf_ln(s, color,
931 _(" (use \"git commit --amend\" to amend the current commit)"));
932 status_printf_ln(s, color,
933 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
936 wt_status_print_trailer(s);
939 static void show_cherry_pick_in_progress(struct wt_status *s,
940 struct wt_status_state *state,
941 const char *color)
943 status_printf_ln(s, color, _("You are currently cherry-picking."));
944 if (advice_status_hints) {
945 if (has_unmerged(s))
946 status_printf_ln(s, color,
947 _(" (fix conflicts and run \"git commit\")"));
948 else
949 status_printf_ln(s, color,
950 _(" (all conflicts fixed: run \"git commit\")"));
952 wt_status_print_trailer(s);
955 static void show_bisect_in_progress(struct wt_status *s,
956 struct wt_status_state *state,
957 const char *color)
959 if (state->branch)
960 status_printf_ln(s, color,
961 _("You are currently bisecting branch '%s'."),
962 state->branch);
963 else
964 status_printf_ln(s, color,
965 _("You are currently bisecting."));
966 if (advice_status_hints)
967 status_printf_ln(s, color,
968 _(" (use \"git bisect reset\" to get back to the original branch)"));
969 wt_status_print_trailer(s);
973 * Extract branch information from rebase/bisect
975 static void read_and_strip_branch(struct strbuf *sb,
976 const char **branch,
977 const char *path)
979 unsigned char sha1[20];
981 strbuf_reset(sb);
982 if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
983 return;
985 while (sb->len && sb->buf[sb->len - 1] == '\n')
986 strbuf_setlen(sb, sb->len - 1);
987 if (!sb->len)
988 return;
989 if (!prefixcmp(sb->buf, "refs/heads/"))
990 *branch = sb->buf + strlen("refs/heads/");
991 else if (!prefixcmp(sb->buf, "refs/"))
992 *branch = sb->buf;
993 else if (!get_sha1_hex(sb->buf, sha1)) {
994 const char *abbrev;
995 abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV);
996 strbuf_reset(sb);
997 strbuf_addstr(sb, abbrev);
998 *branch = sb->buf;
999 } else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */
1001 else /* bisect */
1002 *branch = sb->buf;
1005 static void wt_status_print_state(struct wt_status *s)
1007 const char *state_color = color(WT_STATUS_HEADER, s);
1008 struct strbuf branch = STRBUF_INIT;
1009 struct strbuf onto = STRBUF_INIT;
1010 struct wt_status_state state;
1011 struct stat st;
1013 memset(&state, 0, sizeof(state));
1015 if (!stat(git_path("MERGE_HEAD"), &st)) {
1016 state.merge_in_progress = 1;
1017 } else if (!stat(git_path("rebase-apply"), &st)) {
1018 if (!stat(git_path("rebase-apply/applying"), &st)) {
1019 state.am_in_progress = 1;
1020 if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
1021 state.am_empty_patch = 1;
1022 } else {
1023 state.rebase_in_progress = 1;
1024 read_and_strip_branch(&branch, &state.branch,
1025 "rebase-apply/head-name");
1026 read_and_strip_branch(&onto, &state.onto,
1027 "rebase-apply/onto");
1029 } else if (!stat(git_path("rebase-merge"), &st)) {
1030 if (!stat(git_path("rebase-merge/interactive"), &st))
1031 state.rebase_interactive_in_progress = 1;
1032 else
1033 state.rebase_in_progress = 1;
1034 read_and_strip_branch(&branch, &state.branch,
1035 "rebase-merge/head-name");
1036 read_and_strip_branch(&onto, &state.onto,
1037 "rebase-merge/onto");
1038 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
1039 state.cherry_pick_in_progress = 1;
1041 if (!stat(git_path("BISECT_LOG"), &st)) {
1042 state.bisect_in_progress = 1;
1043 read_and_strip_branch(&branch, &state.branch,
1044 "BISECT_START");
1047 if (state.merge_in_progress)
1048 show_merge_in_progress(s, &state, state_color);
1049 else if (state.am_in_progress)
1050 show_am_in_progress(s, &state, state_color);
1051 else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
1052 show_rebase_in_progress(s, &state, state_color);
1053 else if (state.cherry_pick_in_progress)
1054 show_cherry_pick_in_progress(s, &state, state_color);
1055 if (state.bisect_in_progress)
1056 show_bisect_in_progress(s, &state, state_color);
1057 strbuf_release(&branch);
1058 strbuf_release(&onto);
1061 void wt_status_print(struct wt_status *s)
1063 const char *branch_color = color(WT_STATUS_ONBRANCH, s);
1064 const char *branch_status_color = color(WT_STATUS_HEADER, s);
1066 if (s->branch) {
1067 const char *on_what = _("On branch ");
1068 const char *branch_name = s->branch;
1069 if (!prefixcmp(branch_name, "refs/heads/"))
1070 branch_name += 11;
1071 else if (!strcmp(branch_name, "HEAD")) {
1072 branch_name = "";
1073 branch_status_color = color(WT_STATUS_NOBRANCH, s);
1074 on_what = _("Not currently on any branch.");
1076 status_printf(s, color(WT_STATUS_HEADER, s), "");
1077 status_printf_more(s, branch_status_color, "%s", on_what);
1078 status_printf_more(s, branch_color, "%s\n", branch_name);
1079 if (!s->is_initial)
1080 wt_status_print_tracking(s);
1083 wt_status_print_state(s);
1084 if (s->is_initial) {
1085 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1086 status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit"));
1087 status_printf_ln(s, color(WT_STATUS_HEADER, s), "");
1090 wt_status_print_updated(s);
1091 wt_status_print_unmerged(s);
1092 wt_status_print_changed(s);
1093 if (s->submodule_summary &&
1094 (!s->ignore_submodule_arg ||
1095 strcmp(s->ignore_submodule_arg, "all"))) {
1096 wt_status_print_submodule_summary(s, 0); /* staged */
1097 wt_status_print_submodule_summary(s, 1); /* unstaged */
1099 if (s->show_untracked_files) {
1100 wt_status_print_other(s, &s->untracked, _("Untracked files"), "add");
1101 if (s->show_ignored_files)
1102 wt_status_print_other(s, &s->ignored, _("Ignored files"), "add -f");
1103 } else if (s->commitable)
1104 status_printf_ln(s, GIT_COLOR_NORMAL, _("Untracked files not listed%s"),
1105 advice_status_hints
1106 ? _(" (use -u option to show untracked files)") : "");
1108 if (s->verbose)
1109 wt_status_print_verbose(s);
1110 if (!s->commitable) {
1111 if (s->amend)
1112 status_printf_ln(s, GIT_COLOR_NORMAL, _("No changes"));
1113 else if (s->nowarn)
1114 ; /* nothing */
1115 else if (s->workdir_dirty) {
1116 if (advice_status_hints)
1117 printf(_("no changes added to commit "
1118 "(use \"git add\" and/or \"git commit -a\")\n"));
1119 else
1120 printf(_("no changes added to commit\n"));
1121 } else if (s->untracked.nr) {
1122 if (advice_status_hints)
1123 printf(_("nothing added to commit but untracked files "
1124 "present (use \"git add\" to track)\n"));
1125 else
1126 printf(_("nothing added to commit but untracked files present\n"));
1127 } else if (s->is_initial) {
1128 if (advice_status_hints)
1129 printf(_("nothing to commit (create/copy files "
1130 "and use \"git add\" to track)\n"));
1131 else
1132 printf(_("nothing to commit\n"));
1133 } else if (!s->show_untracked_files) {
1134 if (advice_status_hints)
1135 printf(_("nothing to commit (use -u to show untracked files)\n"));
1136 else
1137 printf(_("nothing to commit\n"));
1138 } else
1139 printf(_("nothing to commit, working directory clean\n"));
1143 static void wt_shortstatus_unmerged(struct string_list_item *it,
1144 struct wt_status *s)
1146 struct wt_status_change_data *d = it->util;
1147 const char *how = "??";
1149 switch (d->stagemask) {
1150 case 1: how = "DD"; break; /* both deleted */
1151 case 2: how = "AU"; break; /* added by us */
1152 case 3: how = "UD"; break; /* deleted by them */
1153 case 4: how = "UA"; break; /* added by them */
1154 case 5: how = "DU"; break; /* deleted by us */
1155 case 6: how = "AA"; break; /* both added */
1156 case 7: how = "UU"; break; /* both modified */
1158 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
1159 if (s->null_termination) {
1160 fprintf(stdout, " %s%c", it->string, 0);
1161 } else {
1162 struct strbuf onebuf = STRBUF_INIT;
1163 const char *one;
1164 one = quote_path(it->string, -1, &onebuf, s->prefix);
1165 printf(" %s\n", one);
1166 strbuf_release(&onebuf);
1170 static void wt_shortstatus_status(struct string_list_item *it,
1171 struct wt_status *s)
1173 struct wt_status_change_data *d = it->util;
1175 if (d->index_status)
1176 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
1177 else
1178 putchar(' ');
1179 if (d->worktree_status)
1180 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
1181 else
1182 putchar(' ');
1183 putchar(' ');
1184 if (s->null_termination) {
1185 fprintf(stdout, "%s%c", it->string, 0);
1186 if (d->head_path)
1187 fprintf(stdout, "%s%c", d->head_path, 0);
1188 } else {
1189 struct strbuf onebuf = STRBUF_INIT;
1190 const char *one;
1191 if (d->head_path) {
1192 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
1193 if (*one != '"' && strchr(one, ' ') != NULL) {
1194 putchar('"');
1195 strbuf_addch(&onebuf, '"');
1196 one = onebuf.buf;
1198 printf("%s -> ", one);
1199 strbuf_release(&onebuf);
1201 one = quote_path(it->string, -1, &onebuf, s->prefix);
1202 if (*one != '"' && strchr(one, ' ') != NULL) {
1203 putchar('"');
1204 strbuf_addch(&onebuf, '"');
1205 one = onebuf.buf;
1207 printf("%s\n", one);
1208 strbuf_release(&onebuf);
1212 static void wt_shortstatus_other(struct string_list_item *it,
1213 struct wt_status *s, const char *sign)
1215 if (s->null_termination) {
1216 fprintf(stdout, "%s %s%c", sign, it->string, 0);
1217 } else {
1218 struct strbuf onebuf = STRBUF_INIT;
1219 const char *one;
1220 one = quote_path(it->string, -1, &onebuf, s->prefix);
1221 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
1222 printf(" %s\n", one);
1223 strbuf_release(&onebuf);
1227 static void wt_shortstatus_print_tracking(struct wt_status *s)
1229 struct branch *branch;
1230 const char *header_color = color(WT_STATUS_HEADER, s);
1231 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
1232 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
1234 const char *base;
1235 const char *branch_name;
1236 int num_ours, num_theirs;
1238 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
1240 if (!s->branch)
1241 return;
1242 branch_name = s->branch;
1244 if (!prefixcmp(branch_name, "refs/heads/"))
1245 branch_name += 11;
1246 else if (!strcmp(branch_name, "HEAD")) {
1247 branch_name = _("HEAD (no branch)");
1248 branch_color_local = color(WT_STATUS_NOBRANCH, s);
1251 branch = branch_get(s->branch + 11);
1252 if (s->is_initial)
1253 color_fprintf(s->fp, header_color, _("Initial commit on "));
1254 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
1255 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1256 fputc(s->null_termination ? '\0' : '\n', s->fp);
1257 return;
1260 base = branch->merge[0]->dst;
1261 base = shorten_unambiguous_ref(base, 0);
1262 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
1263 color_fprintf(s->fp, header_color, "...");
1264 color_fprintf(s->fp, branch_color_remote, "%s", base);
1266 color_fprintf(s->fp, header_color, " [");
1267 if (!num_ours) {
1268 color_fprintf(s->fp, header_color, _("behind "));
1269 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1270 } else if (!num_theirs) {
1271 color_fprintf(s->fp, header_color, _("ahead "));
1272 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1273 } else {
1274 color_fprintf(s->fp, header_color, _("ahead "));
1275 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
1276 color_fprintf(s->fp, header_color, _(", behind "));
1277 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
1280 color_fprintf(s->fp, header_color, "]");
1281 fputc(s->null_termination ? '\0' : '\n', s->fp);
1284 void wt_shortstatus_print(struct wt_status *s)
1286 int i;
1288 if (s->show_branch)
1289 wt_shortstatus_print_tracking(s);
1291 for (i = 0; i < s->change.nr; i++) {
1292 struct wt_status_change_data *d;
1293 struct string_list_item *it;
1295 it = &(s->change.items[i]);
1296 d = it->util;
1297 if (d->stagemask)
1298 wt_shortstatus_unmerged(it, s);
1299 else
1300 wt_shortstatus_status(it, s);
1302 for (i = 0; i < s->untracked.nr; i++) {
1303 struct string_list_item *it;
1305 it = &(s->untracked.items[i]);
1306 wt_shortstatus_other(it, s, "??");
1308 for (i = 0; i < s->ignored.nr; i++) {
1309 struct string_list_item *it;
1311 it = &(s->ignored.items[i]);
1312 wt_shortstatus_other(it, s, "!!");
1316 void wt_porcelain_print(struct wt_status *s)
1318 s->use_color = 0;
1319 s->relative_paths = 0;
1320 s->prefix = NULL;
1321 wt_shortstatus_print(s);