1 #include "git-compat-util.h"
8 #include "environment.h"
12 #include "object-name.h"
17 #include "run-command.h"
21 #include "submodule.h"
23 #include "read-cache.h"
32 #include "sequencer.h"
33 #include "fsmonitor-settings.h"
35 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
36 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
38 static const char cut_line
[] =
39 "------------------------ >8 ------------------------\n";
41 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
42 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
43 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
44 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
45 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
46 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
47 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
48 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
49 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
50 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
53 static const char *color(int slot
, struct wt_status
*s
)
56 if (want_color(s
->use_color
))
57 c
= s
->color_palette
[slot
];
58 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
59 c
= s
->color_palette
[WT_STATUS_HEADER
];
63 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
64 const char *fmt
, va_list ap
, const char *trail
)
66 struct strbuf sb
= STRBUF_INIT
;
67 struct strbuf linebuf
= STRBUF_INIT
;
68 const char *line
, *eol
;
70 strbuf_vaddf(&sb
, fmt
, ap
);
72 if (s
->display_comment_prefix
) {
73 strbuf_addstr(&sb
, comment_line_str
);
75 strbuf_addch(&sb
, ' ');
77 color_print_strbuf(s
->fp
, color
, &sb
);
79 fprintf(s
->fp
, "%s", trail
);
83 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
84 eol
= strchr(line
, '\n');
86 strbuf_reset(&linebuf
);
87 if (at_bol
&& s
->display_comment_prefix
) {
88 strbuf_addstr(&linebuf
, comment_line_str
);
89 if (*line
!= '\n' && *line
!= '\t')
90 strbuf_addch(&linebuf
, ' ');
93 strbuf_add(&linebuf
, line
, eol
- line
);
95 strbuf_addstr(&linebuf
, line
);
96 color_print_strbuf(s
->fp
, color
, &linebuf
);
104 fprintf(s
->fp
, "%s", trail
);
105 strbuf_release(&linebuf
);
109 void status_printf_ln(struct wt_status
*s
, const char *color
,
110 const char *fmt
, ...)
115 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
119 void status_printf(struct wt_status
*s
, const char *color
,
120 const char *fmt
, ...)
125 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
129 static void status_printf_more(struct wt_status
*s
, const char *color
,
130 const char *fmt
, ...)
135 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
139 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
141 memset(s
, 0, sizeof(*s
));
143 memcpy(s
->color_palette
, default_wt_status_colors
,
144 sizeof(default_wt_status_colors
));
145 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
147 s
->relative_paths
= 1;
148 s
->branch
= refs_resolve_refdup(get_main_ref_store(the_repository
),
149 "HEAD", 0, NULL
, NULL
);
150 s
->reference
= "HEAD";
152 s
->index_file
= get_index_file();
153 s
->change
.strdup_strings
= 1;
154 s
->untracked
.strdup_strings
= 1;
155 s
->ignored
.strdup_strings
= 1;
156 s
->show_branch
= -1; /* unspecified */
158 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
159 s
->display_comment_prefix
= 0;
160 s
->detect_rename
= -1;
161 s
->rename_score
= -1;
162 s
->rename_limit
= -1;
165 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
168 int del_mod_conflict
= 0;
169 int both_deleted
= 0;
171 const char *c
= color(WT_STATUS_HEADER
, s
);
173 status_printf_ln(s
, c
, _("Unmerged paths:"));
175 for (i
= 0; i
< s
->change
.nr
; i
++) {
176 struct string_list_item
*it
= &(s
->change
.items
[i
]);
177 struct wt_status_change_data
*d
= it
->util
;
179 switch (d
->stagemask
) {
187 del_mod_conflict
= 1;
197 if (s
->whence
!= FROM_COMMIT
)
199 else if (!s
->is_initial
) {
200 if (!strcmp(s
->reference
, "HEAD"))
201 status_printf_ln(s
, c
,
202 _(" (use \"git restore --staged <file>...\" to unstage)"));
204 status_printf_ln(s
, c
,
205 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
208 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
211 if (!del_mod_conflict
)
212 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
214 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
215 } else if (!del_mod_conflict
&& !not_deleted
) {
216 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
218 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
222 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
224 const char *c
= color(WT_STATUS_HEADER
, s
);
226 status_printf_ln(s
, c
, _("Changes to be committed:"));
229 if (s
->whence
!= FROM_COMMIT
)
230 ; /* NEEDSWORK: use "git reset --unresolve"??? */
231 else if (!s
->is_initial
) {
232 if (!strcmp(s
->reference
, "HEAD"))
233 status_printf_ln(s
, c
234 , _(" (use \"git restore --staged <file>...\" to unstage)"));
236 status_printf_ln(s
, c
,
237 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
240 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
243 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
245 int has_dirty_submodules
)
247 const char *c
= color(WT_STATUS_HEADER
, s
);
249 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
253 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
255 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
256 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
257 if (has_dirty_submodules
)
258 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
261 static void wt_longstatus_print_other_header(struct wt_status
*s
,
265 const char *c
= color(WT_STATUS_HEADER
, s
);
266 status_printf_ln(s
, c
, "%s:", what
);
269 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
272 static void wt_longstatus_print_trailer(struct wt_status
*s
)
274 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
277 static const char *wt_status_unmerged_status_string(int stagemask
)
281 return _("both deleted:");
283 return _("added by us:");
285 return _("deleted by them:");
287 return _("added by them:");
289 return _("deleted by us:");
291 return _("both added:");
293 return _("both modified:");
295 BUG("unhandled unmerged status %x", stagemask
);
299 static const char *wt_status_diff_status_string(int status
)
302 case DIFF_STATUS_ADDED
:
303 return _("new file:");
304 case DIFF_STATUS_COPIED
:
306 case DIFF_STATUS_DELETED
:
307 return _("deleted:");
308 case DIFF_STATUS_MODIFIED
:
309 return _("modified:");
310 case DIFF_STATUS_RENAMED
:
311 return _("renamed:");
312 case DIFF_STATUS_TYPE_CHANGED
:
313 return _("typechange:");
314 case DIFF_STATUS_UNKNOWN
:
315 return _("unknown:");
316 case DIFF_STATUS_UNMERGED
:
317 return _("unmerged:");
323 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
327 for (i
= minval
; i
<= maxval
; i
++) {
328 const char *s
= label(i
);
329 int len
= s
? utf8_strwidth(s
) : 0;
336 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
337 struct string_list_item
*it
)
339 const char *c
= color(WT_STATUS_UNMERGED
, s
);
340 struct wt_status_change_data
*d
= it
->util
;
341 struct strbuf onebuf
= STRBUF_INIT
;
342 static char *padding
;
343 static int label_width
;
344 const char *one
, *how
;
348 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
349 label_width
+= strlen(" ");
350 padding
= xmallocz(label_width
);
351 memset(padding
, ' ', label_width
);
354 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
355 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
357 how
= wt_status_unmerged_status_string(d
->stagemask
);
358 len
= label_width
- utf8_strwidth(how
);
359 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
360 strbuf_release(&onebuf
);
363 static void wt_longstatus_print_change_data(struct wt_status
*s
,
365 struct string_list_item
*it
)
367 struct wt_status_change_data
*d
= it
->util
;
368 const char *c
= color(change_type
, s
);
372 const char *one
, *two
;
373 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
374 struct strbuf extra
= STRBUF_INIT
;
375 static char *padding
;
376 static int label_width
;
381 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
382 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
383 label_width
+= strlen(" ");
384 padding
= xmallocz(label_width
);
385 memset(padding
, ' ', label_width
);
388 one_name
= two_name
= it
->string
;
389 switch (change_type
) {
390 case WT_STATUS_UPDATED
:
391 status
= d
->index_status
;
393 case WT_STATUS_CHANGED
:
394 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
395 strbuf_addstr(&extra
, " (");
396 if (d
->new_submodule_commits
)
397 strbuf_addstr(&extra
, _("new commits, "));
398 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
399 strbuf_addstr(&extra
, _("modified content, "));
400 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
401 strbuf_addstr(&extra
, _("untracked content, "));
402 strbuf_setlen(&extra
, extra
.len
- 2);
403 strbuf_addch(&extra
, ')');
405 status
= d
->worktree_status
;
408 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
413 * Only pick up the rename it's relevant. If the rename is for
414 * the changed section and we're printing the updated section,
417 if (d
->rename_status
== status
)
418 one_name
= d
->rename_source
;
420 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
421 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
423 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
424 what
= wt_status_diff_status_string(status
);
426 BUG("unhandled diff status %c", status
);
427 len
= label_width
- utf8_strwidth(what
);
429 if (one_name
!= two_name
)
430 status_printf_more(s
, c
, "%s%.*s%s -> %s",
431 what
, len
, padding
, one
, two
);
433 status_printf_more(s
, c
, "%s%.*s%s",
434 what
, len
, padding
, one
);
436 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
437 strbuf_release(&extra
);
439 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
440 strbuf_release(&onebuf
);
441 strbuf_release(&twobuf
);
444 static char short_submodule_status(struct wt_status_change_data
*d
)
446 if (d
->new_submodule_commits
)
448 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
450 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
452 return d
->worktree_status
;
455 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
456 struct diff_options
*options UNUSED
,
459 struct wt_status
*s
= data
;
464 s
->workdir_dirty
= 1;
465 for (i
= 0; i
< q
->nr
; i
++) {
466 struct diff_filepair
*p
;
467 struct string_list_item
*it
;
468 struct wt_status_change_data
*d
;
471 it
= string_list_insert(&s
->change
, p
->two
->path
);
477 if (!d
->worktree_status
)
478 d
->worktree_status
= p
->status
;
479 if (S_ISGITLINK(p
->two
->mode
)) {
480 d
->dirty_submodule
= p
->two
->dirty_submodule
;
481 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
483 if (s
->status_format
== STATUS_FORMAT_SHORT
)
484 d
->worktree_status
= short_submodule_status(d
);
488 case DIFF_STATUS_ADDED
:
489 d
->mode_worktree
= p
->two
->mode
;
492 case DIFF_STATUS_DELETED
:
493 d
->mode_index
= p
->one
->mode
;
494 oidcpy(&d
->oid_index
, &p
->one
->oid
);
495 /* mode_worktree is zero for a delete. */
498 case DIFF_STATUS_COPIED
:
499 case DIFF_STATUS_RENAMED
:
500 if (d
->rename_status
)
501 BUG("multiple renames on the same target? how?");
502 d
->rename_source
= xstrdup(p
->one
->path
);
503 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
504 d
->rename_status
= p
->status
;
506 case DIFF_STATUS_MODIFIED
:
507 case DIFF_STATUS_TYPE_CHANGED
:
508 case DIFF_STATUS_UNMERGED
:
509 d
->mode_index
= p
->one
->mode
;
510 d
->mode_worktree
= p
->two
->mode
;
511 oidcpy(&d
->oid_index
, &p
->one
->oid
);
515 BUG("unhandled diff-files status '%c'", p
->status
);
522 static int unmerged_mask(struct index_state
*istate
, const char *path
)
525 const struct cache_entry
*ce
;
527 pos
= index_name_pos(istate
, path
, strlen(path
));
533 while (pos
< istate
->cache_nr
) {
534 ce
= istate
->cache
[pos
++];
535 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
537 mask
|= (1 << (ce_stage(ce
) - 1));
542 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
543 struct diff_options
*options UNUSED
,
546 struct wt_status
*s
= data
;
549 for (i
= 0; i
< q
->nr
; i
++) {
550 struct diff_filepair
*p
;
551 struct string_list_item
*it
;
552 struct wt_status_change_data
*d
;
555 it
= string_list_insert(&s
->change
, p
->two
->path
);
561 if (!d
->index_status
)
562 d
->index_status
= p
->status
;
564 case DIFF_STATUS_ADDED
:
565 /* Leave {mode,oid}_head zero for an add. */
566 d
->mode_index
= p
->two
->mode
;
567 oidcpy(&d
->oid_index
, &p
->two
->oid
);
570 case DIFF_STATUS_DELETED
:
571 d
->mode_head
= p
->one
->mode
;
572 oidcpy(&d
->oid_head
, &p
->one
->oid
);
574 /* Leave {mode,oid}_index zero for a delete. */
577 case DIFF_STATUS_COPIED
:
578 case DIFF_STATUS_RENAMED
:
579 if (d
->rename_status
)
580 BUG("multiple renames on the same target? how?");
581 d
->rename_source
= xstrdup(p
->one
->path
);
582 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
583 d
->rename_status
= p
->status
;
585 case DIFF_STATUS_MODIFIED
:
586 case DIFF_STATUS_TYPE_CHANGED
:
587 d
->mode_head
= p
->one
->mode
;
588 d
->mode_index
= p
->two
->mode
;
589 oidcpy(&d
->oid_head
, &p
->one
->oid
);
590 oidcpy(&d
->oid_index
, &p
->two
->oid
);
593 case DIFF_STATUS_UNMERGED
:
594 d
->stagemask
= unmerged_mask(s
->repo
->index
,
597 * Don't bother setting {mode,oid}_{head,index} since the print
598 * code will output the stage values directly and not use the
599 * values in these fields.
604 BUG("unhandled diff-index status '%c'", p
->status
);
610 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
614 repo_init_revisions(s
->repo
, &rev
, NULL
);
615 setup_revisions(0, NULL
, &rev
, NULL
);
616 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
617 rev
.diffopt
.flags
.dirty_submodules
= 1;
618 rev
.diffopt
.ita_invisible_in_index
= 1;
619 if (!s
->show_untracked_files
)
620 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
621 if (s
->ignore_submodule_arg
) {
622 rev
.diffopt
.flags
.override_submodule_config
= 1;
623 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
624 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
625 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
626 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
627 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
628 rev
.diffopt
.format_callback_data
= s
;
629 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
630 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
631 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
632 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
633 run_diff_files(&rev
, 0);
634 release_revisions(&rev
);
637 static void wt_status_collect_changes_index(struct wt_status
*s
)
640 struct setup_revision_opt opt
;
642 repo_init_revisions(s
->repo
, &rev
, NULL
);
643 memset(&opt
, 0, sizeof(opt
));
644 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
645 setup_revisions(0, NULL
, &rev
, &opt
);
647 rev
.diffopt
.flags
.override_submodule_config
= 1;
648 rev
.diffopt
.ita_invisible_in_index
= 1;
649 if (s
->ignore_submodule_arg
) {
650 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
653 * Unless the user did explicitly request a submodule ignore
654 * mode by passing a command line option we do not ignore any
655 * changed submodule SHA-1s when comparing index and HEAD, no
656 * matter what is configured. Otherwise the user won't be
657 * shown any submodules manually added (and which are
658 * staged to be committed), which would be really confusing.
660 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
663 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
664 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
665 rev
.diffopt
.format_callback_data
= s
;
666 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
667 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
668 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
671 * The `recursive` option must be enabled to allow the diff to recurse
672 * into subdirectories of sparse directory index entries. If it is not
673 * enabled, a subdirectory containing file(s) with changes is reported
674 * as "modified", rather than the modified files themselves.
676 rev
.diffopt
.flags
.recursive
= 1;
678 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
679 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
680 release_revisions(&rev
);
683 static int add_file_to_list(const struct object_id
*oid
,
684 struct strbuf
*base
, const char *path
,
685 unsigned int mode
, void *context
)
687 struct string_list_item
*it
;
688 struct wt_status_change_data
*d
;
689 struct wt_status
*s
= context
;
690 struct strbuf full_name
= STRBUF_INIT
;
693 return READ_TREE_RECURSIVE
;
695 strbuf_add(&full_name
, base
->buf
, base
->len
);
696 strbuf_addstr(&full_name
, path
);
697 it
= string_list_insert(&s
->change
, full_name
.buf
);
704 d
->index_status
= DIFF_STATUS_ADDED
;
705 /* Leave {mode,oid}_head zero for adds. */
706 d
->mode_index
= mode
;
707 oidcpy(&d
->oid_index
, oid
);
709 strbuf_release(&full_name
);
713 static void wt_status_collect_changes_initial(struct wt_status
*s
)
715 struct index_state
*istate
= s
->repo
->index
;
718 for (i
= 0; i
< istate
->cache_nr
; i
++) {
719 struct string_list_item
*it
;
720 struct wt_status_change_data
*d
;
721 const struct cache_entry
*ce
= istate
->cache
[i
];
723 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
725 if (ce_intent_to_add(ce
))
727 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
729 * This is a sparse directory entry, so we want to collect all
730 * of the added files within the tree. This requires recursively
731 * expanding the trees to find the elements that are new in this
732 * tree and marking them with DIFF_STATUS_ADDED.
734 struct strbuf base
= STRBUF_INIT
;
735 struct pathspec ps
= { 0 };
736 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
742 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
743 read_tree_at(istate
->repo
, tree
, &base
, 0, &ps
,
744 add_file_to_list
, s
);
748 it
= string_list_insert(&s
->change
, ce
->name
);
755 d
->index_status
= DIFF_STATUS_UNMERGED
;
756 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
758 * Don't bother setting {mode,oid}_{head,index} since the print
759 * code will output the stage values directly and not use the
760 * values in these fields.
764 d
->index_status
= DIFF_STATUS_ADDED
;
765 /* Leave {mode,oid}_head zero for adds. */
766 d
->mode_index
= ce
->ce_mode
;
767 oidcpy(&d
->oid_index
, &ce
->oid
);
773 static void wt_status_collect_untracked(struct wt_status
*s
)
776 struct dir_struct dir
= DIR_INIT
;
777 uint64_t t_begin
= getnanotime();
778 struct index_state
*istate
= s
->repo
->index
;
780 if (!s
->show_untracked_files
)
783 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
785 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
786 if (s
->show_ignored_mode
) {
787 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
789 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
790 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
792 dir
.untracked
= istate
->untracked
;
795 setup_standard_excludes(&dir
);
797 fill_directory(&dir
, istate
, &s
->pathspec
);
799 for (i
= 0; i
< dir
.nr
; i
++) {
800 struct dir_entry
*ent
= dir
.entries
[i
];
801 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
802 string_list_insert(&s
->untracked
, ent
->name
);
805 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
806 struct dir_entry
*ent
= dir
.ignored
[i
];
807 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
808 string_list_insert(&s
->ignored
, ent
->name
);
813 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
814 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
817 static int has_unmerged(struct wt_status
*s
)
821 for (i
= 0; i
< s
->change
.nr
; i
++) {
822 struct wt_status_change_data
*d
;
823 d
= s
->change
.items
[i
].util
;
830 void wt_status_collect(struct wt_status
*s
)
832 trace2_region_enter("status", "worktrees", s
->repo
);
833 wt_status_collect_changes_worktree(s
);
834 trace2_region_leave("status", "worktrees", s
->repo
);
837 trace2_region_enter("status", "initial", s
->repo
);
838 wt_status_collect_changes_initial(s
);
839 trace2_region_leave("status", "initial", s
->repo
);
841 trace2_region_enter("status", "index", s
->repo
);
842 wt_status_collect_changes_index(s
);
843 trace2_region_leave("status", "index", s
->repo
);
846 trace2_region_enter("status", "untracked", s
->repo
);
847 wt_status_collect_untracked(s
);
848 trace2_region_leave("status", "untracked", s
->repo
);
850 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
851 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
855 void wt_status_collect_free_buffers(struct wt_status
*s
)
857 wt_status_state_free_buffers(&s
->state
);
860 void wt_status_state_free_buffers(struct wt_status_state
*state
)
862 FREE_AND_NULL(state
->branch
);
863 FREE_AND_NULL(state
->onto
);
864 FREE_AND_NULL(state
->detached_from
);
865 FREE_AND_NULL(state
->bisecting_from
);
868 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
870 int shown_header
= 0;
873 for (i
= 0; i
< s
->change
.nr
; i
++) {
874 struct wt_status_change_data
*d
;
875 struct string_list_item
*it
;
876 it
= &(s
->change
.items
[i
]);
881 wt_longstatus_print_unmerged_header(s
);
884 wt_longstatus_print_unmerged_data(s
, it
);
887 wt_longstatus_print_trailer(s
);
891 static void wt_longstatus_print_updated(struct wt_status
*s
)
893 int shown_header
= 0;
896 for (i
= 0; i
< s
->change
.nr
; i
++) {
897 struct wt_status_change_data
*d
;
898 struct string_list_item
*it
;
899 it
= &(s
->change
.items
[i
]);
901 if (!d
->index_status
||
902 d
->index_status
== DIFF_STATUS_UNMERGED
)
905 wt_longstatus_print_cached_header(s
);
908 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
911 wt_longstatus_print_trailer(s
);
917 * 1 : some change but no delete
919 static int wt_status_check_worktree_changes(struct wt_status
*s
,
920 int *dirty_submodules
)
925 *dirty_submodules
= 0;
927 for (i
= 0; i
< s
->change
.nr
; i
++) {
928 struct wt_status_change_data
*d
;
929 d
= s
->change
.items
[i
].util
;
930 if (!d
->worktree_status
||
931 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
935 if (d
->dirty_submodule
)
936 *dirty_submodules
= 1;
937 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
943 static void wt_longstatus_print_changed(struct wt_status
*s
)
945 int i
, dirty_submodules
;
946 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
948 if (!worktree_changes
)
951 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
953 for (i
= 0; i
< s
->change
.nr
; i
++) {
954 struct wt_status_change_data
*d
;
955 struct string_list_item
*it
;
956 it
= &(s
->change
.items
[i
]);
958 if (!d
->worktree_status
||
959 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
961 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
963 wt_longstatus_print_trailer(s
);
966 static int stash_count_refs(struct object_id
*ooid UNUSED
,
967 struct object_id
*noid UNUSED
,
968 const char *email UNUSED
,
969 timestamp_t timestamp UNUSED
, int tz UNUSED
,
970 const char *message UNUSED
, void *cb_data
)
977 static int count_stash_entries(void)
980 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
981 "refs/stash", stash_count_refs
, &n
);
985 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
987 int stash_count
= count_stash_entries();
990 status_printf_ln(s
, GIT_COLOR_NORMAL
,
991 Q_("Your stash currently has %d entry",
992 "Your stash currently has %d entries", stash_count
),
996 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
998 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
999 struct strbuf cmd_stdout
= STRBUF_INIT
;
1000 struct strbuf summary
= STRBUF_INIT
;
1001 char *summary_content
;
1003 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
1005 strvec_push(&sm_summary
.args
, "submodule");
1006 strvec_push(&sm_summary
.args
, "summary");
1007 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
1008 strvec_push(&sm_summary
.args
, "--for-status");
1009 strvec_push(&sm_summary
.args
, "--summary-limit");
1010 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
1012 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1014 sm_summary
.git_cmd
= 1;
1015 sm_summary
.no_stdin
= 1;
1017 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1019 /* prepend header, only if there's an actual output */
1020 if (cmd_stdout
.len
) {
1022 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1024 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1025 strbuf_addstr(&summary
, "\n\n");
1027 strbuf_addbuf(&summary
, &cmd_stdout
);
1028 strbuf_release(&cmd_stdout
);
1030 if (s
->display_comment_prefix
) {
1032 summary_content
= strbuf_detach(&summary
, &len
);
1033 strbuf_add_commented_lines(&summary
, summary_content
, len
, comment_line_str
);
1034 free(summary_content
);
1037 fputs(summary
.buf
, s
->fp
);
1038 strbuf_release(&summary
);
1041 static void wt_longstatus_print_other(struct wt_status
*s
,
1042 struct string_list
*l
,
1047 struct strbuf buf
= STRBUF_INIT
;
1048 static struct string_list output
= STRING_LIST_INIT_DUP
;
1049 struct column_options copts
;
1054 wt_longstatus_print_other_header(s
, what
, how
);
1056 for (i
= 0; i
< l
->nr
; i
++) {
1057 struct string_list_item
*it
;
1059 it
= &(l
->items
[i
]);
1060 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1061 if (column_active(s
->colopts
)) {
1062 string_list_append(&output
, path
);
1065 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1066 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1070 strbuf_release(&buf
);
1071 if (!column_active(s
->colopts
))
1074 strbuf_addf(&buf
, "%s%s\t%s",
1075 color(WT_STATUS_HEADER
, s
),
1076 s
->display_comment_prefix
? "#" : "",
1077 color(WT_STATUS_UNTRACKED
, s
));
1078 memset(&copts
, 0, sizeof(copts
));
1080 copts
.indent
= buf
.buf
;
1081 if (want_color(s
->use_color
))
1082 copts
.nl
= GIT_COLOR_RESET
"\n";
1083 print_columns(&output
, s
->colopts
, &copts
);
1084 string_list_clear(&output
, 0);
1085 strbuf_release(&buf
);
1087 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1090 size_t wt_status_locate_end(const char *s
, size_t len
)
1093 struct strbuf pattern
= STRBUF_INIT
;
1095 strbuf_addf(&pattern
, "\n%s %s", comment_line_str
, cut_line
);
1096 if (starts_with(s
, pattern
.buf
+ 1))
1098 else if ((p
= strstr(s
, pattern
.buf
))) {
1099 size_t newlen
= p
- s
+ 1;
1103 strbuf_release(&pattern
);
1107 void wt_status_append_cut_line(struct strbuf
*buf
)
1109 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1111 strbuf_commented_addf(buf
, comment_line_str
, "%s", cut_line
);
1112 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
), comment_line_str
);
1115 void wt_status_add_cut_line(struct wt_status
*s
)
1117 struct strbuf buf
= STRBUF_INIT
;
1119 if (s
->added_cut_line
)
1121 s
->added_cut_line
= 1;
1122 wt_status_append_cut_line(&buf
);
1123 fputs(buf
.buf
, s
->fp
);
1124 strbuf_release(&buf
);
1127 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1129 struct rev_info rev
;
1130 struct setup_revision_opt opt
;
1131 int dirty_submodules
;
1132 const char *c
= color(WT_STATUS_HEADER
, s
);
1134 repo_init_revisions(s
->repo
, &rev
, NULL
);
1135 rev
.diffopt
.flags
.allow_textconv
= 1;
1136 rev
.diffopt
.ita_invisible_in_index
= 1;
1138 memset(&opt
, 0, sizeof(opt
));
1139 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1140 setup_revisions(0, NULL
, &rev
, &opt
);
1142 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1143 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1144 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1145 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1146 rev
.diffopt
.file
= s
->fp
;
1147 rev
.diffopt
.close_file
= 0;
1149 * If we're not going to stdout, then we definitely don't
1150 * want color, since we are going to the commit message
1151 * file (and even the "auto" setting won't work, since it
1152 * will have checked isatty on stdout). But we then do want
1153 * to insert the scissor line here to reliably remove the
1154 * diff before committing, if we didn't already include one
1157 if (s
->fp
!= stdout
) {
1158 rev
.diffopt
.use_color
= 0;
1159 wt_status_add_cut_line(s
);
1161 if (s
->verbose
> 1 && s
->committable
) {
1162 /* print_updated() printed a header, so do we */
1163 if (s
->fp
!= stdout
)
1164 wt_longstatus_print_trailer(s
);
1165 status_printf_ln(s
, c
, _("Changes to be committed:"));
1166 rev
.diffopt
.a_prefix
= "c/";
1167 rev
.diffopt
.b_prefix
= "i/";
1168 } /* else use prefix as per user config */
1169 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
1170 if (s
->verbose
> 1 &&
1171 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1172 status_printf_ln(s
, c
,
1173 "--------------------------------------------------");
1174 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1176 rev
.diffopt
.a_prefix
= "i/";
1177 rev
.diffopt
.b_prefix
= "w/";
1178 run_diff_files(&rev
, 0);
1180 release_revisions(&rev
);
1183 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1185 struct strbuf sb
= STRBUF_INIT
;
1186 const char *cp
, *ep
, *branch_name
;
1187 struct branch
*branch
;
1188 uint64_t t_begin
= 0;
1190 assert(s
->branch
&& !s
->is_initial
);
1191 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1193 branch
= branch_get(branch_name
);
1195 t_begin
= getnanotime();
1197 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
,
1198 !s
->commit_template
))
1201 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1202 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1203 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1204 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1205 strbuf_addf(&sb
, _("\n"
1206 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1207 "You can use '--no-ahead-behind' to avoid this.\n"),
1208 t_delta_in_ms
/ 1000.0);
1212 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1213 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1215 s
->display_comment_prefix
? comment_line_str
: "",
1216 s
->display_comment_prefix
? " " : "",
1217 (int)(ep
- cp
), cp
);
1218 if (s
->display_comment_prefix
)
1219 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%s",
1223 strbuf_release(&sb
);
1226 static int uf_was_slow(struct wt_status
*s
)
1228 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1229 s
->untracked_in_ms
= 3250;
1230 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1233 static void show_merge_in_progress(struct wt_status
*s
,
1236 if (has_unmerged(s
)) {
1237 status_printf_ln(s
, color
, _("You have unmerged paths."));
1239 status_printf_ln(s
, color
,
1240 _(" (fix conflicts and run \"git commit\")"));
1241 status_printf_ln(s
, color
,
1242 _(" (use \"git merge --abort\" to abort the merge)"));
1245 status_printf_ln(s
, color
,
1246 _("All conflicts fixed but you are still merging."));
1248 status_printf_ln(s
, color
,
1249 _(" (use \"git commit\" to conclude merge)"));
1251 wt_longstatus_print_trailer(s
);
1254 static void show_am_in_progress(struct wt_status
*s
,
1259 status_printf_ln(s
, color
,
1260 _("You are in the middle of an am session."));
1261 if (s
->state
.am_empty_patch
)
1262 status_printf_ln(s
, color
,
1263 _("The current patch is empty."));
1265 am_empty_patch
= s
->state
.am_empty_patch
;
1266 if (!am_empty_patch
)
1267 status_printf_ln(s
, color
,
1268 _(" (fix conflicts and then run \"git am --continue\")"));
1269 status_printf_ln(s
, color
,
1270 _(" (use \"git am --skip\" to skip this patch)"));
1272 status_printf_ln(s
, color
,
1273 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1274 status_printf_ln(s
, color
,
1275 _(" (use \"git am --abort\" to restore the original branch)"));
1277 wt_longstatus_print_trailer(s
);
1280 static char *read_line_from_git_path(const char *filename
)
1282 struct strbuf buf
= STRBUF_INIT
;
1283 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1286 strbuf_release(&buf
);
1289 strbuf_getline_lf(&buf
, fp
);
1291 return strbuf_detach(&buf
, NULL
);
1293 strbuf_release(&buf
);
1298 static int split_commit_in_progress(struct wt_status
*s
)
1300 int split_in_progress
= 0;
1301 struct object_id head_oid
, orig_head_oid
;
1302 char *rebase_amend
, *rebase_orig_head
;
1303 int head_flags
, orig_head_flags
;
1305 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1306 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1309 if (refs_read_ref_full(get_main_ref_store(the_repository
), "HEAD", RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1310 &head_oid
, &head_flags
) ||
1311 refs_read_ref_full(get_main_ref_store(the_repository
), "ORIG_HEAD", RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1312 &orig_head_oid
, &orig_head_flags
))
1314 if (head_flags
& REF_ISSYMREF
|| orig_head_flags
& REF_ISSYMREF
)
1317 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1318 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1320 if (!rebase_amend
|| !rebase_orig_head
)
1321 ; /* fall through, no split in progress */
1322 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1323 split_in_progress
= !!strcmp(oid_to_hex(&head_oid
), rebase_amend
);
1324 else if (strcmp(oid_to_hex(&orig_head_oid
), rebase_orig_head
))
1325 split_in_progress
= 1;
1328 free(rebase_orig_head
);
1330 return split_in_progress
;
1335 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1337 * "pick d6a2f03 some message"
1339 * The function assumes that the line does not contain useless spaces
1340 * before or after the command.
1342 static void abbrev_oid_in_line(struct strbuf
*line
)
1344 struct strbuf
**split
;
1347 if (starts_with(line
->buf
, "exec ") ||
1348 starts_with(line
->buf
, "x ") ||
1349 starts_with(line
->buf
, "label ") ||
1350 starts_with(line
->buf
, "l "))
1353 split
= strbuf_split_max(line
, ' ', 3);
1354 if (split
[0] && split
[1]) {
1355 struct object_id oid
;
1358 * strbuf_split_max left a space. Trim it and re-add
1359 * it after abbreviation.
1361 strbuf_trim(split
[1]);
1362 if (!repo_get_oid(the_repository
, split
[1]->buf
, &oid
)) {
1363 strbuf_reset(split
[1]);
1364 strbuf_add_unique_abbrev(split
[1], &oid
,
1366 strbuf_addch(split
[1], ' ');
1368 for (i
= 0; split
[i
]; i
++)
1369 strbuf_addbuf(line
, split
[i
]);
1372 strbuf_list_free(split
);
1375 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1377 struct strbuf line
= STRBUF_INIT
;
1378 FILE *f
= fopen(git_path("%s", fname
), "r");
1381 if (errno
== ENOENT
)
1383 die_errno("Could not open file %s for reading",
1384 git_path("%s", fname
));
1386 while (!strbuf_getline_lf(&line
, f
)) {
1387 if (starts_with(line
.buf
, comment_line_str
))
1392 abbrev_oid_in_line(&line
);
1393 string_list_append(lines
, line
.buf
);
1396 strbuf_release(&line
);
1400 static void show_rebase_information(struct wt_status
*s
,
1403 if (s
->state
.rebase_interactive_in_progress
) {
1405 int nr_lines_to_show
= 2;
1407 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1408 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1410 read_rebase_todolist("rebase-merge/done", &have_done
);
1411 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1413 status_printf_ln(s
, color
,
1414 _("git-rebase-todo is missing."));
1415 if (have_done
.nr
== 0)
1416 status_printf_ln(s
, color
, _("No commands done."));
1418 status_printf_ln(s
, color
,
1419 Q_("Last command done (%"PRIuMAX
" command done):",
1420 "Last commands done (%"PRIuMAX
" commands done):",
1422 (uintmax_t)have_done
.nr
);
1423 for (i
= (have_done
.nr
> nr_lines_to_show
)
1424 ? have_done
.nr
- nr_lines_to_show
: 0;
1427 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1428 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1429 status_printf_ln(s
, color
,
1430 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1433 if (yet_to_do
.nr
== 0)
1434 status_printf_ln(s
, color
,
1435 _("No commands remaining."));
1437 status_printf_ln(s
, color
,
1438 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1439 "Next commands to do (%"PRIuMAX
" remaining commands):",
1441 (uintmax_t)yet_to_do
.nr
);
1442 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1443 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1445 status_printf_ln(s
, color
,
1446 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1448 string_list_clear(&yet_to_do
, 0);
1449 string_list_clear(&have_done
, 0);
1453 static void print_rebase_state(struct wt_status
*s
,
1456 if (s
->state
.branch
)
1457 status_printf_ln(s
, color
,
1458 _("You are currently rebasing branch '%s' on '%s'."),
1462 status_printf_ln(s
, color
,
1463 _("You are currently rebasing."));
1466 static void show_rebase_in_progress(struct wt_status
*s
,
1471 show_rebase_information(s
, color
);
1472 if (has_unmerged(s
)) {
1473 print_rebase_state(s
, color
);
1475 status_printf_ln(s
, color
,
1476 _(" (fix conflicts and then run \"git rebase --continue\")"));
1477 status_printf_ln(s
, color
,
1478 _(" (use \"git rebase --skip\" to skip this patch)"));
1479 status_printf_ln(s
, color
,
1480 _(" (use \"git rebase --abort\" to check out the original branch)"));
1482 } else if (s
->state
.rebase_in_progress
||
1483 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1484 print_rebase_state(s
, color
);
1486 status_printf_ln(s
, color
,
1487 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1488 } else if (split_commit_in_progress(s
)) {
1489 if (s
->state
.branch
)
1490 status_printf_ln(s
, color
,
1491 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1495 status_printf_ln(s
, color
,
1496 _("You are currently splitting a commit during a rebase."));
1498 status_printf_ln(s
, color
,
1499 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1501 if (s
->state
.branch
)
1502 status_printf_ln(s
, color
,
1503 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1507 status_printf_ln(s
, color
,
1508 _("You are currently editing a commit during a rebase."));
1509 if (s
->hints
&& !s
->amend
) {
1510 status_printf_ln(s
, color
,
1511 _(" (use \"git commit --amend\" to amend the current commit)"));
1512 status_printf_ln(s
, color
,
1513 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1516 wt_longstatus_print_trailer(s
);
1519 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1522 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1523 status_printf_ln(s
, color
,
1524 _("Cherry-pick currently in progress."));
1526 status_printf_ln(s
, color
,
1527 _("You are currently cherry-picking commit %s."),
1528 repo_find_unique_abbrev(the_repository
, &s
->state
.cherry_pick_head_oid
,
1532 if (has_unmerged(s
))
1533 status_printf_ln(s
, color
,
1534 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1535 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1536 status_printf_ln(s
, color
,
1537 _(" (run \"git cherry-pick --continue\" to continue)"));
1539 status_printf_ln(s
, color
,
1540 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1541 status_printf_ln(s
, color
,
1542 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1543 status_printf_ln(s
, color
,
1544 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1546 wt_longstatus_print_trailer(s
);
1549 static void show_revert_in_progress(struct wt_status
*s
,
1552 if (is_null_oid(&s
->state
.revert_head_oid
))
1553 status_printf_ln(s
, color
,
1554 _("Revert currently in progress."));
1556 status_printf_ln(s
, color
,
1557 _("You are currently reverting commit %s."),
1558 repo_find_unique_abbrev(the_repository
, &s
->state
.revert_head_oid
,
1561 if (has_unmerged(s
))
1562 status_printf_ln(s
, color
,
1563 _(" (fix conflicts and run \"git revert --continue\")"));
1564 else if (is_null_oid(&s
->state
.revert_head_oid
))
1565 status_printf_ln(s
, color
,
1566 _(" (run \"git revert --continue\" to continue)"));
1568 status_printf_ln(s
, color
,
1569 _(" (all conflicts fixed: run \"git revert --continue\")"));
1570 status_printf_ln(s
, color
,
1571 _(" (use \"git revert --skip\" to skip this patch)"));
1572 status_printf_ln(s
, color
,
1573 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1575 wt_longstatus_print_trailer(s
);
1578 static void show_bisect_in_progress(struct wt_status
*s
,
1581 if (s
->state
.bisecting_from
)
1582 status_printf_ln(s
, color
,
1583 _("You are currently bisecting, started from branch '%s'."),
1584 s
->state
.bisecting_from
);
1586 status_printf_ln(s
, color
,
1587 _("You are currently bisecting."));
1589 status_printf_ln(s
, color
,
1590 _(" (use \"git bisect reset\" to get back to the original branch)"));
1591 wt_longstatus_print_trailer(s
);
1594 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1597 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1600 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1601 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1603 status_printf_ln(s
, color
,
1604 _("You are in a sparse checkout with %d%% of tracked files present."),
1605 s
->state
.sparse_checkout_percentage
);
1606 wt_longstatus_print_trailer(s
);
1610 * Extract branch information from rebase/bisect
1612 static char *get_branch(const struct worktree
*wt
, const char *path
)
1614 struct strbuf sb
= STRBUF_INIT
;
1615 struct object_id oid
;
1616 const char *branch_name
;
1618 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1621 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1622 strbuf_setlen(&sb
, sb
.len
- 1);
1625 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1626 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1627 else if (starts_with(sb
.buf
, "refs/"))
1629 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1631 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1632 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1636 return strbuf_detach(&sb
, NULL
);
1639 strbuf_release(&sb
);
1643 struct grab_1st_switch_cbdata
{
1645 struct object_id noid
;
1648 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1649 struct object_id
*noid
,
1650 const char *email UNUSED
,
1651 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1652 const char *message
, void *cb_data
)
1654 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1655 const char *target
= NULL
, *end
;
1657 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1659 target
= strstr(message
, " to ");
1662 target
+= strlen(" to ");
1663 strbuf_reset(&cb
->buf
);
1664 oidcpy(&cb
->noid
, noid
);
1665 end
= strchrnul(target
, '\n');
1666 strbuf_add(&cb
->buf
, target
, end
- target
);
1667 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1668 /* HEAD is relative. Resolve it to the right reflog entry. */
1669 strbuf_reset(&cb
->buf
);
1670 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1675 static void wt_status_get_detached_from(struct repository
*r
,
1676 struct wt_status_state
*state
)
1678 struct grab_1st_switch_cbdata cb
;
1679 struct commit
*commit
;
1680 struct object_id oid
;
1683 strbuf_init(&cb
.buf
, 0);
1684 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository
), "HEAD", grab_1st_switch
, &cb
) <= 0) {
1685 strbuf_release(&cb
.buf
);
1689 if (repo_dwim_ref(r
, cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
,
1691 /* oid is a commit? match without further lookup */
1692 (oideq(&cb
.noid
, &oid
) ||
1693 /* perhaps oid is a tag, try to dereference to a commit */
1694 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1695 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1696 const char *from
= ref
;
1697 if (!skip_prefix(from
, "refs/tags/", &from
))
1698 skip_prefix(from
, "refs/remotes/", &from
);
1699 state
->detached_from
= xstrdup(from
);
1701 state
->detached_from
=
1702 xstrdup(repo_find_unique_abbrev(r
, &cb
.noid
, DEFAULT_ABBREV
));
1703 oidcpy(&state
->detached_oid
, &cb
.noid
);
1704 state
->detached_at
= !repo_get_oid(r
, "HEAD", &oid
) &&
1705 oideq(&oid
, &state
->detached_oid
);
1708 strbuf_release(&cb
.buf
);
1711 int wt_status_check_rebase(const struct worktree
*wt
,
1712 struct wt_status_state
*state
)
1716 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1717 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1718 state
->am_in_progress
= 1;
1719 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1720 state
->am_empty_patch
= 1;
1722 state
->rebase_in_progress
= 1;
1723 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1724 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1726 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1727 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1728 state
->rebase_interactive_in_progress
= 1;
1730 state
->rebase_in_progress
= 1;
1731 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1732 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1738 int wt_status_check_bisect(const struct worktree
*wt
,
1739 struct wt_status_state
*state
)
1743 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1744 state
->bisect_in_progress
= 1;
1745 state
->bisecting_from
= get_branch(wt
, "BISECT_START");
1751 static void wt_status_check_sparse_checkout(struct repository
*r
,
1752 struct wt_status_state
*state
)
1754 int skip_worktree
= 0;
1757 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1759 * Don't compute percentage of checked out files if we
1760 * aren't in a sparse checkout or would get division by 0.
1762 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1766 if (r
->index
->sparse_index
) {
1767 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1771 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1772 struct cache_entry
*ce
= r
->index
->cache
[i
];
1773 if (ce_skip_worktree(ce
))
1777 state
->sparse_checkout_percentage
=
1778 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1781 void wt_status_get_state(struct repository
*r
,
1782 struct wt_status_state
*state
,
1783 int get_detached_from
)
1786 struct object_id oid
;
1787 enum replay_action action
;
1789 if (!stat(git_path_merge_head(r
), &st
)) {
1790 wt_status_check_rebase(NULL
, state
);
1791 state
->merge_in_progress
= 1;
1792 } else if (wt_status_check_rebase(NULL
, state
)) {
1794 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1795 !repo_get_oid(r
, "CHERRY_PICK_HEAD", &oid
)) {
1796 state
->cherry_pick_in_progress
= 1;
1797 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1799 wt_status_check_bisect(NULL
, state
);
1800 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1801 !repo_get_oid(r
, "REVERT_HEAD", &oid
)) {
1802 state
->revert_in_progress
= 1;
1803 oidcpy(&state
->revert_head_oid
, &oid
);
1805 if (!sequencer_get_last_command(r
, &action
)) {
1806 if (action
== REPLAY_PICK
&& !state
->cherry_pick_in_progress
) {
1807 state
->cherry_pick_in_progress
= 1;
1808 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1809 } else if (action
== REPLAY_REVERT
&& !state
->revert_in_progress
) {
1810 state
->revert_in_progress
= 1;
1811 oidcpy(&state
->revert_head_oid
, null_oid());
1814 if (get_detached_from
)
1815 wt_status_get_detached_from(r
, state
);
1816 wt_status_check_sparse_checkout(r
, state
);
1819 static void wt_longstatus_print_state(struct wt_status
*s
)
1821 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1822 struct wt_status_state
*state
= &s
->state
;
1824 if (state
->merge_in_progress
) {
1825 if (state
->rebase_interactive_in_progress
) {
1826 show_rebase_information(s
, state_color
);
1829 show_merge_in_progress(s
, state_color
);
1830 } else if (state
->am_in_progress
)
1831 show_am_in_progress(s
, state_color
);
1832 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1833 show_rebase_in_progress(s
, state_color
);
1834 else if (state
->cherry_pick_in_progress
)
1835 show_cherry_pick_in_progress(s
, state_color
);
1836 else if (state
->revert_in_progress
)
1837 show_revert_in_progress(s
, state_color
);
1838 if (state
->bisect_in_progress
)
1839 show_bisect_in_progress(s
, state_color
);
1841 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1842 show_sparse_checkout_in_use(s
, state_color
);
1845 static void wt_longstatus_print(struct wt_status
*s
)
1847 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1848 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1849 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1852 const char *on_what
= _("On branch ");
1853 const char *branch_name
= s
->branch
;
1854 if (!strcmp(branch_name
, "HEAD")) {
1855 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1856 if (s
->state
.rebase_in_progress
||
1857 s
->state
.rebase_interactive_in_progress
) {
1858 if (s
->state
.rebase_interactive_in_progress
)
1859 on_what
= _("interactive rebase in progress; onto ");
1861 on_what
= _("rebase in progress; onto ");
1862 branch_name
= s
->state
.onto
;
1863 } else if (s
->state
.detached_from
) {
1864 branch_name
= s
->state
.detached_from
;
1865 if (s
->state
.detached_at
)
1866 on_what
= _("HEAD detached at ");
1868 on_what
= _("HEAD detached from ");
1871 on_what
= _("Not currently on any branch.");
1874 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1875 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1876 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1877 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1879 wt_longstatus_print_tracking(s
);
1882 wt_longstatus_print_state(s
);
1884 if (s
->is_initial
) {
1885 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1886 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1888 ? _("Initial commit")
1889 : _("No commits yet"));
1890 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1893 wt_longstatus_print_updated(s
);
1894 wt_longstatus_print_unmerged(s
);
1895 wt_longstatus_print_changed(s
);
1896 if (s
->submodule_summary
&&
1897 (!s
->ignore_submodule_arg
||
1898 strcmp(s
->ignore_submodule_arg
, "all"))) {
1899 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1900 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1902 if (s
->show_untracked_files
) {
1903 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1904 if (s
->show_ignored_mode
)
1905 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1906 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1907 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1908 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1909 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1910 _("It took %.2f seconds to enumerate untracked files,\n"
1911 "but the results were cached, and subsequent runs may be faster."),
1912 s
->untracked_in_ms
/ 1000.0);
1914 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1915 _("It took %.2f seconds to enumerate untracked files."),
1916 s
->untracked_in_ms
/ 1000.0);
1918 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1919 _("See 'git help status' for information on how to improve this."));
1920 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1922 } else if (s
->committable
)
1923 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1925 ? _(" (use -u option to show untracked files)") : "");
1928 wt_longstatus_print_verbose(s
);
1929 if (!s
->committable
) {
1931 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1934 else if (s
->workdir_dirty
) {
1936 fprintf(s
->fp
, _("no changes added to commit "
1937 "(use \"git add\" and/or "
1938 "\"git commit -a\")\n"));
1940 fprintf(s
->fp
, _("no changes added to "
1942 } else if (s
->untracked
.nr
) {
1944 fprintf(s
->fp
, _("nothing added to commit but "
1945 "untracked files present (use "
1946 "\"git add\" to track)\n"));
1948 fprintf(s
->fp
, _("nothing added to commit but "
1949 "untracked files present\n"));
1950 } else if (s
->is_initial
) {
1952 fprintf(s
->fp
, _("nothing to commit (create/"
1953 "copy files and use \"git "
1954 "add\" to track)\n"));
1956 fprintf(s
->fp
, _("nothing to commit\n"));
1957 } else if (!s
->show_untracked_files
) {
1959 fprintf(s
->fp
, _("nothing to commit (use -u to "
1960 "show untracked files)\n"));
1962 fprintf(s
->fp
, _("nothing to commit\n"));
1964 fprintf(s
->fp
, _("nothing to commit, working tree "
1968 wt_longstatus_print_stash_summary(s
);
1971 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1972 struct wt_status
*s
)
1974 struct wt_status_change_data
*d
= it
->util
;
1975 const char *how
= "??";
1977 switch (d
->stagemask
) {
1978 case 1: how
= "DD"; break; /* both deleted */
1979 case 2: how
= "AU"; break; /* added by us */
1980 case 3: how
= "UD"; break; /* deleted by them */
1981 case 4: how
= "UA"; break; /* added by them */
1982 case 5: how
= "DU"; break; /* deleted by us */
1983 case 6: how
= "AA"; break; /* both added */
1984 case 7: how
= "UU"; break; /* both modified */
1986 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1987 if (s
->null_termination
) {
1988 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1990 struct strbuf onebuf
= STRBUF_INIT
;
1992 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1993 fprintf(s
->fp
, " %s\n", one
);
1994 strbuf_release(&onebuf
);
1998 static void wt_shortstatus_status(struct string_list_item
*it
,
1999 struct wt_status
*s
)
2001 struct wt_status_change_data
*d
= it
->util
;
2003 if (d
->index_status
)
2004 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
2007 if (d
->worktree_status
)
2008 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
2012 if (s
->null_termination
) {
2013 fprintf(s
->fp
, "%s%c", it
->string
, 0);
2014 if (d
->rename_source
)
2015 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
2017 struct strbuf onebuf
= STRBUF_INIT
;
2020 if (d
->rename_source
) {
2021 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2022 QUOTE_PATH_QUOTE_SP
);
2023 fprintf(s
->fp
, "%s -> ", one
);
2024 strbuf_release(&onebuf
);
2026 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2027 fprintf(s
->fp
, "%s\n", one
);
2028 strbuf_release(&onebuf
);
2032 static void wt_shortstatus_other(struct string_list_item
*it
,
2033 struct wt_status
*s
, const char *sign
)
2035 if (s
->null_termination
) {
2036 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2038 struct strbuf onebuf
= STRBUF_INIT
;
2040 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2041 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2042 fprintf(s
->fp
, " %s\n", one
);
2043 strbuf_release(&onebuf
);
2047 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2049 struct branch
*branch
;
2050 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2051 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2052 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2056 const char *branch_name
;
2057 int num_ours
, num_theirs
, sti
;
2058 int upstream_is_gone
= 0;
2060 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2064 branch_name
= s
->branch
;
2066 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2069 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2071 if (!strcmp(s
->branch
, "HEAD")) {
2072 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2073 LABEL(N_("HEAD (no branch)")));
2077 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2079 branch
= branch_get(branch_name
);
2081 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2083 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2084 0, s
->ahead_behind_flags
);
2089 upstream_is_gone
= 1;
2092 short_base
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2094 color_fprintf(s
->fp
, header_color
, "...");
2095 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2098 if (!upstream_is_gone
&& !sti
)
2101 color_fprintf(s
->fp
, header_color
, " [");
2102 if (upstream_is_gone
) {
2103 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2104 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2105 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2106 } else if (!num_ours
) {
2107 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2108 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2109 } else if (!num_theirs
) {
2110 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2111 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2113 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2114 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2115 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2116 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2119 color_fprintf(s
->fp
, header_color
, "]");
2121 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2124 static void wt_shortstatus_print(struct wt_status
*s
)
2126 struct string_list_item
*it
;
2129 wt_shortstatus_print_tracking(s
);
2131 for_each_string_list_item(it
, &s
->change
) {
2132 struct wt_status_change_data
*d
= it
->util
;
2135 wt_shortstatus_unmerged(it
, s
);
2137 wt_shortstatus_status(it
, s
);
2139 for_each_string_list_item(it
, &s
->untracked
)
2140 wt_shortstatus_other(it
, s
, "??");
2142 for_each_string_list_item(it
, &s
->ignored
)
2143 wt_shortstatus_other(it
, s
, "!!");
2146 static void wt_porcelain_print(struct wt_status
*s
)
2149 s
->relative_paths
= 0;
2152 wt_shortstatus_print(s
);
2156 * Print branch information for porcelain v2 output. These lines
2157 * are printed when the '--branch' parameter is given.
2159 * # branch.oid <commit><eol>
2160 * # branch.head <head><eol>
2161 * [# branch.upstream <upstream><eol>
2162 * [# branch.ab +<ahead> -<behind><eol>]]
2164 * <commit> ::= the current commit hash or the literal
2165 * "(initial)" to indicate an initialized repo
2168 * <head> ::= <branch_name> the current branch name or
2169 * "(detached)" literal when detached head or
2170 * "(unknown)" when something is wrong.
2172 * <upstream> ::= the upstream branch name, when set.
2174 * <ahead> ::= integer ahead value or '?'.
2176 * <behind> ::= integer behind value or '?'.
2178 * The end-of-line is defined by the -z flag.
2180 * <eol> ::= NUL when -z,
2183 * When an upstream is set and present, the 'branch.ab' line will
2184 * be printed with the ahead/behind counts for the branch and the
2185 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2186 * are different, '?' will be substituted for the actual count.
2188 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2190 struct branch
*branch
;
2192 const char *branch_name
;
2193 int ab_info
, nr_ahead
, nr_behind
;
2194 char eol
= s
->null_termination
? '\0' : '\n';
2196 fprintf(s
->fp
, "# branch.oid %s%c",
2197 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2201 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2203 if (!strcmp(s
->branch
, "HEAD")) {
2204 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2206 if (s
->state
.rebase_in_progress
||
2207 s
->state
.rebase_interactive_in_progress
)
2208 branch_name
= s
->state
.onto
;
2209 else if (s
->state
.detached_from
)
2210 branch_name
= s
->state
.detached_from
;
2215 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2217 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2220 /* Lookup stats on the upstream tracking branch, if set. */
2221 branch
= branch_get(branch_name
);
2223 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2224 &base
, 0, s
->ahead_behind_flags
);
2226 base
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2228 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2233 if (nr_ahead
|| nr_behind
)
2234 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2235 nr_ahead
, nr_behind
, eol
);
2237 fprintf(s
->fp
, "# branch.ab +? -?%c",
2239 } else if (!ab_info
) {
2241 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2248 * Print the stash count in a porcelain-friendly format
2250 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2252 int stash_count
= count_stash_entries();
2253 char eol
= s
->null_termination
? '\0' : '\n';
2255 if (stash_count
> 0)
2256 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2260 * Convert various submodule status values into a
2261 * fixed-length string of characters in the buffer provided.
2263 static void wt_porcelain_v2_submodule_state(
2264 struct wt_status_change_data
*d
,
2267 if (S_ISGITLINK(d
->mode_head
) ||
2268 S_ISGITLINK(d
->mode_index
) ||
2269 S_ISGITLINK(d
->mode_worktree
)) {
2271 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2272 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2273 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2284 * Fix-up changed entries before we print them.
2286 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2288 struct wt_status_change_data
*d
= it
->util
;
2290 if (!d
->index_status
) {
2292 * This entry is unchanged in the index (relative to the head).
2293 * Therefore, the collect_updated_cb was never called for this
2294 * entry (during the head-vs-index scan) and so the head column
2295 * fields were never set.
2297 * We must have data for the index column (from the
2298 * index-vs-worktree scan (otherwise, this entry should not be
2299 * in the list of changes)).
2301 * Copy index column fields to the head column, so that our
2302 * output looks complete.
2304 assert(d
->mode_head
== 0);
2305 d
->mode_head
= d
->mode_index
;
2306 oidcpy(&d
->oid_head
, &d
->oid_index
);
2309 if (!d
->worktree_status
) {
2311 * This entry is unchanged in the worktree (relative to the index).
2312 * Therefore, the collect_changed_cb was never called for this entry
2313 * (during the index-vs-worktree scan) and so the worktree column
2314 * fields were never set.
2316 * We must have data for the index column (from the head-vs-index
2319 * Copy the index column fields to the worktree column so that
2320 * our output looks complete.
2322 * Note that we only have a mode field in the worktree column
2323 * because the scan code tries really hard to not have to compute it.
2325 assert(d
->mode_worktree
== 0);
2326 d
->mode_worktree
= d
->mode_index
;
2331 * Print porcelain v2 info for tracked entries with changes.
2333 static void wt_porcelain_v2_print_changed_entry(
2334 struct string_list_item
*it
,
2335 struct wt_status
*s
)
2337 struct wt_status_change_data
*d
= it
->util
;
2338 struct strbuf buf
= STRBUF_INIT
;
2339 struct strbuf buf_from
= STRBUF_INIT
;
2340 const char *path
= NULL
;
2341 const char *path_from
= NULL
;
2343 char submodule_token
[5];
2344 char sep_char
, eol_char
;
2346 wt_porcelain_v2_fix_up_changed(it
);
2347 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2349 key
[0] = d
->index_status
? d
->index_status
: '.';
2350 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2353 if (s
->null_termination
) {
2355 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2356 * A single NUL character separates them.
2361 path_from
= d
->rename_source
;
2364 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2365 * The source path is only present when necessary.
2366 * A single TAB separates them (because paths can contain spaces
2367 * which are not escaped and C-quoting does escape TAB characters).
2371 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2372 if (d
->rename_source
)
2373 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2377 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2378 key
, submodule_token
,
2379 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2380 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2381 d
->rename_status
, d
->rename_score
,
2382 path
, sep_char
, path_from
, eol_char
);
2384 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2385 key
, submodule_token
,
2386 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2387 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2390 strbuf_release(&buf
);
2391 strbuf_release(&buf_from
);
2395 * Print porcelain v2 status info for unmerged entries.
2397 static void wt_porcelain_v2_print_unmerged_entry(
2398 struct string_list_item
*it
,
2399 struct wt_status
*s
)
2401 struct wt_status_change_data
*d
= it
->util
;
2402 struct index_state
*istate
= s
->repo
->index
;
2403 const struct cache_entry
*ce
;
2404 struct strbuf buf_index
= STRBUF_INIT
;
2405 const char *path_index
= NULL
;
2406 int pos
, stage
, sum
;
2409 struct object_id oid
;
2412 char submodule_token
[5];
2413 char unmerged_prefix
= 'u';
2414 char eol_char
= s
->null_termination
? '\0' : '\n';
2416 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2418 switch (d
->stagemask
) {
2419 case 1: key
= "DD"; break; /* both deleted */
2420 case 2: key
= "AU"; break; /* added by us */
2421 case 3: key
= "UD"; break; /* deleted by them */
2422 case 4: key
= "UA"; break; /* added by them */
2423 case 5: key
= "DU"; break; /* deleted by us */
2424 case 6: key
= "AA"; break; /* both added */
2425 case 7: key
= "UU"; break; /* both modified */
2427 BUG("unhandled unmerged status %x", d
->stagemask
);
2431 * Disregard d.aux.porcelain_v2 data that we accumulated
2432 * for the head and index columns during the scans and
2433 * replace with the actual stage data.
2435 * Note that this is a last-one-wins for each the individual
2436 * stage [123] columns in the event of multiple cache entries
2439 memset(stages
, 0, sizeof(stages
));
2441 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2444 while (pos
< istate
->cache_nr
) {
2445 ce
= istate
->cache
[pos
++];
2446 stage
= ce_stage(ce
);
2447 if (strcmp(ce
->name
, it
->string
) || !stage
)
2449 stages
[stage
- 1].mode
= ce
->ce_mode
;
2450 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2451 sum
|= (1 << (stage
- 1));
2453 if (sum
!= d
->stagemask
)
2454 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2456 if (s
->null_termination
)
2457 path_index
= it
->string
;
2459 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2461 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2462 unmerged_prefix
, key
, submodule_token
,
2463 stages
[0].mode
, /* stage 1 */
2464 stages
[1].mode
, /* stage 2 */
2465 stages
[2].mode
, /* stage 3 */
2467 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2468 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2469 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2473 strbuf_release(&buf_index
);
2477 * Print porcelain V2 status info for untracked and ignored entries.
2479 static void wt_porcelain_v2_print_other(
2480 struct string_list_item
*it
,
2481 struct wt_status
*s
,
2484 struct strbuf buf
= STRBUF_INIT
;
2488 if (s
->null_termination
) {
2492 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2496 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2498 strbuf_release(&buf
);
2502 * Print porcelain V2 status.
2505 * [<v2_changed_items>]*
2506 * [<v2_unmerged_items>]*
2507 * [<v2_untracked_items>]*
2508 * [<v2_ignored_items>]*
2511 static void wt_porcelain_v2_print(struct wt_status
*s
)
2513 struct wt_status_change_data
*d
;
2514 struct string_list_item
*it
;
2518 wt_porcelain_v2_print_tracking(s
);
2521 wt_porcelain_v2_print_stash(s
);
2523 for (i
= 0; i
< s
->change
.nr
; i
++) {
2524 it
= &(s
->change
.items
[i
]);
2527 wt_porcelain_v2_print_changed_entry(it
, s
);
2530 for (i
= 0; i
< s
->change
.nr
; i
++) {
2531 it
= &(s
->change
.items
[i
]);
2534 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2537 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2538 it
= &(s
->untracked
.items
[i
]);
2539 wt_porcelain_v2_print_other(it
, s
, '?');
2542 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2543 it
= &(s
->ignored
.items
[i
]);
2544 wt_porcelain_v2_print_other(it
, s
, '!');
2548 void wt_status_print(struct wt_status
*s
)
2550 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2551 trace2_data_intmax("status", s
->repo
, "count/untracked",
2553 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2555 trace2_region_enter("status", "print", s
->repo
);
2557 switch (s
->status_format
) {
2558 case STATUS_FORMAT_SHORT
:
2559 wt_shortstatus_print(s
);
2561 case STATUS_FORMAT_PORCELAIN
:
2562 wt_porcelain_print(s
);
2564 case STATUS_FORMAT_PORCELAIN_V2
:
2565 wt_porcelain_v2_print(s
);
2567 case STATUS_FORMAT_UNSPECIFIED
:
2568 BUG("finalize_deferred_config() should have been called");
2570 case STATUS_FORMAT_NONE
:
2571 case STATUS_FORMAT_LONG
:
2572 wt_longstatus_print(s
);
2576 trace2_region_leave("status", "print", s
->repo
);
2580 * Returns 1 if there are unstaged changes, 0 otherwise.
2582 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2584 struct rev_info rev_info
;
2587 repo_init_revisions(r
, &rev_info
, NULL
);
2588 if (ignore_submodules
) {
2589 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2590 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2592 rev_info
.diffopt
.flags
.quick
= 1;
2593 diff_setup_done(&rev_info
.diffopt
);
2594 run_diff_files(&rev_info
, 0);
2595 result
= diff_result_code(&rev_info
.diffopt
);
2596 release_revisions(&rev_info
);
2601 * Returns 1 if there are uncommitted changes, 0 otherwise.
2603 int has_uncommitted_changes(struct repository
*r
,
2604 int ignore_submodules
)
2606 struct rev_info rev_info
;
2609 if (is_index_unborn(r
->index
))
2612 repo_init_revisions(r
, &rev_info
, NULL
);
2613 if (ignore_submodules
)
2614 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2615 rev_info
.diffopt
.flags
.quick
= 1;
2617 add_head_to_pending(&rev_info
);
2618 if (!rev_info
.pending
.nr
) {
2620 * We have no head (or it's corrupt); use the empty tree,
2621 * which will complain if the index is non-empty.
2623 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2624 add_pending_object(&rev_info
, &tree
->object
, "");
2627 diff_setup_done(&rev_info
.diffopt
);
2628 run_diff_index(&rev_info
, DIFF_INDEX_CACHED
);
2629 result
= diff_result_code(&rev_info
.diffopt
);
2630 release_revisions(&rev_info
);
2635 * If the work tree has unstaged or uncommitted changes, dies with the
2636 * appropriate message.
2638 int require_clean_work_tree(struct repository
*r
,
2641 int ignore_submodules
,
2644 struct lock_file lock_file
= LOCK_INIT
;
2647 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2648 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2650 repo_update_index_if_able(r
, &lock_file
);
2651 rollback_lock_file(&lock_file
);
2653 if (has_unstaged_changes(r
, ignore_submodules
)) {
2654 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2655 error(_("cannot %s: You have unstaged changes."), _(action
));
2659 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2661 error(_("additionally, your index contains uncommitted changes."));
2663 error(_("cannot %s: Your index contains uncommitted changes."),
2671 BUG("empty hint passed to require_clean_work_tree();"
2672 " use NULL instead");