Merge branch 'il/maint-xmallocz'
[git/dkf.git] / wt-status.c
blob5807fc3211a3aa8f886694776fe8c86b5bc5eb59
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;
294 init_revisions(&rev, NULL);
295 setup_revisions(0, NULL, &rev,
296 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
297 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
298 rev.diffopt.format_callback = wt_status_collect_updated_cb;
299 rev.diffopt.format_callback_data = s;
300 rev.diffopt.detect_rename = 1;
301 rev.diffopt.rename_limit = 200;
302 rev.diffopt.break_opt = 0;
303 rev.prune_data = s->pathspec;
304 run_diff_index(&rev, 1);
307 static void wt_status_collect_changes_initial(struct wt_status *s)
309 int i;
311 for (i = 0; i < active_nr; i++) {
312 struct string_list_item *it;
313 struct wt_status_change_data *d;
314 struct cache_entry *ce = active_cache[i];
316 if (!ce_path_match(ce, s->pathspec))
317 continue;
318 it = string_list_insert(ce->name, &s->change);
319 d = it->util;
320 if (!d) {
321 d = xcalloc(1, sizeof(*d));
322 it->util = d;
324 if (ce_stage(ce)) {
325 d->index_status = DIFF_STATUS_UNMERGED;
326 d->stagemask |= (1 << (ce_stage(ce) - 1));
328 else
329 d->index_status = DIFF_STATUS_ADDED;
333 static void wt_status_collect_untracked(struct wt_status *s)
335 int i;
336 struct dir_struct dir;
338 if (!s->show_untracked_files)
339 return;
340 memset(&dir, 0, sizeof(dir));
341 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
342 dir.flags |=
343 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
344 setup_standard_excludes(&dir);
346 fill_directory(&dir, s->pathspec);
347 for (i = 0; i < dir.nr; i++) {
348 struct dir_entry *ent = dir.entries[i];
349 if (!cache_name_is_other(ent->name, ent->len))
350 continue;
351 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
352 continue;
353 s->workdir_untracked = 1;
354 string_list_insert(ent->name, &s->untracked);
358 void wt_status_collect(struct wt_status *s)
360 wt_status_collect_changes_worktree(s);
362 if (s->is_initial)
363 wt_status_collect_changes_initial(s);
364 else
365 wt_status_collect_changes_index(s);
366 wt_status_collect_untracked(s);
369 static void wt_status_print_unmerged(struct wt_status *s)
371 int shown_header = 0;
372 int i;
374 for (i = 0; i < s->change.nr; i++) {
375 struct wt_status_change_data *d;
376 struct string_list_item *it;
377 it = &(s->change.items[i]);
378 d = it->util;
379 if (!d->stagemask)
380 continue;
381 if (!shown_header) {
382 wt_status_print_unmerged_header(s);
383 shown_header = 1;
385 wt_status_print_unmerged_data(s, it);
387 if (shown_header)
388 wt_status_print_trailer(s);
392 static void wt_status_print_updated(struct wt_status *s)
394 int shown_header = 0;
395 int i;
397 for (i = 0; i < s->change.nr; i++) {
398 struct wt_status_change_data *d;
399 struct string_list_item *it;
400 it = &(s->change.items[i]);
401 d = it->util;
402 if (!d->index_status ||
403 d->index_status == DIFF_STATUS_UNMERGED)
404 continue;
405 if (!shown_header) {
406 wt_status_print_cached_header(s);
407 s->commitable = 1;
408 shown_header = 1;
410 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
412 if (shown_header)
413 wt_status_print_trailer(s);
417 * -1 : has delete
418 * 0 : no change
419 * 1 : some change but no delete
421 static int wt_status_check_worktree_changes(struct wt_status *s)
423 int i;
424 int changes = 0;
426 for (i = 0; i < s->change.nr; i++) {
427 struct wt_status_change_data *d;
428 d = s->change.items[i].util;
429 if (!d->worktree_status ||
430 d->worktree_status == DIFF_STATUS_UNMERGED)
431 continue;
432 changes = 1;
433 if (d->worktree_status == DIFF_STATUS_DELETED)
434 return -1;
436 return changes;
439 static void wt_status_print_changed(struct wt_status *s)
441 int i;
442 int worktree_changes = wt_status_check_worktree_changes(s);
444 if (!worktree_changes)
445 return;
447 wt_status_print_dirty_header(s, worktree_changes < 0);
449 for (i = 0; i < s->change.nr; i++) {
450 struct wt_status_change_data *d;
451 struct string_list_item *it;
452 it = &(s->change.items[i]);
453 d = it->util;
454 if (!d->worktree_status ||
455 d->worktree_status == DIFF_STATUS_UNMERGED)
456 continue;
457 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
459 wt_status_print_trailer(s);
462 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
464 struct child_process sm_summary;
465 char summary_limit[64];
466 char index[PATH_MAX];
467 const char *env[] = { index, NULL };
468 const char *argv[] = {
469 "submodule",
470 "summary",
471 uncommitted ? "--files" : "--cached",
472 "--for-status",
473 "--summary-limit",
474 summary_limit,
475 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
476 NULL
479 sprintf(summary_limit, "%d", s->submodule_summary);
480 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
482 memset(&sm_summary, 0, sizeof(sm_summary));
483 sm_summary.argv = argv;
484 sm_summary.env = env;
485 sm_summary.git_cmd = 1;
486 sm_summary.no_stdin = 1;
487 fflush(s->fp);
488 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
489 run_command(&sm_summary);
492 static void wt_status_print_untracked(struct wt_status *s)
494 int i;
495 struct strbuf buf = STRBUF_INIT;
497 if (!s->untracked.nr)
498 return;
500 wt_status_print_untracked_header(s);
501 for (i = 0; i < s->untracked.nr; i++) {
502 struct string_list_item *it;
503 it = &(s->untracked.items[i]);
504 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
505 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
506 quote_path(it->string, strlen(it->string),
507 &buf, s->prefix));
509 strbuf_release(&buf);
512 static void wt_status_print_verbose(struct wt_status *s)
514 struct rev_info rev;
516 init_revisions(&rev, NULL);
517 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
518 setup_revisions(0, NULL, &rev,
519 s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference);
520 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
521 rev.diffopt.detect_rename = 1;
522 rev.diffopt.file = s->fp;
523 rev.diffopt.close_file = 0;
525 * If we're not going to stdout, then we definitely don't
526 * want color, since we are going to the commit message
527 * file (and even the "auto" setting won't work, since it
528 * will have checked isatty on stdout).
530 if (s->fp != stdout)
531 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
532 run_diff_index(&rev, 1);
535 static void wt_status_print_tracking(struct wt_status *s)
537 struct strbuf sb = STRBUF_INIT;
538 const char *cp, *ep;
539 struct branch *branch;
541 assert(s->branch && !s->is_initial);
542 if (prefixcmp(s->branch, "refs/heads/"))
543 return;
544 branch = branch_get(s->branch + 11);
545 if (!format_tracking_info(branch, &sb))
546 return;
548 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
549 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
550 "# %.*s", (int)(ep - cp), cp);
551 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
554 void wt_status_print(struct wt_status *s)
556 const char *branch_color = color(WT_STATUS_HEADER, s);
558 if (s->branch) {
559 const char *on_what = "On branch ";
560 const char *branch_name = s->branch;
561 if (!prefixcmp(branch_name, "refs/heads/"))
562 branch_name += 11;
563 else if (!strcmp(branch_name, "HEAD")) {
564 branch_name = "";
565 branch_color = color(WT_STATUS_NOBRANCH, s);
566 on_what = "Not currently on any branch.";
568 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
569 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
570 if (!s->is_initial)
571 wt_status_print_tracking(s);
574 if (s->is_initial) {
575 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
576 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
577 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
580 wt_status_print_updated(s);
581 wt_status_print_unmerged(s);
582 wt_status_print_changed(s);
583 if (s->submodule_summary) {
584 wt_status_print_submodule_summary(s, 0); /* staged */
585 wt_status_print_submodule_summary(s, 1); /* unstaged */
587 if (s->show_untracked_files)
588 wt_status_print_untracked(s);
589 else if (s->commitable)
590 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
592 if (s->verbose)
593 wt_status_print_verbose(s);
594 if (!s->commitable) {
595 if (s->amend)
596 fprintf(s->fp, "# No changes\n");
597 else if (s->nowarn)
598 ; /* nothing */
599 else if (s->workdir_dirty)
600 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
601 else if (s->untracked.nr)
602 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
603 else if (s->is_initial)
604 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
605 else if (!s->show_untracked_files)
606 printf("nothing to commit (use -u to show untracked files)\n");
607 else
608 printf("nothing to commit (working directory clean)\n");
612 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
613 struct wt_status *s)
615 struct wt_status_change_data *d = it->util;
616 const char *how = "??";
618 switch (d->stagemask) {
619 case 1: how = "DD"; break; /* both deleted */
620 case 2: how = "AU"; break; /* added by us */
621 case 3: how = "UD"; break; /* deleted by them */
622 case 4: how = "UA"; break; /* added by them */
623 case 5: how = "DU"; break; /* deleted by us */
624 case 6: how = "AA"; break; /* both added */
625 case 7: how = "UU"; break; /* both modified */
627 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
628 if (null_termination) {
629 fprintf(stdout, " %s%c", it->string, 0);
630 } else {
631 struct strbuf onebuf = STRBUF_INIT;
632 const char *one;
633 one = quote_path(it->string, -1, &onebuf, s->prefix);
634 printf(" %s\n", one);
635 strbuf_release(&onebuf);
639 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
640 struct wt_status *s)
642 struct wt_status_change_data *d = it->util;
644 if (d->index_status)
645 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
646 else
647 putchar(' ');
648 if (d->worktree_status)
649 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
650 else
651 putchar(' ');
652 putchar(' ');
653 if (null_termination) {
654 fprintf(stdout, "%s%c", it->string, 0);
655 if (d->head_path)
656 fprintf(stdout, "%s%c", d->head_path, 0);
657 } else {
658 struct strbuf onebuf = STRBUF_INIT;
659 const char *one;
660 if (d->head_path) {
661 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
662 printf("%s -> ", one);
663 strbuf_release(&onebuf);
665 one = quote_path(it->string, -1, &onebuf, s->prefix);
666 printf("%s\n", one);
667 strbuf_release(&onebuf);
671 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
672 struct wt_status *s)
674 if (null_termination) {
675 fprintf(stdout, "?? %s%c", it->string, 0);
676 } else {
677 struct strbuf onebuf = STRBUF_INIT;
678 const char *one;
679 one = quote_path(it->string, -1, &onebuf, s->prefix);
680 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
681 printf(" %s\n", one);
682 strbuf_release(&onebuf);
686 void wt_shortstatus_print(struct wt_status *s, int null_termination)
688 int i;
689 for (i = 0; i < s->change.nr; i++) {
690 struct wt_status_change_data *d;
691 struct string_list_item *it;
693 it = &(s->change.items[i]);
694 d = it->util;
695 if (d->stagemask)
696 wt_shortstatus_unmerged(null_termination, it, s);
697 else
698 wt_shortstatus_status(null_termination, it, s);
700 for (i = 0; i < s->untracked.nr; i++) {
701 struct string_list_item *it;
703 it = &(s->untracked.items[i]);
704 wt_shortstatus_untracked(null_termination, it, s);
708 void wt_porcelain_print(struct wt_status *s, int null_termination)
710 s->use_color = 0;
711 s->relative_paths = 0;
712 s->prefix = NULL;
713 wt_shortstatus_print(s, null_termination);