10 #include "run-command.h"
11 #include "argv-array.h"
14 #include "submodule.h"
21 static const char cut_line
[] =
22 "------------------------ >8 ------------------------\n";
24 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
25 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
26 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
27 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
28 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
29 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
30 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
31 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
32 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
33 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
36 static const char *color(int slot
, struct wt_status
*s
)
39 if (want_color(s
->use_color
))
40 c
= s
->color_palette
[slot
];
41 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
42 c
= s
->color_palette
[WT_STATUS_HEADER
];
46 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
47 const char *fmt
, va_list ap
, const char *trail
)
49 struct strbuf sb
= STRBUF_INIT
;
50 struct strbuf linebuf
= STRBUF_INIT
;
51 const char *line
, *eol
;
53 strbuf_vaddf(&sb
, fmt
, ap
);
55 if (s
->display_comment_prefix
) {
56 strbuf_addch(&sb
, comment_line_char
);
58 strbuf_addch(&sb
, ' ');
60 color_print_strbuf(s
->fp
, color
, &sb
);
62 fprintf(s
->fp
, "%s", trail
);
66 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
67 eol
= strchr(line
, '\n');
69 strbuf_reset(&linebuf
);
70 if (at_bol
&& s
->display_comment_prefix
) {
71 strbuf_addch(&linebuf
, comment_line_char
);
72 if (*line
!= '\n' && *line
!= '\t')
73 strbuf_addch(&linebuf
, ' ');
76 strbuf_add(&linebuf
, line
, eol
- line
);
78 strbuf_addstr(&linebuf
, line
);
79 color_print_strbuf(s
->fp
, color
, &linebuf
);
87 fprintf(s
->fp
, "%s", trail
);
88 strbuf_release(&linebuf
);
92 void status_printf_ln(struct wt_status
*s
, const char *color
,
98 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
102 void status_printf(struct wt_status
*s
, const char *color
,
103 const char *fmt
, ...)
108 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
112 static void status_printf_more(struct wt_status
*s
, const char *color
,
113 const char *fmt
, ...)
118 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
122 void wt_status_prepare(struct repository
*r
, struct wt_status
*s
)
124 memset(s
, 0, sizeof(*s
));
126 memcpy(s
->color_palette
, default_wt_status_colors
,
127 sizeof(default_wt_status_colors
));
128 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
130 s
->relative_paths
= 1;
131 s
->branch
= resolve_refdup("HEAD", 0, NULL
, NULL
);
132 s
->reference
= "HEAD";
134 s
->index_file
= get_index_file();
135 s
->change
.strdup_strings
= 1;
136 s
->untracked
.strdup_strings
= 1;
137 s
->ignored
.strdup_strings
= 1;
138 s
->show_branch
= -1; /* unspecified */
140 s
->ahead_behind_flags
= AHEAD_BEHIND_UNSPECIFIED
;
141 s
->display_comment_prefix
= 0;
142 s
->detect_rename
= -1;
143 s
->rename_score
= -1;
144 s
->rename_limit
= -1;
147 static void wt_longstatus_print_unmerged_header(struct wt_status
*s
)
150 int del_mod_conflict
= 0;
151 int both_deleted
= 0;
153 const char *c
= color(WT_STATUS_HEADER
, s
);
155 status_printf_ln(s
, c
, _("Unmerged paths:"));
157 for (i
= 0; i
< s
->change
.nr
; i
++) {
158 struct string_list_item
*it
= &(s
->change
.items
[i
]);
159 struct wt_status_change_data
*d
= it
->util
;
161 switch (d
->stagemask
) {
169 del_mod_conflict
= 1;
179 if (s
->whence
!= FROM_COMMIT
)
181 else if (!s
->is_initial
)
182 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
184 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
187 if (!del_mod_conflict
)
188 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
190 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
191 } else if (!del_mod_conflict
&& !not_deleted
) {
192 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
194 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
196 status_printf_ln(s
, c
, "%s", "");
199 static void wt_longstatus_print_cached_header(struct wt_status
*s
)
201 const char *c
= color(WT_STATUS_HEADER
, s
);
203 status_printf_ln(s
, c
, _("Changes to be committed:"));
206 if (s
->whence
!= FROM_COMMIT
)
207 ; /* NEEDSWORK: use "git reset --unresolve"??? */
208 else if (!s
->is_initial
)
209 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
211 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
212 status_printf_ln(s
, c
, "%s", "");
215 static void wt_longstatus_print_dirty_header(struct wt_status
*s
,
217 int has_dirty_submodules
)
219 const char *c
= color(WT_STATUS_HEADER
, s
);
221 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
225 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
227 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
228 status_printf_ln(s
, c
, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
229 if (has_dirty_submodules
)
230 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
231 status_printf_ln(s
, c
, "%s", "");
234 static void wt_longstatus_print_other_header(struct wt_status
*s
,
238 const char *c
= color(WT_STATUS_HEADER
, s
);
239 status_printf_ln(s
, c
, "%s:", what
);
242 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
243 status_printf_ln(s
, c
, "%s", "");
246 static void wt_longstatus_print_trailer(struct wt_status
*s
)
248 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
251 #define quote_path quote_path_relative
253 static const char *wt_status_unmerged_status_string(int stagemask
)
257 return _("both deleted:");
259 return _("added by us:");
261 return _("deleted by them:");
263 return _("added by them:");
265 return _("deleted by us:");
267 return _("both added:");
269 return _("both modified:");
271 BUG("unhandled unmerged status %x", stagemask
);
275 static const char *wt_status_diff_status_string(int status
)
278 case DIFF_STATUS_ADDED
:
279 return _("new file:");
280 case DIFF_STATUS_COPIED
:
282 case DIFF_STATUS_DELETED
:
283 return _("deleted:");
284 case DIFF_STATUS_MODIFIED
:
285 return _("modified:");
286 case DIFF_STATUS_RENAMED
:
287 return _("renamed:");
288 case DIFF_STATUS_TYPE_CHANGED
:
289 return _("typechange:");
290 case DIFF_STATUS_UNKNOWN
:
291 return _("unknown:");
292 case DIFF_STATUS_UNMERGED
:
293 return _("unmerged:");
299 static int maxwidth(const char *(*label
)(int), int minval
, int maxval
)
303 for (i
= minval
; i
<= maxval
; i
++) {
304 const char *s
= label(i
);
305 int len
= s
? utf8_strwidth(s
) : 0;
312 static void wt_longstatus_print_unmerged_data(struct wt_status
*s
,
313 struct string_list_item
*it
)
315 const char *c
= color(WT_STATUS_UNMERGED
, s
);
316 struct wt_status_change_data
*d
= it
->util
;
317 struct strbuf onebuf
= STRBUF_INIT
;
318 static char *padding
;
319 static int label_width
;
320 const char *one
, *how
;
324 label_width
= maxwidth(wt_status_unmerged_status_string
, 1, 7);
325 label_width
+= strlen(" ");
326 padding
= xmallocz(label_width
);
327 memset(padding
, ' ', label_width
);
330 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
331 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
333 how
= wt_status_unmerged_status_string(d
->stagemask
);
334 len
= label_width
- utf8_strwidth(how
);
335 status_printf_more(s
, c
, "%s%.*s%s\n", how
, len
, padding
, one
);
336 strbuf_release(&onebuf
);
339 static void wt_longstatus_print_change_data(struct wt_status
*s
,
341 struct string_list_item
*it
)
343 struct wt_status_change_data
*d
= it
->util
;
344 const char *c
= color(change_type
, s
);
348 const char *one
, *two
;
349 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
350 struct strbuf extra
= STRBUF_INIT
;
351 static char *padding
;
352 static int label_width
;
357 /* If DIFF_STATUS_* uses outside the range [A..Z], we're in trouble */
358 label_width
= maxwidth(wt_status_diff_status_string
, 'A', 'Z');
359 label_width
+= strlen(" ");
360 padding
= xmallocz(label_width
);
361 memset(padding
, ' ', label_width
);
364 one_name
= two_name
= it
->string
;
365 switch (change_type
) {
366 case WT_STATUS_UPDATED
:
367 status
= d
->index_status
;
369 case WT_STATUS_CHANGED
:
370 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
371 strbuf_addstr(&extra
, " (");
372 if (d
->new_submodule_commits
)
373 strbuf_addstr(&extra
, _("new commits, "));
374 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
375 strbuf_addstr(&extra
, _("modified content, "));
376 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
377 strbuf_addstr(&extra
, _("untracked content, "));
378 strbuf_setlen(&extra
, extra
.len
- 2);
379 strbuf_addch(&extra
, ')');
381 status
= d
->worktree_status
;
384 BUG("unhandled change_type %d in wt_longstatus_print_change_data",
389 * Only pick up the rename it's relevant. If the rename is for
390 * the changed section and we're printing the updated section,
393 if (d
->rename_status
== status
)
394 one_name
= d
->rename_source
;
396 one
= quote_path(one_name
, s
->prefix
, &onebuf
);
397 two
= quote_path(two_name
, s
->prefix
, &twobuf
);
399 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
400 what
= wt_status_diff_status_string(status
);
402 BUG("unhandled diff status %c", status
);
403 len
= label_width
- utf8_strwidth(what
);
405 if (one_name
!= two_name
)
406 status_printf_more(s
, c
, "%s%.*s%s -> %s",
407 what
, len
, padding
, one
, two
);
409 status_printf_more(s
, c
, "%s%.*s%s",
410 what
, len
, padding
, one
);
412 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
413 strbuf_release(&extra
);
415 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
416 strbuf_release(&onebuf
);
417 strbuf_release(&twobuf
);
420 static char short_submodule_status(struct wt_status_change_data
*d
)
422 if (d
->new_submodule_commits
)
424 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
426 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
428 return d
->worktree_status
;
431 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
432 struct diff_options
*options
,
435 struct wt_status
*s
= data
;
440 s
->workdir_dirty
= 1;
441 for (i
= 0; i
< q
->nr
; i
++) {
442 struct diff_filepair
*p
;
443 struct string_list_item
*it
;
444 struct wt_status_change_data
*d
;
447 it
= string_list_insert(&s
->change
, p
->two
->path
);
450 d
= xcalloc(1, sizeof(*d
));
453 if (!d
->worktree_status
)
454 d
->worktree_status
= p
->status
;
455 if (S_ISGITLINK(p
->two
->mode
)) {
456 d
->dirty_submodule
= p
->two
->dirty_submodule
;
457 d
->new_submodule_commits
= !oideq(&p
->one
->oid
,
459 if (s
->status_format
== STATUS_FORMAT_SHORT
)
460 d
->worktree_status
= short_submodule_status(d
);
464 case DIFF_STATUS_ADDED
:
465 d
->mode_worktree
= p
->two
->mode
;
468 case DIFF_STATUS_DELETED
:
469 d
->mode_index
= p
->one
->mode
;
470 oidcpy(&d
->oid_index
, &p
->one
->oid
);
471 /* mode_worktree is zero for a delete. */
474 case DIFF_STATUS_COPIED
:
475 case DIFF_STATUS_RENAMED
:
476 if (d
->rename_status
)
477 BUG("multiple renames on the same target? how?");
478 d
->rename_source
= xstrdup(p
->one
->path
);
479 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
480 d
->rename_status
= p
->status
;
482 case DIFF_STATUS_MODIFIED
:
483 case DIFF_STATUS_TYPE_CHANGED
:
484 case DIFF_STATUS_UNMERGED
:
485 d
->mode_index
= p
->one
->mode
;
486 d
->mode_worktree
= p
->two
->mode
;
487 oidcpy(&d
->oid_index
, &p
->one
->oid
);
491 BUG("unhandled diff-files status '%c'", p
->status
);
498 static int unmerged_mask(struct index_state
*istate
, const char *path
)
501 const struct cache_entry
*ce
;
503 pos
= index_name_pos(istate
, path
, strlen(path
));
509 while (pos
< istate
->cache_nr
) {
510 ce
= istate
->cache
[pos
++];
511 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
513 mask
|= (1 << (ce_stage(ce
) - 1));
518 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
519 struct diff_options
*options
,
522 struct wt_status
*s
= data
;
525 for (i
= 0; i
< q
->nr
; i
++) {
526 struct diff_filepair
*p
;
527 struct string_list_item
*it
;
528 struct wt_status_change_data
*d
;
531 it
= string_list_insert(&s
->change
, p
->two
->path
);
534 d
= xcalloc(1, sizeof(*d
));
537 if (!d
->index_status
)
538 d
->index_status
= p
->status
;
540 case DIFF_STATUS_ADDED
:
541 /* Leave {mode,oid}_head zero for an add. */
542 d
->mode_index
= p
->two
->mode
;
543 oidcpy(&d
->oid_index
, &p
->two
->oid
);
546 case DIFF_STATUS_DELETED
:
547 d
->mode_head
= p
->one
->mode
;
548 oidcpy(&d
->oid_head
, &p
->one
->oid
);
550 /* Leave {mode,oid}_index zero for a delete. */
553 case DIFF_STATUS_COPIED
:
554 case DIFF_STATUS_RENAMED
:
555 if (d
->rename_status
)
556 BUG("multiple renames on the same target? how?");
557 d
->rename_source
= xstrdup(p
->one
->path
);
558 d
->rename_score
= p
->score
* 100 / MAX_SCORE
;
559 d
->rename_status
= p
->status
;
561 case DIFF_STATUS_MODIFIED
:
562 case DIFF_STATUS_TYPE_CHANGED
:
563 d
->mode_head
= p
->one
->mode
;
564 d
->mode_index
= p
->two
->mode
;
565 oidcpy(&d
->oid_head
, &p
->one
->oid
);
566 oidcpy(&d
->oid_index
, &p
->two
->oid
);
569 case DIFF_STATUS_UNMERGED
:
570 d
->stagemask
= unmerged_mask(s
->repo
->index
,
573 * Don't bother setting {mode,oid}_{head,index} since the print
574 * code will output the stage values directly and not use the
575 * values in these fields.
580 BUG("unhandled diff-index status '%c'", p
->status
);
586 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
590 repo_init_revisions(s
->repo
, &rev
, NULL
);
591 setup_revisions(0, NULL
, &rev
, NULL
);
592 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
593 rev
.diffopt
.flags
.dirty_submodules
= 1;
594 rev
.diffopt
.ita_invisible_in_index
= 1;
595 if (!s
->show_untracked_files
)
596 rev
.diffopt
.flags
.ignore_untracked_in_submodules
= 1;
597 if (s
->ignore_submodule_arg
) {
598 rev
.diffopt
.flags
.override_submodule_config
= 1;
599 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
601 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
602 rev
.diffopt
.format_callback_data
= s
;
603 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
604 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
605 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
606 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
607 run_diff_files(&rev
, 0);
610 static void wt_status_collect_changes_index(struct wt_status
*s
)
613 struct setup_revision_opt opt
;
615 repo_init_revisions(s
->repo
, &rev
, NULL
);
616 memset(&opt
, 0, sizeof(opt
));
617 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
618 setup_revisions(0, NULL
, &rev
, &opt
);
620 rev
.diffopt
.flags
.override_submodule_config
= 1;
621 rev
.diffopt
.ita_invisible_in_index
= 1;
622 if (s
->ignore_submodule_arg
) {
623 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
626 * Unless the user did explicitly request a submodule ignore
627 * mode by passing a command line option we do not ignore any
628 * changed submodule SHA-1s when comparing index and HEAD, no
629 * matter what is configured. Otherwise the user won't be
630 * shown any submodules she manually added (and which are
631 * staged to be committed), which would be really confusing.
633 handle_ignore_submodules_arg(&rev
.diffopt
, "dirty");
636 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
637 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
638 rev
.diffopt
.format_callback_data
= s
;
639 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
640 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
641 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
642 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
643 run_diff_index(&rev
, 1);
646 static void wt_status_collect_changes_initial(struct wt_status
*s
)
648 struct index_state
*istate
= s
->repo
->index
;
651 for (i
= 0; i
< istate
->cache_nr
; i
++) {
652 struct string_list_item
*it
;
653 struct wt_status_change_data
*d
;
654 const struct cache_entry
*ce
= istate
->cache
[i
];
656 if (!ce_path_match(istate
, ce
, &s
->pathspec
, NULL
))
658 if (ce_intent_to_add(ce
))
660 it
= string_list_insert(&s
->change
, ce
->name
);
663 d
= xcalloc(1, sizeof(*d
));
667 d
->index_status
= DIFF_STATUS_UNMERGED
;
668 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
670 * Don't bother setting {mode,oid}_{head,index} since the print
671 * code will output the stage values directly and not use the
672 * values in these fields.
676 d
->index_status
= DIFF_STATUS_ADDED
;
677 /* Leave {mode,oid}_head zero for adds. */
678 d
->mode_index
= ce
->ce_mode
;
679 oidcpy(&d
->oid_index
, &ce
->oid
);
685 static void wt_status_collect_untracked(struct wt_status
*s
)
688 struct dir_struct dir
;
689 uint64_t t_begin
= getnanotime();
690 struct index_state
*istate
= s
->repo
->index
;
692 if (!s
->show_untracked_files
)
695 memset(&dir
, 0, sizeof(dir
));
696 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
698 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
699 if (s
->show_ignored_mode
) {
700 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
702 if (s
->show_ignored_mode
== SHOW_MATCHING_IGNORED
)
703 dir
.flags
|= DIR_SHOW_IGNORED_TOO_MODE_MATCHING
;
705 dir
.untracked
= istate
->untracked
;
708 setup_standard_excludes(&dir
);
710 fill_directory(&dir
, istate
, &s
->pathspec
);
712 for (i
= 0; i
< dir
.nr
; i
++) {
713 struct dir_entry
*ent
= dir
.entries
[i
];
714 if (index_name_is_other(istate
, ent
->name
, ent
->len
) &&
715 dir_path_match(istate
, ent
, &s
->pathspec
, 0, NULL
))
716 string_list_insert(&s
->untracked
, ent
->name
);
720 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
721 struct dir_entry
*ent
= dir
.ignored
[i
];
722 if (index_name_is_other(istate
, ent
->name
, ent
->len
) &&
723 dir_path_match(istate
, ent
, &s
->pathspec
, 0, NULL
))
724 string_list_insert(&s
->ignored
, ent
->name
);
730 clear_directory(&dir
);
732 if (advice_status_u_option
)
733 s
->untracked_in_ms
= (getnanotime() - t_begin
) / 1000000;
736 static int has_unmerged(struct wt_status
*s
)
740 for (i
= 0; i
< s
->change
.nr
; i
++) {
741 struct wt_status_change_data
*d
;
742 d
= s
->change
.items
[i
].util
;
749 void wt_status_collect(struct wt_status
*s
)
751 wt_status_collect_changes_worktree(s
);
753 wt_status_collect_changes_initial(s
);
755 wt_status_collect_changes_index(s
);
756 wt_status_collect_untracked(s
);
758 wt_status_get_state(s
->repo
, &s
->state
, s
->branch
&& !strcmp(s
->branch
, "HEAD"));
759 if (s
->state
.merge_in_progress
&& !has_unmerged(s
))
763 void wt_status_collect_free_buffers(struct wt_status
*s
)
765 free(s
->state
.branch
);
767 free(s
->state
.detached_from
);
770 static void wt_longstatus_print_unmerged(struct wt_status
*s
)
772 int shown_header
= 0;
775 for (i
= 0; i
< s
->change
.nr
; i
++) {
776 struct wt_status_change_data
*d
;
777 struct string_list_item
*it
;
778 it
= &(s
->change
.items
[i
]);
783 wt_longstatus_print_unmerged_header(s
);
786 wt_longstatus_print_unmerged_data(s
, it
);
789 wt_longstatus_print_trailer(s
);
793 static void wt_longstatus_print_updated(struct wt_status
*s
)
795 int shown_header
= 0;
798 for (i
= 0; i
< s
->change
.nr
; i
++) {
799 struct wt_status_change_data
*d
;
800 struct string_list_item
*it
;
801 it
= &(s
->change
.items
[i
]);
803 if (!d
->index_status
||
804 d
->index_status
== DIFF_STATUS_UNMERGED
)
807 wt_longstatus_print_cached_header(s
);
810 wt_longstatus_print_change_data(s
, WT_STATUS_UPDATED
, it
);
813 wt_longstatus_print_trailer(s
);
819 * 1 : some change but no delete
821 static int wt_status_check_worktree_changes(struct wt_status
*s
,
822 int *dirty_submodules
)
827 *dirty_submodules
= 0;
829 for (i
= 0; i
< s
->change
.nr
; i
++) {
830 struct wt_status_change_data
*d
;
831 d
= s
->change
.items
[i
].util
;
832 if (!d
->worktree_status
||
833 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
837 if (d
->dirty_submodule
)
838 *dirty_submodules
= 1;
839 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
845 static void wt_longstatus_print_changed(struct wt_status
*s
)
847 int i
, dirty_submodules
;
848 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
850 if (!worktree_changes
)
853 wt_longstatus_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
855 for (i
= 0; i
< s
->change
.nr
; i
++) {
856 struct wt_status_change_data
*d
;
857 struct string_list_item
*it
;
858 it
= &(s
->change
.items
[i
]);
860 if (!d
->worktree_status
||
861 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
863 wt_longstatus_print_change_data(s
, WT_STATUS_CHANGED
, it
);
865 wt_longstatus_print_trailer(s
);
868 static int stash_count_refs(struct object_id
*ooid
, struct object_id
*noid
,
869 const char *email
, timestamp_t timestamp
, int tz
,
870 const char *message
, void *cb_data
)
877 static void wt_longstatus_print_stash_summary(struct wt_status
*s
)
881 for_each_reflog_ent("refs/stash", stash_count_refs
, &stash_count
);
883 status_printf_ln(s
, GIT_COLOR_NORMAL
,
884 Q_("Your stash currently has %d entry",
885 "Your stash currently has %d entries", stash_count
),
889 static void wt_longstatus_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
891 struct child_process sm_summary
= CHILD_PROCESS_INIT
;
892 struct strbuf cmd_stdout
= STRBUF_INIT
;
893 struct strbuf summary
= STRBUF_INIT
;
894 char *summary_content
;
896 argv_array_pushf(&sm_summary
.env_array
, "GIT_INDEX_FILE=%s",
899 argv_array_push(&sm_summary
.args
, "submodule");
900 argv_array_push(&sm_summary
.args
, "summary");
901 argv_array_push(&sm_summary
.args
, uncommitted
? "--files" : "--cached");
902 argv_array_push(&sm_summary
.args
, "--for-status");
903 argv_array_push(&sm_summary
.args
, "--summary-limit");
904 argv_array_pushf(&sm_summary
.args
, "%d", s
->submodule_summary
);
906 argv_array_push(&sm_summary
.args
, s
->amend
? "HEAD^" : "HEAD");
908 sm_summary
.git_cmd
= 1;
909 sm_summary
.no_stdin
= 1;
911 capture_command(&sm_summary
, &cmd_stdout
, 1024);
913 /* prepend header, only if there's an actual output */
914 if (cmd_stdout
.len
) {
916 strbuf_addstr(&summary
, _("Submodules changed but not updated:"));
918 strbuf_addstr(&summary
, _("Submodule changes to be committed:"));
919 strbuf_addstr(&summary
, "\n\n");
921 strbuf_addbuf(&summary
, &cmd_stdout
);
922 strbuf_release(&cmd_stdout
);
924 if (s
->display_comment_prefix
) {
926 summary_content
= strbuf_detach(&summary
, &len
);
927 strbuf_add_commented_lines(&summary
, summary_content
, len
);
928 free(summary_content
);
931 fputs(summary
.buf
, s
->fp
);
932 strbuf_release(&summary
);
935 static void wt_longstatus_print_other(struct wt_status
*s
,
936 struct string_list
*l
,
941 struct strbuf buf
= STRBUF_INIT
;
942 static struct string_list output
= STRING_LIST_INIT_DUP
;
943 struct column_options copts
;
948 wt_longstatus_print_other_header(s
, what
, how
);
950 for (i
= 0; i
< l
->nr
; i
++) {
951 struct string_list_item
*it
;
954 path
= quote_path(it
->string
, s
->prefix
, &buf
);
955 if (column_active(s
->colopts
)) {
956 string_list_append(&output
, path
);
959 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
960 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
964 strbuf_release(&buf
);
965 if (!column_active(s
->colopts
))
968 strbuf_addf(&buf
, "%s%s\t%s",
969 color(WT_STATUS_HEADER
, s
),
970 s
->display_comment_prefix
? "#" : "",
971 color(WT_STATUS_UNTRACKED
, s
));
972 memset(&copts
, 0, sizeof(copts
));
974 copts
.indent
= buf
.buf
;
975 if (want_color(s
->use_color
))
976 copts
.nl
= GIT_COLOR_RESET
"\n";
977 print_columns(&output
, s
->colopts
, &copts
);
978 string_list_clear(&output
, 0);
979 strbuf_release(&buf
);
981 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
984 size_t wt_status_locate_end(const char *s
, size_t len
)
987 struct strbuf pattern
= STRBUF_INIT
;
989 strbuf_addf(&pattern
, "\n%c %s", comment_line_char
, cut_line
);
990 if (starts_with(s
, pattern
.buf
+ 1))
992 else if ((p
= strstr(s
, pattern
.buf
)))
994 strbuf_release(&pattern
);
998 void wt_status_add_cut_line(FILE *fp
)
1000 const char *explanation
= _("Do not modify or remove the line above.\nEverything below it will be ignored.");
1001 struct strbuf buf
= STRBUF_INIT
;
1003 fprintf(fp
, "%c %s", comment_line_char
, cut_line
);
1004 strbuf_add_commented_lines(&buf
, explanation
, strlen(explanation
));
1006 strbuf_release(&buf
);
1009 static void wt_longstatus_print_verbose(struct wt_status
*s
)
1011 struct rev_info rev
;
1012 struct setup_revision_opt opt
;
1013 int dirty_submodules
;
1014 const char *c
= color(WT_STATUS_HEADER
, s
);
1016 repo_init_revisions(s
->repo
, &rev
, NULL
);
1017 rev
.diffopt
.flags
.allow_textconv
= 1;
1018 rev
.diffopt
.ita_invisible_in_index
= 1;
1020 memset(&opt
, 0, sizeof(opt
));
1021 opt
.def
= s
->is_initial
? empty_tree_oid_hex() : s
->reference
;
1022 setup_revisions(0, NULL
, &rev
, &opt
);
1024 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
1025 rev
.diffopt
.detect_rename
= s
->detect_rename
>= 0 ? s
->detect_rename
: rev
.diffopt
.detect_rename
;
1026 rev
.diffopt
.rename_limit
= s
->rename_limit
>= 0 ? s
->rename_limit
: rev
.diffopt
.rename_limit
;
1027 rev
.diffopt
.rename_score
= s
->rename_score
>= 0 ? s
->rename_score
: rev
.diffopt
.rename_score
;
1028 rev
.diffopt
.file
= s
->fp
;
1029 rev
.diffopt
.close_file
= 0;
1031 * If we're not going to stdout, then we definitely don't
1032 * want color, since we are going to the commit message
1033 * file (and even the "auto" setting won't work, since it
1034 * will have checked isatty on stdout). But we then do want
1035 * to insert the scissor line here to reliably remove the
1036 * diff before committing.
1038 if (s
->fp
!= stdout
) {
1039 rev
.diffopt
.use_color
= 0;
1040 wt_status_add_cut_line(s
->fp
);
1042 if (s
->verbose
> 1 && s
->committable
) {
1043 /* print_updated() printed a header, so do we */
1044 if (s
->fp
!= stdout
)
1045 wt_longstatus_print_trailer(s
);
1046 status_printf_ln(s
, c
, _("Changes to be committed:"));
1047 rev
.diffopt
.a_prefix
= "c/";
1048 rev
.diffopt
.b_prefix
= "i/";
1049 } /* else use prefix as per user config */
1050 run_diff_index(&rev
, 1);
1051 if (s
->verbose
> 1 &&
1052 wt_status_check_worktree_changes(s
, &dirty_submodules
)) {
1053 status_printf_ln(s
, c
,
1054 "--------------------------------------------------");
1055 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
1057 rev
.diffopt
.a_prefix
= "i/";
1058 rev
.diffopt
.b_prefix
= "w/";
1059 run_diff_files(&rev
, 0);
1063 static void wt_longstatus_print_tracking(struct wt_status
*s
)
1065 struct strbuf sb
= STRBUF_INIT
;
1066 const char *cp
, *ep
, *branch_name
;
1067 struct branch
*branch
;
1068 char comment_line_string
[3];
1071 assert(s
->branch
&& !s
->is_initial
);
1072 if (!skip_prefix(s
->branch
, "refs/heads/", &branch_name
))
1074 branch
= branch_get(branch_name
);
1075 if (!format_tracking_info(branch
, &sb
, s
->ahead_behind_flags
))
1079 if (s
->display_comment_prefix
) {
1080 comment_line_string
[i
++] = comment_line_char
;
1081 comment_line_string
[i
++] = ' ';
1083 comment_line_string
[i
] = '\0';
1085 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
1086 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
1087 "%s%.*s", comment_line_string
,
1088 (int)(ep
- cp
), cp
);
1089 if (s
->display_comment_prefix
)
1090 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
1094 strbuf_release(&sb
);
1097 static void show_merge_in_progress(struct wt_status
*s
,
1100 if (has_unmerged(s
)) {
1101 status_printf_ln(s
, color
, _("You have unmerged paths."));
1103 status_printf_ln(s
, color
,
1104 _(" (fix conflicts and run \"git commit\")"));
1105 status_printf_ln(s
, color
,
1106 _(" (use \"git merge --abort\" to abort the merge)"));
1109 status_printf_ln(s
, color
,
1110 _("All conflicts fixed but you are still merging."));
1112 status_printf_ln(s
, color
,
1113 _(" (use \"git commit\" to conclude merge)"));
1115 wt_longstatus_print_trailer(s
);
1118 static void show_am_in_progress(struct wt_status
*s
,
1121 status_printf_ln(s
, color
,
1122 _("You are in the middle of an am session."));
1123 if (s
->state
.am_empty_patch
)
1124 status_printf_ln(s
, color
,
1125 _("The current patch is empty."));
1127 if (!s
->state
.am_empty_patch
)
1128 status_printf_ln(s
, color
,
1129 _(" (fix conflicts and then run \"git am --continue\")"));
1130 status_printf_ln(s
, color
,
1131 _(" (use \"git am --skip\" to skip this patch)"));
1132 status_printf_ln(s
, color
,
1133 _(" (use \"git am --abort\" to restore the original branch)"));
1135 wt_longstatus_print_trailer(s
);
1138 static char *read_line_from_git_path(const char *filename
)
1140 struct strbuf buf
= STRBUF_INIT
;
1141 FILE *fp
= fopen_or_warn(git_path("%s", filename
), "r");
1144 strbuf_release(&buf
);
1147 strbuf_getline_lf(&buf
, fp
);
1149 return strbuf_detach(&buf
, NULL
);
1151 strbuf_release(&buf
);
1156 static int split_commit_in_progress(struct wt_status
*s
)
1158 int split_in_progress
= 0;
1159 char *head
, *orig_head
, *rebase_amend
, *rebase_orig_head
;
1161 if ((!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
) ||
1162 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
1165 head
= read_line_from_git_path("HEAD");
1166 orig_head
= read_line_from_git_path("ORIG_HEAD");
1167 rebase_amend
= read_line_from_git_path("rebase-merge/amend");
1168 rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
1170 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
)
1171 ; /* fall through, no split in progress */
1172 else if (!strcmp(rebase_amend
, rebase_orig_head
))
1173 split_in_progress
= !!strcmp(head
, rebase_amend
);
1174 else if (strcmp(orig_head
, rebase_orig_head
))
1175 split_in_progress
= 1;
1180 free(rebase_orig_head
);
1182 return split_in_progress
;
1187 * "pick d6a2f0303e897ec257dd0e0a39a5ccb709bc2047 some message"
1189 * "pick d6a2f03 some message"
1191 * The function assumes that the line does not contain useless spaces
1192 * before or after the command.
1194 static void abbrev_sha1_in_line(struct strbuf
*line
)
1196 struct strbuf
**split
;
1199 if (starts_with(line
->buf
, "exec ") ||
1200 starts_with(line
->buf
, "x "))
1203 split
= strbuf_split_max(line
, ' ', 3);
1204 if (split
[0] && split
[1]) {
1205 struct object_id oid
;
1208 * strbuf_split_max left a space. Trim it and re-add
1209 * it after abbreviation.
1211 strbuf_trim(split
[1]);
1212 if (!get_oid(split
[1]->buf
, &oid
)) {
1213 strbuf_reset(split
[1]);
1214 strbuf_add_unique_abbrev(split
[1], &oid
,
1216 strbuf_addch(split
[1], ' ');
1218 for (i
= 0; split
[i
]; i
++)
1219 strbuf_addbuf(line
, split
[i
]);
1222 strbuf_list_free(split
);
1225 static int read_rebase_todolist(const char *fname
, struct string_list
*lines
)
1227 struct strbuf line
= STRBUF_INIT
;
1228 FILE *f
= fopen(git_path("%s", fname
), "r");
1231 if (errno
== ENOENT
)
1233 die_errno("Could not open file %s for reading",
1234 git_path("%s", fname
));
1236 while (!strbuf_getline_lf(&line
, f
)) {
1237 if (line
.len
&& line
.buf
[0] == comment_line_char
)
1242 abbrev_sha1_in_line(&line
);
1243 string_list_append(lines
, line
.buf
);
1246 strbuf_release(&line
);
1250 static void show_rebase_information(struct wt_status
*s
,
1253 if (s
->state
.rebase_interactive_in_progress
) {
1255 int nr_lines_to_show
= 2;
1257 struct string_list have_done
= STRING_LIST_INIT_DUP
;
1258 struct string_list yet_to_do
= STRING_LIST_INIT_DUP
;
1260 read_rebase_todolist("rebase-merge/done", &have_done
);
1261 if (read_rebase_todolist("rebase-merge/git-rebase-todo",
1263 status_printf_ln(s
, color
,
1264 _("git-rebase-todo is missing."));
1265 if (have_done
.nr
== 0)
1266 status_printf_ln(s
, color
, _("No commands done."));
1268 status_printf_ln(s
, color
,
1269 Q_("Last command done (%d command done):",
1270 "Last commands done (%d commands done):",
1273 for (i
= (have_done
.nr
> nr_lines_to_show
)
1274 ? have_done
.nr
- nr_lines_to_show
: 0;
1277 status_printf_ln(s
, color
, " %s", have_done
.items
[i
].string
);
1278 if (have_done
.nr
> nr_lines_to_show
&& s
->hints
)
1279 status_printf_ln(s
, color
,
1280 _(" (see more in file %s)"), git_path("rebase-merge/done"));
1283 if (yet_to_do
.nr
== 0)
1284 status_printf_ln(s
, color
,
1285 _("No commands remaining."));
1287 status_printf_ln(s
, color
,
1288 Q_("Next command to do (%d remaining command):",
1289 "Next commands to do (%d remaining commands):",
1292 for (i
= 0; i
< nr_lines_to_show
&& i
< yet_to_do
.nr
; i
++)
1293 status_printf_ln(s
, color
, " %s", yet_to_do
.items
[i
].string
);
1295 status_printf_ln(s
, color
,
1296 _(" (use \"git rebase --edit-todo\" to view and edit)"));
1298 string_list_clear(&yet_to_do
, 0);
1299 string_list_clear(&have_done
, 0);
1303 static void print_rebase_state(struct wt_status
*s
,
1306 if (s
->state
.branch
)
1307 status_printf_ln(s
, color
,
1308 _("You are currently rebasing branch '%s' on '%s'."),
1312 status_printf_ln(s
, color
,
1313 _("You are currently rebasing."));
1316 static void show_rebase_in_progress(struct wt_status
*s
,
1321 show_rebase_information(s
, color
);
1322 if (has_unmerged(s
)) {
1323 print_rebase_state(s
, color
);
1325 status_printf_ln(s
, color
,
1326 _(" (fix conflicts and then run \"git rebase --continue\")"));
1327 status_printf_ln(s
, color
,
1328 _(" (use \"git rebase --skip\" to skip this patch)"));
1329 status_printf_ln(s
, color
,
1330 _(" (use \"git rebase --abort\" to check out the original branch)"));
1332 } else if (s
->state
.rebase_in_progress
||
1333 !stat(git_path_merge_msg(s
->repo
), &st
)) {
1334 print_rebase_state(s
, color
);
1336 status_printf_ln(s
, color
,
1337 _(" (all conflicts fixed: run \"git rebase --continue\")"));
1338 } else if (split_commit_in_progress(s
)) {
1339 if (s
->state
.branch
)
1340 status_printf_ln(s
, color
,
1341 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
1345 status_printf_ln(s
, color
,
1346 _("You are currently splitting a commit during a rebase."));
1348 status_printf_ln(s
, color
,
1349 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
1351 if (s
->state
.branch
)
1352 status_printf_ln(s
, color
,
1353 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
1357 status_printf_ln(s
, color
,
1358 _("You are currently editing a commit during a rebase."));
1359 if (s
->hints
&& !s
->amend
) {
1360 status_printf_ln(s
, color
,
1361 _(" (use \"git commit --amend\" to amend the current commit)"));
1362 status_printf_ln(s
, color
,
1363 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
1366 wt_longstatus_print_trailer(s
);
1369 static void show_cherry_pick_in_progress(struct wt_status
*s
,
1372 status_printf_ln(s
, color
, _("You are currently cherry-picking commit %s."),
1373 find_unique_abbrev(&s
->state
.cherry_pick_head_oid
, DEFAULT_ABBREV
));
1375 if (has_unmerged(s
))
1376 status_printf_ln(s
, color
,
1377 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
1379 status_printf_ln(s
, color
,
1380 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
1381 status_printf_ln(s
, color
,
1382 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
1384 wt_longstatus_print_trailer(s
);
1387 static void show_revert_in_progress(struct wt_status
*s
,
1390 status_printf_ln(s
, color
, _("You are currently reverting commit %s."),
1391 find_unique_abbrev(&s
->state
.revert_head_oid
, DEFAULT_ABBREV
));
1393 if (has_unmerged(s
))
1394 status_printf_ln(s
, color
,
1395 _(" (fix conflicts and run \"git revert --continue\")"));
1397 status_printf_ln(s
, color
,
1398 _(" (all conflicts fixed: run \"git revert --continue\")"));
1399 status_printf_ln(s
, color
,
1400 _(" (use \"git revert --abort\" to cancel the revert operation)"));
1402 wt_longstatus_print_trailer(s
);
1405 static void show_bisect_in_progress(struct wt_status
*s
,
1408 if (s
->state
.branch
)
1409 status_printf_ln(s
, color
,
1410 _("You are currently bisecting, started from branch '%s'."),
1413 status_printf_ln(s
, color
,
1414 _("You are currently bisecting."));
1416 status_printf_ln(s
, color
,
1417 _(" (use \"git bisect reset\" to get back to the original branch)"));
1418 wt_longstatus_print_trailer(s
);
1422 * Extract branch information from rebase/bisect
1424 static char *get_branch(const struct worktree
*wt
, const char *path
)
1426 struct strbuf sb
= STRBUF_INIT
;
1427 struct object_id oid
;
1428 const char *branch_name
;
1430 if (strbuf_read_file(&sb
, worktree_git_path(wt
, "%s", path
), 0) <= 0)
1433 while (sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1434 strbuf_setlen(&sb
, sb
.len
- 1);
1437 if (skip_prefix(sb
.buf
, "refs/heads/", &branch_name
))
1438 strbuf_remove(&sb
, 0, branch_name
- sb
.buf
);
1439 else if (starts_with(sb
.buf
, "refs/"))
1441 else if (!get_oid_hex(sb
.buf
, &oid
)) {
1443 strbuf_add_unique_abbrev(&sb
, &oid
, DEFAULT_ABBREV
);
1444 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1448 return strbuf_detach(&sb
, NULL
);
1451 strbuf_release(&sb
);
1455 struct grab_1st_switch_cbdata
{
1457 struct object_id noid
;
1460 static int grab_1st_switch(struct object_id
*ooid
, struct object_id
*noid
,
1461 const char *email
, timestamp_t timestamp
, int tz
,
1462 const char *message
, void *cb_data
)
1464 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1465 const char *target
= NULL
, *end
;
1467 if (!skip_prefix(message
, "checkout: moving from ", &message
))
1469 target
= strstr(message
, " to ");
1472 target
+= strlen(" to ");
1473 strbuf_reset(&cb
->buf
);
1474 oidcpy(&cb
->noid
, noid
);
1475 end
= strchrnul(target
, '\n');
1476 strbuf_add(&cb
->buf
, target
, end
- target
);
1477 if (!strcmp(cb
->buf
.buf
, "HEAD")) {
1478 /* HEAD is relative. Resolve it to the right reflog entry. */
1479 strbuf_reset(&cb
->buf
);
1480 strbuf_add_unique_abbrev(&cb
->buf
, noid
, DEFAULT_ABBREV
);
1485 static void wt_status_get_detached_from(struct repository
*r
,
1486 struct wt_status_state
*state
)
1488 struct grab_1st_switch_cbdata cb
;
1489 struct commit
*commit
;
1490 struct object_id oid
;
1493 strbuf_init(&cb
.buf
, 0);
1494 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1495 strbuf_release(&cb
.buf
);
1499 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, &oid
, &ref
) == 1 &&
1500 /* sha1 is a commit? match without further lookup */
1501 (oideq(&cb
.noid
, &oid
) ||
1502 /* perhaps sha1 is a tag, try to dereference to a commit */
1503 ((commit
= lookup_commit_reference_gently(r
, &oid
, 1)) != NULL
&&
1504 oideq(&cb
.noid
, &commit
->object
.oid
)))) {
1505 const char *from
= ref
;
1506 if (!skip_prefix(from
, "refs/tags/", &from
))
1507 skip_prefix(from
, "refs/remotes/", &from
);
1508 state
->detached_from
= xstrdup(from
);
1510 state
->detached_from
=
1511 xstrdup(find_unique_abbrev(&cb
.noid
, DEFAULT_ABBREV
));
1512 oidcpy(&state
->detached_oid
, &cb
.noid
);
1513 state
->detached_at
= !get_oid("HEAD", &oid
) &&
1514 oideq(&oid
, &state
->detached_oid
);
1517 strbuf_release(&cb
.buf
);
1520 int wt_status_check_rebase(const struct worktree
*wt
,
1521 struct wt_status_state
*state
)
1525 if (!stat(worktree_git_path(wt
, "rebase-apply"), &st
)) {
1526 if (!stat(worktree_git_path(wt
, "rebase-apply/applying"), &st
)) {
1527 state
->am_in_progress
= 1;
1528 if (!stat(worktree_git_path(wt
, "rebase-apply/patch"), &st
) && !st
.st_size
)
1529 state
->am_empty_patch
= 1;
1531 state
->rebase_in_progress
= 1;
1532 state
->branch
= get_branch(wt
, "rebase-apply/head-name");
1533 state
->onto
= get_branch(wt
, "rebase-apply/onto");
1535 } else if (!stat(worktree_git_path(wt
, "rebase-merge"), &st
)) {
1536 if (!stat(worktree_git_path(wt
, "rebase-merge/interactive"), &st
))
1537 state
->rebase_interactive_in_progress
= 1;
1539 state
->rebase_in_progress
= 1;
1540 state
->branch
= get_branch(wt
, "rebase-merge/head-name");
1541 state
->onto
= get_branch(wt
, "rebase-merge/onto");
1547 int wt_status_check_bisect(const struct worktree
*wt
,
1548 struct wt_status_state
*state
)
1552 if (!stat(worktree_git_path(wt
, "BISECT_LOG"), &st
)) {
1553 state
->bisect_in_progress
= 1;
1554 state
->branch
= get_branch(wt
, "BISECT_START");
1560 void wt_status_get_state(struct repository
*r
,
1561 struct wt_status_state
*state
,
1562 int get_detached_from
)
1565 struct object_id oid
;
1567 if (!stat(git_path_merge_head(r
), &st
)) {
1568 wt_status_check_rebase(NULL
, state
);
1569 state
->merge_in_progress
= 1;
1570 } else if (wt_status_check_rebase(NULL
, state
)) {
1572 } else if (!stat(git_path_cherry_pick_head(r
), &st
) &&
1573 !get_oid("CHERRY_PICK_HEAD", &oid
)) {
1574 state
->cherry_pick_in_progress
= 1;
1575 oidcpy(&state
->cherry_pick_head_oid
, &oid
);
1577 wt_status_check_bisect(NULL
, state
);
1578 if (!stat(git_path_revert_head(r
), &st
) &&
1579 !get_oid("REVERT_HEAD", &oid
)) {
1580 state
->revert_in_progress
= 1;
1581 oidcpy(&state
->revert_head_oid
, &oid
);
1584 if (get_detached_from
)
1585 wt_status_get_detached_from(r
, state
);
1588 static void wt_longstatus_print_state(struct wt_status
*s
)
1590 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1591 struct wt_status_state
*state
= &s
->state
;
1593 if (state
->merge_in_progress
) {
1594 if (state
->rebase_interactive_in_progress
) {
1595 show_rebase_information(s
, state_color
);
1598 show_merge_in_progress(s
, state_color
);
1599 } else if (state
->am_in_progress
)
1600 show_am_in_progress(s
, state_color
);
1601 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1602 show_rebase_in_progress(s
, state_color
);
1603 else if (state
->cherry_pick_in_progress
)
1604 show_cherry_pick_in_progress(s
, state_color
);
1605 else if (state
->revert_in_progress
)
1606 show_revert_in_progress(s
, state_color
);
1607 if (state
->bisect_in_progress
)
1608 show_bisect_in_progress(s
, state_color
);
1611 static void wt_longstatus_print(struct wt_status
*s
)
1613 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1614 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1617 const char *on_what
= _("On branch ");
1618 const char *branch_name
= s
->branch
;
1619 if (!strcmp(branch_name
, "HEAD")) {
1620 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1621 if (s
->state
.rebase_in_progress
||
1622 s
->state
.rebase_interactive_in_progress
) {
1623 if (s
->state
.rebase_interactive_in_progress
)
1624 on_what
= _("interactive rebase in progress; onto ");
1626 on_what
= _("rebase in progress; onto ");
1627 branch_name
= s
->state
.onto
;
1628 } else if (s
->state
.detached_from
) {
1629 branch_name
= s
->state
.detached_from
;
1630 if (s
->state
.detached_at
)
1631 on_what
= _("HEAD detached at ");
1633 on_what
= _("HEAD detached from ");
1636 on_what
= _("Not currently on any branch.");
1639 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1640 status_printf(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1641 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1642 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1644 wt_longstatus_print_tracking(s
);
1647 wt_longstatus_print_state(s
);
1649 if (s
->is_initial
) {
1650 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1651 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
),
1653 ? _("Initial commit")
1654 : _("No commits yet"));
1655 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "%s", "");
1658 wt_longstatus_print_updated(s
);
1659 wt_longstatus_print_unmerged(s
);
1660 wt_longstatus_print_changed(s
);
1661 if (s
->submodule_summary
&&
1662 (!s
->ignore_submodule_arg
||
1663 strcmp(s
->ignore_submodule_arg
, "all"))) {
1664 wt_longstatus_print_submodule_summary(s
, 0); /* staged */
1665 wt_longstatus_print_submodule_summary(s
, 1); /* unstaged */
1667 if (s
->show_untracked_files
) {
1668 wt_longstatus_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1669 if (s
->show_ignored_mode
)
1670 wt_longstatus_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1671 if (advice_status_u_option
&& 2000 < s
->untracked_in_ms
) {
1672 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", "");
1673 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1674 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1675 "may speed it up, but you have to be careful not to forget to add\n"
1676 "new files yourself (see 'git help status')."),
1677 s
->untracked_in_ms
/ 1000.0);
1679 } else if (s
->committable
)
1680 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1682 ? _(" (use -u option to show untracked files)") : "");
1685 wt_longstatus_print_verbose(s
);
1686 if (!s
->committable
) {
1688 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1691 else if (s
->workdir_dirty
) {
1693 printf(_("no changes added to commit "
1694 "(use \"git add\" and/or \"git commit -a\")\n"));
1696 printf(_("no changes added to commit\n"));
1697 } else if (s
->untracked
.nr
) {
1699 printf(_("nothing added to commit but untracked files "
1700 "present (use \"git add\" to track)\n"));
1702 printf(_("nothing added to commit but untracked files present\n"));
1703 } else if (s
->is_initial
) {
1705 printf(_("nothing to commit (create/copy files "
1706 "and use \"git add\" to track)\n"));
1708 printf(_("nothing to commit\n"));
1709 } else if (!s
->show_untracked_files
) {
1711 printf(_("nothing to commit (use -u to show untracked files)\n"));
1713 printf(_("nothing to commit\n"));
1715 printf(_("nothing to commit, working tree clean\n"));
1718 wt_longstatus_print_stash_summary(s
);
1721 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1722 struct wt_status
*s
)
1724 struct wt_status_change_data
*d
= it
->util
;
1725 const char *how
= "??";
1727 switch (d
->stagemask
) {
1728 case 1: how
= "DD"; break; /* both deleted */
1729 case 2: how
= "AU"; break; /* added by us */
1730 case 3: how
= "UD"; break; /* deleted by them */
1731 case 4: how
= "UA"; break; /* added by them */
1732 case 5: how
= "DU"; break; /* deleted by us */
1733 case 6: how
= "AA"; break; /* both added */
1734 case 7: how
= "UU"; break; /* both modified */
1736 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1737 if (s
->null_termination
) {
1738 fprintf(stdout
, " %s%c", it
->string
, 0);
1740 struct strbuf onebuf
= STRBUF_INIT
;
1742 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1743 printf(" %s\n", one
);
1744 strbuf_release(&onebuf
);
1748 static void wt_shortstatus_status(struct string_list_item
*it
,
1749 struct wt_status
*s
)
1751 struct wt_status_change_data
*d
= it
->util
;
1753 if (d
->index_status
)
1754 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1757 if (d
->worktree_status
)
1758 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1762 if (s
->null_termination
) {
1763 fprintf(stdout
, "%s%c", it
->string
, 0);
1764 if (d
->rename_source
)
1765 fprintf(stdout
, "%s%c", d
->rename_source
, 0);
1767 struct strbuf onebuf
= STRBUF_INIT
;
1770 if (d
->rename_source
) {
1771 one
= quote_path(d
->rename_source
, s
->prefix
, &onebuf
);
1772 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1774 strbuf_addch(&onebuf
, '"');
1777 printf("%s -> ", one
);
1778 strbuf_release(&onebuf
);
1780 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1781 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1783 strbuf_addch(&onebuf
, '"');
1786 printf("%s\n", one
);
1787 strbuf_release(&onebuf
);
1791 static void wt_shortstatus_other(struct string_list_item
*it
,
1792 struct wt_status
*s
, const char *sign
)
1794 if (s
->null_termination
) {
1795 fprintf(stdout
, "%s %s%c", sign
, it
->string
, 0);
1797 struct strbuf onebuf
= STRBUF_INIT
;
1799 one
= quote_path(it
->string
, s
->prefix
, &onebuf
);
1800 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
1801 printf(" %s\n", one
);
1802 strbuf_release(&onebuf
);
1806 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
1808 struct branch
*branch
;
1809 const char *header_color
= color(WT_STATUS_HEADER
, s
);
1810 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
1811 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
1815 const char *branch_name
;
1816 int num_ours
, num_theirs
, sti
;
1817 int upstream_is_gone
= 0;
1819 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
1823 branch_name
= s
->branch
;
1825 #define LABEL(string) (s->no_gettext ? (string) : _(string))
1828 color_fprintf(s
->fp
, header_color
, LABEL(N_("No commits yet on ")));
1830 if (!strcmp(s
->branch
, "HEAD")) {
1831 color_fprintf(s
->fp
, color(WT_STATUS_NOBRANCH
, s
), "%s",
1832 LABEL(N_("HEAD (no branch)")));
1836 skip_prefix(branch_name
, "refs/heads/", &branch_name
);
1838 branch
= branch_get(branch_name
);
1840 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1842 sti
= stat_tracking_info(branch
, &num_ours
, &num_theirs
, &base
,
1843 s
->ahead_behind_flags
);
1848 upstream_is_gone
= 1;
1851 short_base
= shorten_unambiguous_ref(base
, 0);
1852 color_fprintf(s
->fp
, header_color
, "...");
1853 color_fprintf(s
->fp
, branch_color_remote
, "%s", short_base
);
1856 if (!upstream_is_gone
&& !sti
)
1859 color_fprintf(s
->fp
, header_color
, " [");
1860 if (upstream_is_gone
) {
1861 color_fprintf(s
->fp
, header_color
, LABEL(N_("gone")));
1862 } else if (s
->ahead_behind_flags
== AHEAD_BEHIND_QUICK
) {
1863 color_fprintf(s
->fp
, header_color
, LABEL(N_("different")));
1864 } else if (!num_ours
) {
1865 color_fprintf(s
->fp
, header_color
, LABEL(N_("behind ")));
1866 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1867 } else if (!num_theirs
) {
1868 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
1869 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1871 color_fprintf(s
->fp
, header_color
, LABEL(N_("ahead ")));
1872 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1873 color_fprintf(s
->fp
, header_color
, ", %s", LABEL(N_("behind ")));
1874 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1877 color_fprintf(s
->fp
, header_color
, "]");
1879 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1882 static void wt_shortstatus_print(struct wt_status
*s
)
1884 struct string_list_item
*it
;
1887 wt_shortstatus_print_tracking(s
);
1889 for_each_string_list_item(it
, &s
->change
) {
1890 struct wt_status_change_data
*d
= it
->util
;
1893 wt_shortstatus_unmerged(it
, s
);
1895 wt_shortstatus_status(it
, s
);
1897 for_each_string_list_item(it
, &s
->untracked
)
1898 wt_shortstatus_other(it
, s
, "??");
1900 for_each_string_list_item(it
, &s
->ignored
)
1901 wt_shortstatus_other(it
, s
, "!!");
1904 static void wt_porcelain_print(struct wt_status
*s
)
1907 s
->relative_paths
= 0;
1910 wt_shortstatus_print(s
);
1914 * Print branch information for porcelain v2 output. These lines
1915 * are printed when the '--branch' parameter is given.
1917 * # branch.oid <commit><eol>
1918 * # branch.head <head><eol>
1919 * [# branch.upstream <upstream><eol>
1920 * [# branch.ab +<ahead> -<behind><eol>]]
1922 * <commit> ::= the current commit hash or the the literal
1923 * "(initial)" to indicate an initialized repo
1926 * <head> ::= <branch_name> the current branch name or
1927 * "(detached)" literal when detached head or
1928 * "(unknown)" when something is wrong.
1930 * <upstream> ::= the upstream branch name, when set.
1932 * <ahead> ::= integer ahead value or '?'.
1934 * <behind> ::= integer behind value or '?'.
1936 * The end-of-line is defined by the -z flag.
1938 * <eol> ::= NUL when -z,
1941 * When an upstream is set and present, the 'branch.ab' line will
1942 * be printed with the ahead/behind counts for the branch and the
1943 * upstream. When AHEAD_BEHIND_QUICK is requested and the branches
1944 * are different, '?' will be substituted for the actual count.
1946 static void wt_porcelain_v2_print_tracking(struct wt_status
*s
)
1948 struct branch
*branch
;
1950 const char *branch_name
;
1951 int ab_info
, nr_ahead
, nr_behind
;
1952 char eol
= s
->null_termination
? '\0' : '\n';
1954 fprintf(s
->fp
, "# branch.oid %s%c",
1955 (s
->is_initial
? "(initial)" : sha1_to_hex(s
->sha1_commit
)),
1959 fprintf(s
->fp
, "# branch.head %s%c", "(unknown)", eol
);
1961 if (!strcmp(s
->branch
, "HEAD")) {
1962 fprintf(s
->fp
, "# branch.head %s%c", "(detached)", eol
);
1964 if (s
->state
.rebase_in_progress
||
1965 s
->state
.rebase_interactive_in_progress
)
1966 branch_name
= s
->state
.onto
;
1967 else if (s
->state
.detached_from
)
1968 branch_name
= s
->state
.detached_from
;
1973 skip_prefix(s
->branch
, "refs/heads/", &branch_name
);
1975 fprintf(s
->fp
, "# branch.head %s%c", branch_name
, eol
);
1978 /* Lookup stats on the upstream tracking branch, if set. */
1979 branch
= branch_get(branch_name
);
1981 ab_info
= stat_tracking_info(branch
, &nr_ahead
, &nr_behind
,
1982 &base
, s
->ahead_behind_flags
);
1984 base
= shorten_unambiguous_ref(base
, 0);
1985 fprintf(s
->fp
, "# branch.upstream %s%c", base
, eol
);
1990 if (nr_ahead
|| nr_behind
)
1991 fprintf(s
->fp
, "# branch.ab +%d -%d%c",
1992 nr_ahead
, nr_behind
, eol
);
1994 fprintf(s
->fp
, "# branch.ab +? -?%c",
1996 } else if (!ab_info
) {
1998 fprintf(s
->fp
, "# branch.ab +0 -0%c", eol
);
2005 * Convert various submodule status values into a
2006 * fixed-length string of characters in the buffer provided.
2008 static void wt_porcelain_v2_submodule_state(
2009 struct wt_status_change_data
*d
,
2012 if (S_ISGITLINK(d
->mode_head
) ||
2013 S_ISGITLINK(d
->mode_index
) ||
2014 S_ISGITLINK(d
->mode_worktree
)) {
2016 sub
[1] = d
->new_submodule_commits
? 'C' : '.';
2017 sub
[2] = (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) ? 'M' : '.';
2018 sub
[3] = (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ? 'U' : '.';
2029 * Fix-up changed entries before we print them.
2031 static void wt_porcelain_v2_fix_up_changed(
2032 struct string_list_item
*it
,
2033 struct wt_status
*s
)
2035 struct wt_status_change_data
*d
= it
->util
;
2037 if (!d
->index_status
) {
2039 * This entry is unchanged in the index (relative to the head).
2040 * Therefore, the collect_updated_cb was never called for this
2041 * entry (during the head-vs-index scan) and so the head column
2042 * fields were never set.
2044 * We must have data for the index column (from the
2045 * index-vs-worktree scan (otherwise, this entry should not be
2046 * in the list of changes)).
2048 * Copy index column fields to the head column, so that our
2049 * output looks complete.
2051 assert(d
->mode_head
== 0);
2052 d
->mode_head
= d
->mode_index
;
2053 oidcpy(&d
->oid_head
, &d
->oid_index
);
2056 if (!d
->worktree_status
) {
2058 * This entry is unchanged in the worktree (relative to the index).
2059 * Therefore, the collect_changed_cb was never called for this entry
2060 * (during the index-vs-worktree scan) and so the worktree column
2061 * fields were never set.
2063 * We must have data for the index column (from the head-vs-index
2066 * Copy the index column fields to the worktree column so that
2067 * our output looks complete.
2069 * Note that we only have a mode field in the worktree column
2070 * because the scan code tries really hard to not have to compute it.
2072 assert(d
->mode_worktree
== 0);
2073 d
->mode_worktree
= d
->mode_index
;
2078 * Print porcelain v2 info for tracked entries with changes.
2080 static void wt_porcelain_v2_print_changed_entry(
2081 struct string_list_item
*it
,
2082 struct wt_status
*s
)
2084 struct wt_status_change_data
*d
= it
->util
;
2085 struct strbuf buf
= STRBUF_INIT
;
2086 struct strbuf buf_from
= STRBUF_INIT
;
2087 const char *path
= NULL
;
2088 const char *path_from
= NULL
;
2090 char submodule_token
[5];
2091 char sep_char
, eol_char
;
2093 wt_porcelain_v2_fix_up_changed(it
, s
);
2094 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2096 key
[0] = d
->index_status
? d
->index_status
: '.';
2097 key
[1] = d
->worktree_status
? d
->worktree_status
: '.';
2100 if (s
->null_termination
) {
2102 * In -z mode, we DO NOT C-quote pathnames. Current path is ALWAYS first.
2103 * A single NUL character separates them.
2108 path_from
= d
->rename_source
;
2111 * Path(s) are C-quoted if necessary. Current path is ALWAYS first.
2112 * The source path is only present when necessary.
2113 * A single TAB separates them (because paths can contain spaces
2114 * which are not escaped and C-quoting does escape TAB characters).
2118 path
= quote_path(it
->string
, s
->prefix
, &buf
);
2119 if (d
->rename_source
)
2120 path_from
= quote_path(d
->rename_source
, s
->prefix
, &buf_from
);
2124 fprintf(s
->fp
, "2 %s %s %06o %06o %06o %s %s %c%d %s%c%s%c",
2125 key
, submodule_token
,
2126 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2127 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2128 d
->rename_status
, d
->rename_score
,
2129 path
, sep_char
, path_from
, eol_char
);
2131 fprintf(s
->fp
, "1 %s %s %06o %06o %06o %s %s %s%c",
2132 key
, submodule_token
,
2133 d
->mode_head
, d
->mode_index
, d
->mode_worktree
,
2134 oid_to_hex(&d
->oid_head
), oid_to_hex(&d
->oid_index
),
2137 strbuf_release(&buf
);
2138 strbuf_release(&buf_from
);
2142 * Print porcelain v2 status info for unmerged entries.
2144 static void wt_porcelain_v2_print_unmerged_entry(
2145 struct string_list_item
*it
,
2146 struct wt_status
*s
)
2148 struct wt_status_change_data
*d
= it
->util
;
2149 struct index_state
*istate
= s
->repo
->index
;
2150 const struct cache_entry
*ce
;
2151 struct strbuf buf_index
= STRBUF_INIT
;
2152 const char *path_index
= NULL
;
2153 int pos
, stage
, sum
;
2156 struct object_id oid
;
2159 char submodule_token
[5];
2160 char unmerged_prefix
= 'u';
2161 char eol_char
= s
->null_termination
? '\0' : '\n';
2163 wt_porcelain_v2_submodule_state(d
, submodule_token
);
2165 switch (d
->stagemask
) {
2166 case 1: key
= "DD"; break; /* both deleted */
2167 case 2: key
= "AU"; break; /* added by us */
2168 case 3: key
= "UD"; break; /* deleted by them */
2169 case 4: key
= "UA"; break; /* added by them */
2170 case 5: key
= "DU"; break; /* deleted by us */
2171 case 6: key
= "AA"; break; /* both added */
2172 case 7: key
= "UU"; break; /* both modified */
2174 BUG("unhandled unmerged status %x", d
->stagemask
);
2178 * Disregard d.aux.porcelain_v2 data that we accumulated
2179 * for the head and index columns during the scans and
2180 * replace with the actual stage data.
2182 * Note that this is a last-one-wins for each the individual
2183 * stage [123] columns in the event of multiple cache entries
2186 memset(stages
, 0, sizeof(stages
));
2188 pos
= index_name_pos(istate
, it
->string
, strlen(it
->string
));
2191 while (pos
< istate
->cache_nr
) {
2192 ce
= istate
->cache
[pos
++];
2193 stage
= ce_stage(ce
);
2194 if (strcmp(ce
->name
, it
->string
) || !stage
)
2196 stages
[stage
- 1].mode
= ce
->ce_mode
;
2197 oidcpy(&stages
[stage
- 1].oid
, &ce
->oid
);
2198 sum
|= (1 << (stage
- 1));
2200 if (sum
!= d
->stagemask
)
2201 BUG("observed stagemask 0x%x != expected stagemask 0x%x", sum
, d
->stagemask
);
2203 if (s
->null_termination
)
2204 path_index
= it
->string
;
2206 path_index
= quote_path(it
->string
, s
->prefix
, &buf_index
);
2208 fprintf(s
->fp
, "%c %s %s %06o %06o %06o %06o %s %s %s %s%c",
2209 unmerged_prefix
, key
, submodule_token
,
2210 stages
[0].mode
, /* stage 1 */
2211 stages
[1].mode
, /* stage 2 */
2212 stages
[2].mode
, /* stage 3 */
2214 oid_to_hex(&stages
[0].oid
), /* stage 1 */
2215 oid_to_hex(&stages
[1].oid
), /* stage 2 */
2216 oid_to_hex(&stages
[2].oid
), /* stage 3 */
2220 strbuf_release(&buf_index
);
2224 * Print porcelain V2 status info for untracked and ignored entries.
2226 static void wt_porcelain_v2_print_other(
2227 struct string_list_item
*it
,
2228 struct wt_status
*s
,
2231 struct strbuf buf
= STRBUF_INIT
;
2235 if (s
->null_termination
) {
2239 path
= quote_path(it
->string
, s
->prefix
, &buf
);
2243 fprintf(s
->fp
, "%c %s%c", prefix
, path
, eol_char
);
2245 strbuf_release(&buf
);
2249 * Print porcelain V2 status.
2252 * [<v2_changed_items>]*
2253 * [<v2_unmerged_items>]*
2254 * [<v2_untracked_items>]*
2255 * [<v2_ignored_items>]*
2258 static void wt_porcelain_v2_print(struct wt_status
*s
)
2260 struct wt_status_change_data
*d
;
2261 struct string_list_item
*it
;
2265 wt_porcelain_v2_print_tracking(s
);
2267 for (i
= 0; i
< s
->change
.nr
; i
++) {
2268 it
= &(s
->change
.items
[i
]);
2271 wt_porcelain_v2_print_changed_entry(it
, s
);
2274 for (i
= 0; i
< s
->change
.nr
; i
++) {
2275 it
= &(s
->change
.items
[i
]);
2278 wt_porcelain_v2_print_unmerged_entry(it
, s
);
2281 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
2282 it
= &(s
->untracked
.items
[i
]);
2283 wt_porcelain_v2_print_other(it
, s
, '?');
2286 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
2287 it
= &(s
->ignored
.items
[i
]);
2288 wt_porcelain_v2_print_other(it
, s
, '!');
2292 void wt_status_print(struct wt_status
*s
)
2294 switch (s
->status_format
) {
2295 case STATUS_FORMAT_SHORT
:
2296 wt_shortstatus_print(s
);
2298 case STATUS_FORMAT_PORCELAIN
:
2299 wt_porcelain_print(s
);
2301 case STATUS_FORMAT_PORCELAIN_V2
:
2302 wt_porcelain_v2_print(s
);
2304 case STATUS_FORMAT_UNSPECIFIED
:
2305 BUG("finalize_deferred_config() should have been called");
2307 case STATUS_FORMAT_NONE
:
2308 case STATUS_FORMAT_LONG
:
2309 wt_longstatus_print(s
);
2315 * Returns 1 if there are unstaged changes, 0 otherwise.
2317 int has_unstaged_changes(struct repository
*r
, int ignore_submodules
)
2319 struct rev_info rev_info
;
2322 repo_init_revisions(r
, &rev_info
, NULL
);
2323 if (ignore_submodules
) {
2324 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2325 rev_info
.diffopt
.flags
.override_submodule_config
= 1;
2327 rev_info
.diffopt
.flags
.quick
= 1;
2328 diff_setup_done(&rev_info
.diffopt
);
2329 result
= run_diff_files(&rev_info
, 0);
2330 return diff_result_code(&rev_info
.diffopt
, result
);
2334 * Returns 1 if there are uncommitted changes, 0 otherwise.
2336 int has_uncommitted_changes(struct repository
*r
,
2337 int ignore_submodules
)
2339 struct rev_info rev_info
;
2342 if (is_index_unborn(r
->index
))
2345 repo_init_revisions(r
, &rev_info
, NULL
);
2346 if (ignore_submodules
)
2347 rev_info
.diffopt
.flags
.ignore_submodules
= 1;
2348 rev_info
.diffopt
.flags
.quick
= 1;
2350 add_head_to_pending(&rev_info
);
2351 if (!rev_info
.pending
.nr
) {
2353 * We have no head (or it's corrupt); use the empty tree,
2354 * which will complain if the index is non-empty.
2356 struct tree
*tree
= lookup_tree(r
, the_hash_algo
->empty_tree
);
2357 add_pending_object(&rev_info
, &tree
->object
, "");
2360 diff_setup_done(&rev_info
.diffopt
);
2361 result
= run_diff_index(&rev_info
, 1);
2362 return diff_result_code(&rev_info
.diffopt
, result
);
2366 * If the work tree has unstaged or uncommitted changes, dies with the
2367 * appropriate message.
2369 int require_clean_work_tree(struct repository
*r
,
2372 int ignore_submodules
,
2375 struct lock_file lock_file
= LOCK_INIT
;
2378 fd
= repo_hold_locked_index(r
, &lock_file
, 0);
2379 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
2381 repo_update_index_if_able(r
, &lock_file
);
2382 rollback_lock_file(&lock_file
);
2384 if (has_unstaged_changes(r
, ignore_submodules
)) {
2385 /* TRANSLATORS: the action is e.g. "pull with rebase" */
2386 error(_("cannot %s: You have unstaged changes."), _(action
));
2390 if (has_uncommitted_changes(r
, ignore_submodules
)) {
2392 error(_("additionally, your index contains uncommitted changes."));
2394 error(_("cannot %s: Your index contains uncommitted changes."),