10 #include "run-command.h"
11 #include "argv-array.h"
14 #include "submodule.h"
20 #include "sequencer.h"
22 static const char cut_line
[] =
23 "------------------------ >8 ------------------------\n";
25 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
26 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
27 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
28 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
29 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
30 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
31 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
32 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
33 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
34 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
37 static const char *color(int slot
, struct wt_status
*s
)
40 if (want_color(s
->use_color
))
41 c
= s
->color_palette
[slot
];
42 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
43 c
= s
->color_palette
[WT_STATUS_HEADER
];
47 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
48 const char *fmt
, va_list ap
, const char *trail
)
50 struct strbuf sb
= STRBUF_INIT
;
51 struct strbuf linebuf
= STRBUF_INIT
;
52 const char *line
, *eol
;
54 strbuf_vaddf(&sb
, fmt
, ap
);
56 if (s
->display_comment_prefix
) {
57 strbuf_addch(&sb
, comment_line_char
);
59 strbuf_addch(&sb
, ' ');
61 color_print_strbuf(s
->fp
, color
, &sb
);
63 fprintf(s
->fp
, "%s", trail
);
67 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
68 eol
= strchr(line
, '\n');
70 strbuf_reset(&linebuf
);
71 if (at_bol
&& s
->display_comment_prefix
) {
72 strbuf_addch(&linebuf
, comment_line_char
);
73 if (*line
!= '\n' && *line
!= '\t')
74 strbuf_addch(&linebuf
, ' ');
77 strbuf_add(&linebuf
, line
, eol
- line
);
79 strbuf_addstr(&linebuf
, line
);
80 color_print_strbuf(s
->fp
, color
, &linebuf
);
88 fprintf(s
->fp
, "%s", trail
);
89 strbuf_release(&linebuf
);
93 void status_printf_ln(struct wt_status
*s
, const char *color
,
99 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
103 void status_printf(struct wt_status
*s
, const char *color
,
104 const char *fmt
, ...)
109 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
113 static void status_printf_more(struct wt_status
*s
, const char *color
,
114 const char *fmt
, ...)
119 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
123 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
125 memset(s
, 0, sizeof(*s
));
127 memcpy(s
->color_palette
, default_wt_status_colors
,
128 sizeof(default_wt_status_colors
));
129 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
131 s
->relative_paths
= 1;
132 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
133 s
->reference
= "HEAD";
135 s
->index_file
= get_index_file();
136 s
->change
.strdup_strings
= 1;
137 s
->untracked
.strdup_strings
= 1;
138 s
->ignored
.strdup_strings
= 1;
139 s
->show_branch
= -1; /* unspecified */
141 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
142 s
->display_comment_prefix
= 0;
143 s
->detect_rename
= -1;
144 s
->rename_score
= -1;
145 s
->rename_limit
= -1;
148 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
151 int del_mod_conflict
= 0;
152 int both_deleted
= 0;
154 const char *c
= color(WT_STATUS_HEADER
, s
);
156 status_printf_ln(s
, c
, _("Unmerged paths:"));
158 for (i
= 0; i
< s
->change
.nr
; i
++) {
159 struct string_list_item
*it
= &(s
->change
.items
[i
]);
160 struct wt_status_change_data
*d
= it
->util
;
162 switch (d
->stagemask
) {
170 del_mod_conflict
= 1;
180 if (s
->whence
!= FROM_COMMIT
)
182 else if (!s
->is_initial
)
183 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
185 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
188 if (!del_mod_conflict
)
189 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
191 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
192 } else if (!del_mod_conflict
&& !not_deleted
) {
193 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
195 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
197 status_printf_ln(s
, c
, "%s", "");
200 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
202 const char *c
= color(WT_STATUS_HEADER
, s
);
204 status_printf_ln(s
, c
, _("Changes to be committed:"));
207 if (s
->whence
!= FROM_COMMIT
)
208 ; /* NEEDSWORK: use "git reset --unresolve"??? */
209 else if (!s
->is_initial
)
210 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
212 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
213 status_printf_ln(s
, c
, "%s", "");
216 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
218 int has_dirty_submodules
)
220 const char *c
= color(WT_STATUS_HEADER
, s
);
222 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
226 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
228 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
229 status_printf_ln(s
, c
, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
230 if (has_dirty_submodules
)
231 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
232 status_printf_ln(s
, c
, "%s", "");
235 static void wt_longstatus_print_other_header(struct wt_status
*s
,
239 const char *c
= color(WT_STATUS_HEADER
, s
);
240 status_printf_ln(s
, c
, "%s:", what
);
243 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
244 status_printf_ln(s
, c
, "%s", "");
247 static void wt_longstatus_print_trailer(struct wt_status
*s
)
249 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
252 #define quote_path quote_path_relative
254 static const char *wt_status_unmerged_status_string(int stagemask
)
258 return _("both deleted:");
260 return _("added by us:");
262 return _("deleted by them:");
264 return _("added by them:");
266 return _("deleted by us:");
268 return _("both added:");
270 return _("both modified:");
272 BUG("unhandled unmerged status %x", stagemask
);
276 static const char *wt_status_diff_status_string(int status
)
279 case DIFF_STATUS_ADDED
:
280 return _("new file:");
281 case DIFF_STATUS_COPIED
:
283 case DIFF_STATUS_DELETED
:
284 return _("deleted:");
285 case DIFF_STATUS_MODIFIED
:
286 return _("modified:");
287 case DIFF_STATUS_RENAMED
:
288 return _("renamed:");
289 case DIFF_STATUS_TYPE_CHANGED
:
290 return _("typechange:");
291 case DIFF_STATUS_UNKNOWN
:
292 return _("unknown:");
293 case DIFF_STATUS_UNMERGED
:
294 return _("unmerged:");
300 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
304 for (i
= minval
; i
<= maxval
; i
++) {
305 const char *s
= label(i
);
306 int len
= s
? utf8_strwidth(s
) : 0;
313 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
314 struct string_list_item
*it
)
316 const char *c
= color(WT_STATUS_UNMERGED
, s
);
317 struct wt_status_change_data
*d
= it
->util
;
318 struct strbuf onebuf
= STRBUF_INIT
;
319 static char *padding
;
320 static int label_width
;
321 const char *one
, *how
;
325 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
326 label_width
+= strlen(" ");
327 padding
= xmallocz(label_width
);
328 memset(padding
, ' ', label_width
);
331 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
332 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
334 how
= wt_status_unmerged_status_string(d
->stagemask
);
335 len
= label_width
- utf8_strwidth(how
);
336 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
337 strbuf_release(&onebuf
);
340 static void wt_longstatus_print_change_data(struct wt_status
*s
,
342 struct string_list_item
*it
)
344 struct wt_status_change_data
*d
= it
->util
;
345 const char *c
= color(change_type
, s
);
349 const char *one
, *two
;
350 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
351 struct strbuf extra
= STRBUF_INIT
;
352 static char *padding
;
353 static int label_width
;
358 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
359 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
360 label_width
+= strlen(" ");
361 padding
= xmallocz(label_width
);
362 memset(padding
, ' ', label_width
);
365 one_name
= two_name
= it
->string
;
366 switch (change_type
) {
367 case WT_STATUS_UPDATED
:
368 status
= d
->index_status
;
370 case WT_STATUS_CHANGED
:
371 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
372 strbuf_addstr(&extra
, " (");
373 if (d
->new_submodule_commits
)
374 strbuf_addstr(&extra
, _("new commits, "));
375 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
376 strbuf_addstr(&extra
, _("modified content, "));
377 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
378 strbuf_addstr(&extra
, _("untracked content, "));
379 strbuf_setlen(&extra
, extra
.len
- 2);
380 strbuf_addch(&extra
, ')');
382 status
= d
->worktree_status
;
385 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
390 * Only pick up the rename it's relevant. If the rename is for
391 * the changed section and we're printing the updated section,
394 if (d
->rename_status
== status
)
395 one_name
= d
->rename_source
;
397 one
= quote_path(one_name
, s
->prefix
, &onebuf
);
398 two
= quote_path(two_name
, s
->prefix
, &twobuf
);
400 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
401 what
= wt_status_diff_status_string(status
);
403 BUG("unhandled diff status %c", status
);
404 len
= label_width
- utf8_strwidth(what
);
406 if (one_name
!= two_name
)
407 status_printf_more(s
, c
, "%s%.*s%s -> %s",
408 what
, len
, padding
, one
, two
);
410 status_printf_more(s
, c
, "%s%.*s%s",
411 what
, len
, padding
, one
);
413 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
414 strbuf_release(&extra
);
416 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
417 strbuf_release(&onebuf
);
418 strbuf_release(&twobuf
);
421 static char short_submodule_status(struct wt_status_change_data
*d
)
423 if (d
->new_submodule_commits
)
425 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
427 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
429 return d
->worktree_status
;
432 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
433 struct diff_options
*options
,
436 struct wt_status
*s
= data
;
441 s
->workdir_dirty
= 1;
442 for (i
= 0; i
< q
->nr
; i
++) {
443 struct diff_filepair
*p
;
444 struct string_list_item
*it
;
445 struct wt_status_change_data
*d
;
448 it
= string_list_insert(&s
->change
, p
->two
->path
);
451 d
= xcalloc(1, sizeof(*d
));
454 if (!d
->worktree_status
)
455 d
->worktree_status
= p
->status
;
456 if (S_ISGITLINK(p
->two
->mode
)) {
457 d
->dirty_submodule
= p
->two
->dirty_submodule
;
458 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
460 if (s
->status_format
== STATUS_FORMAT_SHORT
)
461 d
->worktree_status
= short_submodule_status(d
);
465 case DIFF_STATUS_ADDED
:
466 d
->mode_worktree
= p
->two
->mode
;
469 case DIFF_STATUS_DELETED
:
470 d
->mode_index
= p
->one
->mode
;
471 oidcpy(&d
->oid_index
, &p
->one
->oid
);
472 /* mode_worktree is zero for a delete. */
475 case DIFF_STATUS_COPIED
:
476 case DIFF_STATUS_RENAMED
:
477 if (d
->rename_status
)
478 BUG("multiple renames on the same target? how?");
479 d
->rename_source
= xstrdup(p
->one
->path
);
480 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
481 d
->rename_status
= p
->status
;
483 case DIFF_STATUS_MODIFIED
:
484 case DIFF_STATUS_TYPE_CHANGED
:
485 case DIFF_STATUS_UNMERGED
:
486 d
->mode_index
= p
->one
->mode
;
487 d
->mode_worktree
= p
->two
->mode
;
488 oidcpy(&d
->oid_index
, &p
->one
->oid
);
492 BUG("unhandled diff-files status '%c'", p
->status
);
499 static int unmerged_mask(struct index_state
*istate
, const char *path
)
502 const struct cache_entry
*ce
;
504 pos
= index_name_pos(istate
, path
, strlen(path
));
510 while (pos
< istate
->cache_nr
) {
511 ce
= istate
->cache
[pos
++];
512 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
514 mask
|= (1 << (ce_stage(ce
) - 1));
519 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
520 struct diff_options
*options
,
523 struct wt_status
*s
= data
;
526 for (i
= 0; i
< q
->nr
; i
++) {
527 struct diff_filepair
*p
;
528 struct string_list_item
*it
;
529 struct wt_status_change_data
*d
;
532 it
= string_list_insert(&s
->change
, p
->two
->path
);
535 d
= xcalloc(1, sizeof(*d
));
538 if (!d
->index_status
)
539 d
->index_status
= p
->status
;
541 case DIFF_STATUS_ADDED
:
542 /* Leave {mode,oid}_head zero for an add. */
543 d
->mode_index
= p
->two
->mode
;
544 oidcpy(&d
->oid_index
, &p
->two
->oid
);
547 case DIFF_STATUS_DELETED
:
548 d
->mode_head
= p
->one
->mode
;
549 oidcpy(&d
->oid_head
, &p
->one
->oid
);
551 /* Leave {mode,oid}_index zero for a delete. */
554 case DIFF_STATUS_COPIED
:
555 case DIFF_STATUS_RENAMED
:
556 if (d
->rename_status
)
557 BUG("multiple renames on the same target? how?");
558 d
->rename_source
= xstrdup(p
->one
->path
);
559 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
560 d
->rename_status
= p
->status
;
562 case DIFF_STATUS_MODIFIED
:
563 case DIFF_STATUS_TYPE_CHANGED
:
564 d
->mode_head
= p
->one
->mode
;
565 d
->mode_index
= p
->two
->mode
;
566 oidcpy(&d
->oid_head
, &p
->one
->oid
);
567 oidcpy(&d
->oid_index
, &p
->two
->oid
);
570 case DIFF_STATUS_UNMERGED
:
571 d
->stagemask
= unmerged_mask(s
->repo
->index
,
574 * Don't bother setting {mode,oid}_{head,index} since the print
575 * code will output the stage values directly and not use the
576 * values in these fields.
581 BUG("unhandled diff-index status '%c'", p
->status
);
587 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
591 repo_init_revisions(s
->repo
, &rev
, NULL
);
592 setup_revisions(0, NULL
, &rev
, NULL
);
593 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
594 rev
.diffopt
.flags
.dirty_submodules
= 1;
595 rev
.diffopt
.ita_invisible_in_index
= 1;
596 if (!s
->show_untracked_files
)
597 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
598 if (s
->ignore_submodule_arg
) {
599 rev
.diffopt
.flags
.override_submodule_config
= 1;
600 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
602 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
603 rev
.diffopt
.format_callback_data
= s
;
604 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
605 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
606 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
607 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
608 run_diff_files(&rev
, 0);
611 static void wt_status_collect_changes_index(struct wt_status
*s
)
614 struct setup_revision_opt opt
;
616 repo_init_revisions(s
->repo
, &rev
, NULL
);
617 memset(&opt
, 0, sizeof(opt
));
618 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
619 setup_revisions(0, NULL
, &rev
, &opt
);
621 rev
.diffopt
.flags
.override_submodule_config
= 1;
622 rev
.diffopt
.ita_invisible_in_index
= 1;
623 if (s
->ignore_submodule_arg
) {
624 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
627 * Unless the user did explicitly request a submodule ignore
628 * mode by passing a command line option we do not ignore any
629 * changed submodule SHA-1s when comparing index and HEAD, no
630 * matter what is configured. Otherwise the user won't be
631 * shown any submodules she manually added (and which are
632 * staged to be committed), which would be really confusing.
634 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
637 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
638 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
639 rev
.diffopt
.format_callback_data
= s
;
640 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
641 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
642 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
643 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
644 run_diff_index(&rev
, 1);
647 static void wt_status_collect_changes_initial(struct wt_status
*s
)
649 struct index_state
*istate
= s
->repo
->index
;
652 for (i
= 0; i
< istate
->cache_nr
; i
++) {
653 struct string_list_item
*it
;
654 struct wt_status_change_data
*d
;
655 const struct cache_entry
*ce
= istate
->cache
[i
];
657 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
659 if (ce_intent_to_add(ce
))
661 it
= string_list_insert(&s
->change
, ce
->name
);
664 d
= xcalloc(1, sizeof(*d
));
668 d
->index_status
= DIFF_STATUS_UNMERGED
;
669 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
671 * Don't bother setting {mode,oid}_{head,index} since the print
672 * code will output the stage values directly and not use the
673 * values in these fields.
677 d
->index_status
= DIFF_STATUS_ADDED
;
678 /* Leave {mode,oid}_head zero for adds. */
679 d
->mode_index
= ce
->ce_mode
;
680 oidcpy(&d
->oid_index
, &ce
->oid
);
686 static void wt_status_collect_untracked(struct wt_status
*s
)
689 struct dir_struct dir
;
690 uint64_t t_begin
= getnanotime();
691 struct index_state
*istate
= s
->repo
->index
;
693 if (!s
->show_untracked_files
)
696 memset(&dir
, 0, sizeof(dir
));
697 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
699 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
700 if (s
->show_ignored_mode
) {
701 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
703 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
704 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
706 dir
.untracked
= istate
->untracked
;
709 setup_standard_excludes(&dir
);
711 fill_directory(&dir
, istate
, &s
->pathspec
);
713 for (i
= 0; i
< dir
.nr
; i
++) {
714 struct dir_entry
*ent
= dir
.entries
[i
];
715 if (index_name_is_other(istate
, ent
->name
, ent
->len
) &&
716 dir_path_match(istate
, ent
, &s
->pathspec
, 0, NULL
))
717 string_list_insert(&s
->untracked
, ent
->name
);
721 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
722 struct dir_entry
*ent
= dir
.ignored
[i
];
723 if (index_name_is_other(istate
, ent
->name
, ent
->len
) &&
724 dir_path_match(istate
, ent
, &s
->pathspec
, 0, NULL
))
725 string_list_insert(&s
->ignored
, ent
->name
);
731 clear_directory(&dir
);
733 if (advice_status_u_option
)
734 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
737 static int has_unmerged(struct wt_status
*s
)
741 for (i
= 0; i
< s
->change
.nr
; i
++) {
742 struct wt_status_change_data
*d
;
743 d
= s
->change
.items
[i
].util
;
750 void wt_status_collect(struct wt_status
*s
)
752 trace2_region_enter("status", "worktrees", s
->repo
);
753 wt_status_collect_changes_worktree(s
);
754 trace2_region_leave("status", "worktrees", s
->repo
);
757 trace2_region_enter("status", "initial", s
->repo
);
758 wt_status_collect_changes_initial(s
);
759 trace2_region_leave("status", "initial", s
->repo
);
761 trace2_region_enter("status", "index", s
->repo
);
762 wt_status_collect_changes_index(s
);
763 trace2_region_leave("status", "index", s
->repo
);
766 trace2_region_enter("status", "untracked", s
->repo
);
767 wt_status_collect_untracked(s
);
768 trace2_region_leave("status", "untracked", s
->repo
);
770 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
771 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
775 void wt_status_collect_free_buffers(struct wt_status
*s
)
777 free(s
->state
.branch
);
779 free(s
->state
.detached_from
);
782 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
784 int shown_header
= 0;
787 for (i
= 0; i
< s
->change
.nr
; i
++) {
788 struct wt_status_change_data
*d
;
789 struct string_list_item
*it
;
790 it
= &(s
->change
.items
[i
]);
795 wt_longstatus_print_unmerged_header(s
);
798 wt_longstatus_print_unmerged_data(s
, it
);
801 wt_longstatus_print_trailer(s
);
805 static void wt_longstatus_print_updated(struct wt_status
*s
)
807 int shown_header
= 0;
810 for (i
= 0; i
< s
->change
.nr
; i
++) {
811 struct wt_status_change_data
*d
;
812 struct string_list_item
*it
;
813 it
= &(s
->change
.items
[i
]);
815 if (!d
->index_status
||
816 d
->index_status
== DIFF_STATUS_UNMERGED
)
819 wt_longstatus_print_cached_header(s
);
822 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
825 wt_longstatus_print_trailer(s
);
831 * 1 : some change but no delete
833 static int wt_status_check_worktree_changes(struct wt_status
*s
,
834 int *dirty_submodules
)
839 *dirty_submodules
= 0;
841 for (i
= 0; i
< s
->change
.nr
; i
++) {
842 struct wt_status_change_data
*d
;
843 d
= s
->change
.items
[i
].util
;
844 if (!d
->worktree_status
||
845 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
849 if (d
->dirty_submodule
)
850 *dirty_submodules
= 1;
851 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
857 static void wt_longstatus_print_changed(struct wt_status
*s
)
859 int i
, dirty_submodules
;
860 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
862 if (!worktree_changes
)
865 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
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
]);
872 if (!d
->worktree_status
||
873 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
875 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
877 wt_longstatus_print_trailer(s
);
880 static int stash_count_refs(struct object_id
*ooid
, struct object_id
*noid
,
881 const char *email
, timestamp_t timestamp
, int tz
,
882 const char *message
, void *cb_data
)
889 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
893 for_each_reflog_ent("refs/stash", stash_count_refs
, &stash_count
);
895 status_printf_ln(s
, GIT_COLOR_NORMAL
,
896 Q_("Your stash currently has %d entry",
897 "Your stash currently has %d entries", stash_count
),
901 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
903 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
904 struct strbuf cmd_stdout
= STRBUF_INIT
;
905 struct strbuf summary
= STRBUF_INIT
;
906 char *summary_content
;
908 argv_array_pushf(&sm_summary
.env_array
, "GIT_INDEX_FILE=%s",
911 argv_array_push(&sm_summary
.args
, "submodule");
912 argv_array_push(&sm_summary
.args
, "summary");
913 argv_array_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
914 argv_array_push(&sm_summary
.args
, "--for-status");
915 argv_array_push(&sm_summary
.args
, "--summary-limit");
916 argv_array_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
918 argv_array_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
920 sm_summary
.git_cmd
= 1;
921 sm_summary
.no_stdin
= 1;
923 capture_command(&sm_summary
, &cmd_stdout
, 1024);
925 /* prepend header, only if there's an actual output */
926 if (cmd_stdout
.len
) {
928 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
930 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
931 strbuf_addstr(&summary
, "\n\n");
933 strbuf_addbuf(&summary
, &cmd_stdout
);
934 strbuf_release(&cmd_stdout
);
936 if (s
->display_comment_prefix
) {
938 summary_content
= strbuf_detach(&summary
, &len
);
939 strbuf_add_commented_lines(&summary
, summary_content
, len
);
940 free(summary_content
);
943 fputs(summary
.buf
, s
->fp
);
944 strbuf_release(&summary
);
947 static void wt_longstatus_print_other(struct wt_status
*s
,
948 struct string_list
*l
,
953 struct strbuf buf
= STRBUF_INIT
;
954 static struct string_list output
= STRING_LIST_INIT_DUP
;
955 struct column_options copts
;
960 wt_longstatus_print_other_header(s
, what
, how
);
962 for (i
= 0; i
< l
->nr
; i
++) {
963 struct string_list_item
*it
;
966 path
= quote_path(it
->string
, s
->prefix
, &buf
);
967 if (column_active(s
->colopts
)) {
968 string_list_append(&output
, path
);
971 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
972 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
976 strbuf_release(&buf
);
977 if (!column_active(s
->colopts
))
980 strbuf_addf(&buf
, "%s%s\t%s",
981 color(WT_STATUS_HEADER
, s
),
982 s
->display_comment_prefix
? "#" : "",
983 color(WT_STATUS_UNTRACKED
, s
));
984 memset(&copts
, 0, sizeof(copts
));
986 copts
.indent
= buf
.buf
;
987 if (want_color(s
->use_color
))
988 copts
.nl
= GIT_COLOR_RESET
"\n";
989 print_columns(&output
, s
->colopts
, &copts
);
990 string_list_clear(&output
, 0);
991 strbuf_release(&buf
);
993 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
996 size_t wt_status_locate_end(const char *s
, size_t len
)
999 struct strbuf pattern
= STRBUF_INIT
;
1001 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
1002 if (starts_with(s
, pattern
.buf
+ 1))
1004 else if ((p
= strstr(s
, pattern
.buf
)))
1006 strbuf_release(&pattern
);
1010 void wt_status_append_cut_line(struct strbuf
*buf
)
1012 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1014 strbuf_commented_addf(buf
, "%s", cut_line
);
1015 strbuf_add_commented_lines(buf
, explanation
, strlen(explanation
));
1018 void wt_status_add_cut_line(FILE *fp
)
1020 struct strbuf buf
= STRBUF_INIT
;
1022 wt_status_append_cut_line(&buf
);
1024 strbuf_release(&buf
);
1027 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1029 struct rev_info rev
;
1030 struct setup_revision_opt opt
;
1031 int dirty_submodules
;
1032 const char *c
= color(WT_STATUS_HEADER
, s
);
1034 repo_init_revisions(s
->repo
, &rev
, NULL
);
1035 rev
.diffopt
.flags
.allow_textconv
= 1;
1036 rev
.diffopt
.ita_invisible_in_index
= 1;
1038 memset(&opt
, 0, sizeof(opt
));
1039 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1040 setup_revisions(0, NULL
, &rev
, &opt
);
1042 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1043 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1044 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1045 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1046 rev
.diffopt
.file
= s
->fp
;
1047 rev
.diffopt
.close_file
= 0;
1049 * If we're not going to stdout, then we definitely don't
1050 * want color, since we are going to the commit message
1051 * file (and even the "auto" setting won't work, since it
1052 * will have checked isatty on stdout). But we then do want
1053 * to insert the scissor line here to reliably remove the
1054 * diff before committing.
1056 if (s
->fp
!= stdout
) {
1057 rev
.diffopt
.use_color
= 0;
1058 wt_status_add_cut_line(s
->fp
);
1060 if (s
->verbose
> 1 && s
->committable
) {
1061 /* print_updated() printed a header, so do we */
1062 if (s
->fp
!= stdout
)
1063 wt_longstatus_print_trailer(s
);
1064 status_printf_ln(s
, c
, _("Changes to be committed:"));
1065 rev
.diffopt
.a_prefix
= "c/";
1066 rev
.diffopt
.b_prefix
= "i/";
1067 } /* else use prefix as per user config */
1068 run_diff_index(&rev
, 1);
1069 if (s
->verbose
> 1 &&
1070 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1071 status_printf_ln(s
, c
,
1072 "--------------------------------------------------");
1073 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1075 rev
.diffopt
.a_prefix
= "i/";
1076 rev
.diffopt
.b_prefix
= "w/";
1077 run_diff_files(&rev
, 0);
1081 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1083 struct strbuf sb
= STRBUF_INIT
;
1084 const char *cp
, *ep
, *branch_name
;
1085 struct branch
*branch
;
1086 char comment_line_string
[3];
1089 assert(s
->branch
&& !s
->is_initial
);
1090 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1092 branch
= branch_get(branch_name
);
1093 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1097 if (s
->display_comment_prefix
) {
1098 comment_line_string
[i
++] = comment_line_char
;
1099 comment_line_string
[i
++] = ' ';
1101 comment_line_string
[i
] = '\0';
1103 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1104 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1105 "%s%.*s", comment_line_string
,
1106 (int)(ep
- cp
), cp
);
1107 if (s
->display_comment_prefix
)
1108 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1112 strbuf_release(&sb
);
1115 static void show_merge_in_progress(struct wt_status
*s
,
1118 if (has_unmerged(s
)) {
1119 status_printf_ln(s
, color
, _("You have unmerged paths."));
1121 status_printf_ln(s
, color
,
1122 _(" (fix conflicts and run \"git commit\")"));
1123 status_printf_ln(s
, color
,
1124 _(" (use \"git merge --abort\" to abort the merge)"));
1127 status_printf_ln(s
, color
,
1128 _("All conflicts fixed but you are still merging."));
1130 status_printf_ln(s
, color
,
1131 _(" (use \"git commit\" to conclude merge)"));
1133 wt_longstatus_print_trailer(s
);
1136 static void show_am_in_progress(struct wt_status
*s
,
1139 status_printf_ln(s
, color
,
1140 _("You are in the middle of an am session."));
1141 if (s
->state
.am_empty_patch
)
1142 status_printf_ln(s
, color
,
1143 _("The current patch is empty."));
1145 if (!s
->state
.am_empty_patch
)
1146 status_printf_ln(s
, color
,
1147 _(" (fix conflicts and then run \"git am --continue\")"));
1148 status_printf_ln(s
, color
,
1149 _(" (use \"git am --skip\" to skip this patch)"));
1150 status_printf_ln(s
, color
,
1151 _(" (use \"git am --abort\" to restore the original branch)"));
1153 wt_longstatus_print_trailer(s
);
1156 static char *read_line_from_git_path(const char *filename
)
1158 struct strbuf buf
= STRBUF_INIT
;
1159 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1162 strbuf_release(&buf
);
1165 strbuf_getline_lf(&buf
, fp
);
1167 return strbuf_detach(&buf
, NULL
);
1169 strbuf_release(&buf
);
1174 static int split_commit_in_progress(struct wt_status
*s
)
1176 int split_in_progress
= 0;
1177 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1179 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1180 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1183 head
= read_line_from_git_path("HEAD");
1184 orig_head
= read_line_from_git_path("ORIG_HEAD");
1185 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1186 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1188 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1189 ; /* fall through, no split in progress */
1190 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1191 split_in_progress
= !!strcmp(head
, rebase_amend
);
1192 else if (strcmp(orig_head
, rebase_orig_head
))
1193 split_in_progress
= 1;
1198 free(rebase_orig_head
);
1200 return split_in_progress
;
1205 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1207 * "pick d6a2f03 some message"
1209 * The function assumes that the line does not contain useless spaces
1210 * before or after the command.
1212 static void abbrev_sha1_in_line(struct strbuf
*line
)
1214 struct strbuf
**split
;
1217 if (starts_with(line
->buf
, "exec ") ||
1218 starts_with(line
->buf
, "x "))
1221 split
= strbuf_split_max(line
, ' ', 3);
1222 if (split
[0] && split
[1]) {
1223 struct object_id oid
;
1226 * strbuf_split_max left a space. Trim it and re-add
1227 * it after abbreviation.
1229 strbuf_trim(split
[1]);
1230 if (!get_oid(split
[1]->buf
, &oid
)) {
1231 strbuf_reset(split
[1]);
1232 strbuf_add_unique_abbrev(split
[1], &oid
,
1234 strbuf_addch(split
[1], ' ');
1236 for (i
= 0; split
[i
]; i
++)
1237 strbuf_addbuf(line
, split
[i
]);
1240 strbuf_list_free(split
);
1243 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1245 struct strbuf line
= STRBUF_INIT
;
1246 FILE *f
= fopen(git_path("%s", fname
), "r");
1249 if (errno
== ENOENT
)
1251 die_errno("Could not open file %s for reading",
1252 git_path("%s", fname
));
1254 while (!strbuf_getline_lf(&line
, f
)) {
1255 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1260 abbrev_sha1_in_line(&line
);
1261 string_list_append(lines
, line
.buf
);
1264 strbuf_release(&line
);
1268 static void show_rebase_information(struct wt_status
*s
,
1271 if (s
->state
.rebase_interactive_in_progress
) {
1273 int nr_lines_to_show
= 2;
1275 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1276 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1278 read_rebase_todolist("rebase-merge/done", &have_done
);
1279 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1281 status_printf_ln(s
, color
,
1282 _("git-rebase-todo is missing."));
1283 if (have_done
.nr
== 0)
1284 status_printf_ln(s
, color
, _("No commands done."));
1286 status_printf_ln(s
, color
,
1287 Q_("Last command done (%d command done):",
1288 "Last commands done (%d commands done):",
1291 for (i
= (have_done
.nr
> nr_lines_to_show
)
1292 ? have_done
.nr
- nr_lines_to_show
: 0;
1295 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1296 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1297 status_printf_ln(s
, color
,
1298 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1301 if (yet_to_do
.nr
== 0)
1302 status_printf_ln(s
, color
,
1303 _("No commands remaining."));
1305 status_printf_ln(s
, color
,
1306 Q_("Next command to do (%d remaining command):",
1307 "Next commands to do (%d remaining commands):",
1310 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1311 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1313 status_printf_ln(s
, color
,
1314 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1316 string_list_clear(&yet_to_do
, 0);
1317 string_list_clear(&have_done
, 0);
1321 static void print_rebase_state(struct wt_status
*s
,
1324 if (s
->state
.branch
)
1325 status_printf_ln(s
, color
,
1326 _("You are currently rebasing branch '%s' on '%s'."),
1330 status_printf_ln(s
, color
,
1331 _("You are currently rebasing."));
1334 static void show_rebase_in_progress(struct wt_status
*s
,
1339 show_rebase_information(s
, color
);
1340 if (has_unmerged(s
)) {
1341 print_rebase_state(s
, color
);
1343 status_printf_ln(s
, color
,
1344 _(" (fix conflicts and then run \"git rebase --continue\")"));
1345 status_printf_ln(s
, color
,
1346 _(" (use \"git rebase --skip\" to skip this patch)"));
1347 status_printf_ln(s
, color
,
1348 _(" (use \"git rebase --abort\" to check out the original branch)"));
1350 } else if (s
->state
.rebase_in_progress
||
1351 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1352 print_rebase_state(s
, color
);
1354 status_printf_ln(s
, color
,
1355 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1356 } else if (split_commit_in_progress(s
)) {
1357 if (s
->state
.branch
)
1358 status_printf_ln(s
, color
,
1359 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1363 status_printf_ln(s
, color
,
1364 _("You are currently splitting a commit during a rebase."));
1366 status_printf_ln(s
, color
,
1367 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1369 if (s
->state
.branch
)
1370 status_printf_ln(s
, color
,
1371 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1375 status_printf_ln(s
, color
,
1376 _("You are currently editing a commit during a rebase."));
1377 if (s
->hints
&& !s
->amend
) {
1378 status_printf_ln(s
, color
,
1379 _(" (use \"git commit --amend\" to amend the current commit)"));
1380 status_printf_ln(s
, color
,
1381 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1384 wt_longstatus_print_trailer(s
);
1387 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1390 if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1391 status_printf_ln(s
, color
,
1392 _("Cherry-pick currently in progress."));
1394 status_printf_ln(s
, color
,
1395 _("You are currently cherry-picking commit %s."),
1396 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
,
1400 if (has_unmerged(s
))
1401 status_printf_ln(s
, color
,
1402 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1403 else if (is_null_oid(&s
->state
.cherry_pick_head_oid
))
1404 status_printf_ln(s
, color
,
1405 _(" (run \"git cherry-pick --continue\" to continue)"));
1407 status_printf_ln(s
, color
,
1408 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1409 status_printf_ln(s
, color
,
1410 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1412 wt_longstatus_print_trailer(s
);
1415 static void show_revert_in_progress(struct wt_status
*s
,
1418 if (is_null_oid(&s
->state
.revert_head_oid
))
1419 status_printf_ln(s
, color
,
1420 _("Revert currently in progress."));
1422 status_printf_ln(s
, color
,
1423 _("You are currently reverting commit %s."),
1424 find_unique_abbrev(&s
->state
.revert_head_oid
,
1427 if (has_unmerged(s
))
1428 status_printf_ln(s
, color
,
1429 _(" (fix conflicts and run \"git revert --continue\")"));
1430 else if (is_null_oid(&s
->state
.revert_head_oid
))
1431 status_printf_ln(s
, color
,
1432 _(" (run \"git revert --continue\" to continue)"));
1434 status_printf_ln(s
, color
,
1435 _(" (all conflicts fixed: run \"git revert --continue\")"));
1436 status_printf_ln(s
, color
,
1437 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1439 wt_longstatus_print_trailer(s
);
1442 static void show_bisect_in_progress(struct wt_status
*s
,
1445 if (s
->state
.branch
)
1446 status_printf_ln(s
, color
,
1447 _("You are currently bisecting, started from branch '%s'."),
1450 status_printf_ln(s
, color
,
1451 _("You are currently bisecting."));
1453 status_printf_ln(s
, color
,
1454 _(" (use \"git bisect reset\" to get back to the original branch)"));
1455 wt_longstatus_print_trailer(s
);
1459 * Extract branch information from rebase/bisect
1461 static char *get_branch(const struct worktree
*wt
, const char *path
)
1463 struct strbuf sb
= STRBUF_INIT
;
1464 struct object_id oid
;
1465 const char *branch_name
;
1467 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1470 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1471 strbuf_setlen(&sb
, sb
.len
- 1);
1474 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1475 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1476 else if (starts_with(sb
.buf
, "refs/"))
1478 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1480 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1481 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1485 return strbuf_detach(&sb
, NULL
);
1488 strbuf_release(&sb
);
1492 struct grab_1st_switch_cbdata
{
1494 struct object_id noid
;
1497 static int grab_1st_switch(struct object_id
*ooid
, struct object_id
*noid
,
1498 const char *email
, timestamp_t timestamp
, int tz
,
1499 const char *message
, void *cb_data
)
1501 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1502 const char *target
= NULL
, *end
;
1504 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1506 target
= strstr(message
, " to ");
1509 target
+= strlen(" to ");
1510 strbuf_reset(&cb
->buf
);
1511 oidcpy(&cb
->noid
, noid
);
1512 end
= strchrnul(target
, '\n');
1513 strbuf_add(&cb
->buf
, target
, end
- target
);
1514 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1515 /* HEAD is relative. Resolve it to the right reflog entry. */
1516 strbuf_reset(&cb
->buf
);
1517 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1522 static void wt_status_get_detached_from(struct repository
*r
,
1523 struct wt_status_state
*state
)
1525 struct grab_1st_switch_cbdata cb
;
1526 struct commit
*commit
;
1527 struct object_id oid
;
1530 strbuf_init(&cb
.buf
, 0);
1531 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1532 strbuf_release(&cb
.buf
);
1536 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
) == 1 &&
1537 /* sha1 is a commit? match without further lookup */
1538 (oideq(&cb
.noid
, &oid
) ||
1539 /* perhaps sha1 is a tag, try to dereference to a commit */
1540 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1541 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1542 const char *from
= ref
;
1543 if (!skip_prefix(from
, "refs/tags/", &from
))
1544 skip_prefix(from
, "refs/remotes/", &from
);
1545 state
->detached_from
= xstrdup(from
);
1547 state
->detached_from
=
1548 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1549 oidcpy(&state
->detached_oid
, &cb
.noid
);
1550 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1551 oideq(&oid
, &state
->detached_oid
);
1554 strbuf_release(&cb
.buf
);
1557 int wt_status_check_rebase(const struct worktree
*wt
,
1558 struct wt_status_state
*state
)
1562 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1563 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1564 state
->am_in_progress
= 1;
1565 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1566 state
->am_empty_patch
= 1;
1568 state
->rebase_in_progress
= 1;
1569 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1570 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1572 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1573 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1574 state
->rebase_interactive_in_progress
= 1;
1576 state
->rebase_in_progress
= 1;
1577 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1578 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1584 int wt_status_check_bisect(const struct worktree
*wt
,
1585 struct wt_status_state
*state
)
1589 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1590 state
->bisect_in_progress
= 1;
1591 state
->branch
= get_branch(wt
, "BISECT_START");
1597 void wt_status_get_state(struct repository
*r
,
1598 struct wt_status_state
*state
,
1599 int get_detached_from
)
1602 struct object_id oid
;
1603 enum replay_action action
;
1605 if (!stat(git_path_merge_head(r
), &st
)) {
1606 wt_status_check_rebase(NULL
, state
);
1607 state
->merge_in_progress
= 1;
1608 } else if (wt_status_check_rebase(NULL
, state
)) {
1610 } else if (!stat(git_path_cherry_pick_head(r
), &st
) &&
1611 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1612 state
->cherry_pick_in_progress
= 1;
1613 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1615 wt_status_check_bisect(NULL
, state
);
1616 if (!stat(git_path_revert_head(r
), &st
) &&
1617 !get_oid("REVERT_HEAD", &oid
)) {
1618 state
->revert_in_progress
= 1;
1619 oidcpy(&state
->revert_head_oid
, &oid
);
1621 if (!sequencer_get_last_command(r
, &action
)) {
1622 if (action
== REPLAY_PICK
) {
1623 state
->cherry_pick_in_progress
= 1;
1624 oidcpy(&state
->cherry_pick_head_oid
, &null_oid
);
1626 state
->revert_in_progress
= 1;
1627 oidcpy(&state
->revert_head_oid
, &null_oid
);
1630 if (get_detached_from
)
1631 wt_status_get_detached_from(r
, state
);
1634 static void wt_longstatus_print_state(struct wt_status
*s
)
1636 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1637 struct wt_status_state
*state
= &s
->state
;
1639 if (state
->merge_in_progress
) {
1640 if (state
->rebase_interactive_in_progress
) {
1641 show_rebase_information(s
, state_color
);
1644 show_merge_in_progress(s
, state_color
);
1645 } else if (state
->am_in_progress
)
1646 show_am_in_progress(s
, state_color
);
1647 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1648 show_rebase_in_progress(s
, state_color
);
1649 else if (state
->cherry_pick_in_progress
)
1650 show_cherry_pick_in_progress(s
, state_color
);
1651 else if (state
->revert_in_progress
)
1652 show_revert_in_progress(s
, state_color
);
1653 if (state
->bisect_in_progress
)
1654 show_bisect_in_progress(s
, state_color
);
1657 static void wt_longstatus_print(struct wt_status
*s
)
1659 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1660 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1663 const char *on_what
= _("On branch ");
1664 const char *branch_name
= s
->branch
;
1665 if (!strcmp(branch_name
, "HEAD")) {
1666 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1667 if (s
->state
.rebase_in_progress
||
1668 s
->state
.rebase_interactive_in_progress
) {
1669 if (s
->state
.rebase_interactive_in_progress
)
1670 on_what
= _("interactive rebase in progress; onto ");
1672 on_what
= _("rebase in progress; onto ");
1673 branch_name
= s
->state
.onto
;
1674 } else if (s
->state
.detached_from
) {
1675 branch_name
= s
->state
.detached_from
;
1676 if (s
->state
.detached_at
)
1677 on_what
= _("HEAD detached at ");
1679 on_what
= _("HEAD detached from ");
1682 on_what
= _("Not currently on any branch.");
1685 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1686 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1687 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1688 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1690 wt_longstatus_print_tracking(s
);
1693 wt_longstatus_print_state(s
);
1695 if (s
->is_initial
) {
1696 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1697 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1699 ? _("Initial commit")
1700 : _("No commits yet"));
1701 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1704 wt_longstatus_print_updated(s
);
1705 wt_longstatus_print_unmerged(s
);
1706 wt_longstatus_print_changed(s
);
1707 if (s
->submodule_summary
&&
1708 (!s
->ignore_submodule_arg
||
1709 strcmp(s
->ignore_submodule_arg
, "all"))) {
1710 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1711 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1713 if (s
->show_untracked_files
) {
1714 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1715 if (s
->show_ignored_mode
)
1716 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1717 if (advice_status_u_option
&& 2000 < s
->untracked_in_ms
) {
1718 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1719 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1720 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1721 "may speed it up, but you have to be careful not to forget to add\n"
1722 "new files yourself (see 'git help status')."),
1723 s
->untracked_in_ms
/ 1000.0);
1725 } else if (s
->committable
)
1726 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1728 ? _(" (use -u option to show untracked files)") : "");
1731 wt_longstatus_print_verbose(s
);
1732 if (!s
->committable
) {
1734 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1737 else if (s
->workdir_dirty
) {
1739 printf(_("no changes added to commit "
1740 "(use \"git add\" and/or \"git commit -a\")\n"));
1742 printf(_("no changes added to commit\n"));
1743 } else if (s
->untracked
.nr
) {
1745 printf(_("nothing added to commit but untracked files "
1746 "present (use \"git add\" to track)\n"));
1748 printf(_("nothing added to commit but untracked files present\n"));
1749 } else if (s
->is_initial
) {
1751 printf(_("nothing to commit (create/copy files "
1752 "and use \"git add\" to track)\n"));
1754 printf(_("nothing to commit\n"));
1755 } else if (!s
->show_untracked_files
) {
1757 printf(_("nothing to commit (use -u to show untracked files)\n"));
1759 printf(_("nothing to commit\n"));
1761 printf(_("nothing to commit, working tree clean\n"));
1764 wt_longstatus_print_stash_summary(s
);
1767 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1768 struct wt_status
*s
)
1770 struct wt_status_change_data
*d
= it
->util
;
1771 const char *how
= "??";
1773 switch (d
->stagemask
) {
1774 case 1: how
= "DD"; break; /* both deleted */
1775 case 2: how
= "AU"; break; /* added by us */
1776 case 3: how
= "UD"; break; /* deleted by them */
1777 case 4: how
= "UA"; break; /* added by them */
1778 case 5: how
= "DU"; break; /* deleted by us */
1779 case 6: how
= "AA"; break; /* both added */
1780 case 7: how
= "UU"; break; /* both modified */
1782 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1783 if (s
->null_termination
) {
1784 fprintf(stdout
, " %s%c", it
->string
, 0);
1786 struct strbuf onebuf
= STRBUF_INIT
;
1788 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1789 printf(" %s\n", one
);
1790 strbuf_release(&onebuf
);
1794 static void wt_shortstatus_status(struct string_list_item
*it
,
1795 struct wt_status
*s
)
1797 struct wt_status_change_data
*d
= it
->util
;
1799 if (d
->index_status
)
1800 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1803 if (d
->worktree_status
)
1804 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1808 if (s
->null_termination
) {
1809 fprintf(stdout
, "%s%c", it
->string
, 0);
1810 if (d
->rename_source
)
1811 fprintf(stdout
, "%s%c", d
->rename_source
, 0);
1813 struct strbuf onebuf
= STRBUF_INIT
;
1816 if (d
->rename_source
) {
1817 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
);
1818 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1820 strbuf_addch(&onebuf
, '"');
1823 printf("%s -> ", one
);
1824 strbuf_release(&onebuf
);
1826 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1827 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1829 strbuf_addch(&onebuf
, '"');
1832 printf("%s\n", one
);
1833 strbuf_release(&onebuf
);
1837 static void wt_shortstatus_other(struct string_list_item
*it
,
1838 struct wt_status
*s
, const char *sign
)
1840 if (s
->null_termination
) {
1841 fprintf(stdout
, "%s %s%c", sign
, it
->string
, 0);
1843 struct strbuf onebuf
= STRBUF_INIT
;
1845 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1846 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
1847 printf(" %s\n", one
);
1848 strbuf_release(&onebuf
);
1852 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
1854 struct branch
*branch
;
1855 const char *header_color
= color(WT_STATUS_HEADER
, s
);
1856 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
1857 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
1861 const char *branch_name
;
1862 int num_ours
, num_theirs
, sti
;
1863 int upstream_is_gone
= 0;
1865 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
1869 branch_name
= s
->branch
;
1871 #define LABEL(string) (s->no_gettext ? (string) : _(string))
1874 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
1876 if (!strcmp(s
->branch
, "HEAD")) {
1877 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
1878 LABEL(N_("HEAD (no branch)")));
1882 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1884 branch
= branch_get(branch_name
);
1886 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1888 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
1889 0, s
->ahead_behind_flags
);
1894 upstream_is_gone
= 1;
1897 short_base
= shorten_unambiguous_ref(base
, 0);
1898 color_fprintf(s
->fp
, header_color
, "...");
1899 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
1902 if (!upstream_is_gone
&& !sti
)
1905 color_fprintf(s
->fp
, header_color
, " [");
1906 if (upstream_is_gone
) {
1907 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
1908 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
1909 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
1910 } else if (!num_ours
) {
1911 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
1912 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1913 } else if (!num_theirs
) {
1914 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
1915 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1917 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
1918 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1919 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
1920 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1923 color_fprintf(s
->fp
, header_color
, "]");
1925 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1928 static void wt_shortstatus_print(struct wt_status
*s
)
1930 struct string_list_item
*it
;
1933 wt_shortstatus_print_tracking(s
);
1935 for_each_string_list_item(it
, &s
->change
) {
1936 struct wt_status_change_data
*d
= it
->util
;
1939 wt_shortstatus_unmerged(it
, s
);
1941 wt_shortstatus_status(it
, s
);
1943 for_each_string_list_item(it
, &s
->untracked
)
1944 wt_shortstatus_other(it
, s
, "??");
1946 for_each_string_list_item(it
, &s
->ignored
)
1947 wt_shortstatus_other(it
, s
, "!!");
1950 static void wt_porcelain_print(struct wt_status
*s
)
1953 s
->relative_paths
= 0;
1956 wt_shortstatus_print(s
);
1960 * Print branch information for porcelain v2 output. These lines
1961 * are printed when the '--branch' parameter is given.
1963 * # branch.oid <commit><eol>
1964 * # branch.head <head><eol>
1965 * [# branch.upstream <upstream><eol>
1966 * [# branch.ab +<ahead> -<behind><eol>]]
1968 * <commit> ::= the current commit hash or the the literal
1969 * "(initial)" to indicate an initialized repo
1972 * <head> ::= <branch_name> the current branch name or
1973 * "(detached)" literal when detached head or
1974 * "(unknown)" when something is wrong.
1976 * <upstream> ::= the upstream branch name, when set.
1978 * <ahead> ::= integer ahead value or '?'.
1980 * <behind> ::= integer behind value or '?'.
1982 * The end-of-line is defined by the -z flag.
1984 * <eol> ::= NUL when -z,
1987 * When an upstream is set and present, the 'branch.ab' line will
1988 * be printed with the ahead/behind counts for the branch and the
1989 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1990 * are different, '?' will be substituted for the actual count.
1992 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
1994 struct branch
*branch
;
1996 const char *branch_name
;
1997 int ab_info
, nr_ahead
, nr_behind
;
1998 char eol
= s
->null_termination
? '\0' : '\n';
2000 fprintf(s
->fp
, "# branch.oid %s%c",
2001 (s
->is_initial
? "(initial)" : sha1_to_hex(s
->sha1_commit
)),
2005 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
2007 if (!strcmp(s
->branch
, "HEAD")) {
2008 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
2010 if (s
->state
.rebase_in_progress
||
2011 s
->state
.rebase_interactive_in_progress
)
2012 branch_name
= s
->state
.onto
;
2013 else if (s
->state
.detached_from
)
2014 branch_name
= s
->state
.detached_from
;
2019 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
2021 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
2024 /* Lookup stats on the upstream tracking branch, if set. */
2025 branch
= branch_get(branch_name
);
2027 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
2028 &base
, 0, s
->ahead_behind_flags
);
2030 base
= shorten_unambiguous_ref(base
, 0);
2031 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
2036 if (nr_ahead
|| nr_behind
)
2037 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
2038 nr_ahead
, nr_behind
, eol
);
2040 fprintf(s
->fp
, "# branch.ab +? -?%c",
2042 } else if (!ab_info
) {
2044 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2051 * Convert various submodule status values into a
2052 * fixed-length string of characters in the buffer provided.
2054 static void wt_porcelain_v2_submodule_state(
2055 struct wt_status_change_data
*d
,
2058 if (S_ISGITLINK(d
->mode_head
) ||
2059 S_ISGITLINK(d
->mode_index
) ||
2060 S_ISGITLINK(d
->mode_worktree
)) {
2062 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2063 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2064 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2075 * Fix-up changed entries before we print them.
2077 static void wt_porcelain_v2_fix_up_changed(
2078 struct string_list_item
*it
,
2079 struct wt_status
*s
)
2081 struct wt_status_change_data
*d
= it
->util
;
2083 if (!d
->index_status
) {
2085 * This entry is unchanged in the index (relative to the head).
2086 * Therefore, the collect_updated_cb was never called for this
2087 * entry (during the head-vs-index scan) and so the head column
2088 * fields were never set.
2090 * We must have data for the index column (from the
2091 * index-vs-worktree scan (otherwise, this entry should not be
2092 * in the list of changes)).
2094 * Copy index column fields to the head column, so that our
2095 * output looks complete.
2097 assert(d
->mode_head
== 0);
2098 d
->mode_head
= d
->mode_index
;
2099 oidcpy(&d
->oid_head
, &d
->oid_index
);
2102 if (!d
->worktree_status
) {
2104 * This entry is unchanged in the worktree (relative to the index).
2105 * Therefore, the collect_changed_cb was never called for this entry
2106 * (during the index-vs-worktree scan) and so the worktree column
2107 * fields were never set.
2109 * We must have data for the index column (from the head-vs-index
2112 * Copy the index column fields to the worktree column so that
2113 * our output looks complete.
2115 * Note that we only have a mode field in the worktree column
2116 * because the scan code tries really hard to not have to compute it.
2118 assert(d
->mode_worktree
== 0);
2119 d
->mode_worktree
= d
->mode_index
;
2124 * Print porcelain v2 info for tracked entries with changes.
2126 static void wt_porcelain_v2_print_changed_entry(
2127 struct string_list_item
*it
,
2128 struct wt_status
*s
)
2130 struct wt_status_change_data
*d
= it
->util
;
2131 struct strbuf buf
= STRBUF_INIT
;
2132 struct strbuf buf_from
= STRBUF_INIT
;
2133 const char *path
= NULL
;
2134 const char *path_from
= NULL
;
2136 char submodule_token
[5];
2137 char sep_char
, eol_char
;
2139 wt_porcelain_v2_fix_up_changed(it
, s
);
2140 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2142 key
[0] = d
->index_status
? d
->index_status
: '.';
2143 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2146 if (s
->null_termination
) {
2148 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2149 * A single NUL character separates them.
2154 path_from
= d
->rename_source
;
2157 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2158 * The source path is only present when necessary.
2159 * A single TAB separates them (because paths can contain spaces
2160 * which are not escaped and C-quoting does escape TAB characters).
2164 path
= quote_path(it
->string
, s
->prefix
, &buf
);
2165 if (d
->rename_source
)
2166 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
);
2170 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2171 key
, submodule_token
,
2172 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2173 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2174 d
->rename_status
, d
->rename_score
,
2175 path
, sep_char
, path_from
, eol_char
);
2177 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2178 key
, submodule_token
,
2179 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2180 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2183 strbuf_release(&buf
);
2184 strbuf_release(&buf_from
);
2188 * Print porcelain v2 status info for unmerged entries.
2190 static void wt_porcelain_v2_print_unmerged_entry(
2191 struct string_list_item
*it
,
2192 struct wt_status
*s
)
2194 struct wt_status_change_data
*d
= it
->util
;
2195 struct index_state
*istate
= s
->repo
->index
;
2196 const struct cache_entry
*ce
;
2197 struct strbuf buf_index
= STRBUF_INIT
;
2198 const char *path_index
= NULL
;
2199 int pos
, stage
, sum
;
2202 struct object_id oid
;
2205 char submodule_token
[5];
2206 char unmerged_prefix
= 'u';
2207 char eol_char
= s
->null_termination
? '\0' : '\n';
2209 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2211 switch (d
->stagemask
) {
2212 case 1: key
= "DD"; break; /* both deleted */
2213 case 2: key
= "AU"; break; /* added by us */
2214 case 3: key
= "UD"; break; /* deleted by them */
2215 case 4: key
= "UA"; break; /* added by them */
2216 case 5: key
= "DU"; break; /* deleted by us */
2217 case 6: key
= "AA"; break; /* both added */
2218 case 7: key
= "UU"; break; /* both modified */
2220 BUG("unhandled unmerged status %x", d
->stagemask
);
2224 * Disregard d.aux.porcelain_v2 data that we accumulated
2225 * for the head and index columns during the scans and
2226 * replace with the actual stage data.
2228 * Note that this is a last-one-wins for each the individual
2229 * stage [123] columns in the event of multiple cache entries
2232 memset(stages
, 0, sizeof(stages
));
2234 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2237 while (pos
< istate
->cache_nr
) {
2238 ce
= istate
->cache
[pos
++];
2239 stage
= ce_stage(ce
);
2240 if (strcmp(ce
->name
, it
->string
) || !stage
)
2242 stages
[stage
- 1].mode
= ce
->ce_mode
;
2243 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2244 sum
|= (1 << (stage
- 1));
2246 if (sum
!= d
->stagemask
)
2247 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2249 if (s
->null_termination
)
2250 path_index
= it
->string
;
2252 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
);
2254 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2255 unmerged_prefix
, key
, submodule_token
,
2256 stages
[0].mode
, /* stage 1 */
2257 stages
[1].mode
, /* stage 2 */
2258 stages
[2].mode
, /* stage 3 */
2260 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2261 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2262 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2266 strbuf_release(&buf_index
);
2270 * Print porcelain V2 status info for untracked and ignored entries.
2272 static void wt_porcelain_v2_print_other(
2273 struct string_list_item
*it
,
2274 struct wt_status
*s
,
2277 struct strbuf buf
= STRBUF_INIT
;
2281 if (s
->null_termination
) {
2285 path
= quote_path(it
->string
, s
->prefix
, &buf
);
2289 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2291 strbuf_release(&buf
);
2295 * Print porcelain V2 status.
2298 * [<v2_changed_items>]*
2299 * [<v2_unmerged_items>]*
2300 * [<v2_untracked_items>]*
2301 * [<v2_ignored_items>]*
2304 static void wt_porcelain_v2_print(struct wt_status
*s
)
2306 struct wt_status_change_data
*d
;
2307 struct string_list_item
*it
;
2311 wt_porcelain_v2_print_tracking(s
);
2313 for (i
= 0; i
< s
->change
.nr
; i
++) {
2314 it
= &(s
->change
.items
[i
]);
2317 wt_porcelain_v2_print_changed_entry(it
, s
);
2320 for (i
= 0; i
< s
->change
.nr
; i
++) {
2321 it
= &(s
->change
.items
[i
]);
2324 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2327 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2328 it
= &(s
->untracked
.items
[i
]);
2329 wt_porcelain_v2_print_other(it
, s
, '?');
2332 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2333 it
= &(s
->ignored
.items
[i
]);
2334 wt_porcelain_v2_print_other(it
, s
, '!');
2338 void wt_status_print(struct wt_status
*s
)
2340 trace2_data_intmax("status", s
->repo
, "count/changed", s
->change
.nr
);
2341 trace2_data_intmax("status", s
->repo
, "count/untracked",
2343 trace2_data_intmax("status", s
->repo
, "count/ignored", s
->ignored
.nr
);
2345 trace2_region_enter("status", "print", s
->repo
);
2347 switch (s
->status_format
) {
2348 case STATUS_FORMAT_SHORT
:
2349 wt_shortstatus_print(s
);
2351 case STATUS_FORMAT_PORCELAIN
:
2352 wt_porcelain_print(s
);
2354 case STATUS_FORMAT_PORCELAIN_V2
:
2355 wt_porcelain_v2_print(s
);
2357 case STATUS_FORMAT_UNSPECIFIED
:
2358 BUG("finalize_deferred_config() should have been called");
2360 case STATUS_FORMAT_NONE
:
2361 case STATUS_FORMAT_LONG
:
2362 wt_longstatus_print(s
);
2366 trace2_region_leave("status", "print", s
->repo
);
2370 * Returns 1 if there are unstaged changes, 0 otherwise.
2372 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2374 struct rev_info rev_info
;
2377 repo_init_revisions(r
, &rev_info
, NULL
);
2378 if (ignore_submodules
) {
2379 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2380 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2382 rev_info
.diffopt
.flags
.quick
= 1;
2383 diff_setup_done(&rev_info
.diffopt
);
2384 result
= run_diff_files(&rev_info
, 0);
2385 return diff_result_code(&rev_info
.diffopt
, result
);
2389 * Returns 1 if there are uncommitted changes, 0 otherwise.
2391 int has_uncommitted_changes(struct repository
*r
,
2392 int ignore_submodules
)
2394 struct rev_info rev_info
;
2397 if (is_index_unborn(r
->index
))
2400 repo_init_revisions(r
, &rev_info
, NULL
);
2401 if (ignore_submodules
)
2402 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2403 rev_info
.diffopt
.flags
.quick
= 1;
2405 add_head_to_pending(&rev_info
);
2406 if (!rev_info
.pending
.nr
) {
2408 * We have no head (or it's corrupt); use the empty tree,
2409 * which will complain if the index is non-empty.
2411 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2412 add_pending_object(&rev_info
, &tree
->object
, "");
2415 diff_setup_done(&rev_info
.diffopt
);
2416 result
= run_diff_index(&rev_info
, 1);
2417 return diff_result_code(&rev_info
.diffopt
, result
);
2421 * If the work tree has unstaged or uncommitted changes, dies with the
2422 * appropriate message.
2424 int require_clean_work_tree(struct repository
*r
,
2427 int ignore_submodules
,
2430 struct lock_file lock_file
= LOCK_INIT
;
2433 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2434 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2436 repo_update_index_if_able(r
, &lock_file
);
2437 rollback_lock_file(&lock_file
);
2439 if (has_unstaged_changes(r
, ignore_submodules
)) {
2440 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2441 error(_("cannot %s: You have unstaged changes."), _(action
));
2445 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2447 error(_("additionally, your index contains uncommitted changes."));
2449 error(_("cannot %s: Your index contains uncommitted changes."),