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 "submodule.h"
13
14static 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};
22
23static const char *color(int slot, struct wt_status *s)
24{
25 return s->use_color > 0 ? s->color_palette[slot] : "";
26}
27
28void wt_status_prepare(struct wt_status *s)
29{
30 unsigned char sha1[20];
31 const char *head;
32
33 memset(s, 0, sizeof(*s));
34 memcpy(s->color_palette, default_wt_status_colors,
35 sizeof(default_wt_status_colors));
36 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
37 s->use_color = -1;
38 s->relative_paths = 1;
39 head = resolve_ref("HEAD", sha1, 0, NULL);
40 s->branch = head ? xstrdup(head) : NULL;
41 s->reference = "HEAD";
42 s->fp = stdout;
43 s->index_file = get_index_file();
44 s->change.strdup_strings = 1;
45 s->untracked.strdup_strings = 1;
46}
47
48static void wt_status_print_unmerged_header(struct wt_status *s)
49{
50 const char *c = color(WT_STATUS_HEADER, s);
51
52 color_fprintf_ln(s->fp, c, "# Unmerged paths:");
53 if (!advice_status_hints)
54 return;
55 if (s->in_merge)
56 ;
57 else if (!s->is_initial)
58 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
59 else
60 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
61 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" as appropriate to mark resolution)");
62 color_fprintf_ln(s->fp, c, "#");
63}
64
65static void wt_status_print_cached_header(struct wt_status *s)
66{
67 const char *c = color(WT_STATUS_HEADER, s);
68
69 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
70 if (!advice_status_hints)
71 return;
72 if (s->in_merge)
73 ; /* NEEDSWORK: use "git reset --unresolve"??? */
74 else if (!s->is_initial)
75 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
76 else
77 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
78 color_fprintf_ln(s->fp, c, "#");
79}
80
81static void wt_status_print_dirty_header(struct wt_status *s,
82 int has_deleted,
83 int has_dirty_submodules)
84{
85 const char *c = color(WT_STATUS_HEADER, s);
86
87 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
88 if (!advice_status_hints)
89 return;
90 if (!has_deleted)
91 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
92 else
93 color_fprintf_ln(s->fp, c, "# (use \"git add/rm <file>...\" to update what will be committed)");
94 color_fprintf_ln(s->fp, c, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
95 if (has_dirty_submodules)
96 color_fprintf_ln(s->fp, c, "# (commit or discard the untracked or modified content in submodules)");
97 color_fprintf_ln(s->fp, c, "#");
98}
99
100static void wt_status_print_untracked_header(struct wt_status *s)
101{
102 const char *c = color(WT_STATUS_HEADER, s);
103 color_fprintf_ln(s->fp, c, "# Untracked files:");
104 if (!advice_status_hints)
105 return;
106 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
107 color_fprintf_ln(s->fp, c, "#");
108}
109
110static void wt_status_print_trailer(struct wt_status *s)
111{
112 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
113}
114
115#define quote_path quote_path_relative
116
117static void wt_status_print_unmerged_data(struct wt_status *s,
118 struct string_list_item *it)
119{
120 const char *c = color(WT_STATUS_UNMERGED, s);
121 struct wt_status_change_data *d = it->util;
122 struct strbuf onebuf = STRBUF_INIT;
123 const char *one, *how = "bug";
124
125 one = quote_path(it->string, -1, &onebuf, s->prefix);
126 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
127 switch (d->stagemask) {
128 case 1: how = "both deleted:"; break;
129 case 2: how = "added by us:"; break;
130 case 3: how = "deleted by them:"; break;
131 case 4: how = "added by them:"; break;
132 case 5: how = "deleted by us:"; break;
133 case 6: how = "both added:"; break;
134 case 7: how = "both modified:"; break;
135 }
136 color_fprintf(s->fp, c, "%-20s%s\n", how, one);
137 strbuf_release(&onebuf);
138}
139
140static void wt_status_print_change_data(struct wt_status *s,
141 int change_type,
142 struct string_list_item *it)
143{
144 struct wt_status_change_data *d = it->util;
145 const char *c = color(change_type, s);
146 int status = status;
147 char *one_name;
148 char *two_name;
149 const char *one, *two;
150 struct strbuf onebuf = STRBUF_INIT, twobuf = STRBUF_INIT;
151 struct strbuf extra = STRBUF_INIT;
152
153 one_name = two_name = it->string;
154 switch (change_type) {
155 case WT_STATUS_UPDATED:
156 status = d->index_status;
157 if (d->head_path)
158 one_name = d->head_path;
159 break;
160 case WT_STATUS_CHANGED:
161 if (d->new_submodule_commits || d->dirty_submodule) {
162 strbuf_addstr(&extra, " (");
163 if (d->new_submodule_commits)
164 strbuf_addf(&extra, "new commits, ");
165 if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
166 strbuf_addf(&extra, "modified content, ");
167 if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
168 strbuf_addf(&extra, "untracked content, ");
169 strbuf_setlen(&extra, extra.len - 2);
170 strbuf_addch(&extra, ')');
171 }
172 status = d->worktree_status;
173 break;
174 }
175
176 one = quote_path(one_name, -1, &onebuf, s->prefix);
177 two = quote_path(two_name, -1, &twobuf, s->prefix);
178
179 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
180 switch (status) {
181 case DIFF_STATUS_ADDED:
182 color_fprintf(s->fp, c, "new file: %s", one);
183 break;
184 case DIFF_STATUS_COPIED:
185 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
186 break;
187 case DIFF_STATUS_DELETED:
188 color_fprintf(s->fp, c, "deleted: %s", one);
189 break;
190 case DIFF_STATUS_MODIFIED:
191 color_fprintf(s->fp, c, "modified: %s", one);
192 break;
193 case DIFF_STATUS_RENAMED:
194 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
195 break;
196 case DIFF_STATUS_TYPE_CHANGED:
197 color_fprintf(s->fp, c, "typechange: %s", one);
198 break;
199 case DIFF_STATUS_UNKNOWN:
200 color_fprintf(s->fp, c, "unknown: %s", one);
201 break;
202 case DIFF_STATUS_UNMERGED:
203 color_fprintf(s->fp, c, "unmerged: %s", one);
204 break;
205 default:
206 die("bug: unhandled diff status %c", status);
207 }
208 if (extra.len) {
209 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "%s", extra.buf);
210 strbuf_release(&extra);
211 }
212 fprintf(s->fp, "\n");
213 strbuf_release(&onebuf);
214 strbuf_release(&twobuf);
215}
216
217static void wt_status_collect_changed_cb(struct diff_queue_struct *q,
218 struct diff_options *options,
219 void *data)
220{
221 struct wt_status *s = data;
222 int i;
223
224 if (!q->nr)
225 return;
226 s->workdir_dirty = 1;
227 for (i = 0; i < q->nr; i++) {
228 struct diff_filepair *p;
229 struct string_list_item *it;
230 struct wt_status_change_data *d;
231
232 p = q->queue[i];
233 it = string_list_insert(p->one->path, &s->change);
234 d = it->util;
235 if (!d) {
236 d = xcalloc(1, sizeof(*d));
237 it->util = d;
238 }
239 if (!d->worktree_status)
240 d->worktree_status = p->status;
241 d->dirty_submodule = p->two->dirty_submodule;
242 if (S_ISGITLINK(p->two->mode))
243 d->new_submodule_commits = !!hashcmp(p->one->sha1, p->two->sha1);
244 }
245}
246
247static int unmerged_mask(const char *path)
248{
249 int pos, mask;
250 struct cache_entry *ce;
251
252 pos = cache_name_pos(path, strlen(path));
253 if (0 <= pos)
254 return 0;
255
256 mask = 0;
257 pos = -pos-1;
258 while (pos < active_nr) {
259 ce = active_cache[pos++];
260 if (strcmp(ce->name, path) || !ce_stage(ce))
261 break;
262 mask |= (1 << (ce_stage(ce) - 1));
263 }
264 return mask;
265}
266
267static void wt_status_collect_updated_cb(struct diff_queue_struct *q,
268 struct diff_options *options,
269 void *data)
270{
271 struct wt_status *s = data;
272 int i;
273
274 for (i = 0; i < q->nr; i++) {
275 struct diff_filepair *p;
276 struct string_list_item *it;
277 struct wt_status_change_data *d;
278
279 p = q->queue[i];
280 it = string_list_insert(p->two->path, &s->change);
281 d = it->util;
282 if (!d) {
283 d = xcalloc(1, sizeof(*d));
284 it->util = d;
285 }
286 if (!d->index_status)
287 d->index_status = p->status;
288 switch (p->status) {
289 case DIFF_STATUS_COPIED:
290 case DIFF_STATUS_RENAMED:
291 d->head_path = xstrdup(p->one->path);
292 break;
293 case DIFF_STATUS_UNMERGED:
294 d->stagemask = unmerged_mask(p->two->path);
295 break;
296 }
297 }
298}
299
300static void wt_status_collect_changes_worktree(struct wt_status *s)
301{
302 struct rev_info rev;
303
304 init_revisions(&rev, NULL);
305 setup_revisions(0, NULL, &rev, NULL);
306 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
307 DIFF_OPT_SET(&rev.diffopt, DIRTY_SUBMODULES);
308 if (!s->show_untracked_files)
309 DIFF_OPT_SET(&rev.diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
310 if (s->ignore_submodule_arg)
311 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
312 rev.diffopt.format_callback = wt_status_collect_changed_cb;
313 rev.diffopt.format_callback_data = s;
314 rev.prune_data = s->pathspec;
315 run_diff_files(&rev, 0);
316}
317
318static void wt_status_collect_changes_index(struct wt_status *s)
319{
320 struct rev_info rev;
321 struct setup_revision_opt opt;
322
323 init_revisions(&rev, NULL);
324 memset(&opt, 0, sizeof(opt));
325 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
326 setup_revisions(0, NULL, &rev, &opt);
327
328 if (s->ignore_submodule_arg)
329 handle_ignore_submodules_arg(&rev.diffopt, s->ignore_submodule_arg);
330
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);
339}
340
341static void wt_status_collect_changes_initial(struct wt_status *s)
342{
343 int i;
344
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];
349
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;
357 }
358 if (ce_stage(ce)) {
359 d->index_status = DIFF_STATUS_UNMERGED;
360 d->stagemask |= (1 << (ce_stage(ce) - 1));
361 }
362 else
363 d->index_status = DIFF_STATUS_ADDED;
364 }
365}
366
367static void wt_status_collect_untracked(struct wt_status *s)
368{
369 int i;
370 struct dir_struct dir;
371
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);
379
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 s->workdir_untracked = 1;
388 string_list_insert(ent->name, &s->untracked);
389 }
390}
391
392void wt_status_collect(struct wt_status *s)
393{
394 wt_status_collect_changes_worktree(s);
395
396 if (s->is_initial)
397 wt_status_collect_changes_initial(s);
398 else
399 wt_status_collect_changes_index(s);
400 wt_status_collect_untracked(s);
401}
402
403static void wt_status_print_unmerged(struct wt_status *s)
404{
405 int shown_header = 0;
406 int i;
407
408 for (i = 0; i < s->change.nr; i++) {
409 struct wt_status_change_data *d;
410 struct string_list_item *it;
411 it = &(s->change.items[i]);
412 d = it->util;
413 if (!d->stagemask)
414 continue;
415 if (!shown_header) {
416 wt_status_print_unmerged_header(s);
417 shown_header = 1;
418 }
419 wt_status_print_unmerged_data(s, it);
420 }
421 if (shown_header)
422 wt_status_print_trailer(s);
423
424}
425
426static void wt_status_print_updated(struct wt_status *s)
427{
428 int shown_header = 0;
429 int i;
430
431 for (i = 0; i < s->change.nr; i++) {
432 struct wt_status_change_data *d;
433 struct string_list_item *it;
434 it = &(s->change.items[i]);
435 d = it->util;
436 if (!d->index_status ||
437 d->index_status == DIFF_STATUS_UNMERGED)
438 continue;
439 if (!shown_header) {
440 wt_status_print_cached_header(s);
441 s->commitable = 1;
442 shown_header = 1;
443 }
444 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
445 }
446 if (shown_header)
447 wt_status_print_trailer(s);
448}
449
450/*
451 * -1 : has delete
452 * 0 : no change
453 * 1 : some change but no delete
454 */
455static int wt_status_check_worktree_changes(struct wt_status *s,
456 int *dirty_submodules)
457{
458 int i;
459 int changes = 0;
460
461 *dirty_submodules = 0;
462
463 for (i = 0; i < s->change.nr; i++) {
464 struct wt_status_change_data *d;
465 d = s->change.items[i].util;
466 if (!d->worktree_status ||
467 d->worktree_status == DIFF_STATUS_UNMERGED)
468 continue;
469 if (!changes)
470 changes = 1;
471 if (d->dirty_submodule)
472 *dirty_submodules = 1;
473 if (d->worktree_status == DIFF_STATUS_DELETED)
474 changes = -1;
475 }
476 return changes;
477}
478
479static void wt_status_print_changed(struct wt_status *s)
480{
481 int i, dirty_submodules;
482 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
483
484 if (!worktree_changes)
485 return;
486
487 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
488
489 for (i = 0; i < s->change.nr; i++) {
490 struct wt_status_change_data *d;
491 struct string_list_item *it;
492 it = &(s->change.items[i]);
493 d = it->util;
494 if (!d->worktree_status ||
495 d->worktree_status == DIFF_STATUS_UNMERGED)
496 continue;
497 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
498 }
499 wt_status_print_trailer(s);
500}
501
502static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
503{
504 struct child_process sm_summary;
505 char summary_limit[64];
506 char index[PATH_MAX];
507 const char *env[] = { index, NULL };
508 const char *argv[] = {
509 "submodule",
510 "summary",
511 uncommitted ? "--files" : "--cached",
512 "--for-status",
513 "--summary-limit",
514 summary_limit,
515 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
516 NULL
517 };
518
519 sprintf(summary_limit, "%d", s->submodule_summary);
520 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
521
522 memset(&sm_summary, 0, sizeof(sm_summary));
523 sm_summary.argv = argv;
524 sm_summary.env = env;
525 sm_summary.git_cmd = 1;
526 sm_summary.no_stdin = 1;
527 fflush(s->fp);
528 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
529 run_command(&sm_summary);
530}
531
532static void wt_status_print_untracked(struct wt_status *s)
533{
534 int i;
535 struct strbuf buf = STRBUF_INIT;
536
537 if (!s->untracked.nr)
538 return;
539
540 wt_status_print_untracked_header(s);
541 for (i = 0; i < s->untracked.nr; i++) {
542 struct string_list_item *it;
543 it = &(s->untracked.items[i]);
544 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
545 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
546 quote_path(it->string, strlen(it->string),
547 &buf, s->prefix));
548 }
549 strbuf_release(&buf);
550}
551
552static void wt_status_print_verbose(struct wt_status *s)
553{
554 struct rev_info rev;
555 struct setup_revision_opt opt;
556
557 init_revisions(&rev, NULL);
558 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
559
560 memset(&opt, 0, sizeof(opt));
561 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
562 setup_revisions(0, NULL, &rev, &opt);
563
564 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
565 rev.diffopt.detect_rename = 1;
566 rev.diffopt.file = s->fp;
567 rev.diffopt.close_file = 0;
568 /*
569 * If we're not going to stdout, then we definitely don't
570 * want color, since we are going to the commit message
571 * file (and even the "auto" setting won't work, since it
572 * will have checked isatty on stdout).
573 */
574 if (s->fp != stdout)
575 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
576 run_diff_index(&rev, 1);
577}
578
579static void wt_status_print_tracking(struct wt_status *s)
580{
581 struct strbuf sb = STRBUF_INIT;
582 const char *cp, *ep;
583 struct branch *branch;
584
585 assert(s->branch && !s->is_initial);
586 if (prefixcmp(s->branch, "refs/heads/"))
587 return;
588 branch = branch_get(s->branch + 11);
589 if (!format_tracking_info(branch, &sb))
590 return;
591
592 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
593 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
594 "# %.*s", (int)(ep - cp), cp);
595 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
596}
597
598void wt_status_print(struct wt_status *s)
599{
600 const char *branch_color = color(WT_STATUS_HEADER, s);
601
602 if (s->branch) {
603 const char *on_what = "On branch ";
604 const char *branch_name = s->branch;
605 if (!prefixcmp(branch_name, "refs/heads/"))
606 branch_name += 11;
607 else if (!strcmp(branch_name, "HEAD")) {
608 branch_name = "";
609 branch_color = color(WT_STATUS_NOBRANCH, s);
610 on_what = "Not currently on any branch.";
611 }
612 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
613 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
614 if (!s->is_initial)
615 wt_status_print_tracking(s);
616 }
617
618 if (s->is_initial) {
619 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
620 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
621 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
622 }
623
624 wt_status_print_updated(s);
625 wt_status_print_unmerged(s);
626 wt_status_print_changed(s);
627 if (s->submodule_summary &&
628 (!s->ignore_submodule_arg ||
629 strcmp(s->ignore_submodule_arg, "all"))) {
630 wt_status_print_submodule_summary(s, 0); /* staged */
631 wt_status_print_submodule_summary(s, 1); /* unstaged */
632 }
633 if (s->show_untracked_files)
634 wt_status_print_untracked(s);
635 else if (s->commitable)
636 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
637
638 if (s->verbose)
639 wt_status_print_verbose(s);
640 if (!s->commitable) {
641 if (s->amend)
642 fprintf(s->fp, "# No changes\n");
643 else if (s->nowarn)
644 ; /* nothing */
645 else if (s->workdir_dirty)
646 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
647 else if (s->untracked.nr)
648 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
649 else if (s->is_initial)
650 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
651 else if (!s->show_untracked_files)
652 printf("nothing to commit (use -u to show untracked files)\n");
653 else
654 printf("nothing to commit (working directory clean)\n");
655 }
656}
657
658static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
659 struct wt_status *s)
660{
661 struct wt_status_change_data *d = it->util;
662 const char *how = "??";
663
664 switch (d->stagemask) {
665 case 1: how = "DD"; break; /* both deleted */
666 case 2: how = "AU"; break; /* added by us */
667 case 3: how = "UD"; break; /* deleted by them */
668 case 4: how = "UA"; break; /* added by them */
669 case 5: how = "DU"; break; /* deleted by us */
670 case 6: how = "AA"; break; /* both added */
671 case 7: how = "UU"; break; /* both modified */
672 }
673 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
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 printf(" %s\n", one);
681 strbuf_release(&onebuf);
682 }
683}
684
685static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
686 struct wt_status *s)
687{
688 struct wt_status_change_data *d = it->util;
689
690 if (d->index_status)
691 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
692 else
693 putchar(' ');
694 if (d->worktree_status)
695 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
696 else
697 putchar(' ');
698 putchar(' ');
699 if (null_termination) {
700 fprintf(stdout, "%s%c", it->string, 0);
701 if (d->head_path)
702 fprintf(stdout, "%s%c", d->head_path, 0);
703 } else {
704 struct strbuf onebuf = STRBUF_INIT;
705 const char *one;
706 if (d->head_path) {
707 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
708 printf("%s -> ", one);
709 strbuf_release(&onebuf);
710 }
711 one = quote_path(it->string, -1, &onebuf, s->prefix);
712 printf("%s\n", one);
713 strbuf_release(&onebuf);
714 }
715}
716
717static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
718 struct wt_status *s)
719{
720 if (null_termination) {
721 fprintf(stdout, "?? %s%c", it->string, 0);
722 } else {
723 struct strbuf onebuf = STRBUF_INIT;
724 const char *one;
725 one = quote_path(it->string, -1, &onebuf, s->prefix);
726 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
727 printf(" %s\n", one);
728 strbuf_release(&onebuf);
729 }
730}
731
732void wt_shortstatus_print(struct wt_status *s, int null_termination)
733{
734 int i;
735 for (i = 0; i < s->change.nr; i++) {
736 struct wt_status_change_data *d;
737 struct string_list_item *it;
738
739 it = &(s->change.items[i]);
740 d = it->util;
741 if (d->stagemask)
742 wt_shortstatus_unmerged(null_termination, it, s);
743 else
744 wt_shortstatus_status(null_termination, it, s);
745 }
746 for (i = 0; i < s->untracked.nr; i++) {
747 struct string_list_item *it;
748
749 it = &(s->untracked.items[i]);
750 wt_shortstatus_untracked(null_termination, it, s);
751 }
752}
753
754void wt_porcelain_print(struct wt_status *s, int null_termination)
755{
756 s->use_color = 0;
757 s->relative_paths = 0;
758 s->prefix = NULL;
759 wt_shortstatus_print(s, null_termination);
760}