10 #include "run-command.h"
14 #include "submodule.h"
20 #include "sequencer.h"
21 #include "fsmonitor-settings.h"
23 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
24 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
26 static const char cut_line
[] =
27 "------------------------ >8 ------------------------\n";
29 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
30 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
31 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
32 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
33 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
34 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
35 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
36 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
37 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
38 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
41 static const char *color(int slot
, struct wt_status
*s
)
44 if (want_color(s
->use_color
))
45 c
= s
->color_palette
[slot
];
46 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
47 c
= s
->color_palette
[WT_STATUS_HEADER
];
51 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
52 const char *fmt
, va_list ap
, const char *trail
)
54 struct strbuf sb
= STRBUF_INIT
;
55 struct strbuf linebuf
= STRBUF_INIT
;
56 const char *line
, *eol
;
58 strbuf_vaddf(&sb
, fmt
, ap
);
60 if (s
->display_comment_prefix
) {
61 strbuf_addch(&sb
, comment_line_char
);
63 strbuf_addch(&sb
, ' ');
65 color_print_strbuf(s
->fp
, color
, &sb
);
67 fprintf(s
->fp
, "%s", trail
);
71 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
72 eol
= strchr(line
, '\n');
74 strbuf_reset(&linebuf
);
75 if (at_bol
&& s
->display_comment_prefix
) {
76 strbuf_addch(&linebuf
, comment_line_char
);
77 if (*line
!= '\n' && *line
!= '\t')
78 strbuf_addch(&linebuf
, ' ');
81 strbuf_add(&linebuf
, line
, eol
- line
);
83 strbuf_addstr(&linebuf
, line
);
84 color_print_strbuf(s
->fp
, color
, &linebuf
);
92 fprintf(s
->fp
, "%s", trail
);
93 strbuf_release(&linebuf
);
97 void status_printf_ln(struct wt_status
*s
, const char *color
,
103 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
107 void status_printf(struct wt_status
*s
, const char *color
,
108 const char *fmt
, ...)
113 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
117 static void status_printf_more(struct wt_status
*s
, const char *color
,
118 const char *fmt
, ...)
123 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
127 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
129 memset(s
, 0, sizeof(*s
));
131 memcpy(s
->color_palette
, default_wt_status_colors
,
132 sizeof(default_wt_status_colors
));
133 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
135 s
->relative_paths
= 1;
136 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
137 s
->reference
= "HEAD";
139 s
->index_file
= get_index_file();
140 s
->change
.strdup_strings
= 1;
141 s
->untracked
.strdup_strings
= 1;
142 s
->ignored
.strdup_strings
= 1;
143 s
->show_branch
= -1; /* unspecified */
145 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
146 s
->display_comment_prefix
= 0;
147 s
->detect_rename
= -1;
148 s
->rename_score
= -1;
149 s
->rename_limit
= -1;
152 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
155 int del_mod_conflict
= 0;
156 int both_deleted
= 0;
158 const char *c
= color(WT_STATUS_HEADER
, s
);
160 status_printf_ln(s
, c
, _("Unmerged paths:"));
162 for (i
= 0; i
< s
->change
.nr
; i
++) {
163 struct string_list_item
*it
= &(s
->change
.items
[i
]);
164 struct wt_status_change_data
*d
= it
->util
;
166 switch (d
->stagemask
) {
174 del_mod_conflict
= 1;
184 if (s
->whence
!= FROM_COMMIT
)
186 else if (!s
->is_initial
) {
187 if (!strcmp(s
->reference
, "HEAD"))
188 status_printf_ln(s
, c
,
189 _(" (use \"git restore --staged <file>...\" to unstage)"));
191 status_printf_ln(s
, c
,
192 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
195 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
198 if (!del_mod_conflict
)
199 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
201 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
202 } else if (!del_mod_conflict
&& !not_deleted
) {
203 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
205 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
209 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
211 const char *c
= color(WT_STATUS_HEADER
, s
);
213 status_printf_ln(s
, c
, _("Changes to be committed:"));
216 if (s
->whence
!= FROM_COMMIT
)
217 ; /* NEEDSWORK: use "git reset --unresolve"??? */
218 else if (!s
->is_initial
) {
219 if (!strcmp(s
->reference
, "HEAD"))
220 status_printf_ln(s
, c
221 , _(" (use \"git restore --staged <file>...\" to unstage)"));
223 status_printf_ln(s
, c
,
224 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
227 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
230 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
232 int has_dirty_submodules
)
234 const char *c
= color(WT_STATUS_HEADER
, s
);
236 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
240 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
242 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
243 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
244 if (has_dirty_submodules
)
245 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
248 static void wt_longstatus_print_other_header(struct wt_status
*s
,
252 const char *c
= color(WT_STATUS_HEADER
, s
);
253 status_printf_ln(s
, c
, "%s:", what
);
256 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
259 static void wt_longstatus_print_trailer(struct wt_status
*s
)
261 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
264 static const char *wt_status_unmerged_status_string(int stagemask
)
268 return _("both deleted:");
270 return _("added by us:");
272 return _("deleted by them:");
274 return _("added by them:");
276 return _("deleted by us:");
278 return _("both added:");
280 return _("both modified:");
282 BUG("unhandled unmerged status %x", stagemask
);
286 static const char *wt_status_diff_status_string(int status
)
289 case DIFF_STATUS_ADDED
:
290 return _("new file:");
291 case DIFF_STATUS_COPIED
:
293 case DIFF_STATUS_DELETED
:
294 return _("deleted:");
295 case DIFF_STATUS_MODIFIED
:
296 return _("modified:");
297 case DIFF_STATUS_RENAMED
:
298 return _("renamed:");
299 case DIFF_STATUS_TYPE_CHANGED
:
300 return _("typechange:");
301 case DIFF_STATUS_UNKNOWN
:
302 return _("unknown:");
303 case DIFF_STATUS_UNMERGED
:
304 return _("unmerged:");
310 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
314 for (i
= minval
; i
<= maxval
; i
++) {
315 const char *s
= label(i
);
316 int len
= s
? utf8_strwidth(s
) : 0;
323 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
324 struct string_list_item
*it
)
326 const char *c
= color(WT_STATUS_UNMERGED
, s
);
327 struct wt_status_change_data
*d
= it
->util
;
328 struct strbuf onebuf
= STRBUF_INIT
;
329 static char *padding
;
330 static int label_width
;
331 const char *one
, *how
;
335 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
336 label_width
+= strlen(" ");
337 padding
= xmallocz(label_width
);
338 memset(padding
, ' ', label_width
);
341 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
342 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
344 how
= wt_status_unmerged_status_string(d
->stagemask
);
345 len
= label_width
- utf8_strwidth(how
);
346 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
347 strbuf_release(&onebuf
);
350 static void wt_longstatus_print_change_data(struct wt_status
*s
,
352 struct string_list_item
*it
)
354 struct wt_status_change_data
*d
= it
->util
;
355 const char *c
= color(change_type
, s
);
359 const char *one
, *two
;
360 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
361 struct strbuf extra
= STRBUF_INIT
;
362 static char *padding
;
363 static int label_width
;
368 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
369 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
370 label_width
+= strlen(" ");
371 padding
= xmallocz(label_width
);
372 memset(padding
, ' ', label_width
);
375 one_name
= two_name
= it
->string
;
376 switch (change_type
) {
377 case WT_STATUS_UPDATED
:
378 status
= d
->index_status
;
380 case WT_STATUS_CHANGED
:
381 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
382 strbuf_addstr(&extra
, " (");
383 if (d
->new_submodule_commits
)
384 strbuf_addstr(&extra
, _("new commits, "));
385 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
386 strbuf_addstr(&extra
, _("modified content, "));
387 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
388 strbuf_addstr(&extra
, _("untracked content, "));
389 strbuf_setlen(&extra
, extra
.len
- 2);
390 strbuf_addch(&extra
, ')');
392 status
= d
->worktree_status
;
395 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
400 * Only pick up the rename it's relevant. If the rename is for
401 * the changed section and we're printing the updated section,
404 if (d
->rename_status
== status
)
405 one_name
= d
->rename_source
;
407 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
408 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
410 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
411 what
= wt_status_diff_status_string(status
);
413 BUG("unhandled diff status %c", status
);
414 len
= label_width
- utf8_strwidth(what
);
416 if (one_name
!= two_name
)
417 status_printf_more(s
, c
, "%s%.*s%s -> %s",
418 what
, len
, padding
, one
, two
);
420 status_printf_more(s
, c
, "%s%.*s%s",
421 what
, len
, padding
, one
);
423 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
424 strbuf_release(&extra
);
426 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
427 strbuf_release(&onebuf
);
428 strbuf_release(&twobuf
);
431 static char short_submodule_status(struct wt_status_change_data
*d
)
433 if (d
->new_submodule_commits
)
435 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
437 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
439 return d
->worktree_status
;
442 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
443 struct diff_options
*options UNUSED
,
446 struct wt_status
*s
= data
;
451 s
->workdir_dirty
= 1;
452 for (i
= 0; i
< q
->nr
; i
++) {
453 struct diff_filepair
*p
;
454 struct string_list_item
*it
;
455 struct wt_status_change_data
*d
;
458 it
= string_list_insert(&s
->change
, p
->two
->path
);
464 if (!d
->worktree_status
)
465 d
->worktree_status
= p
->status
;
466 if (S_ISGITLINK(p
->two
->mode
)) {
467 d
->dirty_submodule
= p
->two
->dirty_submodule
;
468 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
470 if (s
->status_format
== STATUS_FORMAT_SHORT
)
471 d
->worktree_status
= short_submodule_status(d
);
475 case DIFF_STATUS_ADDED
:
476 d
->mode_worktree
= p
->two
->mode
;
479 case DIFF_STATUS_DELETED
:
480 d
->mode_index
= p
->one
->mode
;
481 oidcpy(&d
->oid_index
, &p
->one
->oid
);
482 /* mode_worktree is zero for a delete. */
485 case DIFF_STATUS_COPIED
:
486 case DIFF_STATUS_RENAMED
:
487 if (d
->rename_status
)
488 BUG("multiple renames on the same target? how?");
489 d
->rename_source
= xstrdup(p
->one
->path
);
490 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
491 d
->rename_status
= p
->status
;
493 case DIFF_STATUS_MODIFIED
:
494 case DIFF_STATUS_TYPE_CHANGED
:
495 case DIFF_STATUS_UNMERGED
:
496 d
->mode_index
= p
->one
->mode
;
497 d
->mode_worktree
= p
->two
->mode
;
498 oidcpy(&d
->oid_index
, &p
->one
->oid
);
502 BUG("unhandled diff-files status '%c'", p
->status
);
509 static int unmerged_mask(struct index_state
*istate
, const char *path
)
512 const struct cache_entry
*ce
;
514 pos
= index_name_pos(istate
, path
, strlen(path
));
520 while (pos
< istate
->cache_nr
) {
521 ce
= istate
->cache
[pos
++];
522 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
524 mask
|= (1 << (ce_stage(ce
) - 1));
529 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
530 struct diff_options
*options UNUSED
,
533 struct wt_status
*s
= data
;
536 for (i
= 0; i
< q
->nr
; i
++) {
537 struct diff_filepair
*p
;
538 struct string_list_item
*it
;
539 struct wt_status_change_data
*d
;
542 it
= string_list_insert(&s
->change
, p
->two
->path
);
548 if (!d
->index_status
)
549 d
->index_status
= p
->status
;
551 case DIFF_STATUS_ADDED
:
552 /* Leave {mode,oid}_head zero for an add. */
553 d
->mode_index
= p
->two
->mode
;
554 oidcpy(&d
->oid_index
, &p
->two
->oid
);
557 case DIFF_STATUS_DELETED
:
558 d
->mode_head
= p
->one
->mode
;
559 oidcpy(&d
->oid_head
, &p
->one
->oid
);
561 /* Leave {mode,oid}_index zero for a delete. */
564 case DIFF_STATUS_COPIED
:
565 case DIFF_STATUS_RENAMED
:
566 if (d
->rename_status
)
567 BUG("multiple renames on the same target? how?");
568 d
->rename_source
= xstrdup(p
->one
->path
);
569 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
570 d
->rename_status
= p
->status
;
572 case DIFF_STATUS_MODIFIED
:
573 case DIFF_STATUS_TYPE_CHANGED
:
574 d
->mode_head
= p
->one
->mode
;
575 d
->mode_index
= p
->two
->mode
;
576 oidcpy(&d
->oid_head
, &p
->one
->oid
);
577 oidcpy(&d
->oid_index
, &p
->two
->oid
);
580 case DIFF_STATUS_UNMERGED
:
581 d
->stagemask
= unmerged_mask(s
->repo
->index
,
584 * Don't bother setting {mode,oid}_{head,index} since the print
585 * code will output the stage values directly and not use the
586 * values in these fields.
591 BUG("unhandled diff-index status '%c'", p
->status
);
597 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
601 repo_init_revisions(s
->repo
, &rev
, NULL
);
602 setup_revisions(0, NULL
, &rev
, NULL
);
603 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
604 rev
.diffopt
.flags
.dirty_submodules
= 1;
605 rev
.diffopt
.ita_invisible_in_index
= 1;
606 if (!s
->show_untracked_files
)
607 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
608 if (s
->ignore_submodule_arg
) {
609 rev
.diffopt
.flags
.override_submodule_config
= 1;
610 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
611 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
612 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
613 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
614 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
615 rev
.diffopt
.format_callback_data
= s
;
616 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
617 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
618 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
619 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
620 run_diff_files(&rev
, 0);
621 release_revisions(&rev
);
624 static void wt_status_collect_changes_index(struct wt_status
*s
)
627 struct setup_revision_opt opt
;
629 repo_init_revisions(s
->repo
, &rev
, NULL
);
630 memset(&opt
, 0, sizeof(opt
));
631 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
632 setup_revisions(0, NULL
, &rev
, &opt
);
634 rev
.diffopt
.flags
.override_submodule_config
= 1;
635 rev
.diffopt
.ita_invisible_in_index
= 1;
636 if (s
->ignore_submodule_arg
) {
637 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
640 * Unless the user did explicitly request a submodule ignore
641 * mode by passing a command line option we do not ignore any
642 * changed submodule SHA-1s when comparing index and HEAD, no
643 * matter what is configured. Otherwise the user won't be
644 * shown any submodules manually added (and which are
645 * staged to be committed), which would be really confusing.
647 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
650 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
651 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
652 rev
.diffopt
.format_callback_data
= s
;
653 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
654 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
655 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
658 * The `recursive` option must be enabled to allow the diff to recurse
659 * into subdirectories of sparse directory index entries. If it is not
660 * enabled, a subdirectory containing file(s) with changes is reported
661 * as "modified", rather than the modified files themselves.
663 rev
.diffopt
.flags
.recursive
= 1;
665 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
666 run_diff_index(&rev
, 1);
667 release_revisions(&rev
);
670 static int add_file_to_list(const struct object_id
*oid
,
671 struct strbuf
*base
, const char *path
,
672 unsigned int mode
, void *context
)
674 struct string_list_item
*it
;
675 struct wt_status_change_data
*d
;
676 struct wt_status
*s
= context
;
677 struct strbuf full_name
= STRBUF_INIT
;
680 return READ_TREE_RECURSIVE
;
682 strbuf_add(&full_name
, base
->buf
, base
->len
);
683 strbuf_addstr(&full_name
, path
);
684 it
= string_list_insert(&s
->change
, full_name
.buf
);
691 d
->index_status
= DIFF_STATUS_ADDED
;
692 /* Leave {mode,oid}_head zero for adds. */
693 d
->mode_index
= mode
;
694 oidcpy(&d
->oid_index
, oid
);
696 strbuf_release(&full_name
);
700 static void wt_status_collect_changes_initial(struct wt_status
*s
)
702 struct index_state
*istate
= s
->repo
->index
;
705 for (i
= 0; i
< istate
->cache_nr
; i
++) {
706 struct string_list_item
*it
;
707 struct wt_status_change_data
*d
;
708 const struct cache_entry
*ce
= istate
->cache
[i
];
710 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
712 if (ce_intent_to_add(ce
))
714 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
716 * This is a sparse directory entry, so we want to collect all
717 * of the added files within the tree. This requires recursively
718 * expanding the trees to find the elements that are new in this
719 * tree and marking them with DIFF_STATUS_ADDED.
721 struct strbuf base
= STRBUF_INIT
;
722 struct pathspec ps
= { 0 };
723 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
729 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
730 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
731 add_file_to_list
, s
);
735 it
= string_list_insert(&s
->change
, ce
->name
);
742 d
->index_status
= DIFF_STATUS_UNMERGED
;
743 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
745 * Don't bother setting {mode,oid}_{head,index} since the print
746 * code will output the stage values directly and not use the
747 * values in these fields.
751 d
->index_status
= DIFF_STATUS_ADDED
;
752 /* Leave {mode,oid}_head zero for adds. */
753 d
->mode_index
= ce
->ce_mode
;
754 oidcpy(&d
->oid_index
, &ce
->oid
);
760 static void wt_status_collect_untracked(struct wt_status
*s
)
763 struct dir_struct dir
= DIR_INIT
;
764 uint64_t t_begin
= getnanotime();
765 struct index_state
*istate
= s
->repo
->index
;
767 if (!s
->show_untracked_files
)
770 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
772 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
773 if (s
->show_ignored_mode
) {
774 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
776 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
777 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
779 dir
.untracked
= istate
->untracked
;
782 setup_standard_excludes(&dir
);
784 fill_directory(&dir
, istate
, &s
->pathspec
);
786 for (i
= 0; i
< dir
.nr
; i
++) {
787 struct dir_entry
*ent
= dir
.entries
[i
];
788 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
789 string_list_insert(&s
->untracked
, ent
->name
);
792 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
793 struct dir_entry
*ent
= dir
.ignored
[i
];
794 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
795 string_list_insert(&s
->ignored
, ent
->name
);
800 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
801 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
804 static int has_unmerged(struct wt_status
*s
)
808 for (i
= 0; i
< s
->change
.nr
; i
++) {
809 struct wt_status_change_data
*d
;
810 d
= s
->change
.items
[i
].util
;
817 void wt_status_collect(struct wt_status
*s
)
819 trace2_region_enter("status", "worktrees", s
->repo
);
820 wt_status_collect_changes_worktree(s
);
821 trace2_region_leave("status", "worktrees", s
->repo
);
824 trace2_region_enter("status", "initial", s
->repo
);
825 wt_status_collect_changes_initial(s
);
826 trace2_region_leave("status", "initial", s
->repo
);
828 trace2_region_enter("status", "index", s
->repo
);
829 wt_status_collect_changes_index(s
);
830 trace2_region_leave("status", "index", s
->repo
);
833 trace2_region_enter("status", "untracked", s
->repo
);
834 wt_status_collect_untracked(s
);
835 trace2_region_leave("status", "untracked", s
->repo
);
837 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
838 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
842 void wt_status_collect_free_buffers(struct wt_status
*s
)
844 wt_status_state_free_buffers(&s
->state
);
847 void wt_status_state_free_buffers(struct wt_status_state
*state
)
849 FREE_AND_NULL(state
->branch
);
850 FREE_AND_NULL(state
->onto
);
851 FREE_AND_NULL(state
->detached_from
);
854 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
856 int shown_header
= 0;
859 for (i
= 0; i
< s
->change
.nr
; i
++) {
860 struct wt_status_change_data
*d
;
861 struct string_list_item
*it
;
862 it
= &(s
->change
.items
[i
]);
867 wt_longstatus_print_unmerged_header(s
);
870 wt_longstatus_print_unmerged_data(s
, it
);
873 wt_longstatus_print_trailer(s
);
877 static void wt_longstatus_print_updated(struct wt_status
*s
)
879 int shown_header
= 0;
882 for (i
= 0; i
< s
->change
.nr
; i
++) {
883 struct wt_status_change_data
*d
;
884 struct string_list_item
*it
;
885 it
= &(s
->change
.items
[i
]);
887 if (!d
->index_status
||
888 d
->index_status
== DIFF_STATUS_UNMERGED
)
891 wt_longstatus_print_cached_header(s
);
894 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
897 wt_longstatus_print_trailer(s
);
903 * 1 : some change but no delete
905 static int wt_status_check_worktree_changes(struct wt_status
*s
,
906 int *dirty_submodules
)
911 *dirty_submodules
= 0;
913 for (i
= 0; i
< s
->change
.nr
; i
++) {
914 struct wt_status_change_data
*d
;
915 d
= s
->change
.items
[i
].util
;
916 if (!d
->worktree_status
||
917 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
921 if (d
->dirty_submodule
)
922 *dirty_submodules
= 1;
923 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
929 static void wt_longstatus_print_changed(struct wt_status
*s
)
931 int i
, dirty_submodules
;
932 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
934 if (!worktree_changes
)
937 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
939 for (i
= 0; i
< s
->change
.nr
; i
++) {
940 struct wt_status_change_data
*d
;
941 struct string_list_item
*it
;
942 it
= &(s
->change
.items
[i
]);
944 if (!d
->worktree_status
||
945 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
947 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
949 wt_longstatus_print_trailer(s
);
952 static int stash_count_refs(struct object_id
*ooid UNUSED
,
953 struct object_id
*noid UNUSED
,
954 const char *email UNUSED
,
955 timestamp_t timestamp UNUSED
, int tz UNUSED
,
956 const char *message UNUSED
, void *cb_data
)
963 static int count_stash_entries(void)
966 for_each_reflog_ent("refs/stash", stash_count_refs
, &n
);
970 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
972 int stash_count
= count_stash_entries();
975 status_printf_ln(s
, GIT_COLOR_NORMAL
,
976 Q_("Your stash currently has %d entry",
977 "Your stash currently has %d entries", stash_count
),
981 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
983 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
984 struct strbuf cmd_stdout
= STRBUF_INIT
;
985 struct strbuf summary
= STRBUF_INIT
;
986 char *summary_content
;
988 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
990 strvec_push(&sm_summary
.args
, "submodule");
991 strvec_push(&sm_summary
.args
, "summary");
992 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
993 strvec_push(&sm_summary
.args
, "--for-status");
994 strvec_push(&sm_summary
.args
, "--summary-limit");
995 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
997 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
999 sm_summary
.git_cmd
= 1;
1000 sm_summary
.no_stdin
= 1;
1002 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1004 /* prepend header, only if there's an actual output */
1005 if (cmd_stdout
.len
) {
1007 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1009 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1010 strbuf_addstr(&summary
, "\n\n");
1012 strbuf_addbuf(&summary
, &cmd_stdout
);
1013 strbuf_release(&cmd_stdout
);
1015 if (s
->display_comment_prefix
) {
1017 summary_content
= strbuf_detach(&summary
, &len
);
1018 strbuf_add_commented_lines(&summary
, summary_content
, len
);
1019 free(summary_content
);
1022 fputs(summary
.buf
, s
->fp
);
1023 strbuf_release(&summary
);
1026 static void wt_longstatus_print_other(struct wt_status
*s
,
1027 struct string_list
*l
,
1032 struct strbuf buf
= STRBUF_INIT
;
1033 static struct string_list output
= STRING_LIST_INIT_DUP
;
1034 struct column_options copts
;
1039 wt_longstatus_print_other_header(s
, what
, how
);
1041 for (i
= 0; i
< l
->nr
; i
++) {
1042 struct string_list_item
*it
;
1044 it
= &(l
->items
[i
]);
1045 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1046 if (column_active(s
->colopts
)) {
1047 string_list_append(&output
, path
);
1050 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1051 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1055 strbuf_release(&buf
);
1056 if (!column_active(s
->colopts
))
1059 strbuf_addf(&buf
, "%s%s\t%s",
1060 color(WT_STATUS_HEADER
, s
),
1061 s
->display_comment_prefix
? "#" : "",
1062 color(WT_STATUS_UNTRACKED
, s
));
1063 memset(&copts
, 0, sizeof(copts
));
1065 copts
.indent
= buf
.buf
;
1066 if (want_color(s
->use_color
))
1067 copts
.nl
= GIT_COLOR_RESET
"\n";
1068 print_columns(&output
, s
->colopts
, &copts
);
1069 string_list_clear(&output
, 0);
1070 strbuf_release(&buf
);
1072 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1075 size_t wt_status_locate_end(const char *s
, size_t len
)
1078 struct strbuf pattern
= STRBUF_INIT
;
1080 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1081 if (starts_with(s
, pattern
.buf
+ 1))
1083 else if ((p
= strstr(s
, pattern
.buf
)))
1085 strbuf_release(&pattern
);
1089 void wt_status_append_cut_line(struct strbuf
*buf
)
1091 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1093 strbuf_commented_addf(buf
, "%s", cut_line
);
1094 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1097 void wt_status_add_cut_line(FILE *fp
)
1099 struct strbuf buf
= STRBUF_INIT
;
1101 wt_status_append_cut_line(&buf
);
1103 strbuf_release(&buf
);
1106 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1108 struct rev_info rev
;
1109 struct setup_revision_opt opt
;
1110 int dirty_submodules
;
1111 const char *c
= color(WT_STATUS_HEADER
, s
);
1113 repo_init_revisions(s
->repo
, &rev
, NULL
);
1114 rev
.diffopt
.flags
.allow_textconv
= 1;
1115 rev
.diffopt
.ita_invisible_in_index
= 1;
1117 memset(&opt
, 0, sizeof(opt
));
1118 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1119 setup_revisions(0, NULL
, &rev
, &opt
);
1121 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1122 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1123 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1124 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1125 rev
.diffopt
.file
= s
->fp
;
1126 rev
.diffopt
.close_file
= 0;
1128 * If we're not going to stdout, then we definitely don't
1129 * want color, since we are going to the commit message
1130 * file (and even the "auto" setting won't work, since it
1131 * will have checked isatty on stdout). But we then do want
1132 * to insert the scissor line here to reliably remove the
1133 * diff before committing.
1135 if (s
->fp
!= stdout
) {
1136 rev
.diffopt
.use_color
= 0;
1137 wt_status_add_cut_line(s
->fp
);
1139 if (s
->verbose
> 1 && s
->committable
) {
1140 /* print_updated() printed a header, so do we */
1141 if (s
->fp
!= stdout
)
1142 wt_longstatus_print_trailer(s
);
1143 status_printf_ln(s
, c
, _("Changes to be committed:"));
1144 rev
.diffopt
.a_prefix
= "c/";
1145 rev
.diffopt
.b_prefix
= "i/";
1146 } /* else use prefix as per user config */
1147 run_diff_index(&rev
, 1);
1148 if (s
->verbose
> 1 &&
1149 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1150 status_printf_ln(s
, c
,
1151 "--------------------------------------------------");
1152 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1154 rev
.diffopt
.a_prefix
= "i/";
1155 rev
.diffopt
.b_prefix
= "w/";
1156 run_diff_files(&rev
, 0);
1158 release_revisions(&rev
);
1161 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1163 struct strbuf sb
= STRBUF_INIT
;
1164 const char *cp
, *ep
, *branch_name
;
1165 struct branch
*branch
;
1166 char comment_line_string
[3];
1168 uint64_t t_begin
= 0;
1170 assert(s
->branch
&& !s
->is_initial
);
1171 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1173 branch
= branch_get(branch_name
);
1175 t_begin
= getnanotime();
1177 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1180 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1181 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1182 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1183 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1184 strbuf_addf(&sb
, _("\n"
1185 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1186 "You can use '--no-ahead-behind' to avoid this.\n"),
1187 t_delta_in_ms
/ 1000.0);
1192 if (s
->display_comment_prefix
) {
1193 comment_line_string
[i
++] = comment_line_char
;
1194 comment_line_string
[i
++] = ' ';
1196 comment_line_string
[i
] = '\0';
1198 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1199 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1200 "%s%.*s", comment_line_string
,
1201 (int)(ep
- cp
), cp
);
1202 if (s
->display_comment_prefix
)
1203 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1207 strbuf_release(&sb
);
1210 static int uf_was_slow(struct wt_status
*s
)
1212 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1213 s
->untracked_in_ms
= 3250;
1214 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1217 static void show_merge_in_progress(struct wt_status
*s
,
1220 if (has_unmerged(s
)) {
1221 status_printf_ln(s
, color
, _("You have unmerged paths."));
1223 status_printf_ln(s
, color
,
1224 _(" (fix conflicts and run \"git commit\")"));
1225 status_printf_ln(s
, color
,
1226 _(" (use \"git merge --abort\" to abort the merge)"));
1229 status_printf_ln(s
, color
,
1230 _("All conflicts fixed but you are still merging."));
1232 status_printf_ln(s
, color
,
1233 _(" (use \"git commit\" to conclude merge)"));
1235 wt_longstatus_print_trailer(s
);
1238 static void show_am_in_progress(struct wt_status
*s
,
1243 status_printf_ln(s
, color
,
1244 _("You are in the middle of an am session."));
1245 if (s
->state
.am_empty_patch
)
1246 status_printf_ln(s
, color
,
1247 _("The current patch is empty."));
1249 am_empty_patch
= s
->state
.am_empty_patch
;
1250 if (!am_empty_patch
)
1251 status_printf_ln(s
, color
,
1252 _(" (fix conflicts and then run \"git am --continue\")"));
1253 status_printf_ln(s
, color
,
1254 _(" (use \"git am --skip\" to skip this patch)"));
1256 status_printf_ln(s
, color
,
1257 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1258 status_printf_ln(s
, color
,
1259 _(" (use \"git am --abort\" to restore the original branch)"));
1261 wt_longstatus_print_trailer(s
);
1264 static char *read_line_from_git_path(const char *filename
)
1266 struct strbuf buf
= STRBUF_INIT
;
1267 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1270 strbuf_release(&buf
);
1273 strbuf_getline_lf(&buf
, fp
);
1275 return strbuf_detach(&buf
, NULL
);
1277 strbuf_release(&buf
);
1282 static int split_commit_in_progress(struct wt_status
*s
)
1284 int split_in_progress
= 0;
1285 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1287 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1288 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1291 head
= read_line_from_git_path("HEAD");
1292 orig_head
= read_line_from_git_path("ORIG_HEAD");
1293 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1294 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1296 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1297 ; /* fall through, no split in progress */
1298 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1299 split_in_progress
= !!strcmp(head
, rebase_amend
);
1300 else if (strcmp(orig_head
, rebase_orig_head
))
1301 split_in_progress
= 1;
1306 free(rebase_orig_head
);
1308 return split_in_progress
;
1313 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1315 * "pick d6a2f03 some message"
1317 * The function assumes that the line does not contain useless spaces
1318 * before or after the command.
1320 static void abbrev_oid_in_line(struct strbuf
*line
)
1322 struct strbuf
**split
;
1325 if (starts_with(line
->buf
, "exec ") ||
1326 starts_with(line
->buf
, "x ") ||
1327 starts_with(line
->buf
, "label ") ||
1328 starts_with(line
->buf
, "l "))
1331 split
= strbuf_split_max(line
, ' ', 3);
1332 if (split
[0] && split
[1]) {
1333 struct object_id oid
;
1336 * strbuf_split_max left a space. Trim it and re-add
1337 * it after abbreviation.
1339 strbuf_trim(split
[1]);
1340 if (!get_oid(split
[1]->buf
, &oid
)) {
1341 strbuf_reset(split
[1]);
1342 strbuf_add_unique_abbrev(split
[1], &oid
,
1344 strbuf_addch(split
[1], ' ');
1346 for (i
= 0; split
[i
]; i
++)
1347 strbuf_addbuf(line
, split
[i
]);
1350 strbuf_list_free(split
);
1353 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1355 struct strbuf line
= STRBUF_INIT
;
1356 FILE *f
= fopen(git_path("%s", fname
), "r");
1359 if (errno
== ENOENT
)
1361 die_errno("Could not open file %s for reading",
1362 git_path("%s", fname
));
1364 while (!strbuf_getline_lf(&line
, f
)) {
1365 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1370 abbrev_oid_in_line(&line
);
1371 string_list_append(lines
, line
.buf
);
1374 strbuf_release(&line
);
1378 static void show_rebase_information(struct wt_status
*s
,
1381 if (s
->state
.rebase_interactive_in_progress
) {
1383 int nr_lines_to_show
= 2;
1385 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1386 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1388 read_rebase_todolist("rebase-merge/done", &have_done
);
1389 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1391 status_printf_ln(s
, color
,
1392 _("git-rebase-todo is missing."));
1393 if (have_done
.nr
== 0)
1394 status_printf_ln(s
, color
, _("No commands done."));
1396 status_printf_ln(s
, color
,
1397 Q_("Last command done (%"PRIuMAX
" command done):",
1398 "Last commands done (%"PRIuMAX
" commands done):",
1400 (uintmax_t)have_done
.nr
);
1401 for (i
= (have_done
.nr
> nr_lines_to_show
)
1402 ? have_done
.nr
- nr_lines_to_show
: 0;
1405 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1406 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1407 status_printf_ln(s
, color
,
1408 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1411 if (yet_to_do
.nr
== 0)
1412 status_printf_ln(s
, color
,
1413 _("No commands remaining."));
1415 status_printf_ln(s
, color
,
1416 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1417 "Next commands to do (%"PRIuMAX
" remaining commands):",
1419 (uintmax_t)yet_to_do
.nr
);
1420 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1421 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1423 status_printf_ln(s
, color
,
1424 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1426 string_list_clear(&yet_to_do
, 0);
1427 string_list_clear(&have_done
, 0);
1431 static void print_rebase_state(struct wt_status
*s
,
1434 if (s
->state
.branch
)
1435 status_printf_ln(s
, color
,
1436 _("You are currently rebasing branch '%s' on '%s'."),
1440 status_printf_ln(s
, color
,
1441 _("You are currently rebasing."));
1444 static void show_rebase_in_progress(struct wt_status
*s
,
1449 show_rebase_information(s
, color
);
1450 if (has_unmerged(s
)) {
1451 print_rebase_state(s
, color
);
1453 status_printf_ln(s
, color
,
1454 _(" (fix conflicts and then run \"git rebase --continue\")"));
1455 status_printf_ln(s
, color
,
1456 _(" (use \"git rebase --skip\" to skip this patch)"));
1457 status_printf_ln(s
, color
,
1458 _(" (use \"git rebase --abort\" to check out the original branch)"));
1460 } else if (s
->state
.rebase_in_progress
||
1461 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1462 print_rebase_state(s
, color
);
1464 status_printf_ln(s
, color
,
1465 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1466 } else if (split_commit_in_progress(s
)) {
1467 if (s
->state
.branch
)
1468 status_printf_ln(s
, color
,
1469 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1473 status_printf_ln(s
, color
,
1474 _("You are currently splitting a commit during a rebase."));
1476 status_printf_ln(s
, color
,
1477 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1479 if (s
->state
.branch
)
1480 status_printf_ln(s
, color
,
1481 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1485 status_printf_ln(s
, color
,
1486 _("You are currently editing a commit during a rebase."));
1487 if (s
->hints
&& !s
->amend
) {
1488 status_printf_ln(s
, color
,
1489 _(" (use \"git commit --amend\" to amend the current commit)"));
1490 status_printf_ln(s
, color
,
1491 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1494 wt_longstatus_print_trailer(s
);
1497 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1500 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1501 status_printf_ln(s
, color
,
1502 _("Cherry-pick currently in progress."));
1504 status_printf_ln(s
, color
,
1505 _("You are currently cherry-picking commit %s."),
1506 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
,
1510 if (has_unmerged(s
))
1511 status_printf_ln(s
, color
,
1512 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1513 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1514 status_printf_ln(s
, color
,
1515 _(" (run \"git cherry-pick --continue\" to continue)"));
1517 status_printf_ln(s
, color
,
1518 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1519 status_printf_ln(s
, color
,
1520 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1521 status_printf_ln(s
, color
,
1522 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1524 wt_longstatus_print_trailer(s
);
1527 static void show_revert_in_progress(struct wt_status
*s
,
1530 if (is_null_oid(&s
->state
.revert_head_oid
))
1531 status_printf_ln(s
, color
,
1532 _("Revert currently in progress."));
1534 status_printf_ln(s
, color
,
1535 _("You are currently reverting commit %s."),
1536 find_unique_abbrev(&s
->state
.revert_head_oid
,
1539 if (has_unmerged(s
))
1540 status_printf_ln(s
, color
,
1541 _(" (fix conflicts and run \"git revert --continue\")"));
1542 else if (is_null_oid(&s
->state
.revert_head_oid
))
1543 status_printf_ln(s
, color
,
1544 _(" (run \"git revert --continue\" to continue)"));
1546 status_printf_ln(s
, color
,
1547 _(" (all conflicts fixed: run \"git revert --continue\")"));
1548 status_printf_ln(s
, color
,
1549 _(" (use \"git revert --skip\" to skip this patch)"));
1550 status_printf_ln(s
, color
,
1551 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1553 wt_longstatus_print_trailer(s
);
1556 static void show_bisect_in_progress(struct wt_status
*s
,
1559 if (s
->state
.branch
)
1560 status_printf_ln(s
, color
,
1561 _("You are currently bisecting, started from branch '%s'."),
1564 status_printf_ln(s
, color
,
1565 _("You are currently bisecting."));
1567 status_printf_ln(s
, color
,
1568 _(" (use \"git bisect reset\" to get back to the original branch)"));
1569 wt_longstatus_print_trailer(s
);
1572 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1575 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1578 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1579 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1581 status_printf_ln(s
, color
,
1582 _("You are in a sparse checkout with %d%% of tracked files present."),
1583 s
->state
.sparse_checkout_percentage
);
1584 wt_longstatus_print_trailer(s
);
1588 * Extract branch information from rebase/bisect
1590 static char *get_branch(const struct worktree
*wt
, const char *path
)
1592 struct strbuf sb
= STRBUF_INIT
;
1593 struct object_id oid
;
1594 const char *branch_name
;
1596 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1599 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1600 strbuf_setlen(&sb
, sb
.len
- 1);
1603 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1604 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1605 else if (starts_with(sb
.buf
, "refs/"))
1607 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1609 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1610 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1614 return strbuf_detach(&sb
, NULL
);
1617 strbuf_release(&sb
);
1621 struct grab_1st_switch_cbdata
{
1623 struct object_id noid
;
1626 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1627 struct object_id
*noid
,
1628 const char *email UNUSED
,
1629 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1630 const char *message
, void *cb_data
)
1632 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1633 const char *target
= NULL
, *end
;
1635 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1637 target
= strstr(message
, " to ");
1640 target
+= strlen(" to ");
1641 strbuf_reset(&cb
->buf
);
1642 oidcpy(&cb
->noid
, noid
);
1643 end
= strchrnul(target
, '\n');
1644 strbuf_add(&cb
->buf
, target
, end
- target
);
1645 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1646 /* HEAD is relative. Resolve it to the right reflog entry. */
1647 strbuf_reset(&cb
->buf
);
1648 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1653 static void wt_status_get_detached_from(struct repository
*r
,
1654 struct wt_status_state
*state
)
1656 struct grab_1st_switch_cbdata cb
;
1657 struct commit
*commit
;
1658 struct object_id oid
;
1661 strbuf_init(&cb
.buf
, 0);
1662 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1663 strbuf_release(&cb
.buf
);
1667 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
, 1) == 1 &&
1668 /* oid is a commit? match without further lookup */
1669 (oideq(&cb
.noid
, &oid
) ||
1670 /* perhaps oid is a tag, try to dereference to a commit */
1671 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1672 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1673 const char *from
= ref
;
1674 if (!skip_prefix(from
, "refs/tags/", &from
))
1675 skip_prefix(from
, "refs/remotes/", &from
);
1676 state
->detached_from
= xstrdup(from
);
1678 state
->detached_from
=
1679 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1680 oidcpy(&state
->detached_oid
, &cb
.noid
);
1681 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1682 oideq(&oid
, &state
->detached_oid
);
1685 strbuf_release(&cb
.buf
);
1688 int wt_status_check_rebase(const struct worktree
*wt
,
1689 struct wt_status_state
*state
)
1693 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1694 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1695 state
->am_in_progress
= 1;
1696 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1697 state
->am_empty_patch
= 1;
1699 state
->rebase_in_progress
= 1;
1700 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1701 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1703 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1704 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1705 state
->rebase_interactive_in_progress
= 1;
1707 state
->rebase_in_progress
= 1;
1708 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1709 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1715 int wt_status_check_bisect(const struct worktree
*wt
,
1716 struct wt_status_state
*state
)
1720 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1721 state
->bisect_in_progress
= 1;
1722 state
->branch
= get_branch(wt
, "BISECT_START");
1728 static void wt_status_check_sparse_checkout(struct repository
*r
,
1729 struct wt_status_state
*state
)
1731 int skip_worktree
= 0;
1734 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1736 * Don't compute percentage of checked out files if we
1737 * aren't in a sparse checkout or would get division by 0.
1739 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1743 if (r
->index
->sparse_index
) {
1744 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1748 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1749 struct cache_entry
*ce
= r
->index
->cache
[i
];
1750 if (ce_skip_worktree(ce
))
1754 state
->sparse_checkout_percentage
=
1755 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1758 void wt_status_get_state(struct repository
*r
,
1759 struct wt_status_state
*state
,
1760 int get_detached_from
)
1763 struct object_id oid
;
1764 enum replay_action action
;
1766 if (!stat(git_path_merge_head(r
), &st
)) {
1767 wt_status_check_rebase(NULL
, state
);
1768 state
->merge_in_progress
= 1;
1769 } else if (wt_status_check_rebase(NULL
, state
)) {
1771 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1772 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1773 state
->cherry_pick_in_progress
= 1;
1774 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1776 wt_status_check_bisect(NULL
, state
);
1777 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1778 !get_oid("REVERT_HEAD", &oid
)) {
1779 state
->revert_in_progress
= 1;
1780 oidcpy(&state
->revert_head_oid
, &oid
);
1782 if (!sequencer_get_last_command(r
, &action
)) {
1783 if (action
== REPLAY_PICK
) {
1784 state
->cherry_pick_in_progress
= 1;
1785 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1787 state
->revert_in_progress
= 1;
1788 oidcpy(&state
->revert_head_oid
, null_oid());
1791 if (get_detached_from
)
1792 wt_status_get_detached_from(r
, state
);
1793 wt_status_check_sparse_checkout(r
, state
);
1796 static void wt_longstatus_print_state(struct wt_status
*s
)
1798 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1799 struct wt_status_state
*state
= &s
->state
;
1801 if (state
->merge_in_progress
) {
1802 if (state
->rebase_interactive_in_progress
) {
1803 show_rebase_information(s
, state_color
);
1806 show_merge_in_progress(s
, state_color
);
1807 } else if (state
->am_in_progress
)
1808 show_am_in_progress(s
, state_color
);
1809 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1810 show_rebase_in_progress(s
, state_color
);
1811 else if (state
->cherry_pick_in_progress
)
1812 show_cherry_pick_in_progress(s
, state_color
);
1813 else if (state
->revert_in_progress
)
1814 show_revert_in_progress(s
, state_color
);
1815 if (state
->bisect_in_progress
)
1816 show_bisect_in_progress(s
, state_color
);
1818 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1819 show_sparse_checkout_in_use(s
, state_color
);
1822 static void wt_longstatus_print(struct wt_status
*s
)
1824 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1825 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1826 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1829 const char *on_what
= _("On branch ");
1830 const char *branch_name
= s
->branch
;
1831 if (!strcmp(branch_name
, "HEAD")) {
1832 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1833 if (s
->state
.rebase_in_progress
||
1834 s
->state
.rebase_interactive_in_progress
) {
1835 if (s
->state
.rebase_interactive_in_progress
)
1836 on_what
= _("interactive rebase in progress; onto ");
1838 on_what
= _("rebase in progress; onto ");
1839 branch_name
= s
->state
.onto
;
1840 } else if (s
->state
.detached_from
) {
1841 branch_name
= s
->state
.detached_from
;
1842 if (s
->state
.detached_at
)
1843 on_what
= _("HEAD detached at ");
1845 on_what
= _("HEAD detached from ");
1848 on_what
= _("Not currently on any branch.");
1851 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1852 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1853 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1854 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1856 wt_longstatus_print_tracking(s
);
1859 wt_longstatus_print_state(s
);
1861 if (s
->is_initial
) {
1862 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1863 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1865 ? _("Initial commit")
1866 : _("No commits yet"));
1867 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1870 wt_longstatus_print_updated(s
);
1871 wt_longstatus_print_unmerged(s
);
1872 wt_longstatus_print_changed(s
);
1873 if (s
->submodule_summary
&&
1874 (!s
->ignore_submodule_arg
||
1875 strcmp(s
->ignore_submodule_arg
, "all"))) {
1876 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1877 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1879 if (s
->show_untracked_files
) {
1880 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1881 if (s
->show_ignored_mode
)
1882 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1883 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1884 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1885 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1886 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1887 _("It took %.2f seconds to enumerate untracked files,\n"
1888 "but the results were cached, and subsequent runs may be faster."),
1889 s
->untracked_in_ms
/ 1000.0);
1891 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1892 _("It took %.2f seconds to enumerate untracked files."),
1893 s
->untracked_in_ms
/ 1000.0);
1895 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1896 _("See 'git help status' for information on how to improve this."));
1897 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1899 } else if (s
->committable
)
1900 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1902 ? _(" (use -u option to show untracked files)") : "");
1905 wt_longstatus_print_verbose(s
);
1906 if (!s
->committable
) {
1908 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1911 else if (s
->workdir_dirty
) {
1913 fprintf(s
->fp
, _("no changes added to commit "
1914 "(use \"git add\" and/or "
1915 "\"git commit -a\")\n"));
1917 fprintf(s
->fp
, _("no changes added to "
1919 } else if (s
->untracked
.nr
) {
1921 fprintf(s
->fp
, _("nothing added to commit but "
1922 "untracked files present (use "
1923 "\"git add\" to track)\n"));
1925 fprintf(s
->fp
, _("nothing added to commit but "
1926 "untracked files present\n"));
1927 } else if (s
->is_initial
) {
1929 fprintf(s
->fp
, _("nothing to commit (create/"
1930 "copy files and use \"git "
1931 "add\" to track)\n"));
1933 fprintf(s
->fp
, _("nothing to commit\n"));
1934 } else if (!s
->show_untracked_files
) {
1936 fprintf(s
->fp
, _("nothing to commit (use -u to "
1937 "show untracked files)\n"));
1939 fprintf(s
->fp
, _("nothing to commit\n"));
1941 fprintf(s
->fp
, _("nothing to commit, working tree "
1945 wt_longstatus_print_stash_summary(s
);
1948 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1949 struct wt_status
*s
)
1951 struct wt_status_change_data
*d
= it
->util
;
1952 const char *how
= "??";
1954 switch (d
->stagemask
) {
1955 case 1: how
= "DD"; break; /* both deleted */
1956 case 2: how
= "AU"; break; /* added by us */
1957 case 3: how
= "UD"; break; /* deleted by them */
1958 case 4: how
= "UA"; break; /* added by them */
1959 case 5: how
= "DU"; break; /* deleted by us */
1960 case 6: how
= "AA"; break; /* both added */
1961 case 7: how
= "UU"; break; /* both modified */
1963 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1964 if (s
->null_termination
) {
1965 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1967 struct strbuf onebuf
= STRBUF_INIT
;
1969 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1970 fprintf(s
->fp
, " %s\n", one
);
1971 strbuf_release(&onebuf
);
1975 static void wt_shortstatus_status(struct string_list_item
*it
,
1976 struct wt_status
*s
)
1978 struct wt_status_change_data
*d
= it
->util
;
1980 if (d
->index_status
)
1981 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1984 if (d
->worktree_status
)
1985 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1989 if (s
->null_termination
) {
1990 fprintf(s
->fp
, "%s%c", it
->string
, 0);
1991 if (d
->rename_source
)
1992 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
1994 struct strbuf onebuf
= STRBUF_INIT
;
1997 if (d
->rename_source
) {
1998 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
1999 QUOTE_PATH_QUOTE_SP
);
2000 fprintf(s
->fp
, "%s -> ", one
);
2001 strbuf_release(&onebuf
);
2003 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2004 fprintf(s
->fp
, "%s\n", one
);
2005 strbuf_release(&onebuf
);
2009 static void wt_shortstatus_other(struct string_list_item
*it
,
2010 struct wt_status
*s
, const char *sign
)
2012 if (s
->null_termination
) {
2013 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2015 struct strbuf onebuf
= STRBUF_INIT
;
2017 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2018 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2019 fprintf(s
->fp
, " %s\n", one
);
2020 strbuf_release(&onebuf
);
2024 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2026 struct branch
*branch
;
2027 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2028 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2029 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2033 const char *branch_name
;
2034 int num_ours
, num_theirs
, sti
;
2035 int upstream_is_gone
= 0;
2037 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2041 branch_name
= s
->branch
;
2043 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2046 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2048 if (!strcmp(s
->branch
, "HEAD")) {
2049 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2050 LABEL(N_("HEAD (no branch)")));
2054 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2056 branch
= branch_get(branch_name
);
2058 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2060 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2061 0, s
->ahead_behind_flags
);
2066 upstream_is_gone
= 1;
2069 short_base
= shorten_unambiguous_ref(base
, 0);
2070 color_fprintf(s
->fp
, header_color
, "...");
2071 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2074 if (!upstream_is_gone
&& !sti
)
2077 color_fprintf(s
->fp
, header_color
, " [");
2078 if (upstream_is_gone
) {
2079 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2080 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2081 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2082 } else if (!num_ours
) {
2083 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2084 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2085 } else if (!num_theirs
) {
2086 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2087 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2089 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2090 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2091 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2092 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2095 color_fprintf(s
->fp
, header_color
, "]");
2097 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2100 static void wt_shortstatus_print(struct wt_status
*s
)
2102 struct string_list_item
*it
;
2105 wt_shortstatus_print_tracking(s
);
2107 for_each_string_list_item(it
, &s
->change
) {
2108 struct wt_status_change_data
*d
= it
->util
;
2111 wt_shortstatus_unmerged(it
, s
);
2113 wt_shortstatus_status(it
, s
);
2115 for_each_string_list_item(it
, &s
->untracked
)
2116 wt_shortstatus_other(it
, s
, "??");
2118 for_each_string_list_item(it
, &s
->ignored
)
2119 wt_shortstatus_other(it
, s
, "!!");
2122 static void wt_porcelain_print(struct wt_status
*s
)
2125 s
->relative_paths
= 0;
2128 wt_shortstatus_print(s
);
2132 * Print branch information for porcelain v2 output. These lines
2133 * are printed when the '--branch' parameter is given.
2135 * # branch.oid <commit><eol>
2136 * # branch.head <head><eol>
2137 * [# branch.upstream <upstream><eol>
2138 * [# branch.ab +<ahead> -<behind><eol>]]
2140 * <commit> ::= the current commit hash or the literal
2141 * "(initial)" to indicate an initialized repo
2144 * <head> ::= <branch_name> the current branch name or
2145 * "(detached)" literal when detached head or
2146 * "(unknown)" when something is wrong.
2148 * <upstream> ::= the upstream branch name, when set.
2150 * <ahead> ::= integer ahead value or '?'.
2152 * <behind> ::= integer behind value or '?'.
2154 * The end-of-line is defined by the -z flag.
2156 * <eol> ::= NUL when -z,
2159 * When an upstream is set and present, the 'branch.ab' line will
2160 * be printed with the ahead/behind counts for the branch and the
2161 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2162 * are different, '?' will be substituted for the actual count.
2164 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2166 struct branch
*branch
;
2168 const char *branch_name
;
2169 int ab_info
, nr_ahead
, nr_behind
;
2170 char eol
= s
->null_termination
? '\0' : '\n';
2172 fprintf(s
->fp
, "# branch.oid %s%c",
2173 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2177 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2179 if (!strcmp(s
->branch
, "HEAD")) {
2180 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2182 if (s
->state
.rebase_in_progress
||
2183 s
->state
.rebase_interactive_in_progress
)
2184 branch_name
= s
->state
.onto
;
2185 else if (s
->state
.detached_from
)
2186 branch_name
= s
->state
.detached_from
;
2191 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2193 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2196 /* Lookup stats on the upstream tracking branch, if set. */
2197 branch
= branch_get(branch_name
);
2199 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2200 &base
, 0, s
->ahead_behind_flags
);
2202 base
= shorten_unambiguous_ref(base
, 0);
2203 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2208 if (nr_ahead
|| nr_behind
)
2209 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2210 nr_ahead
, nr_behind
, eol
);
2212 fprintf(s
->fp
, "# branch.ab +? -?%c",
2214 } else if (!ab_info
) {
2216 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2223 * Print the stash count in a porcelain-friendly format
2225 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2227 int stash_count
= count_stash_entries();
2228 char eol
= s
->null_termination
? '\0' : '\n';
2230 if (stash_count
> 0)
2231 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2235 * Convert various submodule status values into a
2236 * fixed-length string of characters in the buffer provided.
2238 static void wt_porcelain_v2_submodule_state(
2239 struct wt_status_change_data
*d
,
2242 if (S_ISGITLINK(d
->mode_head
) ||
2243 S_ISGITLINK(d
->mode_index
) ||
2244 S_ISGITLINK(d
->mode_worktree
)) {
2246 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2247 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2248 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2259 * Fix-up changed entries before we print them.
2261 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2263 struct wt_status_change_data
*d
= it
->util
;
2265 if (!d
->index_status
) {
2267 * This entry is unchanged in the index (relative to the head).
2268 * Therefore, the collect_updated_cb was never called for this
2269 * entry (during the head-vs-index scan) and so the head column
2270 * fields were never set.
2272 * We must have data for the index column (from the
2273 * index-vs-worktree scan (otherwise, this entry should not be
2274 * in the list of changes)).
2276 * Copy index column fields to the head column, so that our
2277 * output looks complete.
2279 assert(d
->mode_head
== 0);
2280 d
->mode_head
= d
->mode_index
;
2281 oidcpy(&d
->oid_head
, &d
->oid_index
);
2284 if (!d
->worktree_status
) {
2286 * This entry is unchanged in the worktree (relative to the index).
2287 * Therefore, the collect_changed_cb was never called for this entry
2288 * (during the index-vs-worktree scan) and so the worktree column
2289 * fields were never set.
2291 * We must have data for the index column (from the head-vs-index
2294 * Copy the index column fields to the worktree column so that
2295 * our output looks complete.
2297 * Note that we only have a mode field in the worktree column
2298 * because the scan code tries really hard to not have to compute it.
2300 assert(d
->mode_worktree
== 0);
2301 d
->mode_worktree
= d
->mode_index
;
2306 * Print porcelain v2 info for tracked entries with changes.
2308 static void wt_porcelain_v2_print_changed_entry(
2309 struct string_list_item
*it
,
2310 struct wt_status
*s
)
2312 struct wt_status_change_data
*d
= it
->util
;
2313 struct strbuf buf
= STRBUF_INIT
;
2314 struct strbuf buf_from
= STRBUF_INIT
;
2315 const char *path
= NULL
;
2316 const char *path_from
= NULL
;
2318 char submodule_token
[5];
2319 char sep_char
, eol_char
;
2321 wt_porcelain_v2_fix_up_changed(it
);
2322 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2324 key
[0] = d
->index_status
? d
->index_status
: '.';
2325 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2328 if (s
->null_termination
) {
2330 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2331 * A single NUL character separates them.
2336 path_from
= d
->rename_source
;
2339 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2340 * The source path is only present when necessary.
2341 * A single TAB separates them (because paths can contain spaces
2342 * which are not escaped and C-quoting does escape TAB characters).
2346 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2347 if (d
->rename_source
)
2348 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2352 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2353 key
, submodule_token
,
2354 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2355 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2356 d
->rename_status
, d
->rename_score
,
2357 path
, sep_char
, path_from
, eol_char
);
2359 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2360 key
, submodule_token
,
2361 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2362 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2365 strbuf_release(&buf
);
2366 strbuf_release(&buf_from
);
2370 * Print porcelain v2 status info for unmerged entries.
2372 static void wt_porcelain_v2_print_unmerged_entry(
2373 struct string_list_item
*it
,
2374 struct wt_status
*s
)
2376 struct wt_status_change_data
*d
= it
->util
;
2377 struct index_state
*istate
= s
->repo
->index
;
2378 const struct cache_entry
*ce
;
2379 struct strbuf buf_index
= STRBUF_INIT
;
2380 const char *path_index
= NULL
;
2381 int pos
, stage
, sum
;
2384 struct object_id oid
;
2387 char submodule_token
[5];
2388 char unmerged_prefix
= 'u';
2389 char eol_char
= s
->null_termination
? '\0' : '\n';
2391 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2393 switch (d
->stagemask
) {
2394 case 1: key
= "DD"; break; /* both deleted */
2395 case 2: key
= "AU"; break; /* added by us */
2396 case 3: key
= "UD"; break; /* deleted by them */
2397 case 4: key
= "UA"; break; /* added by them */
2398 case 5: key
= "DU"; break; /* deleted by us */
2399 case 6: key
= "AA"; break; /* both added */
2400 case 7: key
= "UU"; break; /* both modified */
2402 BUG("unhandled unmerged status %x", d
->stagemask
);
2406 * Disregard d.aux.porcelain_v2 data that we accumulated
2407 * for the head and index columns during the scans and
2408 * replace with the actual stage data.
2410 * Note that this is a last-one-wins for each the individual
2411 * stage [123] columns in the event of multiple cache entries
2414 memset(stages
, 0, sizeof(stages
));
2416 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2419 while (pos
< istate
->cache_nr
) {
2420 ce
= istate
->cache
[pos
++];
2421 stage
= ce_stage(ce
);
2422 if (strcmp(ce
->name
, it
->string
) || !stage
)
2424 stages
[stage
- 1].mode
= ce
->ce_mode
;
2425 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2426 sum
|= (1 << (stage
- 1));
2428 if (sum
!= d
->stagemask
)
2429 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2431 if (s
->null_termination
)
2432 path_index
= it
->string
;
2434 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2436 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2437 unmerged_prefix
, key
, submodule_token
,
2438 stages
[0].mode
, /* stage 1 */
2439 stages
[1].mode
, /* stage 2 */
2440 stages
[2].mode
, /* stage 3 */
2442 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2443 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2444 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2448 strbuf_release(&buf_index
);
2452 * Print porcelain V2 status info for untracked and ignored entries.
2454 static void wt_porcelain_v2_print_other(
2455 struct string_list_item
*it
,
2456 struct wt_status
*s
,
2459 struct strbuf buf
= STRBUF_INIT
;
2463 if (s
->null_termination
) {
2467 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2471 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2473 strbuf_release(&buf
);
2477 * Print porcelain V2 status.
2480 * [<v2_changed_items>]*
2481 * [<v2_unmerged_items>]*
2482 * [<v2_untracked_items>]*
2483 * [<v2_ignored_items>]*
2486 static void wt_porcelain_v2_print(struct wt_status
*s
)
2488 struct wt_status_change_data
*d
;
2489 struct string_list_item
*it
;
2493 wt_porcelain_v2_print_tracking(s
);
2496 wt_porcelain_v2_print_stash(s
);
2498 for (i
= 0; i
< s
->change
.nr
; i
++) {
2499 it
= &(s
->change
.items
[i
]);
2502 wt_porcelain_v2_print_changed_entry(it
, s
);
2505 for (i
= 0; i
< s
->change
.nr
; i
++) {
2506 it
= &(s
->change
.items
[i
]);
2509 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2512 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2513 it
= &(s
->untracked
.items
[i
]);
2514 wt_porcelain_v2_print_other(it
, s
, '?');
2517 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2518 it
= &(s
->ignored
.items
[i
]);
2519 wt_porcelain_v2_print_other(it
, s
, '!');
2523 void wt_status_print(struct wt_status
*s
)
2525 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2526 trace2_data_intmax("status", s
->repo
, "count/untracked",
2528 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2530 trace2_region_enter("status", "print", s
->repo
);
2532 switch (s
->status_format
) {
2533 case STATUS_FORMAT_SHORT
:
2534 wt_shortstatus_print(s
);
2536 case STATUS_FORMAT_PORCELAIN
:
2537 wt_porcelain_print(s
);
2539 case STATUS_FORMAT_PORCELAIN_V2
:
2540 wt_porcelain_v2_print(s
);
2542 case STATUS_FORMAT_UNSPECIFIED
:
2543 BUG("finalize_deferred_config() should have been called");
2545 case STATUS_FORMAT_NONE
:
2546 case STATUS_FORMAT_LONG
:
2547 wt_longstatus_print(s
);
2551 trace2_region_leave("status", "print", s
->repo
);
2555 * Returns 1 if there are unstaged changes, 0 otherwise.
2557 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2559 struct rev_info rev_info
;
2562 repo_init_revisions(r
, &rev_info
, NULL
);
2563 if (ignore_submodules
) {
2564 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2565 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2567 rev_info
.diffopt
.flags
.quick
= 1;
2568 diff_setup_done(&rev_info
.diffopt
);
2569 result
= run_diff_files(&rev_info
, 0);
2570 result
= diff_result_code(&rev_info
.diffopt
, result
);
2571 release_revisions(&rev_info
);
2576 * Returns 1 if there are uncommitted changes, 0 otherwise.
2578 int has_uncommitted_changes(struct repository
*r
,
2579 int ignore_submodules
)
2581 struct rev_info rev_info
;
2584 if (is_index_unborn(r
->index
))
2587 repo_init_revisions(r
, &rev_info
, NULL
);
2588 if (ignore_submodules
)
2589 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2590 rev_info
.diffopt
.flags
.quick
= 1;
2592 add_head_to_pending(&rev_info
);
2593 if (!rev_info
.pending
.nr
) {
2595 * We have no head (or it's corrupt); use the empty tree,
2596 * which will complain if the index is non-empty.
2598 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2599 add_pending_object(&rev_info
, &tree
->object
, "");
2602 diff_setup_done(&rev_info
.diffopt
);
2603 result
= run_diff_index(&rev_info
, 1);
2604 result
= diff_result_code(&rev_info
.diffopt
, result
);
2605 release_revisions(&rev_info
);
2610 * If the work tree has unstaged or uncommitted changes, dies with the
2611 * appropriate message.
2613 int require_clean_work_tree(struct repository
*r
,
2616 int ignore_submodules
,
2619 struct lock_file lock_file
= LOCK_INIT
;
2622 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2623 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2625 repo_update_index_if_able(r
, &lock_file
);
2626 rollback_lock_file(&lock_file
);
2628 if (has_unstaged_changes(r
, ignore_submodules
)) {
2629 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2630 error(_("cannot %s: You have unstaged changes."), _(action
));
2634 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2636 error(_("additionally, your index contains uncommitted changes."));
2638 error(_("cannot %s: Your index contains uncommitted changes."),