7 #include "environment.h"
13 #include "run-command.h"
17 #include "submodule.h"
23 #include "sequencer.h"
24 #include "fsmonitor-settings.h"
26 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
27 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
29 static const char cut_line
[] =
30 "------------------------ >8 ------------------------\n";
32 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
33 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
34 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
35 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
36 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
37 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
38 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
39 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
40 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
41 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
44 static const char *color(int slot
, struct wt_status
*s
)
47 if (want_color(s
->use_color
))
48 c
= s
->color_palette
[slot
];
49 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
50 c
= s
->color_palette
[WT_STATUS_HEADER
];
54 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
55 const char *fmt
, va_list ap
, const char *trail
)
57 struct strbuf sb
= STRBUF_INIT
;
58 struct strbuf linebuf
= STRBUF_INIT
;
59 const char *line
, *eol
;
61 strbuf_vaddf(&sb
, fmt
, ap
);
63 if (s
->display_comment_prefix
) {
64 strbuf_addch(&sb
, comment_line_char
);
66 strbuf_addch(&sb
, ' ');
68 color_print_strbuf(s
->fp
, color
, &sb
);
70 fprintf(s
->fp
, "%s", trail
);
74 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
75 eol
= strchr(line
, '\n');
77 strbuf_reset(&linebuf
);
78 if (at_bol
&& s
->display_comment_prefix
) {
79 strbuf_addch(&linebuf
, comment_line_char
);
80 if (*line
!= '\n' && *line
!= '\t')
81 strbuf_addch(&linebuf
, ' ');
84 strbuf_add(&linebuf
, line
, eol
- line
);
86 strbuf_addstr(&linebuf
, line
);
87 color_print_strbuf(s
->fp
, color
, &linebuf
);
95 fprintf(s
->fp
, "%s", trail
);
96 strbuf_release(&linebuf
);
100 void status_printf_ln(struct wt_status
*s
, const char *color
,
101 const char *fmt
, ...)
106 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
110 void status_printf(struct wt_status
*s
, const char *color
,
111 const char *fmt
, ...)
116 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
120 static void status_printf_more(struct wt_status
*s
, const char *color
,
121 const char *fmt
, ...)
126 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
130 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
132 memset(s
, 0, sizeof(*s
));
134 memcpy(s
->color_palette
, default_wt_status_colors
,
135 sizeof(default_wt_status_colors
));
136 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
138 s
->relative_paths
= 1;
139 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
140 s
->reference
= "HEAD";
142 s
->index_file
= get_index_file();
143 s
->change
.strdup_strings
= 1;
144 s
->untracked
.strdup_strings
= 1;
145 s
->ignored
.strdup_strings
= 1;
146 s
->show_branch
= -1; /* unspecified */
148 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
149 s
->display_comment_prefix
= 0;
150 s
->detect_rename
= -1;
151 s
->rename_score
= -1;
152 s
->rename_limit
= -1;
155 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
158 int del_mod_conflict
= 0;
159 int both_deleted
= 0;
161 const char *c
= color(WT_STATUS_HEADER
, s
);
163 status_printf_ln(s
, c
, _("Unmerged paths:"));
165 for (i
= 0; i
< s
->change
.nr
; i
++) {
166 struct string_list_item
*it
= &(s
->change
.items
[i
]);
167 struct wt_status_change_data
*d
= it
->util
;
169 switch (d
->stagemask
) {
177 del_mod_conflict
= 1;
187 if (s
->whence
!= FROM_COMMIT
)
189 else if (!s
->is_initial
) {
190 if (!strcmp(s
->reference
, "HEAD"))
191 status_printf_ln(s
, c
,
192 _(" (use \"git restore --staged <file>...\" to unstage)"));
194 status_printf_ln(s
, c
,
195 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
198 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
201 if (!del_mod_conflict
)
202 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
204 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
205 } else if (!del_mod_conflict
&& !not_deleted
) {
206 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
208 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
212 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
214 const char *c
= color(WT_STATUS_HEADER
, s
);
216 status_printf_ln(s
, c
, _("Changes to be committed:"));
219 if (s
->whence
!= FROM_COMMIT
)
220 ; /* NEEDSWORK: use "git reset --unresolve"??? */
221 else if (!s
->is_initial
) {
222 if (!strcmp(s
->reference
, "HEAD"))
223 status_printf_ln(s
, c
224 , _(" (use \"git restore --staged <file>...\" to unstage)"));
226 status_printf_ln(s
, c
,
227 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
230 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
233 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
235 int has_dirty_submodules
)
237 const char *c
= color(WT_STATUS_HEADER
, s
);
239 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
243 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
245 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
246 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
247 if (has_dirty_submodules
)
248 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
251 static void wt_longstatus_print_other_header(struct wt_status
*s
,
255 const char *c
= color(WT_STATUS_HEADER
, s
);
256 status_printf_ln(s
, c
, "%s:", what
);
259 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
262 static void wt_longstatus_print_trailer(struct wt_status
*s
)
264 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
267 static const char *wt_status_unmerged_status_string(int stagemask
)
271 return _("both deleted:");
273 return _("added by us:");
275 return _("deleted by them:");
277 return _("added by them:");
279 return _("deleted by us:");
281 return _("both added:");
283 return _("both modified:");
285 BUG("unhandled unmerged status %x", stagemask
);
289 static const char *wt_status_diff_status_string(int status
)
292 case DIFF_STATUS_ADDED
:
293 return _("new file:");
294 case DIFF_STATUS_COPIED
:
296 case DIFF_STATUS_DELETED
:
297 return _("deleted:");
298 case DIFF_STATUS_MODIFIED
:
299 return _("modified:");
300 case DIFF_STATUS_RENAMED
:
301 return _("renamed:");
302 case DIFF_STATUS_TYPE_CHANGED
:
303 return _("typechange:");
304 case DIFF_STATUS_UNKNOWN
:
305 return _("unknown:");
306 case DIFF_STATUS_UNMERGED
:
307 return _("unmerged:");
313 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
317 for (i
= minval
; i
<= maxval
; i
++) {
318 const char *s
= label(i
);
319 int len
= s
? utf8_strwidth(s
) : 0;
326 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
327 struct string_list_item
*it
)
329 const char *c
= color(WT_STATUS_UNMERGED
, s
);
330 struct wt_status_change_data
*d
= it
->util
;
331 struct strbuf onebuf
= STRBUF_INIT
;
332 static char *padding
;
333 static int label_width
;
334 const char *one
, *how
;
338 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
339 label_width
+= strlen(" ");
340 padding
= xmallocz(label_width
);
341 memset(padding
, ' ', label_width
);
344 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
345 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
347 how
= wt_status_unmerged_status_string(d
->stagemask
);
348 len
= label_width
- utf8_strwidth(how
);
349 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
350 strbuf_release(&onebuf
);
353 static void wt_longstatus_print_change_data(struct wt_status
*s
,
355 struct string_list_item
*it
)
357 struct wt_status_change_data
*d
= it
->util
;
358 const char *c
= color(change_type
, s
);
362 const char *one
, *two
;
363 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
364 struct strbuf extra
= STRBUF_INIT
;
365 static char *padding
;
366 static int label_width
;
371 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
372 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
373 label_width
+= strlen(" ");
374 padding
= xmallocz(label_width
);
375 memset(padding
, ' ', label_width
);
378 one_name
= two_name
= it
->string
;
379 switch (change_type
) {
380 case WT_STATUS_UPDATED
:
381 status
= d
->index_status
;
383 case WT_STATUS_CHANGED
:
384 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
385 strbuf_addstr(&extra
, " (");
386 if (d
->new_submodule_commits
)
387 strbuf_addstr(&extra
, _("new commits, "));
388 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
389 strbuf_addstr(&extra
, _("modified content, "));
390 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
391 strbuf_addstr(&extra
, _("untracked content, "));
392 strbuf_setlen(&extra
, extra
.len
- 2);
393 strbuf_addch(&extra
, ')');
395 status
= d
->worktree_status
;
398 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
403 * Only pick up the rename it's relevant. If the rename is for
404 * the changed section and we're printing the updated section,
407 if (d
->rename_status
== status
)
408 one_name
= d
->rename_source
;
410 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
411 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
413 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
414 what
= wt_status_diff_status_string(status
);
416 BUG("unhandled diff status %c", status
);
417 len
= label_width
- utf8_strwidth(what
);
419 if (one_name
!= two_name
)
420 status_printf_more(s
, c
, "%s%.*s%s -> %s",
421 what
, len
, padding
, one
, two
);
423 status_printf_more(s
, c
, "%s%.*s%s",
424 what
, len
, padding
, one
);
426 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
427 strbuf_release(&extra
);
429 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
430 strbuf_release(&onebuf
);
431 strbuf_release(&twobuf
);
434 static char short_submodule_status(struct wt_status_change_data
*d
)
436 if (d
->new_submodule_commits
)
438 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
440 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
442 return d
->worktree_status
;
445 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
446 struct diff_options
*options UNUSED
,
449 struct wt_status
*s
= data
;
454 s
->workdir_dirty
= 1;
455 for (i
= 0; i
< q
->nr
; i
++) {
456 struct diff_filepair
*p
;
457 struct string_list_item
*it
;
458 struct wt_status_change_data
*d
;
461 it
= string_list_insert(&s
->change
, p
->two
->path
);
467 if (!d
->worktree_status
)
468 d
->worktree_status
= p
->status
;
469 if (S_ISGITLINK(p
->two
->mode
)) {
470 d
->dirty_submodule
= p
->two
->dirty_submodule
;
471 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
473 if (s
->status_format
== STATUS_FORMAT_SHORT
)
474 d
->worktree_status
= short_submodule_status(d
);
478 case DIFF_STATUS_ADDED
:
479 d
->mode_worktree
= p
->two
->mode
;
482 case DIFF_STATUS_DELETED
:
483 d
->mode_index
= p
->one
->mode
;
484 oidcpy(&d
->oid_index
, &p
->one
->oid
);
485 /* mode_worktree is zero for a delete. */
488 case DIFF_STATUS_COPIED
:
489 case DIFF_STATUS_RENAMED
:
490 if (d
->rename_status
)
491 BUG("multiple renames on the same target? how?");
492 d
->rename_source
= xstrdup(p
->one
->path
);
493 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
494 d
->rename_status
= p
->status
;
496 case DIFF_STATUS_MODIFIED
:
497 case DIFF_STATUS_TYPE_CHANGED
:
498 case DIFF_STATUS_UNMERGED
:
499 d
->mode_index
= p
->one
->mode
;
500 d
->mode_worktree
= p
->two
->mode
;
501 oidcpy(&d
->oid_index
, &p
->one
->oid
);
505 BUG("unhandled diff-files status '%c'", p
->status
);
512 static int unmerged_mask(struct index_state
*istate
, const char *path
)
515 const struct cache_entry
*ce
;
517 pos
= index_name_pos(istate
, path
, strlen(path
));
523 while (pos
< istate
->cache_nr
) {
524 ce
= istate
->cache
[pos
++];
525 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
527 mask
|= (1 << (ce_stage(ce
) - 1));
532 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
533 struct diff_options
*options UNUSED
,
536 struct wt_status
*s
= data
;
539 for (i
= 0; i
< q
->nr
; i
++) {
540 struct diff_filepair
*p
;
541 struct string_list_item
*it
;
542 struct wt_status_change_data
*d
;
545 it
= string_list_insert(&s
->change
, p
->two
->path
);
551 if (!d
->index_status
)
552 d
->index_status
= p
->status
;
554 case DIFF_STATUS_ADDED
:
555 /* Leave {mode,oid}_head zero for an add. */
556 d
->mode_index
= p
->two
->mode
;
557 oidcpy(&d
->oid_index
, &p
->two
->oid
);
560 case DIFF_STATUS_DELETED
:
561 d
->mode_head
= p
->one
->mode
;
562 oidcpy(&d
->oid_head
, &p
->one
->oid
);
564 /* Leave {mode,oid}_index zero for a delete. */
567 case DIFF_STATUS_COPIED
:
568 case DIFF_STATUS_RENAMED
:
569 if (d
->rename_status
)
570 BUG("multiple renames on the same target? how?");
571 d
->rename_source
= xstrdup(p
->one
->path
);
572 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
573 d
->rename_status
= p
->status
;
575 case DIFF_STATUS_MODIFIED
:
576 case DIFF_STATUS_TYPE_CHANGED
:
577 d
->mode_head
= p
->one
->mode
;
578 d
->mode_index
= p
->two
->mode
;
579 oidcpy(&d
->oid_head
, &p
->one
->oid
);
580 oidcpy(&d
->oid_index
, &p
->two
->oid
);
583 case DIFF_STATUS_UNMERGED
:
584 d
->stagemask
= unmerged_mask(s
->repo
->index
,
587 * Don't bother setting {mode,oid}_{head,index} since the print
588 * code will output the stage values directly and not use the
589 * values in these fields.
594 BUG("unhandled diff-index status '%c'", p
->status
);
600 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
604 repo_init_revisions(s
->repo
, &rev
, NULL
);
605 setup_revisions(0, NULL
, &rev
, NULL
);
606 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
607 rev
.diffopt
.flags
.dirty_submodules
= 1;
608 rev
.diffopt
.ita_invisible_in_index
= 1;
609 if (!s
->show_untracked_files
)
610 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
611 if (s
->ignore_submodule_arg
) {
612 rev
.diffopt
.flags
.override_submodule_config
= 1;
613 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
614 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
615 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
616 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
617 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
618 rev
.diffopt
.format_callback_data
= s
;
619 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
620 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
621 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
622 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
623 run_diff_files(&rev
, 0);
624 release_revisions(&rev
);
627 static void wt_status_collect_changes_index(struct wt_status
*s
)
630 struct setup_revision_opt opt
;
632 repo_init_revisions(s
->repo
, &rev
, NULL
);
633 memset(&opt
, 0, sizeof(opt
));
634 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
635 setup_revisions(0, NULL
, &rev
, &opt
);
637 rev
.diffopt
.flags
.override_submodule_config
= 1;
638 rev
.diffopt
.ita_invisible_in_index
= 1;
639 if (s
->ignore_submodule_arg
) {
640 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
643 * Unless the user did explicitly request a submodule ignore
644 * mode by passing a command line option we do not ignore any
645 * changed submodule SHA-1s when comparing index and HEAD, no
646 * matter what is configured. Otherwise the user won't be
647 * shown any submodules manually added (and which are
648 * staged to be committed), which would be really confusing.
650 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
653 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
654 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
655 rev
.diffopt
.format_callback_data
= s
;
656 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
657 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
658 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
661 * The `recursive` option must be enabled to allow the diff to recurse
662 * into subdirectories of sparse directory index entries. If it is not
663 * enabled, a subdirectory containing file(s) with changes is reported
664 * as "modified", rather than the modified files themselves.
666 rev
.diffopt
.flags
.recursive
= 1;
668 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
669 run_diff_index(&rev
, 1);
670 release_revisions(&rev
);
673 static int add_file_to_list(const struct object_id
*oid
,
674 struct strbuf
*base
, const char *path
,
675 unsigned int mode
, void *context
)
677 struct string_list_item
*it
;
678 struct wt_status_change_data
*d
;
679 struct wt_status
*s
= context
;
680 struct strbuf full_name
= STRBUF_INIT
;
683 return READ_TREE_RECURSIVE
;
685 strbuf_add(&full_name
, base
->buf
, base
->len
);
686 strbuf_addstr(&full_name
, path
);
687 it
= string_list_insert(&s
->change
, full_name
.buf
);
694 d
->index_status
= DIFF_STATUS_ADDED
;
695 /* Leave {mode,oid}_head zero for adds. */
696 d
->mode_index
= mode
;
697 oidcpy(&d
->oid_index
, oid
);
699 strbuf_release(&full_name
);
703 static void wt_status_collect_changes_initial(struct wt_status
*s
)
705 struct index_state
*istate
= s
->repo
->index
;
708 for (i
= 0; i
< istate
->cache_nr
; i
++) {
709 struct string_list_item
*it
;
710 struct wt_status_change_data
*d
;
711 const struct cache_entry
*ce
= istate
->cache
[i
];
713 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
715 if (ce_intent_to_add(ce
))
717 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
719 * This is a sparse directory entry, so we want to collect all
720 * of the added files within the tree. This requires recursively
721 * expanding the trees to find the elements that are new in this
722 * tree and marking them with DIFF_STATUS_ADDED.
724 struct strbuf base
= STRBUF_INIT
;
725 struct pathspec ps
= { 0 };
726 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
732 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
733 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
734 add_file_to_list
, s
);
738 it
= string_list_insert(&s
->change
, ce
->name
);
745 d
->index_status
= DIFF_STATUS_UNMERGED
;
746 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
748 * Don't bother setting {mode,oid}_{head,index} since the print
749 * code will output the stage values directly and not use the
750 * values in these fields.
754 d
->index_status
= DIFF_STATUS_ADDED
;
755 /* Leave {mode,oid}_head zero for adds. */
756 d
->mode_index
= ce
->ce_mode
;
757 oidcpy(&d
->oid_index
, &ce
->oid
);
763 static void wt_status_collect_untracked(struct wt_status
*s
)
766 struct dir_struct dir
= DIR_INIT
;
767 uint64_t t_begin
= getnanotime();
768 struct index_state
*istate
= s
->repo
->index
;
770 if (!s
->show_untracked_files
)
773 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
775 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
776 if (s
->show_ignored_mode
) {
777 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
779 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
780 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
782 dir
.untracked
= istate
->untracked
;
785 setup_standard_excludes(&dir
);
787 fill_directory(&dir
, istate
, &s
->pathspec
);
789 for (i
= 0; i
< dir
.nr
; i
++) {
790 struct dir_entry
*ent
= dir
.entries
[i
];
791 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
792 string_list_insert(&s
->untracked
, ent
->name
);
795 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
796 struct dir_entry
*ent
= dir
.ignored
[i
];
797 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
798 string_list_insert(&s
->ignored
, ent
->name
);
803 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
804 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
807 static int has_unmerged(struct wt_status
*s
)
811 for (i
= 0; i
< s
->change
.nr
; i
++) {
812 struct wt_status_change_data
*d
;
813 d
= s
->change
.items
[i
].util
;
820 void wt_status_collect(struct wt_status
*s
)
822 trace2_region_enter("status", "worktrees", s
->repo
);
823 wt_status_collect_changes_worktree(s
);
824 trace2_region_leave("status", "worktrees", s
->repo
);
827 trace2_region_enter("status", "initial", s
->repo
);
828 wt_status_collect_changes_initial(s
);
829 trace2_region_leave("status", "initial", s
->repo
);
831 trace2_region_enter("status", "index", s
->repo
);
832 wt_status_collect_changes_index(s
);
833 trace2_region_leave("status", "index", s
->repo
);
836 trace2_region_enter("status", "untracked", s
->repo
);
837 wt_status_collect_untracked(s
);
838 trace2_region_leave("status", "untracked", s
->repo
);
840 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
841 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
845 void wt_status_collect_free_buffers(struct wt_status
*s
)
847 wt_status_state_free_buffers(&s
->state
);
850 void wt_status_state_free_buffers(struct wt_status_state
*state
)
852 FREE_AND_NULL(state
->branch
);
853 FREE_AND_NULL(state
->onto
);
854 FREE_AND_NULL(state
->detached_from
);
857 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
859 int shown_header
= 0;
862 for (i
= 0; i
< s
->change
.nr
; i
++) {
863 struct wt_status_change_data
*d
;
864 struct string_list_item
*it
;
865 it
= &(s
->change
.items
[i
]);
870 wt_longstatus_print_unmerged_header(s
);
873 wt_longstatus_print_unmerged_data(s
, it
);
876 wt_longstatus_print_trailer(s
);
880 static void wt_longstatus_print_updated(struct wt_status
*s
)
882 int shown_header
= 0;
885 for (i
= 0; i
< s
->change
.nr
; i
++) {
886 struct wt_status_change_data
*d
;
887 struct string_list_item
*it
;
888 it
= &(s
->change
.items
[i
]);
890 if (!d
->index_status
||
891 d
->index_status
== DIFF_STATUS_UNMERGED
)
894 wt_longstatus_print_cached_header(s
);
897 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
900 wt_longstatus_print_trailer(s
);
906 * 1 : some change but no delete
908 static int wt_status_check_worktree_changes(struct wt_status
*s
,
909 int *dirty_submodules
)
914 *dirty_submodules
= 0;
916 for (i
= 0; i
< s
->change
.nr
; i
++) {
917 struct wt_status_change_data
*d
;
918 d
= s
->change
.items
[i
].util
;
919 if (!d
->worktree_status
||
920 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
924 if (d
->dirty_submodule
)
925 *dirty_submodules
= 1;
926 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
932 static void wt_longstatus_print_changed(struct wt_status
*s
)
934 int i
, dirty_submodules
;
935 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
937 if (!worktree_changes
)
940 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
942 for (i
= 0; i
< s
->change
.nr
; i
++) {
943 struct wt_status_change_data
*d
;
944 struct string_list_item
*it
;
945 it
= &(s
->change
.items
[i
]);
947 if (!d
->worktree_status
||
948 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
950 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
952 wt_longstatus_print_trailer(s
);
955 static int stash_count_refs(struct object_id
*ooid UNUSED
,
956 struct object_id
*noid UNUSED
,
957 const char *email UNUSED
,
958 timestamp_t timestamp UNUSED
, int tz UNUSED
,
959 const char *message UNUSED
, void *cb_data
)
966 static int count_stash_entries(void)
969 for_each_reflog_ent("refs/stash", stash_count_refs
, &n
);
973 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
975 int stash_count
= count_stash_entries();
978 status_printf_ln(s
, GIT_COLOR_NORMAL
,
979 Q_("Your stash currently has %d entry",
980 "Your stash currently has %d entries", stash_count
),
984 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
986 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
987 struct strbuf cmd_stdout
= STRBUF_INIT
;
988 struct strbuf summary
= STRBUF_INIT
;
989 char *summary_content
;
991 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
993 strvec_push(&sm_summary
.args
, "submodule");
994 strvec_push(&sm_summary
.args
, "summary");
995 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
996 strvec_push(&sm_summary
.args
, "--for-status");
997 strvec_push(&sm_summary
.args
, "--summary-limit");
998 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
1000 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1002 sm_summary
.git_cmd
= 1;
1003 sm_summary
.no_stdin
= 1;
1005 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1007 /* prepend header, only if there's an actual output */
1008 if (cmd_stdout
.len
) {
1010 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1012 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1013 strbuf_addstr(&summary
, "\n\n");
1015 strbuf_addbuf(&summary
, &cmd_stdout
);
1016 strbuf_release(&cmd_stdout
);
1018 if (s
->display_comment_prefix
) {
1020 summary_content
= strbuf_detach(&summary
, &len
);
1021 strbuf_add_commented_lines(&summary
, summary_content
, len
);
1022 free(summary_content
);
1025 fputs(summary
.buf
, s
->fp
);
1026 strbuf_release(&summary
);
1029 static void wt_longstatus_print_other(struct wt_status
*s
,
1030 struct string_list
*l
,
1035 struct strbuf buf
= STRBUF_INIT
;
1036 static struct string_list output
= STRING_LIST_INIT_DUP
;
1037 struct column_options copts
;
1042 wt_longstatus_print_other_header(s
, what
, how
);
1044 for (i
= 0; i
< l
->nr
; i
++) {
1045 struct string_list_item
*it
;
1047 it
= &(l
->items
[i
]);
1048 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1049 if (column_active(s
->colopts
)) {
1050 string_list_append(&output
, path
);
1053 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1054 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1058 strbuf_release(&buf
);
1059 if (!column_active(s
->colopts
))
1062 strbuf_addf(&buf
, "%s%s\t%s",
1063 color(WT_STATUS_HEADER
, s
),
1064 s
->display_comment_prefix
? "#" : "",
1065 color(WT_STATUS_UNTRACKED
, s
));
1066 memset(&copts
, 0, sizeof(copts
));
1068 copts
.indent
= buf
.buf
;
1069 if (want_color(s
->use_color
))
1070 copts
.nl
= GIT_COLOR_RESET
"\n";
1071 print_columns(&output
, s
->colopts
, &copts
);
1072 string_list_clear(&output
, 0);
1073 strbuf_release(&buf
);
1075 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1078 size_t wt_status_locate_end(const char *s
, size_t len
)
1081 struct strbuf pattern
= STRBUF_INIT
;
1083 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1084 if (starts_with(s
, pattern
.buf
+ 1))
1086 else if ((p
= strstr(s
, pattern
.buf
)))
1088 strbuf_release(&pattern
);
1092 void wt_status_append_cut_line(struct strbuf
*buf
)
1094 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1096 strbuf_commented_addf(buf
, "%s", cut_line
);
1097 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1100 void wt_status_add_cut_line(FILE *fp
)
1102 struct strbuf buf
= STRBUF_INIT
;
1104 wt_status_append_cut_line(&buf
);
1106 strbuf_release(&buf
);
1109 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1111 struct rev_info rev
;
1112 struct setup_revision_opt opt
;
1113 int dirty_submodules
;
1114 const char *c
= color(WT_STATUS_HEADER
, s
);
1116 repo_init_revisions(s
->repo
, &rev
, NULL
);
1117 rev
.diffopt
.flags
.allow_textconv
= 1;
1118 rev
.diffopt
.ita_invisible_in_index
= 1;
1120 memset(&opt
, 0, sizeof(opt
));
1121 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1122 setup_revisions(0, NULL
, &rev
, &opt
);
1124 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1125 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1126 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1127 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1128 rev
.diffopt
.file
= s
->fp
;
1129 rev
.diffopt
.close_file
= 0;
1131 * If we're not going to stdout, then we definitely don't
1132 * want color, since we are going to the commit message
1133 * file (and even the "auto" setting won't work, since it
1134 * will have checked isatty on stdout). But we then do want
1135 * to insert the scissor line here to reliably remove the
1136 * diff before committing.
1138 if (s
->fp
!= stdout
) {
1139 rev
.diffopt
.use_color
= 0;
1140 wt_status_add_cut_line(s
->fp
);
1142 if (s
->verbose
> 1 && s
->committable
) {
1143 /* print_updated() printed a header, so do we */
1144 if (s
->fp
!= stdout
)
1145 wt_longstatus_print_trailer(s
);
1146 status_printf_ln(s
, c
, _("Changes to be committed:"));
1147 rev
.diffopt
.a_prefix
= "c/";
1148 rev
.diffopt
.b_prefix
= "i/";
1149 } /* else use prefix as per user config */
1150 run_diff_index(&rev
, 1);
1151 if (s
->verbose
> 1 &&
1152 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1153 status_printf_ln(s
, c
,
1154 "--------------------------------------------------");
1155 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1157 rev
.diffopt
.a_prefix
= "i/";
1158 rev
.diffopt
.b_prefix
= "w/";
1159 run_diff_files(&rev
, 0);
1161 release_revisions(&rev
);
1164 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1166 struct strbuf sb
= STRBUF_INIT
;
1167 const char *cp
, *ep
, *branch_name
;
1168 struct branch
*branch
;
1169 char comment_line_string
[3];
1171 uint64_t t_begin
= 0;
1173 assert(s
->branch
&& !s
->is_initial
);
1174 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1176 branch
= branch_get(branch_name
);
1178 t_begin
= getnanotime();
1180 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1183 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1184 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1185 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1186 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1187 strbuf_addf(&sb
, _("\n"
1188 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1189 "You can use '--no-ahead-behind' to avoid this.\n"),
1190 t_delta_in_ms
/ 1000.0);
1195 if (s
->display_comment_prefix
) {
1196 comment_line_string
[i
++] = comment_line_char
;
1197 comment_line_string
[i
++] = ' ';
1199 comment_line_string
[i
] = '\0';
1201 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1202 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1203 "%s%.*s", comment_line_string
,
1204 (int)(ep
- cp
), cp
);
1205 if (s
->display_comment_prefix
)
1206 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1210 strbuf_release(&sb
);
1213 static int uf_was_slow(struct wt_status
*s
)
1215 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1216 s
->untracked_in_ms
= 3250;
1217 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1220 static void show_merge_in_progress(struct wt_status
*s
,
1223 if (has_unmerged(s
)) {
1224 status_printf_ln(s
, color
, _("You have unmerged paths."));
1226 status_printf_ln(s
, color
,
1227 _(" (fix conflicts and run \"git commit\")"));
1228 status_printf_ln(s
, color
,
1229 _(" (use \"git merge --abort\" to abort the merge)"));
1232 status_printf_ln(s
, color
,
1233 _("All conflicts fixed but you are still merging."));
1235 status_printf_ln(s
, color
,
1236 _(" (use \"git commit\" to conclude merge)"));
1238 wt_longstatus_print_trailer(s
);
1241 static void show_am_in_progress(struct wt_status
*s
,
1246 status_printf_ln(s
, color
,
1247 _("You are in the middle of an am session."));
1248 if (s
->state
.am_empty_patch
)
1249 status_printf_ln(s
, color
,
1250 _("The current patch is empty."));
1252 am_empty_patch
= s
->state
.am_empty_patch
;
1253 if (!am_empty_patch
)
1254 status_printf_ln(s
, color
,
1255 _(" (fix conflicts and then run \"git am --continue\")"));
1256 status_printf_ln(s
, color
,
1257 _(" (use \"git am --skip\" to skip this patch)"));
1259 status_printf_ln(s
, color
,
1260 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1261 status_printf_ln(s
, color
,
1262 _(" (use \"git am --abort\" to restore the original branch)"));
1264 wt_longstatus_print_trailer(s
);
1267 static char *read_line_from_git_path(const char *filename
)
1269 struct strbuf buf
= STRBUF_INIT
;
1270 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1273 strbuf_release(&buf
);
1276 strbuf_getline_lf(&buf
, fp
);
1278 return strbuf_detach(&buf
, NULL
);
1280 strbuf_release(&buf
);
1285 static int split_commit_in_progress(struct wt_status
*s
)
1287 int split_in_progress
= 0;
1288 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1290 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1291 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1294 head
= read_line_from_git_path("HEAD");
1295 orig_head
= read_line_from_git_path("ORIG_HEAD");
1296 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1297 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1299 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1300 ; /* fall through, no split in progress */
1301 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1302 split_in_progress
= !!strcmp(head
, rebase_amend
);
1303 else if (strcmp(orig_head
, rebase_orig_head
))
1304 split_in_progress
= 1;
1309 free(rebase_orig_head
);
1311 return split_in_progress
;
1316 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1318 * "pick d6a2f03 some message"
1320 * The function assumes that the line does not contain useless spaces
1321 * before or after the command.
1323 static void abbrev_oid_in_line(struct strbuf
*line
)
1325 struct strbuf
**split
;
1328 if (starts_with(line
->buf
, "exec ") ||
1329 starts_with(line
->buf
, "x ") ||
1330 starts_with(line
->buf
, "label ") ||
1331 starts_with(line
->buf
, "l "))
1334 split
= strbuf_split_max(line
, ' ', 3);
1335 if (split
[0] && split
[1]) {
1336 struct object_id oid
;
1339 * strbuf_split_max left a space. Trim it and re-add
1340 * it after abbreviation.
1342 strbuf_trim(split
[1]);
1343 if (!get_oid(split
[1]->buf
, &oid
)) {
1344 strbuf_reset(split
[1]);
1345 strbuf_add_unique_abbrev(split
[1], &oid
,
1347 strbuf_addch(split
[1], ' ');
1349 for (i
= 0; split
[i
]; i
++)
1350 strbuf_addbuf(line
, split
[i
]);
1353 strbuf_list_free(split
);
1356 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1358 struct strbuf line
= STRBUF_INIT
;
1359 FILE *f
= fopen(git_path("%s", fname
), "r");
1362 if (errno
== ENOENT
)
1364 die_errno("Could not open file %s for reading",
1365 git_path("%s", fname
));
1367 while (!strbuf_getline_lf(&line
, f
)) {
1368 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1373 abbrev_oid_in_line(&line
);
1374 string_list_append(lines
, line
.buf
);
1377 strbuf_release(&line
);
1381 static void show_rebase_information(struct wt_status
*s
,
1384 if (s
->state
.rebase_interactive_in_progress
) {
1386 int nr_lines_to_show
= 2;
1388 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1389 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1391 read_rebase_todolist("rebase-merge/done", &have_done
);
1392 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1394 status_printf_ln(s
, color
,
1395 _("git-rebase-todo is missing."));
1396 if (have_done
.nr
== 0)
1397 status_printf_ln(s
, color
, _("No commands done."));
1399 status_printf_ln(s
, color
,
1400 Q_("Last command done (%"PRIuMAX
" command done):",
1401 "Last commands done (%"PRIuMAX
" commands done):",
1403 (uintmax_t)have_done
.nr
);
1404 for (i
= (have_done
.nr
> nr_lines_to_show
)
1405 ? have_done
.nr
- nr_lines_to_show
: 0;
1408 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1409 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1410 status_printf_ln(s
, color
,
1411 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1414 if (yet_to_do
.nr
== 0)
1415 status_printf_ln(s
, color
,
1416 _("No commands remaining."));
1418 status_printf_ln(s
, color
,
1419 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1420 "Next commands to do (%"PRIuMAX
" remaining commands):",
1422 (uintmax_t)yet_to_do
.nr
);
1423 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1424 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1426 status_printf_ln(s
, color
,
1427 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1429 string_list_clear(&yet_to_do
, 0);
1430 string_list_clear(&have_done
, 0);
1434 static void print_rebase_state(struct wt_status
*s
,
1437 if (s
->state
.branch
)
1438 status_printf_ln(s
, color
,
1439 _("You are currently rebasing branch '%s' on '%s'."),
1443 status_printf_ln(s
, color
,
1444 _("You are currently rebasing."));
1447 static void show_rebase_in_progress(struct wt_status
*s
,
1452 show_rebase_information(s
, color
);
1453 if (has_unmerged(s
)) {
1454 print_rebase_state(s
, color
);
1456 status_printf_ln(s
, color
,
1457 _(" (fix conflicts and then run \"git rebase --continue\")"));
1458 status_printf_ln(s
, color
,
1459 _(" (use \"git rebase --skip\" to skip this patch)"));
1460 status_printf_ln(s
, color
,
1461 _(" (use \"git rebase --abort\" to check out the original branch)"));
1463 } else if (s
->state
.rebase_in_progress
||
1464 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1465 print_rebase_state(s
, color
);
1467 status_printf_ln(s
, color
,
1468 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1469 } else if (split_commit_in_progress(s
)) {
1470 if (s
->state
.branch
)
1471 status_printf_ln(s
, color
,
1472 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1476 status_printf_ln(s
, color
,
1477 _("You are currently splitting a commit during a rebase."));
1479 status_printf_ln(s
, color
,
1480 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1482 if (s
->state
.branch
)
1483 status_printf_ln(s
, color
,
1484 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1488 status_printf_ln(s
, color
,
1489 _("You are currently editing a commit during a rebase."));
1490 if (s
->hints
&& !s
->amend
) {
1491 status_printf_ln(s
, color
,
1492 _(" (use \"git commit --amend\" to amend the current commit)"));
1493 status_printf_ln(s
, color
,
1494 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1497 wt_longstatus_print_trailer(s
);
1500 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1503 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1504 status_printf_ln(s
, color
,
1505 _("Cherry-pick currently in progress."));
1507 status_printf_ln(s
, color
,
1508 _("You are currently cherry-picking commit %s."),
1509 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
,
1513 if (has_unmerged(s
))
1514 status_printf_ln(s
, color
,
1515 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1516 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1517 status_printf_ln(s
, color
,
1518 _(" (run \"git cherry-pick --continue\" to continue)"));
1520 status_printf_ln(s
, color
,
1521 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1522 status_printf_ln(s
, color
,
1523 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1524 status_printf_ln(s
, color
,
1525 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1527 wt_longstatus_print_trailer(s
);
1530 static void show_revert_in_progress(struct wt_status
*s
,
1533 if (is_null_oid(&s
->state
.revert_head_oid
))
1534 status_printf_ln(s
, color
,
1535 _("Revert currently in progress."));
1537 status_printf_ln(s
, color
,
1538 _("You are currently reverting commit %s."),
1539 find_unique_abbrev(&s
->state
.revert_head_oid
,
1542 if (has_unmerged(s
))
1543 status_printf_ln(s
, color
,
1544 _(" (fix conflicts and run \"git revert --continue\")"));
1545 else if (is_null_oid(&s
->state
.revert_head_oid
))
1546 status_printf_ln(s
, color
,
1547 _(" (run \"git revert --continue\" to continue)"));
1549 status_printf_ln(s
, color
,
1550 _(" (all conflicts fixed: run \"git revert --continue\")"));
1551 status_printf_ln(s
, color
,
1552 _(" (use \"git revert --skip\" to skip this patch)"));
1553 status_printf_ln(s
, color
,
1554 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1556 wt_longstatus_print_trailer(s
);
1559 static void show_bisect_in_progress(struct wt_status
*s
,
1562 if (s
->state
.branch
)
1563 status_printf_ln(s
, color
,
1564 _("You are currently bisecting, started from branch '%s'."),
1567 status_printf_ln(s
, color
,
1568 _("You are currently bisecting."));
1570 status_printf_ln(s
, color
,
1571 _(" (use \"git bisect reset\" to get back to the original branch)"));
1572 wt_longstatus_print_trailer(s
);
1575 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1578 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1581 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1582 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1584 status_printf_ln(s
, color
,
1585 _("You are in a sparse checkout with %d%% of tracked files present."),
1586 s
->state
.sparse_checkout_percentage
);
1587 wt_longstatus_print_trailer(s
);
1591 * Extract branch information from rebase/bisect
1593 static char *get_branch(const struct worktree
*wt
, const char *path
)
1595 struct strbuf sb
= STRBUF_INIT
;
1596 struct object_id oid
;
1597 const char *branch_name
;
1599 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1602 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1603 strbuf_setlen(&sb
, sb
.len
- 1);
1606 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1607 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1608 else if (starts_with(sb
.buf
, "refs/"))
1610 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1612 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1613 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1617 return strbuf_detach(&sb
, NULL
);
1620 strbuf_release(&sb
);
1624 struct grab_1st_switch_cbdata
{
1626 struct object_id noid
;
1629 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1630 struct object_id
*noid
,
1631 const char *email UNUSED
,
1632 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1633 const char *message
, void *cb_data
)
1635 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1636 const char *target
= NULL
, *end
;
1638 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1640 target
= strstr(message
, " to ");
1643 target
+= strlen(" to ");
1644 strbuf_reset(&cb
->buf
);
1645 oidcpy(&cb
->noid
, noid
);
1646 end
= strchrnul(target
, '\n');
1647 strbuf_add(&cb
->buf
, target
, end
- target
);
1648 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1649 /* HEAD is relative. Resolve it to the right reflog entry. */
1650 strbuf_reset(&cb
->buf
);
1651 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1656 static void wt_status_get_detached_from(struct repository
*r
,
1657 struct wt_status_state
*state
)
1659 struct grab_1st_switch_cbdata cb
;
1660 struct commit
*commit
;
1661 struct object_id oid
;
1664 strbuf_init(&cb
.buf
, 0);
1665 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1666 strbuf_release(&cb
.buf
);
1670 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
, 1) == 1 &&
1671 /* oid is a commit? match without further lookup */
1672 (oideq(&cb
.noid
, &oid
) ||
1673 /* perhaps oid is a tag, try to dereference to a commit */
1674 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1675 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1676 const char *from
= ref
;
1677 if (!skip_prefix(from
, "refs/tags/", &from
))
1678 skip_prefix(from
, "refs/remotes/", &from
);
1679 state
->detached_from
= xstrdup(from
);
1681 state
->detached_from
=
1682 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1683 oidcpy(&state
->detached_oid
, &cb
.noid
);
1684 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1685 oideq(&oid
, &state
->detached_oid
);
1688 strbuf_release(&cb
.buf
);
1691 int wt_status_check_rebase(const struct worktree
*wt
,
1692 struct wt_status_state
*state
)
1696 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1697 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1698 state
->am_in_progress
= 1;
1699 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1700 state
->am_empty_patch
= 1;
1702 state
->rebase_in_progress
= 1;
1703 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1704 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1706 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1707 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1708 state
->rebase_interactive_in_progress
= 1;
1710 state
->rebase_in_progress
= 1;
1711 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1712 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1718 int wt_status_check_bisect(const struct worktree
*wt
,
1719 struct wt_status_state
*state
)
1723 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1724 state
->bisect_in_progress
= 1;
1725 state
->branch
= get_branch(wt
, "BISECT_START");
1731 static void wt_status_check_sparse_checkout(struct repository
*r
,
1732 struct wt_status_state
*state
)
1734 int skip_worktree
= 0;
1737 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1739 * Don't compute percentage of checked out files if we
1740 * aren't in a sparse checkout or would get division by 0.
1742 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1746 if (r
->index
->sparse_index
) {
1747 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1751 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1752 struct cache_entry
*ce
= r
->index
->cache
[i
];
1753 if (ce_skip_worktree(ce
))
1757 state
->sparse_checkout_percentage
=
1758 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1761 void wt_status_get_state(struct repository
*r
,
1762 struct wt_status_state
*state
,
1763 int get_detached_from
)
1766 struct object_id oid
;
1767 enum replay_action action
;
1769 if (!stat(git_path_merge_head(r
), &st
)) {
1770 wt_status_check_rebase(NULL
, state
);
1771 state
->merge_in_progress
= 1;
1772 } else if (wt_status_check_rebase(NULL
, state
)) {
1774 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1775 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1776 state
->cherry_pick_in_progress
= 1;
1777 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1779 wt_status_check_bisect(NULL
, state
);
1780 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1781 !get_oid("REVERT_HEAD", &oid
)) {
1782 state
->revert_in_progress
= 1;
1783 oidcpy(&state
->revert_head_oid
, &oid
);
1785 if (!sequencer_get_last_command(r
, &action
)) {
1786 if (action
== REPLAY_PICK
) {
1787 state
->cherry_pick_in_progress
= 1;
1788 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1790 state
->revert_in_progress
= 1;
1791 oidcpy(&state
->revert_head_oid
, null_oid());
1794 if (get_detached_from
)
1795 wt_status_get_detached_from(r
, state
);
1796 wt_status_check_sparse_checkout(r
, state
);
1799 static void wt_longstatus_print_state(struct wt_status
*s
)
1801 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1802 struct wt_status_state
*state
= &s
->state
;
1804 if (state
->merge_in_progress
) {
1805 if (state
->rebase_interactive_in_progress
) {
1806 show_rebase_information(s
, state_color
);
1809 show_merge_in_progress(s
, state_color
);
1810 } else if (state
->am_in_progress
)
1811 show_am_in_progress(s
, state_color
);
1812 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1813 show_rebase_in_progress(s
, state_color
);
1814 else if (state
->cherry_pick_in_progress
)
1815 show_cherry_pick_in_progress(s
, state_color
);
1816 else if (state
->revert_in_progress
)
1817 show_revert_in_progress(s
, state_color
);
1818 if (state
->bisect_in_progress
)
1819 show_bisect_in_progress(s
, state_color
);
1821 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1822 show_sparse_checkout_in_use(s
, state_color
);
1825 static void wt_longstatus_print(struct wt_status
*s
)
1827 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1828 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1829 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1832 const char *on_what
= _("On branch ");
1833 const char *branch_name
= s
->branch
;
1834 if (!strcmp(branch_name
, "HEAD")) {
1835 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1836 if (s
->state
.rebase_in_progress
||
1837 s
->state
.rebase_interactive_in_progress
) {
1838 if (s
->state
.rebase_interactive_in_progress
)
1839 on_what
= _("interactive rebase in progress; onto ");
1841 on_what
= _("rebase in progress; onto ");
1842 branch_name
= s
->state
.onto
;
1843 } else if (s
->state
.detached_from
) {
1844 branch_name
= s
->state
.detached_from
;
1845 if (s
->state
.detached_at
)
1846 on_what
= _("HEAD detached at ");
1848 on_what
= _("HEAD detached from ");
1851 on_what
= _("Not currently on any branch.");
1854 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1855 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1856 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1857 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1859 wt_longstatus_print_tracking(s
);
1862 wt_longstatus_print_state(s
);
1864 if (s
->is_initial
) {
1865 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1866 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1868 ? _("Initial commit")
1869 : _("No commits yet"));
1870 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1873 wt_longstatus_print_updated(s
);
1874 wt_longstatus_print_unmerged(s
);
1875 wt_longstatus_print_changed(s
);
1876 if (s
->submodule_summary
&&
1877 (!s
->ignore_submodule_arg
||
1878 strcmp(s
->ignore_submodule_arg
, "all"))) {
1879 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1880 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1882 if (s
->show_untracked_files
) {
1883 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1884 if (s
->show_ignored_mode
)
1885 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1886 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1887 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1888 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1889 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1890 _("It took %.2f seconds to enumerate untracked files,\n"
1891 "but the results were cached, and subsequent runs may be faster."),
1892 s
->untracked_in_ms
/ 1000.0);
1894 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1895 _("It took %.2f seconds to enumerate untracked files."),
1896 s
->untracked_in_ms
/ 1000.0);
1898 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1899 _("See 'git help status' for information on how to improve this."));
1900 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1902 } else if (s
->committable
)
1903 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1905 ? _(" (use -u option to show untracked files)") : "");
1908 wt_longstatus_print_verbose(s
);
1909 if (!s
->committable
) {
1911 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1914 else if (s
->workdir_dirty
) {
1916 fprintf(s
->fp
, _("no changes added to commit "
1917 "(use \"git add\" and/or "
1918 "\"git commit -a\")\n"));
1920 fprintf(s
->fp
, _("no changes added to "
1922 } else if (s
->untracked
.nr
) {
1924 fprintf(s
->fp
, _("nothing added to commit but "
1925 "untracked files present (use "
1926 "\"git add\" to track)\n"));
1928 fprintf(s
->fp
, _("nothing added to commit but "
1929 "untracked files present\n"));
1930 } else if (s
->is_initial
) {
1932 fprintf(s
->fp
, _("nothing to commit (create/"
1933 "copy files and use \"git "
1934 "add\" to track)\n"));
1936 fprintf(s
->fp
, _("nothing to commit\n"));
1937 } else if (!s
->show_untracked_files
) {
1939 fprintf(s
->fp
, _("nothing to commit (use -u to "
1940 "show untracked files)\n"));
1942 fprintf(s
->fp
, _("nothing to commit\n"));
1944 fprintf(s
->fp
, _("nothing to commit, working tree "
1948 wt_longstatus_print_stash_summary(s
);
1951 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1952 struct wt_status
*s
)
1954 struct wt_status_change_data
*d
= it
->util
;
1955 const char *how
= "??";
1957 switch (d
->stagemask
) {
1958 case 1: how
= "DD"; break; /* both deleted */
1959 case 2: how
= "AU"; break; /* added by us */
1960 case 3: how
= "UD"; break; /* deleted by them */
1961 case 4: how
= "UA"; break; /* added by them */
1962 case 5: how
= "DU"; break; /* deleted by us */
1963 case 6: how
= "AA"; break; /* both added */
1964 case 7: how
= "UU"; break; /* both modified */
1966 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1967 if (s
->null_termination
) {
1968 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1970 struct strbuf onebuf
= STRBUF_INIT
;
1972 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1973 fprintf(s
->fp
, " %s\n", one
);
1974 strbuf_release(&onebuf
);
1978 static void wt_shortstatus_status(struct string_list_item
*it
,
1979 struct wt_status
*s
)
1981 struct wt_status_change_data
*d
= it
->util
;
1983 if (d
->index_status
)
1984 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1987 if (d
->worktree_status
)
1988 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1992 if (s
->null_termination
) {
1993 fprintf(s
->fp
, "%s%c", it
->string
, 0);
1994 if (d
->rename_source
)
1995 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
1997 struct strbuf onebuf
= STRBUF_INIT
;
2000 if (d
->rename_source
) {
2001 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2002 QUOTE_PATH_QUOTE_SP
);
2003 fprintf(s
->fp
, "%s -> ", one
);
2004 strbuf_release(&onebuf
);
2006 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2007 fprintf(s
->fp
, "%s\n", one
);
2008 strbuf_release(&onebuf
);
2012 static void wt_shortstatus_other(struct string_list_item
*it
,
2013 struct wt_status
*s
, const char *sign
)
2015 if (s
->null_termination
) {
2016 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2018 struct strbuf onebuf
= STRBUF_INIT
;
2020 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2021 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2022 fprintf(s
->fp
, " %s\n", one
);
2023 strbuf_release(&onebuf
);
2027 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2029 struct branch
*branch
;
2030 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2031 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2032 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2036 const char *branch_name
;
2037 int num_ours
, num_theirs
, sti
;
2038 int upstream_is_gone
= 0;
2040 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2044 branch_name
= s
->branch
;
2046 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2049 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2051 if (!strcmp(s
->branch
, "HEAD")) {
2052 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2053 LABEL(N_("HEAD (no branch)")));
2057 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2059 branch
= branch_get(branch_name
);
2061 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2063 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2064 0, s
->ahead_behind_flags
);
2069 upstream_is_gone
= 1;
2072 short_base
= shorten_unambiguous_ref(base
, 0);
2073 color_fprintf(s
->fp
, header_color
, "...");
2074 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2077 if (!upstream_is_gone
&& !sti
)
2080 color_fprintf(s
->fp
, header_color
, " [");
2081 if (upstream_is_gone
) {
2082 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2083 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2084 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2085 } else if (!num_ours
) {
2086 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2087 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2088 } else if (!num_theirs
) {
2089 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2090 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2092 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2093 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2094 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2095 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2098 color_fprintf(s
->fp
, header_color
, "]");
2100 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2103 static void wt_shortstatus_print(struct wt_status
*s
)
2105 struct string_list_item
*it
;
2108 wt_shortstatus_print_tracking(s
);
2110 for_each_string_list_item(it
, &s
->change
) {
2111 struct wt_status_change_data
*d
= it
->util
;
2114 wt_shortstatus_unmerged(it
, s
);
2116 wt_shortstatus_status(it
, s
);
2118 for_each_string_list_item(it
, &s
->untracked
)
2119 wt_shortstatus_other(it
, s
, "??");
2121 for_each_string_list_item(it
, &s
->ignored
)
2122 wt_shortstatus_other(it
, s
, "!!");
2125 static void wt_porcelain_print(struct wt_status
*s
)
2128 s
->relative_paths
= 0;
2131 wt_shortstatus_print(s
);
2135 * Print branch information for porcelain v2 output. These lines
2136 * are printed when the '--branch' parameter is given.
2138 * # branch.oid <commit><eol>
2139 * # branch.head <head><eol>
2140 * [# branch.upstream <upstream><eol>
2141 * [# branch.ab +<ahead> -<behind><eol>]]
2143 * <commit> ::= the current commit hash or the literal
2144 * "(initial)" to indicate an initialized repo
2147 * <head> ::= <branch_name> the current branch name or
2148 * "(detached)" literal when detached head or
2149 * "(unknown)" when something is wrong.
2151 * <upstream> ::= the upstream branch name, when set.
2153 * <ahead> ::= integer ahead value or '?'.
2155 * <behind> ::= integer behind value or '?'.
2157 * The end-of-line is defined by the -z flag.
2159 * <eol> ::= NUL when -z,
2162 * When an upstream is set and present, the 'branch.ab' line will
2163 * be printed with the ahead/behind counts for the branch and the
2164 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2165 * are different, '?' will be substituted for the actual count.
2167 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2169 struct branch
*branch
;
2171 const char *branch_name
;
2172 int ab_info
, nr_ahead
, nr_behind
;
2173 char eol
= s
->null_termination
? '\0' : '\n';
2175 fprintf(s
->fp
, "# branch.oid %s%c",
2176 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2180 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2182 if (!strcmp(s
->branch
, "HEAD")) {
2183 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2185 if (s
->state
.rebase_in_progress
||
2186 s
->state
.rebase_interactive_in_progress
)
2187 branch_name
= s
->state
.onto
;
2188 else if (s
->state
.detached_from
)
2189 branch_name
= s
->state
.detached_from
;
2194 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2196 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2199 /* Lookup stats on the upstream tracking branch, if set. */
2200 branch
= branch_get(branch_name
);
2202 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2203 &base
, 0, s
->ahead_behind_flags
);
2205 base
= shorten_unambiguous_ref(base
, 0);
2206 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2211 if (nr_ahead
|| nr_behind
)
2212 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2213 nr_ahead
, nr_behind
, eol
);
2215 fprintf(s
->fp
, "# branch.ab +? -?%c",
2217 } else if (!ab_info
) {
2219 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2226 * Print the stash count in a porcelain-friendly format
2228 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2230 int stash_count
= count_stash_entries();
2231 char eol
= s
->null_termination
? '\0' : '\n';
2233 if (stash_count
> 0)
2234 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2238 * Convert various submodule status values into a
2239 * fixed-length string of characters in the buffer provided.
2241 static void wt_porcelain_v2_submodule_state(
2242 struct wt_status_change_data
*d
,
2245 if (S_ISGITLINK(d
->mode_head
) ||
2246 S_ISGITLINK(d
->mode_index
) ||
2247 S_ISGITLINK(d
->mode_worktree
)) {
2249 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2250 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2251 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2262 * Fix-up changed entries before we print them.
2264 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2266 struct wt_status_change_data
*d
= it
->util
;
2268 if (!d
->index_status
) {
2270 * This entry is unchanged in the index (relative to the head).
2271 * Therefore, the collect_updated_cb was never called for this
2272 * entry (during the head-vs-index scan) and so the head column
2273 * fields were never set.
2275 * We must have data for the index column (from the
2276 * index-vs-worktree scan (otherwise, this entry should not be
2277 * in the list of changes)).
2279 * Copy index column fields to the head column, so that our
2280 * output looks complete.
2282 assert(d
->mode_head
== 0);
2283 d
->mode_head
= d
->mode_index
;
2284 oidcpy(&d
->oid_head
, &d
->oid_index
);
2287 if (!d
->worktree_status
) {
2289 * This entry is unchanged in the worktree (relative to the index).
2290 * Therefore, the collect_changed_cb was never called for this entry
2291 * (during the index-vs-worktree scan) and so the worktree column
2292 * fields were never set.
2294 * We must have data for the index column (from the head-vs-index
2297 * Copy the index column fields to the worktree column so that
2298 * our output looks complete.
2300 * Note that we only have a mode field in the worktree column
2301 * because the scan code tries really hard to not have to compute it.
2303 assert(d
->mode_worktree
== 0);
2304 d
->mode_worktree
= d
->mode_index
;
2309 * Print porcelain v2 info for tracked entries with changes.
2311 static void wt_porcelain_v2_print_changed_entry(
2312 struct string_list_item
*it
,
2313 struct wt_status
*s
)
2315 struct wt_status_change_data
*d
= it
->util
;
2316 struct strbuf buf
= STRBUF_INIT
;
2317 struct strbuf buf_from
= STRBUF_INIT
;
2318 const char *path
= NULL
;
2319 const char *path_from
= NULL
;
2321 char submodule_token
[5];
2322 char sep_char
, eol_char
;
2324 wt_porcelain_v2_fix_up_changed(it
);
2325 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2327 key
[0] = d
->index_status
? d
->index_status
: '.';
2328 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2331 if (s
->null_termination
) {
2333 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2334 * A single NUL character separates them.
2339 path_from
= d
->rename_source
;
2342 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2343 * The source path is only present when necessary.
2344 * A single TAB separates them (because paths can contain spaces
2345 * which are not escaped and C-quoting does escape TAB characters).
2349 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2350 if (d
->rename_source
)
2351 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2355 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2356 key
, submodule_token
,
2357 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2358 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2359 d
->rename_status
, d
->rename_score
,
2360 path
, sep_char
, path_from
, eol_char
);
2362 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2363 key
, submodule_token
,
2364 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2365 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2368 strbuf_release(&buf
);
2369 strbuf_release(&buf_from
);
2373 * Print porcelain v2 status info for unmerged entries.
2375 static void wt_porcelain_v2_print_unmerged_entry(
2376 struct string_list_item
*it
,
2377 struct wt_status
*s
)
2379 struct wt_status_change_data
*d
= it
->util
;
2380 struct index_state
*istate
= s
->repo
->index
;
2381 const struct cache_entry
*ce
;
2382 struct strbuf buf_index
= STRBUF_INIT
;
2383 const char *path_index
= NULL
;
2384 int pos
, stage
, sum
;
2387 struct object_id oid
;
2390 char submodule_token
[5];
2391 char unmerged_prefix
= 'u';
2392 char eol_char
= s
->null_termination
? '\0' : '\n';
2394 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2396 switch (d
->stagemask
) {
2397 case 1: key
= "DD"; break; /* both deleted */
2398 case 2: key
= "AU"; break; /* added by us */
2399 case 3: key
= "UD"; break; /* deleted by them */
2400 case 4: key
= "UA"; break; /* added by them */
2401 case 5: key
= "DU"; break; /* deleted by us */
2402 case 6: key
= "AA"; break; /* both added */
2403 case 7: key
= "UU"; break; /* both modified */
2405 BUG("unhandled unmerged status %x", d
->stagemask
);
2409 * Disregard d.aux.porcelain_v2 data that we accumulated
2410 * for the head and index columns during the scans and
2411 * replace with the actual stage data.
2413 * Note that this is a last-one-wins for each the individual
2414 * stage [123] columns in the event of multiple cache entries
2417 memset(stages
, 0, sizeof(stages
));
2419 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2422 while (pos
< istate
->cache_nr
) {
2423 ce
= istate
->cache
[pos
++];
2424 stage
= ce_stage(ce
);
2425 if (strcmp(ce
->name
, it
->string
) || !stage
)
2427 stages
[stage
- 1].mode
= ce
->ce_mode
;
2428 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2429 sum
|= (1 << (stage
- 1));
2431 if (sum
!= d
->stagemask
)
2432 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2434 if (s
->null_termination
)
2435 path_index
= it
->string
;
2437 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2439 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2440 unmerged_prefix
, key
, submodule_token
,
2441 stages
[0].mode
, /* stage 1 */
2442 stages
[1].mode
, /* stage 2 */
2443 stages
[2].mode
, /* stage 3 */
2445 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2446 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2447 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2451 strbuf_release(&buf_index
);
2455 * Print porcelain V2 status info for untracked and ignored entries.
2457 static void wt_porcelain_v2_print_other(
2458 struct string_list_item
*it
,
2459 struct wt_status
*s
,
2462 struct strbuf buf
= STRBUF_INIT
;
2466 if (s
->null_termination
) {
2470 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2474 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2476 strbuf_release(&buf
);
2480 * Print porcelain V2 status.
2483 * [<v2_changed_items>]*
2484 * [<v2_unmerged_items>]*
2485 * [<v2_untracked_items>]*
2486 * [<v2_ignored_items>]*
2489 static void wt_porcelain_v2_print(struct wt_status
*s
)
2491 struct wt_status_change_data
*d
;
2492 struct string_list_item
*it
;
2496 wt_porcelain_v2_print_tracking(s
);
2499 wt_porcelain_v2_print_stash(s
);
2501 for (i
= 0; i
< s
->change
.nr
; i
++) {
2502 it
= &(s
->change
.items
[i
]);
2505 wt_porcelain_v2_print_changed_entry(it
, s
);
2508 for (i
= 0; i
< s
->change
.nr
; i
++) {
2509 it
= &(s
->change
.items
[i
]);
2512 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2515 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2516 it
= &(s
->untracked
.items
[i
]);
2517 wt_porcelain_v2_print_other(it
, s
, '?');
2520 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2521 it
= &(s
->ignored
.items
[i
]);
2522 wt_porcelain_v2_print_other(it
, s
, '!');
2526 void wt_status_print(struct wt_status
*s
)
2528 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2529 trace2_data_intmax("status", s
->repo
, "count/untracked",
2531 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2533 trace2_region_enter("status", "print", s
->repo
);
2535 switch (s
->status_format
) {
2536 case STATUS_FORMAT_SHORT
:
2537 wt_shortstatus_print(s
);
2539 case STATUS_FORMAT_PORCELAIN
:
2540 wt_porcelain_print(s
);
2542 case STATUS_FORMAT_PORCELAIN_V2
:
2543 wt_porcelain_v2_print(s
);
2545 case STATUS_FORMAT_UNSPECIFIED
:
2546 BUG("finalize_deferred_config() should have been called");
2548 case STATUS_FORMAT_NONE
:
2549 case STATUS_FORMAT_LONG
:
2550 wt_longstatus_print(s
);
2554 trace2_region_leave("status", "print", s
->repo
);
2558 * Returns 1 if there are unstaged changes, 0 otherwise.
2560 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2562 struct rev_info rev_info
;
2565 repo_init_revisions(r
, &rev_info
, NULL
);
2566 if (ignore_submodules
) {
2567 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2568 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2570 rev_info
.diffopt
.flags
.quick
= 1;
2571 diff_setup_done(&rev_info
.diffopt
);
2572 result
= run_diff_files(&rev_info
, 0);
2573 result
= diff_result_code(&rev_info
.diffopt
, result
);
2574 release_revisions(&rev_info
);
2579 * Returns 1 if there are uncommitted changes, 0 otherwise.
2581 int has_uncommitted_changes(struct repository
*r
,
2582 int ignore_submodules
)
2584 struct rev_info rev_info
;
2587 if (is_index_unborn(r
->index
))
2590 repo_init_revisions(r
, &rev_info
, NULL
);
2591 if (ignore_submodules
)
2592 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2593 rev_info
.diffopt
.flags
.quick
= 1;
2595 add_head_to_pending(&rev_info
);
2596 if (!rev_info
.pending
.nr
) {
2598 * We have no head (or it's corrupt); use the empty tree,
2599 * which will complain if the index is non-empty.
2601 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2602 add_pending_object(&rev_info
, &tree
->object
, "");
2605 diff_setup_done(&rev_info
.diffopt
);
2606 result
= run_diff_index(&rev_info
, 1);
2607 result
= diff_result_code(&rev_info
.diffopt
, result
);
2608 release_revisions(&rev_info
);
2613 * If the work tree has unstaged or uncommitted changes, dies with the
2614 * appropriate message.
2616 int require_clean_work_tree(struct repository
*r
,
2619 int ignore_submodules
,
2622 struct lock_file lock_file
= LOCK_INIT
;
2625 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2626 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2628 repo_update_index_if_able(r
, &lock_file
);
2629 rollback_lock_file(&lock_file
);
2631 if (has_unstaged_changes(r
, ignore_submodules
)) {
2632 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2633 error(_("cannot %s: You have unstaged changes."), _(action
));
2637 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2639 error(_("additionally, your index contains uncommitted changes."));
2641 error(_("cannot %s: Your index contains uncommitted changes."),