8 #include "environment.h"
11 #include "object-name.h"
15 #include "run-command.h"
19 #include "submodule.h"
28 #include "sequencer.h"
29 #include "fsmonitor-settings.h"
31 #define AB_DELAY_WARNING_IN_MS (2 * 1000)
32 #define UF_DELAY_WARNING_IN_MS (2 * 1000)
34 static const char cut_line
[] =
35 "------------------------ >8 ------------------------\n";
37 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
38 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
39 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
40 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
41 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
42 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
43 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
44 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
45 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
46 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
49 static const char *color(int slot
, struct wt_status
*s
)
52 if (want_color(s
->use_color
))
53 c
= s
->color_palette
[slot
];
54 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
55 c
= s
->color_palette
[WT_STATUS_HEADER
];
59 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
60 const char *fmt
, va_list ap
, const char *trail
)
62 struct strbuf sb
= STRBUF_INIT
;
63 struct strbuf linebuf
= STRBUF_INIT
;
64 const char *line
, *eol
;
66 strbuf_vaddf(&sb
, fmt
, ap
);
68 if (s
->display_comment_prefix
) {
69 strbuf_addch(&sb
, comment_line_char
);
71 strbuf_addch(&sb
, ' ');
73 color_print_strbuf(s
->fp
, color
, &sb
);
75 fprintf(s
->fp
, "%s", trail
);
79 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
80 eol
= strchr(line
, '\n');
82 strbuf_reset(&linebuf
);
83 if (at_bol
&& s
->display_comment_prefix
) {
84 strbuf_addch(&linebuf
, comment_line_char
);
85 if (*line
!= '\n' && *line
!= '\t')
86 strbuf_addch(&linebuf
, ' ');
89 strbuf_add(&linebuf
, line
, eol
- line
);
91 strbuf_addstr(&linebuf
, line
);
92 color_print_strbuf(s
->fp
, color
, &linebuf
);
100 fprintf(s
->fp
, "%s", trail
);
101 strbuf_release(&linebuf
);
105 void status_printf_ln(struct wt_status
*s
, const char *color
,
106 const char *fmt
, ...)
111 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
115 void status_printf(struct wt_status
*s
, const char *color
,
116 const char *fmt
, ...)
121 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
125 static void status_printf_more(struct wt_status
*s
, const char *color
,
126 const char *fmt
, ...)
131 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
135 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
137 memset(s
, 0, sizeof(*s
));
139 memcpy(s
->color_palette
, default_wt_status_colors
,
140 sizeof(default_wt_status_colors
));
141 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
143 s
->relative_paths
= 1;
144 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
145 s
->reference
= "HEAD";
147 s
->index_file
= get_index_file();
148 s
->change
.strdup_strings
= 1;
149 s
->untracked
.strdup_strings
= 1;
150 s
->ignored
.strdup_strings
= 1;
151 s
->show_branch
= -1; /* unspecified */
153 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
154 s
->display_comment_prefix
= 0;
155 s
->detect_rename
= -1;
156 s
->rename_score
= -1;
157 s
->rename_limit
= -1;
160 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
163 int del_mod_conflict
= 0;
164 int both_deleted
= 0;
166 const char *c
= color(WT_STATUS_HEADER
, s
);
168 status_printf_ln(s
, c
, _("Unmerged paths:"));
170 for (i
= 0; i
< s
->change
.nr
; i
++) {
171 struct string_list_item
*it
= &(s
->change
.items
[i
]);
172 struct wt_status_change_data
*d
= it
->util
;
174 switch (d
->stagemask
) {
182 del_mod_conflict
= 1;
192 if (s
->whence
!= FROM_COMMIT
)
194 else if (!s
->is_initial
) {
195 if (!strcmp(s
->reference
, "HEAD"))
196 status_printf_ln(s
, c
,
197 _(" (use \"git restore --staged <file>...\" to unstage)"));
199 status_printf_ln(s
, c
,
200 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
203 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
206 if (!del_mod_conflict
)
207 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
209 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
210 } else if (!del_mod_conflict
&& !not_deleted
) {
211 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
213 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
217 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
219 const char *c
= color(WT_STATUS_HEADER
, s
);
221 status_printf_ln(s
, c
, _("Changes to be committed:"));
224 if (s
->whence
!= FROM_COMMIT
)
225 ; /* NEEDSWORK: use "git reset --unresolve"??? */
226 else if (!s
->is_initial
) {
227 if (!strcmp(s
->reference
, "HEAD"))
228 status_printf_ln(s
, c
229 , _(" (use \"git restore --staged <file>...\" to unstage)"));
231 status_printf_ln(s
, c
,
232 _(" (use \"git restore --source=%s --staged <file>...\" to unstage)"),
235 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
238 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
240 int has_dirty_submodules
)
242 const char *c
= color(WT_STATUS_HEADER
, s
);
244 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
248 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
250 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
251 status_printf_ln(s
, c
, _(" (use \"git restore <file>...\" to discard changes in working directory)"));
252 if (has_dirty_submodules
)
253 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
256 static void wt_longstatus_print_other_header(struct wt_status
*s
,
260 const char *c
= color(WT_STATUS_HEADER
, s
);
261 status_printf_ln(s
, c
, "%s:", what
);
264 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
267 static void wt_longstatus_print_trailer(struct wt_status
*s
)
269 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
272 static const char *wt_status_unmerged_status_string(int stagemask
)
276 return _("both deleted:");
278 return _("added by us:");
280 return _("deleted by them:");
282 return _("added by them:");
284 return _("deleted by us:");
286 return _("both added:");
288 return _("both modified:");
290 BUG("unhandled unmerged status %x", stagemask
);
294 static const char *wt_status_diff_status_string(int status
)
297 case DIFF_STATUS_ADDED
:
298 return _("new file:");
299 case DIFF_STATUS_COPIED
:
301 case DIFF_STATUS_DELETED
:
302 return _("deleted:");
303 case DIFF_STATUS_MODIFIED
:
304 return _("modified:");
305 case DIFF_STATUS_RENAMED
:
306 return _("renamed:");
307 case DIFF_STATUS_TYPE_CHANGED
:
308 return _("typechange:");
309 case DIFF_STATUS_UNKNOWN
:
310 return _("unknown:");
311 case DIFF_STATUS_UNMERGED
:
312 return _("unmerged:");
318 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
322 for (i
= minval
; i
<= maxval
; i
++) {
323 const char *s
= label(i
);
324 int len
= s
? utf8_strwidth(s
) : 0;
331 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
332 struct string_list_item
*it
)
334 const char *c
= color(WT_STATUS_UNMERGED
, s
);
335 struct wt_status_change_data
*d
= it
->util
;
336 struct strbuf onebuf
= STRBUF_INIT
;
337 static char *padding
;
338 static int label_width
;
339 const char *one
, *how
;
343 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
344 label_width
+= strlen(" ");
345 padding
= xmallocz(label_width
);
346 memset(padding
, ' ', label_width
);
349 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, 0);
350 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
352 how
= wt_status_unmerged_status_string(d
->stagemask
);
353 len
= label_width
- utf8_strwidth(how
);
354 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
355 strbuf_release(&onebuf
);
358 static void wt_longstatus_print_change_data(struct wt_status
*s
,
360 struct string_list_item
*it
)
362 struct wt_status_change_data
*d
= it
->util
;
363 const char *c
= color(change_type
, s
);
367 const char *one
, *two
;
368 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
369 struct strbuf extra
= STRBUF_INIT
;
370 static char *padding
;
371 static int label_width
;
376 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
377 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
378 label_width
+= strlen(" ");
379 padding
= xmallocz(label_width
);
380 memset(padding
, ' ', label_width
);
383 one_name
= two_name
= it
->string
;
384 switch (change_type
) {
385 case WT_STATUS_UPDATED
:
386 status
= d
->index_status
;
388 case WT_STATUS_CHANGED
:
389 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
390 strbuf_addstr(&extra
, " (");
391 if (d
->new_submodule_commits
)
392 strbuf_addstr(&extra
, _("new commits, "));
393 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
394 strbuf_addstr(&extra
, _("modified content, "));
395 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
396 strbuf_addstr(&extra
, _("untracked content, "));
397 strbuf_setlen(&extra
, extra
.len
- 2);
398 strbuf_addch(&extra
, ')');
400 status
= d
->worktree_status
;
403 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
408 * Only pick up the rename it's relevant. If the rename is for
409 * the changed section and we're printing the updated section,
412 if (d
->rename_status
== status
)
413 one_name
= d
->rename_source
;
415 one
= quote_path(one_name
, s
->prefix
, &onebuf
, 0);
416 two
= quote_path(two_name
, s
->prefix
, &twobuf
, 0);
418 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
419 what
= wt_status_diff_status_string(status
);
421 BUG("unhandled diff status %c", status
);
422 len
= label_width
- utf8_strwidth(what
);
424 if (one_name
!= two_name
)
425 status_printf_more(s
, c
, "%s%.*s%s -> %s",
426 what
, len
, padding
, one
, two
);
428 status_printf_more(s
, c
, "%s%.*s%s",
429 what
, len
, padding
, one
);
431 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
432 strbuf_release(&extra
);
434 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
435 strbuf_release(&onebuf
);
436 strbuf_release(&twobuf
);
439 static char short_submodule_status(struct wt_status_change_data
*d
)
441 if (d
->new_submodule_commits
)
443 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
445 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
447 return d
->worktree_status
;
450 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
451 struct diff_options
*options UNUSED
,
454 struct wt_status
*s
= data
;
459 s
->workdir_dirty
= 1;
460 for (i
= 0; i
< q
->nr
; i
++) {
461 struct diff_filepair
*p
;
462 struct string_list_item
*it
;
463 struct wt_status_change_data
*d
;
466 it
= string_list_insert(&s
->change
, p
->two
->path
);
472 if (!d
->worktree_status
)
473 d
->worktree_status
= p
->status
;
474 if (S_ISGITLINK(p
->two
->mode
)) {
475 d
->dirty_submodule
= p
->two
->dirty_submodule
;
476 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
478 if (s
->status_format
== STATUS_FORMAT_SHORT
)
479 d
->worktree_status
= short_submodule_status(d
);
483 case DIFF_STATUS_ADDED
:
484 d
->mode_worktree
= p
->two
->mode
;
487 case DIFF_STATUS_DELETED
:
488 d
->mode_index
= p
->one
->mode
;
489 oidcpy(&d
->oid_index
, &p
->one
->oid
);
490 /* mode_worktree is zero for a delete. */
493 case DIFF_STATUS_COPIED
:
494 case DIFF_STATUS_RENAMED
:
495 if (d
->rename_status
)
496 BUG("multiple renames on the same target? how?");
497 d
->rename_source
= xstrdup(p
->one
->path
);
498 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
499 d
->rename_status
= p
->status
;
501 case DIFF_STATUS_MODIFIED
:
502 case DIFF_STATUS_TYPE_CHANGED
:
503 case DIFF_STATUS_UNMERGED
:
504 d
->mode_index
= p
->one
->mode
;
505 d
->mode_worktree
= p
->two
->mode
;
506 oidcpy(&d
->oid_index
, &p
->one
->oid
);
510 BUG("unhandled diff-files status '%c'", p
->status
);
517 static int unmerged_mask(struct index_state
*istate
, const char *path
)
520 const struct cache_entry
*ce
;
522 pos
= index_name_pos(istate
, path
, strlen(path
));
528 while (pos
< istate
->cache_nr
) {
529 ce
= istate
->cache
[pos
++];
530 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
532 mask
|= (1 << (ce_stage(ce
) - 1));
537 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
538 struct diff_options
*options UNUSED
,
541 struct wt_status
*s
= data
;
544 for (i
= 0; i
< q
->nr
; i
++) {
545 struct diff_filepair
*p
;
546 struct string_list_item
*it
;
547 struct wt_status_change_data
*d
;
550 it
= string_list_insert(&s
->change
, p
->two
->path
);
556 if (!d
->index_status
)
557 d
->index_status
= p
->status
;
559 case DIFF_STATUS_ADDED
:
560 /* Leave {mode,oid}_head zero for an add. */
561 d
->mode_index
= p
->two
->mode
;
562 oidcpy(&d
->oid_index
, &p
->two
->oid
);
565 case DIFF_STATUS_DELETED
:
566 d
->mode_head
= p
->one
->mode
;
567 oidcpy(&d
->oid_head
, &p
->one
->oid
);
569 /* Leave {mode,oid}_index zero for a delete. */
572 case DIFF_STATUS_COPIED
:
573 case DIFF_STATUS_RENAMED
:
574 if (d
->rename_status
)
575 BUG("multiple renames on the same target? how?");
576 d
->rename_source
= xstrdup(p
->one
->path
);
577 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
578 d
->rename_status
= p
->status
;
580 case DIFF_STATUS_MODIFIED
:
581 case DIFF_STATUS_TYPE_CHANGED
:
582 d
->mode_head
= p
->one
->mode
;
583 d
->mode_index
= p
->two
->mode
;
584 oidcpy(&d
->oid_head
, &p
->one
->oid
);
585 oidcpy(&d
->oid_index
, &p
->two
->oid
);
588 case DIFF_STATUS_UNMERGED
:
589 d
->stagemask
= unmerged_mask(s
->repo
->index
,
592 * Don't bother setting {mode,oid}_{head,index} since the print
593 * code will output the stage values directly and not use the
594 * values in these fields.
599 BUG("unhandled diff-index status '%c'", p
->status
);
605 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
609 repo_init_revisions(s
->repo
, &rev
, NULL
);
610 setup_revisions(0, NULL
, &rev
, NULL
);
611 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
612 rev
.diffopt
.flags
.dirty_submodules
= 1;
613 rev
.diffopt
.ita_invisible_in_index
= 1;
614 if (!s
->show_untracked_files
)
615 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
616 if (s
->ignore_submodule_arg
) {
617 rev
.diffopt
.flags
.override_submodule_config
= 1;
618 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
619 } else if (!rev
.diffopt
.flags
.ignore_submodule_set
&&
620 s
->show_untracked_files
!= SHOW_NO_UNTRACKED_FILES
)
621 handle_ignore_submodules_arg(&rev
.diffopt
, "none");
622 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
623 rev
.diffopt
.format_callback_data
= s
;
624 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
625 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
626 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
627 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
628 run_diff_files(&rev
, 0);
629 release_revisions(&rev
);
632 static void wt_status_collect_changes_index(struct wt_status
*s
)
635 struct setup_revision_opt opt
;
637 repo_init_revisions(s
->repo
, &rev
, NULL
);
638 memset(&opt
, 0, sizeof(opt
));
639 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
640 setup_revisions(0, NULL
, &rev
, &opt
);
642 rev
.diffopt
.flags
.override_submodule_config
= 1;
643 rev
.diffopt
.ita_invisible_in_index
= 1;
644 if (s
->ignore_submodule_arg
) {
645 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
648 * Unless the user did explicitly request a submodule ignore
649 * mode by passing a command line option we do not ignore any
650 * changed submodule SHA-1s when comparing index and HEAD, no
651 * matter what is configured. Otherwise the user won't be
652 * shown any submodules manually added (and which are
653 * staged to be committed), which would be really confusing.
655 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
658 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
659 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
660 rev
.diffopt
.format_callback_data
= s
;
661 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
662 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
663 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
666 * The `recursive` option must be enabled to allow the diff to recurse
667 * into subdirectories of sparse directory index entries. If it is not
668 * enabled, a subdirectory containing file(s) with changes is reported
669 * as "modified", rather than the modified files themselves.
671 rev
.diffopt
.flags
.recursive
= 1;
673 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
674 run_diff_index(&rev
, 1);
675 release_revisions(&rev
);
678 static int add_file_to_list(const struct object_id
*oid
,
679 struct strbuf
*base
, const char *path
,
680 unsigned int mode
, void *context
)
682 struct string_list_item
*it
;
683 struct wt_status_change_data
*d
;
684 struct wt_status
*s
= context
;
685 struct strbuf full_name
= STRBUF_INIT
;
688 return READ_TREE_RECURSIVE
;
690 strbuf_add(&full_name
, base
->buf
, base
->len
);
691 strbuf_addstr(&full_name
, path
);
692 it
= string_list_insert(&s
->change
, full_name
.buf
);
699 d
->index_status
= DIFF_STATUS_ADDED
;
700 /* Leave {mode,oid}_head zero for adds. */
701 d
->mode_index
= mode
;
702 oidcpy(&d
->oid_index
, oid
);
704 strbuf_release(&full_name
);
708 static void wt_status_collect_changes_initial(struct wt_status
*s
)
710 struct index_state
*istate
= s
->repo
->index
;
713 for (i
= 0; i
< istate
->cache_nr
; i
++) {
714 struct string_list_item
*it
;
715 struct wt_status_change_data
*d
;
716 const struct cache_entry
*ce
= istate
->cache
[i
];
718 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
720 if (ce_intent_to_add(ce
))
722 if (S_ISSPARSEDIR(ce
->ce_mode
)) {
724 * This is a sparse directory entry, so we want to collect all
725 * of the added files within the tree. This requires recursively
726 * expanding the trees to find the elements that are new in this
727 * tree and marking them with DIFF_STATUS_ADDED.
729 struct strbuf base
= STRBUF_INIT
;
730 struct pathspec ps
= { 0 };
731 struct tree
*tree
= lookup_tree(istate
->repo
, &ce
->oid
);
737 strbuf_add(&base
, ce
->name
, ce
->ce_namelen
);
738 read_tree_at(istate
->repo
, tree
, &base
, &ps
,
739 add_file_to_list
, s
);
743 it
= string_list_insert(&s
->change
, ce
->name
);
750 d
->index_status
= DIFF_STATUS_UNMERGED
;
751 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
753 * Don't bother setting {mode,oid}_{head,index} since the print
754 * code will output the stage values directly and not use the
755 * values in these fields.
759 d
->index_status
= DIFF_STATUS_ADDED
;
760 /* Leave {mode,oid}_head zero for adds. */
761 d
->mode_index
= ce
->ce_mode
;
762 oidcpy(&d
->oid_index
, &ce
->oid
);
768 static void wt_status_collect_untracked(struct wt_status
*s
)
771 struct dir_struct dir
= DIR_INIT
;
772 uint64_t t_begin
= getnanotime();
773 struct index_state
*istate
= s
->repo
->index
;
775 if (!s
->show_untracked_files
)
778 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
780 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
781 if (s
->show_ignored_mode
) {
782 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
784 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
785 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
787 dir
.untracked
= istate
->untracked
;
790 setup_standard_excludes(&dir
);
792 fill_directory(&dir
, istate
, &s
->pathspec
);
794 for (i
= 0; i
< dir
.nr
; i
++) {
795 struct dir_entry
*ent
= dir
.entries
[i
];
796 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
797 string_list_insert(&s
->untracked
, ent
->name
);
800 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
801 struct dir_entry
*ent
= dir
.ignored
[i
];
802 if (index_name_is_other(istate
, ent
->name
, ent
->len
))
803 string_list_insert(&s
->ignored
, ent
->name
);
808 if (advice_enabled(ADVICE_STATUS_U_OPTION
))
809 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
812 static int has_unmerged(struct wt_status
*s
)
816 for (i
= 0; i
< s
->change
.nr
; i
++) {
817 struct wt_status_change_data
*d
;
818 d
= s
->change
.items
[i
].util
;
825 void wt_status_collect(struct wt_status
*s
)
827 trace2_region_enter("status", "worktrees", s
->repo
);
828 wt_status_collect_changes_worktree(s
);
829 trace2_region_leave("status", "worktrees", s
->repo
);
832 trace2_region_enter("status", "initial", s
->repo
);
833 wt_status_collect_changes_initial(s
);
834 trace2_region_leave("status", "initial", s
->repo
);
836 trace2_region_enter("status", "index", s
->repo
);
837 wt_status_collect_changes_index(s
);
838 trace2_region_leave("status", "index", s
->repo
);
841 trace2_region_enter("status", "untracked", s
->repo
);
842 wt_status_collect_untracked(s
);
843 trace2_region_leave("status", "untracked", s
->repo
);
845 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
846 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
850 void wt_status_collect_free_buffers(struct wt_status
*s
)
852 wt_status_state_free_buffers(&s
->state
);
855 void wt_status_state_free_buffers(struct wt_status_state
*state
)
857 FREE_AND_NULL(state
->branch
);
858 FREE_AND_NULL(state
->onto
);
859 FREE_AND_NULL(state
->detached_from
);
862 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
864 int shown_header
= 0;
867 for (i
= 0; i
< s
->change
.nr
; i
++) {
868 struct wt_status_change_data
*d
;
869 struct string_list_item
*it
;
870 it
= &(s
->change
.items
[i
]);
875 wt_longstatus_print_unmerged_header(s
);
878 wt_longstatus_print_unmerged_data(s
, it
);
881 wt_longstatus_print_trailer(s
);
885 static void wt_longstatus_print_updated(struct wt_status
*s
)
887 int shown_header
= 0;
890 for (i
= 0; i
< s
->change
.nr
; i
++) {
891 struct wt_status_change_data
*d
;
892 struct string_list_item
*it
;
893 it
= &(s
->change
.items
[i
]);
895 if (!d
->index_status
||
896 d
->index_status
== DIFF_STATUS_UNMERGED
)
899 wt_longstatus_print_cached_header(s
);
902 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
905 wt_longstatus_print_trailer(s
);
911 * 1 : some change but no delete
913 static int wt_status_check_worktree_changes(struct wt_status
*s
,
914 int *dirty_submodules
)
919 *dirty_submodules
= 0;
921 for (i
= 0; i
< s
->change
.nr
; i
++) {
922 struct wt_status_change_data
*d
;
923 d
= s
->change
.items
[i
].util
;
924 if (!d
->worktree_status
||
925 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
929 if (d
->dirty_submodule
)
930 *dirty_submodules
= 1;
931 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
937 static void wt_longstatus_print_changed(struct wt_status
*s
)
939 int i
, dirty_submodules
;
940 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
942 if (!worktree_changes
)
945 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
947 for (i
= 0; i
< s
->change
.nr
; i
++) {
948 struct wt_status_change_data
*d
;
949 struct string_list_item
*it
;
950 it
= &(s
->change
.items
[i
]);
952 if (!d
->worktree_status
||
953 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
955 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
957 wt_longstatus_print_trailer(s
);
960 static int stash_count_refs(struct object_id
*ooid UNUSED
,
961 struct object_id
*noid UNUSED
,
962 const char *email UNUSED
,
963 timestamp_t timestamp UNUSED
, int tz UNUSED
,
964 const char *message UNUSED
, void *cb_data
)
971 static int count_stash_entries(void)
974 for_each_reflog_ent("refs/stash", stash_count_refs
, &n
);
978 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
980 int stash_count
= count_stash_entries();
983 status_printf_ln(s
, GIT_COLOR_NORMAL
,
984 Q_("Your stash currently has %d entry",
985 "Your stash currently has %d entries", stash_count
),
989 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
991 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
992 struct strbuf cmd_stdout
= STRBUF_INIT
;
993 struct strbuf summary
= STRBUF_INIT
;
994 char *summary_content
;
996 strvec_pushf(&sm_summary
.env
, "GIT_INDEX_FILE=%s", s
->index_file
);
998 strvec_push(&sm_summary
.args
, "submodule");
999 strvec_push(&sm_summary
.args
, "summary");
1000 strvec_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
1001 strvec_push(&sm_summary
.args
, "--for-status");
1002 strvec_push(&sm_summary
.args
, "--summary-limit");
1003 strvec_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
1005 strvec_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
1007 sm_summary
.git_cmd
= 1;
1008 sm_summary
.no_stdin
= 1;
1010 capture_command(&sm_summary
, &cmd_stdout
, 1024);
1012 /* prepend header, only if there's an actual output */
1013 if (cmd_stdout
.len
) {
1015 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
1017 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
1018 strbuf_addstr(&summary
, "\n\n");
1020 strbuf_addbuf(&summary
, &cmd_stdout
);
1021 strbuf_release(&cmd_stdout
);
1023 if (s
->display_comment_prefix
) {
1025 summary_content
= strbuf_detach(&summary
, &len
);
1026 strbuf_add_commented_lines(&summary
, summary_content
, len
);
1027 free(summary_content
);
1030 fputs(summary
.buf
, s
->fp
);
1031 strbuf_release(&summary
);
1034 static void wt_longstatus_print_other(struct wt_status
*s
,
1035 struct string_list
*l
,
1040 struct strbuf buf
= STRBUF_INIT
;
1041 static struct string_list output
= STRING_LIST_INIT_DUP
;
1042 struct column_options copts
;
1047 wt_longstatus_print_other_header(s
, what
, how
);
1049 for (i
= 0; i
< l
->nr
; i
++) {
1050 struct string_list_item
*it
;
1052 it
= &(l
->items
[i
]);
1053 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
1054 if (column_active(s
->colopts
)) {
1055 string_list_append(&output
, path
);
1058 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
1059 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
1063 strbuf_release(&buf
);
1064 if (!column_active(s
->colopts
))
1067 strbuf_addf(&buf
, "%s%s\t%s",
1068 color(WT_STATUS_HEADER
, s
),
1069 s
->display_comment_prefix
? "#" : "",
1070 color(WT_STATUS_UNTRACKED
, s
));
1071 memset(&copts
, 0, sizeof(copts
));
1073 copts
.indent
= buf
.buf
;
1074 if (want_color(s
->use_color
))
1075 copts
.nl
= GIT_COLOR_RESET
"\n";
1076 print_columns(&output
, s
->colopts
, &copts
);
1077 string_list_clear(&output
, 0);
1078 strbuf_release(&buf
);
1080 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1083 size_t wt_status_locate_end(const char *s
, size_t len
)
1086 struct strbuf pattern
= STRBUF_INIT
;
1088 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1089 if (starts_with(s
, pattern
.buf
+ 1))
1091 else if ((p
= strstr(s
, pattern
.buf
)))
1093 strbuf_release(&pattern
);
1097 void wt_status_append_cut_line(struct strbuf
*buf
)
1099 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1101 strbuf_commented_addf(buf
, "%s", cut_line
);
1102 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1105 void wt_status_add_cut_line(FILE *fp
)
1107 struct strbuf buf
= STRBUF_INIT
;
1109 wt_status_append_cut_line(&buf
);
1111 strbuf_release(&buf
);
1114 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1116 struct rev_info rev
;
1117 struct setup_revision_opt opt
;
1118 int dirty_submodules
;
1119 const char *c
= color(WT_STATUS_HEADER
, s
);
1121 repo_init_revisions(s
->repo
, &rev
, NULL
);
1122 rev
.diffopt
.flags
.allow_textconv
= 1;
1123 rev
.diffopt
.ita_invisible_in_index
= 1;
1125 memset(&opt
, 0, sizeof(opt
));
1126 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1127 setup_revisions(0, NULL
, &rev
, &opt
);
1129 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1130 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1131 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1132 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1133 rev
.diffopt
.file
= s
->fp
;
1134 rev
.diffopt
.close_file
= 0;
1136 * If we're not going to stdout, then we definitely don't
1137 * want color, since we are going to the commit message
1138 * file (and even the "auto" setting won't work, since it
1139 * will have checked isatty on stdout). But we then do want
1140 * to insert the scissor line here to reliably remove the
1141 * diff before committing.
1143 if (s
->fp
!= stdout
) {
1144 rev
.diffopt
.use_color
= 0;
1145 wt_status_add_cut_line(s
->fp
);
1147 if (s
->verbose
> 1 && s
->committable
) {
1148 /* print_updated() printed a header, so do we */
1149 if (s
->fp
!= stdout
)
1150 wt_longstatus_print_trailer(s
);
1151 status_printf_ln(s
, c
, _("Changes to be committed:"));
1152 rev
.diffopt
.a_prefix
= "c/";
1153 rev
.diffopt
.b_prefix
= "i/";
1154 } /* else use prefix as per user config */
1155 run_diff_index(&rev
, 1);
1156 if (s
->verbose
> 1 &&
1157 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1158 status_printf_ln(s
, c
,
1159 "--------------------------------------------------");
1160 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1162 rev
.diffopt
.a_prefix
= "i/";
1163 rev
.diffopt
.b_prefix
= "w/";
1164 run_diff_files(&rev
, 0);
1166 release_revisions(&rev
);
1169 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1171 struct strbuf sb
= STRBUF_INIT
;
1172 const char *cp
, *ep
, *branch_name
;
1173 struct branch
*branch
;
1174 char comment_line_string
[3];
1176 uint64_t t_begin
= 0;
1178 assert(s
->branch
&& !s
->is_initial
);
1179 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1181 branch
= branch_get(branch_name
);
1183 t_begin
= getnanotime();
1185 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1188 if (advice_enabled(ADVICE_STATUS_AHEAD_BEHIND_WARNING
) &&
1189 s
->ahead_behind_flags
== AHEAD_BEHIND_FULL
) {
1190 uint64_t t_delta_in_ms
= (getnanotime() - t_begin
) / 1000000;
1191 if (t_delta_in_ms
> AB_DELAY_WARNING_IN_MS
) {
1192 strbuf_addf(&sb
, _("\n"
1193 "It took %.2f seconds to compute the branch ahead/behind values.\n"
1194 "You can use '--no-ahead-behind' to avoid this.\n"),
1195 t_delta_in_ms
/ 1000.0);
1200 if (s
->display_comment_prefix
) {
1201 comment_line_string
[i
++] = comment_line_char
;
1202 comment_line_string
[i
++] = ' ';
1204 comment_line_string
[i
] = '\0';
1206 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1207 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1208 "%s%.*s", comment_line_string
,
1209 (int)(ep
- cp
), cp
);
1210 if (s
->display_comment_prefix
)
1211 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1215 strbuf_release(&sb
);
1218 static int uf_was_slow(struct wt_status
*s
)
1220 if (getenv("GIT_TEST_UF_DELAY_WARNING"))
1221 s
->untracked_in_ms
= 3250;
1222 return UF_DELAY_WARNING_IN_MS
< s
->untracked_in_ms
;
1225 static void show_merge_in_progress(struct wt_status
*s
,
1228 if (has_unmerged(s
)) {
1229 status_printf_ln(s
, color
, _("You have unmerged paths."));
1231 status_printf_ln(s
, color
,
1232 _(" (fix conflicts and run \"git commit\")"));
1233 status_printf_ln(s
, color
,
1234 _(" (use \"git merge --abort\" to abort the merge)"));
1237 status_printf_ln(s
, color
,
1238 _("All conflicts fixed but you are still merging."));
1240 status_printf_ln(s
, color
,
1241 _(" (use \"git commit\" to conclude merge)"));
1243 wt_longstatus_print_trailer(s
);
1246 static void show_am_in_progress(struct wt_status
*s
,
1251 status_printf_ln(s
, color
,
1252 _("You are in the middle of an am session."));
1253 if (s
->state
.am_empty_patch
)
1254 status_printf_ln(s
, color
,
1255 _("The current patch is empty."));
1257 am_empty_patch
= s
->state
.am_empty_patch
;
1258 if (!am_empty_patch
)
1259 status_printf_ln(s
, color
,
1260 _(" (fix conflicts and then run \"git am --continue\")"));
1261 status_printf_ln(s
, color
,
1262 _(" (use \"git am --skip\" to skip this patch)"));
1264 status_printf_ln(s
, color
,
1265 _(" (use \"git am --allow-empty\" to record this patch as an empty commit)"));
1266 status_printf_ln(s
, color
,
1267 _(" (use \"git am --abort\" to restore the original branch)"));
1269 wt_longstatus_print_trailer(s
);
1272 static char *read_line_from_git_path(const char *filename
)
1274 struct strbuf buf
= STRBUF_INIT
;
1275 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1278 strbuf_release(&buf
);
1281 strbuf_getline_lf(&buf
, fp
);
1283 return strbuf_detach(&buf
, NULL
);
1285 strbuf_release(&buf
);
1290 static int split_commit_in_progress(struct wt_status
*s
)
1292 int split_in_progress
= 0;
1293 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1295 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1296 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1299 head
= read_line_from_git_path("HEAD");
1300 orig_head
= read_line_from_git_path("ORIG_HEAD");
1301 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1302 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1304 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1305 ; /* fall through, no split in progress */
1306 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1307 split_in_progress
= !!strcmp(head
, rebase_amend
);
1308 else if (strcmp(orig_head
, rebase_orig_head
))
1309 split_in_progress
= 1;
1314 free(rebase_orig_head
);
1316 return split_in_progress
;
1321 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1323 * "pick d6a2f03 some message"
1325 * The function assumes that the line does not contain useless spaces
1326 * before or after the command.
1328 static void abbrev_oid_in_line(struct strbuf
*line
)
1330 struct strbuf
**split
;
1333 if (starts_with(line
->buf
, "exec ") ||
1334 starts_with(line
->buf
, "x ") ||
1335 starts_with(line
->buf
, "label ") ||
1336 starts_with(line
->buf
, "l "))
1339 split
= strbuf_split_max(line
, ' ', 3);
1340 if (split
[0] && split
[1]) {
1341 struct object_id oid
;
1344 * strbuf_split_max left a space. Trim it and re-add
1345 * it after abbreviation.
1347 strbuf_trim(split
[1]);
1348 if (!repo_get_oid(the_repository
, split
[1]->buf
, &oid
)) {
1349 strbuf_reset(split
[1]);
1350 strbuf_add_unique_abbrev(split
[1], &oid
,
1352 strbuf_addch(split
[1], ' ');
1354 for (i
= 0; split
[i
]; i
++)
1355 strbuf_addbuf(line
, split
[i
]);
1358 strbuf_list_free(split
);
1361 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1363 struct strbuf line
= STRBUF_INIT
;
1364 FILE *f
= fopen(git_path("%s", fname
), "r");
1367 if (errno
== ENOENT
)
1369 die_errno("Could not open file %s for reading",
1370 git_path("%s", fname
));
1372 while (!strbuf_getline_lf(&line
, f
)) {
1373 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1378 abbrev_oid_in_line(&line
);
1379 string_list_append(lines
, line
.buf
);
1382 strbuf_release(&line
);
1386 static void show_rebase_information(struct wt_status
*s
,
1389 if (s
->state
.rebase_interactive_in_progress
) {
1391 int nr_lines_to_show
= 2;
1393 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1394 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1396 read_rebase_todolist("rebase-merge/done", &have_done
);
1397 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1399 status_printf_ln(s
, color
,
1400 _("git-rebase-todo is missing."));
1401 if (have_done
.nr
== 0)
1402 status_printf_ln(s
, color
, _("No commands done."));
1404 status_printf_ln(s
, color
,
1405 Q_("Last command done (%"PRIuMAX
" command done):",
1406 "Last commands done (%"PRIuMAX
" commands done):",
1408 (uintmax_t)have_done
.nr
);
1409 for (i
= (have_done
.nr
> nr_lines_to_show
)
1410 ? have_done
.nr
- nr_lines_to_show
: 0;
1413 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1414 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1415 status_printf_ln(s
, color
,
1416 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1419 if (yet_to_do
.nr
== 0)
1420 status_printf_ln(s
, color
,
1421 _("No commands remaining."));
1423 status_printf_ln(s
, color
,
1424 Q_("Next command to do (%"PRIuMAX
" remaining command):",
1425 "Next commands to do (%"PRIuMAX
" remaining commands):",
1427 (uintmax_t)yet_to_do
.nr
);
1428 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1429 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1431 status_printf_ln(s
, color
,
1432 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1434 string_list_clear(&yet_to_do
, 0);
1435 string_list_clear(&have_done
, 0);
1439 static void print_rebase_state(struct wt_status
*s
,
1442 if (s
->state
.branch
)
1443 status_printf_ln(s
, color
,
1444 _("You are currently rebasing branch '%s' on '%s'."),
1448 status_printf_ln(s
, color
,
1449 _("You are currently rebasing."));
1452 static void show_rebase_in_progress(struct wt_status
*s
,
1457 show_rebase_information(s
, color
);
1458 if (has_unmerged(s
)) {
1459 print_rebase_state(s
, color
);
1461 status_printf_ln(s
, color
,
1462 _(" (fix conflicts and then run \"git rebase --continue\")"));
1463 status_printf_ln(s
, color
,
1464 _(" (use \"git rebase --skip\" to skip this patch)"));
1465 status_printf_ln(s
, color
,
1466 _(" (use \"git rebase --abort\" to check out the original branch)"));
1468 } else if (s
->state
.rebase_in_progress
||
1469 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1470 print_rebase_state(s
, color
);
1472 status_printf_ln(s
, color
,
1473 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1474 } else if (split_commit_in_progress(s
)) {
1475 if (s
->state
.branch
)
1476 status_printf_ln(s
, color
,
1477 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1481 status_printf_ln(s
, color
,
1482 _("You are currently splitting a commit during a rebase."));
1484 status_printf_ln(s
, color
,
1485 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1487 if (s
->state
.branch
)
1488 status_printf_ln(s
, color
,
1489 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1493 status_printf_ln(s
, color
,
1494 _("You are currently editing a commit during a rebase."));
1495 if (s
->hints
&& !s
->amend
) {
1496 status_printf_ln(s
, color
,
1497 _(" (use \"git commit --amend\" to amend the current commit)"));
1498 status_printf_ln(s
, color
,
1499 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1502 wt_longstatus_print_trailer(s
);
1505 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1508 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1509 status_printf_ln(s
, color
,
1510 _("Cherry-pick currently in progress."));
1512 status_printf_ln(s
, color
,
1513 _("You are currently cherry-picking commit %s."),
1514 repo_find_unique_abbrev(the_repository
, &s
->state
.cherry_pick_head_oid
,
1518 if (has_unmerged(s
))
1519 status_printf_ln(s
, color
,
1520 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1521 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1522 status_printf_ln(s
, color
,
1523 _(" (run \"git cherry-pick --continue\" to continue)"));
1525 status_printf_ln(s
, color
,
1526 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1527 status_printf_ln(s
, color
,
1528 _(" (use \"git cherry-pick --skip\" to skip this patch)"));
1529 status_printf_ln(s
, color
,
1530 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1532 wt_longstatus_print_trailer(s
);
1535 static void show_revert_in_progress(struct wt_status
*s
,
1538 if (is_null_oid(&s
->state
.revert_head_oid
))
1539 status_printf_ln(s
, color
,
1540 _("Revert currently in progress."));
1542 status_printf_ln(s
, color
,
1543 _("You are currently reverting commit %s."),
1544 repo_find_unique_abbrev(the_repository
, &s
->state
.revert_head_oid
,
1547 if (has_unmerged(s
))
1548 status_printf_ln(s
, color
,
1549 _(" (fix conflicts and run \"git revert --continue\")"));
1550 else if (is_null_oid(&s
->state
.revert_head_oid
))
1551 status_printf_ln(s
, color
,
1552 _(" (run \"git revert --continue\" to continue)"));
1554 status_printf_ln(s
, color
,
1555 _(" (all conflicts fixed: run \"git revert --continue\")"));
1556 status_printf_ln(s
, color
,
1557 _(" (use \"git revert --skip\" to skip this patch)"));
1558 status_printf_ln(s
, color
,
1559 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1561 wt_longstatus_print_trailer(s
);
1564 static void show_bisect_in_progress(struct wt_status
*s
,
1567 if (s
->state
.branch
)
1568 status_printf_ln(s
, color
,
1569 _("You are currently bisecting, started from branch '%s'."),
1572 status_printf_ln(s
, color
,
1573 _("You are currently bisecting."));
1575 status_printf_ln(s
, color
,
1576 _(" (use \"git bisect reset\" to get back to the original branch)"));
1577 wt_longstatus_print_trailer(s
);
1580 static void show_sparse_checkout_in_use(struct wt_status
*s
,
1583 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_DISABLED
)
1586 if (s
->state
.sparse_checkout_percentage
== SPARSE_CHECKOUT_SPARSE_INDEX
)
1587 status_printf_ln(s
, color
, _("You are in a sparse checkout."));
1589 status_printf_ln(s
, color
,
1590 _("You are in a sparse checkout with %d%% of tracked files present."),
1591 s
->state
.sparse_checkout_percentage
);
1592 wt_longstatus_print_trailer(s
);
1596 * Extract branch information from rebase/bisect
1598 static char *get_branch(const struct worktree
*wt
, const char *path
)
1600 struct strbuf sb
= STRBUF_INIT
;
1601 struct object_id oid
;
1602 const char *branch_name
;
1604 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1607 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1608 strbuf_setlen(&sb
, sb
.len
- 1);
1611 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1612 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1613 else if (starts_with(sb
.buf
, "refs/"))
1615 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1617 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1618 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1622 return strbuf_detach(&sb
, NULL
);
1625 strbuf_release(&sb
);
1629 struct grab_1st_switch_cbdata
{
1631 struct object_id noid
;
1634 static int grab_1st_switch(struct object_id
*ooid UNUSED
,
1635 struct object_id
*noid
,
1636 const char *email UNUSED
,
1637 timestamp_t timestamp UNUSED
, int tz UNUSED
,
1638 const char *message
, void *cb_data
)
1640 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1641 const char *target
= NULL
, *end
;
1643 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1645 target
= strstr(message
, " to ");
1648 target
+= strlen(" to ");
1649 strbuf_reset(&cb
->buf
);
1650 oidcpy(&cb
->noid
, noid
);
1651 end
= strchrnul(target
, '\n');
1652 strbuf_add(&cb
->buf
, target
, end
- target
);
1653 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1654 /* HEAD is relative. Resolve it to the right reflog entry. */
1655 strbuf_reset(&cb
->buf
);
1656 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1661 static void wt_status_get_detached_from(struct repository
*r
,
1662 struct wt_status_state
*state
)
1664 struct grab_1st_switch_cbdata cb
;
1665 struct commit
*commit
;
1666 struct object_id oid
;
1669 strbuf_init(&cb
.buf
, 0);
1670 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1671 strbuf_release(&cb
.buf
);
1675 if (repo_dwim_ref(r
, cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
,
1677 /* oid is a commit? match without further lookup */
1678 (oideq(&cb
.noid
, &oid
) ||
1679 /* perhaps oid is a tag, try to dereference to a commit */
1680 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1681 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1682 const char *from
= ref
;
1683 if (!skip_prefix(from
, "refs/tags/", &from
))
1684 skip_prefix(from
, "refs/remotes/", &from
);
1685 state
->detached_from
= xstrdup(from
);
1687 state
->detached_from
=
1688 xstrdup(repo_find_unique_abbrev(r
, &cb
.noid
, DEFAULT_ABBREV
));
1689 oidcpy(&state
->detached_oid
, &cb
.noid
);
1690 state
->detached_at
= !repo_get_oid(r
, "HEAD", &oid
) &&
1691 oideq(&oid
, &state
->detached_oid
);
1694 strbuf_release(&cb
.buf
);
1697 int wt_status_check_rebase(const struct worktree
*wt
,
1698 struct wt_status_state
*state
)
1702 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1703 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1704 state
->am_in_progress
= 1;
1705 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1706 state
->am_empty_patch
= 1;
1708 state
->rebase_in_progress
= 1;
1709 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1710 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1712 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1713 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1714 state
->rebase_interactive_in_progress
= 1;
1716 state
->rebase_in_progress
= 1;
1717 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1718 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1724 int wt_status_check_bisect(const struct worktree
*wt
,
1725 struct wt_status_state
*state
)
1729 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1730 state
->bisect_in_progress
= 1;
1731 state
->branch
= get_branch(wt
, "BISECT_START");
1737 static void wt_status_check_sparse_checkout(struct repository
*r
,
1738 struct wt_status_state
*state
)
1740 int skip_worktree
= 0;
1743 if (!core_apply_sparse_checkout
|| r
->index
->cache_nr
== 0) {
1745 * Don't compute percentage of checked out files if we
1746 * aren't in a sparse checkout or would get division by 0.
1748 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_DISABLED
;
1752 if (r
->index
->sparse_index
) {
1753 state
->sparse_checkout_percentage
= SPARSE_CHECKOUT_SPARSE_INDEX
;
1757 for (i
= 0; i
< r
->index
->cache_nr
; i
++) {
1758 struct cache_entry
*ce
= r
->index
->cache
[i
];
1759 if (ce_skip_worktree(ce
))
1763 state
->sparse_checkout_percentage
=
1764 100 - (100 * skip_worktree
)/r
->index
->cache_nr
;
1767 void wt_status_get_state(struct repository
*r
,
1768 struct wt_status_state
*state
,
1769 int get_detached_from
)
1772 struct object_id oid
;
1773 enum replay_action action
;
1775 if (!stat(git_path_merge_head(r
), &st
)) {
1776 wt_status_check_rebase(NULL
, state
);
1777 state
->merge_in_progress
= 1;
1778 } else if (wt_status_check_rebase(NULL
, state
)) {
1780 } else if (refs_ref_exists(get_main_ref_store(r
), "CHERRY_PICK_HEAD") &&
1781 !repo_get_oid(r
, "CHERRY_PICK_HEAD", &oid
)) {
1782 state
->cherry_pick_in_progress
= 1;
1783 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1785 wt_status_check_bisect(NULL
, state
);
1786 if (refs_ref_exists(get_main_ref_store(r
), "REVERT_HEAD") &&
1787 !repo_get_oid(r
, "REVERT_HEAD", &oid
)) {
1788 state
->revert_in_progress
= 1;
1789 oidcpy(&state
->revert_head_oid
, &oid
);
1791 if (!sequencer_get_last_command(r
, &action
)) {
1792 if (action
== REPLAY_PICK
) {
1793 state
->cherry_pick_in_progress
= 1;
1794 oidcpy(&state
->cherry_pick_head_oid
, null_oid());
1796 state
->revert_in_progress
= 1;
1797 oidcpy(&state
->revert_head_oid
, null_oid());
1800 if (get_detached_from
)
1801 wt_status_get_detached_from(r
, state
);
1802 wt_status_check_sparse_checkout(r
, state
);
1805 static void wt_longstatus_print_state(struct wt_status
*s
)
1807 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1808 struct wt_status_state
*state
= &s
->state
;
1810 if (state
->merge_in_progress
) {
1811 if (state
->rebase_interactive_in_progress
) {
1812 show_rebase_information(s
, state_color
);
1815 show_merge_in_progress(s
, state_color
);
1816 } else if (state
->am_in_progress
)
1817 show_am_in_progress(s
, state_color
);
1818 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1819 show_rebase_in_progress(s
, state_color
);
1820 else if (state
->cherry_pick_in_progress
)
1821 show_cherry_pick_in_progress(s
, state_color
);
1822 else if (state
->revert_in_progress
)
1823 show_revert_in_progress(s
, state_color
);
1824 if (state
->bisect_in_progress
)
1825 show_bisect_in_progress(s
, state_color
);
1827 if (state
->sparse_checkout_percentage
!= SPARSE_CHECKOUT_DISABLED
)
1828 show_sparse_checkout_in_use(s
, state_color
);
1831 static void wt_longstatus_print(struct wt_status
*s
)
1833 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1834 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1835 enum fsmonitor_mode fsm_mode
= fsm_settings__get_mode(s
->repo
);
1838 const char *on_what
= _("On branch ");
1839 const char *branch_name
= s
->branch
;
1840 if (!strcmp(branch_name
, "HEAD")) {
1841 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1842 if (s
->state
.rebase_in_progress
||
1843 s
->state
.rebase_interactive_in_progress
) {
1844 if (s
->state
.rebase_interactive_in_progress
)
1845 on_what
= _("interactive rebase in progress; onto ");
1847 on_what
= _("rebase in progress; onto ");
1848 branch_name
= s
->state
.onto
;
1849 } else if (s
->state
.detached_from
) {
1850 branch_name
= s
->state
.detached_from
;
1851 if (s
->state
.detached_at
)
1852 on_what
= _("HEAD detached at ");
1854 on_what
= _("HEAD detached from ");
1857 on_what
= _("Not currently on any branch.");
1860 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1861 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1862 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1863 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1865 wt_longstatus_print_tracking(s
);
1868 wt_longstatus_print_state(s
);
1870 if (s
->is_initial
) {
1871 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1872 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1874 ? _("Initial commit")
1875 : _("No commits yet"));
1876 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1879 wt_longstatus_print_updated(s
);
1880 wt_longstatus_print_unmerged(s
);
1881 wt_longstatus_print_changed(s
);
1882 if (s
->submodule_summary
&&
1883 (!s
->ignore_submodule_arg
||
1884 strcmp(s
->ignore_submodule_arg
, "all"))) {
1885 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1886 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1888 if (s
->show_untracked_files
) {
1889 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1890 if (s
->show_ignored_mode
)
1891 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1892 if (advice_enabled(ADVICE_STATUS_U_OPTION
) && uf_was_slow(s
)) {
1893 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1894 if (fsm_mode
> FSMONITOR_MODE_DISABLED
) {
1895 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1896 _("It took %.2f seconds to enumerate untracked files,\n"
1897 "but the results were cached, and subsequent runs may be faster."),
1898 s
->untracked_in_ms
/ 1000.0);
1900 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1901 _("It took %.2f seconds to enumerate untracked files."),
1902 s
->untracked_in_ms
/ 1000.0);
1904 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1905 _("See 'git help status' for information on how to improve this."));
1906 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1908 } else if (s
->committable
)
1909 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1911 ? _(" (use -u option to show untracked files)") : "");
1914 wt_longstatus_print_verbose(s
);
1915 if (!s
->committable
) {
1917 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1920 else if (s
->workdir_dirty
) {
1922 fprintf(s
->fp
, _("no changes added to commit "
1923 "(use \"git add\" and/or "
1924 "\"git commit -a\")\n"));
1926 fprintf(s
->fp
, _("no changes added to "
1928 } else if (s
->untracked
.nr
) {
1930 fprintf(s
->fp
, _("nothing added to commit but "
1931 "untracked files present (use "
1932 "\"git add\" to track)\n"));
1934 fprintf(s
->fp
, _("nothing added to commit but "
1935 "untracked files present\n"));
1936 } else if (s
->is_initial
) {
1938 fprintf(s
->fp
, _("nothing to commit (create/"
1939 "copy files and use \"git "
1940 "add\" to track)\n"));
1942 fprintf(s
->fp
, _("nothing to commit\n"));
1943 } else if (!s
->show_untracked_files
) {
1945 fprintf(s
->fp
, _("nothing to commit (use -u to "
1946 "show untracked files)\n"));
1948 fprintf(s
->fp
, _("nothing to commit\n"));
1950 fprintf(s
->fp
, _("nothing to commit, working tree "
1954 wt_longstatus_print_stash_summary(s
);
1957 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1958 struct wt_status
*s
)
1960 struct wt_status_change_data
*d
= it
->util
;
1961 const char *how
= "??";
1963 switch (d
->stagemask
) {
1964 case 1: how
= "DD"; break; /* both deleted */
1965 case 2: how
= "AU"; break; /* added by us */
1966 case 3: how
= "UD"; break; /* deleted by them */
1967 case 4: how
= "UA"; break; /* added by them */
1968 case 5: how
= "DU"; break; /* deleted by us */
1969 case 6: how
= "AA"; break; /* both added */
1970 case 7: how
= "UU"; break; /* both modified */
1972 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1973 if (s
->null_termination
) {
1974 fprintf(s
->fp
, " %s%c", it
->string
, 0);
1976 struct strbuf onebuf
= STRBUF_INIT
;
1978 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
1979 fprintf(s
->fp
, " %s\n", one
);
1980 strbuf_release(&onebuf
);
1984 static void wt_shortstatus_status(struct string_list_item
*it
,
1985 struct wt_status
*s
)
1987 struct wt_status_change_data
*d
= it
->util
;
1989 if (d
->index_status
)
1990 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1993 if (d
->worktree_status
)
1994 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1998 if (s
->null_termination
) {
1999 fprintf(s
->fp
, "%s%c", it
->string
, 0);
2000 if (d
->rename_source
)
2001 fprintf(s
->fp
, "%s%c", d
->rename_source
, 0);
2003 struct strbuf onebuf
= STRBUF_INIT
;
2006 if (d
->rename_source
) {
2007 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
,
2008 QUOTE_PATH_QUOTE_SP
);
2009 fprintf(s
->fp
, "%s -> ", one
);
2010 strbuf_release(&onebuf
);
2012 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2013 fprintf(s
->fp
, "%s\n", one
);
2014 strbuf_release(&onebuf
);
2018 static void wt_shortstatus_other(struct string_list_item
*it
,
2019 struct wt_status
*s
, const char *sign
)
2021 if (s
->null_termination
) {
2022 fprintf(s
->fp
, "%s %s%c", sign
, it
->string
, 0);
2024 struct strbuf onebuf
= STRBUF_INIT
;
2026 one
= quote_path(it
->string
, s
->prefix
, &onebuf
, QUOTE_PATH_QUOTE_SP
);
2027 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
2028 fprintf(s
->fp
, " %s\n", one
);
2029 strbuf_release(&onebuf
);
2033 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
2035 struct branch
*branch
;
2036 const char *header_color
= color(WT_STATUS_HEADER
, s
);
2037 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
2038 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
2042 const char *branch_name
;
2043 int num_ours
, num_theirs
, sti
;
2044 int upstream_is_gone
= 0;
2046 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
2050 branch_name
= s
->branch
;
2052 #define LABEL(string) (s->no_gettext ? (string) : _(string))
2055 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
2057 if (!strcmp(s
->branch
, "HEAD")) {
2058 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
2059 LABEL(N_("HEAD (no branch)")));
2063 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
2065 branch
= branch_get(branch_name
);
2067 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
2069 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
2070 0, s
->ahead_behind_flags
);
2075 upstream_is_gone
= 1;
2078 short_base
= shorten_unambiguous_ref(base
, 0);
2079 color_fprintf(s
->fp
, header_color
, "...");
2080 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
2083 if (!upstream_is_gone
&& !sti
)
2086 color_fprintf(s
->fp
, header_color
, " [");
2087 if (upstream_is_gone
) {
2088 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
2089 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
2090 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
2091 } else if (!num_ours
) {
2092 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
2093 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2094 } else if (!num_theirs
) {
2095 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2096 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2098 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
2099 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
2100 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
2101 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
2104 color_fprintf(s
->fp
, header_color
, "]");
2106 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
2109 static void wt_shortstatus_print(struct wt_status
*s
)
2111 struct string_list_item
*it
;
2114 wt_shortstatus_print_tracking(s
);
2116 for_each_string_list_item(it
, &s
->change
) {
2117 struct wt_status_change_data
*d
= it
->util
;
2120 wt_shortstatus_unmerged(it
, s
);
2122 wt_shortstatus_status(it
, s
);
2124 for_each_string_list_item(it
, &s
->untracked
)
2125 wt_shortstatus_other(it
, s
, "??");
2127 for_each_string_list_item(it
, &s
->ignored
)
2128 wt_shortstatus_other(it
, s
, "!!");
2131 static void wt_porcelain_print(struct wt_status
*s
)
2134 s
->relative_paths
= 0;
2137 wt_shortstatus_print(s
);
2141 * Print branch information for porcelain v2 output. These lines
2142 * are printed when the '--branch' parameter is given.
2144 * # branch.oid <commit><eol>
2145 * # branch.head <head><eol>
2146 * [# branch.upstream <upstream><eol>
2147 * [# branch.ab +<ahead> -<behind><eol>]]
2149 * <commit> ::= the current commit hash or the literal
2150 * "(initial)" to indicate an initialized repo
2153 * <head> ::= <branch_name> the current branch name or
2154 * "(detached)" literal when detached head or
2155 * "(unknown)" when something is wrong.
2157 * <upstream> ::= the upstream branch name, when set.
2159 * <ahead> ::= integer ahead value or '?'.
2161 * <behind> ::= integer behind value or '?'.
2163 * The end-of-line is defined by the -z flag.
2165 * <eol> ::= NUL when -z,
2168 * When an upstream is set and present, the 'branch.ab' line will
2169 * be printed with the ahead/behind counts for the branch and the
2170 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
2171 * are different, '?' will be substituted for the actual count.
2173 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
2175 struct branch
*branch
;
2177 const char *branch_name
;
2178 int ab_info
, nr_ahead
, nr_behind
;
2179 char eol
= s
->null_termination
? '\0' : '\n';
2181 fprintf(s
->fp
, "# branch.oid %s%c",
2182 (s
->is_initial
? "(initial)" : oid_to_hex(&s
->oid_commit
)),
2186 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2188 if (!strcmp(s
->branch
, "HEAD")) {
2189 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2191 if (s
->state
.rebase_in_progress
||
2192 s
->state
.rebase_interactive_in_progress
)
2193 branch_name
= s
->state
.onto
;
2194 else if (s
->state
.detached_from
)
2195 branch_name
= s
->state
.detached_from
;
2200 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2202 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2205 /* Lookup stats on the upstream tracking branch, if set. */
2206 branch
= branch_get(branch_name
);
2208 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2209 &base
, 0, s
->ahead_behind_flags
);
2211 base
= shorten_unambiguous_ref(base
, 0);
2212 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2217 if (nr_ahead
|| nr_behind
)
2218 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2219 nr_ahead
, nr_behind
, eol
);
2221 fprintf(s
->fp
, "# branch.ab +? -?%c",
2223 } else if (!ab_info
) {
2225 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2232 * Print the stash count in a porcelain-friendly format
2234 static void wt_porcelain_v2_print_stash(struct wt_status
*s
)
2236 int stash_count
= count_stash_entries();
2237 char eol
= s
->null_termination
? '\0' : '\n';
2239 if (stash_count
> 0)
2240 fprintf(s
->fp
, "# stash %d%c", stash_count
, eol
);
2244 * Convert various submodule status values into a
2245 * fixed-length string of characters in the buffer provided.
2247 static void wt_porcelain_v2_submodule_state(
2248 struct wt_status_change_data
*d
,
2251 if (S_ISGITLINK(d
->mode_head
) ||
2252 S_ISGITLINK(d
->mode_index
) ||
2253 S_ISGITLINK(d
->mode_worktree
)) {
2255 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2256 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2257 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2268 * Fix-up changed entries before we print them.
2270 static void wt_porcelain_v2_fix_up_changed(struct string_list_item
*it
)
2272 struct wt_status_change_data
*d
= it
->util
;
2274 if (!d
->index_status
) {
2276 * This entry is unchanged in the index (relative to the head).
2277 * Therefore, the collect_updated_cb was never called for this
2278 * entry (during the head-vs-index scan) and so the head column
2279 * fields were never set.
2281 * We must have data for the index column (from the
2282 * index-vs-worktree scan (otherwise, this entry should not be
2283 * in the list of changes)).
2285 * Copy index column fields to the head column, so that our
2286 * output looks complete.
2288 assert(d
->mode_head
== 0);
2289 d
->mode_head
= d
->mode_index
;
2290 oidcpy(&d
->oid_head
, &d
->oid_index
);
2293 if (!d
->worktree_status
) {
2295 * This entry is unchanged in the worktree (relative to the index).
2296 * Therefore, the collect_changed_cb was never called for this entry
2297 * (during the index-vs-worktree scan) and so the worktree column
2298 * fields were never set.
2300 * We must have data for the index column (from the head-vs-index
2303 * Copy the index column fields to the worktree column so that
2304 * our output looks complete.
2306 * Note that we only have a mode field in the worktree column
2307 * because the scan code tries really hard to not have to compute it.
2309 assert(d
->mode_worktree
== 0);
2310 d
->mode_worktree
= d
->mode_index
;
2315 * Print porcelain v2 info for tracked entries with changes.
2317 static void wt_porcelain_v2_print_changed_entry(
2318 struct string_list_item
*it
,
2319 struct wt_status
*s
)
2321 struct wt_status_change_data
*d
= it
->util
;
2322 struct strbuf buf
= STRBUF_INIT
;
2323 struct strbuf buf_from
= STRBUF_INIT
;
2324 const char *path
= NULL
;
2325 const char *path_from
= NULL
;
2327 char submodule_token
[5];
2328 char sep_char
, eol_char
;
2330 wt_porcelain_v2_fix_up_changed(it
);
2331 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2333 key
[0] = d
->index_status
? d
->index_status
: '.';
2334 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2337 if (s
->null_termination
) {
2339 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2340 * A single NUL character separates them.
2345 path_from
= d
->rename_source
;
2348 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2349 * The source path is only present when necessary.
2350 * A single TAB separates them (because paths can contain spaces
2351 * which are not escaped and C-quoting does escape TAB characters).
2355 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2356 if (d
->rename_source
)
2357 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
, 0);
2361 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2362 key
, submodule_token
,
2363 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2364 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2365 d
->rename_status
, d
->rename_score
,
2366 path
, sep_char
, path_from
, eol_char
);
2368 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2369 key
, submodule_token
,
2370 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2371 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2374 strbuf_release(&buf
);
2375 strbuf_release(&buf_from
);
2379 * Print porcelain v2 status info for unmerged entries.
2381 static void wt_porcelain_v2_print_unmerged_entry(
2382 struct string_list_item
*it
,
2383 struct wt_status
*s
)
2385 struct wt_status_change_data
*d
= it
->util
;
2386 struct index_state
*istate
= s
->repo
->index
;
2387 const struct cache_entry
*ce
;
2388 struct strbuf buf_index
= STRBUF_INIT
;
2389 const char *path_index
= NULL
;
2390 int pos
, stage
, sum
;
2393 struct object_id oid
;
2396 char submodule_token
[5];
2397 char unmerged_prefix
= 'u';
2398 char eol_char
= s
->null_termination
? '\0' : '\n';
2400 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2402 switch (d
->stagemask
) {
2403 case 1: key
= "DD"; break; /* both deleted */
2404 case 2: key
= "AU"; break; /* added by us */
2405 case 3: key
= "UD"; break; /* deleted by them */
2406 case 4: key
= "UA"; break; /* added by them */
2407 case 5: key
= "DU"; break; /* deleted by us */
2408 case 6: key
= "AA"; break; /* both added */
2409 case 7: key
= "UU"; break; /* both modified */
2411 BUG("unhandled unmerged status %x", d
->stagemask
);
2415 * Disregard d.aux.porcelain_v2 data that we accumulated
2416 * for the head and index columns during the scans and
2417 * replace with the actual stage data.
2419 * Note that this is a last-one-wins for each the individual
2420 * stage [123] columns in the event of multiple cache entries
2423 memset(stages
, 0, sizeof(stages
));
2425 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2428 while (pos
< istate
->cache_nr
) {
2429 ce
= istate
->cache
[pos
++];
2430 stage
= ce_stage(ce
);
2431 if (strcmp(ce
->name
, it
->string
) || !stage
)
2433 stages
[stage
- 1].mode
= ce
->ce_mode
;
2434 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2435 sum
|= (1 << (stage
- 1));
2437 if (sum
!= d
->stagemask
)
2438 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2440 if (s
->null_termination
)
2441 path_index
= it
->string
;
2443 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
, 0);
2445 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2446 unmerged_prefix
, key
, submodule_token
,
2447 stages
[0].mode
, /* stage 1 */
2448 stages
[1].mode
, /* stage 2 */
2449 stages
[2].mode
, /* stage 3 */
2451 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2452 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2453 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2457 strbuf_release(&buf_index
);
2461 * Print porcelain V2 status info for untracked and ignored entries.
2463 static void wt_porcelain_v2_print_other(
2464 struct string_list_item
*it
,
2465 struct wt_status
*s
,
2468 struct strbuf buf
= STRBUF_INIT
;
2472 if (s
->null_termination
) {
2476 path
= quote_path(it
->string
, s
->prefix
, &buf
, 0);
2480 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2482 strbuf_release(&buf
);
2486 * Print porcelain V2 status.
2489 * [<v2_changed_items>]*
2490 * [<v2_unmerged_items>]*
2491 * [<v2_untracked_items>]*
2492 * [<v2_ignored_items>]*
2495 static void wt_porcelain_v2_print(struct wt_status
*s
)
2497 struct wt_status_change_data
*d
;
2498 struct string_list_item
*it
;
2502 wt_porcelain_v2_print_tracking(s
);
2505 wt_porcelain_v2_print_stash(s
);
2507 for (i
= 0; i
< s
->change
.nr
; i
++) {
2508 it
= &(s
->change
.items
[i
]);
2511 wt_porcelain_v2_print_changed_entry(it
, s
);
2514 for (i
= 0; i
< s
->change
.nr
; i
++) {
2515 it
= &(s
->change
.items
[i
]);
2518 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2521 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2522 it
= &(s
->untracked
.items
[i
]);
2523 wt_porcelain_v2_print_other(it
, s
, '?');
2526 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2527 it
= &(s
->ignored
.items
[i
]);
2528 wt_porcelain_v2_print_other(it
, s
, '!');
2532 void wt_status_print(struct wt_status
*s
)
2534 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2535 trace2_data_intmax("status", s
->repo
, "count/untracked",
2537 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2539 trace2_region_enter("status", "print", s
->repo
);
2541 switch (s
->status_format
) {
2542 case STATUS_FORMAT_SHORT
:
2543 wt_shortstatus_print(s
);
2545 case STATUS_FORMAT_PORCELAIN
:
2546 wt_porcelain_print(s
);
2548 case STATUS_FORMAT_PORCELAIN_V2
:
2549 wt_porcelain_v2_print(s
);
2551 case STATUS_FORMAT_UNSPECIFIED
:
2552 BUG("finalize_deferred_config() should have been called");
2554 case STATUS_FORMAT_NONE
:
2555 case STATUS_FORMAT_LONG
:
2556 wt_longstatus_print(s
);
2560 trace2_region_leave("status", "print", s
->repo
);
2564 * Returns 1 if there are unstaged changes, 0 otherwise.
2566 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2568 struct rev_info rev_info
;
2571 repo_init_revisions(r
, &rev_info
, NULL
);
2572 if (ignore_submodules
) {
2573 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2574 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2576 rev_info
.diffopt
.flags
.quick
= 1;
2577 diff_setup_done(&rev_info
.diffopt
);
2578 result
= run_diff_files(&rev_info
, 0);
2579 result
= diff_result_code(&rev_info
.diffopt
, result
);
2580 release_revisions(&rev_info
);
2585 * Returns 1 if there are uncommitted changes, 0 otherwise.
2587 int has_uncommitted_changes(struct repository
*r
,
2588 int ignore_submodules
)
2590 struct rev_info rev_info
;
2593 if (is_index_unborn(r
->index
))
2596 repo_init_revisions(r
, &rev_info
, NULL
);
2597 if (ignore_submodules
)
2598 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2599 rev_info
.diffopt
.flags
.quick
= 1;
2601 add_head_to_pending(&rev_info
);
2602 if (!rev_info
.pending
.nr
) {
2604 * We have no head (or it's corrupt); use the empty tree,
2605 * which will complain if the index is non-empty.
2607 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2608 add_pending_object(&rev_info
, &tree
->object
, "");
2611 diff_setup_done(&rev_info
.diffopt
);
2612 result
= run_diff_index(&rev_info
, 1);
2613 result
= diff_result_code(&rev_info
.diffopt
, result
);
2614 release_revisions(&rev_info
);
2619 * If the work tree has unstaged or uncommitted changes, dies with the
2620 * appropriate message.
2622 int require_clean_work_tree(struct repository
*r
,
2625 int ignore_submodules
,
2628 struct lock_file lock_file
= LOCK_INIT
;
2631 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2632 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2634 repo_update_index_if_able(r
, &lock_file
);
2635 rollback_lock_file(&lock_file
);
2637 if (has_unstaged_changes(r
, ignore_submodules
)) {
2638 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2639 error(_("cannot %s: You have unstaged changes."), _(action
));
2643 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2645 error(_("additionally, your index contains uncommitted changes."));
2647 error(_("cannot %s: Your index contains uncommitted changes."),