Merge branch 'em/checkout-orphan'
[git/mingw.git] / wt-status.c
blob636ecdd896c1ceecf79ede6caf4e61c519681053
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"
14 static char default_wt_status_colors[][COLOR_MAXLEN] = {
15 GIT_COLOR_NORMAL, /* WT_STATUS_HEADER */
16 GIT_COLOR_GREEN, /* WT_STATUS_UPDATED */
17 GIT_COLOR_RED, /* WT_STATUS_CHANGED */
18 GIT_COLOR_RED, /* WT_STATUS_UNTRACKED */
19 GIT_COLOR_RED, /* WT_STATUS_NOBRANCH */
20 GIT_COLOR_RED, /* WT_STATUS_UNMERGED */
21 GIT_COLOR_GREEN, /* WT_STATUS_LOCAL_BRANCH */
22 GIT_COLOR_RED, /* WT_STATUS_REMOTE_BRANCH */
25 static const char *color(int slot, struct wt_status *s)
27 return s->use_color > 0 ? s->color_palette[slot] : "";
30 void wt_status_prepare(struct wt_status *s)
32 unsigned char sha1[20];
33 const char *head;
35 memset(s, 0, sizeof(*s));
36 memcpy(s->color_palette, default_wt_status_colors,
37 sizeof(default_wt_status_colors));
38 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
39 s->use_color = -1;
40 s->relative_paths = 1;
41 head = resolve_ref("HEAD", sha1, 0, NULL);
42 s->branch = head ? xstrdup(head) : NULL;
43 s->reference = "HEAD";
44 s->fp = stdout;
45 s->index_file = get_index_file();
46 s->change.strdup_strings = 1;
47 s->untracked.strdup_strings = 1;
48 s->ignored.strdup_strings = 1;
51 static void wt_status_print_unmerged_header(struct wt_status *s)
53 const char *c = color(WT_STATUS_HEADER, s);
55 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
56 if (!advice_status_hints)
57 return;
58 if (s->in_merge)
60 else if (!s->is_initial)
61 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
62 else
63 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
64 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
65 color_fprintf_ln(s->fp, c, "#");
68 static void wt_status_print_cached_header(struct wt_status *s)
70 const char *c = color(WT_STATUS_HEADER, s);
72 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
73 if (!advice_status_hints)
74 return;
75 if (s->in_merge)
76 ; /* NEEDSWORK: use "git reset --unresolve"??? */
77 else if (!s->is_initial)
78 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
79 else
80 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
81 color_fprintf_ln(s->fp, c, "#");
84 static void wt_status_print_dirty_header(struct wt_status *s,
85 int has_deleted,
86 int has_dirty_submodules)
88 const char *c = color(WT_STATUS_HEADER, s);
90 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
91 if (!advice_status_hints)
92 return;
93 if (!has_deleted)
94 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
95 else
96 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
97 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
98 if (has_dirty_submodules)
99 color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)");
100 color_fprintf_ln(s->fp, c, "#");
103 static void wt_status_print_other_header(struct wt_status *s,
104 const char *what,
105 const char *how)
107 const char *c = color(WT_STATUS_HEADER, s);
108 color_fprintf_ln(s->fp, c, "# %s files:", what);
109 if (!advice_status_hints)
110 return;
111 color_fprintf_ln(s->fp, c, "# (use \"git %s <file>...\" to include in what will be committed)", how);
112 color_fprintf_ln(s->fp, c, "#");
115 static void wt_status_print_trailer(struct wt_status *s)
117 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
120 #define quote_path quote_path_relative
122 static void wt_status_print_unmerged_data(struct wt_status *s,
123 struct string_list_item *it)
125 const char *c = color(WT_STATUS_UNMERGED, s);
126 struct wt_status_change_data *d = it->util;
127 struct strbuf onebuf = STRBUF_INIT;
128 const char *one, *how = "bug";
130 one = quote_path(it->string, -1, &onebuf, s->prefix);
131 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
132 switch (d->stagemask) {
133 case 1: how = "both deleted:"; break;
134 case 2: how = "added by us:"; break;
135 case 3: how = "deleted by them:"; break;
136 case 4: how = "added by them:"; break;
137 case 5: how = "deleted by us:"; break;
138 case 6: how = "both added:"; break;
139 case 7: how = "both modified:"; break;
141 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
142 strbuf_release(&onebuf);
145 static void wt_status_print_change_data(struct wt_status *s,
146 int change_type,
147 struct string_list_item *it)
149 struct wt_status_change_data *d = it->util;
150 const char *c = color(change_type, s);
151 int status = status;
152 char *one_name;
153 char *two_name;
154 const char *one, *two;
155 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
156 struct strbuf extra = STRBUF_INIT;
158 one_name = two_name = it->string;
159 switch (change_type) {
160 case WT_STATUS_UPDATED:
161 status = d->index_status;
162 if (d->head_path)
163 one_name = d->head_path;
164 break;
165 case WT_STATUS_CHANGED:
166 if (d->new_submodule_commits || d->dirty_submodule) {
167 strbuf_addstr(&extra, " (");
168 if (d->new_submodule_commits)
169 strbuf_addf(&extra, "new commits, ");
170 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
171 strbuf_addf(&extra, "modified content, ");
172 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
173 strbuf_addf(&extra, "untracked content, ");
174 strbuf_setlen(&extra, extra.len - 2);
175 strbuf_addch(&extra, ')');
177 status = d->worktree_status;
178 break;
181 one = quote_path(one_name, -1, &onebuf, s->prefix);
182 two = quote_path(two_name, -1, &twobuf, s->prefix);
184 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
185 switch (status) {
186 case DIFF_STATUS_ADDED:
187 color_fprintf(s->fp, c, "new file: %s", one);
188 break;
189 case DIFF_STATUS_COPIED:
190 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
191 break;
192 case DIFF_STATUS_DELETED:
193 color_fprintf(s->fp, c, "deleted: %s", one);
194 break;
195 case DIFF_STATUS_MODIFIED:
196 color_fprintf(s->fp, c, "modified: %s", one);
197 break;
198 case DIFF_STATUS_RENAMED:
199 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
200 break;
201 case DIFF_STATUS_TYPE_CHANGED:
202 color_fprintf(s->fp, c, "typechange: %s", one);
203 break;
204 case DIFF_STATUS_UNKNOWN:
205 color_fprintf(s->fp, c, "unknown: %s", one);
206 break;
207 case DIFF_STATUS_UNMERGED:
208 color_fprintf(s->fp, c, "unmerged: %s", one);
209 break;
210 default:
211 die("bug: unhandled diff status %c", status);
213 if (extra.len) {
214 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
215 strbuf_release(&extra);
217 fprintf(s->fp, "\n");
218 strbuf_release(&onebuf);
219 strbuf_release(&twobuf);
222 static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
223 struct diff_options *options,
224 void *data)
226 struct wt_status *s = data;
227 int i;
229 if (!q->nr)
230 return;
231 s->workdir_dirty = 1;
232 for (i = 0; i < q->nr; i++) {
233 struct diff_filepair *p;
234 struct string_list_item *it;
235 struct wt_status_change_data *d;
237 p = q->queue[i];
238 it = string_list_insert(p->one->path, &s->change);
239 d = it->util;
240 if (!d) {
241 d = xcalloc(1, sizeof(*d));
242 it->util = d;
244 if (!d->worktree_status)
245 d->worktree_status = p->status;
246 d->dirty_submodule = p->two->dirty_submodule;
247 if (S_ISGITLINK(p->two->mode))
248 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
252 static int unmerged_mask(const char *path)
254 int pos, mask;
255 struct cache_entry *ce;
257 pos = cache_name_pos(path, strlen(path));
258 if (0 <= pos)
259 return 0;
261 mask = 0;
262 pos = -pos-1;
263 while (pos < active_nr) {
264 ce = active_cache[pos++];
265 if (strcmp(ce->name, path) || !ce_stage(ce))
266 break;
267 mask |= (1 << (ce_stage(ce) - 1));
269 return mask;
272 static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
273 struct diff_options *options,
274 void *data)
276 struct wt_status *s = data;
277 int i;
279 for (i = 0; i < q->nr; i++) {
280 struct diff_filepair *p;
281 struct string_list_item *it;
282 struct wt_status_change_data *d;
284 p = q->queue[i];
285 it = string_list_insert(p->two->path, &s->change);
286 d = it->util;
287 if (!d) {
288 d = xcalloc(1, sizeof(*d));
289 it->util = d;
291 if (!d->index_status)
292 d->index_status = p->status;
293 switch (p->status) {
294 case DIFF_STATUS_COPIED:
295 case DIFF_STATUS_RENAMED:
296 d->head_path = xstrdup(p->one->path);
297 break;
298 case DIFF_STATUS_UNMERGED:
299 d->stagemask = unmerged_mask(p->two->path);
300 break;
305 static void wt_status_collect_changes_worktree(struct wt_status *s)
307 struct rev_info rev;
309 init_revisions(&rev, NULL);
310 setup_revisions(0, NULL, &rev, NULL);
311 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
312 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
313 if (!s->show_untracked_files)
314 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
315 rev.diffopt.format_callback = wt_status_collect_changed_cb;
316 rev.diffopt.format_callback_data = s;
317 rev.prune_data = s->pathspec;
318 run_diff_files(&rev, 0);
321 static void wt_status_collect_changes_index(struct wt_status *s)
323 struct rev_info rev;
324 struct setup_revision_opt opt;
326 init_revisions(&rev, NULL);
327 memset(&opt, 0, sizeof(opt));
328 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
329 setup_revisions(0, NULL, &rev, &opt);
331 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
332 rev.diffopt.format_callback = wt_status_collect_updated_cb;
333 rev.diffopt.format_callback_data = s;
334 rev.diffopt.detect_rename = 1;
335 rev.diffopt.rename_limit = 200;
336 rev.diffopt.break_opt = 0;
337 rev.prune_data = s->pathspec;
338 run_diff_index(&rev, 1);
341 static void wt_status_collect_changes_initial(struct wt_status *s)
343 int i;
345 for (i = 0; i < active_nr; i++) {
346 struct string_list_item *it;
347 struct wt_status_change_data *d;
348 struct cache_entry *ce = active_cache[i];
350 if (!ce_path_match(ce, s->pathspec))
351 continue;
352 it = string_list_insert(ce->name, &s->change);
353 d = it->util;
354 if (!d) {
355 d = xcalloc(1, sizeof(*d));
356 it->util = d;
358 if (ce_stage(ce)) {
359 d->index_status = DIFF_STATUS_UNMERGED;
360 d->stagemask |= (1 << (ce_stage(ce) - 1));
362 else
363 d->index_status = DIFF_STATUS_ADDED;
367 static void wt_status_collect_untracked(struct wt_status *s)
369 int i;
370 struct dir_struct dir;
372 if (!s->show_untracked_files)
373 return;
374 memset(&dir, 0, sizeof(dir));
375 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
376 dir.flags |=
377 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
378 setup_standard_excludes(&dir);
380 fill_directory(&dir, s->pathspec);
381 for (i = 0; i < dir.nr; i++) {
382 struct dir_entry *ent = dir.entries[i];
383 if (!cache_name_is_other(ent->name, ent->len))
384 continue;
385 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
386 continue;
387 string_list_insert(ent->name, &s->untracked);
388 free(ent);
391 if (s->show_ignored_files) {
392 dir.nr = 0;
393 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
394 fill_directory(&dir, s->pathspec);
395 for (i = 0; i < dir.nr; i++) {
396 struct dir_entry *ent = dir.entries[i];
397 if (!cache_name_is_other(ent->name, ent->len))
398 continue;
399 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
400 continue;
401 string_list_insert(ent->name, &s->ignored);
402 free(ent);
406 free(dir.entries);
409 void wt_status_collect(struct wt_status *s)
411 wt_status_collect_changes_worktree(s);
413 if (s->is_initial)
414 wt_status_collect_changes_initial(s);
415 else
416 wt_status_collect_changes_index(s);
417 wt_status_collect_untracked(s);
420 static void wt_status_print_unmerged(struct wt_status *s)
422 int shown_header = 0;
423 int i;
425 for (i = 0; i < s->change.nr; i++) {
426 struct wt_status_change_data *d;
427 struct string_list_item *it;
428 it = &(s->change.items[i]);
429 d = it->util;
430 if (!d->stagemask)
431 continue;
432 if (!shown_header) {
433 wt_status_print_unmerged_header(s);
434 shown_header = 1;
436 wt_status_print_unmerged_data(s, it);
438 if (shown_header)
439 wt_status_print_trailer(s);
443 static void wt_status_print_updated(struct wt_status *s)
445 int shown_header = 0;
446 int i;
448 for (i = 0; i < s->change.nr; i++) {
449 struct wt_status_change_data *d;
450 struct string_list_item *it;
451 it = &(s->change.items[i]);
452 d = it->util;
453 if (!d->index_status ||
454 d->index_status == DIFF_STATUS_UNMERGED)
455 continue;
456 if (!shown_header) {
457 wt_status_print_cached_header(s);
458 s->commitable = 1;
459 shown_header = 1;
461 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
463 if (shown_header)
464 wt_status_print_trailer(s);
468 * -1 : has delete
469 * 0 : no change
470 * 1 : some change but no delete
472 static int wt_status_check_worktree_changes(struct wt_status *s,
473 int *dirty_submodules)
475 int i;
476 int changes = 0;
478 *dirty_submodules = 0;
480 for (i = 0; i < s->change.nr; i++) {
481 struct wt_status_change_data *d;
482 d = s->change.items[i].util;
483 if (!d->worktree_status ||
484 d->worktree_status == DIFF_STATUS_UNMERGED)
485 continue;
486 if (!changes)
487 changes = 1;
488 if (d->dirty_submodule)
489 *dirty_submodules = 1;
490 if (d->worktree_status == DIFF_STATUS_DELETED)
491 changes = -1;
493 return changes;
496 static void wt_status_print_changed(struct wt_status *s)
498 int i, dirty_submodules;
499 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
501 if (!worktree_changes)
502 return;
504 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
506 for (i = 0; i < s->change.nr; i++) {
507 struct wt_status_change_data *d;
508 struct string_list_item *it;
509 it = &(s->change.items[i]);
510 d = it->util;
511 if (!d->worktree_status ||
512 d->worktree_status == DIFF_STATUS_UNMERGED)
513 continue;
514 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
516 wt_status_print_trailer(s);
519 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
521 struct child_process sm_summary;
522 char summary_limit[64];
523 char index[PATH_MAX];
524 const char *env[] = { index, NULL };
525 const char *argv[] = {
526 "submodule",
527 "summary",
528 uncommitted ? "--files" : "--cached",
529 "--for-status",
530 "--summary-limit",
531 summary_limit,
532 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
533 NULL
536 sprintf(summary_limit, "%d", s->submodule_summary);
537 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
539 memset(&sm_summary, 0, sizeof(sm_summary));
540 sm_summary.argv = argv;
541 sm_summary.env = env;
542 sm_summary.git_cmd = 1;
543 sm_summary.no_stdin = 1;
544 fflush(s->fp);
545 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
546 run_command(&sm_summary);
549 static void wt_status_print_other(struct wt_status *s,
550 struct string_list *l,
551 const char *what,
552 const char *how)
554 int i;
555 struct strbuf buf = STRBUF_INIT;
557 if (!s->untracked.nr)
558 return;
560 wt_status_print_other_header(s, what, how);
562 for (i = 0; i < l->nr; i++) {
563 struct string_list_item *it;
564 it = &(l->items[i]);
565 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
566 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
567 quote_path(it->string, strlen(it->string),
568 &buf, s->prefix));
570 strbuf_release(&buf);
573 static void wt_status_print_verbose(struct wt_status *s)
575 struct rev_info rev;
576 struct setup_revision_opt opt;
578 init_revisions(&rev, NULL);
579 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
581 memset(&opt, 0, sizeof(opt));
582 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
583 setup_revisions(0, NULL, &rev, &opt);
585 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
586 rev.diffopt.detect_rename = 1;
587 rev.diffopt.file = s->fp;
588 rev.diffopt.close_file = 0;
590 * If we're not going to stdout, then we definitely don't
591 * want color, since we are going to the commit message
592 * file (and even the "auto" setting won't work, since it
593 * will have checked isatty on stdout).
595 if (s->fp != stdout)
596 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
597 run_diff_index(&rev, 1);
600 static void wt_status_print_tracking(struct wt_status *s)
602 struct strbuf sb = STRBUF_INIT;
603 const char *cp, *ep;
604 struct branch *branch;
606 assert(s->branch && !s->is_initial);
607 if (prefixcmp(s->branch, "refs/heads/"))
608 return;
609 branch = branch_get(s->branch + 11);
610 if (!format_tracking_info(branch, &sb))
611 return;
613 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
614 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
615 "# %.*s", (int)(ep - cp), cp);
616 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
619 void wt_status_print(struct wt_status *s)
621 const char *branch_color = color(WT_STATUS_HEADER, s);
623 if (s->branch) {
624 const char *on_what = "On branch ";
625 const char *branch_name = s->branch;
626 if (!prefixcmp(branch_name, "refs/heads/"))
627 branch_name += 11;
628 else if (!strcmp(branch_name, "HEAD")) {
629 branch_name = "";
630 branch_color = color(WT_STATUS_NOBRANCH, s);
631 on_what = "Not currently on any branch.";
633 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
634 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
635 if (!s->is_initial)
636 wt_status_print_tracking(s);
639 if (s->is_initial) {
640 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
641 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
642 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
645 wt_status_print_updated(s);
646 wt_status_print_unmerged(s);
647 wt_status_print_changed(s);
648 if (s->submodule_summary) {
649 wt_status_print_submodule_summary(s, 0); /* staged */
650 wt_status_print_submodule_summary(s, 1); /* unstaged */
652 if (s->show_untracked_files) {
653 wt_status_print_other(s, &s->untracked, "Untracked", "add");
654 if (s->show_ignored_files)
655 wt_status_print_other(s, &s->ignored, "Ignored", "add -f");
656 } else if (s->commitable)
657 fprintf(s->fp, "# Untracked files not listed%s\n",
658 advice_status_hints
659 ? " (use -u option to show untracked files)" : "");
661 if (s->verbose)
662 wt_status_print_verbose(s);
663 if (!s->commitable) {
664 if (s->amend)
665 fprintf(s->fp, "# No changes\n");
666 else if (s->nowarn)
667 ; /* nothing */
668 else if (s->workdir_dirty)
669 printf("no changes added to commit%s\n",
670 advice_status_hints
671 ? " (use \"git add\" and/or \"git commit -a\")" : "");
672 else if (s->untracked.nr)
673 printf("nothing added to commit but untracked files present%s\n",
674 advice_status_hints
675 ? " (use \"git add\" to track)" : "");
676 else if (s->is_initial)
677 printf("nothing to commit%s\n", advice_status_hints
678 ? " (create/copy files and use \"git add\" to track)" : "");
679 else if (!s->show_untracked_files)
680 printf("nothing to commit%s\n", advice_status_hints
681 ? " (use -u to show untracked files)" : "");
682 else
683 printf("nothing to commit%s\n", advice_status_hints
684 ? " (working directory clean)" : "");
688 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
689 struct wt_status *s)
691 struct wt_status_change_data *d = it->util;
692 const char *how = "??";
694 switch (d->stagemask) {
695 case 1: how = "DD"; break; /* both deleted */
696 case 2: how = "AU"; break; /* added by us */
697 case 3: how = "UD"; break; /* deleted by them */
698 case 4: how = "UA"; break; /* added by them */
699 case 5: how = "DU"; break; /* deleted by us */
700 case 6: how = "AA"; break; /* both added */
701 case 7: how = "UU"; break; /* both modified */
703 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
704 if (null_termination) {
705 fprintf(stdout, " %s%c", it->string, 0);
706 } else {
707 struct strbuf onebuf = STRBUF_INIT;
708 const char *one;
709 one = quote_path(it->string, -1, &onebuf, s->prefix);
710 printf(" %s\n", one);
711 strbuf_release(&onebuf);
715 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
716 struct wt_status *s)
718 struct wt_status_change_data *d = it->util;
720 if (d->index_status)
721 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
722 else
723 putchar(' ');
724 if (d->worktree_status)
725 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
726 else
727 putchar(' ');
728 putchar(' ');
729 if (null_termination) {
730 fprintf(stdout, "%s%c", it->string, 0);
731 if (d->head_path)
732 fprintf(stdout, "%s%c", d->head_path, 0);
733 } else {
734 struct strbuf onebuf = STRBUF_INIT;
735 const char *one;
736 if (d->head_path) {
737 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
738 printf("%s -> ", one);
739 strbuf_release(&onebuf);
741 one = quote_path(it->string, -1, &onebuf, s->prefix);
742 printf("%s\n", one);
743 strbuf_release(&onebuf);
747 static void wt_shortstatus_other(int null_termination, struct string_list_item *it,
748 struct wt_status *s, const char *sign)
750 if (null_termination) {
751 fprintf(stdout, "%s %s%c", sign, it->string, 0);
752 } else {
753 struct strbuf onebuf = STRBUF_INIT;
754 const char *one;
755 one = quote_path(it->string, -1, &onebuf, s->prefix);
756 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "%s", sign);
757 printf(" %s\n", one);
758 strbuf_release(&onebuf);
762 static void wt_shortstatus_print_tracking(struct wt_status *s)
764 struct branch *branch;
765 const char *header_color = color(WT_STATUS_HEADER, s);
766 const char *branch_color_local = color(WT_STATUS_LOCAL_BRANCH, s);
767 const char *branch_color_remote = color(WT_STATUS_REMOTE_BRANCH, s);
769 const char *base;
770 const char *branch_name;
771 int num_ours, num_theirs;
773 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "## ");
775 if (!s->branch)
776 return;
777 branch_name = s->branch;
779 if (!prefixcmp(branch_name, "refs/heads/"))
780 branch_name += 11;
781 else if (!strcmp(branch_name, "HEAD")) {
782 branch_name = "HEAD (no branch)";
783 branch_color_local = color(WT_STATUS_NOBRANCH, s);
786 branch = branch_get(s->branch + 11);
787 if (s->is_initial)
788 color_fprintf(s->fp, header_color, "Initial commit on ");
789 if (!stat_tracking_info(branch, &num_ours, &num_theirs)) {
790 color_fprintf_ln(s->fp, branch_color_local,
791 "%s", branch_name);
792 return;
795 base = branch->merge[0]->dst;
796 base = shorten_unambiguous_ref(base, 0);
797 color_fprintf(s->fp, branch_color_local, "%s", branch_name);
798 color_fprintf(s->fp, header_color, "...");
799 color_fprintf(s->fp, branch_color_remote, "%s", base);
801 color_fprintf(s->fp, header_color, " [");
802 if (!num_ours) {
803 color_fprintf(s->fp, header_color, "behind ");
804 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
805 } else if (!num_theirs) {
806 color_fprintf(s->fp, header_color, "ahead ");
807 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
808 } else {
809 color_fprintf(s->fp, header_color, "ahead ");
810 color_fprintf(s->fp, branch_color_local, "%d", num_ours);
811 color_fprintf(s->fp, header_color, ", behind ");
812 color_fprintf(s->fp, branch_color_remote, "%d", num_theirs);
815 color_fprintf_ln(s->fp, header_color, "]");
818 void wt_shortstatus_print(struct wt_status *s, int null_termination, int show_branch)
820 int i;
822 if (show_branch)
823 wt_shortstatus_print_tracking(s);
825 for (i = 0; i < s->change.nr; i++) {
826 struct wt_status_change_data *d;
827 struct string_list_item *it;
829 it = &(s->change.items[i]);
830 d = it->util;
831 if (d->stagemask)
832 wt_shortstatus_unmerged(null_termination, it, s);
833 else
834 wt_shortstatus_status(null_termination, it, s);
836 for (i = 0; i < s->untracked.nr; i++) {
837 struct string_list_item *it;
839 it = &(s->untracked.items[i]);
840 wt_shortstatus_other(null_termination, it, s, "??");
842 for (i = 0; i < s->ignored.nr; i++) {
843 struct string_list_item *it;
845 it = &(s->ignored.items[i]);
846 wt_shortstatus_other(null_termination, it, s, "!!");
850 void wt_porcelain_print(struct wt_status *s, int null_termination)
852 s->use_color = 0;
853 s->relative_paths = 0;
854 s->prefix = NULL;
855 wt_shortstatus_print(s, null_termination, 0);