]> git.ipfire.org Git - thirdparty/git.git/blob - wt-status.c
wt-status: collect ignored files
[thirdparty/git.git] / wt-status.c
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
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 */
20 };
21
22 static const char *color(int slot, struct wt_status *s)
23 {
24 return s->use_color > 0 ? s->color_palette[slot] : "";
25 }
26
27 void wt_status_prepare(struct wt_status *s)
28 {
29 unsigned char sha1[20];
30 const char *head;
31
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;
45 s->ignored.strdup_strings = 1;
46 }
47
48 static 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
65 static 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
81 static 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
100 static 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
110 static 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
117 static 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
140 static 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
217 static 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
247 static 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
267 static 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
300 static 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 rev.diffopt.format_callback = wt_status_collect_changed_cb;
311 rev.diffopt.format_callback_data = s;
312 rev.prune_data = s->pathspec;
313 run_diff_files(&rev, 0);
314 }
315
316 static void wt_status_collect_changes_index(struct wt_status *s)
317 {
318 struct rev_info rev;
319 struct setup_revision_opt opt;
320
321 init_revisions(&rev, NULL);
322 memset(&opt, 0, sizeof(opt));
323 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
324 setup_revisions(0, NULL, &rev, &opt);
325
326 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
327 rev.diffopt.format_callback = wt_status_collect_updated_cb;
328 rev.diffopt.format_callback_data = s;
329 rev.diffopt.detect_rename = 1;
330 rev.diffopt.rename_limit = 200;
331 rev.diffopt.break_opt = 0;
332 rev.prune_data = s->pathspec;
333 run_diff_index(&rev, 1);
334 }
335
336 static void wt_status_collect_changes_initial(struct wt_status *s)
337 {
338 int i;
339
340 for (i = 0; i < active_nr; i++) {
341 struct string_list_item *it;
342 struct wt_status_change_data *d;
343 struct cache_entry *ce = active_cache[i];
344
345 if (!ce_path_match(ce, s->pathspec))
346 continue;
347 it = string_list_insert(ce->name, &s->change);
348 d = it->util;
349 if (!d) {
350 d = xcalloc(1, sizeof(*d));
351 it->util = d;
352 }
353 if (ce_stage(ce)) {
354 d->index_status = DIFF_STATUS_UNMERGED;
355 d->stagemask |= (1 << (ce_stage(ce) - 1));
356 }
357 else
358 d->index_status = DIFF_STATUS_ADDED;
359 }
360 }
361
362 static void wt_status_collect_untracked(struct wt_status *s)
363 {
364 int i;
365 struct dir_struct dir;
366
367 if (!s->show_untracked_files)
368 return;
369 memset(&dir, 0, sizeof(dir));
370 if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
371 dir.flags |=
372 DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
373 setup_standard_excludes(&dir);
374
375 fill_directory(&dir, s->pathspec);
376 for (i = 0; i < dir.nr; i++) {
377 struct dir_entry *ent = dir.entries[i];
378 if (!cache_name_is_other(ent->name, ent->len))
379 continue;
380 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
381 continue;
382 string_list_insert(ent->name, &s->untracked);
383 free(ent);
384 }
385
386 if (s->show_ignored_files) {
387 dir.nr = 0;
388 dir.flags = DIR_SHOW_IGNORED | DIR_SHOW_OTHER_DIRECTORIES;
389 fill_directory(&dir, s->pathspec);
390 for (i = 0; i < dir.nr; i++) {
391 struct dir_entry *ent = dir.entries[i];
392 if (!cache_name_is_other(ent->name, ent->len))
393 continue;
394 if (!match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
395 continue;
396 string_list_insert(ent->name, &s->ignored);
397 free(ent);
398 }
399 }
400
401 free(dir.entries);
402 }
403
404 void wt_status_collect(struct wt_status *s)
405 {
406 wt_status_collect_changes_worktree(s);
407
408 if (s->is_initial)
409 wt_status_collect_changes_initial(s);
410 else
411 wt_status_collect_changes_index(s);
412 wt_status_collect_untracked(s);
413 }
414
415 static void wt_status_print_unmerged(struct wt_status *s)
416 {
417 int shown_header = 0;
418 int i;
419
420 for (i = 0; i < s->change.nr; i++) {
421 struct wt_status_change_data *d;
422 struct string_list_item *it;
423 it = &(s->change.items[i]);
424 d = it->util;
425 if (!d->stagemask)
426 continue;
427 if (!shown_header) {
428 wt_status_print_unmerged_header(s);
429 shown_header = 1;
430 }
431 wt_status_print_unmerged_data(s, it);
432 }
433 if (shown_header)
434 wt_status_print_trailer(s);
435
436 }
437
438 static void wt_status_print_updated(struct wt_status *s)
439 {
440 int shown_header = 0;
441 int i;
442
443 for (i = 0; i < s->change.nr; i++) {
444 struct wt_status_change_data *d;
445 struct string_list_item *it;
446 it = &(s->change.items[i]);
447 d = it->util;
448 if (!d->index_status ||
449 d->index_status == DIFF_STATUS_UNMERGED)
450 continue;
451 if (!shown_header) {
452 wt_status_print_cached_header(s);
453 s->commitable = 1;
454 shown_header = 1;
455 }
456 wt_status_print_change_data(s, WT_STATUS_UPDATED, it);
457 }
458 if (shown_header)
459 wt_status_print_trailer(s);
460 }
461
462 /*
463 * -1 : has delete
464 * 0 : no change
465 * 1 : some change but no delete
466 */
467 static int wt_status_check_worktree_changes(struct wt_status *s,
468 int *dirty_submodules)
469 {
470 int i;
471 int changes = 0;
472
473 *dirty_submodules = 0;
474
475 for (i = 0; i < s->change.nr; i++) {
476 struct wt_status_change_data *d;
477 d = s->change.items[i].util;
478 if (!d->worktree_status ||
479 d->worktree_status == DIFF_STATUS_UNMERGED)
480 continue;
481 if (!changes)
482 changes = 1;
483 if (d->dirty_submodule)
484 *dirty_submodules = 1;
485 if (d->worktree_status == DIFF_STATUS_DELETED)
486 changes = -1;
487 }
488 return changes;
489 }
490
491 static void wt_status_print_changed(struct wt_status *s)
492 {
493 int i, dirty_submodules;
494 int worktree_changes = wt_status_check_worktree_changes(s, &dirty_submodules);
495
496 if (!worktree_changes)
497 return;
498
499 wt_status_print_dirty_header(s, worktree_changes < 0, dirty_submodules);
500
501 for (i = 0; i < s->change.nr; i++) {
502 struct wt_status_change_data *d;
503 struct string_list_item *it;
504 it = &(s->change.items[i]);
505 d = it->util;
506 if (!d->worktree_status ||
507 d->worktree_status == DIFF_STATUS_UNMERGED)
508 continue;
509 wt_status_print_change_data(s, WT_STATUS_CHANGED, it);
510 }
511 wt_status_print_trailer(s);
512 }
513
514 static void wt_status_print_submodule_summary(struct wt_status *s, int uncommitted)
515 {
516 struct child_process sm_summary;
517 char summary_limit[64];
518 char index[PATH_MAX];
519 const char *env[] = { index, NULL };
520 const char *argv[] = {
521 "submodule",
522 "summary",
523 uncommitted ? "--files" : "--cached",
524 "--for-status",
525 "--summary-limit",
526 summary_limit,
527 uncommitted ? NULL : (s->amend ? "HEAD^" : "HEAD"),
528 NULL
529 };
530
531 sprintf(summary_limit, "%d", s->submodule_summary);
532 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
533
534 memset(&sm_summary, 0, sizeof(sm_summary));
535 sm_summary.argv = argv;
536 sm_summary.env = env;
537 sm_summary.git_cmd = 1;
538 sm_summary.no_stdin = 1;
539 fflush(s->fp);
540 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
541 run_command(&sm_summary);
542 }
543
544 static void wt_status_print_untracked(struct wt_status *s)
545 {
546 int i;
547 struct strbuf buf = STRBUF_INIT;
548
549 if (!s->untracked.nr)
550 return;
551
552 wt_status_print_untracked_header(s);
553 for (i = 0; i < s->untracked.nr; i++) {
554 struct string_list_item *it;
555 it = &(s->untracked.items[i]);
556 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "#\t");
557 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED, s), "%s",
558 quote_path(it->string, strlen(it->string),
559 &buf, s->prefix));
560 }
561 strbuf_release(&buf);
562 }
563
564 static void wt_status_print_verbose(struct wt_status *s)
565 {
566 struct rev_info rev;
567 struct setup_revision_opt opt;
568
569 init_revisions(&rev, NULL);
570 DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
571
572 memset(&opt, 0, sizeof(opt));
573 opt.def = s->is_initial ? EMPTY_TREE_SHA1_HEX : s->reference;
574 setup_revisions(0, NULL, &rev, &opt);
575
576 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
577 rev.diffopt.detect_rename = 1;
578 rev.diffopt.file = s->fp;
579 rev.diffopt.close_file = 0;
580 /*
581 * If we're not going to stdout, then we definitely don't
582 * want color, since we are going to the commit message
583 * file (and even the "auto" setting won't work, since it
584 * will have checked isatty on stdout).
585 */
586 if (s->fp != stdout)
587 DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
588 run_diff_index(&rev, 1);
589 }
590
591 static void wt_status_print_tracking(struct wt_status *s)
592 {
593 struct strbuf sb = STRBUF_INIT;
594 const char *cp, *ep;
595 struct branch *branch;
596
597 assert(s->branch && !s->is_initial);
598 if (prefixcmp(s->branch, "refs/heads/"))
599 return;
600 branch = branch_get(s->branch + 11);
601 if (!format_tracking_info(branch, &sb))
602 return;
603
604 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
605 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s),
606 "# %.*s", (int)(ep - cp), cp);
607 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
608 }
609
610 void wt_status_print(struct wt_status *s)
611 {
612 const char *branch_color = color(WT_STATUS_HEADER, s);
613
614 if (s->branch) {
615 const char *on_what = "On branch ";
616 const char *branch_name = s->branch;
617 if (!prefixcmp(branch_name, "refs/heads/"))
618 branch_name += 11;
619 else if (!strcmp(branch_name, "HEAD")) {
620 branch_name = "";
621 branch_color = color(WT_STATUS_NOBRANCH, s);
622 on_what = "Not currently on any branch.";
623 }
624 color_fprintf(s->fp, color(WT_STATUS_HEADER, s), "# ");
625 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
626 if (!s->is_initial)
627 wt_status_print_tracking(s);
628 }
629
630 if (s->is_initial) {
631 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
632 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "# Initial commit");
633 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER, s), "#");
634 }
635
636 wt_status_print_updated(s);
637 wt_status_print_unmerged(s);
638 wt_status_print_changed(s);
639 if (s->submodule_summary) {
640 wt_status_print_submodule_summary(s, 0); /* staged */
641 wt_status_print_submodule_summary(s, 1); /* unstaged */
642 }
643 if (s->show_untracked_files)
644 wt_status_print_untracked(s);
645 else if (s->commitable)
646 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
647
648 if (s->verbose)
649 wt_status_print_verbose(s);
650 if (!s->commitable) {
651 if (s->amend)
652 fprintf(s->fp, "# No changes\n");
653 else if (s->nowarn)
654 ; /* nothing */
655 else if (s->workdir_dirty)
656 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
657 else if (s->untracked.nr)
658 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
659 else if (s->is_initial)
660 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
661 else if (!s->show_untracked_files)
662 printf("nothing to commit (use -u to show untracked files)\n");
663 else
664 printf("nothing to commit (working directory clean)\n");
665 }
666 }
667
668 static void wt_shortstatus_unmerged(int null_termination, struct string_list_item *it,
669 struct wt_status *s)
670 {
671 struct wt_status_change_data *d = it->util;
672 const char *how = "??";
673
674 switch (d->stagemask) {
675 case 1: how = "DD"; break; /* both deleted */
676 case 2: how = "AU"; break; /* added by us */
677 case 3: how = "UD"; break; /* deleted by them */
678 case 4: how = "UA"; break; /* added by them */
679 case 5: how = "DU"; break; /* deleted by us */
680 case 6: how = "AA"; break; /* both added */
681 case 7: how = "UU"; break; /* both modified */
682 }
683 color_fprintf(s->fp, color(WT_STATUS_UNMERGED, s), "%s", how);
684 if (null_termination) {
685 fprintf(stdout, " %s%c", it->string, 0);
686 } else {
687 struct strbuf onebuf = STRBUF_INIT;
688 const char *one;
689 one = quote_path(it->string, -1, &onebuf, s->prefix);
690 printf(" %s\n", one);
691 strbuf_release(&onebuf);
692 }
693 }
694
695 static void wt_shortstatus_status(int null_termination, struct string_list_item *it,
696 struct wt_status *s)
697 {
698 struct wt_status_change_data *d = it->util;
699
700 if (d->index_status)
701 color_fprintf(s->fp, color(WT_STATUS_UPDATED, s), "%c", d->index_status);
702 else
703 putchar(' ');
704 if (d->worktree_status)
705 color_fprintf(s->fp, color(WT_STATUS_CHANGED, s), "%c", d->worktree_status);
706 else
707 putchar(' ');
708 putchar(' ');
709 if (null_termination) {
710 fprintf(stdout, "%s%c", it->string, 0);
711 if (d->head_path)
712 fprintf(stdout, "%s%c", d->head_path, 0);
713 } else {
714 struct strbuf onebuf = STRBUF_INIT;
715 const char *one;
716 if (d->head_path) {
717 one = quote_path(d->head_path, -1, &onebuf, s->prefix);
718 printf("%s -> ", one);
719 strbuf_release(&onebuf);
720 }
721 one = quote_path(it->string, -1, &onebuf, s->prefix);
722 printf("%s\n", one);
723 strbuf_release(&onebuf);
724 }
725 }
726
727 static void wt_shortstatus_untracked(int null_termination, struct string_list_item *it,
728 struct wt_status *s)
729 {
730 if (null_termination) {
731 fprintf(stdout, "?? %s%c", it->string, 0);
732 } else {
733 struct strbuf onebuf = STRBUF_INIT;
734 const char *one;
735 one = quote_path(it->string, -1, &onebuf, s->prefix);
736 color_fprintf(s->fp, color(WT_STATUS_UNTRACKED, s), "??");
737 printf(" %s\n", one);
738 strbuf_release(&onebuf);
739 }
740 }
741
742 void wt_shortstatus_print(struct wt_status *s, int null_termination)
743 {
744 int i;
745 for (i = 0; i < s->change.nr; i++) {
746 struct wt_status_change_data *d;
747 struct string_list_item *it;
748
749 it = &(s->change.items[i]);
750 d = it->util;
751 if (d->stagemask)
752 wt_shortstatus_unmerged(null_termination, it, s);
753 else
754 wt_shortstatus_status(null_termination, it, s);
755 }
756 for (i = 0; i < s->untracked.nr; i++) {
757 struct string_list_item *it;
758
759 it = &(s->untracked.items[i]);
760 wt_shortstatus_untracked(null_termination, it, s);
761 }
762 }
763
764 void wt_porcelain_print(struct wt_status *s, int null_termination)
765 {
766 s->use_color = 0;
767 s->relative_paths = 0;
768 s->prefix = NULL;
769 wt_shortstatus_print(s, null_termination);
770 }