status/commit: do not suggest "reset HEAD <path>" while merging
[git/jrn.git] / wt-status.c
blobc4589055bb7d6b8e945477d09e0015fe2da349af
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 (s->in_merge)
54 else if (!s->is_initial)
55 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
56 else
57 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
58 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
59 color_fprintf_ln(s->fp, c, "#");
62 static void wt_status_print_cached_header(struct wt_status *s)
64 const char *c = color(WT_STATUS_HEADER, s);
66 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
67 if (s->in_merge)
68 ; /* NEEDSWORK: use "git reset --unresolve"??? */
69 else if (!s->is_initial)
70 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
71 else
72 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
73 color_fprintf_ln(s->fp, c, "#");
76 static void wt_status_print_dirty_header(struct wt_status *s,
77 int has_deleted)
79 const char *c = color(WT_STATUS_HEADER, s);
81 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
82 if (!has_deleted)
83 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
84 else
85 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
86 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
87 color_fprintf_ln(s->fp, c, "#");
90 static void wt_status_print_untracked_header(struct wt_status *s)
92 const char *c = color(WT_STATUS_HEADER, s);
93 color_fprintf_ln(s->fp, c, "# Untracked files:");
94 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
95 color_fprintf_ln(s->fp, c, "#");
98 static void wt_status_print_trailer(struct wt_status *s)
100 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
103 #define quote_path quote_path_relative
105 static void wt_status_print_unmerged_data(struct wt_status *s,
106 struct string_list_item *it)
108 const char *c = color(WT_STATUS_UNMERGED, s);
109 struct wt_status_change_data *d = it->util;
110 struct strbuf onebuf = STRBUF_INIT;
111 const char *one, *how = "bug";
113 one = quote_path(it->string, -1, &onebuf, s->prefix);
114 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
115 switch (d->stagemask) {
116 case 1: how = "both deleted:"; break;
117 case 2: how = "added by us:"; break;
118 case 3: how = "deleted by them:"; break;
119 case 4: how = "added by them:"; break;
120 case 5: how = "deleted by us:"; break;
121 case 6: how = "both added:"; break;
122 case 7: how = "both modified:"; break;
124 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
125 strbuf_release(&onebuf);
128 static void wt_status_print_change_data(struct wt_status *s,
129 int change_type,
130 struct string_list_item *it)
132 struct wt_status_change_data *d = it->util;
133 const char *c = color(change_type, s);
134 int status = status;
135 char *one_name;
136 char *two_name;
137 const char *one, *two;
138 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
140 one_name = two_name = it->string;
141 switch (change_type) {
142 case WT_STATUS_UPDATED:
143 status = d->index_status;
144 if (d->head_path)
145 one_name = d->head_path;
146 break;
147 case WT_STATUS_CHANGED:
148 status = d->worktree_status;
149 break;
152 one = quote_path(one_name, -1, &onebuf, s->prefix);
153 two = quote_path(two_name, -1, &twobuf, s->prefix);
155 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
156 switch (status) {
157 case DIFF_STATUS_ADDED:
158 color_fprintf(s->fp, c, "new file: %s", one);
159 break;
160 case DIFF_STATUS_COPIED:
161 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
162 break;
163 case DIFF_STATUS_DELETED:
164 color_fprintf(s->fp, c, "deleted: %s", one);
165 break;
166 case DIFF_STATUS_MODIFIED:
167 color_fprintf(s->fp, c, "modified: %s", one);
168 break;
169 case DIFF_STATUS_RENAMED:
170 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
171 break;
172 case DIFF_STATUS_TYPE_CHANGED:
173 color_fprintf(s->fp, c, "typechange: %s", one);
174 break;
175 case DIFF_STATUS_UNKNOWN:
176 color_fprintf(s->fp, c, "unknown: %s", one);
177 break;
178 case DIFF_STATUS_UNMERGED:
179 color_fprintf(s->fp, c, "unmerged: %s", one);
180 break;
181 default:
182 die("bug: unhandled diff status %c", status);
184 fprintf(s->fp, "\n");
185 strbuf_release(&onebuf);
186 strbuf_release(&twobuf);
189 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
190 struct diff_options *options,
191 void *data)
193 struct wt_status *s = data;
194 int i;
196 if (!q->nr)
197 return;
198 s->workdir_dirty = 1;
199 for (i = 0; i < q->nr; i++) {
200 struct diff_filepair *p;
201 struct string_list_item *it;
202 struct wt_status_change_data *d;
204 p = q->queue[i];
205 it = string_list_insert(p->one->path, &s->change);
206 d = it->util;
207 if (!d) {
208 d = xcalloc(1, sizeof(*d));
209 it->util = d;
211 if (!d->worktree_status)
212 d->worktree_status = p->status;
216 static int unmerged_mask(const char *path)
218 int pos, mask;
219 struct cache_entry *ce;
221 pos = cache_name_pos(path, strlen(path));
222 if (0 <= pos)
223 return 0;
225 mask = 0;
226 pos = -pos-1;
227 while (pos < active_nr) {
228 ce = active_cache[pos++];
229 if (strcmp(ce->name, path) || !ce_stage(ce))
230 break;
231 mask |= (1 << (ce_stage(ce) - 1));
233 return mask;
236 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
237 struct diff_options *options,
238 void *data)
240 struct wt_status *s = data;
241 int i;
243 for (i = 0; i < q->nr; i++) {
244 struct diff_filepair *p;
245 struct string_list_item *it;
246 struct wt_status_change_data *d;
248 p = q->queue[i];
249 it = string_list_insert(p->two->path, &s->change);
250 d = it->util;
251 if (!d) {
252 d = xcalloc(1, sizeof(*d));
253 it->util = d;
255 if (!d->index_status)
256 d->index_status = p->status;
257 switch (p->status) {
258 case DIFF_STATUS_COPIED:
259 case DIFF_STATUS_RENAMED:
260 d->head_path = xstrdup(p->one->path);
261 break;
262 case DIFF_STATUS_UNMERGED:
263 d->stagemask = unmerged_mask(p->two->path);
264 break;
269 static void wt_status_collect_changes_worktree(struct wt_status *s)
271 struct rev_info rev;
273 init_revisions(&rev, NULL);
274 setup_revisions(0, NULL, &rev, NULL);
275 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
276 rev.diffopt.format_callback = wt_status_collect_changed_cb;
277 rev.diffopt.format_callback_data = s;
278 rev.prune_data = s->pathspec;
279 run_diff_files(&rev, 0);
282 static void wt_status_collect_changes_index(struct wt_status *s)
284 struct rev_info rev;
286 init_revisions(&rev, NULL);
287 setup_revisions(0, NULL, &rev,
288 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
289 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
290 rev.diffopt.format_callback = wt_status_collect_updated_cb;
291 rev.diffopt.format_callback_data = s;
292 rev.diffopt.detect_rename = 1;
293 rev.diffopt.rename_limit = 200;
294 rev.diffopt.break_opt = 0;
295 rev.prune_data = s->pathspec;
296 run_diff_index(&rev, 1);
299 static void wt_status_collect_changes_initial(struct wt_status *s)
301 int i;
303 for (i = 0; i < active_nr; i++) {
304 struct string_list_item *it;
305 struct wt_status_change_data *d;
306 struct cache_entry *ce = active_cache[i];
308 if (!ce_path_match(ce, s->pathspec))
309 continue;
310 it = string_list_insert(ce->name, &s->change);
311 d = it->util;
312 if (!d) {
313 d = xcalloc(1, sizeof(*d));
314 it->util = d;
316 if (ce_stage(ce)) {
317 d->index_status = DIFF_STATUS_UNMERGED;
318 d->stagemask |= (1 << (ce_stage(ce) - 1));
320 else
321 d->index_status = DIFF_STATUS_ADDED;
325 static void wt_status_collect_untracked(struct wt_status *s)
327 int i;
328 struct dir_struct dir;
330 if (!s->show_untracked_files)
331 return;
332 memset(&dir, 0, sizeof(dir));
333 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
334 dir.flags |=
335 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
336 setup_standard_excludes(&dir);
338 fill_directory(&dir, NULL);
339 for(i = 0; i < dir.nr; i++) {
340 struct dir_entry *ent = dir.entries[i];
341 if (!cache_name_is_other(ent->name, ent->len))
342 continue;
343 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
344 continue;
345 s->workdir_untracked = 1;
346 string_list_insert(ent->name, &s->untracked);
350 void wt_status_collect(struct wt_status *s)
352 wt_status_collect_changes_worktree(s);
354 if (s->is_initial)
355 wt_status_collect_changes_initial(s);
356 else
357 wt_status_collect_changes_index(s);
358 wt_status_collect_untracked(s);
361 static void wt_status_print_unmerged(struct wt_status *s)
363 int shown_header = 0;
364 int i;
366 for (i = 0; i < s->change.nr; i++) {
367 struct wt_status_change_data *d;
368 struct string_list_item *it;
369 it = &(s->change.items[i]);
370 d = it->util;
371 if (!d->stagemask)
372 continue;
373 if (!shown_header) {
374 wt_status_print_unmerged_header(s);
375 shown_header = 1;
377 wt_status_print_unmerged_data(s, it);
379 if (shown_header)
380 wt_status_print_trailer(s);
384 static void wt_status_print_updated(struct wt_status *s)
386 int shown_header = 0;
387 int i;
389 for (i = 0; i < s->change.nr; i++) {
390 struct wt_status_change_data *d;
391 struct string_list_item *it;
392 it = &(s->change.items[i]);
393 d = it->util;
394 if (!d->index_status ||
395 d->index_status == DIFF_STATUS_UNMERGED)
396 continue;
397 if (!shown_header) {
398 wt_status_print_cached_header(s);
399 s->commitable = 1;
400 shown_header = 1;
402 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
404 if (shown_header)
405 wt_status_print_trailer(s);
409 * -1 : has delete
410 * 0 : no change
411 * 1 : some change but no delete
413 static int wt_status_check_worktree_changes(struct wt_status *s)
415 int i;
416 int changes = 0;
418 for (i = 0; i < s->change.nr; i++) {
419 struct wt_status_change_data *d;
420 d = s->change.items[i].util;
421 if (!d->worktree_status ||
422 d->worktree_status == DIFF_STATUS_UNMERGED)
423 continue;
424 changes = 1;
425 if (d->worktree_status == DIFF_STATUS_DELETED)
426 return -1;
428 return changes;
431 static void wt_status_print_changed(struct wt_status *s)
433 int i;
434 int worktree_changes = wt_status_check_worktree_changes(s);
436 if (!worktree_changes)
437 return;
439 wt_status_print_dirty_header(s, worktree_changes < 0);
441 for (i = 0; i < s->change.nr; i++) {
442 struct wt_status_change_data *d;
443 struct string_list_item *it;
444 it = &(s->change.items[i]);
445 d = it->util;
446 if (!d->worktree_status ||
447 d->worktree_status == DIFF_STATUS_UNMERGED)
448 continue;
449 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
451 wt_status_print_trailer(s);
454 static void wt_status_print_submodule_summary(struct wt_status *s)
456 struct child_process sm_summary;
457 char summary_limit[64];
458 char index[PATH_MAX];
459 const char *env[] = { index, NULL };
460 const char *argv[] = {
461 "submodule",
462 "summary",
463 "--cached",
464 "--for-status",
465 "--summary-limit",
466 summary_limit,
467 s->amend ? "HEAD^" : "HEAD",
468 NULL
471 sprintf(summary_limit, "%d", s->submodule_summary);
472 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
474 memset(&sm_summary, 0, sizeof(sm_summary));
475 sm_summary.argv = argv;
476 sm_summary.env = env;
477 sm_summary.git_cmd = 1;
478 sm_summary.no_stdin = 1;
479 fflush(s->fp);
480 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
481 run_command(&sm_summary);
484 static void wt_status_print_untracked(struct wt_status *s)
486 int i;
487 struct strbuf buf = STRBUF_INIT;
489 if (!s->untracked.nr)
490 return;
492 wt_status_print_untracked_header(s);
493 for (i = 0; i < s->untracked.nr; i++) {
494 struct string_list_item *it;
495 it = &(s->untracked.items[i]);
496 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
497 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
498 quote_path(it->string, strlen(it->string),
499 &buf, s->prefix));
501 strbuf_release(&buf);
504 static void wt_status_print_verbose(struct wt_status *s)
506 struct rev_info rev;
508 init_revisions(&rev, NULL);
509 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
510 setup_revisions(0, NULL, &rev,
511 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
512 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
513 rev.diffopt.detect_rename = 1;
514 rev.diffopt.file = s->fp;
515 rev.diffopt.close_file = 0;
517 * If we're not going to stdout, then we definitely don't
518 * want color, since we are going to the commit message
519 * file (and even the "auto" setting won't work, since it
520 * will have checked isatty on stdout).
522 if (s->fp != stdout)
523 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
524 run_diff_index(&rev, 1);
527 static void wt_status_print_tracking(struct wt_status *s)
529 struct strbuf sb = STRBUF_INIT;
530 const char *cp, *ep;
531 struct branch *branch;
533 assert(s->branch && !s->is_initial);
534 if (prefixcmp(s->branch, "refs/heads/"))
535 return;
536 branch = branch_get(s->branch + 11);
537 if (!format_tracking_info(branch, &sb))
538 return;
540 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
541 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
542 "# %.*s", (int)(ep - cp), cp);
543 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
546 void wt_status_print(struct wt_status *s)
548 const char *branch_color = color(WT_STATUS_HEADER, s);
550 if (s->branch) {
551 const char *on_what = "On branch ";
552 const char *branch_name = s->branch;
553 if (!prefixcmp(branch_name, "refs/heads/"))
554 branch_name += 11;
555 else if (!strcmp(branch_name, "HEAD")) {
556 branch_name = "";
557 branch_color = color(WT_STATUS_NOBRANCH, s);
558 on_what = "Not currently on any branch.";
560 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
561 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
562 if (!s->is_initial)
563 wt_status_print_tracking(s);
566 if (s->is_initial) {
567 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
568 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
569 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
572 wt_status_print_unmerged(s);
573 wt_status_print_updated(s);
574 wt_status_print_changed(s);
575 if (s->submodule_summary)
576 wt_status_print_submodule_summary(s);
577 if (s->show_untracked_files)
578 wt_status_print_untracked(s);
579 else if (s->commitable)
580 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
582 if (s->verbose)
583 wt_status_print_verbose(s);
584 if (!s->commitable) {
585 if (s->amend)
586 fprintf(s->fp, "# No changes\n");
587 else if (s->nowarn)
588 ; /* nothing */
589 else if (s->workdir_dirty)
590 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
591 else if (s->untracked.nr)
592 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
593 else if (s->is_initial)
594 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
595 else if (!s->show_untracked_files)
596 printf("nothing to commit (use -u to show untracked files)\n");
597 else
598 printf("nothing to commit (working directory clean)\n");
602 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
603 struct wt_status *s)
605 struct wt_status_change_data *d = it->util;
606 const char *how = "??";
608 switch (d->stagemask) {
609 case 1: how = "DD"; break; /* both deleted */
610 case 2: how = "AU"; break; /* added by us */
611 case 3: how = "UD"; break; /* deleted by them */
612 case 4: how = "UA"; break; /* added by them */
613 case 5: how = "DU"; break; /* deleted by us */
614 case 6: how = "AA"; break; /* both added */
615 case 7: how = "UU"; break; /* both modified */
617 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
618 if (null_termination) {
619 fprintf(stdout, " %s%c", it->string, 0);
620 } else {
621 struct strbuf onebuf = STRBUF_INIT;
622 const char *one;
623 one = quote_path(it->string, -1, &onebuf, s->prefix);
624 printf(" %s\n", one);
625 strbuf_release(&onebuf);
629 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
630 struct wt_status *s)
632 struct wt_status_change_data *d = it->util;
634 if (d->index_status)
635 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
636 else
637 putchar(' ');
638 if (d->worktree_status)
639 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
640 else
641 putchar(' ');
642 putchar(' ');
643 if (null_termination) {
644 fprintf(stdout, "%s%c", it->string, 0);
645 if (d->head_path)
646 fprintf(stdout, "%s%c", d->head_path, 0);
647 } else {
648 struct strbuf onebuf = STRBUF_INIT;
649 const char *one;
650 if (d->head_path) {
651 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
652 printf("%s -> ", one);
653 strbuf_release(&onebuf);
655 one = quote_path(it->string, -1, &onebuf, s->prefix);
656 printf("%s\n", one);
657 strbuf_release(&onebuf);
661 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
662 struct wt_status *s)
664 if (null_termination) {
665 fprintf(stdout, "?? %s%c", it->string, 0);
666 } else {
667 struct strbuf onebuf = STRBUF_INIT;
668 const char *one;
669 one = quote_path(it->string, -1, &onebuf, s->prefix);
670 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
671 printf(" %s\n", one);
672 strbuf_release(&onebuf);
676 void wt_shortstatus_print(struct wt_status *s, int null_termination)
678 int i;
679 for (i = 0; i < s->change.nr; i++) {
680 struct wt_status_change_data *d;
681 struct string_list_item *it;
683 it = &(s->change.items[i]);
684 d = it->util;
685 if (d->stagemask)
686 wt_shortstatus_unmerged(null_termination, it, s);
687 else
688 wt_shortstatus_status(null_termination, it, s);
690 for (i = 0; i < s->untracked.nr; i++) {
691 struct string_list_item *it;
693 it = &(s->untracked.items[i]);
694 wt_shortstatus_untracked(null_termination, it, s);
698 void wt_porcelain_print(struct wt_status *s, int null_termination)
700 s->use_color = 0;
701 s->relative_paths = 0;
702 s->prefix = NULL;
703 wt_shortstatus_print(s, null_termination);