1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
10 #include "environment.h"
14 #include "object-name.h"
19 #include "repository.h"
20 #include "run-command.h"
24 #include "submodule.h"
26 #include "read-cache.h"
35 #include "sequencer.h"
36 #include "fsmonitor-settings.h"
38 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
39 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
41 static const char cut_line
[] =
42 "------------------------ >8 ------------------------\n";
44 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
45 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
46 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
47 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
48 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
49 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
50 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
51 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
52 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
53 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
56 static const char *color(int slot
, struct wt_status
*s
)
59 if (want_color(s
->use_color
))
60 c
= s
->color_palette
[slot
];
61 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
62 c
= s
->color_palette
[WT_STATUS_HEADER
];
66 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
67 const char *fmt
, va_list ap
, const char *trail
)
69 struct strbuf sb
= STRBUF_INIT
;
70 struct strbuf linebuf
= STRBUF_INIT
;
71 const char *line
, *eol
;
73 strbuf_vaddf(&sb
, fmt
, ap
);
75 if (s
->display_comment_prefix
) {
76 strbuf_addstr(&sb
, comment_line_str
);
78 strbuf_addch(&sb
, ' ');
80 color_print_strbuf(s
->fp
, color
, &sb
);
82 fprintf(s
->fp
, "%s", trail
);
86 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
87 eol
= strchr(line
, '\n');
89 strbuf_reset(&linebuf
);
90 if (at_bol
&& s
->display_comment_prefix
) {
91 strbuf_addstr(&linebuf
, comment_line_str
);
92 if (*line
!= '\n' && *line
!= '\t')
93 strbuf_addch(&linebuf
, ' ');
96 strbuf_add(&linebuf
, line
, eol
- line
);
98 strbuf_addstr(&linebuf
, line
);
99 color_print_strbuf(s
->fp
, color
, &linebuf
);
101 fprintf(s
->fp
, "\n");
107 fprintf(s
->fp
, "%s", trail
);
108 strbuf_release(&linebuf
);
112 void status_printf_ln(struct wt_status
*s
, const char *color
,
113 const char *fmt
, ...)
118 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
122 void status_printf(struct wt_status
*s
, const char *color
,
123 const char *fmt
, ...)
128 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
132 __attribute__((format (printf
, 3, 4)))
133 static void status_printf_more(struct wt_status
*s
, const char *color
,
134 const char *fmt
, ...)
139 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
143 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
145 memset(s
, 0, sizeof(*s
));
147 memcpy(s
->color_palette
, default_wt_status_colors
,
148 sizeof(default_wt_status_colors
));
149 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
151 s
->relative_paths
= 1;
152 s
->branch
= refs_resolve_refdup(get_main_ref_store(the_repository
),
153 "HEAD", 0, NULL
, NULL
);
154 s
->reference
= "HEAD";
156 s
->index_file
= repo_get_index_file(the_repository
);
157 s
->change
.strdup_strings
= 1;
158 s
->untracked
.strdup_strings
= 1;
159 s
->ignored
.strdup_strings
= 1;
160 s
->show_branch
= -1; /* unspecified */
162 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
163 s
->display_comment_prefix
= 0;
164 s
->detect_rename
= -1;
165 s
->rename_score
= -1;
166 s
->rename_limit
= -1;
169 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
172 int del_mod_conflict
= 0;
173 int both_deleted
= 0;
175 const char *c
= color(WT_STATUS_HEADER
, s
);
177 status_printf_ln(s
, c
, _("Unmerged paths:"));
179 for (i
= 0; i
< s
->change
.nr
; i
++) {
180 struct string_list_item
*it
= &(s
->change
.items
[i
]);
181 struct wt_status_change_data
*d
= it
->util
;
183 switch (d
->stagemask
) {
191 del_mod_conflict
= 1;
201 if (s
->whence
!= FROM_COMMIT
)
203 else if (!s
->is_initial
) {
204 if (!strcmp(s
->reference
, "HEAD"))
205 status_printf_ln(s
, c
,
206 _(" (use \"git restore --staged <file>...\" to unstage)"));
208 status_printf_ln(s
, c
,
209 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
212 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
215 if (!del_mod_conflict
)
216 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
218 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
219 } else if (!del_mod_conflict
&& !not_deleted
) {
220 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
222 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
226 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
228 const char *c
= color(WT_STATUS_HEADER
, s
);
230 status_printf_ln(s
, c
, _("Changes to be committed:"));
233 if (s
->whence
!= FROM_COMMIT
)
234 ; /* NEEDSWORK: use "git reset --unresolve"??? */
235 else if (!s
->is_initial
) {
236 if (!strcmp(s
->reference
, "HEAD"))
237 status_printf_ln(s
, c
238 , _(" (use \"git restore --staged <file>...\" to unstage)"));
240 status_printf_ln(s
, c
,
241 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
244 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
247 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
249 int has_dirty_submodules
)
251 const char *c
= color(WT_STATUS_HEADER
, s
);
253 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
257 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
259 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
260 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
261 if (has_dirty_submodules
)
262 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
265 static void wt_longstatus_print_other_header(struct wt_status
*s
,
269 const char *c
= color(WT_STATUS_HEADER
, s
);
270 status_printf_ln(s
, c
, "%s:", what
);
273 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
276 static void wt_longstatus_print_trailer(struct wt_status
*s
)
278 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
281 static const char *wt_status_unmerged_status_string(int stagemask
)
285 return _("both deleted:");
287 return _("added by us:");
289 return _("deleted by them:");
291 return _("added by them:");
293 return _("deleted by us:");
295 return _("both added:");
297 return _("both modified:");
299 BUG("unhandled unmerged status %x", stagemask
);
303 static const char *wt_status_diff_status_string(int status
)
306 case DIFF_STATUS_ADDED
:
307 return _("new file:");
308 case DIFF_STATUS_COPIED
:
310 case DIFF_STATUS_DELETED
:
311 return _("deleted:");
312 case DIFF_STATUS_MODIFIED
:
313 return _("modified:");
314 case DIFF_STATUS_RENAMED
:
315 return _("renamed:");
316 case DIFF_STATUS_TYPE_CHANGED
:
317 return _("typechange:");
318 case DIFF_STATUS_UNKNOWN
:
319 return _("unknown:");
320 case DIFF_STATUS_UNMERGED
:
321 return _("unmerged:");
327 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
331 for (i
= minval
; i
<= maxval
; i
++) {
332 const char *s
= label(i
);
333 int len
= s
? utf8_strwidth(s
) : 0;
340 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
341 struct string_list_item
*it
)
343 const char *c
= color(WT_STATUS_UNMERGED
, s
);
344 struct wt_status_change_data
*d
= it
->util
;
345 struct strbuf onebuf
= STRBUF_INIT
;
346 static char *padding
;
347 static int label_width
;
348 const char *one
, *how
;
352 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
353 label_width
+= strlen(" ");
354 padding
= xmallocz(label_width
);
355 memset(padding
, ' ', label_width
);
358 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
359 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
361 how
= wt_status_unmerged_status_string(d
->stagemask
);
362 len
= label_width
- utf8_strwidth(how
);
363 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
364 strbuf_release(&onebuf
);
367 static void wt_longstatus_print_change_data(struct wt_status
*s
,
369 struct string_list_item
*it
)
371 struct wt_status_change_data
*d
= it
->util
;
372 const char *c
= color(change_type
, s
);
376 const char *one
, *two
;
377 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
378 struct strbuf extra
= STRBUF_INIT
;
379 static char *padding
;
380 static int label_width
;
385 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
386 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
387 label_width
+= strlen(" ");
388 padding
= xmallocz(label_width
);
389 memset(padding
, ' ', label_width
);
392 one_name
= two_name
= it
->string
;
393 switch (change_type
) {
394 case WT_STATUS_UPDATED
:
395 status
= d
->index_status
;
397 case WT_STATUS_CHANGED
:
398 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
399 strbuf_addstr(&extra
, " (");
400 if (d
->new_submodule_commits
)
401 strbuf_addstr(&extra
, _("new commits, "));
402 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
403 strbuf_addstr(&extra
, _("modified content, "));
404 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
405 strbuf_addstr(&extra
, _("untracked content, "));
406 strbuf_setlen(&extra
, extra
.len
- 2);
407 strbuf_addch(&extra
, ')');
409 status
= d
->worktree_status
;
412 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
417 * Only pick up the rename it's relevant. If the rename is for
418 * the changed section and we're printing the updated section,
421 if (d
->rename_status
== status
)
422 one_name
= d
->rename_source
;
424 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
425 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
427 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
428 what
= wt_status_diff_status_string(status
);
430 BUG("unhandled diff status %c", status
);
431 len
= label_width
- utf8_strwidth(what
);
433 if (one_name
!= two_name
)
434 status_printf_more(s
, c
, "%s%.*s%s -> %s",
435 what
, len
, padding
, one
, two
);
437 status_printf_more(s
, c
, "%s%.*s%s",
438 what
, len
, padding
, one
);
440 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
441 strbuf_release(&extra
);
443 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
444 strbuf_release(&onebuf
);
445 strbuf_release(&twobuf
);
448 static char short_submodule_status(struct wt_status_change_data
*d
)
450 if (d
->new_submodule_commits
)
452 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
454 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
456 return d
->worktree_status
;
459 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
460 struct diff_options
*options UNUSED
,
463 struct wt_status
*s
= data
;
468 s
->workdir_dirty
= 1;
469 for (i
= 0; i
< q
->nr
; i
++) {
470 struct diff_filepair
*p
;
471 struct string_list_item
*it
;
472 struct wt_status_change_data
*d
;
475 it
= string_list_insert(&s
->change
, p
->two
->path
);
481 if (!d
->worktree_status
)
482 d
->worktree_status
= p
->status
;
483 if (S_ISGITLINK(p
->two
->mode
)) {
484 d
->dirty_submodule
= p
->two
->dirty_submodule
;
485 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
487 if (s
->status_format
== STATUS_FORMAT_SHORT
)
488 d
->worktree_status
= short_submodule_status(d
);
492 case DIFF_STATUS_ADDED
:
493 d
->mode_worktree
= p
->two
->mode
;
496 case DIFF_STATUS_DELETED
:
497 d
->mode_index
= p
->one
->mode
;
498 oidcpy(&d
->oid_index
, &p
->one
->oid
);
499 /* mode_worktree is zero for a delete. */
502 case DIFF_STATUS_COPIED
:
503 case DIFF_STATUS_RENAMED
:
504 if (d
->rename_status
)
505 BUG("multiple renames on the same target? how?");
506 d
->rename_source
= xstrdup(p
->one
->path
);
507 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
508 d
->rename_status
= p
->status
;
510 case DIFF_STATUS_MODIFIED
:
511 case DIFF_STATUS_TYPE_CHANGED
:
512 case DIFF_STATUS_UNMERGED
:
513 d
->mode_index
= p
->one
->mode
;
514 d
->mode_worktree
= p
->two
->mode
;
515 oidcpy(&d
->oid_index
, &p
->one
->oid
);
519 BUG("unhandled diff-files status '%c'", p
->status
);
526 static int unmerged_mask(struct index_state
*istate
, const char *path
)
529 const struct cache_entry
*ce
;
531 pos
= index_name_pos(istate
, path
, strlen(path
));
537 while (pos
< istate
->cache_nr
) {
538 ce
= istate
->cache
[pos
++];
539 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
541 mask
|= (1 << (ce_stage(ce
) - 1));
546 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
547 struct diff_options
*options UNUSED
,
550 struct wt_status
*s
= data
;
553 for (i
= 0; i
< q
->nr
; i
++) {
554 struct diff_filepair
*p
;
555 struct string_list_item
*it
;
556 struct wt_status_change_data
*d
;
559 it
= string_list_insert(&s
->change
, p
->two
->path
);
565 if (!d
->index_status
)
566 d
->index_status
= p
->status
;
568 case DIFF_STATUS_ADDED
:
569 /* Leave {mode,oid}_head zero for an add. */
570 d
->mode_index
= p
->two
->mode
;
571 oidcpy(&d
->oid_index
, &p
->two
->oid
);
574 case DIFF_STATUS_DELETED
:
575 d
->mode_head
= p
->one
->mode
;
576 oidcpy(&d
->oid_head
, &p
->one
->oid
);
578 /* Leave {mode,oid}_index zero for a delete. */
581 case DIFF_STATUS_COPIED
:
582 case DIFF_STATUS_RENAMED
:
583 if (d
->rename_status
)
584 BUG("multiple renames on the same target? how?");
585 d
->rename_source
= xstrdup(p
->one
->path
);
586 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
587 d
->rename_status
= p
->status
;
589 case DIFF_STATUS_MODIFIED
:
590 case DIFF_STATUS_TYPE_CHANGED
:
591 d
->mode_head
= p
->one
->mode
;
592 d
->mode_index
= p
->two
->mode
;
593 oidcpy(&d
->oid_head
, &p
->one
->oid
);
594 oidcpy(&d
->oid_index
, &p
->two
->oid
);
597 case DIFF_STATUS_UNMERGED
:
598 d
->stagemask
= unmerged_mask(s
->repo
->index
,
601 * Don't bother setting {mode,oid}_{head,index} since the print
602 * code will output the stage values directly and not use the
603 * values in these fields.
608 BUG("unhandled diff-index status '%c'", p
->status
);
614 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
618 repo_init_revisions(s
->repo
, &rev
, NULL
);
619 setup_revisions(0, NULL
, &rev
, NULL
);
620 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
621 rev
.diffopt
.flags
.dirty_submodules
= 1;
622 rev
.diffopt
.ita_invisible_in_index
= 1;
623 if (!s
->show_untracked_files
)
624 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
625 if (s
->ignore_submodule_arg
) {
626 rev
.diffopt
.flags
.override_submodule_config
= 1;
627 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
628 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
629 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
630 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
631 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
632 rev
.diffopt
.format_callback_data
= s
;
633 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
634 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
635 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
636 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
637 run_diff_files(&rev
, 0);
638 release_revisions(&rev
);
641 static void wt_status_collect_changes_index(struct wt_status
*s
)
644 struct setup_revision_opt opt
;
646 repo_init_revisions(s
->repo
, &rev
, NULL
);
647 memset(&opt
, 0, sizeof(opt
));
648 opt
.def
= s
->is_initial
? empty_tree_oid_hex(the_repository
->hash_algo
) : s
->reference
;
649 setup_revisions(0, NULL
, &rev
, &opt
);
651 rev
.diffopt
.flags
.override_submodule_config
= 1;
652 rev
.diffopt
.ita_invisible_in_index
= 1;
653 if (s
->ignore_submodule_arg
) {
654 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
657 * Unless the user did explicitly request a submodule ignore
658 * mode by passing a command line option we do not ignore any
659 * changed submodule SHA-1s when comparing index and HEAD, no
660 * matter what is configured. Otherwise the user won't be
661 * shown any submodules manually added (and which are
662 * staged to be committed), which would be really confusing.
664 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
667 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
668 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
669 rev
.diffopt
.format_callback_data
= s
;
670 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
671 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
672 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
675 * The `recursive` option must be enabled to allow the diff to recurse
676 * into subdirectories of sparse directory index entries. If it is not
677 * enabled, a subdirectory containing file(s) with changes is reported
678 * as "modified", rather than the modified files themselves.
680 rev
.diffopt
.flags
.recursive
= 1;
682 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
683 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
684 release_revisions(&rev
);
687 static int add_file_to_list(const struct object_id
*oid
,
688 struct strbuf
*base
, const char *path
,
689 unsigned int mode
, void *context
)
691 struct string_list_item
*it
;
692 struct wt_status_change_data
*d
;
693 struct wt_status
*s
= context
;
694 struct strbuf full_name
= STRBUF_INIT
;
697 return READ_TREE_RECURSIVE
;
699 strbuf_add(&full_name
, base
->buf
, base
->len
);
700 strbuf_addstr(&full_name
, path
);
701 it
= string_list_insert(&s
->change
, full_name
.buf
);
708 d
->index_status
= DIFF_STATUS_ADDED
;
709 /* Leave {mode,oid}_head zero for adds. */
710 d
->mode_index
= mode
;
711 oidcpy(&d
->oid_index
, oid
);
713 strbuf_release(&full_name
);
717 static void wt_status_collect_changes_initial(struct wt_status
*s
)
719 struct index_state
*istate
= s
->repo
->index
;
722 for (i
= 0; i
< istate
->cache_nr
; i
++) {
723 struct string_list_item
*it
;
724 struct wt_status_change_data
*d
;
725 const struct cache_entry
*ce
= istate
->cache
[i
];
727 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
729 if (ce_intent_to_add(ce
))
731 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
733 * This is a sparse directory entry, so we want to collect all
734 * of the added files within the tree. This requires recursively
735 * expanding the trees to find the elements that are new in this
736 * tree and marking them with DIFF_STATUS_ADDED.
738 struct strbuf base
= STRBUF_INIT
;
739 struct pathspec ps
= { 0 };
740 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
746 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
747 read_tree_at(istate
->repo
, tree
, &base
, 0, &ps
,
748 add_file_to_list
, s
);
752 it
= string_list_insert(&s
->change
, ce
->name
);
759 d
->index_status
= DIFF_STATUS_UNMERGED
;
760 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
762 * Don't bother setting {mode,oid}_{head,index} since the print
763 * code will output the stage values directly and not use the
764 * values in these fields.
768 d
->index_status
= DIFF_STATUS_ADDED
;
769 /* Leave {mode,oid}_head zero for adds. */
770 d
->mode_index
= ce
->ce_mode
;
771 oidcpy(&d
->oid_index
, &ce
->oid
);
777 static void wt_status_collect_untracked(struct wt_status
*s
)
780 struct dir_struct dir
= DIR_INIT
;
781 uint64_t t_begin
= getnanotime();
782 struct index_state
*istate
= s
->repo
->index
;
784 if (!s
->show_untracked_files
)
787 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
789 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
790 if (s
->show_ignored_mode
) {
791 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
793 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
794 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
796 dir
.untracked
= istate
->untracked
;
799 setup_standard_excludes(&dir
);
801 fill_directory(&dir
, istate
, &s
->pathspec
);
803 for (i
= 0; i
< dir
.nr
; i
++) {
804 struct dir_entry
*ent
= dir
.entries
[i
];
805 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
806 string_list_insert(&s
->untracked
, ent
->name
);
809 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
810 struct dir_entry
*ent
= dir
.ignored
[i
];
811 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
812 string_list_insert(&s
->ignored
, ent
->name
);
817 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
818 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
821 static int has_unmerged(struct wt_status
*s
)
825 for (i
= 0; i
< s
->change
.nr
; i
++) {
826 struct wt_status_change_data
*d
;
827 d
= s
->change
.items
[i
].util
;
834 void wt_status_collect(struct wt_status
*s
)
836 trace2_region_enter("status", "worktrees", s
->repo
);
837 wt_status_collect_changes_worktree(s
);
838 trace2_region_leave("status", "worktrees", s
->repo
);
841 trace2_region_enter("status", "initial", s
->repo
);
842 wt_status_collect_changes_initial(s
);
843 trace2_region_leave("status", "initial", s
->repo
);
845 trace2_region_enter("status", "index", s
->repo
);
846 wt_status_collect_changes_index(s
);
847 trace2_region_leave("status", "index", s
->repo
);
850 trace2_region_enter("status", "untracked", s
->repo
);
851 wt_status_collect_untracked(s
);
852 trace2_region_leave("status", "untracked", s
->repo
);
854 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
855 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
859 void wt_status_collect_free_buffers(struct wt_status
*s
)
861 wt_status_state_free_buffers(&s
->state
);
864 void wt_status_state_free_buffers(struct wt_status_state
*state
)
866 FREE_AND_NULL(state
->branch
);
867 FREE_AND_NULL(state
->onto
);
868 FREE_AND_NULL(state
->detached_from
);
869 FREE_AND_NULL(state
->bisecting_from
);
872 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
874 int shown_header
= 0;
877 for (i
= 0; i
< s
->change
.nr
; i
++) {
878 struct wt_status_change_data
*d
;
879 struct string_list_item
*it
;
880 it
= &(s
->change
.items
[i
]);
885 wt_longstatus_print_unmerged_header(s
);
888 wt_longstatus_print_unmerged_data(s
, it
);
891 wt_longstatus_print_trailer(s
);
895 static void wt_longstatus_print_updated(struct wt_status
*s
)
897 int shown_header
= 0;
900 for (i
= 0; i
< s
->change
.nr
; i
++) {
901 struct wt_status_change_data
*d
;
902 struct string_list_item
*it
;
903 it
= &(s
->change
.items
[i
]);
905 if (!d
->index_status
||
906 d
->index_status
== DIFF_STATUS_UNMERGED
)
909 wt_longstatus_print_cached_header(s
);
912 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
915 wt_longstatus_print_trailer(s
);
921 * 1 : some change but no delete
923 static int wt_status_check_worktree_changes(struct wt_status
*s
,
924 int *dirty_submodules
)
929 *dirty_submodules
= 0;
931 for (i
= 0; i
< s
->change
.nr
; i
++) {
932 struct wt_status_change_data
*d
;
933 d
= s
->change
.items
[i
].util
;
934 if (!d
->worktree_status
||
935 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
939 if (d
->dirty_submodule
)
940 *dirty_submodules
= 1;
941 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
947 static void wt_longstatus_print_changed(struct wt_status
*s
)
949 int i
, dirty_submodules
;
950 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
952 if (!worktree_changes
)
955 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
957 for (i
= 0; i
< s
->change
.nr
; i
++) {
958 struct wt_status_change_data
*d
;
959 struct string_list_item
*it
;
960 it
= &(s
->change
.items
[i
]);
962 if (!d
->worktree_status
||
963 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
965 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
967 wt_longstatus_print_trailer(s
);
970 static int stash_count_refs(struct object_id
*ooid UNUSED
,
971 struct object_id
*noid UNUSED
,
972 const char *email UNUSED
,
973 timestamp_t timestamp UNUSED
, int tz UNUSED
,
974 const char *message UNUSED
, void *cb_data
)
981 static int count_stash_entries(void)
984 refs_for_each_reflog_ent(get_main_ref_store(the_repository
),
985 "refs/stash", stash_count_refs
, &n
);
989 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
991 int stash_count
= count_stash_entries();
994 status_printf_ln(s
, GIT_COLOR_NORMAL
,
995 Q_("Your stash currently has %d entry",
996 "Your stash currently has %d entries", stash_count
),
1000 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
1002 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
1003 struct strbuf cmd_stdout
= STRBUF_INIT
;
1004 struct strbuf summary
= STRBUF_INIT
;
1005 char *summary_content
;
1007 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
1009 strvec_push(&sm_summary
.args
, "submodule");
1010 strvec_push(&sm_summary
.args
, "summary");
1011 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
1012 strvec_push(&sm_summary
.args
, "--for-status");
1013 strvec_push(&sm_summary
.args
, "--summary-limit");
1014 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
1016 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1018 sm_summary
.git_cmd
= 1;
1019 sm_summary
.no_stdin
= 1;
1021 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1023 /* prepend header, only if there's an actual output */
1024 if (cmd_stdout
.len
) {
1026 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1028 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1029 strbuf_addstr(&summary
, "\n\n");
1031 strbuf_addbuf(&summary
, &cmd_stdout
);
1032 strbuf_release(&cmd_stdout
);
1034 if (s
->display_comment_prefix
) {
1036 summary_content
= strbuf_detach(&summary
, &len
);
1037 strbuf_add_commented_lines(&summary
, summary_content
, len
, comment_line_str
);
1038 free(summary_content
);
1041 fputs(summary
.buf
, s
->fp
);
1042 strbuf_release(&summary
);
1045 static void wt_longstatus_print_other(struct wt_status
*s
,
1046 struct string_list
*l
,
1051 struct strbuf buf
= STRBUF_INIT
;
1052 static struct string_list output
= STRING_LIST_INIT_DUP
;
1053 struct column_options copts
;
1058 wt_longstatus_print_other_header(s
, what
, how
);
1060 for (i
= 0; i
< l
->nr
; i
++) {
1061 struct string_list_item
*it
;
1063 it
= &(l
->items
[i
]);
1064 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1065 if (column_active(s
->colopts
)) {
1066 string_list_append(&output
, path
);
1069 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1070 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1074 strbuf_release(&buf
);
1075 if (!column_active(s
->colopts
))
1078 strbuf_addf(&buf
, "%s%s\t%s",
1079 color(WT_STATUS_HEADER
, s
),
1080 s
->display_comment_prefix
? "#" : "",
1081 color(WT_STATUS_UNTRACKED
, s
));
1082 memset(&copts
, 0, sizeof(copts
));
1084 copts
.indent
= buf
.buf
;
1085 if (want_color(s
->use_color
))
1086 copts
.nl
= GIT_COLOR_RESET
"\n";
1087 print_columns(&output
, s
->colopts
, &copts
);
1088 string_list_clear(&output
, 0);
1089 strbuf_release(&buf
);
1091 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1094 size_t wt_status_locate_end(const char *s
, size_t len
)
1097 struct strbuf pattern
= STRBUF_INIT
;
1099 strbuf_addf(&pattern
, "\n%s %s", comment_line_str
, cut_line
);
1100 if (starts_with(s
, pattern
.buf
+ 1))
1102 else if ((p
= strstr(s
, pattern
.buf
))) {
1103 size_t newlen
= p
- s
+ 1;
1107 strbuf_release(&pattern
);
1111 void wt_status_append_cut_line(struct strbuf
*buf
)
1113 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1115 strbuf_commented_addf(buf
, comment_line_str
, "%s", cut_line
);
1116 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
), comment_line_str
);
1119 void wt_status_add_cut_line(struct wt_status
*s
)
1121 struct strbuf buf
= STRBUF_INIT
;
1123 if (s
->added_cut_line
)
1125 s
->added_cut_line
= 1;
1126 wt_status_append_cut_line(&buf
);
1127 fputs(buf
.buf
, s
->fp
);
1128 strbuf_release(&buf
);
1131 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1133 struct rev_info rev
;
1134 struct setup_revision_opt opt
;
1135 int dirty_submodules
;
1136 const char *c
= color(WT_STATUS_HEADER
, s
);
1138 repo_init_revisions(s
->repo
, &rev
, NULL
);
1139 rev
.diffopt
.flags
.allow_textconv
= 1;
1140 rev
.diffopt
.ita_invisible_in_index
= 1;
1142 memset(&opt
, 0, sizeof(opt
));
1143 opt
.def
= s
->is_initial
? empty_tree_oid_hex(the_repository
->hash_algo
) : s
->reference
;
1144 setup_revisions(0, NULL
, &rev
, &opt
);
1146 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1147 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1148 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1149 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1150 rev
.diffopt
.file
= s
->fp
;
1151 rev
.diffopt
.close_file
= 0;
1153 * If we're not going to stdout, then we definitely don't
1154 * want color, since we are going to the commit message
1155 * file (and even the "auto" setting won't work, since it
1156 * will have checked isatty on stdout). But we then do want
1157 * to insert the scissor line here to reliably remove the
1158 * diff before committing, if we didn't already include one
1161 if (s
->fp
!= stdout
) {
1162 rev
.diffopt
.use_color
= 0;
1163 wt_status_add_cut_line(s
);
1165 if (s
->verbose
> 1 && s
->committable
) {
1166 /* print_updated() printed a header, so do we */
1167 if (s
->fp
!= stdout
)
1168 wt_longstatus_print_trailer(s
);
1169 status_printf_ln(s
, c
, _("Changes to be committed:"));
1170 rev
.diffopt
.a_prefix
= "c/";
1171 rev
.diffopt
.b_prefix
= "i/";
1172 } /* else use prefix as per user config */
1173 run_diff_index(&rev
, DIFF_INDEX_CACHED
);
1174 if (s
->verbose
> 1 &&
1175 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1176 status_printf_ln(s
, c
,
1177 "--------------------------------------------------");
1178 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1180 rev
.diffopt
.a_prefix
= "i/";
1181 rev
.diffopt
.b_prefix
= "w/";
1182 run_diff_files(&rev
, 0);
1184 release_revisions(&rev
);
1187 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1189 struct strbuf sb
= STRBUF_INIT
;
1190 const char *cp
, *ep
, *branch_name
;
1191 struct branch
*branch
;
1192 uint64_t t_begin
= 0;
1194 assert(s
->branch
&& !s
->is_initial
);
1195 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1197 branch
= branch_get(branch_name
);
1199 t_begin
= getnanotime();
1201 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
,
1202 !s
->commit_template
))
1205 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1206 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1207 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1208 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1209 strbuf_addf(&sb
, _("\n"
1210 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1211 "You can use '--no-ahead-behind' to avoid this.\n"),
1212 t_delta_in_ms
/ 1000.0);
1216 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1217 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1219 s
->display_comment_prefix
? comment_line_str
: "",
1220 s
->display_comment_prefix
? " " : "",
1221 (int)(ep
- cp
), cp
);
1222 if (s
->display_comment_prefix
)
1223 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%s",
1227 strbuf_release(&sb
);
1230 static int uf_was_slow(struct wt_status
*s
)
1232 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1233 s
->untracked_in_ms
= 3250;
1234 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1237 static void show_merge_in_progress(struct wt_status
*s
,
1240 if (has_unmerged(s
)) {
1241 status_printf_ln(s
, color
, _("You have unmerged paths."));
1243 status_printf_ln(s
, color
,
1244 _(" (fix conflicts and run \"git commit\")"));
1245 status_printf_ln(s
, color
,
1246 _(" (use \"git merge --abort\" to abort the merge)"));
1249 status_printf_ln(s
, color
,
1250 _("All conflicts fixed but you are still merging."));
1252 status_printf_ln(s
, color
,
1253 _(" (use \"git commit\" to conclude merge)"));
1255 wt_longstatus_print_trailer(s
);
1258 static void show_am_in_progress(struct wt_status
*s
,
1263 status_printf_ln(s
, color
,
1264 _("You are in the middle of an am session."));
1265 if (s
->state
.am_empty_patch
)
1266 status_printf_ln(s
, color
,
1267 _("The current patch is empty."));
1269 am_empty_patch
= s
->state
.am_empty_patch
;
1270 if (!am_empty_patch
)
1271 status_printf_ln(s
, color
,
1272 _(" (fix conflicts and then run \"git am --continue\")"));
1273 status_printf_ln(s
, color
,
1274 _(" (use \"git am --skip\" to skip this patch)"));
1276 status_printf_ln(s
, color
,
1277 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1278 status_printf_ln(s
, color
,
1279 _(" (use \"git am --abort\" to restore the original branch)"));
1281 wt_longstatus_print_trailer(s
);
1284 static char *read_line_from_git_path(const char *filename
)
1286 struct strbuf buf
= STRBUF_INIT
;
1287 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1290 strbuf_release(&buf
);
1293 strbuf_getline_lf(&buf
, fp
);
1295 return strbuf_detach(&buf
, NULL
);
1297 strbuf_release(&buf
);
1302 static int split_commit_in_progress(struct wt_status
*s
)
1304 int split_in_progress
= 0;
1305 struct object_id head_oid
, orig_head_oid
;
1306 char *rebase_amend
, *rebase_orig_head
;
1307 int head_flags
, orig_head_flags
;
1309 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1310 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1313 if (refs_read_ref_full(get_main_ref_store(the_repository
), "HEAD", RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1314 &head_oid
, &head_flags
) ||
1315 refs_read_ref_full(get_main_ref_store(the_repository
), "ORIG_HEAD", RESOLVE_REF_READING
| RESOLVE_REF_NO_RECURSE
,
1316 &orig_head_oid
, &orig_head_flags
))
1318 if (head_flags
& REF_ISSYMREF
|| orig_head_flags
& REF_ISSYMREF
)
1321 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1322 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1324 if (!rebase_amend
|| !rebase_orig_head
)
1325 ; /* fall through, no split in progress */
1326 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1327 split_in_progress
= !!strcmp(oid_to_hex(&head_oid
), rebase_amend
);
1328 else if (strcmp(oid_to_hex(&orig_head_oid
), rebase_orig_head
))
1329 split_in_progress
= 1;
1332 free(rebase_orig_head
);
1334 return split_in_progress
;
1339 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1341 * "pick d6a2f03 some message"
1343 * The function assumes that the line does not contain useless spaces
1344 * before or after the command.
1346 static void abbrev_oid_in_line(struct strbuf
*line
)
1348 struct strbuf
**split
;
1351 if (starts_with(line
->buf
, "exec ") ||
1352 starts_with(line
->buf
, "x ") ||
1353 starts_with(line
->buf
, "label ") ||
1354 starts_with(line
->buf
, "l "))
1357 split
= strbuf_split_max(line
, ' ', 3);
1358 if (split
[0] && split
[1]) {
1359 struct object_id oid
;
1362 * strbuf_split_max left a space. Trim it and re-add
1363 * it after abbreviation.
1365 strbuf_trim(split
[1]);
1366 if (!repo_get_oid(the_repository
, split
[1]->buf
, &oid
)) {
1367 strbuf_reset(split
[1]);
1368 strbuf_add_unique_abbrev(split
[1], &oid
,
1370 strbuf_addch(split
[1], ' ');
1372 for (i
= 0; split
[i
]; i
++)
1373 strbuf_addbuf(line
, split
[i
]);
1376 strbuf_list_free(split
);
1379 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1381 struct strbuf line
= STRBUF_INIT
;
1382 FILE *f
= fopen(git_path("%s", fname
), "r");
1385 if (errno
== ENOENT
)
1387 die_errno("Could not open file %s for reading",
1388 git_path("%s", fname
));
1390 while (!strbuf_getline_lf(&line
, f
)) {
1391 if (starts_with(line
.buf
, comment_line_str
))
1396 abbrev_oid_in_line(&line
);
1397 string_list_append(lines
, line
.buf
);
1400 strbuf_release(&line
);
1404 static void show_rebase_information(struct wt_status
*s
,
1407 if (s
->state
.rebase_interactive_in_progress
) {
1409 int nr_lines_to_show
= 2;
1411 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1412 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1414 read_rebase_todolist("rebase-merge/done", &have_done
);
1415 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1417 status_printf_ln(s
, color
,
1418 _("git-rebase-todo is missing."));
1419 if (have_done
.nr
== 0)
1420 status_printf_ln(s
, color
, _("No commands done."));
1422 status_printf_ln(s
, color
,
1423 Q_("Last command done (%"PRIuMAX
" command done):",
1424 "Last commands done (%"PRIuMAX
" commands done):",
1426 (uintmax_t)have_done
.nr
);
1427 for (i
= (have_done
.nr
> nr_lines_to_show
)
1428 ? have_done
.nr
- nr_lines_to_show
: 0;
1431 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1432 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1433 status_printf_ln(s
, color
,
1434 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1437 if (yet_to_do
.nr
== 0)
1438 status_printf_ln(s
, color
,
1439 _("No commands remaining."));
1441 status_printf_ln(s
, color
,
1442 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1443 "Next commands to do (%"PRIuMAX
" remaining commands):",
1445 (uintmax_t)yet_to_do
.nr
);
1446 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1447 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1449 status_printf_ln(s
, color
,
1450 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1452 string_list_clear(&yet_to_do
, 0);
1453 string_list_clear(&have_done
, 0);
1457 static void print_rebase_state(struct wt_status
*s
,
1460 if (s
->state
.branch
)
1461 status_printf_ln(s
, color
,
1462 _("You are currently rebasing branch '%s' on '%s'."),
1466 status_printf_ln(s
, color
,
1467 _("You are currently rebasing."));
1470 static void show_rebase_in_progress(struct wt_status
*s
,
1475 show_rebase_information(s
, color
);
1476 if (has_unmerged(s
)) {
1477 print_rebase_state(s
, color
);
1479 status_printf_ln(s
, color
,
1480 _(" (fix conflicts and then run \"git rebase --continue\")"));
1481 status_printf_ln(s
, color
,
1482 _(" (use \"git rebase --skip\" to skip this patch)"));
1483 status_printf_ln(s
, color
,
1484 _(" (use \"git rebase --abort\" to check out the original branch)"));
1486 } else if (s
->state
.rebase_in_progress
||
1487 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1488 print_rebase_state(s
, color
);
1490 status_printf_ln(s
, color
,
1491 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1492 } else if (split_commit_in_progress(s
)) {
1493 if (s
->state
.branch
)
1494 status_printf_ln(s
, color
,
1495 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1499 status_printf_ln(s
, color
,
1500 _("You are currently splitting a commit during a rebase."));
1502 status_printf_ln(s
, color
,
1503 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1505 if (s
->state
.branch
)
1506 status_printf_ln(s
, color
,
1507 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1511 status_printf_ln(s
, color
,
1512 _("You are currently editing a commit during a rebase."));
1513 if (s
->hints
&& !s
->amend
) {
1514 status_printf_ln(s
, color
,
1515 _(" (use \"git commit --amend\" to amend the current commit)"));
1516 status_printf_ln(s
, color
,
1517 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1520 wt_longstatus_print_trailer(s
);
1523 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1526 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1527 status_printf_ln(s
, color
,
1528 _("Cherry-pick currently in progress."));
1530 status_printf_ln(s
, color
,
1531 _("You are currently cherry-picking commit %s."),
1532 repo_find_unique_abbrev(the_repository
, &s
->state
.cherry_pick_head_oid
,
1536 if (has_unmerged(s
))
1537 status_printf_ln(s
, color
,
1538 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1539 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1540 status_printf_ln(s
, color
,
1541 _(" (run \"git cherry-pick --continue\" to continue)"));
1543 status_printf_ln(s
, color
,
1544 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1545 status_printf_ln(s
, color
,
1546 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1547 status_printf_ln(s
, color
,
1548 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1550 wt_longstatus_print_trailer(s
);
1553 static void show_revert_in_progress(struct wt_status
*s
,
1556 if (is_null_oid(&s
->state
.revert_head_oid
))
1557 status_printf_ln(s
, color
,
1558 _("Revert currently in progress."));
1560 status_printf_ln(s
, color
,
1561 _("You are currently reverting commit %s."),
1562 repo_find_unique_abbrev(the_repository
, &s
->state
.revert_head_oid
,
1565 if (has_unmerged(s
))
1566 status_printf_ln(s
, color
,
1567 _(" (fix conflicts and run \"git revert --continue\")"));
1568 else if (is_null_oid(&s
->state
.revert_head_oid
))
1569 status_printf_ln(s
, color
,
1570 _(" (run \"git revert --continue\" to continue)"));
1572 status_printf_ln(s
, color
,
1573 _(" (all conflicts fixed: run \"git revert --continue\")"));
1574 status_printf_ln(s
, color
,
1575 _(" (use \"git revert --skip\" to skip this patch)"));
1576 status_printf_ln(s
, color
,
1577 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1579 wt_longstatus_print_trailer(s
);
1582 static void show_bisect_in_progress(struct wt_status
*s
,
1585 if (s
->state
.bisecting_from
)
1586 status_printf_ln(s
, color
,
1587 _("You are currently bisecting, started from branch '%s'."),
1588 s
->state
.bisecting_from
);
1590 status_printf_ln(s
, color
,
1591 _("You are currently bisecting."));
1593 status_printf_ln(s
, color
,
1594 _(" (use \"git bisect reset\" to get back to the original branch)"));
1595 wt_longstatus_print_trailer(s
);
1598 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1601 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1604 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1605 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1607 status_printf_ln(s
, color
,
1608 _("You are in a sparse checkout with %d%% of tracked files present."),
1609 s
->state
.sparse_checkout_percentage
);
1610 wt_longstatus_print_trailer(s
);
1614 * Extract branch information from rebase/bisect
1616 static char *get_branch(const struct worktree
*wt
, const char *path
)
1618 struct strbuf sb
= STRBUF_INIT
;
1619 struct object_id oid
;
1620 const char *branch_name
;
1622 if (strbuf_read_file(&sb
, worktree_git_path(the_repository
, wt
, "%s", path
), 0) <= 0)
1625 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1626 strbuf_setlen(&sb
, sb
.len
- 1);
1629 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1630 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1631 else if (starts_with(sb
.buf
, "refs/"))
1633 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1635 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1636 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1640 return strbuf_detach(&sb
, NULL
);
1643 strbuf_release(&sb
);
1647 struct grab_1st_switch_cbdata
{
1649 struct object_id noid
;
1652 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1653 struct object_id
*noid
,
1654 const char *email UNUSED
,
1655 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1656 const char *message
, void *cb_data
)
1658 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1659 const char *target
= NULL
, *end
;
1661 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1663 target
= strstr(message
, " to ");
1666 target
+= strlen(" to ");
1667 strbuf_reset(&cb
->buf
);
1668 oidcpy(&cb
->noid
, noid
);
1669 end
= strchrnul(target
, '\n');
1670 strbuf_add(&cb
->buf
, target
, end
- target
);
1671 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1672 /* HEAD is relative. Resolve it to the right reflog entry. */
1673 strbuf_reset(&cb
->buf
);
1674 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1679 static void wt_status_get_detached_from(struct repository
*r
,
1680 struct wt_status_state
*state
)
1682 struct grab_1st_switch_cbdata cb
;
1683 struct commit
*commit
;
1684 struct object_id oid
;
1687 strbuf_init(&cb
.buf
, 0);
1688 if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository
), "HEAD", grab_1st_switch
, &cb
) <= 0) {
1689 strbuf_release(&cb
.buf
);
1693 if (repo_dwim_ref(r
, cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
,
1695 /* oid is a commit? match without further lookup */
1696 (oideq(&cb
.noid
, &oid
) ||
1697 /* perhaps oid is a tag, try to dereference to a commit */
1698 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1699 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1700 const char *from
= ref
;
1701 if (!skip_prefix(from
, "refs/tags/", &from
))
1702 skip_prefix(from
, "refs/remotes/", &from
);
1703 state
->detached_from
= xstrdup(from
);
1705 state
->detached_from
=
1706 xstrdup(repo_find_unique_abbrev(r
, &cb
.noid
, DEFAULT_ABBREV
));
1707 oidcpy(&state
->detached_oid
, &cb
.noid
);
1708 state
->detached_at
= !repo_get_oid(r
, "HEAD", &oid
) &&
1709 oideq(&oid
, &state
->detached_oid
);
1712 strbuf_release(&cb
.buf
);
1715 int wt_status_check_rebase(const struct worktree
*wt
,
1716 struct wt_status_state
*state
)
1720 if (!stat(worktree_git_path(the_repository
, wt
, "rebase-apply"), &st
)) {
1721 if (!stat(worktree_git_path(the_repository
, wt
, "rebase-apply/applying"), &st
)) {
1722 state
->am_in_progress
= 1;
1723 if (!stat(worktree_git_path(the_repository
, wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1724 state
->am_empty_patch
= 1;
1726 state
->rebase_in_progress
= 1;
1727 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1728 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1730 } else if (!stat(worktree_git_path(the_repository
, wt
, "rebase-merge"), &st
)) {
1731 if (!stat(worktree_git_path(the_repository
, wt
, "rebase-merge/interactive"), &st
))
1732 state
->rebase_interactive_in_progress
= 1;
1734 state
->rebase_in_progress
= 1;
1735 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1736 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1742 int wt_status_check_bisect(const struct worktree
*wt
,
1743 struct wt_status_state
*state
)
1747 if (!stat(worktree_git_path(the_repository
, wt
, "BISECT_LOG"), &st
)) {
1748 state
->bisect_in_progress
= 1;
1749 state
->bisecting_from
= get_branch(wt
, "BISECT_START");
1755 static void wt_status_check_sparse_checkout(struct repository
*r
,
1756 struct wt_status_state
*state
)
1758 int skip_worktree
= 0;
1761 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1763 * Don't compute percentage of checked out files if we
1764 * aren't in a sparse checkout or would get division by 0.
1766 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1770 if (r
->index
->sparse_index
) {
1771 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1775 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1776 struct cache_entry
*ce
= r
->index
->cache
[i
];
1777 if (ce_skip_worktree(ce
))
1781 state
->sparse_checkout_percentage
=
1782 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1785 void wt_status_get_state(struct repository
*r
,
1786 struct wt_status_state
*state
,
1787 int get_detached_from
)
1790 struct object_id oid
;
1791 enum replay_action action
;
1793 if (!stat(git_path_merge_head(r
), &st
)) {
1794 wt_status_check_rebase(NULL
, state
);
1795 state
->merge_in_progress
= 1;
1796 } else if (wt_status_check_rebase(NULL
, state
)) {
1798 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1799 !repo_get_oid(r
, "CHERRY_PICK_HEAD", &oid
)) {
1800 state
->cherry_pick_in_progress
= 1;
1801 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1803 wt_status_check_bisect(NULL
, state
);
1804 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1805 !repo_get_oid(r
, "REVERT_HEAD", &oid
)) {
1806 state
->revert_in_progress
= 1;
1807 oidcpy(&state
->revert_head_oid
, &oid
);
1809 if (!sequencer_get_last_command(r
, &action
)) {
1810 if (action
== REPLAY_PICK
&& !state
->cherry_pick_in_progress
) {
1811 state
->cherry_pick_in_progress
= 1;
1812 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1813 } else if (action
== REPLAY_REVERT
&& !state
->revert_in_progress
) {
1814 state
->revert_in_progress
= 1;
1815 oidcpy(&state
->revert_head_oid
, null_oid());
1818 if (get_detached_from
)
1819 wt_status_get_detached_from(r
, state
);
1820 wt_status_check_sparse_checkout(r
, state
);
1823 static void wt_longstatus_print_state(struct wt_status
*s
)
1825 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1826 struct wt_status_state
*state
= &s
->state
;
1828 if (state
->merge_in_progress
) {
1829 if (state
->rebase_interactive_in_progress
) {
1830 show_rebase_information(s
, state_color
);
1833 show_merge_in_progress(s
, state_color
);
1834 } else if (state
->am_in_progress
)
1835 show_am_in_progress(s
, state_color
);
1836 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1837 show_rebase_in_progress(s
, state_color
);
1838 else if (state
->cherry_pick_in_progress
)
1839 show_cherry_pick_in_progress(s
, state_color
);
1840 else if (state
->revert_in_progress
)
1841 show_revert_in_progress(s
, state_color
);
1842 if (state
->bisect_in_progress
)
1843 show_bisect_in_progress(s
, state_color
);
1845 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1846 show_sparse_checkout_in_use(s
, state_color
);
1849 static void wt_longstatus_print(struct wt_status
*s
)
1851 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1852 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1853 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1856 const char *on_what
= _("On branch ");
1857 const char *branch_name
= s
->branch
;
1858 if (!strcmp(branch_name
, "HEAD")) {
1859 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1860 if (s
->state
.rebase_in_progress
||
1861 s
->state
.rebase_interactive_in_progress
) {
1862 if (s
->state
.rebase_interactive_in_progress
)
1863 on_what
= _("interactive rebase in progress; onto ");
1865 on_what
= _("rebase in progress; onto ");
1866 branch_name
= s
->state
.onto
;
1867 } else if (s
->state
.detached_from
) {
1868 branch_name
= s
->state
.detached_from
;
1869 if (s
->state
.detached_at
)
1870 on_what
= _("HEAD detached at ");
1872 on_what
= _("HEAD detached from ");
1875 on_what
= _("Not currently on any branch.");
1878 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1879 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1880 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1881 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1883 wt_longstatus_print_tracking(s
);
1886 wt_longstatus_print_state(s
);
1888 if (s
->is_initial
) {
1889 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1890 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1892 ? _("Initial commit")
1893 : _("No commits yet"));
1894 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1897 wt_longstatus_print_updated(s
);
1898 wt_longstatus_print_unmerged(s
);
1899 wt_longstatus_print_changed(s
);
1900 if (s
->submodule_summary
&&
1901 (!s
->ignore_submodule_arg
||
1902 strcmp(s
->ignore_submodule_arg
, "all"))) {
1903 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1904 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1906 if (s
->show_untracked_files
) {
1907 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1908 if (s
->show_ignored_mode
)
1909 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1910 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1911 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1912 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1913 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1914 _("It took %.2f seconds to enumerate untracked files,\n"
1915 "but the results were cached, and subsequent runs may be faster."),
1916 s
->untracked_in_ms
/ 1000.0);
1918 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1919 _("It took %.2f seconds to enumerate untracked files."),
1920 s
->untracked_in_ms
/ 1000.0);
1922 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1923 _("See 'git help status' for information on how to improve this."));
1924 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1926 } else if (s
->committable
)
1927 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1929 ? _(" (use -u option to show untracked files)") : "");
1932 wt_longstatus_print_verbose(s
);
1933 if (!s
->committable
) {
1935 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1938 else if (s
->workdir_dirty
) {
1940 fprintf(s
->fp
, _("no changes added to commit "
1941 "(use \"git add\" and/or "
1942 "\"git commit -a\")\n"));
1944 fprintf(s
->fp
, _("no changes added to "
1946 } else if (s
->untracked
.nr
) {
1948 fprintf(s
->fp
, _("nothing added to commit but "
1949 "untracked files present (use "
1950 "\"git add\" to track)\n"));
1952 fprintf(s
->fp
, _("nothing added to commit but "
1953 "untracked files present\n"));
1954 } else if (s
->is_initial
) {
1956 fprintf(s
->fp
, _("nothing to commit (create/"
1957 "copy files and use \"git "
1958 "add\" to track)\n"));
1960 fprintf(s
->fp
, _("nothing to commit\n"));
1961 } else if (!s
->show_untracked_files
) {
1963 fprintf(s
->fp
, _("nothing to commit (use -u to "
1964 "show untracked files)\n"));
1966 fprintf(s
->fp
, _("nothing to commit\n"));
1968 fprintf(s
->fp
, _("nothing to commit, working tree "
1972 wt_longstatus_print_stash_summary(s
);
1975 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1976 struct wt_status
*s
)
1978 struct wt_status_change_data
*d
= it
->util
;
1979 const char *how
= "??";
1981 switch (d
->stagemask
) {
1982 case 1: how
= "DD"; break; /* both deleted */
1983 case 2: how
= "AU"; break; /* added by us */
1984 case 3: how
= "UD"; break; /* deleted by them */
1985 case 4: how
= "UA"; break; /* added by them */
1986 case 5: how
= "DU"; break; /* deleted by us */
1987 case 6: how
= "AA"; break; /* both added */
1988 case 7: how
= "UU"; break; /* both modified */
1990 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1991 if (s
->null_termination
) {
1992 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1994 struct strbuf onebuf
= STRBUF_INIT
;
1996 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1997 fprintf(s
->fp
, " %s\n", one
);
1998 strbuf_release(&onebuf
);
2002 static void wt_shortstatus_status(struct string_list_item
*it
,
2003 struct wt_status
*s
)
2005 struct wt_status_change_data
*d
= it
->util
;
2007 if (d
->index_status
)
2008 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
2011 if (d
->worktree_status
)
2012 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
2016 if (s
->null_termination
) {
2017 fprintf(s
->fp
, "%s%c", it
->string
, 0);
2018 if (d
->rename_source
)
2019 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
2021 struct strbuf onebuf
= STRBUF_INIT
;
2024 if (d
->rename_source
) {
2025 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2026 QUOTE_PATH_QUOTE_SP
);
2027 fprintf(s
->fp
, "%s -> ", one
);
2028 strbuf_release(&onebuf
);
2030 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2031 fprintf(s
->fp
, "%s\n", one
);
2032 strbuf_release(&onebuf
);
2036 static void wt_shortstatus_other(struct string_list_item
*it
,
2037 struct wt_status
*s
, const char *sign
)
2039 if (s
->null_termination
) {
2040 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2042 struct strbuf onebuf
= STRBUF_INIT
;
2044 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2045 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2046 fprintf(s
->fp
, " %s\n", one
);
2047 strbuf_release(&onebuf
);
2051 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2053 struct branch
*branch
;
2054 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2055 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2056 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2060 const char *branch_name
;
2061 int num_ours
, num_theirs
, sti
;
2062 int upstream_is_gone
= 0;
2064 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2068 branch_name
= s
->branch
;
2070 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2073 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2075 if (!strcmp(s
->branch
, "HEAD")) {
2076 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2077 LABEL(N_("HEAD (no branch)")));
2081 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2083 branch
= branch_get(branch_name
);
2085 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2087 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2088 0, s
->ahead_behind_flags
);
2093 upstream_is_gone
= 1;
2096 short_base
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2098 color_fprintf(s
->fp
, header_color
, "...");
2099 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2102 if (!upstream_is_gone
&& !sti
)
2105 color_fprintf(s
->fp
, header_color
, " [");
2106 if (upstream_is_gone
) {
2107 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2108 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2109 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2110 } else if (!num_ours
) {
2111 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2112 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2113 } else if (!num_theirs
) {
2114 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2115 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2117 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2118 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2119 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2120 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2123 color_fprintf(s
->fp
, header_color
, "]");
2125 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2128 static void wt_shortstatus_print(struct wt_status
*s
)
2130 struct string_list_item
*it
;
2133 wt_shortstatus_print_tracking(s
);
2135 for_each_string_list_item(it
, &s
->change
) {
2136 struct wt_status_change_data
*d
= it
->util
;
2139 wt_shortstatus_unmerged(it
, s
);
2141 wt_shortstatus_status(it
, s
);
2143 for_each_string_list_item(it
, &s
->untracked
)
2144 wt_shortstatus_other(it
, s
, "??");
2146 for_each_string_list_item(it
, &s
->ignored
)
2147 wt_shortstatus_other(it
, s
, "!!");
2150 static void wt_porcelain_print(struct wt_status
*s
)
2153 s
->relative_paths
= 0;
2156 wt_shortstatus_print(s
);
2160 * Print branch information for porcelain v2 output. These lines
2161 * are printed when the '--branch' parameter is given.
2163 * # branch.oid <commit><eol>
2164 * # branch.head <head><eol>
2165 * [# branch.upstream <upstream><eol>
2166 * [# branch.ab +<ahead> -<behind><eol>]]
2168 * <commit> ::= the current commit hash or the literal
2169 * "(initial)" to indicate an initialized repo
2172 * <head> ::= <branch_name> the current branch name or
2173 * "(detached)" literal when detached head or
2174 * "(unknown)" when something is wrong.
2176 * <upstream> ::= the upstream branch name, when set.
2178 * <ahead> ::= integer ahead value or '?'.
2180 * <behind> ::= integer behind value or '?'.
2182 * The end-of-line is defined by the -z flag.
2184 * <eol> ::= NUL when -z,
2187 * When an upstream is set and present, the 'branch.ab' line will
2188 * be printed with the ahead/behind counts for the branch and the
2189 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2190 * are different, '?' will be substituted for the actual count.
2192 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2194 struct branch
*branch
;
2196 const char *branch_name
;
2197 int ab_info
, nr_ahead
, nr_behind
;
2198 char eol
= s
->null_termination
? '\0' : '\n';
2200 fprintf(s
->fp
, "# branch.oid %s%c",
2201 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2205 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2207 if (!strcmp(s
->branch
, "HEAD")) {
2208 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2210 if (s
->state
.rebase_in_progress
||
2211 s
->state
.rebase_interactive_in_progress
)
2212 branch_name
= s
->state
.onto
;
2213 else if (s
->state
.detached_from
)
2214 branch_name
= s
->state
.detached_from
;
2219 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2221 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2224 /* Lookup stats on the upstream tracking branch, if set. */
2225 branch
= branch_get(branch_name
);
2227 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2228 &base
, 0, s
->ahead_behind_flags
);
2230 base
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
2232 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2237 if (nr_ahead
|| nr_behind
)
2238 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2239 nr_ahead
, nr_behind
, eol
);
2241 fprintf(s
->fp
, "# branch.ab +? -?%c",
2243 } else if (!ab_info
) {
2245 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2252 * Print the stash count in a porcelain-friendly format
2254 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2256 int stash_count
= count_stash_entries();
2257 char eol
= s
->null_termination
? '\0' : '\n';
2259 if (stash_count
> 0)
2260 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2264 * Convert various submodule status values into a
2265 * fixed-length string of characters in the buffer provided.
2267 static void wt_porcelain_v2_submodule_state(
2268 struct wt_status_change_data
*d
,
2271 if (S_ISGITLINK(d
->mode_head
) ||
2272 S_ISGITLINK(d
->mode_index
) ||
2273 S_ISGITLINK(d
->mode_worktree
)) {
2275 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2276 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2277 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2288 * Fix-up changed entries before we print them.
2290 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2292 struct wt_status_change_data
*d
= it
->util
;
2294 if (!d
->index_status
) {
2296 * This entry is unchanged in the index (relative to the head).
2297 * Therefore, the collect_updated_cb was never called for this
2298 * entry (during the head-vs-index scan) and so the head column
2299 * fields were never set.
2301 * We must have data for the index column (from the
2302 * index-vs-worktree scan (otherwise, this entry should not be
2303 * in the list of changes)).
2305 * Copy index column fields to the head column, so that our
2306 * output looks complete.
2308 assert(d
->mode_head
== 0);
2309 d
->mode_head
= d
->mode_index
;
2310 oidcpy(&d
->oid_head
, &d
->oid_index
);
2313 if (!d
->worktree_status
) {
2315 * This entry is unchanged in the worktree (relative to the index).
2316 * Therefore, the collect_changed_cb was never called for this entry
2317 * (during the index-vs-worktree scan) and so the worktree column
2318 * fields were never set.
2320 * We must have data for the index column (from the head-vs-index
2323 * Copy the index column fields to the worktree column so that
2324 * our output looks complete.
2326 * Note that we only have a mode field in the worktree column
2327 * because the scan code tries really hard to not have to compute it.
2329 assert(d
->mode_worktree
== 0);
2330 d
->mode_worktree
= d
->mode_index
;
2335 * Print porcelain v2 info for tracked entries with changes.
2337 static void wt_porcelain_v2_print_changed_entry(
2338 struct string_list_item
*it
,
2339 struct wt_status
*s
)
2341 struct wt_status_change_data
*d
= it
->util
;
2342 struct strbuf buf
= STRBUF_INIT
;
2343 struct strbuf buf_from
= STRBUF_INIT
;
2344 const char *path
= NULL
;
2345 const char *path_from
= NULL
;
2347 char submodule_token
[5];
2348 char sep_char
, eol_char
;
2350 wt_porcelain_v2_fix_up_changed(it
);
2351 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2353 key
[0] = d
->index_status
? d
->index_status
: '.';
2354 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2357 if (s
->null_termination
) {
2359 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2360 * A single NUL character separates them.
2365 path_from
= d
->rename_source
;
2368 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2369 * The source path is only present when necessary.
2370 * A single TAB separates them (because paths can contain spaces
2371 * which are not escaped and C-quoting does escape TAB characters).
2375 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2376 if (d
->rename_source
)
2377 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2381 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2382 key
, submodule_token
,
2383 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2384 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2385 d
->rename_status
, d
->rename_score
,
2386 path
, sep_char
, path_from
, eol_char
);
2388 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2389 key
, submodule_token
,
2390 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2391 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2394 strbuf_release(&buf
);
2395 strbuf_release(&buf_from
);
2399 * Print porcelain v2 status info for unmerged entries.
2401 static void wt_porcelain_v2_print_unmerged_entry(
2402 struct string_list_item
*it
,
2403 struct wt_status
*s
)
2405 struct wt_status_change_data
*d
= it
->util
;
2406 struct index_state
*istate
= s
->repo
->index
;
2407 const struct cache_entry
*ce
;
2408 struct strbuf buf_index
= STRBUF_INIT
;
2409 const char *path_index
= NULL
;
2410 int pos
, stage
, sum
;
2413 struct object_id oid
;
2416 char submodule_token
[5];
2417 char unmerged_prefix
= 'u';
2418 char eol_char
= s
->null_termination
? '\0' : '\n';
2420 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2422 switch (d
->stagemask
) {
2423 case 1: key
= "DD"; break; /* both deleted */
2424 case 2: key
= "AU"; break; /* added by us */
2425 case 3: key
= "UD"; break; /* deleted by them */
2426 case 4: key
= "UA"; break; /* added by them */
2427 case 5: key
= "DU"; break; /* deleted by us */
2428 case 6: key
= "AA"; break; /* both added */
2429 case 7: key
= "UU"; break; /* both modified */
2431 BUG("unhandled unmerged status %x", d
->stagemask
);
2435 * Disregard d.aux.porcelain_v2 data that we accumulated
2436 * for the head and index columns during the scans and
2437 * replace with the actual stage data.
2439 * Note that this is a last-one-wins for each the individual
2440 * stage [123] columns in the event of multiple cache entries
2443 memset(stages
, 0, sizeof(stages
));
2445 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2448 while (pos
< istate
->cache_nr
) {
2449 ce
= istate
->cache
[pos
++];
2450 stage
= ce_stage(ce
);
2451 if (strcmp(ce
->name
, it
->string
) || !stage
)
2453 stages
[stage
- 1].mode
= ce
->ce_mode
;
2454 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2455 sum
|= (1 << (stage
- 1));
2457 if (sum
!= d
->stagemask
)
2458 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2460 if (s
->null_termination
)
2461 path_index
= it
->string
;
2463 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2465 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2466 unmerged_prefix
, key
, submodule_token
,
2467 stages
[0].mode
, /* stage 1 */
2468 stages
[1].mode
, /* stage 2 */
2469 stages
[2].mode
, /* stage 3 */
2471 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2472 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2473 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2477 strbuf_release(&buf_index
);
2481 * Print porcelain V2 status info for untracked and ignored entries.
2483 static void wt_porcelain_v2_print_other(
2484 struct string_list_item
*it
,
2485 struct wt_status
*s
,
2488 struct strbuf buf
= STRBUF_INIT
;
2492 if (s
->null_termination
) {
2496 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2500 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2502 strbuf_release(&buf
);
2506 * Print porcelain V2 status.
2509 * [<v2_changed_items>]*
2510 * [<v2_unmerged_items>]*
2511 * [<v2_untracked_items>]*
2512 * [<v2_ignored_items>]*
2515 static void wt_porcelain_v2_print(struct wt_status
*s
)
2517 struct wt_status_change_data
*d
;
2518 struct string_list_item
*it
;
2522 wt_porcelain_v2_print_tracking(s
);
2525 wt_porcelain_v2_print_stash(s
);
2527 for (i
= 0; i
< s
->change
.nr
; i
++) {
2528 it
= &(s
->change
.items
[i
]);
2531 wt_porcelain_v2_print_changed_entry(it
, s
);
2534 for (i
= 0; i
< s
->change
.nr
; i
++) {
2535 it
= &(s
->change
.items
[i
]);
2538 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2541 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2542 it
= &(s
->untracked
.items
[i
]);
2543 wt_porcelain_v2_print_other(it
, s
, '?');
2546 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2547 it
= &(s
->ignored
.items
[i
]);
2548 wt_porcelain_v2_print_other(it
, s
, '!');
2552 void wt_status_print(struct wt_status
*s
)
2554 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2555 trace2_data_intmax("status", s
->repo
, "count/untracked",
2557 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2559 trace2_region_enter("status", "print", s
->repo
);
2561 switch (s
->status_format
) {
2562 case STATUS_FORMAT_SHORT
:
2563 wt_shortstatus_print(s
);
2565 case STATUS_FORMAT_PORCELAIN
:
2566 wt_porcelain_print(s
);
2568 case STATUS_FORMAT_PORCELAIN_V2
:
2569 wt_porcelain_v2_print(s
);
2571 case STATUS_FORMAT_UNSPECIFIED
:
2572 BUG("finalize_deferred_config() should have been called");
2574 case STATUS_FORMAT_NONE
:
2575 case STATUS_FORMAT_LONG
:
2576 wt_longstatus_print(s
);
2580 trace2_region_leave("status", "print", s
->repo
);
2584 * Returns 1 if there are unstaged changes, 0 otherwise.
2586 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2588 struct rev_info rev_info
;
2591 repo_init_revisions(r
, &rev_info
, NULL
);
2592 if (ignore_submodules
) {
2593 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2594 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2596 rev_info
.diffopt
.flags
.quick
= 1;
2597 diff_setup_done(&rev_info
.diffopt
);
2598 run_diff_files(&rev_info
, 0);
2599 result
= diff_result_code(&rev_info
);
2600 release_revisions(&rev_info
);
2605 * Returns 1 if there are uncommitted changes, 0 otherwise.
2607 int has_uncommitted_changes(struct repository
*r
,
2608 int ignore_submodules
)
2610 struct rev_info rev_info
;
2613 if (is_index_unborn(r
->index
))
2616 repo_init_revisions(r
, &rev_info
, NULL
);
2617 if (ignore_submodules
)
2618 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2619 rev_info
.diffopt
.flags
.quick
= 1;
2621 add_head_to_pending(&rev_info
);
2622 if (!rev_info
.pending
.nr
) {
2624 * We have no head (or it's corrupt); use the empty tree,
2625 * which will complain if the index is non-empty.
2627 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2628 add_pending_object(&rev_info
, &tree
->object
, "");
2631 diff_setup_done(&rev_info
.diffopt
);
2632 run_diff_index(&rev_info
, DIFF_INDEX_CACHED
);
2633 result
= diff_result_code(&rev_info
);
2634 release_revisions(&rev_info
);
2639 * If the work tree has unstaged or uncommitted changes, dies with the
2640 * appropriate message.
2642 int require_clean_work_tree(struct repository
*r
,
2645 int ignore_submodules
,
2648 struct lock_file lock_file
= LOCK_INIT
;
2651 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2652 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2654 repo_update_index_if_able(r
, &lock_file
);
2655 rollback_lock_file(&lock_file
);
2657 if (has_unstaged_changes(r
, ignore_submodules
)) {
2658 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2659 error(_("cannot %s: You have unstaged changes."), _(action
));
2663 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2665 error(_("additionally, your index contains uncommitted changes."));
2667 error(_("cannot %s: Your index contains uncommitted changes."),
2675 BUG("empty hint passed to require_clean_work_tree();"
2676 " use NULL instead");