10 #include "run-command.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];
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
;
37 s
->relative_paths
= 1;
38 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
39 s
->branch
= head
? xstrdup(head
) : NULL
;
40 s
->reference
= "HEAD";
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
)
56 else if (!s
->is_initial
)
57 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
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
)
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
);
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
,
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
)
89 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to update what will be committed)");
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
)
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
,
138 struct string_list_item
*it
)
140 struct wt_status_change_data
*d
= it
->util
;
141 const char *c
= color(change_type
, s
);
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
;
153 one_name
= d
->head_path
;
155 case WT_STATUS_CHANGED
:
156 status
= d
->worktree_status
;
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");
165 case DIFF_STATUS_ADDED
:
166 color_fprintf(s
->fp
, c
, "new file: %s", one
);
168 case DIFF_STATUS_COPIED
:
169 color_fprintf(s
->fp
, c
, "copied: %s -> %s", one
, two
);
171 case DIFF_STATUS_DELETED
:
172 color_fprintf(s
->fp
, c
, "deleted: %s", one
);
174 case DIFF_STATUS_MODIFIED
:
175 color_fprintf(s
->fp
, c
, "modified: %s", one
);
177 case DIFF_STATUS_RENAMED
:
178 color_fprintf(s
->fp
, c
, "renamed: %s -> %s", one
, two
);
180 case DIFF_STATUS_TYPE_CHANGED
:
181 color_fprintf(s
->fp
, c
, "typechange: %s", one
);
183 case DIFF_STATUS_UNKNOWN
:
184 color_fprintf(s
->fp
, c
, "unknown: %s", one
);
186 case DIFF_STATUS_UNMERGED
:
187 color_fprintf(s
->fp
, c
, "unmerged: %s", one
);
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
,
201 struct wt_status
*s
= data
;
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
;
213 it
= string_list_insert(p
->one
->path
, &s
->change
);
216 d
= xcalloc(1, sizeof(*d
));
219 if (!d
->worktree_status
)
220 d
->worktree_status
= p
->status
;
224 static int unmerged_mask(const char *path
)
227 struct cache_entry
*ce
;
229 pos
= cache_name_pos(path
, strlen(path
));
235 while (pos
< active_nr
) {
236 ce
= active_cache
[pos
++];
237 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
239 mask
|= (1 << (ce_stage(ce
) - 1));
244 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
245 struct diff_options
*options
,
248 struct wt_status
*s
= data
;
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
;
257 it
= string_list_insert(p
->two
->path
, &s
->change
);
260 d
= xcalloc(1, sizeof(*d
));
263 if (!d
->index_status
)
264 d
->index_status
= p
->status
;
266 case DIFF_STATUS_COPIED
:
267 case DIFF_STATUS_RENAMED
:
268 d
->head_path
= xstrdup(p
->one
->path
);
270 case DIFF_STATUS_UNMERGED
:
271 d
->stagemask
= unmerged_mask(p
->two
->path
);
277 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
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
)
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
)
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
))
318 it
= string_list_insert(ce
->name
, &s
->change
);
321 d
= xcalloc(1, sizeof(*d
));
325 d
->index_status
= DIFF_STATUS_UNMERGED
;
326 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
329 d
->index_status
= DIFF_STATUS_ADDED
;
333 static void wt_status_collect_untracked(struct wt_status
*s
)
336 struct dir_struct dir
;
338 if (!s
->show_untracked_files
)
340 memset(&dir
, 0, sizeof(dir
));
341 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
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
))
351 if (!match_pathspec(s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
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
);
363 wt_status_collect_changes_initial(s
);
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;
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
]);
382 wt_status_print_unmerged_header(s
);
385 wt_status_print_unmerged_data(s
, it
);
388 wt_status_print_trailer(s
);
392 static void wt_status_print_updated(struct wt_status
*s
)
394 int shown_header
= 0;
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
]);
402 if (!d
->index_status
||
403 d
->index_status
== DIFF_STATUS_UNMERGED
)
406 wt_status_print_cached_header(s
);
410 wt_status_print_change_data(s
, WT_STATUS_UPDATED
, it
);
413 wt_status_print_trailer(s
);
419 * 1 : some change but no delete
421 static int wt_status_check_worktree_changes(struct wt_status
*s
)
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
)
433 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
439 static void wt_status_print_changed(struct wt_status
*s
)
442 int worktree_changes
= wt_status_check_worktree_changes(s
);
444 if (!worktree_changes
)
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
]);
454 if (!d
->worktree_status
||
455 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
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
[] = {
471 uncommitted
? "--files" : "--cached",
475 uncommitted
? NULL
: (s
->amend
? "HEAD^" : "HEAD"),
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;
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
)
495 struct strbuf buf
= STRBUF_INIT
;
497 if (!s
->untracked
.nr
)
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
),
509 strbuf_release(&buf
);
512 static void wt_status_print_verbose(struct wt_status
*s
)
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).
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
;
539 struct branch
*branch
;
541 assert(s
->branch
&& !s
->is_initial
);
542 if (prefixcmp(s
->branch
, "refs/heads/"))
544 branch
= branch_get(s
->branch
+ 11);
545 if (!format_tracking_info(branch
, &sb
))
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
);
559 const char *on_what
= "On branch ";
560 const char *branch_name
= s
->branch
;
561 if (!prefixcmp(branch_name
, "refs/heads/"))
563 else if (!strcmp(branch_name
, "HEAD")) {
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
);
571 wt_status_print_tracking(s
);
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");
593 wt_status_print_verbose(s
);
594 if (!s
->commitable
) {
596 fprintf(s
->fp
, "# No changes\n");
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");
608 printf("nothing to commit (working directory clean)\n");
612 static void wt_shortstatus_unmerged(int null_termination
, struct string_list_item
*it
,
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);
631 struct strbuf onebuf
= STRBUF_INIT
;
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
,
642 struct wt_status_change_data
*d
= it
->util
;
645 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
648 if (d
->worktree_status
)
649 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
653 if (null_termination
) {
654 fprintf(stdout
, "%s%c", it
->string
, 0);
656 fprintf(stdout
, "%s%c", d
->head_path
, 0);
658 struct strbuf onebuf
= STRBUF_INIT
;
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
);
667 strbuf_release(&onebuf
);
671 static void wt_shortstatus_untracked(int null_termination
, struct string_list_item
*it
,
674 if (null_termination
) {
675 fprintf(stdout
, "?? %s%c", it
->string
, 0);
677 struct strbuf onebuf
= STRBUF_INIT
;
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
)
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
]);
696 wt_shortstatus_unmerged(null_termination
, it
, s
);
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
)
711 s
->relative_paths
= 0;
713 wt_shortstatus_print(s
, null_termination
);