12 #include "run-command.h"
16 #include "submodule.h"
22 #include "sequencer.h"
23 #include "fsmonitor-settings.h"
25 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
26 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
28 static const char cut_line
[] =
29 "------------------------ >8 ------------------------\n";
31 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
32 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
33 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
34 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
35 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
36 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
37 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
38 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
39 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
40 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
43 static const char *color(int slot
, struct wt_status
*s
)
46 if (want_color(s
->use_color
))
47 c
= s
->color_palette
[slot
];
48 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
49 c
= s
->color_palette
[WT_STATUS_HEADER
];
53 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
54 const char *fmt
, va_list ap
, const char *trail
)
56 struct strbuf sb
= STRBUF_INIT
;
57 struct strbuf linebuf
= STRBUF_INIT
;
58 const char *line
, *eol
;
60 strbuf_vaddf(&sb
, fmt
, ap
);
62 if (s
->display_comment_prefix
) {
63 strbuf_addch(&sb
, comment_line_char
);
65 strbuf_addch(&sb
, ' ');
67 color_print_strbuf(s
->fp
, color
, &sb
);
69 fprintf(s
->fp
, "%s", trail
);
73 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
74 eol
= strchr(line
, '\n');
76 strbuf_reset(&linebuf
);
77 if (at_bol
&& s
->display_comment_prefix
) {
78 strbuf_addch(&linebuf
, comment_line_char
);
79 if (*line
!= '\n' && *line
!= '\t')
80 strbuf_addch(&linebuf
, ' ');
83 strbuf_add(&linebuf
, line
, eol
- line
);
85 strbuf_addstr(&linebuf
, line
);
86 color_print_strbuf(s
->fp
, color
, &linebuf
);
94 fprintf(s
->fp
, "%s", trail
);
95 strbuf_release(&linebuf
);
99 void status_printf_ln(struct wt_status
*s
, const char *color
,
100 const char *fmt
, ...)
105 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
109 void status_printf(struct wt_status
*s
, const char *color
,
110 const char *fmt
, ...)
115 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
119 static void status_printf_more(struct wt_status
*s
, const char *color
,
120 const char *fmt
, ...)
125 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
129 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
131 memset(s
, 0, sizeof(*s
));
133 memcpy(s
->color_palette
, default_wt_status_colors
,
134 sizeof(default_wt_status_colors
));
135 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
137 s
->relative_paths
= 1;
138 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
139 s
->reference
= "HEAD";
141 s
->index_file
= get_index_file();
142 s
->change
.strdup_strings
= 1;
143 s
->untracked
.strdup_strings
= 1;
144 s
->ignored
.strdup_strings
= 1;
145 s
->show_branch
= -1; /* unspecified */
147 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
148 s
->display_comment_prefix
= 0;
149 s
->detect_rename
= -1;
150 s
->rename_score
= -1;
151 s
->rename_limit
= -1;
154 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
157 int del_mod_conflict
= 0;
158 int both_deleted
= 0;
160 const char *c
= color(WT_STATUS_HEADER
, s
);
162 status_printf_ln(s
, c
, _("Unmerged paths:"));
164 for (i
= 0; i
< s
->change
.nr
; i
++) {
165 struct string_list_item
*it
= &(s
->change
.items
[i
]);
166 struct wt_status_change_data
*d
= it
->util
;
168 switch (d
->stagemask
) {
176 del_mod_conflict
= 1;
186 if (s
->whence
!= FROM_COMMIT
)
188 else if (!s
->is_initial
) {
189 if (!strcmp(s
->reference
, "HEAD"))
190 status_printf_ln(s
, c
,
191 _(" (use \"git restore --staged <file>...\" to unstage)"));
193 status_printf_ln(s
, c
,
194 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
197 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
200 if (!del_mod_conflict
)
201 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
203 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
204 } else if (!del_mod_conflict
&& !not_deleted
) {
205 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
207 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
211 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
213 const char *c
= color(WT_STATUS_HEADER
, s
);
215 status_printf_ln(s
, c
, _("Changes to be committed:"));
218 if (s
->whence
!= FROM_COMMIT
)
219 ; /* NEEDSWORK: use "git reset --unresolve"??? */
220 else if (!s
->is_initial
) {
221 if (!strcmp(s
->reference
, "HEAD"))
222 status_printf_ln(s
, c
223 , _(" (use \"git restore --staged <file>...\" to unstage)"));
225 status_printf_ln(s
, c
,
226 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
229 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
232 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
234 int has_dirty_submodules
)
236 const char *c
= color(WT_STATUS_HEADER
, s
);
238 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
242 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
244 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
245 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
246 if (has_dirty_submodules
)
247 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
250 static void wt_longstatus_print_other_header(struct wt_status
*s
,
254 const char *c
= color(WT_STATUS_HEADER
, s
);
255 status_printf_ln(s
, c
, "%s:", what
);
258 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
261 static void wt_longstatus_print_trailer(struct wt_status
*s
)
263 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
266 static const char *wt_status_unmerged_status_string(int stagemask
)
270 return _("both deleted:");
272 return _("added by us:");
274 return _("deleted by them:");
276 return _("added by them:");
278 return _("deleted by us:");
280 return _("both added:");
282 return _("both modified:");
284 BUG("unhandled unmerged status %x", stagemask
);
288 static const char *wt_status_diff_status_string(int status
)
291 case DIFF_STATUS_ADDED
:
292 return _("new file:");
293 case DIFF_STATUS_COPIED
:
295 case DIFF_STATUS_DELETED
:
296 return _("deleted:");
297 case DIFF_STATUS_MODIFIED
:
298 return _("modified:");
299 case DIFF_STATUS_RENAMED
:
300 return _("renamed:");
301 case DIFF_STATUS_TYPE_CHANGED
:
302 return _("typechange:");
303 case DIFF_STATUS_UNKNOWN
:
304 return _("unknown:");
305 case DIFF_STATUS_UNMERGED
:
306 return _("unmerged:");
312 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
316 for (i
= minval
; i
<= maxval
; i
++) {
317 const char *s
= label(i
);
318 int len
= s
? utf8_strwidth(s
) : 0;
325 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
326 struct string_list_item
*it
)
328 const char *c
= color(WT_STATUS_UNMERGED
, s
);
329 struct wt_status_change_data
*d
= it
->util
;
330 struct strbuf onebuf
= STRBUF_INIT
;
331 static char *padding
;
332 static int label_width
;
333 const char *one
, *how
;
337 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
338 label_width
+= strlen(" ");
339 padding
= xmallocz(label_width
);
340 memset(padding
, ' ', label_width
);
343 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
344 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
346 how
= wt_status_unmerged_status_string(d
->stagemask
);
347 len
= label_width
- utf8_strwidth(how
);
348 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
349 strbuf_release(&onebuf
);
352 static void wt_longstatus_print_change_data(struct wt_status
*s
,
354 struct string_list_item
*it
)
356 struct wt_status_change_data
*d
= it
->util
;
357 const char *c
= color(change_type
, s
);
361 const char *one
, *two
;
362 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
363 struct strbuf extra
= STRBUF_INIT
;
364 static char *padding
;
365 static int label_width
;
370 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
371 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
372 label_width
+= strlen(" ");
373 padding
= xmallocz(label_width
);
374 memset(padding
, ' ', label_width
);
377 one_name
= two_name
= it
->string
;
378 switch (change_type
) {
379 case WT_STATUS_UPDATED
:
380 status
= d
->index_status
;
382 case WT_STATUS_CHANGED
:
383 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
384 strbuf_addstr(&extra
, " (");
385 if (d
->new_submodule_commits
)
386 strbuf_addstr(&extra
, _("new commits, "));
387 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
388 strbuf_addstr(&extra
, _("modified content, "));
389 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
390 strbuf_addstr(&extra
, _("untracked content, "));
391 strbuf_setlen(&extra
, extra
.len
- 2);
392 strbuf_addch(&extra
, ')');
394 status
= d
->worktree_status
;
397 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
402 * Only pick up the rename it's relevant. If the rename is for
403 * the changed section and we're printing the updated section,
406 if (d
->rename_status
== status
)
407 one_name
= d
->rename_source
;
409 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
410 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
412 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
413 what
= wt_status_diff_status_string(status
);
415 BUG("unhandled diff status %c", status
);
416 len
= label_width
- utf8_strwidth(what
);
418 if (one_name
!= two_name
)
419 status_printf_more(s
, c
, "%s%.*s%s -> %s",
420 what
, len
, padding
, one
, two
);
422 status_printf_more(s
, c
, "%s%.*s%s",
423 what
, len
, padding
, one
);
425 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
426 strbuf_release(&extra
);
428 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
429 strbuf_release(&onebuf
);
430 strbuf_release(&twobuf
);
433 static char short_submodule_status(struct wt_status_change_data
*d
)
435 if (d
->new_submodule_commits
)
437 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
439 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
441 return d
->worktree_status
;
444 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
445 struct diff_options
*options UNUSED
,
448 struct wt_status
*s
= data
;
453 s
->workdir_dirty
= 1;
454 for (i
= 0; i
< q
->nr
; i
++) {
455 struct diff_filepair
*p
;
456 struct string_list_item
*it
;
457 struct wt_status_change_data
*d
;
460 it
= string_list_insert(&s
->change
, p
->two
->path
);
466 if (!d
->worktree_status
)
467 d
->worktree_status
= p
->status
;
468 if (S_ISGITLINK(p
->two
->mode
)) {
469 d
->dirty_submodule
= p
->two
->dirty_submodule
;
470 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
472 if (s
->status_format
== STATUS_FORMAT_SHORT
)
473 d
->worktree_status
= short_submodule_status(d
);
477 case DIFF_STATUS_ADDED
:
478 d
->mode_worktree
= p
->two
->mode
;
481 case DIFF_STATUS_DELETED
:
482 d
->mode_index
= p
->one
->mode
;
483 oidcpy(&d
->oid_index
, &p
->one
->oid
);
484 /* mode_worktree is zero for a delete. */
487 case DIFF_STATUS_COPIED
:
488 case DIFF_STATUS_RENAMED
:
489 if (d
->rename_status
)
490 BUG("multiple renames on the same target? how?");
491 d
->rename_source
= xstrdup(p
->one
->path
);
492 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
493 d
->rename_status
= p
->status
;
495 case DIFF_STATUS_MODIFIED
:
496 case DIFF_STATUS_TYPE_CHANGED
:
497 case DIFF_STATUS_UNMERGED
:
498 d
->mode_index
= p
->one
->mode
;
499 d
->mode_worktree
= p
->two
->mode
;
500 oidcpy(&d
->oid_index
, &p
->one
->oid
);
504 BUG("unhandled diff-files status '%c'", p
->status
);
511 static int unmerged_mask(struct index_state
*istate
, const char *path
)
514 const struct cache_entry
*ce
;
516 pos
= index_name_pos(istate
, path
, strlen(path
));
522 while (pos
< istate
->cache_nr
) {
523 ce
= istate
->cache
[pos
++];
524 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
526 mask
|= (1 << (ce_stage(ce
) - 1));
531 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
532 struct diff_options
*options UNUSED
,
535 struct wt_status
*s
= data
;
538 for (i
= 0; i
< q
->nr
; i
++) {
539 struct diff_filepair
*p
;
540 struct string_list_item
*it
;
541 struct wt_status_change_data
*d
;
544 it
= string_list_insert(&s
->change
, p
->two
->path
);
550 if (!d
->index_status
)
551 d
->index_status
= p
->status
;
553 case DIFF_STATUS_ADDED
:
554 /* Leave {mode,oid}_head zero for an add. */
555 d
->mode_index
= p
->two
->mode
;
556 oidcpy(&d
->oid_index
, &p
->two
->oid
);
559 case DIFF_STATUS_DELETED
:
560 d
->mode_head
= p
->one
->mode
;
561 oidcpy(&d
->oid_head
, &p
->one
->oid
);
563 /* Leave {mode,oid}_index zero for a delete. */
566 case DIFF_STATUS_COPIED
:
567 case DIFF_STATUS_RENAMED
:
568 if (d
->rename_status
)
569 BUG("multiple renames on the same target? how?");
570 d
->rename_source
= xstrdup(p
->one
->path
);
571 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
572 d
->rename_status
= p
->status
;
574 case DIFF_STATUS_MODIFIED
:
575 case DIFF_STATUS_TYPE_CHANGED
:
576 d
->mode_head
= p
->one
->mode
;
577 d
->mode_index
= p
->two
->mode
;
578 oidcpy(&d
->oid_head
, &p
->one
->oid
);
579 oidcpy(&d
->oid_index
, &p
->two
->oid
);
582 case DIFF_STATUS_UNMERGED
:
583 d
->stagemask
= unmerged_mask(s
->repo
->index
,
586 * Don't bother setting {mode,oid}_{head,index} since the print
587 * code will output the stage values directly and not use the
588 * values in these fields.
593 BUG("unhandled diff-index status '%c'", p
->status
);
599 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
603 repo_init_revisions(s
->repo
, &rev
, NULL
);
604 setup_revisions(0, NULL
, &rev
, NULL
);
605 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
606 rev
.diffopt
.flags
.dirty_submodules
= 1;
607 rev
.diffopt
.ita_invisible_in_index
= 1;
608 if (!s
->show_untracked_files
)
609 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
610 if (s
->ignore_submodule_arg
) {
611 rev
.diffopt
.flags
.override_submodule_config
= 1;
612 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
613 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
614 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
615 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
616 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
617 rev
.diffopt
.format_callback_data
= s
;
618 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
619 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
620 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
621 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
622 run_diff_files(&rev
, 0);
623 release_revisions(&rev
);
626 static void wt_status_collect_changes_index(struct wt_status
*s
)
629 struct setup_revision_opt opt
;
631 repo_init_revisions(s
->repo
, &rev
, NULL
);
632 memset(&opt
, 0, sizeof(opt
));
633 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
634 setup_revisions(0, NULL
, &rev
, &opt
);
636 rev
.diffopt
.flags
.override_submodule_config
= 1;
637 rev
.diffopt
.ita_invisible_in_index
= 1;
638 if (s
->ignore_submodule_arg
) {
639 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
642 * Unless the user did explicitly request a submodule ignore
643 * mode by passing a command line option we do not ignore any
644 * changed submodule SHA-1s when comparing index and HEAD, no
645 * matter what is configured. Otherwise the user won't be
646 * shown any submodules manually added (and which are
647 * staged to be committed), which would be really confusing.
649 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
652 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
653 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
654 rev
.diffopt
.format_callback_data
= s
;
655 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
656 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
657 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
660 * The `recursive` option must be enabled to allow the diff to recurse
661 * into subdirectories of sparse directory index entries. If it is not
662 * enabled, a subdirectory containing file(s) with changes is reported
663 * as "modified", rather than the modified files themselves.
665 rev
.diffopt
.flags
.recursive
= 1;
667 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
668 run_diff_index(&rev
, 1);
669 release_revisions(&rev
);
672 static int add_file_to_list(const struct object_id
*oid
,
673 struct strbuf
*base
, const char *path
,
674 unsigned int mode
, void *context
)
676 struct string_list_item
*it
;
677 struct wt_status_change_data
*d
;
678 struct wt_status
*s
= context
;
679 struct strbuf full_name
= STRBUF_INIT
;
682 return READ_TREE_RECURSIVE
;
684 strbuf_add(&full_name
, base
->buf
, base
->len
);
685 strbuf_addstr(&full_name
, path
);
686 it
= string_list_insert(&s
->change
, full_name
.buf
);
693 d
->index_status
= DIFF_STATUS_ADDED
;
694 /* Leave {mode,oid}_head zero for adds. */
695 d
->mode_index
= mode
;
696 oidcpy(&d
->oid_index
, oid
);
698 strbuf_release(&full_name
);
702 static void wt_status_collect_changes_initial(struct wt_status
*s
)
704 struct index_state
*istate
= s
->repo
->index
;
707 for (i
= 0; i
< istate
->cache_nr
; i
++) {
708 struct string_list_item
*it
;
709 struct wt_status_change_data
*d
;
710 const struct cache_entry
*ce
= istate
->cache
[i
];
712 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
714 if (ce_intent_to_add(ce
))
716 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
718 * This is a sparse directory entry, so we want to collect all
719 * of the added files within the tree. This requires recursively
720 * expanding the trees to find the elements that are new in this
721 * tree and marking them with DIFF_STATUS_ADDED.
723 struct strbuf base
= STRBUF_INIT
;
724 struct pathspec ps
= { 0 };
725 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
731 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
732 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
733 add_file_to_list
, s
);
737 it
= string_list_insert(&s
->change
, ce
->name
);
744 d
->index_status
= DIFF_STATUS_UNMERGED
;
745 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
747 * Don't bother setting {mode,oid}_{head,index} since the print
748 * code will output the stage values directly and not use the
749 * values in these fields.
753 d
->index_status
= DIFF_STATUS_ADDED
;
754 /* Leave {mode,oid}_head zero for adds. */
755 d
->mode_index
= ce
->ce_mode
;
756 oidcpy(&d
->oid_index
, &ce
->oid
);
762 static void wt_status_collect_untracked(struct wt_status
*s
)
765 struct dir_struct dir
= DIR_INIT
;
766 uint64_t t_begin
= getnanotime();
767 struct index_state
*istate
= s
->repo
->index
;
769 if (!s
->show_untracked_files
)
772 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
774 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
775 if (s
->show_ignored_mode
) {
776 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
778 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
779 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
781 dir
.untracked
= istate
->untracked
;
784 setup_standard_excludes(&dir
);
786 fill_directory(&dir
, istate
, &s
->pathspec
);
788 for (i
= 0; i
< dir
.nr
; i
++) {
789 struct dir_entry
*ent
= dir
.entries
[i
];
790 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
791 string_list_insert(&s
->untracked
, ent
->name
);
794 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
795 struct dir_entry
*ent
= dir
.ignored
[i
];
796 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
797 string_list_insert(&s
->ignored
, ent
->name
);
802 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
803 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
806 static int has_unmerged(struct wt_status
*s
)
810 for (i
= 0; i
< s
->change
.nr
; i
++) {
811 struct wt_status_change_data
*d
;
812 d
= s
->change
.items
[i
].util
;
819 void wt_status_collect(struct wt_status
*s
)
821 trace2_region_enter("status", "worktrees", s
->repo
);
822 wt_status_collect_changes_worktree(s
);
823 trace2_region_leave("status", "worktrees", s
->repo
);
826 trace2_region_enter("status", "initial", s
->repo
);
827 wt_status_collect_changes_initial(s
);
828 trace2_region_leave("status", "initial", s
->repo
);
830 trace2_region_enter("status", "index", s
->repo
);
831 wt_status_collect_changes_index(s
);
832 trace2_region_leave("status", "index", s
->repo
);
835 trace2_region_enter("status", "untracked", s
->repo
);
836 wt_status_collect_untracked(s
);
837 trace2_region_leave("status", "untracked", s
->repo
);
839 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
840 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
844 void wt_status_collect_free_buffers(struct wt_status
*s
)
846 wt_status_state_free_buffers(&s
->state
);
849 void wt_status_state_free_buffers(struct wt_status_state
*state
)
851 FREE_AND_NULL(state
->branch
);
852 FREE_AND_NULL(state
->onto
);
853 FREE_AND_NULL(state
->detached_from
);
856 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
858 int shown_header
= 0;
861 for (i
= 0; i
< s
->change
.nr
; i
++) {
862 struct wt_status_change_data
*d
;
863 struct string_list_item
*it
;
864 it
= &(s
->change
.items
[i
]);
869 wt_longstatus_print_unmerged_header(s
);
872 wt_longstatus_print_unmerged_data(s
, it
);
875 wt_longstatus_print_trailer(s
);
879 static void wt_longstatus_print_updated(struct wt_status
*s
)
881 int shown_header
= 0;
884 for (i
= 0; i
< s
->change
.nr
; i
++) {
885 struct wt_status_change_data
*d
;
886 struct string_list_item
*it
;
887 it
= &(s
->change
.items
[i
]);
889 if (!d
->index_status
||
890 d
->index_status
== DIFF_STATUS_UNMERGED
)
893 wt_longstatus_print_cached_header(s
);
896 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
899 wt_longstatus_print_trailer(s
);
905 * 1 : some change but no delete
907 static int wt_status_check_worktree_changes(struct wt_status
*s
,
908 int *dirty_submodules
)
913 *dirty_submodules
= 0;
915 for (i
= 0; i
< s
->change
.nr
; i
++) {
916 struct wt_status_change_data
*d
;
917 d
= s
->change
.items
[i
].util
;
918 if (!d
->worktree_status
||
919 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
923 if (d
->dirty_submodule
)
924 *dirty_submodules
= 1;
925 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
931 static void wt_longstatus_print_changed(struct wt_status
*s
)
933 int i
, dirty_submodules
;
934 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
936 if (!worktree_changes
)
939 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
941 for (i
= 0; i
< s
->change
.nr
; i
++) {
942 struct wt_status_change_data
*d
;
943 struct string_list_item
*it
;
944 it
= &(s
->change
.items
[i
]);
946 if (!d
->worktree_status
||
947 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
949 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
951 wt_longstatus_print_trailer(s
);
954 static int stash_count_refs(struct object_id
*ooid UNUSED
,
955 struct object_id
*noid UNUSED
,
956 const char *email UNUSED
,
957 timestamp_t timestamp UNUSED
, int tz UNUSED
,
958 const char *message UNUSED
, void *cb_data
)
965 static int count_stash_entries(void)
968 for_each_reflog_ent("refs/stash", stash_count_refs
, &n
);
972 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
974 int stash_count
= count_stash_entries();
977 status_printf_ln(s
, GIT_COLOR_NORMAL
,
978 Q_("Your stash currently has %d entry",
979 "Your stash currently has %d entries", stash_count
),
983 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
985 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
986 struct strbuf cmd_stdout
= STRBUF_INIT
;
987 struct strbuf summary
= STRBUF_INIT
;
988 char *summary_content
;
990 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
992 strvec_push(&sm_summary
.args
, "submodule");
993 strvec_push(&sm_summary
.args
, "summary");
994 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
995 strvec_push(&sm_summary
.args
, "--for-status");
996 strvec_push(&sm_summary
.args
, "--summary-limit");
997 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
999 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1001 sm_summary
.git_cmd
= 1;
1002 sm_summary
.no_stdin
= 1;
1004 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1006 /* prepend header, only if there's an actual output */
1007 if (cmd_stdout
.len
) {
1009 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1011 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1012 strbuf_addstr(&summary
, "\n\n");
1014 strbuf_addbuf(&summary
, &cmd_stdout
);
1015 strbuf_release(&cmd_stdout
);
1017 if (s
->display_comment_prefix
) {
1019 summary_content
= strbuf_detach(&summary
, &len
);
1020 strbuf_add_commented_lines(&summary
, summary_content
, len
);
1021 free(summary_content
);
1024 fputs(summary
.buf
, s
->fp
);
1025 strbuf_release(&summary
);
1028 static void wt_longstatus_print_other(struct wt_status
*s
,
1029 struct string_list
*l
,
1034 struct strbuf buf
= STRBUF_INIT
;
1035 static struct string_list output
= STRING_LIST_INIT_DUP
;
1036 struct column_options copts
;
1041 wt_longstatus_print_other_header(s
, what
, how
);
1043 for (i
= 0; i
< l
->nr
; i
++) {
1044 struct string_list_item
*it
;
1046 it
= &(l
->items
[i
]);
1047 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1048 if (column_active(s
->colopts
)) {
1049 string_list_append(&output
, path
);
1052 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1053 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1057 strbuf_release(&buf
);
1058 if (!column_active(s
->colopts
))
1061 strbuf_addf(&buf
, "%s%s\t%s",
1062 color(WT_STATUS_HEADER
, s
),
1063 s
->display_comment_prefix
? "#" : "",
1064 color(WT_STATUS_UNTRACKED
, s
));
1065 memset(&copts
, 0, sizeof(copts
));
1067 copts
.indent
= buf
.buf
;
1068 if (want_color(s
->use_color
))
1069 copts
.nl
= GIT_COLOR_RESET
"\n";
1070 print_columns(&output
, s
->colopts
, &copts
);
1071 string_list_clear(&output
, 0);
1072 strbuf_release(&buf
);
1074 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1077 size_t wt_status_locate_end(const char *s
, size_t len
)
1080 struct strbuf pattern
= STRBUF_INIT
;
1082 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1083 if (starts_with(s
, pattern
.buf
+ 1))
1085 else if ((p
= strstr(s
, pattern
.buf
)))
1087 strbuf_release(&pattern
);
1091 void wt_status_append_cut_line(struct strbuf
*buf
)
1093 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1095 strbuf_commented_addf(buf
, "%s", cut_line
);
1096 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1099 void wt_status_add_cut_line(FILE *fp
)
1101 struct strbuf buf
= STRBUF_INIT
;
1103 wt_status_append_cut_line(&buf
);
1105 strbuf_release(&buf
);
1108 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1110 struct rev_info rev
;
1111 struct setup_revision_opt opt
;
1112 int dirty_submodules
;
1113 const char *c
= color(WT_STATUS_HEADER
, s
);
1115 repo_init_revisions(s
->repo
, &rev
, NULL
);
1116 rev
.diffopt
.flags
.allow_textconv
= 1;
1117 rev
.diffopt
.ita_invisible_in_index
= 1;
1119 memset(&opt
, 0, sizeof(opt
));
1120 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1121 setup_revisions(0, NULL
, &rev
, &opt
);
1123 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1124 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1125 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1126 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1127 rev
.diffopt
.file
= s
->fp
;
1128 rev
.diffopt
.close_file
= 0;
1130 * If we're not going to stdout, then we definitely don't
1131 * want color, since we are going to the commit message
1132 * file (and even the "auto" setting won't work, since it
1133 * will have checked isatty on stdout). But we then do want
1134 * to insert the scissor line here to reliably remove the
1135 * diff before committing.
1137 if (s
->fp
!= stdout
) {
1138 rev
.diffopt
.use_color
= 0;
1139 wt_status_add_cut_line(s
->fp
);
1141 if (s
->verbose
> 1 && s
->committable
) {
1142 /* print_updated() printed a header, so do we */
1143 if (s
->fp
!= stdout
)
1144 wt_longstatus_print_trailer(s
);
1145 status_printf_ln(s
, c
, _("Changes to be committed:"));
1146 rev
.diffopt
.a_prefix
= "c/";
1147 rev
.diffopt
.b_prefix
= "i/";
1148 } /* else use prefix as per user config */
1149 run_diff_index(&rev
, 1);
1150 if (s
->verbose
> 1 &&
1151 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1152 status_printf_ln(s
, c
,
1153 "--------------------------------------------------");
1154 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1156 rev
.diffopt
.a_prefix
= "i/";
1157 rev
.diffopt
.b_prefix
= "w/";
1158 run_diff_files(&rev
, 0);
1160 release_revisions(&rev
);
1163 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1165 struct strbuf sb
= STRBUF_INIT
;
1166 const char *cp
, *ep
, *branch_name
;
1167 struct branch
*branch
;
1168 char comment_line_string
[3];
1170 uint64_t t_begin
= 0;
1172 assert(s
->branch
&& !s
->is_initial
);
1173 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1175 branch
= branch_get(branch_name
);
1177 t_begin
= getnanotime();
1179 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1182 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1183 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1184 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1185 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1186 strbuf_addf(&sb
, _("\n"
1187 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1188 "You can use '--no-ahead-behind' to avoid this.\n"),
1189 t_delta_in_ms
/ 1000.0);
1194 if (s
->display_comment_prefix
) {
1195 comment_line_string
[i
++] = comment_line_char
;
1196 comment_line_string
[i
++] = ' ';
1198 comment_line_string
[i
] = '\0';
1200 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1201 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1202 "%s%.*s", comment_line_string
,
1203 (int)(ep
- cp
), cp
);
1204 if (s
->display_comment_prefix
)
1205 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1209 strbuf_release(&sb
);
1212 static int uf_was_slow(struct wt_status
*s
)
1214 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1215 s
->untracked_in_ms
= 3250;
1216 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1219 static void show_merge_in_progress(struct wt_status
*s
,
1222 if (has_unmerged(s
)) {
1223 status_printf_ln(s
, color
, _("You have unmerged paths."));
1225 status_printf_ln(s
, color
,
1226 _(" (fix conflicts and run \"git commit\")"));
1227 status_printf_ln(s
, color
,
1228 _(" (use \"git merge --abort\" to abort the merge)"));
1231 status_printf_ln(s
, color
,
1232 _("All conflicts fixed but you are still merging."));
1234 status_printf_ln(s
, color
,
1235 _(" (use \"git commit\" to conclude merge)"));
1237 wt_longstatus_print_trailer(s
);
1240 static void show_am_in_progress(struct wt_status
*s
,
1245 status_printf_ln(s
, color
,
1246 _("You are in the middle of an am session."));
1247 if (s
->state
.am_empty_patch
)
1248 status_printf_ln(s
, color
,
1249 _("The current patch is empty."));
1251 am_empty_patch
= s
->state
.am_empty_patch
;
1252 if (!am_empty_patch
)
1253 status_printf_ln(s
, color
,
1254 _(" (fix conflicts and then run \"git am --continue\")"));
1255 status_printf_ln(s
, color
,
1256 _(" (use \"git am --skip\" to skip this patch)"));
1258 status_printf_ln(s
, color
,
1259 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1260 status_printf_ln(s
, color
,
1261 _(" (use \"git am --abort\" to restore the original branch)"));
1263 wt_longstatus_print_trailer(s
);
1266 static char *read_line_from_git_path(const char *filename
)
1268 struct strbuf buf
= STRBUF_INIT
;
1269 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1272 strbuf_release(&buf
);
1275 strbuf_getline_lf(&buf
, fp
);
1277 return strbuf_detach(&buf
, NULL
);
1279 strbuf_release(&buf
);
1284 static int split_commit_in_progress(struct wt_status
*s
)
1286 int split_in_progress
= 0;
1287 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1289 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1290 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1293 head
= read_line_from_git_path("HEAD");
1294 orig_head
= read_line_from_git_path("ORIG_HEAD");
1295 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1296 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1298 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1299 ; /* fall through, no split in progress */
1300 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1301 split_in_progress
= !!strcmp(head
, rebase_amend
);
1302 else if (strcmp(orig_head
, rebase_orig_head
))
1303 split_in_progress
= 1;
1308 free(rebase_orig_head
);
1310 return split_in_progress
;
1315 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1317 * "pick d6a2f03 some message"
1319 * The function assumes that the line does not contain useless spaces
1320 * before or after the command.
1322 static void abbrev_oid_in_line(struct strbuf
*line
)
1324 struct strbuf
**split
;
1327 if (starts_with(line
->buf
, "exec ") ||
1328 starts_with(line
->buf
, "x ") ||
1329 starts_with(line
->buf
, "label ") ||
1330 starts_with(line
->buf
, "l "))
1333 split
= strbuf_split_max(line
, ' ', 3);
1334 if (split
[0] && split
[1]) {
1335 struct object_id oid
;
1338 * strbuf_split_max left a space. Trim it and re-add
1339 * it after abbreviation.
1341 strbuf_trim(split
[1]);
1342 if (!get_oid(split
[1]->buf
, &oid
)) {
1343 strbuf_reset(split
[1]);
1344 strbuf_add_unique_abbrev(split
[1], &oid
,
1346 strbuf_addch(split
[1], ' ');
1348 for (i
= 0; split
[i
]; i
++)
1349 strbuf_addbuf(line
, split
[i
]);
1352 strbuf_list_free(split
);
1355 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1357 struct strbuf line
= STRBUF_INIT
;
1358 FILE *f
= fopen(git_path("%s", fname
), "r");
1361 if (errno
== ENOENT
)
1363 die_errno("Could not open file %s for reading",
1364 git_path("%s", fname
));
1366 while (!strbuf_getline_lf(&line
, f
)) {
1367 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1372 abbrev_oid_in_line(&line
);
1373 string_list_append(lines
, line
.buf
);
1376 strbuf_release(&line
);
1380 static void show_rebase_information(struct wt_status
*s
,
1383 if (s
->state
.rebase_interactive_in_progress
) {
1385 int nr_lines_to_show
= 2;
1387 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1388 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1390 read_rebase_todolist("rebase-merge/done", &have_done
);
1391 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1393 status_printf_ln(s
, color
,
1394 _("git-rebase-todo is missing."));
1395 if (have_done
.nr
== 0)
1396 status_printf_ln(s
, color
, _("No commands done."));
1398 status_printf_ln(s
, color
,
1399 Q_("Last command done (%"PRIuMAX
" command done):",
1400 "Last commands done (%"PRIuMAX
" commands done):",
1402 (uintmax_t)have_done
.nr
);
1403 for (i
= (have_done
.nr
> nr_lines_to_show
)
1404 ? have_done
.nr
- nr_lines_to_show
: 0;
1407 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1408 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1409 status_printf_ln(s
, color
,
1410 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1413 if (yet_to_do
.nr
== 0)
1414 status_printf_ln(s
, color
,
1415 _("No commands remaining."));
1417 status_printf_ln(s
, color
,
1418 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1419 "Next commands to do (%"PRIuMAX
" remaining commands):",
1421 (uintmax_t)yet_to_do
.nr
);
1422 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1423 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1425 status_printf_ln(s
, color
,
1426 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1428 string_list_clear(&yet_to_do
, 0);
1429 string_list_clear(&have_done
, 0);
1433 static void print_rebase_state(struct wt_status
*s
,
1436 if (s
->state
.branch
)
1437 status_printf_ln(s
, color
,
1438 _("You are currently rebasing branch '%s' on '%s'."),
1442 status_printf_ln(s
, color
,
1443 _("You are currently rebasing."));
1446 static void show_rebase_in_progress(struct wt_status
*s
,
1451 show_rebase_information(s
, color
);
1452 if (has_unmerged(s
)) {
1453 print_rebase_state(s
, color
);
1455 status_printf_ln(s
, color
,
1456 _(" (fix conflicts and then run \"git rebase --continue\")"));
1457 status_printf_ln(s
, color
,
1458 _(" (use \"git rebase --skip\" to skip this patch)"));
1459 status_printf_ln(s
, color
,
1460 _(" (use \"git rebase --abort\" to check out the original branch)"));
1462 } else if (s
->state
.rebase_in_progress
||
1463 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1464 print_rebase_state(s
, color
);
1466 status_printf_ln(s
, color
,
1467 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1468 } else if (split_commit_in_progress(s
)) {
1469 if (s
->state
.branch
)
1470 status_printf_ln(s
, color
,
1471 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1475 status_printf_ln(s
, color
,
1476 _("You are currently splitting a commit during a rebase."));
1478 status_printf_ln(s
, color
,
1479 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1481 if (s
->state
.branch
)
1482 status_printf_ln(s
, color
,
1483 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1487 status_printf_ln(s
, color
,
1488 _("You are currently editing a commit during a rebase."));
1489 if (s
->hints
&& !s
->amend
) {
1490 status_printf_ln(s
, color
,
1491 _(" (use \"git commit --amend\" to amend the current commit)"));
1492 status_printf_ln(s
, color
,
1493 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1496 wt_longstatus_print_trailer(s
);
1499 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1502 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1503 status_printf_ln(s
, color
,
1504 _("Cherry-pick currently in progress."));
1506 status_printf_ln(s
, color
,
1507 _("You are currently cherry-picking commit %s."),
1508 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
,
1512 if (has_unmerged(s
))
1513 status_printf_ln(s
, color
,
1514 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1515 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1516 status_printf_ln(s
, color
,
1517 _(" (run \"git cherry-pick --continue\" to continue)"));
1519 status_printf_ln(s
, color
,
1520 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1521 status_printf_ln(s
, color
,
1522 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1523 status_printf_ln(s
, color
,
1524 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1526 wt_longstatus_print_trailer(s
);
1529 static void show_revert_in_progress(struct wt_status
*s
,
1532 if (is_null_oid(&s
->state
.revert_head_oid
))
1533 status_printf_ln(s
, color
,
1534 _("Revert currently in progress."));
1536 status_printf_ln(s
, color
,
1537 _("You are currently reverting commit %s."),
1538 find_unique_abbrev(&s
->state
.revert_head_oid
,
1541 if (has_unmerged(s
))
1542 status_printf_ln(s
, color
,
1543 _(" (fix conflicts and run \"git revert --continue\")"));
1544 else if (is_null_oid(&s
->state
.revert_head_oid
))
1545 status_printf_ln(s
, color
,
1546 _(" (run \"git revert --continue\" to continue)"));
1548 status_printf_ln(s
, color
,
1549 _(" (all conflicts fixed: run \"git revert --continue\")"));
1550 status_printf_ln(s
, color
,
1551 _(" (use \"git revert --skip\" to skip this patch)"));
1552 status_printf_ln(s
, color
,
1553 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1555 wt_longstatus_print_trailer(s
);
1558 static void show_bisect_in_progress(struct wt_status
*s
,
1561 if (s
->state
.branch
)
1562 status_printf_ln(s
, color
,
1563 _("You are currently bisecting, started from branch '%s'."),
1566 status_printf_ln(s
, color
,
1567 _("You are currently bisecting."));
1569 status_printf_ln(s
, color
,
1570 _(" (use \"git bisect reset\" to get back to the original branch)"));
1571 wt_longstatus_print_trailer(s
);
1574 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1577 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1580 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1581 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1583 status_printf_ln(s
, color
,
1584 _("You are in a sparse checkout with %d%% of tracked files present."),
1585 s
->state
.sparse_checkout_percentage
);
1586 wt_longstatus_print_trailer(s
);
1590 * Extract branch information from rebase/bisect
1592 static char *get_branch(const struct worktree
*wt
, const char *path
)
1594 struct strbuf sb
= STRBUF_INIT
;
1595 struct object_id oid
;
1596 const char *branch_name
;
1598 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1601 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1602 strbuf_setlen(&sb
, sb
.len
- 1);
1605 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1606 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1607 else if (starts_with(sb
.buf
, "refs/"))
1609 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1611 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1612 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1616 return strbuf_detach(&sb
, NULL
);
1619 strbuf_release(&sb
);
1623 struct grab_1st_switch_cbdata
{
1625 struct object_id noid
;
1628 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1629 struct object_id
*noid
,
1630 const char *email UNUSED
,
1631 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1632 const char *message
, void *cb_data
)
1634 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1635 const char *target
= NULL
, *end
;
1637 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1639 target
= strstr(message
, " to ");
1642 target
+= strlen(" to ");
1643 strbuf_reset(&cb
->buf
);
1644 oidcpy(&cb
->noid
, noid
);
1645 end
= strchrnul(target
, '\n');
1646 strbuf_add(&cb
->buf
, target
, end
- target
);
1647 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1648 /* HEAD is relative. Resolve it to the right reflog entry. */
1649 strbuf_reset(&cb
->buf
);
1650 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1655 static void wt_status_get_detached_from(struct repository
*r
,
1656 struct wt_status_state
*state
)
1658 struct grab_1st_switch_cbdata cb
;
1659 struct commit
*commit
;
1660 struct object_id oid
;
1663 strbuf_init(&cb
.buf
, 0);
1664 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1665 strbuf_release(&cb
.buf
);
1669 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
, 1) == 1 &&
1670 /* oid is a commit? match without further lookup */
1671 (oideq(&cb
.noid
, &oid
) ||
1672 /* perhaps oid is a tag, try to dereference to a commit */
1673 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1674 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1675 const char *from
= ref
;
1676 if (!skip_prefix(from
, "refs/tags/", &from
))
1677 skip_prefix(from
, "refs/remotes/", &from
);
1678 state
->detached_from
= xstrdup(from
);
1680 state
->detached_from
=
1681 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1682 oidcpy(&state
->detached_oid
, &cb
.noid
);
1683 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1684 oideq(&oid
, &state
->detached_oid
);
1687 strbuf_release(&cb
.buf
);
1690 int wt_status_check_rebase(const struct worktree
*wt
,
1691 struct wt_status_state
*state
)
1695 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1696 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1697 state
->am_in_progress
= 1;
1698 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1699 state
->am_empty_patch
= 1;
1701 state
->rebase_in_progress
= 1;
1702 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1703 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1705 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1706 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1707 state
->rebase_interactive_in_progress
= 1;
1709 state
->rebase_in_progress
= 1;
1710 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1711 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1717 int wt_status_check_bisect(const struct worktree
*wt
,
1718 struct wt_status_state
*state
)
1722 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1723 state
->bisect_in_progress
= 1;
1724 state
->branch
= get_branch(wt
, "BISECT_START");
1730 static void wt_status_check_sparse_checkout(struct repository
*r
,
1731 struct wt_status_state
*state
)
1733 int skip_worktree
= 0;
1736 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1738 * Don't compute percentage of checked out files if we
1739 * aren't in a sparse checkout or would get division by 0.
1741 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1745 if (r
->index
->sparse_index
) {
1746 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1750 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1751 struct cache_entry
*ce
= r
->index
->cache
[i
];
1752 if (ce_skip_worktree(ce
))
1756 state
->sparse_checkout_percentage
=
1757 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1760 void wt_status_get_state(struct repository
*r
,
1761 struct wt_status_state
*state
,
1762 int get_detached_from
)
1765 struct object_id oid
;
1766 enum replay_action action
;
1768 if (!stat(git_path_merge_head(r
), &st
)) {
1769 wt_status_check_rebase(NULL
, state
);
1770 state
->merge_in_progress
= 1;
1771 } else if (wt_status_check_rebase(NULL
, state
)) {
1773 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1774 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1775 state
->cherry_pick_in_progress
= 1;
1776 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1778 wt_status_check_bisect(NULL
, state
);
1779 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1780 !get_oid("REVERT_HEAD", &oid
)) {
1781 state
->revert_in_progress
= 1;
1782 oidcpy(&state
->revert_head_oid
, &oid
);
1784 if (!sequencer_get_last_command(r
, &action
)) {
1785 if (action
== REPLAY_PICK
) {
1786 state
->cherry_pick_in_progress
= 1;
1787 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1789 state
->revert_in_progress
= 1;
1790 oidcpy(&state
->revert_head_oid
, null_oid());
1793 if (get_detached_from
)
1794 wt_status_get_detached_from(r
, state
);
1795 wt_status_check_sparse_checkout(r
, state
);
1798 static void wt_longstatus_print_state(struct wt_status
*s
)
1800 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1801 struct wt_status_state
*state
= &s
->state
;
1803 if (state
->merge_in_progress
) {
1804 if (state
->rebase_interactive_in_progress
) {
1805 show_rebase_information(s
, state_color
);
1808 show_merge_in_progress(s
, state_color
);
1809 } else if (state
->am_in_progress
)
1810 show_am_in_progress(s
, state_color
);
1811 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1812 show_rebase_in_progress(s
, state_color
);
1813 else if (state
->cherry_pick_in_progress
)
1814 show_cherry_pick_in_progress(s
, state_color
);
1815 else if (state
->revert_in_progress
)
1816 show_revert_in_progress(s
, state_color
);
1817 if (state
->bisect_in_progress
)
1818 show_bisect_in_progress(s
, state_color
);
1820 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1821 show_sparse_checkout_in_use(s
, state_color
);
1824 static void wt_longstatus_print(struct wt_status
*s
)
1826 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1827 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1828 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1831 const char *on_what
= _("On branch ");
1832 const char *branch_name
= s
->branch
;
1833 if (!strcmp(branch_name
, "HEAD")) {
1834 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1835 if (s
->state
.rebase_in_progress
||
1836 s
->state
.rebase_interactive_in_progress
) {
1837 if (s
->state
.rebase_interactive_in_progress
)
1838 on_what
= _("interactive rebase in progress; onto ");
1840 on_what
= _("rebase in progress; onto ");
1841 branch_name
= s
->state
.onto
;
1842 } else if (s
->state
.detached_from
) {
1843 branch_name
= s
->state
.detached_from
;
1844 if (s
->state
.detached_at
)
1845 on_what
= _("HEAD detached at ");
1847 on_what
= _("HEAD detached from ");
1850 on_what
= _("Not currently on any branch.");
1853 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1854 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1855 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1856 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1858 wt_longstatus_print_tracking(s
);
1861 wt_longstatus_print_state(s
);
1863 if (s
->is_initial
) {
1864 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1865 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1867 ? _("Initial commit")
1868 : _("No commits yet"));
1869 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1872 wt_longstatus_print_updated(s
);
1873 wt_longstatus_print_unmerged(s
);
1874 wt_longstatus_print_changed(s
);
1875 if (s
->submodule_summary
&&
1876 (!s
->ignore_submodule_arg
||
1877 strcmp(s
->ignore_submodule_arg
, "all"))) {
1878 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1879 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1881 if (s
->show_untracked_files
) {
1882 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1883 if (s
->show_ignored_mode
)
1884 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1885 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1886 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1887 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1888 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1889 _("It took %.2f seconds to enumerate untracked files,\n"
1890 "but the results were cached, and subsequent runs may be faster."),
1891 s
->untracked_in_ms
/ 1000.0);
1893 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1894 _("It took %.2f seconds to enumerate untracked files."),
1895 s
->untracked_in_ms
/ 1000.0);
1897 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1898 _("See 'git help status' for information on how to improve this."));
1899 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1901 } else if (s
->committable
)
1902 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1904 ? _(" (use -u option to show untracked files)") : "");
1907 wt_longstatus_print_verbose(s
);
1908 if (!s
->committable
) {
1910 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1913 else if (s
->workdir_dirty
) {
1915 fprintf(s
->fp
, _("no changes added to commit "
1916 "(use \"git add\" and/or "
1917 "\"git commit -a\")\n"));
1919 fprintf(s
->fp
, _("no changes added to "
1921 } else if (s
->untracked
.nr
) {
1923 fprintf(s
->fp
, _("nothing added to commit but "
1924 "untracked files present (use "
1925 "\"git add\" to track)\n"));
1927 fprintf(s
->fp
, _("nothing added to commit but "
1928 "untracked files present\n"));
1929 } else if (s
->is_initial
) {
1931 fprintf(s
->fp
, _("nothing to commit (create/"
1932 "copy files and use \"git "
1933 "add\" to track)\n"));
1935 fprintf(s
->fp
, _("nothing to commit\n"));
1936 } else if (!s
->show_untracked_files
) {
1938 fprintf(s
->fp
, _("nothing to commit (use -u to "
1939 "show untracked files)\n"));
1941 fprintf(s
->fp
, _("nothing to commit\n"));
1943 fprintf(s
->fp
, _("nothing to commit, working tree "
1947 wt_longstatus_print_stash_summary(s
);
1950 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1951 struct wt_status
*s
)
1953 struct wt_status_change_data
*d
= it
->util
;
1954 const char *how
= "??";
1956 switch (d
->stagemask
) {
1957 case 1: how
= "DD"; break; /* both deleted */
1958 case 2: how
= "AU"; break; /* added by us */
1959 case 3: how
= "UD"; break; /* deleted by them */
1960 case 4: how
= "UA"; break; /* added by them */
1961 case 5: how
= "DU"; break; /* deleted by us */
1962 case 6: how
= "AA"; break; /* both added */
1963 case 7: how
= "UU"; break; /* both modified */
1965 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1966 if (s
->null_termination
) {
1967 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1969 struct strbuf onebuf
= STRBUF_INIT
;
1971 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1972 fprintf(s
->fp
, " %s\n", one
);
1973 strbuf_release(&onebuf
);
1977 static void wt_shortstatus_status(struct string_list_item
*it
,
1978 struct wt_status
*s
)
1980 struct wt_status_change_data
*d
= it
->util
;
1982 if (d
->index_status
)
1983 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1986 if (d
->worktree_status
)
1987 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1991 if (s
->null_termination
) {
1992 fprintf(s
->fp
, "%s%c", it
->string
, 0);
1993 if (d
->rename_source
)
1994 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
1996 struct strbuf onebuf
= STRBUF_INIT
;
1999 if (d
->rename_source
) {
2000 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2001 QUOTE_PATH_QUOTE_SP
);
2002 fprintf(s
->fp
, "%s -> ", one
);
2003 strbuf_release(&onebuf
);
2005 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2006 fprintf(s
->fp
, "%s\n", one
);
2007 strbuf_release(&onebuf
);
2011 static void wt_shortstatus_other(struct string_list_item
*it
,
2012 struct wt_status
*s
, const char *sign
)
2014 if (s
->null_termination
) {
2015 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2017 struct strbuf onebuf
= STRBUF_INIT
;
2019 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2020 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2021 fprintf(s
->fp
, " %s\n", one
);
2022 strbuf_release(&onebuf
);
2026 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2028 struct branch
*branch
;
2029 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2030 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2031 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2035 const char *branch_name
;
2036 int num_ours
, num_theirs
, sti
;
2037 int upstream_is_gone
= 0;
2039 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2043 branch_name
= s
->branch
;
2045 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2048 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2050 if (!strcmp(s
->branch
, "HEAD")) {
2051 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2052 LABEL(N_("HEAD (no branch)")));
2056 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2058 branch
= branch_get(branch_name
);
2060 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2062 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2063 0, s
->ahead_behind_flags
);
2068 upstream_is_gone
= 1;
2071 short_base
= shorten_unambiguous_ref(base
, 0);
2072 color_fprintf(s
->fp
, header_color
, "...");
2073 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2076 if (!upstream_is_gone
&& !sti
)
2079 color_fprintf(s
->fp
, header_color
, " [");
2080 if (upstream_is_gone
) {
2081 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2082 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2083 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2084 } else if (!num_ours
) {
2085 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2086 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2087 } else if (!num_theirs
) {
2088 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2089 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2091 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2092 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2093 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2094 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2097 color_fprintf(s
->fp
, header_color
, "]");
2099 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2102 static void wt_shortstatus_print(struct wt_status
*s
)
2104 struct string_list_item
*it
;
2107 wt_shortstatus_print_tracking(s
);
2109 for_each_string_list_item(it
, &s
->change
) {
2110 struct wt_status_change_data
*d
= it
->util
;
2113 wt_shortstatus_unmerged(it
, s
);
2115 wt_shortstatus_status(it
, s
);
2117 for_each_string_list_item(it
, &s
->untracked
)
2118 wt_shortstatus_other(it
, s
, "??");
2120 for_each_string_list_item(it
, &s
->ignored
)
2121 wt_shortstatus_other(it
, s
, "!!");
2124 static void wt_porcelain_print(struct wt_status
*s
)
2127 s
->relative_paths
= 0;
2130 wt_shortstatus_print(s
);
2134 * Print branch information for porcelain v2 output. These lines
2135 * are printed when the '--branch' parameter is given.
2137 * # branch.oid <commit><eol>
2138 * # branch.head <head><eol>
2139 * [# branch.upstream <upstream><eol>
2140 * [# branch.ab +<ahead> -<behind><eol>]]
2142 * <commit> ::= the current commit hash or the literal
2143 * "(initial)" to indicate an initialized repo
2146 * <head> ::= <branch_name> the current branch name or
2147 * "(detached)" literal when detached head or
2148 * "(unknown)" when something is wrong.
2150 * <upstream> ::= the upstream branch name, when set.
2152 * <ahead> ::= integer ahead value or '?'.
2154 * <behind> ::= integer behind value or '?'.
2156 * The end-of-line is defined by the -z flag.
2158 * <eol> ::= NUL when -z,
2161 * When an upstream is set and present, the 'branch.ab' line will
2162 * be printed with the ahead/behind counts for the branch and the
2163 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2164 * are different, '?' will be substituted for the actual count.
2166 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2168 struct branch
*branch
;
2170 const char *branch_name
;
2171 int ab_info
, nr_ahead
, nr_behind
;
2172 char eol
= s
->null_termination
? '\0' : '\n';
2174 fprintf(s
->fp
, "# branch.oid %s%c",
2175 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2179 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2181 if (!strcmp(s
->branch
, "HEAD")) {
2182 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2184 if (s
->state
.rebase_in_progress
||
2185 s
->state
.rebase_interactive_in_progress
)
2186 branch_name
= s
->state
.onto
;
2187 else if (s
->state
.detached_from
)
2188 branch_name
= s
->state
.detached_from
;
2193 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2195 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2198 /* Lookup stats on the upstream tracking branch, if set. */
2199 branch
= branch_get(branch_name
);
2201 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2202 &base
, 0, s
->ahead_behind_flags
);
2204 base
= shorten_unambiguous_ref(base
, 0);
2205 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2210 if (nr_ahead
|| nr_behind
)
2211 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2212 nr_ahead
, nr_behind
, eol
);
2214 fprintf(s
->fp
, "# branch.ab +? -?%c",
2216 } else if (!ab_info
) {
2218 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2225 * Print the stash count in a porcelain-friendly format
2227 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2229 int stash_count
= count_stash_entries();
2230 char eol
= s
->null_termination
? '\0' : '\n';
2232 if (stash_count
> 0)
2233 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2237 * Convert various submodule status values into a
2238 * fixed-length string of characters in the buffer provided.
2240 static void wt_porcelain_v2_submodule_state(
2241 struct wt_status_change_data
*d
,
2244 if (S_ISGITLINK(d
->mode_head
) ||
2245 S_ISGITLINK(d
->mode_index
) ||
2246 S_ISGITLINK(d
->mode_worktree
)) {
2248 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2249 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2250 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2261 * Fix-up changed entries before we print them.
2263 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2265 struct wt_status_change_data
*d
= it
->util
;
2267 if (!d
->index_status
) {
2269 * This entry is unchanged in the index (relative to the head).
2270 * Therefore, the collect_updated_cb was never called for this
2271 * entry (during the head-vs-index scan) and so the head column
2272 * fields were never set.
2274 * We must have data for the index column (from the
2275 * index-vs-worktree scan (otherwise, this entry should not be
2276 * in the list of changes)).
2278 * Copy index column fields to the head column, so that our
2279 * output looks complete.
2281 assert(d
->mode_head
== 0);
2282 d
->mode_head
= d
->mode_index
;
2283 oidcpy(&d
->oid_head
, &d
->oid_index
);
2286 if (!d
->worktree_status
) {
2288 * This entry is unchanged in the worktree (relative to the index).
2289 * Therefore, the collect_changed_cb was never called for this entry
2290 * (during the index-vs-worktree scan) and so the worktree column
2291 * fields were never set.
2293 * We must have data for the index column (from the head-vs-index
2296 * Copy the index column fields to the worktree column so that
2297 * our output looks complete.
2299 * Note that we only have a mode field in the worktree column
2300 * because the scan code tries really hard to not have to compute it.
2302 assert(d
->mode_worktree
== 0);
2303 d
->mode_worktree
= d
->mode_index
;
2308 * Print porcelain v2 info for tracked entries with changes.
2310 static void wt_porcelain_v2_print_changed_entry(
2311 struct string_list_item
*it
,
2312 struct wt_status
*s
)
2314 struct wt_status_change_data
*d
= it
->util
;
2315 struct strbuf buf
= STRBUF_INIT
;
2316 struct strbuf buf_from
= STRBUF_INIT
;
2317 const char *path
= NULL
;
2318 const char *path_from
= NULL
;
2320 char submodule_token
[5];
2321 char sep_char
, eol_char
;
2323 wt_porcelain_v2_fix_up_changed(it
);
2324 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2326 key
[0] = d
->index_status
? d
->index_status
: '.';
2327 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2330 if (s
->null_termination
) {
2332 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2333 * A single NUL character separates them.
2338 path_from
= d
->rename_source
;
2341 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2342 * The source path is only present when necessary.
2343 * A single TAB separates them (because paths can contain spaces
2344 * which are not escaped and C-quoting does escape TAB characters).
2348 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2349 if (d
->rename_source
)
2350 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2354 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2355 key
, submodule_token
,
2356 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2357 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2358 d
->rename_status
, d
->rename_score
,
2359 path
, sep_char
, path_from
, eol_char
);
2361 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2362 key
, submodule_token
,
2363 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2364 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2367 strbuf_release(&buf
);
2368 strbuf_release(&buf_from
);
2372 * Print porcelain v2 status info for unmerged entries.
2374 static void wt_porcelain_v2_print_unmerged_entry(
2375 struct string_list_item
*it
,
2376 struct wt_status
*s
)
2378 struct wt_status_change_data
*d
= it
->util
;
2379 struct index_state
*istate
= s
->repo
->index
;
2380 const struct cache_entry
*ce
;
2381 struct strbuf buf_index
= STRBUF_INIT
;
2382 const char *path_index
= NULL
;
2383 int pos
, stage
, sum
;
2386 struct object_id oid
;
2389 char submodule_token
[5];
2390 char unmerged_prefix
= 'u';
2391 char eol_char
= s
->null_termination
? '\0' : '\n';
2393 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2395 switch (d
->stagemask
) {
2396 case 1: key
= "DD"; break; /* both deleted */
2397 case 2: key
= "AU"; break; /* added by us */
2398 case 3: key
= "UD"; break; /* deleted by them */
2399 case 4: key
= "UA"; break; /* added by them */
2400 case 5: key
= "DU"; break; /* deleted by us */
2401 case 6: key
= "AA"; break; /* both added */
2402 case 7: key
= "UU"; break; /* both modified */
2404 BUG("unhandled unmerged status %x", d
->stagemask
);
2408 * Disregard d.aux.porcelain_v2 data that we accumulated
2409 * for the head and index columns during the scans and
2410 * replace with the actual stage data.
2412 * Note that this is a last-one-wins for each the individual
2413 * stage [123] columns in the event of multiple cache entries
2416 memset(stages
, 0, sizeof(stages
));
2418 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2421 while (pos
< istate
->cache_nr
) {
2422 ce
= istate
->cache
[pos
++];
2423 stage
= ce_stage(ce
);
2424 if (strcmp(ce
->name
, it
->string
) || !stage
)
2426 stages
[stage
- 1].mode
= ce
->ce_mode
;
2427 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2428 sum
|= (1 << (stage
- 1));
2430 if (sum
!= d
->stagemask
)
2431 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2433 if (s
->null_termination
)
2434 path_index
= it
->string
;
2436 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2438 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2439 unmerged_prefix
, key
, submodule_token
,
2440 stages
[0].mode
, /* stage 1 */
2441 stages
[1].mode
, /* stage 2 */
2442 stages
[2].mode
, /* stage 3 */
2444 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2445 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2446 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2450 strbuf_release(&buf_index
);
2454 * Print porcelain V2 status info for untracked and ignored entries.
2456 static void wt_porcelain_v2_print_other(
2457 struct string_list_item
*it
,
2458 struct wt_status
*s
,
2461 struct strbuf buf
= STRBUF_INIT
;
2465 if (s
->null_termination
) {
2469 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2473 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2475 strbuf_release(&buf
);
2479 * Print porcelain V2 status.
2482 * [<v2_changed_items>]*
2483 * [<v2_unmerged_items>]*
2484 * [<v2_untracked_items>]*
2485 * [<v2_ignored_items>]*
2488 static void wt_porcelain_v2_print(struct wt_status
*s
)
2490 struct wt_status_change_data
*d
;
2491 struct string_list_item
*it
;
2495 wt_porcelain_v2_print_tracking(s
);
2498 wt_porcelain_v2_print_stash(s
);
2500 for (i
= 0; i
< s
->change
.nr
; i
++) {
2501 it
= &(s
->change
.items
[i
]);
2504 wt_porcelain_v2_print_changed_entry(it
, s
);
2507 for (i
= 0; i
< s
->change
.nr
; i
++) {
2508 it
= &(s
->change
.items
[i
]);
2511 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2514 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2515 it
= &(s
->untracked
.items
[i
]);
2516 wt_porcelain_v2_print_other(it
, s
, '?');
2519 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2520 it
= &(s
->ignored
.items
[i
]);
2521 wt_porcelain_v2_print_other(it
, s
, '!');
2525 void wt_status_print(struct wt_status
*s
)
2527 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2528 trace2_data_intmax("status", s
->repo
, "count/untracked",
2530 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2532 trace2_region_enter("status", "print", s
->repo
);
2534 switch (s
->status_format
) {
2535 case STATUS_FORMAT_SHORT
:
2536 wt_shortstatus_print(s
);
2538 case STATUS_FORMAT_PORCELAIN
:
2539 wt_porcelain_print(s
);
2541 case STATUS_FORMAT_PORCELAIN_V2
:
2542 wt_porcelain_v2_print(s
);
2544 case STATUS_FORMAT_UNSPECIFIED
:
2545 BUG("finalize_deferred_config() should have been called");
2547 case STATUS_FORMAT_NONE
:
2548 case STATUS_FORMAT_LONG
:
2549 wt_longstatus_print(s
);
2553 trace2_region_leave("status", "print", s
->repo
);
2557 * Returns 1 if there are unstaged changes, 0 otherwise.
2559 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2561 struct rev_info rev_info
;
2564 repo_init_revisions(r
, &rev_info
, NULL
);
2565 if (ignore_submodules
) {
2566 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2567 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2569 rev_info
.diffopt
.flags
.quick
= 1;
2570 diff_setup_done(&rev_info
.diffopt
);
2571 result
= run_diff_files(&rev_info
, 0);
2572 result
= diff_result_code(&rev_info
.diffopt
, result
);
2573 release_revisions(&rev_info
);
2578 * Returns 1 if there are uncommitted changes, 0 otherwise.
2580 int has_uncommitted_changes(struct repository
*r
,
2581 int ignore_submodules
)
2583 struct rev_info rev_info
;
2586 if (is_index_unborn(r
->index
))
2589 repo_init_revisions(r
, &rev_info
, NULL
);
2590 if (ignore_submodules
)
2591 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2592 rev_info
.diffopt
.flags
.quick
= 1;
2594 add_head_to_pending(&rev_info
);
2595 if (!rev_info
.pending
.nr
) {
2597 * We have no head (or it's corrupt); use the empty tree,
2598 * which will complain if the index is non-empty.
2600 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2601 add_pending_object(&rev_info
, &tree
->object
, "");
2604 diff_setup_done(&rev_info
.diffopt
);
2605 result
= run_diff_index(&rev_info
, 1);
2606 result
= diff_result_code(&rev_info
.diffopt
, result
);
2607 release_revisions(&rev_info
);
2612 * If the work tree has unstaged or uncommitted changes, dies with the
2613 * appropriate message.
2615 int require_clean_work_tree(struct repository
*r
,
2618 int ignore_submodules
,
2621 struct lock_file lock_file
= LOCK_INIT
;
2624 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2625 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2627 repo_update_index_if_able(r
, &lock_file
);
2628 rollback_lock_file(&lock_file
);
2630 if (has_unstaged_changes(r
, ignore_submodules
)) {
2631 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2632 error(_("cannot %s: You have unstaged changes."), _(action
));
2636 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2638 error(_("additionally, your index contains uncommitted changes."));
2640 error(_("cannot %s: Your index contains uncommitted changes."),