11 #include "run-command.h"
15 #include "submodule.h"
21 #include "sequencer.h"
22 #include "fsmonitor-settings.h"
24 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
25 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
27 static const char cut_line
[] =
28 "------------------------ >8 ------------------------\n";
30 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
31 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
32 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
33 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
34 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
35 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
36 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
37 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
38 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
39 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
42 static const char *color(int slot
, struct wt_status
*s
)
45 if (want_color(s
->use_color
))
46 c
= s
->color_palette
[slot
];
47 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
48 c
= s
->color_palette
[WT_STATUS_HEADER
];
52 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
53 const char *fmt
, va_list ap
, const char *trail
)
55 struct strbuf sb
= STRBUF_INIT
;
56 struct strbuf linebuf
= STRBUF_INIT
;
57 const char *line
, *eol
;
59 strbuf_vaddf(&sb
, fmt
, ap
);
61 if (s
->display_comment_prefix
) {
62 strbuf_addch(&sb
, comment_line_char
);
64 strbuf_addch(&sb
, ' ');
66 color_print_strbuf(s
->fp
, color
, &sb
);
68 fprintf(s
->fp
, "%s", trail
);
72 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
73 eol
= strchr(line
, '\n');
75 strbuf_reset(&linebuf
);
76 if (at_bol
&& s
->display_comment_prefix
) {
77 strbuf_addch(&linebuf
, comment_line_char
);
78 if (*line
!= '\n' && *line
!= '\t')
79 strbuf_addch(&linebuf
, ' ');
82 strbuf_add(&linebuf
, line
, eol
- line
);
84 strbuf_addstr(&linebuf
, line
);
85 color_print_strbuf(s
->fp
, color
, &linebuf
);
93 fprintf(s
->fp
, "%s", trail
);
94 strbuf_release(&linebuf
);
98 void status_printf_ln(struct wt_status
*s
, const char *color
,
104 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
108 void status_printf(struct wt_status
*s
, const char *color
,
109 const char *fmt
, ...)
114 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
118 static void status_printf_more(struct wt_status
*s
, const char *color
,
119 const char *fmt
, ...)
124 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
128 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
130 memset(s
, 0, sizeof(*s
));
132 memcpy(s
->color_palette
, default_wt_status_colors
,
133 sizeof(default_wt_status_colors
));
134 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
136 s
->relative_paths
= 1;
137 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
138 s
->reference
= "HEAD";
140 s
->index_file
= get_index_file();
141 s
->change
.strdup_strings
= 1;
142 s
->untracked
.strdup_strings
= 1;
143 s
->ignored
.strdup_strings
= 1;
144 s
->show_branch
= -1; /* unspecified */
146 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
147 s
->display_comment_prefix
= 0;
148 s
->detect_rename
= -1;
149 s
->rename_score
= -1;
150 s
->rename_limit
= -1;
153 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
156 int del_mod_conflict
= 0;
157 int both_deleted
= 0;
159 const char *c
= color(WT_STATUS_HEADER
, s
);
161 status_printf_ln(s
, c
, _("Unmerged paths:"));
163 for (i
= 0; i
< s
->change
.nr
; i
++) {
164 struct string_list_item
*it
= &(s
->change
.items
[i
]);
165 struct wt_status_change_data
*d
= it
->util
;
167 switch (d
->stagemask
) {
175 del_mod_conflict
= 1;
185 if (s
->whence
!= FROM_COMMIT
)
187 else if (!s
->is_initial
) {
188 if (!strcmp(s
->reference
, "HEAD"))
189 status_printf_ln(s
, c
,
190 _(" (use \"git restore --staged <file>...\" to unstage)"));
192 status_printf_ln(s
, c
,
193 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
196 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
199 if (!del_mod_conflict
)
200 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
202 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
203 } else if (!del_mod_conflict
&& !not_deleted
) {
204 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
206 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
210 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
212 const char *c
= color(WT_STATUS_HEADER
, s
);
214 status_printf_ln(s
, c
, _("Changes to be committed:"));
217 if (s
->whence
!= FROM_COMMIT
)
218 ; /* NEEDSWORK: use "git reset --unresolve"??? */
219 else if (!s
->is_initial
) {
220 if (!strcmp(s
->reference
, "HEAD"))
221 status_printf_ln(s
, c
222 , _(" (use \"git restore --staged <file>...\" to unstage)"));
224 status_printf_ln(s
, c
,
225 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
228 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
231 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
233 int has_dirty_submodules
)
235 const char *c
= color(WT_STATUS_HEADER
, s
);
237 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
241 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
243 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
244 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
245 if (has_dirty_submodules
)
246 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
249 static void wt_longstatus_print_other_header(struct wt_status
*s
,
253 const char *c
= color(WT_STATUS_HEADER
, s
);
254 status_printf_ln(s
, c
, "%s:", what
);
257 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
260 static void wt_longstatus_print_trailer(struct wt_status
*s
)
262 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
265 static const char *wt_status_unmerged_status_string(int stagemask
)
269 return _("both deleted:");
271 return _("added by us:");
273 return _("deleted by them:");
275 return _("added by them:");
277 return _("deleted by us:");
279 return _("both added:");
281 return _("both modified:");
283 BUG("unhandled unmerged status %x", stagemask
);
287 static const char *wt_status_diff_status_string(int status
)
290 case DIFF_STATUS_ADDED
:
291 return _("new file:");
292 case DIFF_STATUS_COPIED
:
294 case DIFF_STATUS_DELETED
:
295 return _("deleted:");
296 case DIFF_STATUS_MODIFIED
:
297 return _("modified:");
298 case DIFF_STATUS_RENAMED
:
299 return _("renamed:");
300 case DIFF_STATUS_TYPE_CHANGED
:
301 return _("typechange:");
302 case DIFF_STATUS_UNKNOWN
:
303 return _("unknown:");
304 case DIFF_STATUS_UNMERGED
:
305 return _("unmerged:");
311 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
315 for (i
= minval
; i
<= maxval
; i
++) {
316 const char *s
= label(i
);
317 int len
= s
? utf8_strwidth(s
) : 0;
324 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
325 struct string_list_item
*it
)
327 const char *c
= color(WT_STATUS_UNMERGED
, s
);
328 struct wt_status_change_data
*d
= it
->util
;
329 struct strbuf onebuf
= STRBUF_INIT
;
330 static char *padding
;
331 static int label_width
;
332 const char *one
, *how
;
336 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
337 label_width
+= strlen(" ");
338 padding
= xmallocz(label_width
);
339 memset(padding
, ' ', label_width
);
342 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
343 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
345 how
= wt_status_unmerged_status_string(d
->stagemask
);
346 len
= label_width
- utf8_strwidth(how
);
347 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
348 strbuf_release(&onebuf
);
351 static void wt_longstatus_print_change_data(struct wt_status
*s
,
353 struct string_list_item
*it
)
355 struct wt_status_change_data
*d
= it
->util
;
356 const char *c
= color(change_type
, s
);
360 const char *one
, *two
;
361 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
362 struct strbuf extra
= STRBUF_INIT
;
363 static char *padding
;
364 static int label_width
;
369 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
370 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
371 label_width
+= strlen(" ");
372 padding
= xmallocz(label_width
);
373 memset(padding
, ' ', label_width
);
376 one_name
= two_name
= it
->string
;
377 switch (change_type
) {
378 case WT_STATUS_UPDATED
:
379 status
= d
->index_status
;
381 case WT_STATUS_CHANGED
:
382 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
383 strbuf_addstr(&extra
, " (");
384 if (d
->new_submodule_commits
)
385 strbuf_addstr(&extra
, _("new commits, "));
386 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
387 strbuf_addstr(&extra
, _("modified content, "));
388 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
389 strbuf_addstr(&extra
, _("untracked content, "));
390 strbuf_setlen(&extra
, extra
.len
- 2);
391 strbuf_addch(&extra
, ')');
393 status
= d
->worktree_status
;
396 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
401 * Only pick up the rename it's relevant. If the rename is for
402 * the changed section and we're printing the updated section,
405 if (d
->rename_status
== status
)
406 one_name
= d
->rename_source
;
408 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
409 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
411 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
412 what
= wt_status_diff_status_string(status
);
414 BUG("unhandled diff status %c", status
);
415 len
= label_width
- utf8_strwidth(what
);
417 if (one_name
!= two_name
)
418 status_printf_more(s
, c
, "%s%.*s%s -> %s",
419 what
, len
, padding
, one
, two
);
421 status_printf_more(s
, c
, "%s%.*s%s",
422 what
, len
, padding
, one
);
424 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
425 strbuf_release(&extra
);
427 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
428 strbuf_release(&onebuf
);
429 strbuf_release(&twobuf
);
432 static char short_submodule_status(struct wt_status_change_data
*d
)
434 if (d
->new_submodule_commits
)
436 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
438 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
440 return d
->worktree_status
;
443 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
444 struct diff_options
*options UNUSED
,
447 struct wt_status
*s
= data
;
452 s
->workdir_dirty
= 1;
453 for (i
= 0; i
< q
->nr
; i
++) {
454 struct diff_filepair
*p
;
455 struct string_list_item
*it
;
456 struct wt_status_change_data
*d
;
459 it
= string_list_insert(&s
->change
, p
->two
->path
);
465 if (!d
->worktree_status
)
466 d
->worktree_status
= p
->status
;
467 if (S_ISGITLINK(p
->two
->mode
)) {
468 d
->dirty_submodule
= p
->two
->dirty_submodule
;
469 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
471 if (s
->status_format
== STATUS_FORMAT_SHORT
)
472 d
->worktree_status
= short_submodule_status(d
);
476 case DIFF_STATUS_ADDED
:
477 d
->mode_worktree
= p
->two
->mode
;
480 case DIFF_STATUS_DELETED
:
481 d
->mode_index
= p
->one
->mode
;
482 oidcpy(&d
->oid_index
, &p
->one
->oid
);
483 /* mode_worktree is zero for a delete. */
486 case DIFF_STATUS_COPIED
:
487 case DIFF_STATUS_RENAMED
:
488 if (d
->rename_status
)
489 BUG("multiple renames on the same target? how?");
490 d
->rename_source
= xstrdup(p
->one
->path
);
491 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
492 d
->rename_status
= p
->status
;
494 case DIFF_STATUS_MODIFIED
:
495 case DIFF_STATUS_TYPE_CHANGED
:
496 case DIFF_STATUS_UNMERGED
:
497 d
->mode_index
= p
->one
->mode
;
498 d
->mode_worktree
= p
->two
->mode
;
499 oidcpy(&d
->oid_index
, &p
->one
->oid
);
503 BUG("unhandled diff-files status '%c'", p
->status
);
510 static int unmerged_mask(struct index_state
*istate
, const char *path
)
513 const struct cache_entry
*ce
;
515 pos
= index_name_pos(istate
, path
, strlen(path
));
521 while (pos
< istate
->cache_nr
) {
522 ce
= istate
->cache
[pos
++];
523 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
525 mask
|= (1 << (ce_stage(ce
) - 1));
530 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
531 struct diff_options
*options UNUSED
,
534 struct wt_status
*s
= data
;
537 for (i
= 0; i
< q
->nr
; i
++) {
538 struct diff_filepair
*p
;
539 struct string_list_item
*it
;
540 struct wt_status_change_data
*d
;
543 it
= string_list_insert(&s
->change
, p
->two
->path
);
549 if (!d
->index_status
)
550 d
->index_status
= p
->status
;
552 case DIFF_STATUS_ADDED
:
553 /* Leave {mode,oid}_head zero for an add. */
554 d
->mode_index
= p
->two
->mode
;
555 oidcpy(&d
->oid_index
, &p
->two
->oid
);
558 case DIFF_STATUS_DELETED
:
559 d
->mode_head
= p
->one
->mode
;
560 oidcpy(&d
->oid_head
, &p
->one
->oid
);
562 /* Leave {mode,oid}_index zero for a delete. */
565 case DIFF_STATUS_COPIED
:
566 case DIFF_STATUS_RENAMED
:
567 if (d
->rename_status
)
568 BUG("multiple renames on the same target? how?");
569 d
->rename_source
= xstrdup(p
->one
->path
);
570 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
571 d
->rename_status
= p
->status
;
573 case DIFF_STATUS_MODIFIED
:
574 case DIFF_STATUS_TYPE_CHANGED
:
575 d
->mode_head
= p
->one
->mode
;
576 d
->mode_index
= p
->two
->mode
;
577 oidcpy(&d
->oid_head
, &p
->one
->oid
);
578 oidcpy(&d
->oid_index
, &p
->two
->oid
);
581 case DIFF_STATUS_UNMERGED
:
582 d
->stagemask
= unmerged_mask(s
->repo
->index
,
585 * Don't bother setting {mode,oid}_{head,index} since the print
586 * code will output the stage values directly and not use the
587 * values in these fields.
592 BUG("unhandled diff-index status '%c'", p
->status
);
598 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
602 repo_init_revisions(s
->repo
, &rev
, NULL
);
603 setup_revisions(0, NULL
, &rev
, NULL
);
604 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
605 rev
.diffopt
.flags
.dirty_submodules
= 1;
606 rev
.diffopt
.ita_invisible_in_index
= 1;
607 if (!s
->show_untracked_files
)
608 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
609 if (s
->ignore_submodule_arg
) {
610 rev
.diffopt
.flags
.override_submodule_config
= 1;
611 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
612 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
613 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
614 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
615 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
616 rev
.diffopt
.format_callback_data
= s
;
617 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
618 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
619 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
620 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
621 run_diff_files(&rev
, 0);
622 release_revisions(&rev
);
625 static void wt_status_collect_changes_index(struct wt_status
*s
)
628 struct setup_revision_opt opt
;
630 repo_init_revisions(s
->repo
, &rev
, NULL
);
631 memset(&opt
, 0, sizeof(opt
));
632 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
633 setup_revisions(0, NULL
, &rev
, &opt
);
635 rev
.diffopt
.flags
.override_submodule_config
= 1;
636 rev
.diffopt
.ita_invisible_in_index
= 1;
637 if (s
->ignore_submodule_arg
) {
638 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
641 * Unless the user did explicitly request a submodule ignore
642 * mode by passing a command line option we do not ignore any
643 * changed submodule SHA-1s when comparing index and HEAD, no
644 * matter what is configured. Otherwise the user won't be
645 * shown any submodules manually added (and which are
646 * staged to be committed), which would be really confusing.
648 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
651 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
652 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
653 rev
.diffopt
.format_callback_data
= s
;
654 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
655 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
656 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
659 * The `recursive` option must be enabled to allow the diff to recurse
660 * into subdirectories of sparse directory index entries. If it is not
661 * enabled, a subdirectory containing file(s) with changes is reported
662 * as "modified", rather than the modified files themselves.
664 rev
.diffopt
.flags
.recursive
= 1;
666 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
667 run_diff_index(&rev
, 1);
668 release_revisions(&rev
);
671 static int add_file_to_list(const struct object_id
*oid
,
672 struct strbuf
*base
, const char *path
,
673 unsigned int mode
, void *context
)
675 struct string_list_item
*it
;
676 struct wt_status_change_data
*d
;
677 struct wt_status
*s
= context
;
678 struct strbuf full_name
= STRBUF_INIT
;
681 return READ_TREE_RECURSIVE
;
683 strbuf_add(&full_name
, base
->buf
, base
->len
);
684 strbuf_addstr(&full_name
, path
);
685 it
= string_list_insert(&s
->change
, full_name
.buf
);
692 d
->index_status
= DIFF_STATUS_ADDED
;
693 /* Leave {mode,oid}_head zero for adds. */
694 d
->mode_index
= mode
;
695 oidcpy(&d
->oid_index
, oid
);
697 strbuf_release(&full_name
);
701 static void wt_status_collect_changes_initial(struct wt_status
*s
)
703 struct index_state
*istate
= s
->repo
->index
;
706 for (i
= 0; i
< istate
->cache_nr
; i
++) {
707 struct string_list_item
*it
;
708 struct wt_status_change_data
*d
;
709 const struct cache_entry
*ce
= istate
->cache
[i
];
711 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
713 if (ce_intent_to_add(ce
))
715 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
717 * This is a sparse directory entry, so we want to collect all
718 * of the added files within the tree. This requires recursively
719 * expanding the trees to find the elements that are new in this
720 * tree and marking them with DIFF_STATUS_ADDED.
722 struct strbuf base
= STRBUF_INIT
;
723 struct pathspec ps
= { 0 };
724 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
730 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
731 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
732 add_file_to_list
, s
);
736 it
= string_list_insert(&s
->change
, ce
->name
);
743 d
->index_status
= DIFF_STATUS_UNMERGED
;
744 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
746 * Don't bother setting {mode,oid}_{head,index} since the print
747 * code will output the stage values directly and not use the
748 * values in these fields.
752 d
->index_status
= DIFF_STATUS_ADDED
;
753 /* Leave {mode,oid}_head zero for adds. */
754 d
->mode_index
= ce
->ce_mode
;
755 oidcpy(&d
->oid_index
, &ce
->oid
);
761 static void wt_status_collect_untracked(struct wt_status
*s
)
764 struct dir_struct dir
= DIR_INIT
;
765 uint64_t t_begin
= getnanotime();
766 struct index_state
*istate
= s
->repo
->index
;
768 if (!s
->show_untracked_files
)
771 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
773 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
774 if (s
->show_ignored_mode
) {
775 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
777 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
778 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
780 dir
.untracked
= istate
->untracked
;
783 setup_standard_excludes(&dir
);
785 fill_directory(&dir
, istate
, &s
->pathspec
);
787 for (i
= 0; i
< dir
.nr
; i
++) {
788 struct dir_entry
*ent
= dir
.entries
[i
];
789 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
790 string_list_insert(&s
->untracked
, ent
->name
);
793 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
794 struct dir_entry
*ent
= dir
.ignored
[i
];
795 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
796 string_list_insert(&s
->ignored
, ent
->name
);
801 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
802 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
805 static int has_unmerged(struct wt_status
*s
)
809 for (i
= 0; i
< s
->change
.nr
; i
++) {
810 struct wt_status_change_data
*d
;
811 d
= s
->change
.items
[i
].util
;
818 void wt_status_collect(struct wt_status
*s
)
820 trace2_region_enter("status", "worktrees", s
->repo
);
821 wt_status_collect_changes_worktree(s
);
822 trace2_region_leave("status", "worktrees", s
->repo
);
825 trace2_region_enter("status", "initial", s
->repo
);
826 wt_status_collect_changes_initial(s
);
827 trace2_region_leave("status", "initial", s
->repo
);
829 trace2_region_enter("status", "index", s
->repo
);
830 wt_status_collect_changes_index(s
);
831 trace2_region_leave("status", "index", s
->repo
);
834 trace2_region_enter("status", "untracked", s
->repo
);
835 wt_status_collect_untracked(s
);
836 trace2_region_leave("status", "untracked", s
->repo
);
838 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
839 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
843 void wt_status_collect_free_buffers(struct wt_status
*s
)
845 wt_status_state_free_buffers(&s
->state
);
848 void wt_status_state_free_buffers(struct wt_status_state
*state
)
850 FREE_AND_NULL(state
->branch
);
851 FREE_AND_NULL(state
->onto
);
852 FREE_AND_NULL(state
->detached_from
);
855 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
857 int shown_header
= 0;
860 for (i
= 0; i
< s
->change
.nr
; i
++) {
861 struct wt_status_change_data
*d
;
862 struct string_list_item
*it
;
863 it
= &(s
->change
.items
[i
]);
868 wt_longstatus_print_unmerged_header(s
);
871 wt_longstatus_print_unmerged_data(s
, it
);
874 wt_longstatus_print_trailer(s
);
878 static void wt_longstatus_print_updated(struct wt_status
*s
)
880 int shown_header
= 0;
883 for (i
= 0; i
< s
->change
.nr
; i
++) {
884 struct wt_status_change_data
*d
;
885 struct string_list_item
*it
;
886 it
= &(s
->change
.items
[i
]);
888 if (!d
->index_status
||
889 d
->index_status
== DIFF_STATUS_UNMERGED
)
892 wt_longstatus_print_cached_header(s
);
895 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
898 wt_longstatus_print_trailer(s
);
904 * 1 : some change but no delete
906 static int wt_status_check_worktree_changes(struct wt_status
*s
,
907 int *dirty_submodules
)
912 *dirty_submodules
= 0;
914 for (i
= 0; i
< s
->change
.nr
; i
++) {
915 struct wt_status_change_data
*d
;
916 d
= s
->change
.items
[i
].util
;
917 if (!d
->worktree_status
||
918 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
922 if (d
->dirty_submodule
)
923 *dirty_submodules
= 1;
924 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
930 static void wt_longstatus_print_changed(struct wt_status
*s
)
932 int i
, dirty_submodules
;
933 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
935 if (!worktree_changes
)
938 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
940 for (i
= 0; i
< s
->change
.nr
; i
++) {
941 struct wt_status_change_data
*d
;
942 struct string_list_item
*it
;
943 it
= &(s
->change
.items
[i
]);
945 if (!d
->worktree_status
||
946 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
948 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
950 wt_longstatus_print_trailer(s
);
953 static int stash_count_refs(struct object_id
*ooid UNUSED
,
954 struct object_id
*noid UNUSED
,
955 const char *email UNUSED
,
956 timestamp_t timestamp UNUSED
, int tz UNUSED
,
957 const char *message UNUSED
, void *cb_data
)
964 static int count_stash_entries(void)
967 for_each_reflog_ent("refs/stash", stash_count_refs
, &n
);
971 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
973 int stash_count
= count_stash_entries();
976 status_printf_ln(s
, GIT_COLOR_NORMAL
,
977 Q_("Your stash currently has %d entry",
978 "Your stash currently has %d entries", stash_count
),
982 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
984 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
985 struct strbuf cmd_stdout
= STRBUF_INIT
;
986 struct strbuf summary
= STRBUF_INIT
;
987 char *summary_content
;
989 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
991 strvec_push(&sm_summary
.args
, "submodule");
992 strvec_push(&sm_summary
.args
, "summary");
993 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
994 strvec_push(&sm_summary
.args
, "--for-status");
995 strvec_push(&sm_summary
.args
, "--summary-limit");
996 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
998 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1000 sm_summary
.git_cmd
= 1;
1001 sm_summary
.no_stdin
= 1;
1003 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1005 /* prepend header, only if there's an actual output */
1006 if (cmd_stdout
.len
) {
1008 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1010 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1011 strbuf_addstr(&summary
, "\n\n");
1013 strbuf_addbuf(&summary
, &cmd_stdout
);
1014 strbuf_release(&cmd_stdout
);
1016 if (s
->display_comment_prefix
) {
1018 summary_content
= strbuf_detach(&summary
, &len
);
1019 strbuf_add_commented_lines(&summary
, summary_content
, len
);
1020 free(summary_content
);
1023 fputs(summary
.buf
, s
->fp
);
1024 strbuf_release(&summary
);
1027 static void wt_longstatus_print_other(struct wt_status
*s
,
1028 struct string_list
*l
,
1033 struct strbuf buf
= STRBUF_INIT
;
1034 static struct string_list output
= STRING_LIST_INIT_DUP
;
1035 struct column_options copts
;
1040 wt_longstatus_print_other_header(s
, what
, how
);
1042 for (i
= 0; i
< l
->nr
; i
++) {
1043 struct string_list_item
*it
;
1045 it
= &(l
->items
[i
]);
1046 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1047 if (column_active(s
->colopts
)) {
1048 string_list_append(&output
, path
);
1051 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1052 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1056 strbuf_release(&buf
);
1057 if (!column_active(s
->colopts
))
1060 strbuf_addf(&buf
, "%s%s\t%s",
1061 color(WT_STATUS_HEADER
, s
),
1062 s
->display_comment_prefix
? "#" : "",
1063 color(WT_STATUS_UNTRACKED
, s
));
1064 memset(&copts
, 0, sizeof(copts
));
1066 copts
.indent
= buf
.buf
;
1067 if (want_color(s
->use_color
))
1068 copts
.nl
= GIT_COLOR_RESET
"\n";
1069 print_columns(&output
, s
->colopts
, &copts
);
1070 string_list_clear(&output
, 0);
1071 strbuf_release(&buf
);
1073 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1076 size_t wt_status_locate_end(const char *s
, size_t len
)
1079 struct strbuf pattern
= STRBUF_INIT
;
1081 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1082 if (starts_with(s
, pattern
.buf
+ 1))
1084 else if ((p
= strstr(s
, pattern
.buf
)))
1086 strbuf_release(&pattern
);
1090 void wt_status_append_cut_line(struct strbuf
*buf
)
1092 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1094 strbuf_commented_addf(buf
, "%s", cut_line
);
1095 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1098 void wt_status_add_cut_line(FILE *fp
)
1100 struct strbuf buf
= STRBUF_INIT
;
1102 wt_status_append_cut_line(&buf
);
1104 strbuf_release(&buf
);
1107 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1109 struct rev_info rev
;
1110 struct setup_revision_opt opt
;
1111 int dirty_submodules
;
1112 const char *c
= color(WT_STATUS_HEADER
, s
);
1114 repo_init_revisions(s
->repo
, &rev
, NULL
);
1115 rev
.diffopt
.flags
.allow_textconv
= 1;
1116 rev
.diffopt
.ita_invisible_in_index
= 1;
1118 memset(&opt
, 0, sizeof(opt
));
1119 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1120 setup_revisions(0, NULL
, &rev
, &opt
);
1122 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1123 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1124 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1125 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1126 rev
.diffopt
.file
= s
->fp
;
1127 rev
.diffopt
.close_file
= 0;
1129 * If we're not going to stdout, then we definitely don't
1130 * want color, since we are going to the commit message
1131 * file (and even the "auto" setting won't work, since it
1132 * will have checked isatty on stdout). But we then do want
1133 * to insert the scissor line here to reliably remove the
1134 * diff before committing.
1136 if (s
->fp
!= stdout
) {
1137 rev
.diffopt
.use_color
= 0;
1138 wt_status_add_cut_line(s
->fp
);
1140 if (s
->verbose
> 1 && s
->committable
) {
1141 /* print_updated() printed a header, so do we */
1142 if (s
->fp
!= stdout
)
1143 wt_longstatus_print_trailer(s
);
1144 status_printf_ln(s
, c
, _("Changes to be committed:"));
1145 rev
.diffopt
.a_prefix
= "c/";
1146 rev
.diffopt
.b_prefix
= "i/";
1147 } /* else use prefix as per user config */
1148 run_diff_index(&rev
, 1);
1149 if (s
->verbose
> 1 &&
1150 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1151 status_printf_ln(s
, c
,
1152 "--------------------------------------------------");
1153 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1155 rev
.diffopt
.a_prefix
= "i/";
1156 rev
.diffopt
.b_prefix
= "w/";
1157 run_diff_files(&rev
, 0);
1159 release_revisions(&rev
);
1162 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1164 struct strbuf sb
= STRBUF_INIT
;
1165 const char *cp
, *ep
, *branch_name
;
1166 struct branch
*branch
;
1167 char comment_line_string
[3];
1169 uint64_t t_begin
= 0;
1171 assert(s
->branch
&& !s
->is_initial
);
1172 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1174 branch
= branch_get(branch_name
);
1176 t_begin
= getnanotime();
1178 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1181 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1182 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1183 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1184 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1185 strbuf_addf(&sb
, _("\n"
1186 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1187 "You can use '--no-ahead-behind' to avoid this.\n"),
1188 t_delta_in_ms
/ 1000.0);
1193 if (s
->display_comment_prefix
) {
1194 comment_line_string
[i
++] = comment_line_char
;
1195 comment_line_string
[i
++] = ' ';
1197 comment_line_string
[i
] = '\0';
1199 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1200 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1201 "%s%.*s", comment_line_string
,
1202 (int)(ep
- cp
), cp
);
1203 if (s
->display_comment_prefix
)
1204 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1208 strbuf_release(&sb
);
1211 static int uf_was_slow(struct wt_status
*s
)
1213 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1214 s
->untracked_in_ms
= 3250;
1215 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1218 static void show_merge_in_progress(struct wt_status
*s
,
1221 if (has_unmerged(s
)) {
1222 status_printf_ln(s
, color
, _("You have unmerged paths."));
1224 status_printf_ln(s
, color
,
1225 _(" (fix conflicts and run \"git commit\")"));
1226 status_printf_ln(s
, color
,
1227 _(" (use \"git merge --abort\" to abort the merge)"));
1230 status_printf_ln(s
, color
,
1231 _("All conflicts fixed but you are still merging."));
1233 status_printf_ln(s
, color
,
1234 _(" (use \"git commit\" to conclude merge)"));
1236 wt_longstatus_print_trailer(s
);
1239 static void show_am_in_progress(struct wt_status
*s
,
1244 status_printf_ln(s
, color
,
1245 _("You are in the middle of an am session."));
1246 if (s
->state
.am_empty_patch
)
1247 status_printf_ln(s
, color
,
1248 _("The current patch is empty."));
1250 am_empty_patch
= s
->state
.am_empty_patch
;
1251 if (!am_empty_patch
)
1252 status_printf_ln(s
, color
,
1253 _(" (fix conflicts and then run \"git am --continue\")"));
1254 status_printf_ln(s
, color
,
1255 _(" (use \"git am --skip\" to skip this patch)"));
1257 status_printf_ln(s
, color
,
1258 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1259 status_printf_ln(s
, color
,
1260 _(" (use \"git am --abort\" to restore the original branch)"));
1262 wt_longstatus_print_trailer(s
);
1265 static char *read_line_from_git_path(const char *filename
)
1267 struct strbuf buf
= STRBUF_INIT
;
1268 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1271 strbuf_release(&buf
);
1274 strbuf_getline_lf(&buf
, fp
);
1276 return strbuf_detach(&buf
, NULL
);
1278 strbuf_release(&buf
);
1283 static int split_commit_in_progress(struct wt_status
*s
)
1285 int split_in_progress
= 0;
1286 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1288 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1289 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1292 head
= read_line_from_git_path("HEAD");
1293 orig_head
= read_line_from_git_path("ORIG_HEAD");
1294 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1295 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1297 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1298 ; /* fall through, no split in progress */
1299 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1300 split_in_progress
= !!strcmp(head
, rebase_amend
);
1301 else if (strcmp(orig_head
, rebase_orig_head
))
1302 split_in_progress
= 1;
1307 free(rebase_orig_head
);
1309 return split_in_progress
;
1314 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1316 * "pick d6a2f03 some message"
1318 * The function assumes that the line does not contain useless spaces
1319 * before or after the command.
1321 static void abbrev_oid_in_line(struct strbuf
*line
)
1323 struct strbuf
**split
;
1326 if (starts_with(line
->buf
, "exec ") ||
1327 starts_with(line
->buf
, "x ") ||
1328 starts_with(line
->buf
, "label ") ||
1329 starts_with(line
->buf
, "l "))
1332 split
= strbuf_split_max(line
, ' ', 3);
1333 if (split
[0] && split
[1]) {
1334 struct object_id oid
;
1337 * strbuf_split_max left a space. Trim it and re-add
1338 * it after abbreviation.
1340 strbuf_trim(split
[1]);
1341 if (!get_oid(split
[1]->buf
, &oid
)) {
1342 strbuf_reset(split
[1]);
1343 strbuf_add_unique_abbrev(split
[1], &oid
,
1345 strbuf_addch(split
[1], ' ');
1347 for (i
= 0; split
[i
]; i
++)
1348 strbuf_addbuf(line
, split
[i
]);
1351 strbuf_list_free(split
);
1354 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1356 struct strbuf line
= STRBUF_INIT
;
1357 FILE *f
= fopen(git_path("%s", fname
), "r");
1360 if (errno
== ENOENT
)
1362 die_errno("Could not open file %s for reading",
1363 git_path("%s", fname
));
1365 while (!strbuf_getline_lf(&line
, f
)) {
1366 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1371 abbrev_oid_in_line(&line
);
1372 string_list_append(lines
, line
.buf
);
1375 strbuf_release(&line
);
1379 static void show_rebase_information(struct wt_status
*s
,
1382 if (s
->state
.rebase_interactive_in_progress
) {
1384 int nr_lines_to_show
= 2;
1386 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1387 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1389 read_rebase_todolist("rebase-merge/done", &have_done
);
1390 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1392 status_printf_ln(s
, color
,
1393 _("git-rebase-todo is missing."));
1394 if (have_done
.nr
== 0)
1395 status_printf_ln(s
, color
, _("No commands done."));
1397 status_printf_ln(s
, color
,
1398 Q_("Last command done (%"PRIuMAX
" command done):",
1399 "Last commands done (%"PRIuMAX
" commands done):",
1401 (uintmax_t)have_done
.nr
);
1402 for (i
= (have_done
.nr
> nr_lines_to_show
)
1403 ? have_done
.nr
- nr_lines_to_show
: 0;
1406 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1407 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1408 status_printf_ln(s
, color
,
1409 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1412 if (yet_to_do
.nr
== 0)
1413 status_printf_ln(s
, color
,
1414 _("No commands remaining."));
1416 status_printf_ln(s
, color
,
1417 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1418 "Next commands to do (%"PRIuMAX
" remaining commands):",
1420 (uintmax_t)yet_to_do
.nr
);
1421 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1422 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1424 status_printf_ln(s
, color
,
1425 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1427 string_list_clear(&yet_to_do
, 0);
1428 string_list_clear(&have_done
, 0);
1432 static void print_rebase_state(struct wt_status
*s
,
1435 if (s
->state
.branch
)
1436 status_printf_ln(s
, color
,
1437 _("You are currently rebasing branch '%s' on '%s'."),
1441 status_printf_ln(s
, color
,
1442 _("You are currently rebasing."));
1445 static void show_rebase_in_progress(struct wt_status
*s
,
1450 show_rebase_information(s
, color
);
1451 if (has_unmerged(s
)) {
1452 print_rebase_state(s
, color
);
1454 status_printf_ln(s
, color
,
1455 _(" (fix conflicts and then run \"git rebase --continue\")"));
1456 status_printf_ln(s
, color
,
1457 _(" (use \"git rebase --skip\" to skip this patch)"));
1458 status_printf_ln(s
, color
,
1459 _(" (use \"git rebase --abort\" to check out the original branch)"));
1461 } else if (s
->state
.rebase_in_progress
||
1462 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1463 print_rebase_state(s
, color
);
1465 status_printf_ln(s
, color
,
1466 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1467 } else if (split_commit_in_progress(s
)) {
1468 if (s
->state
.branch
)
1469 status_printf_ln(s
, color
,
1470 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1474 status_printf_ln(s
, color
,
1475 _("You are currently splitting a commit during a rebase."));
1477 status_printf_ln(s
, color
,
1478 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1480 if (s
->state
.branch
)
1481 status_printf_ln(s
, color
,
1482 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1486 status_printf_ln(s
, color
,
1487 _("You are currently editing a commit during a rebase."));
1488 if (s
->hints
&& !s
->amend
) {
1489 status_printf_ln(s
, color
,
1490 _(" (use \"git commit --amend\" to amend the current commit)"));
1491 status_printf_ln(s
, color
,
1492 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1495 wt_longstatus_print_trailer(s
);
1498 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1501 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1502 status_printf_ln(s
, color
,
1503 _("Cherry-pick currently in progress."));
1505 status_printf_ln(s
, color
,
1506 _("You are currently cherry-picking commit %s."),
1507 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
,
1511 if (has_unmerged(s
))
1512 status_printf_ln(s
, color
,
1513 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1514 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1515 status_printf_ln(s
, color
,
1516 _(" (run \"git cherry-pick --continue\" to continue)"));
1518 status_printf_ln(s
, color
,
1519 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1520 status_printf_ln(s
, color
,
1521 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1522 status_printf_ln(s
, color
,
1523 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1525 wt_longstatus_print_trailer(s
);
1528 static void show_revert_in_progress(struct wt_status
*s
,
1531 if (is_null_oid(&s
->state
.revert_head_oid
))
1532 status_printf_ln(s
, color
,
1533 _("Revert currently in progress."));
1535 status_printf_ln(s
, color
,
1536 _("You are currently reverting commit %s."),
1537 find_unique_abbrev(&s
->state
.revert_head_oid
,
1540 if (has_unmerged(s
))
1541 status_printf_ln(s
, color
,
1542 _(" (fix conflicts and run \"git revert --continue\")"));
1543 else if (is_null_oid(&s
->state
.revert_head_oid
))
1544 status_printf_ln(s
, color
,
1545 _(" (run \"git revert --continue\" to continue)"));
1547 status_printf_ln(s
, color
,
1548 _(" (all conflicts fixed: run \"git revert --continue\")"));
1549 status_printf_ln(s
, color
,
1550 _(" (use \"git revert --skip\" to skip this patch)"));
1551 status_printf_ln(s
, color
,
1552 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1554 wt_longstatus_print_trailer(s
);
1557 static void show_bisect_in_progress(struct wt_status
*s
,
1560 if (s
->state
.branch
)
1561 status_printf_ln(s
, color
,
1562 _("You are currently bisecting, started from branch '%s'."),
1565 status_printf_ln(s
, color
,
1566 _("You are currently bisecting."));
1568 status_printf_ln(s
, color
,
1569 _(" (use \"git bisect reset\" to get back to the original branch)"));
1570 wt_longstatus_print_trailer(s
);
1573 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1576 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1579 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1580 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1582 status_printf_ln(s
, color
,
1583 _("You are in a sparse checkout with %d%% of tracked files present."),
1584 s
->state
.sparse_checkout_percentage
);
1585 wt_longstatus_print_trailer(s
);
1589 * Extract branch information from rebase/bisect
1591 static char *get_branch(const struct worktree
*wt
, const char *path
)
1593 struct strbuf sb
= STRBUF_INIT
;
1594 struct object_id oid
;
1595 const char *branch_name
;
1597 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1600 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1601 strbuf_setlen(&sb
, sb
.len
- 1);
1604 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1605 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1606 else if (starts_with(sb
.buf
, "refs/"))
1608 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1610 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1611 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1615 return strbuf_detach(&sb
, NULL
);
1618 strbuf_release(&sb
);
1622 struct grab_1st_switch_cbdata
{
1624 struct object_id noid
;
1627 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1628 struct object_id
*noid
,
1629 const char *email UNUSED
,
1630 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1631 const char *message
, void *cb_data
)
1633 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1634 const char *target
= NULL
, *end
;
1636 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1638 target
= strstr(message
, " to ");
1641 target
+= strlen(" to ");
1642 strbuf_reset(&cb
->buf
);
1643 oidcpy(&cb
->noid
, noid
);
1644 end
= strchrnul(target
, '\n');
1645 strbuf_add(&cb
->buf
, target
, end
- target
);
1646 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1647 /* HEAD is relative. Resolve it to the right reflog entry. */
1648 strbuf_reset(&cb
->buf
);
1649 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1654 static void wt_status_get_detached_from(struct repository
*r
,
1655 struct wt_status_state
*state
)
1657 struct grab_1st_switch_cbdata cb
;
1658 struct commit
*commit
;
1659 struct object_id oid
;
1662 strbuf_init(&cb
.buf
, 0);
1663 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1664 strbuf_release(&cb
.buf
);
1668 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
, 1) == 1 &&
1669 /* oid is a commit? match without further lookup */
1670 (oideq(&cb
.noid
, &oid
) ||
1671 /* perhaps oid is a tag, try to dereference to a commit */
1672 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1673 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1674 const char *from
= ref
;
1675 if (!skip_prefix(from
, "refs/tags/", &from
))
1676 skip_prefix(from
, "refs/remotes/", &from
);
1677 state
->detached_from
= xstrdup(from
);
1679 state
->detached_from
=
1680 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1681 oidcpy(&state
->detached_oid
, &cb
.noid
);
1682 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1683 oideq(&oid
, &state
->detached_oid
);
1686 strbuf_release(&cb
.buf
);
1689 int wt_status_check_rebase(const struct worktree
*wt
,
1690 struct wt_status_state
*state
)
1694 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1695 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1696 state
->am_in_progress
= 1;
1697 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1698 state
->am_empty_patch
= 1;
1700 state
->rebase_in_progress
= 1;
1701 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1702 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1704 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1705 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1706 state
->rebase_interactive_in_progress
= 1;
1708 state
->rebase_in_progress
= 1;
1709 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1710 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1716 int wt_status_check_bisect(const struct worktree
*wt
,
1717 struct wt_status_state
*state
)
1721 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1722 state
->bisect_in_progress
= 1;
1723 state
->branch
= get_branch(wt
, "BISECT_START");
1729 static void wt_status_check_sparse_checkout(struct repository
*r
,
1730 struct wt_status_state
*state
)
1732 int skip_worktree
= 0;
1735 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1737 * Don't compute percentage of checked out files if we
1738 * aren't in a sparse checkout or would get division by 0.
1740 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1744 if (r
->index
->sparse_index
) {
1745 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1749 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1750 struct cache_entry
*ce
= r
->index
->cache
[i
];
1751 if (ce_skip_worktree(ce
))
1755 state
->sparse_checkout_percentage
=
1756 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1759 void wt_status_get_state(struct repository
*r
,
1760 struct wt_status_state
*state
,
1761 int get_detached_from
)
1764 struct object_id oid
;
1765 enum replay_action action
;
1767 if (!stat(git_path_merge_head(r
), &st
)) {
1768 wt_status_check_rebase(NULL
, state
);
1769 state
->merge_in_progress
= 1;
1770 } else if (wt_status_check_rebase(NULL
, state
)) {
1772 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1773 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1774 state
->cherry_pick_in_progress
= 1;
1775 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1777 wt_status_check_bisect(NULL
, state
);
1778 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1779 !get_oid("REVERT_HEAD", &oid
)) {
1780 state
->revert_in_progress
= 1;
1781 oidcpy(&state
->revert_head_oid
, &oid
);
1783 if (!sequencer_get_last_command(r
, &action
)) {
1784 if (action
== REPLAY_PICK
) {
1785 state
->cherry_pick_in_progress
= 1;
1786 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1788 state
->revert_in_progress
= 1;
1789 oidcpy(&state
->revert_head_oid
, null_oid());
1792 if (get_detached_from
)
1793 wt_status_get_detached_from(r
, state
);
1794 wt_status_check_sparse_checkout(r
, state
);
1797 static void wt_longstatus_print_state(struct wt_status
*s
)
1799 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1800 struct wt_status_state
*state
= &s
->state
;
1802 if (state
->merge_in_progress
) {
1803 if (state
->rebase_interactive_in_progress
) {
1804 show_rebase_information(s
, state_color
);
1807 show_merge_in_progress(s
, state_color
);
1808 } else if (state
->am_in_progress
)
1809 show_am_in_progress(s
, state_color
);
1810 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1811 show_rebase_in_progress(s
, state_color
);
1812 else if (state
->cherry_pick_in_progress
)
1813 show_cherry_pick_in_progress(s
, state_color
);
1814 else if (state
->revert_in_progress
)
1815 show_revert_in_progress(s
, state_color
);
1816 if (state
->bisect_in_progress
)
1817 show_bisect_in_progress(s
, state_color
);
1819 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1820 show_sparse_checkout_in_use(s
, state_color
);
1823 static void wt_longstatus_print(struct wt_status
*s
)
1825 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1826 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1827 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1830 const char *on_what
= _("On branch ");
1831 const char *branch_name
= s
->branch
;
1832 if (!strcmp(branch_name
, "HEAD")) {
1833 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1834 if (s
->state
.rebase_in_progress
||
1835 s
->state
.rebase_interactive_in_progress
) {
1836 if (s
->state
.rebase_interactive_in_progress
)
1837 on_what
= _("interactive rebase in progress; onto ");
1839 on_what
= _("rebase in progress; onto ");
1840 branch_name
= s
->state
.onto
;
1841 } else if (s
->state
.detached_from
) {
1842 branch_name
= s
->state
.detached_from
;
1843 if (s
->state
.detached_at
)
1844 on_what
= _("HEAD detached at ");
1846 on_what
= _("HEAD detached from ");
1849 on_what
= _("Not currently on any branch.");
1852 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1853 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1854 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1855 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1857 wt_longstatus_print_tracking(s
);
1860 wt_longstatus_print_state(s
);
1862 if (s
->is_initial
) {
1863 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1864 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1866 ? _("Initial commit")
1867 : _("No commits yet"));
1868 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1871 wt_longstatus_print_updated(s
);
1872 wt_longstatus_print_unmerged(s
);
1873 wt_longstatus_print_changed(s
);
1874 if (s
->submodule_summary
&&
1875 (!s
->ignore_submodule_arg
||
1876 strcmp(s
->ignore_submodule_arg
, "all"))) {
1877 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1878 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1880 if (s
->show_untracked_files
) {
1881 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1882 if (s
->show_ignored_mode
)
1883 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1884 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1885 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1886 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1887 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1888 _("It took %.2f seconds to enumerate untracked files,\n"
1889 "but the results were cached, and subsequent runs may be faster."),
1890 s
->untracked_in_ms
/ 1000.0);
1892 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1893 _("It took %.2f seconds to enumerate untracked files."),
1894 s
->untracked_in_ms
/ 1000.0);
1896 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1897 _("See 'git help status' for information on how to improve this."));
1898 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1900 } else if (s
->committable
)
1901 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1903 ? _(" (use -u option to show untracked files)") : "");
1906 wt_longstatus_print_verbose(s
);
1907 if (!s
->committable
) {
1909 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1912 else if (s
->workdir_dirty
) {
1914 fprintf(s
->fp
, _("no changes added to commit "
1915 "(use \"git add\" and/or "
1916 "\"git commit -a\")\n"));
1918 fprintf(s
->fp
, _("no changes added to "
1920 } else if (s
->untracked
.nr
) {
1922 fprintf(s
->fp
, _("nothing added to commit but "
1923 "untracked files present (use "
1924 "\"git add\" to track)\n"));
1926 fprintf(s
->fp
, _("nothing added to commit but "
1927 "untracked files present\n"));
1928 } else if (s
->is_initial
) {
1930 fprintf(s
->fp
, _("nothing to commit (create/"
1931 "copy files and use \"git "
1932 "add\" to track)\n"));
1934 fprintf(s
->fp
, _("nothing to commit\n"));
1935 } else if (!s
->show_untracked_files
) {
1937 fprintf(s
->fp
, _("nothing to commit (use -u to "
1938 "show untracked files)\n"));
1940 fprintf(s
->fp
, _("nothing to commit\n"));
1942 fprintf(s
->fp
, _("nothing to commit, working tree "
1946 wt_longstatus_print_stash_summary(s
);
1949 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1950 struct wt_status
*s
)
1952 struct wt_status_change_data
*d
= it
->util
;
1953 const char *how
= "??";
1955 switch (d
->stagemask
) {
1956 case 1: how
= "DD"; break; /* both deleted */
1957 case 2: how
= "AU"; break; /* added by us */
1958 case 3: how
= "UD"; break; /* deleted by them */
1959 case 4: how
= "UA"; break; /* added by them */
1960 case 5: how
= "DU"; break; /* deleted by us */
1961 case 6: how
= "AA"; break; /* both added */
1962 case 7: how
= "UU"; break; /* both modified */
1964 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1965 if (s
->null_termination
) {
1966 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1968 struct strbuf onebuf
= STRBUF_INIT
;
1970 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1971 fprintf(s
->fp
, " %s\n", one
);
1972 strbuf_release(&onebuf
);
1976 static void wt_shortstatus_status(struct string_list_item
*it
,
1977 struct wt_status
*s
)
1979 struct wt_status_change_data
*d
= it
->util
;
1981 if (d
->index_status
)
1982 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1985 if (d
->worktree_status
)
1986 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1990 if (s
->null_termination
) {
1991 fprintf(s
->fp
, "%s%c", it
->string
, 0);
1992 if (d
->rename_source
)
1993 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
1995 struct strbuf onebuf
= STRBUF_INIT
;
1998 if (d
->rename_source
) {
1999 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2000 QUOTE_PATH_QUOTE_SP
);
2001 fprintf(s
->fp
, "%s -> ", one
);
2002 strbuf_release(&onebuf
);
2004 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2005 fprintf(s
->fp
, "%s\n", one
);
2006 strbuf_release(&onebuf
);
2010 static void wt_shortstatus_other(struct string_list_item
*it
,
2011 struct wt_status
*s
, const char *sign
)
2013 if (s
->null_termination
) {
2014 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2016 struct strbuf onebuf
= STRBUF_INIT
;
2018 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2019 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2020 fprintf(s
->fp
, " %s\n", one
);
2021 strbuf_release(&onebuf
);
2025 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2027 struct branch
*branch
;
2028 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2029 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2030 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2034 const char *branch_name
;
2035 int num_ours
, num_theirs
, sti
;
2036 int upstream_is_gone
= 0;
2038 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2042 branch_name
= s
->branch
;
2044 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2047 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2049 if (!strcmp(s
->branch
, "HEAD")) {
2050 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2051 LABEL(N_("HEAD (no branch)")));
2055 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2057 branch
= branch_get(branch_name
);
2059 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2061 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2062 0, s
->ahead_behind_flags
);
2067 upstream_is_gone
= 1;
2070 short_base
= shorten_unambiguous_ref(base
, 0);
2071 color_fprintf(s
->fp
, header_color
, "...");
2072 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2075 if (!upstream_is_gone
&& !sti
)
2078 color_fprintf(s
->fp
, header_color
, " [");
2079 if (upstream_is_gone
) {
2080 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2081 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2082 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2083 } else if (!num_ours
) {
2084 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2085 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2086 } else if (!num_theirs
) {
2087 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2088 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2090 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2091 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2092 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2093 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2096 color_fprintf(s
->fp
, header_color
, "]");
2098 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2101 static void wt_shortstatus_print(struct wt_status
*s
)
2103 struct string_list_item
*it
;
2106 wt_shortstatus_print_tracking(s
);
2108 for_each_string_list_item(it
, &s
->change
) {
2109 struct wt_status_change_data
*d
= it
->util
;
2112 wt_shortstatus_unmerged(it
, s
);
2114 wt_shortstatus_status(it
, s
);
2116 for_each_string_list_item(it
, &s
->untracked
)
2117 wt_shortstatus_other(it
, s
, "??");
2119 for_each_string_list_item(it
, &s
->ignored
)
2120 wt_shortstatus_other(it
, s
, "!!");
2123 static void wt_porcelain_print(struct wt_status
*s
)
2126 s
->relative_paths
= 0;
2129 wt_shortstatus_print(s
);
2133 * Print branch information for porcelain v2 output. These lines
2134 * are printed when the '--branch' parameter is given.
2136 * # branch.oid <commit><eol>
2137 * # branch.head <head><eol>
2138 * [# branch.upstream <upstream><eol>
2139 * [# branch.ab +<ahead> -<behind><eol>]]
2141 * <commit> ::= the current commit hash or the literal
2142 * "(initial)" to indicate an initialized repo
2145 * <head> ::= <branch_name> the current branch name or
2146 * "(detached)" literal when detached head or
2147 * "(unknown)" when something is wrong.
2149 * <upstream> ::= the upstream branch name, when set.
2151 * <ahead> ::= integer ahead value or '?'.
2153 * <behind> ::= integer behind value or '?'.
2155 * The end-of-line is defined by the -z flag.
2157 * <eol> ::= NUL when -z,
2160 * When an upstream is set and present, the 'branch.ab' line will
2161 * be printed with the ahead/behind counts for the branch and the
2162 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2163 * are different, '?' will be substituted for the actual count.
2165 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2167 struct branch
*branch
;
2169 const char *branch_name
;
2170 int ab_info
, nr_ahead
, nr_behind
;
2171 char eol
= s
->null_termination
? '\0' : '\n';
2173 fprintf(s
->fp
, "# branch.oid %s%c",
2174 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2178 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2180 if (!strcmp(s
->branch
, "HEAD")) {
2181 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2183 if (s
->state
.rebase_in_progress
||
2184 s
->state
.rebase_interactive_in_progress
)
2185 branch_name
= s
->state
.onto
;
2186 else if (s
->state
.detached_from
)
2187 branch_name
= s
->state
.detached_from
;
2192 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2194 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2197 /* Lookup stats on the upstream tracking branch, if set. */
2198 branch
= branch_get(branch_name
);
2200 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2201 &base
, 0, s
->ahead_behind_flags
);
2203 base
= shorten_unambiguous_ref(base
, 0);
2204 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2209 if (nr_ahead
|| nr_behind
)
2210 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2211 nr_ahead
, nr_behind
, eol
);
2213 fprintf(s
->fp
, "# branch.ab +? -?%c",
2215 } else if (!ab_info
) {
2217 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2224 * Print the stash count in a porcelain-friendly format
2226 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2228 int stash_count
= count_stash_entries();
2229 char eol
= s
->null_termination
? '\0' : '\n';
2231 if (stash_count
> 0)
2232 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2236 * Convert various submodule status values into a
2237 * fixed-length string of characters in the buffer provided.
2239 static void wt_porcelain_v2_submodule_state(
2240 struct wt_status_change_data
*d
,
2243 if (S_ISGITLINK(d
->mode_head
) ||
2244 S_ISGITLINK(d
->mode_index
) ||
2245 S_ISGITLINK(d
->mode_worktree
)) {
2247 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2248 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2249 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2260 * Fix-up changed entries before we print them.
2262 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2264 struct wt_status_change_data
*d
= it
->util
;
2266 if (!d
->index_status
) {
2268 * This entry is unchanged in the index (relative to the head).
2269 * Therefore, the collect_updated_cb was never called for this
2270 * entry (during the head-vs-index scan) and so the head column
2271 * fields were never set.
2273 * We must have data for the index column (from the
2274 * index-vs-worktree scan (otherwise, this entry should not be
2275 * in the list of changes)).
2277 * Copy index column fields to the head column, so that our
2278 * output looks complete.
2280 assert(d
->mode_head
== 0);
2281 d
->mode_head
= d
->mode_index
;
2282 oidcpy(&d
->oid_head
, &d
->oid_index
);
2285 if (!d
->worktree_status
) {
2287 * This entry is unchanged in the worktree (relative to the index).
2288 * Therefore, the collect_changed_cb was never called for this entry
2289 * (during the index-vs-worktree scan) and so the worktree column
2290 * fields were never set.
2292 * We must have data for the index column (from the head-vs-index
2295 * Copy the index column fields to the worktree column so that
2296 * our output looks complete.
2298 * Note that we only have a mode field in the worktree column
2299 * because the scan code tries really hard to not have to compute it.
2301 assert(d
->mode_worktree
== 0);
2302 d
->mode_worktree
= d
->mode_index
;
2307 * Print porcelain v2 info for tracked entries with changes.
2309 static void wt_porcelain_v2_print_changed_entry(
2310 struct string_list_item
*it
,
2311 struct wt_status
*s
)
2313 struct wt_status_change_data
*d
= it
->util
;
2314 struct strbuf buf
= STRBUF_INIT
;
2315 struct strbuf buf_from
= STRBUF_INIT
;
2316 const char *path
= NULL
;
2317 const char *path_from
= NULL
;
2319 char submodule_token
[5];
2320 char sep_char
, eol_char
;
2322 wt_porcelain_v2_fix_up_changed(it
);
2323 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2325 key
[0] = d
->index_status
? d
->index_status
: '.';
2326 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2329 if (s
->null_termination
) {
2331 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2332 * A single NUL character separates them.
2337 path_from
= d
->rename_source
;
2340 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2341 * The source path is only present when necessary.
2342 * A single TAB separates them (because paths can contain spaces
2343 * which are not escaped and C-quoting does escape TAB characters).
2347 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2348 if (d
->rename_source
)
2349 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2353 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2354 key
, submodule_token
,
2355 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2356 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2357 d
->rename_status
, d
->rename_score
,
2358 path
, sep_char
, path_from
, eol_char
);
2360 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2361 key
, submodule_token
,
2362 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2363 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2366 strbuf_release(&buf
);
2367 strbuf_release(&buf_from
);
2371 * Print porcelain v2 status info for unmerged entries.
2373 static void wt_porcelain_v2_print_unmerged_entry(
2374 struct string_list_item
*it
,
2375 struct wt_status
*s
)
2377 struct wt_status_change_data
*d
= it
->util
;
2378 struct index_state
*istate
= s
->repo
->index
;
2379 const struct cache_entry
*ce
;
2380 struct strbuf buf_index
= STRBUF_INIT
;
2381 const char *path_index
= NULL
;
2382 int pos
, stage
, sum
;
2385 struct object_id oid
;
2388 char submodule_token
[5];
2389 char unmerged_prefix
= 'u';
2390 char eol_char
= s
->null_termination
? '\0' : '\n';
2392 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2394 switch (d
->stagemask
) {
2395 case 1: key
= "DD"; break; /* both deleted */
2396 case 2: key
= "AU"; break; /* added by us */
2397 case 3: key
= "UD"; break; /* deleted by them */
2398 case 4: key
= "UA"; break; /* added by them */
2399 case 5: key
= "DU"; break; /* deleted by us */
2400 case 6: key
= "AA"; break; /* both added */
2401 case 7: key
= "UU"; break; /* both modified */
2403 BUG("unhandled unmerged status %x", d
->stagemask
);
2407 * Disregard d.aux.porcelain_v2 data that we accumulated
2408 * for the head and index columns during the scans and
2409 * replace with the actual stage data.
2411 * Note that this is a last-one-wins for each the individual
2412 * stage [123] columns in the event of multiple cache entries
2415 memset(stages
, 0, sizeof(stages
));
2417 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2420 while (pos
< istate
->cache_nr
) {
2421 ce
= istate
->cache
[pos
++];
2422 stage
= ce_stage(ce
);
2423 if (strcmp(ce
->name
, it
->string
) || !stage
)
2425 stages
[stage
- 1].mode
= ce
->ce_mode
;
2426 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2427 sum
|= (1 << (stage
- 1));
2429 if (sum
!= d
->stagemask
)
2430 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2432 if (s
->null_termination
)
2433 path_index
= it
->string
;
2435 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2437 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2438 unmerged_prefix
, key
, submodule_token
,
2439 stages
[0].mode
, /* stage 1 */
2440 stages
[1].mode
, /* stage 2 */
2441 stages
[2].mode
, /* stage 3 */
2443 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2444 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2445 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2449 strbuf_release(&buf_index
);
2453 * Print porcelain V2 status info for untracked and ignored entries.
2455 static void wt_porcelain_v2_print_other(
2456 struct string_list_item
*it
,
2457 struct wt_status
*s
,
2460 struct strbuf buf
= STRBUF_INIT
;
2464 if (s
->null_termination
) {
2468 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2472 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2474 strbuf_release(&buf
);
2478 * Print porcelain V2 status.
2481 * [<v2_changed_items>]*
2482 * [<v2_unmerged_items>]*
2483 * [<v2_untracked_items>]*
2484 * [<v2_ignored_items>]*
2487 static void wt_porcelain_v2_print(struct wt_status
*s
)
2489 struct wt_status_change_data
*d
;
2490 struct string_list_item
*it
;
2494 wt_porcelain_v2_print_tracking(s
);
2497 wt_porcelain_v2_print_stash(s
);
2499 for (i
= 0; i
< s
->change
.nr
; i
++) {
2500 it
= &(s
->change
.items
[i
]);
2503 wt_porcelain_v2_print_changed_entry(it
, s
);
2506 for (i
= 0; i
< s
->change
.nr
; i
++) {
2507 it
= &(s
->change
.items
[i
]);
2510 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2513 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2514 it
= &(s
->untracked
.items
[i
]);
2515 wt_porcelain_v2_print_other(it
, s
, '?');
2518 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2519 it
= &(s
->ignored
.items
[i
]);
2520 wt_porcelain_v2_print_other(it
, s
, '!');
2524 void wt_status_print(struct wt_status
*s
)
2526 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2527 trace2_data_intmax("status", s
->repo
, "count/untracked",
2529 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2531 trace2_region_enter("status", "print", s
->repo
);
2533 switch (s
->status_format
) {
2534 case STATUS_FORMAT_SHORT
:
2535 wt_shortstatus_print(s
);
2537 case STATUS_FORMAT_PORCELAIN
:
2538 wt_porcelain_print(s
);
2540 case STATUS_FORMAT_PORCELAIN_V2
:
2541 wt_porcelain_v2_print(s
);
2543 case STATUS_FORMAT_UNSPECIFIED
:
2544 BUG("finalize_deferred_config() should have been called");
2546 case STATUS_FORMAT_NONE
:
2547 case STATUS_FORMAT_LONG
:
2548 wt_longstatus_print(s
);
2552 trace2_region_leave("status", "print", s
->repo
);
2556 * Returns 1 if there are unstaged changes, 0 otherwise.
2558 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2560 struct rev_info rev_info
;
2563 repo_init_revisions(r
, &rev_info
, NULL
);
2564 if (ignore_submodules
) {
2565 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2566 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2568 rev_info
.diffopt
.flags
.quick
= 1;
2569 diff_setup_done(&rev_info
.diffopt
);
2570 result
= run_diff_files(&rev_info
, 0);
2571 result
= diff_result_code(&rev_info
.diffopt
, result
);
2572 release_revisions(&rev_info
);
2577 * Returns 1 if there are uncommitted changes, 0 otherwise.
2579 int has_uncommitted_changes(struct repository
*r
,
2580 int ignore_submodules
)
2582 struct rev_info rev_info
;
2585 if (is_index_unborn(r
->index
))
2588 repo_init_revisions(r
, &rev_info
, NULL
);
2589 if (ignore_submodules
)
2590 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2591 rev_info
.diffopt
.flags
.quick
= 1;
2593 add_head_to_pending(&rev_info
);
2594 if (!rev_info
.pending
.nr
) {
2596 * We have no head (or it's corrupt); use the empty tree,
2597 * which will complain if the index is non-empty.
2599 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2600 add_pending_object(&rev_info
, &tree
->object
, "");
2603 diff_setup_done(&rev_info
.diffopt
);
2604 result
= run_diff_index(&rev_info
, 1);
2605 result
= diff_result_code(&rev_info
.diffopt
, result
);
2606 release_revisions(&rev_info
);
2611 * If the work tree has unstaged or uncommitted changes, dies with the
2612 * appropriate message.
2614 int require_clean_work_tree(struct repository
*r
,
2617 int ignore_submodules
,
2620 struct lock_file lock_file
= LOCK_INIT
;
2623 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2624 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2626 repo_update_index_if_able(r
, &lock_file
);
2627 rollback_lock_file(&lock_file
);
2629 if (has_unstaged_changes(r
, ignore_submodules
)) {
2630 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2631 error(_("cannot %s: You have unstaged changes."), _(action
));
2635 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2637 error(_("additionally, your index contains uncommitted changes."));
2639 error(_("cannot %s: Your index contains uncommitted changes."),