10 #include "run-command.h"
13 #include "submodule.h"
17 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
18 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
23 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
24 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
25 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
26 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
29 static const char *color(int slot
, struct wt_status
*s
)
32 if (want_color(s
->use_color
))
33 c
= s
->color_palette
[slot
];
34 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
35 c
= s
->color_palette
[WT_STATUS_HEADER
];
39 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
40 const char *fmt
, va_list ap
, const char *trail
)
42 struct strbuf sb
= STRBUF_INIT
;
43 struct strbuf linebuf
= STRBUF_INIT
;
44 const char *line
, *eol
;
46 strbuf_vaddf(&sb
, fmt
, ap
);
48 strbuf_addch(&sb
, comment_line_char
);
50 strbuf_addch(&sb
, ' ');
51 color_print_strbuf(s
->fp
, color
, &sb
);
53 fprintf(s
->fp
, "%s", trail
);
57 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
58 eol
= strchr(line
, '\n');
60 strbuf_reset(&linebuf
);
62 strbuf_addch(&linebuf
, comment_line_char
);
63 if (*line
!= '\n' && *line
!= '\t')
64 strbuf_addch(&linebuf
, ' ');
67 strbuf_add(&linebuf
, line
, eol
- line
);
69 strbuf_addstr(&linebuf
, line
);
70 color_print_strbuf(s
->fp
, color
, &linebuf
);
78 fprintf(s
->fp
, "%s", trail
);
79 strbuf_release(&linebuf
);
83 void status_printf_ln(struct wt_status
*s
, const char *color
,
89 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
93 void status_printf(struct wt_status
*s
, const char *color
,
99 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
103 static void status_printf_more(struct wt_status
*s
, const char *color
,
104 const char *fmt
, ...)
109 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
113 void wt_status_prepare(struct wt_status
*s
)
115 unsigned char sha1
[20];
117 memset(s
, 0, sizeof(*s
));
118 memcpy(s
->color_palette
, default_wt_status_colors
,
119 sizeof(default_wt_status_colors
));
120 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
122 s
->relative_paths
= 1;
123 s
->branch
= resolve_refdup("HEAD", sha1
, 0, NULL
);
124 s
->reference
= "HEAD";
126 s
->index_file
= get_index_file();
127 s
->change
.strdup_strings
= 1;
128 s
->untracked
.strdup_strings
= 1;
129 s
->ignored
.strdup_strings
= 1;
130 s
->show_branch
= -1; /* unspecified */
133 static void wt_status_print_unmerged_header(struct wt_status
*s
)
136 int del_mod_conflict
= 0;
137 int both_deleted
= 0;
139 const char *c
= color(WT_STATUS_HEADER
, s
);
141 status_printf_ln(s
, c
, _("Unmerged paths:"));
143 for (i
= 0; i
< s
->change
.nr
; i
++) {
144 struct string_list_item
*it
= &(s
->change
.items
[i
]);
145 struct wt_status_change_data
*d
= it
->util
;
147 switch (d
->stagemask
) {
155 del_mod_conflict
= 1;
163 if (!advice_status_hints
)
165 if (s
->whence
!= FROM_COMMIT
)
167 else if (!s
->is_initial
)
168 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
170 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
173 if (!del_mod_conflict
)
174 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
176 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
177 } else if (!del_mod_conflict
&& !not_deleted
) {
178 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
180 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
182 status_printf_ln(s
, c
, "");
185 static void wt_status_print_cached_header(struct wt_status
*s
)
187 const char *c
= color(WT_STATUS_HEADER
, s
);
189 status_printf_ln(s
, c
, _("Changes to be committed:"));
190 if (!advice_status_hints
)
192 if (s
->whence
!= FROM_COMMIT
)
193 ; /* NEEDSWORK: use "git reset --unresolve"??? */
194 else if (!s
->is_initial
)
195 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
197 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
198 status_printf_ln(s
, c
, "");
201 static void wt_status_print_dirty_header(struct wt_status
*s
,
203 int has_dirty_submodules
)
205 const char *c
= color(WT_STATUS_HEADER
, s
);
207 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
208 if (!advice_status_hints
)
211 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
213 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
214 status_printf_ln(s
, c
, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
215 if (has_dirty_submodules
)
216 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
217 status_printf_ln(s
, c
, "");
220 static void wt_status_print_other_header(struct wt_status
*s
,
224 const char *c
= color(WT_STATUS_HEADER
, s
);
225 status_printf_ln(s
, c
, "%s:", what
);
226 if (!advice_status_hints
)
228 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
229 status_printf_ln(s
, c
, "");
232 static void wt_status_print_trailer(struct wt_status
*s
)
234 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
237 #define quote_path quote_path_relative
239 static void wt_status_print_unmerged_data(struct wt_status
*s
,
240 struct string_list_item
*it
)
242 const char *c
= color(WT_STATUS_UNMERGED
, s
);
243 struct wt_status_change_data
*d
= it
->util
;
244 struct strbuf onebuf
= STRBUF_INIT
;
245 const char *one
, *how
= _("bug");
247 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
248 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
249 switch (d
->stagemask
) {
250 case 1: how
= _("both deleted:"); break;
251 case 2: how
= _("added by us:"); break;
252 case 3: how
= _("deleted by them:"); break;
253 case 4: how
= _("added by them:"); break;
254 case 5: how
= _("deleted by us:"); break;
255 case 6: how
= _("both added:"); break;
256 case 7: how
= _("both modified:"); break;
258 status_printf_more(s
, c
, "%-20s%s\n", how
, one
);
259 strbuf_release(&onebuf
);
262 static void wt_status_print_change_data(struct wt_status
*s
,
264 struct string_list_item
*it
)
266 struct wt_status_change_data
*d
= it
->util
;
267 const char *c
= color(change_type
, s
);
271 const char *one
, *two
;
272 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
273 struct strbuf extra
= STRBUF_INIT
;
275 one_name
= two_name
= it
->string
;
276 switch (change_type
) {
277 case WT_STATUS_UPDATED
:
278 status
= d
->index_status
;
280 one_name
= d
->head_path
;
282 case WT_STATUS_CHANGED
:
283 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
284 strbuf_addstr(&extra
, " (");
285 if (d
->new_submodule_commits
)
286 strbuf_addf(&extra
, _("new commits, "));
287 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
288 strbuf_addf(&extra
, _("modified content, "));
289 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
290 strbuf_addf(&extra
, _("untracked content, "));
291 strbuf_setlen(&extra
, extra
.len
- 2);
292 strbuf_addch(&extra
, ')');
294 status
= d
->worktree_status
;
297 die("BUG: unhandled change_type %d in wt_status_print_change_data",
301 one
= quote_path(one_name
, -1, &onebuf
, s
->prefix
);
302 two
= quote_path(two_name
, -1, &twobuf
, s
->prefix
);
304 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
306 case DIFF_STATUS_ADDED
:
307 status_printf_more(s
, c
, _("new file: %s"), one
);
309 case DIFF_STATUS_COPIED
:
310 status_printf_more(s
, c
, _("copied: %s -> %s"), one
, two
);
312 case DIFF_STATUS_DELETED
:
313 status_printf_more(s
, c
, _("deleted: %s"), one
);
315 case DIFF_STATUS_MODIFIED
:
316 status_printf_more(s
, c
, _("modified: %s"), one
);
318 case DIFF_STATUS_RENAMED
:
319 status_printf_more(s
, c
, _("renamed: %s -> %s"), one
, two
);
321 case DIFF_STATUS_TYPE_CHANGED
:
322 status_printf_more(s
, c
, _("typechange: %s"), one
);
324 case DIFF_STATUS_UNKNOWN
:
325 status_printf_more(s
, c
, _("unknown: %s"), one
);
327 case DIFF_STATUS_UNMERGED
:
328 status_printf_more(s
, c
, _("unmerged: %s"), one
);
331 die(_("bug: unhandled diff status %c"), status
);
334 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
335 strbuf_release(&extra
);
337 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
338 strbuf_release(&onebuf
);
339 strbuf_release(&twobuf
);
342 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
343 struct diff_options
*options
,
346 struct wt_status
*s
= data
;
351 s
->workdir_dirty
= 1;
352 for (i
= 0; i
< q
->nr
; i
++) {
353 struct diff_filepair
*p
;
354 struct string_list_item
*it
;
355 struct wt_status_change_data
*d
;
358 it
= string_list_insert(&s
->change
, p
->one
->path
);
361 d
= xcalloc(1, sizeof(*d
));
364 if (!d
->worktree_status
)
365 d
->worktree_status
= p
->status
;
366 d
->dirty_submodule
= p
->two
->dirty_submodule
;
367 if (S_ISGITLINK(p
->two
->mode
))
368 d
->new_submodule_commits
= !!hashcmp(p
->one
->sha1
, p
->two
->sha1
);
372 static int unmerged_mask(const char *path
)
375 struct cache_entry
*ce
;
377 pos
= cache_name_pos(path
, strlen(path
));
383 while (pos
< active_nr
) {
384 ce
= active_cache
[pos
++];
385 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
387 mask
|= (1 << (ce_stage(ce
) - 1));
392 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
393 struct diff_options
*options
,
396 struct wt_status
*s
= data
;
399 for (i
= 0; i
< q
->nr
; i
++) {
400 struct diff_filepair
*p
;
401 struct string_list_item
*it
;
402 struct wt_status_change_data
*d
;
405 it
= string_list_insert(&s
->change
, p
->two
->path
);
408 d
= xcalloc(1, sizeof(*d
));
411 if (!d
->index_status
)
412 d
->index_status
= p
->status
;
414 case DIFF_STATUS_COPIED
:
415 case DIFF_STATUS_RENAMED
:
416 d
->head_path
= xstrdup(p
->one
->path
);
418 case DIFF_STATUS_UNMERGED
:
419 d
->stagemask
= unmerged_mask(p
->two
->path
);
425 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
429 init_revisions(&rev
, NULL
);
430 setup_revisions(0, NULL
, &rev
, NULL
);
431 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
432 DIFF_OPT_SET(&rev
.diffopt
, DIRTY_SUBMODULES
);
433 if (!s
->show_untracked_files
)
434 DIFF_OPT_SET(&rev
.diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
435 if (s
->ignore_submodule_arg
) {
436 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
437 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
439 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
440 rev
.diffopt
.format_callback_data
= s
;
441 init_pathspec(&rev
.prune_data
, s
->pathspec
);
442 run_diff_files(&rev
, 0);
445 static void wt_status_collect_changes_index(struct wt_status
*s
)
448 struct setup_revision_opt opt
;
450 init_revisions(&rev
, NULL
);
451 memset(&opt
, 0, sizeof(opt
));
452 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
453 setup_revisions(0, NULL
, &rev
, &opt
);
455 if (s
->ignore_submodule_arg
) {
456 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
457 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
460 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
461 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
462 rev
.diffopt
.format_callback_data
= s
;
463 rev
.diffopt
.detect_rename
= 1;
464 rev
.diffopt
.rename_limit
= 200;
465 rev
.diffopt
.break_opt
= 0;
466 init_pathspec(&rev
.prune_data
, s
->pathspec
);
467 run_diff_index(&rev
, 1);
470 static void wt_status_collect_changes_initial(struct wt_status
*s
)
472 struct pathspec pathspec
;
475 init_pathspec(&pathspec
, s
->pathspec
);
476 for (i
= 0; i
< active_nr
; i
++) {
477 struct string_list_item
*it
;
478 struct wt_status_change_data
*d
;
479 struct cache_entry
*ce
= active_cache
[i
];
481 if (!ce_path_match(ce
, &pathspec
))
483 it
= string_list_insert(&s
->change
, ce
->name
);
486 d
= xcalloc(1, sizeof(*d
));
490 d
->index_status
= DIFF_STATUS_UNMERGED
;
491 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
494 d
->index_status
= DIFF_STATUS_ADDED
;
496 free_pathspec(&pathspec
);
499 static void wt_status_collect_untracked(struct wt_status
*s
)
502 struct dir_struct dir
;
503 struct timeval t_begin
;
505 if (!s
->show_untracked_files
)
508 if (advice_status_u_option
)
509 gettimeofday(&t_begin
, NULL
);
511 memset(&dir
, 0, sizeof(dir
));
512 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
514 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
515 if (s
->show_ignored_files
)
516 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
517 setup_standard_excludes(&dir
);
519 fill_directory(&dir
, s
->pathspec
);
521 for (i
= 0; i
< dir
.nr
; i
++) {
522 struct dir_entry
*ent
= dir
.entries
[i
];
523 if (cache_name_is_other(ent
->name
, ent
->len
) &&
524 match_pathspec(s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
525 string_list_insert(&s
->untracked
, ent
->name
);
529 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
530 struct dir_entry
*ent
= dir
.ignored
[i
];
531 if (cache_name_is_other(ent
->name
, ent
->len
) &&
532 match_pathspec(s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
533 string_list_insert(&s
->ignored
, ent
->name
);
539 clear_directory(&dir
);
541 if (advice_status_u_option
) {
542 struct timeval t_end
;
543 gettimeofday(&t_end
, NULL
);
545 (uint64_t)t_end
.tv_sec
* 1000 + t_end
.tv_usec
/ 1000 -
546 ((uint64_t)t_begin
.tv_sec
* 1000 + t_begin
.tv_usec
/ 1000);
550 void wt_status_collect(struct wt_status
*s
)
552 wt_status_collect_changes_worktree(s
);
555 wt_status_collect_changes_initial(s
);
557 wt_status_collect_changes_index(s
);
558 wt_status_collect_untracked(s
);
561 static void wt_status_print_unmerged(struct wt_status
*s
)
563 int shown_header
= 0;
566 for (i
= 0; i
< s
->change
.nr
; i
++) {
567 struct wt_status_change_data
*d
;
568 struct string_list_item
*it
;
569 it
= &(s
->change
.items
[i
]);
574 wt_status_print_unmerged_header(s
);
577 wt_status_print_unmerged_data(s
, it
);
580 wt_status_print_trailer(s
);
584 static void wt_status_print_updated(struct wt_status
*s
)
586 int shown_header
= 0;
589 for (i
= 0; i
< s
->change
.nr
; i
++) {
590 struct wt_status_change_data
*d
;
591 struct string_list_item
*it
;
592 it
= &(s
->change
.items
[i
]);
594 if (!d
->index_status
||
595 d
->index_status
== DIFF_STATUS_UNMERGED
)
598 wt_status_print_cached_header(s
);
602 wt_status_print_change_data(s
, WT_STATUS_UPDATED
, it
);
605 wt_status_print_trailer(s
);
611 * 1 : some change but no delete
613 static int wt_status_check_worktree_changes(struct wt_status
*s
,
614 int *dirty_submodules
)
619 *dirty_submodules
= 0;
621 for (i
= 0; i
< s
->change
.nr
; i
++) {
622 struct wt_status_change_data
*d
;
623 d
= s
->change
.items
[i
].util
;
624 if (!d
->worktree_status
||
625 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
629 if (d
->dirty_submodule
)
630 *dirty_submodules
= 1;
631 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
637 static void wt_status_print_changed(struct wt_status
*s
)
639 int i
, dirty_submodules
;
640 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
642 if (!worktree_changes
)
645 wt_status_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
647 for (i
= 0; i
< s
->change
.nr
; i
++) {
648 struct wt_status_change_data
*d
;
649 struct string_list_item
*it
;
650 it
= &(s
->change
.items
[i
]);
652 if (!d
->worktree_status
||
653 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
655 wt_status_print_change_data(s
, WT_STATUS_CHANGED
, it
);
657 wt_status_print_trailer(s
);
660 static void wt_status_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
662 struct child_process sm_summary
;
663 char summary_limit
[64];
664 char index
[PATH_MAX
];
665 const char *env
[] = { NULL
, NULL
};
669 argv
[0] = "submodule";
671 argv
[2] = uncommitted
? "--files" : "--cached";
672 argv
[3] = "--for-status";
673 argv
[4] = "--summary-limit";
674 argv
[5] = summary_limit
;
675 argv
[6] = uncommitted
? NULL
: (s
->amend
? "HEAD^" : "HEAD");
678 sprintf(summary_limit
, "%d", s
->submodule_summary
);
679 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
681 memset(&sm_summary
, 0, sizeof(sm_summary
));
682 sm_summary
.argv
= argv
;
683 sm_summary
.env
= env
;
684 sm_summary
.git_cmd
= 1;
685 sm_summary
.no_stdin
= 1;
687 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
688 run_command(&sm_summary
);
691 static void wt_status_print_other(struct wt_status
*s
,
692 struct string_list
*l
,
697 struct strbuf buf
= STRBUF_INIT
;
698 static struct string_list output
= STRING_LIST_INIT_DUP
;
699 struct column_options copts
;
704 wt_status_print_other_header(s
, what
, how
);
706 for (i
= 0; i
< l
->nr
; i
++) {
707 struct string_list_item
*it
;
710 path
= quote_path(it
->string
, strlen(it
->string
),
712 if (column_active(s
->colopts
)) {
713 string_list_append(&output
, path
);
716 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
717 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
721 strbuf_release(&buf
);
722 if (!column_active(s
->colopts
))
725 strbuf_addf(&buf
, "%s#\t%s",
726 color(WT_STATUS_HEADER
, s
),
727 color(WT_STATUS_UNTRACKED
, s
));
728 memset(&copts
, 0, sizeof(copts
));
730 copts
.indent
= buf
.buf
;
731 if (want_color(s
->use_color
))
732 copts
.nl
= GIT_COLOR_RESET
"\n";
733 print_columns(&output
, s
->colopts
, &copts
);
734 string_list_clear(&output
, 0);
735 strbuf_release(&buf
);
738 static void wt_status_print_verbose(struct wt_status
*s
)
741 struct setup_revision_opt opt
;
743 init_revisions(&rev
, NULL
);
744 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
746 memset(&opt
, 0, sizeof(opt
));
747 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
748 setup_revisions(0, NULL
, &rev
, &opt
);
750 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
751 rev
.diffopt
.detect_rename
= 1;
752 rev
.diffopt
.file
= s
->fp
;
753 rev
.diffopt
.close_file
= 0;
755 * If we're not going to stdout, then we definitely don't
756 * want color, since we are going to the commit message
757 * file (and even the "auto" setting won't work, since it
758 * will have checked isatty on stdout).
761 rev
.diffopt
.use_color
= 0;
762 run_diff_index(&rev
, 1);
765 static void wt_status_print_tracking(struct wt_status
*s
)
767 struct strbuf sb
= STRBUF_INIT
;
769 struct branch
*branch
;
771 assert(s
->branch
&& !s
->is_initial
);
772 if (prefixcmp(s
->branch
, "refs/heads/"))
774 branch
= branch_get(s
->branch
+ 11);
775 if (!format_tracking_info(branch
, &sb
))
778 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
779 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
780 "%c %.*s", comment_line_char
,
782 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
786 static int has_unmerged(struct wt_status
*s
)
790 for (i
= 0; i
< s
->change
.nr
; i
++) {
791 struct wt_status_change_data
*d
;
792 d
= s
->change
.items
[i
].util
;
799 static void show_merge_in_progress(struct wt_status
*s
,
800 struct wt_status_state
*state
,
803 if (has_unmerged(s
)) {
804 status_printf_ln(s
, color
, _("You have unmerged paths."));
805 if (advice_status_hints
)
806 status_printf_ln(s
, color
,
807 _(" (fix conflicts and run \"git commit\")"));
809 status_printf_ln(s
, color
,
810 _("All conflicts fixed but you are still merging."));
811 if (advice_status_hints
)
812 status_printf_ln(s
, color
,
813 _(" (use \"git commit\" to conclude merge)"));
815 wt_status_print_trailer(s
);
818 static void show_am_in_progress(struct wt_status
*s
,
819 struct wt_status_state
*state
,
822 status_printf_ln(s
, color
,
823 _("You are in the middle of an am session."));
824 if (state
->am_empty_patch
)
825 status_printf_ln(s
, color
,
826 _("The current patch is empty."));
827 if (advice_status_hints
) {
828 if (!state
->am_empty_patch
)
829 status_printf_ln(s
, color
,
830 _(" (fix conflicts and then run \"git am --continue\")"));
831 status_printf_ln(s
, color
,
832 _(" (use \"git am --skip\" to skip this patch)"));
833 status_printf_ln(s
, color
,
834 _(" (use \"git am --abort\" to restore the original branch)"));
836 wt_status_print_trailer(s
);
839 static char *read_line_from_git_path(const char *filename
)
841 struct strbuf buf
= STRBUF_INIT
;
842 FILE *fp
= fopen(git_path("%s", filename
), "r");
844 strbuf_release(&buf
);
847 strbuf_getline(&buf
, fp
, '\n');
849 return strbuf_detach(&buf
, NULL
);
851 strbuf_release(&buf
);
856 static int split_commit_in_progress(struct wt_status
*s
)
858 int split_in_progress
= 0;
859 char *head
= read_line_from_git_path("HEAD");
860 char *orig_head
= read_line_from_git_path("ORIG_HEAD");
861 char *rebase_amend
= read_line_from_git_path("rebase-merge/amend");
862 char *rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
864 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
||
865 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
866 return split_in_progress
;
868 if (!strcmp(rebase_amend
, rebase_orig_head
)) {
869 if (strcmp(head
, rebase_amend
))
870 split_in_progress
= 1;
871 } else if (strcmp(orig_head
, rebase_orig_head
)) {
872 split_in_progress
= 1;
875 if (!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
)
876 split_in_progress
= 0;
881 free(rebase_orig_head
);
882 return split_in_progress
;
885 static void show_rebase_in_progress(struct wt_status
*s
,
886 struct wt_status_state
*state
,
891 if (has_unmerged(s
)) {
893 status_printf_ln(s
, color
,
894 _("You are currently rebasing branch '%s' on '%s'."),
898 status_printf_ln(s
, color
,
899 _("You are currently rebasing."));
900 if (advice_status_hints
) {
901 status_printf_ln(s
, color
,
902 _(" (fix conflicts and then run \"git rebase --continue\")"));
903 status_printf_ln(s
, color
,
904 _(" (use \"git rebase --skip\" to skip this patch)"));
905 status_printf_ln(s
, color
,
906 _(" (use \"git rebase --abort\" to check out the original branch)"));
908 } else if (state
->rebase_in_progress
|| !stat(git_path("MERGE_MSG"), &st
)) {
910 status_printf_ln(s
, color
,
911 _("You are currently rebasing branch '%s' on '%s'."),
915 status_printf_ln(s
, color
,
916 _("You are currently rebasing."));
917 if (advice_status_hints
)
918 status_printf_ln(s
, color
,
919 _(" (all conflicts fixed: run \"git rebase --continue\")"));
920 } else if (split_commit_in_progress(s
)) {
922 status_printf_ln(s
, color
,
923 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
927 status_printf_ln(s
, color
,
928 _("You are currently splitting a commit during a rebase."));
929 if (advice_status_hints
)
930 status_printf_ln(s
, color
,
931 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
934 status_printf_ln(s
, color
,
935 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
939 status_printf_ln(s
, color
,
940 _("You are currently editing a commit during a rebase."));
941 if (advice_status_hints
&& !s
->amend
) {
942 status_printf_ln(s
, color
,
943 _(" (use \"git commit --amend\" to amend the current commit)"));
944 status_printf_ln(s
, color
,
945 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
948 wt_status_print_trailer(s
);
951 static void show_cherry_pick_in_progress(struct wt_status
*s
,
952 struct wt_status_state
*state
,
955 status_printf_ln(s
, color
, _("You are currently cherry-picking."));
956 if (advice_status_hints
) {
958 status_printf_ln(s
, color
,
959 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
961 status_printf_ln(s
, color
,
962 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
963 status_printf_ln(s
, color
,
964 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
966 wt_status_print_trailer(s
);
969 static void show_revert_in_progress(struct wt_status
*s
,
970 struct wt_status_state
*state
,
973 status_printf_ln(s
, color
, _("You are currently reverting commit %s."),
974 find_unique_abbrev(state
->revert_head_sha1
, DEFAULT_ABBREV
));
975 if (advice_status_hints
) {
977 status_printf_ln(s
, color
,
978 _(" (fix conflicts and run \"git revert --continue\")"));
980 status_printf_ln(s
, color
,
981 _(" (all conflicts fixed: run \"git revert --continue\")"));
982 status_printf_ln(s
, color
,
983 _(" (use \"git revert --abort\" to cancel the revert operation)"));
985 wt_status_print_trailer(s
);
988 static void show_bisect_in_progress(struct wt_status
*s
,
989 struct wt_status_state
*state
,
993 status_printf_ln(s
, color
,
994 _("You are currently bisecting, started from branch '%s'."),
997 status_printf_ln(s
, color
,
998 _("You are currently bisecting."));
999 if (advice_status_hints
)
1000 status_printf_ln(s
, color
,
1001 _(" (use \"git bisect reset\" to get back to the original branch)"));
1002 wt_status_print_trailer(s
);
1006 * Extract branch information from rebase/bisect
1008 static char *read_and_strip_branch(const char *path
)
1010 struct strbuf sb
= STRBUF_INIT
;
1011 unsigned char sha1
[20];
1013 if (strbuf_read_file(&sb
, git_path("%s", path
), 0) <= 0)
1016 while (&sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1017 strbuf_setlen(&sb
, sb
.len
- 1);
1020 if (!prefixcmp(sb
.buf
, "refs/heads/"))
1021 strbuf_remove(&sb
,0, strlen("refs/heads/"));
1022 else if (!prefixcmp(sb
.buf
, "refs/"))
1024 else if (!get_sha1_hex(sb
.buf
, sha1
)) {
1026 abbrev
= find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
1028 strbuf_addstr(&sb
, abbrev
);
1029 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1033 return strbuf_detach(&sb
, NULL
);
1036 strbuf_release(&sb
);
1040 struct grab_1st_switch_cbdata
{
1042 unsigned char nsha1
[20];
1045 static int grab_1st_switch(unsigned char *osha1
, unsigned char *nsha1
,
1046 const char *email
, unsigned long timestamp
, int tz
,
1047 const char *message
, void *cb_data
)
1049 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1050 const char *target
= NULL
, *end
;
1052 if (prefixcmp(message
, "checkout: moving from "))
1054 message
+= strlen("checkout: moving from ");
1055 target
= strstr(message
, " to ");
1058 target
+= strlen(" to ");
1059 strbuf_reset(&cb
->buf
);
1060 hashcpy(cb
->nsha1
, nsha1
);
1061 for (end
= target
; *end
&& *end
!= '\n'; end
++)
1063 strbuf_add(&cb
->buf
, target
, end
- target
);
1067 static void wt_status_get_detached_from(struct wt_status_state
*state
)
1069 struct grab_1st_switch_cbdata cb
;
1070 struct commit
*commit
;
1071 unsigned char sha1
[20];
1074 strbuf_init(&cb
.buf
, 0);
1075 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1076 strbuf_release(&cb
.buf
);
1080 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, sha1
, &ref
) == 1 &&
1081 /* sha1 is a commit? match without further lookup */
1082 (!hashcmp(cb
.nsha1
, sha1
) ||
1083 /* perhaps sha1 is a tag, try to dereference to a commit */
1084 ((commit
= lookup_commit_reference_gently(sha1
, 1)) != NULL
&&
1085 !hashcmp(cb
.nsha1
, commit
->object
.sha1
)))) {
1087 if (!prefixcmp(ref
, "refs/tags/"))
1088 ofs
= strlen("refs/tags/");
1089 else if (!prefixcmp(ref
, "refs/remotes/"))
1090 ofs
= strlen("refs/remotes/");
1093 state
->detached_from
= xstrdup(ref
+ ofs
);
1095 state
->detached_from
=
1096 xstrdup(find_unique_abbrev(cb
.nsha1
, DEFAULT_ABBREV
));
1097 hashcpy(state
->detached_sha1
, cb
.nsha1
);
1100 strbuf_release(&cb
.buf
);
1103 void wt_status_get_state(struct wt_status_state
*state
,
1104 int get_detached_from
)
1107 unsigned char sha1
[20];
1109 if (!stat(git_path("MERGE_HEAD"), &st
)) {
1110 state
->merge_in_progress
= 1;
1111 } else if (!stat(git_path("rebase-apply"), &st
)) {
1112 if (!stat(git_path("rebase-apply/applying"), &st
)) {
1113 state
->am_in_progress
= 1;
1114 if (!stat(git_path("rebase-apply/patch"), &st
) && !st
.st_size
)
1115 state
->am_empty_patch
= 1;
1117 state
->rebase_in_progress
= 1;
1118 state
->branch
= read_and_strip_branch("rebase-apply/head-name");
1119 state
->onto
= read_and_strip_branch("rebase-apply/onto");
1121 } else if (!stat(git_path("rebase-merge"), &st
)) {
1122 if (!stat(git_path("rebase-merge/interactive"), &st
))
1123 state
->rebase_interactive_in_progress
= 1;
1125 state
->rebase_in_progress
= 1;
1126 state
->branch
= read_and_strip_branch("rebase-merge/head-name");
1127 state
->onto
= read_and_strip_branch("rebase-merge/onto");
1128 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st
)) {
1129 state
->cherry_pick_in_progress
= 1;
1131 if (!stat(git_path("BISECT_LOG"), &st
)) {
1132 state
->bisect_in_progress
= 1;
1133 state
->branch
= read_and_strip_branch("BISECT_START");
1135 if (!stat(git_path("REVERT_HEAD"), &st
) &&
1136 !get_sha1("REVERT_HEAD", sha1
)) {
1137 state
->revert_in_progress
= 1;
1138 hashcpy(state
->revert_head_sha1
, sha1
);
1141 if (get_detached_from
)
1142 wt_status_get_detached_from(state
);
1145 static void wt_status_print_state(struct wt_status
*s
,
1146 struct wt_status_state
*state
)
1148 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1149 if (state
->merge_in_progress
)
1150 show_merge_in_progress(s
, state
, state_color
);
1151 else if (state
->am_in_progress
)
1152 show_am_in_progress(s
, state
, state_color
);
1153 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1154 show_rebase_in_progress(s
, state
, state_color
);
1155 else if (state
->cherry_pick_in_progress
)
1156 show_cherry_pick_in_progress(s
, state
, state_color
);
1157 else if (state
->revert_in_progress
)
1158 show_revert_in_progress(s
, state
, state_color
);
1159 if (state
->bisect_in_progress
)
1160 show_bisect_in_progress(s
, state
, state_color
);
1163 void wt_status_print(struct wt_status
*s
)
1165 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1166 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1167 struct wt_status_state state
;
1169 memset(&state
, 0, sizeof(state
));
1170 wt_status_get_state(&state
,
1171 s
->branch
&& !strcmp(s
->branch
, "HEAD"));
1174 const char *on_what
= _("On branch ");
1175 const char *branch_name
= s
->branch
;
1176 if (!prefixcmp(branch_name
, "refs/heads/"))
1178 else if (!strcmp(branch_name
, "HEAD")) {
1179 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1180 if (state
.rebase_in_progress
|| state
.rebase_interactive_in_progress
) {
1181 on_what
= _("rebase in progress; onto ");
1182 branch_name
= state
.onto
;
1183 } else if (state
.detached_from
) {
1184 unsigned char sha1
[20];
1185 branch_name
= state
.detached_from
;
1186 if (!get_sha1("HEAD", sha1
) &&
1187 !hashcmp(sha1
, state
.detached_sha1
))
1188 on_what
= _("HEAD detached at ");
1190 on_what
= _("HEAD detached from ");
1193 on_what
= _("Not currently on any branch.");
1196 status_printf(s
, color(WT_STATUS_HEADER
, s
), "");
1197 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1198 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1200 wt_status_print_tracking(s
);
1203 wt_status_print_state(s
, &state
);
1206 free(state
.detached_from
);
1208 if (s
->is_initial
) {
1209 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1210 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), _("Initial commit"));
1211 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1214 wt_status_print_updated(s
);
1215 wt_status_print_unmerged(s
);
1216 wt_status_print_changed(s
);
1217 if (s
->submodule_summary
&&
1218 (!s
->ignore_submodule_arg
||
1219 strcmp(s
->ignore_submodule_arg
, "all"))) {
1220 wt_status_print_submodule_summary(s
, 0); /* staged */
1221 wt_status_print_submodule_summary(s
, 1); /* unstaged */
1223 if (s
->show_untracked_files
) {
1224 wt_status_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1225 if (s
->show_ignored_files
)
1226 wt_status_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1227 if (advice_status_u_option
&& 2000 < s
->untracked_in_ms
) {
1228 status_printf_ln(s
, GIT_COLOR_NORMAL
, "");
1229 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1230 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1231 "may speed it up, but you have to be careful not to forget to add\n"
1232 "new files yourself (see 'git help status')."),
1233 s
->untracked_in_ms
/ 1000.0);
1235 } else if (s
->commitable
)
1236 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1238 ? _(" (use -u option to show untracked files)") : "");
1241 wt_status_print_verbose(s
);
1242 if (!s
->commitable
) {
1244 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1247 else if (s
->workdir_dirty
) {
1248 if (advice_status_hints
)
1249 printf(_("no changes added to commit "
1250 "(use \"git add\" and/or \"git commit -a\")\n"));
1252 printf(_("no changes added to commit\n"));
1253 } else if (s
->untracked
.nr
) {
1254 if (advice_status_hints
)
1255 printf(_("nothing added to commit but untracked files "
1256 "present (use \"git add\" to track)\n"));
1258 printf(_("nothing added to commit but untracked files present\n"));
1259 } else if (s
->is_initial
) {
1260 if (advice_status_hints
)
1261 printf(_("nothing to commit (create/copy files "
1262 "and use \"git add\" to track)\n"));
1264 printf(_("nothing to commit\n"));
1265 } else if (!s
->show_untracked_files
) {
1266 if (advice_status_hints
)
1267 printf(_("nothing to commit (use -u to show untracked files)\n"));
1269 printf(_("nothing to commit\n"));
1271 printf(_("nothing to commit, working directory clean\n"));
1275 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1276 struct wt_status
*s
)
1278 struct wt_status_change_data
*d
= it
->util
;
1279 const char *how
= "??";
1281 switch (d
->stagemask
) {
1282 case 1: how
= "DD"; break; /* both deleted */
1283 case 2: how
= "AU"; break; /* added by us */
1284 case 3: how
= "UD"; break; /* deleted by them */
1285 case 4: how
= "UA"; break; /* added by them */
1286 case 5: how
= "DU"; break; /* deleted by us */
1287 case 6: how
= "AA"; break; /* both added */
1288 case 7: how
= "UU"; break; /* both modified */
1290 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1291 if (s
->null_termination
) {
1292 fprintf(stdout
, " %s%c", it
->string
, 0);
1294 struct strbuf onebuf
= STRBUF_INIT
;
1296 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1297 printf(" %s\n", one
);
1298 strbuf_release(&onebuf
);
1302 static void wt_shortstatus_status(struct string_list_item
*it
,
1303 struct wt_status
*s
)
1305 struct wt_status_change_data
*d
= it
->util
;
1307 if (d
->index_status
)
1308 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1311 if (d
->worktree_status
)
1312 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1316 if (s
->null_termination
) {
1317 fprintf(stdout
, "%s%c", it
->string
, 0);
1319 fprintf(stdout
, "%s%c", d
->head_path
, 0);
1321 struct strbuf onebuf
= STRBUF_INIT
;
1324 one
= quote_path(d
->head_path
, -1, &onebuf
, s
->prefix
);
1325 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1327 strbuf_addch(&onebuf
, '"');
1330 printf("%s -> ", one
);
1331 strbuf_release(&onebuf
);
1333 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1334 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1336 strbuf_addch(&onebuf
, '"');
1339 printf("%s\n", one
);
1340 strbuf_release(&onebuf
);
1344 static void wt_shortstatus_other(struct string_list_item
*it
,
1345 struct wt_status
*s
, const char *sign
)
1347 if (s
->null_termination
) {
1348 fprintf(stdout
, "%s %s%c", sign
, it
->string
, 0);
1350 struct strbuf onebuf
= STRBUF_INIT
;
1352 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1353 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
1354 printf(" %s\n", one
);
1355 strbuf_release(&onebuf
);
1359 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
1361 struct branch
*branch
;
1362 const char *header_color
= color(WT_STATUS_HEADER
, s
);
1363 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
1364 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
1367 const char *branch_name
;
1368 int num_ours
, num_theirs
;
1370 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
1374 branch_name
= s
->branch
;
1376 if (!prefixcmp(branch_name
, "refs/heads/"))
1378 else if (!strcmp(branch_name
, "HEAD")) {
1379 branch_name
= _("HEAD (no branch)");
1380 branch_color_local
= color(WT_STATUS_NOBRANCH
, s
);
1383 branch
= branch_get(s
->branch
+ 11);
1385 color_fprintf(s
->fp
, header_color
, _("Initial commit on "));
1386 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
)) {
1387 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1388 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1392 base
= branch
->merge
[0]->dst
;
1393 base
= shorten_unambiguous_ref(base
, 0);
1394 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1395 color_fprintf(s
->fp
, header_color
, "...");
1396 color_fprintf(s
->fp
, branch_color_remote
, "%s", base
);
1398 color_fprintf(s
->fp
, header_color
, " [");
1400 color_fprintf(s
->fp
, header_color
, _("behind "));
1401 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1402 } else if (!num_theirs
) {
1403 color_fprintf(s
->fp
, header_color
, _("ahead "));
1404 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1406 color_fprintf(s
->fp
, header_color
, _("ahead "));
1407 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1408 color_fprintf(s
->fp
, header_color
, _(", behind "));
1409 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1412 color_fprintf(s
->fp
, header_color
, "]");
1413 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1416 void wt_shortstatus_print(struct wt_status
*s
)
1421 wt_shortstatus_print_tracking(s
);
1423 for (i
= 0; i
< s
->change
.nr
; i
++) {
1424 struct wt_status_change_data
*d
;
1425 struct string_list_item
*it
;
1427 it
= &(s
->change
.items
[i
]);
1430 wt_shortstatus_unmerged(it
, s
);
1432 wt_shortstatus_status(it
, s
);
1434 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
1435 struct string_list_item
*it
;
1437 it
= &(s
->untracked
.items
[i
]);
1438 wt_shortstatus_other(it
, s
, "??");
1440 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
1441 struct string_list_item
*it
;
1443 it
= &(s
->ignored
.items
[i
]);
1444 wt_shortstatus_other(it
, s
, "!!");
1448 void wt_porcelain_print(struct wt_status
*s
)
1451 s
->relative_paths
= 0;
1453 wt_shortstatus_print(s
);