show --first-parent/-m: do not default to --cc
[git/jnareb-git.git] / wt-status.c
blobdcaec7f09e8801c0822a27ce2f9736eb05eaa105
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"
13 static char default_wt_status_colors[][COLOR_MAXLEN] = {
14 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
15 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
16 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
17 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
18 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
19 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
22 static const char *color(int slot, struct wt_status *s)
24 return s->use_color > 0 ? s->color_palette[slot] : "";
27 void wt_status_prepare(struct wt_status *s)
29 unsigned char sha1[20];
30 const char *head;
32 memset(s, 0, sizeof(*s));
33 memcpy(s->color_palette, default_wt_status_colors,
34 sizeof(default_wt_status_colors));
35 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
36 s->use_color = -1;
37 s->relative_paths = 1;
38 head = resolve_ref("HEAD", sha1, 0, NULL);
39 s->branch = head ? xstrdup(head) : NULL;
40 s->reference = "HEAD";
41 s->fp = stdout;
42 s->index_file = get_index_file();
43 s->change.strdup_strings = 1;
44 s->untracked.strdup_strings = 1;
47 static void wt_status_print_unmerged_header(struct wt_status *s)
49 const char *c = color(WT_STATUS_HEADER, s);
51 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
52 if (!advice_status_hints)
53 return;
54 if (s->in_merge)
56 else if (!s->is_initial)
57 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
58 else
59 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
60 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
61 color_fprintf_ln(s->fp, c, "#");
64 static void wt_status_print_cached_header(struct wt_status *s)
66 const char *c = color(WT_STATUS_HEADER, s);
68 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
69 if (!advice_status_hints)
70 return;
71 if (s->in_merge)
72 ; /* NEEDSWORK: use "git reset --unresolve"??? */
73 else if (!s->is_initial)
74 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
75 else
76 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
77 color_fprintf_ln(s->fp, c, "#");
80 static void wt_status_print_dirty_header(struct wt_status *s,
81 int has_deleted)
83 const char *c = color(WT_STATUS_HEADER, s);
85 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
86 if (!advice_status_hints)
87 return;
88 if (!has_deleted)
89 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
90 else
91 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
92 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
93 color_fprintf_ln(s->fp, c, "#");
96 static void wt_status_print_untracked_header(struct wt_status *s)
98 const char *c = color(WT_STATUS_HEADER, s);
99 color_fprintf_ln(s->fp, c, "# Untracked files:");
100 if (!advice_status_hints)
101 return;
102 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
103 color_fprintf_ln(s->fp, c, "#");
106 static void wt_status_print_trailer(struct wt_status *s)
108 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
111 #define quote_path quote_path_relative
113 static void wt_status_print_unmerged_data(struct wt_status *s,
114 struct string_list_item *it)
116 const char *c = color(WT_STATUS_UNMERGED, s);
117 struct wt_status_change_data *d = it->util;
118 struct strbuf onebuf = STRBUF_INIT;
119 const char *one, *how = "bug";
121 one = quote_path(it->string, -1, &onebuf, s->prefix);
122 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
123 switch (d->stagemask) {
124 case 1: how = "both deleted:"; break;
125 case 2: how = "added by us:"; break;
126 case 3: how = "deleted by them:"; break;
127 case 4: how = "added by them:"; break;
128 case 5: how = "deleted by us:"; break;
129 case 6: how = "both added:"; break;
130 case 7: how = "both modified:"; break;
132 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
133 strbuf_release(&onebuf);
136 static void wt_status_print_change_data(struct wt_status *s,
137 int change_type,
138 struct string_list_item *it)
140 struct wt_status_change_data *d = it->util;
141 const char *c = color(change_type, s);
142 int status = status;
143 char *one_name;
144 char *two_name;
145 const char *one, *two;
146 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
148 one_name = two_name = it->string;
149 switch (change_type) {
150 case WT_STATUS_UPDATED:
151 status = d->index_status;
152 if (d->head_path)
153 one_name = d->head_path;
154 break;
155 case WT_STATUS_CHANGED:
156 status = d->worktree_status;
157 break;
160 one = quote_path(one_name, -1, &onebuf, s->prefix);
161 two = quote_path(two_name, -1, &twobuf, s->prefix);
163 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
164 switch (status) {
165 case DIFF_STATUS_ADDED:
166 color_fprintf(s->fp, c, "new file: %s", one);
167 break;
168 case DIFF_STATUS_COPIED:
169 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
170 break;
171 case DIFF_STATUS_DELETED:
172 color_fprintf(s->fp, c, "deleted: %s", one);
173 break;
174 case DIFF_STATUS_MODIFIED:
175 color_fprintf(s->fp, c, "modified: %s", one);
176 break;
177 case DIFF_STATUS_RENAMED:
178 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
179 break;
180 case DIFF_STATUS_TYPE_CHANGED:
181 color_fprintf(s->fp, c, "typechange: %s", one);
182 break;
183 case DIFF_STATUS_UNKNOWN:
184 color_fprintf(s->fp, c, "unknown: %s", one);
185 break;
186 case DIFF_STATUS_UNMERGED:
187 color_fprintf(s->fp, c, "unmerged: %s", one);
188 break;
189 default:
190 die("bug: unhandled diff status %c", status);
192 fprintf(s->fp, "\n");
193 strbuf_release(&onebuf);
194 strbuf_release(&twobuf);
197 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
198 struct diff_options *options,
199 void *data)
201 struct wt_status *s = data;
202 int i;
204 if (!q->nr)
205 return;
206 s->workdir_dirty = 1;
207 for (i = 0; i < q->nr; i++) {
208 struct diff_filepair *p;
209 struct string_list_item *it;
210 struct wt_status_change_data *d;
212 p = q->queue[i];
213 it = string_list_insert(p->one->path, &s->change);
214 d = it->util;
215 if (!d) {
216 d = xcalloc(1, sizeof(*d));
217 it->util = d;
219 if (!d->worktree_status)
220 d->worktree_status = p->status;
224 static int unmerged_mask(const char *path)
226 int pos, mask;
227 struct cache_entry *ce;
229 pos = cache_name_pos(path, strlen(path));
230 if (0 <= pos)
231 return 0;
233 mask = 0;
234 pos = -pos-1;
235 while (pos < active_nr) {
236 ce = active_cache[pos++];
237 if (strcmp(ce->name, path) || !ce_stage(ce))
238 break;
239 mask |= (1 << (ce_stage(ce) - 1));
241 return mask;
244 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
245 struct diff_options *options,
246 void *data)
248 struct wt_status *s = data;
249 int i;
251 for (i = 0; i < q->nr; i++) {
252 struct diff_filepair *p;
253 struct string_list_item *it;
254 struct wt_status_change_data *d;
256 p = q->queue[i];
257 it = string_list_insert(p->two->path, &s->change);
258 d = it->util;
259 if (!d) {
260 d = xcalloc(1, sizeof(*d));
261 it->util = d;
263 if (!d->index_status)
264 d->index_status = p->status;
265 switch (p->status) {
266 case DIFF_STATUS_COPIED:
267 case DIFF_STATUS_RENAMED:
268 d->head_path = xstrdup(p->one->path);
269 break;
270 case DIFF_STATUS_UNMERGED:
271 d->stagemask = unmerged_mask(p->two->path);
272 break;
277 static void wt_status_collect_changes_worktree(struct wt_status *s)
279 struct rev_info rev;
281 init_revisions(&rev, NULL);
282 setup_revisions(0, NULL, &rev, NULL);
283 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
284 rev.diffopt.format_callback = wt_status_collect_changed_cb;
285 rev.diffopt.format_callback_data = s;
286 rev.prune_data = s->pathspec;
287 run_diff_files(&rev, 0);
290 static void wt_status_collect_changes_index(struct wt_status *s)
292 struct rev_info rev;
293 struct setup_revision_opt opt;
295 init_revisions(&rev, NULL);
296 memset(&opt, 0, sizeof(opt));
297 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
298 setup_revisions(0, NULL, &rev, &opt);
300 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
301 rev.diffopt.format_callback = wt_status_collect_updated_cb;
302 rev.diffopt.format_callback_data = s;
303 rev.diffopt.detect_rename = 1;
304 rev.diffopt.rename_limit = 200;
305 rev.diffopt.break_opt = 0;
306 rev.prune_data = s->pathspec;
307 run_diff_index(&rev, 1);
310 static void wt_status_collect_changes_initial(struct wt_status *s)
312 int i;
314 for (i = 0; i < active_nr; i++) {
315 struct string_list_item *it;
316 struct wt_status_change_data *d;
317 struct cache_entry *ce = active_cache[i];
319 if (!ce_path_match(ce, s->pathspec))
320 continue;
321 it = string_list_insert(ce->name, &s->change);
322 d = it->util;
323 if (!d) {
324 d = xcalloc(1, sizeof(*d));
325 it->util = d;
327 if (ce_stage(ce)) {
328 d->index_status = DIFF_STATUS_UNMERGED;
329 d->stagemask |= (1 << (ce_stage(ce) - 1));
331 else
332 d->index_status = DIFF_STATUS_ADDED;
336 static void wt_status_collect_untracked(struct wt_status *s)
338 int i;
339 struct dir_struct dir;
341 if (!s->show_untracked_files)
342 return;
343 memset(&dir, 0, sizeof(dir));
344 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
345 dir.flags |=
346 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
347 setup_standard_excludes(&dir);
349 fill_directory(&dir, s->pathspec);
350 for (i = 0; i < dir.nr; i++) {
351 struct dir_entry *ent = dir.entries[i];
352 if (!cache_name_is_other(ent->name, ent->len))
353 continue;
354 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
355 continue;
356 s->workdir_untracked = 1;
357 string_list_insert(ent->name, &s->untracked);
361 void wt_status_collect(struct wt_status *s)
363 wt_status_collect_changes_worktree(s);
365 if (s->is_initial)
366 wt_status_collect_changes_initial(s);
367 else
368 wt_status_collect_changes_index(s);
369 wt_status_collect_untracked(s);
372 static void wt_status_print_unmerged(struct wt_status *s)
374 int shown_header = 0;
375 int i;
377 for (i = 0; i < s->change.nr; i++) {
378 struct wt_status_change_data *d;
379 struct string_list_item *it;
380 it = &(s->change.items[i]);
381 d = it->util;
382 if (!d->stagemask)
383 continue;
384 if (!shown_header) {
385 wt_status_print_unmerged_header(s);
386 shown_header = 1;
388 wt_status_print_unmerged_data(s, it);
390 if (shown_header)
391 wt_status_print_trailer(s);
395 static void wt_status_print_updated(struct wt_status *s)
397 int shown_header = 0;
398 int i;
400 for (i = 0; i < s->change.nr; i++) {
401 struct wt_status_change_data *d;
402 struct string_list_item *it;
403 it = &(s->change.items[i]);
404 d = it->util;
405 if (!d->index_status ||
406 d->index_status == DIFF_STATUS_UNMERGED)
407 continue;
408 if (!shown_header) {
409 wt_status_print_cached_header(s);
410 s->commitable = 1;
411 shown_header = 1;
413 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
415 if (shown_header)
416 wt_status_print_trailer(s);
420 * -1 : has delete
421 * 0 : no change
422 * 1 : some change but no delete
424 static int wt_status_check_worktree_changes(struct wt_status *s)
426 int i;
427 int changes = 0;
429 for (i = 0; i < s->change.nr; i++) {
430 struct wt_status_change_data *d;
431 d = s->change.items[i].util;
432 if (!d->worktree_status ||
433 d->worktree_status == DIFF_STATUS_UNMERGED)
434 continue;
435 changes = 1;
436 if (d->worktree_status == DIFF_STATUS_DELETED)
437 return -1;
439 return changes;
442 static void wt_status_print_changed(struct wt_status *s)
444 int i;
445 int worktree_changes = wt_status_check_worktree_changes(s);
447 if (!worktree_changes)
448 return;
450 wt_status_print_dirty_header(s, worktree_changes < 0);
452 for (i = 0; i < s->change.nr; i++) {
453 struct wt_status_change_data *d;
454 struct string_list_item *it;
455 it = &(s->change.items[i]);
456 d = it->util;
457 if (!d->worktree_status ||
458 d->worktree_status == DIFF_STATUS_UNMERGED)
459 continue;
460 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
462 wt_status_print_trailer(s);
465 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
467 struct child_process sm_summary;
468 char summary_limit[64];
469 char index[PATH_MAX];
470 const char *env[] = { index, NULL };
471 const char *argv[] = {
472 "submodule",
473 "summary",
474 uncommitted ? "--files" : "--cached",
475 "--for-status",
476 "--summary-limit",
477 summary_limit,
478 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
479 NULL
482 sprintf(summary_limit, "%d", s->submodule_summary);
483 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
485 memset(&sm_summary, 0, sizeof(sm_summary));
486 sm_summary.argv = argv;
487 sm_summary.env = env;
488 sm_summary.git_cmd = 1;
489 sm_summary.no_stdin = 1;
490 fflush(s->fp);
491 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
492 run_command(&sm_summary);
495 static void wt_status_print_untracked(struct wt_status *s)
497 int i;
498 struct strbuf buf = STRBUF_INIT;
500 if (!s->untracked.nr)
501 return;
503 wt_status_print_untracked_header(s);
504 for (i = 0; i < s->untracked.nr; i++) {
505 struct string_list_item *it;
506 it = &(s->untracked.items[i]);
507 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
508 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
509 quote_path(it->string, strlen(it->string),
510 &buf, s->prefix));
512 strbuf_release(&buf);
515 static void wt_status_print_verbose(struct wt_status *s)
517 struct rev_info rev;
518 struct setup_revision_opt opt;
520 init_revisions(&rev, NULL);
521 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
523 memset(&opt, 0, sizeof(opt));
524 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
525 setup_revisions(0, NULL, &rev, &opt);
527 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
528 rev.diffopt.detect_rename = 1;
529 rev.diffopt.file = s->fp;
530 rev.diffopt.close_file = 0;
532 * If we're not going to stdout, then we definitely don't
533 * want color, since we are going to the commit message
534 * file (and even the "auto" setting won't work, since it
535 * will have checked isatty on stdout).
537 if (s->fp != stdout)
538 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
539 run_diff_index(&rev, 1);
542 static void wt_status_print_tracking(struct wt_status *s)
544 struct strbuf sb = STRBUF_INIT;
545 const char *cp, *ep;
546 struct branch *branch;
548 assert(s->branch && !s->is_initial);
549 if (prefixcmp(s->branch, "refs/heads/"))
550 return;
551 branch = branch_get(s->branch + 11);
552 if (!format_tracking_info(branch, &sb))
553 return;
555 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
556 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
557 "# %.*s", (int)(ep - cp), cp);
558 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
561 void wt_status_print(struct wt_status *s)
563 const char *branch_color = color(WT_STATUS_HEADER, s);
565 if (s->branch) {
566 const char *on_what = "On branch ";
567 const char *branch_name = s->branch;
568 if (!prefixcmp(branch_name, "refs/heads/"))
569 branch_name += 11;
570 else if (!strcmp(branch_name, "HEAD")) {
571 branch_name = "";
572 branch_color = color(WT_STATUS_NOBRANCH, s);
573 on_what = "Not currently on any branch.";
575 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
576 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
577 if (!s->is_initial)
578 wt_status_print_tracking(s);
581 if (s->is_initial) {
582 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
583 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
584 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
587 wt_status_print_updated(s);
588 wt_status_print_unmerged(s);
589 wt_status_print_changed(s);
590 if (s->submodule_summary) {
591 wt_status_print_submodule_summary(s, 0); /* staged */
592 wt_status_print_submodule_summary(s, 1); /* unstaged */
594 if (s->show_untracked_files)
595 wt_status_print_untracked(s);
596 else if (s->commitable)
597 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
599 if (s->verbose)
600 wt_status_print_verbose(s);
601 if (!s->commitable) {
602 if (s->amend)
603 fprintf(s->fp, "# No changes\n");
604 else if (s->nowarn)
605 ; /* nothing */
606 else if (s->workdir_dirty)
607 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
608 else if (s->untracked.nr)
609 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
610 else if (s->is_initial)
611 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
612 else if (!s->show_untracked_files)
613 printf("nothing to commit (use -u to show untracked files)\n");
614 else
615 printf("nothing to commit (working directory clean)\n");
619 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
620 struct wt_status *s)
622 struct wt_status_change_data *d = it->util;
623 const char *how = "??";
625 switch (d->stagemask) {
626 case 1: how = "DD"; break; /* both deleted */
627 case 2: how = "AU"; break; /* added by us */
628 case 3: how = "UD"; break; /* deleted by them */
629 case 4: how = "UA"; break; /* added by them */
630 case 5: how = "DU"; break; /* deleted by us */
631 case 6: how = "AA"; break; /* both added */
632 case 7: how = "UU"; break; /* both modified */
634 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
635 if (null_termination) {
636 fprintf(stdout, " %s%c", it->string, 0);
637 } else {
638 struct strbuf onebuf = STRBUF_INIT;
639 const char *one;
640 one = quote_path(it->string, -1, &onebuf, s->prefix);
641 printf(" %s\n", one);
642 strbuf_release(&onebuf);
646 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
647 struct wt_status *s)
649 struct wt_status_change_data *d = it->util;
651 if (d->index_status)
652 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
653 else
654 putchar(' ');
655 if (d->worktree_status)
656 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
657 else
658 putchar(' ');
659 putchar(' ');
660 if (null_termination) {
661 fprintf(stdout, "%s%c", it->string, 0);
662 if (d->head_path)
663 fprintf(stdout, "%s%c", d->head_path, 0);
664 } else {
665 struct strbuf onebuf = STRBUF_INIT;
666 const char *one;
667 if (d->head_path) {
668 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
669 printf("%s -> ", one);
670 strbuf_release(&onebuf);
672 one = quote_path(it->string, -1, &onebuf, s->prefix);
673 printf("%s\n", one);
674 strbuf_release(&onebuf);
678 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
679 struct wt_status *s)
681 if (null_termination) {
682 fprintf(stdout, "?? %s%c", it->string, 0);
683 } else {
684 struct strbuf onebuf = STRBUF_INIT;
685 const char *one;
686 one = quote_path(it->string, -1, &onebuf, s->prefix);
687 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
688 printf(" %s\n", one);
689 strbuf_release(&onebuf);
693 void wt_shortstatus_print(struct wt_status *s, int null_termination)
695 int i;
696 for (i = 0; i < s->change.nr; i++) {
697 struct wt_status_change_data *d;
698 struct string_list_item *it;
700 it = &(s->change.items[i]);
701 d = it->util;
702 if (d->stagemask)
703 wt_shortstatus_unmerged(null_termination, it, s);
704 else
705 wt_shortstatus_status(null_termination, it, s);
707 for (i = 0; i < s->untracked.nr; i++) {
708 struct string_list_item *it;
710 it = &(s->untracked.items[i]);
711 wt_shortstatus_untracked(null_termination, it, s);
715 void wt_porcelain_print(struct wt_status *s, int null_termination)
717 s->use_color = 0;
718 s->relative_paths = 0;
719 s->prefix = NULL;
720 wt_shortstatus_print(s, null_termination);