11 #include "run-command.h"
14 #include "submodule.h"
18 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
19 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
20 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
21 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
22 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
23 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
24 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
25 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
26 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
27 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
30 static const char *color(int slot
, struct wt_status
*s
)
33 if (want_color(s
->use_color
))
34 c
= s
->color_palette
[slot
];
35 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
36 c
= s
->color_palette
[WT_STATUS_HEADER
];
40 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
41 const char *fmt
, va_list ap
, const char *trail
)
43 struct strbuf sb
= STRBUF_INIT
;
44 struct strbuf linebuf
= STRBUF_INIT
;
45 const char *line
, *eol
;
47 strbuf_vaddf(&sb
, fmt
, ap
);
49 strbuf_addch(&sb
, comment_line_char
);
51 strbuf_addch(&sb
, ' ');
52 color_print_strbuf(s
->fp
, color
, &sb
);
54 fprintf(s
->fp
, "%s", trail
);
58 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
59 eol
= strchr(line
, '\n');
61 strbuf_reset(&linebuf
);
63 strbuf_addch(&linebuf
, comment_line_char
);
64 if (*line
!= '\n' && *line
!= '\t')
65 strbuf_addch(&linebuf
, ' ');
68 strbuf_add(&linebuf
, line
, eol
- line
);
70 strbuf_addstr(&linebuf
, line
);
71 color_print_strbuf(s
->fp
, color
, &linebuf
);
79 fprintf(s
->fp
, "%s", trail
);
80 strbuf_release(&linebuf
);
84 void status_printf_ln(struct wt_status
*s
, const char *color
,
90 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
94 void status_printf(struct wt_status
*s
, const char *color
,
100 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
104 static void status_printf_more(struct wt_status
*s
, const char *color
,
105 const char *fmt
, ...)
110 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
114 void wt_status_prepare(struct wt_status
*s
)
116 unsigned char sha1
[20];
118 memset(s
, 0, sizeof(*s
));
119 memcpy(s
->color_palette
, default_wt_status_colors
,
120 sizeof(default_wt_status_colors
));
121 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
123 s
->relative_paths
= 1;
124 s
->branch
= resolve_refdup("HEAD", sha1
, 0, NULL
);
125 s
->reference
= "HEAD";
127 s
->index_file
= get_index_file();
128 s
->change
.strdup_strings
= 1;
129 s
->untracked
.strdup_strings
= 1;
130 s
->ignored
.strdup_strings
= 1;
131 s
->show_branch
= -1; /* unspecified */
134 static void wt_status_print_unmerged_header(struct wt_status
*s
)
137 int del_mod_conflict
= 0;
138 int both_deleted
= 0;
140 const char *c
= color(WT_STATUS_HEADER
, s
);
142 status_printf_ln(s
, c
, _("Unmerged paths:"));
144 for (i
= 0; i
< s
->change
.nr
; i
++) {
145 struct string_list_item
*it
= &(s
->change
.items
[i
]);
146 struct wt_status_change_data
*d
= it
->util
;
148 switch (d
->stagemask
) {
156 del_mod_conflict
= 1;
164 if (!advice_status_hints
)
166 if (s
->whence
!= FROM_COMMIT
)
168 else if (!s
->is_initial
)
169 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
171 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
174 if (!del_mod_conflict
)
175 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
177 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
178 } else if (!del_mod_conflict
&& !not_deleted
) {
179 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
181 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
183 status_printf_ln(s
, c
, "");
186 static void wt_status_print_cached_header(struct wt_status
*s
)
188 const char *c
= color(WT_STATUS_HEADER
, s
);
190 status_printf_ln(s
, c
, _("Changes to be committed:"));
191 if (!advice_status_hints
)
193 if (s
->whence
!= FROM_COMMIT
)
194 ; /* NEEDSWORK: use "git reset --unresolve"??? */
195 else if (!s
->is_initial
)
196 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
198 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
199 status_printf_ln(s
, c
, "");
202 static void wt_status_print_dirty_header(struct wt_status
*s
,
204 int has_dirty_submodules
)
206 const char *c
= color(WT_STATUS_HEADER
, s
);
208 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
209 if (!advice_status_hints
)
212 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
214 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
215 status_printf_ln(s
, c
, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
216 if (has_dirty_submodules
)
217 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
218 status_printf_ln(s
, c
, "");
221 static void wt_status_print_other_header(struct wt_status
*s
,
225 const char *c
= color(WT_STATUS_HEADER
, s
);
226 status_printf_ln(s
, c
, "%s:", what
);
227 if (!advice_status_hints
)
229 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
230 status_printf_ln(s
, c
, "");
233 static void wt_status_print_trailer(struct wt_status
*s
)
235 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
238 #define quote_path quote_path_relative
240 static void wt_status_print_unmerged_data(struct wt_status
*s
,
241 struct string_list_item
*it
)
243 const char *c
= color(WT_STATUS_UNMERGED
, s
);
244 struct wt_status_change_data
*d
= it
->util
;
245 struct strbuf onebuf
= STRBUF_INIT
;
246 const char *one
, *how
= _("bug");
248 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
249 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
250 switch (d
->stagemask
) {
251 case 1: how
= _("both deleted:"); break;
252 case 2: how
= _("added by us:"); break;
253 case 3: how
= _("deleted by them:"); break;
254 case 4: how
= _("added by them:"); break;
255 case 5: how
= _("deleted by us:"); break;
256 case 6: how
= _("both added:"); break;
257 case 7: how
= _("both modified:"); break;
259 status_printf_more(s
, c
, "%-20s%s\n", how
, one
);
260 strbuf_release(&onebuf
);
263 static void wt_status_print_change_data(struct wt_status
*s
,
265 struct string_list_item
*it
)
267 struct wt_status_change_data
*d
= it
->util
;
268 const char *c
= color(change_type
, s
);
272 const char *one
, *two
;
273 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
274 struct strbuf extra
= STRBUF_INIT
;
276 one_name
= two_name
= it
->string
;
277 switch (change_type
) {
278 case WT_STATUS_UPDATED
:
279 status
= d
->index_status
;
281 one_name
= d
->head_path
;
283 case WT_STATUS_CHANGED
:
284 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
285 strbuf_addstr(&extra
, " (");
286 if (d
->new_submodule_commits
)
287 strbuf_addf(&extra
, _("new commits, "));
288 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
289 strbuf_addf(&extra
, _("modified content, "));
290 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
291 strbuf_addf(&extra
, _("untracked content, "));
292 strbuf_setlen(&extra
, extra
.len
- 2);
293 strbuf_addch(&extra
, ')');
295 status
= d
->worktree_status
;
298 die("BUG: unhandled change_type %d in wt_status_print_change_data",
302 one
= quote_path(one_name
, -1, &onebuf
, s
->prefix
);
303 two
= quote_path(two_name
, -1, &twobuf
, s
->prefix
);
305 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
307 case DIFF_STATUS_ADDED
:
308 status_printf_more(s
, c
, _("new file: %s"), one
);
310 case DIFF_STATUS_COPIED
:
311 status_printf_more(s
, c
, _("copied: %s -> %s"), one
, two
);
313 case DIFF_STATUS_DELETED
:
314 status_printf_more(s
, c
, _("deleted: %s"), one
);
316 case DIFF_STATUS_MODIFIED
:
317 status_printf_more(s
, c
, _("modified: %s"), one
);
319 case DIFF_STATUS_RENAMED
:
320 status_printf_more(s
, c
, _("renamed: %s -> %s"), one
, two
);
322 case DIFF_STATUS_TYPE_CHANGED
:
323 status_printf_more(s
, c
, _("typechange: %s"), one
);
325 case DIFF_STATUS_UNKNOWN
:
326 status_printf_more(s
, c
, _("unknown: %s"), one
);
328 case DIFF_STATUS_UNMERGED
:
329 status_printf_more(s
, c
, _("unmerged: %s"), one
);
332 die(_("bug: unhandled diff status %c"), status
);
335 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
336 strbuf_release(&extra
);
338 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
339 strbuf_release(&onebuf
);
340 strbuf_release(&twobuf
);
343 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
344 struct diff_options
*options
,
347 struct wt_status
*s
= data
;
352 s
->workdir_dirty
= 1;
353 for (i
= 0; i
< q
->nr
; i
++) {
354 struct diff_filepair
*p
;
355 struct string_list_item
*it
;
356 struct wt_status_change_data
*d
;
359 it
= string_list_insert(&s
->change
, p
->one
->path
);
362 d
= xcalloc(1, sizeof(*d
));
365 if (!d
->worktree_status
)
366 d
->worktree_status
= p
->status
;
367 d
->dirty_submodule
= p
->two
->dirty_submodule
;
368 if (S_ISGITLINK(p
->two
->mode
))
369 d
->new_submodule_commits
= !!hashcmp(p
->one
->sha1
, p
->two
->sha1
);
373 static int unmerged_mask(const char *path
)
376 struct cache_entry
*ce
;
378 pos
= cache_name_pos(path
, strlen(path
));
384 while (pos
< active_nr
) {
385 ce
= active_cache
[pos
++];
386 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
388 mask
|= (1 << (ce_stage(ce
) - 1));
393 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
394 struct diff_options
*options
,
397 struct wt_status
*s
= data
;
400 for (i
= 0; i
< q
->nr
; i
++) {
401 struct diff_filepair
*p
;
402 struct string_list_item
*it
;
403 struct wt_status_change_data
*d
;
406 it
= string_list_insert(&s
->change
, p
->two
->path
);
409 d
= xcalloc(1, sizeof(*d
));
412 if (!d
->index_status
)
413 d
->index_status
= p
->status
;
415 case DIFF_STATUS_COPIED
:
416 case DIFF_STATUS_RENAMED
:
417 d
->head_path
= xstrdup(p
->one
->path
);
419 case DIFF_STATUS_UNMERGED
:
420 d
->stagemask
= unmerged_mask(p
->two
->path
);
426 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
430 init_revisions(&rev
, NULL
);
431 setup_revisions(0, NULL
, &rev
, NULL
);
432 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
433 DIFF_OPT_SET(&rev
.diffopt
, DIRTY_SUBMODULES
);
434 if (!s
->show_untracked_files
)
435 DIFF_OPT_SET(&rev
.diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
436 if (s
->ignore_submodule_arg
) {
437 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
438 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
440 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
441 rev
.diffopt
.format_callback_data
= s
;
442 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
443 run_diff_files(&rev
, 0);
446 static void wt_status_collect_changes_index(struct wt_status
*s
)
449 struct setup_revision_opt opt
;
451 init_revisions(&rev
, NULL
);
452 memset(&opt
, 0, sizeof(opt
));
453 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
454 setup_revisions(0, NULL
, &rev
, &opt
);
456 if (s
->ignore_submodule_arg
) {
457 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
458 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
461 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
462 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
463 rev
.diffopt
.format_callback_data
= s
;
464 rev
.diffopt
.detect_rename
= 1;
465 rev
.diffopt
.rename_limit
= 200;
466 rev
.diffopt
.break_opt
= 0;
467 copy_pathspec(&rev
.prune_data
, &s
->pathspec
);
468 run_diff_index(&rev
, 1);
471 static void wt_status_collect_changes_initial(struct wt_status
*s
)
475 for (i
= 0; i
< active_nr
; i
++) {
476 struct string_list_item
*it
;
477 struct wt_status_change_data
*d
;
478 struct cache_entry
*ce
= active_cache
[i
];
480 if (!ce_path_match(ce
, &s
->pathspec
))
482 it
= string_list_insert(&s
->change
, ce
->name
);
485 d
= xcalloc(1, sizeof(*d
));
489 d
->index_status
= DIFF_STATUS_UNMERGED
;
490 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
493 d
->index_status
= DIFF_STATUS_ADDED
;
497 static void wt_status_collect_untracked(struct wt_status
*s
)
500 struct dir_struct dir
;
501 struct timeval t_begin
;
503 if (!s
->show_untracked_files
)
506 if (advice_status_u_option
)
507 gettimeofday(&t_begin
, NULL
);
509 memset(&dir
, 0, sizeof(dir
));
510 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
512 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
513 if (s
->show_ignored_files
)
514 dir
.flags
|= DIR_SHOW_IGNORED_TOO
;
515 setup_standard_excludes(&dir
);
517 fill_directory(&dir
, &s
->pathspec
);
519 for (i
= 0; i
< dir
.nr
; i
++) {
520 struct dir_entry
*ent
= dir
.entries
[i
];
521 if (cache_name_is_other(ent
->name
, ent
->len
) &&
522 match_pathspec_depth(&s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
523 string_list_insert(&s
->untracked
, ent
->name
);
527 for (i
= 0; i
< dir
.ignored_nr
; i
++) {
528 struct dir_entry
*ent
= dir
.ignored
[i
];
529 if (cache_name_is_other(ent
->name
, ent
->len
) &&
530 match_pathspec_depth(&s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
531 string_list_insert(&s
->ignored
, ent
->name
);
537 clear_directory(&dir
);
539 if (advice_status_u_option
) {
540 struct timeval t_end
;
541 gettimeofday(&t_end
, NULL
);
543 (uint64_t)t_end
.tv_sec
* 1000 + t_end
.tv_usec
/ 1000 -
544 ((uint64_t)t_begin
.tv_sec
* 1000 + t_begin
.tv_usec
/ 1000);
548 void wt_status_collect(struct wt_status
*s
)
550 wt_status_collect_changes_worktree(s
);
553 wt_status_collect_changes_initial(s
);
555 wt_status_collect_changes_index(s
);
556 wt_status_collect_untracked(s
);
559 static void wt_status_print_unmerged(struct wt_status
*s
)
561 int shown_header
= 0;
564 for (i
= 0; i
< s
->change
.nr
; i
++) {
565 struct wt_status_change_data
*d
;
566 struct string_list_item
*it
;
567 it
= &(s
->change
.items
[i
]);
572 wt_status_print_unmerged_header(s
);
575 wt_status_print_unmerged_data(s
, it
);
578 wt_status_print_trailer(s
);
582 static void wt_status_print_updated(struct wt_status
*s
)
584 int shown_header
= 0;
587 for (i
= 0; i
< s
->change
.nr
; i
++) {
588 struct wt_status_change_data
*d
;
589 struct string_list_item
*it
;
590 it
= &(s
->change
.items
[i
]);
592 if (!d
->index_status
||
593 d
->index_status
== DIFF_STATUS_UNMERGED
)
596 wt_status_print_cached_header(s
);
600 wt_status_print_change_data(s
, WT_STATUS_UPDATED
, it
);
603 wt_status_print_trailer(s
);
609 * 1 : some change but no delete
611 static int wt_status_check_worktree_changes(struct wt_status
*s
,
612 int *dirty_submodules
)
617 *dirty_submodules
= 0;
619 for (i
= 0; i
< s
->change
.nr
; i
++) {
620 struct wt_status_change_data
*d
;
621 d
= s
->change
.items
[i
].util
;
622 if (!d
->worktree_status
||
623 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
627 if (d
->dirty_submodule
)
628 *dirty_submodules
= 1;
629 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
635 static void wt_status_print_changed(struct wt_status
*s
)
637 int i
, dirty_submodules
;
638 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
640 if (!worktree_changes
)
643 wt_status_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
645 for (i
= 0; i
< s
->change
.nr
; i
++) {
646 struct wt_status_change_data
*d
;
647 struct string_list_item
*it
;
648 it
= &(s
->change
.items
[i
]);
650 if (!d
->worktree_status
||
651 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
653 wt_status_print_change_data(s
, WT_STATUS_CHANGED
, it
);
655 wt_status_print_trailer(s
);
658 static void wt_status_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
660 struct child_process sm_summary
;
661 char summary_limit
[64];
662 char index
[PATH_MAX
];
663 const char *env
[] = { NULL
, NULL
};
667 argv
[0] = "submodule";
669 argv
[2] = uncommitted
? "--files" : "--cached";
670 argv
[3] = "--for-status";
671 argv
[4] = "--summary-limit";
672 argv
[5] = summary_limit
;
673 argv
[6] = uncommitted
? NULL
: (s
->amend
? "HEAD^" : "HEAD");
676 sprintf(summary_limit
, "%d", s
->submodule_summary
);
677 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
679 memset(&sm_summary
, 0, sizeof(sm_summary
));
680 sm_summary
.argv
= argv
;
681 sm_summary
.env
= env
;
682 sm_summary
.git_cmd
= 1;
683 sm_summary
.no_stdin
= 1;
685 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
686 run_command(&sm_summary
);
689 static void wt_status_print_other(struct wt_status
*s
,
690 struct string_list
*l
,
695 struct strbuf buf
= STRBUF_INIT
;
696 static struct string_list output
= STRING_LIST_INIT_DUP
;
697 struct column_options copts
;
702 wt_status_print_other_header(s
, what
, how
);
704 for (i
= 0; i
< l
->nr
; i
++) {
705 struct string_list_item
*it
;
708 path
= quote_path(it
->string
, strlen(it
->string
),
710 if (column_active(s
->colopts
)) {
711 string_list_append(&output
, path
);
714 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
715 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
719 strbuf_release(&buf
);
720 if (!column_active(s
->colopts
))
723 strbuf_addf(&buf
, "%s#\t%s",
724 color(WT_STATUS_HEADER
, s
),
725 color(WT_STATUS_UNTRACKED
, s
));
726 memset(&copts
, 0, sizeof(copts
));
728 copts
.indent
= buf
.buf
;
729 if (want_color(s
->use_color
))
730 copts
.nl
= GIT_COLOR_RESET
"\n";
731 print_columns(&output
, s
->colopts
, &copts
);
732 string_list_clear(&output
, 0);
733 strbuf_release(&buf
);
736 static void wt_status_print_verbose(struct wt_status
*s
)
739 struct setup_revision_opt opt
;
741 init_revisions(&rev
, NULL
);
742 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
744 memset(&opt
, 0, sizeof(opt
));
745 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
746 setup_revisions(0, NULL
, &rev
, &opt
);
748 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
749 rev
.diffopt
.detect_rename
= 1;
750 rev
.diffopt
.file
= s
->fp
;
751 rev
.diffopt
.close_file
= 0;
753 * If we're not going to stdout, then we definitely don't
754 * want color, since we are going to the commit message
755 * file (and even the "auto" setting won't work, since it
756 * will have checked isatty on stdout).
759 rev
.diffopt
.use_color
= 0;
760 run_diff_index(&rev
, 1);
763 static void wt_status_print_tracking(struct wt_status
*s
)
765 struct strbuf sb
= STRBUF_INIT
;
767 struct branch
*branch
;
769 assert(s
->branch
&& !s
->is_initial
);
770 if (prefixcmp(s
->branch
, "refs/heads/"))
772 branch
= branch_get(s
->branch
+ 11);
773 if (!format_tracking_info(branch
, &sb
))
776 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
777 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
778 "%c %.*s", comment_line_char
,
780 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "%c",
784 static int has_unmerged(struct wt_status
*s
)
788 for (i
= 0; i
< s
->change
.nr
; i
++) {
789 struct wt_status_change_data
*d
;
790 d
= s
->change
.items
[i
].util
;
797 static void show_merge_in_progress(struct wt_status
*s
,
798 struct wt_status_state
*state
,
801 if (has_unmerged(s
)) {
802 status_printf_ln(s
, color
, _("You have unmerged paths."));
803 if (advice_status_hints
)
804 status_printf_ln(s
, color
,
805 _(" (fix conflicts and run \"git commit\")"));
807 status_printf_ln(s
, color
,
808 _("All conflicts fixed but you are still merging."));
809 if (advice_status_hints
)
810 status_printf_ln(s
, color
,
811 _(" (use \"git commit\" to conclude merge)"));
813 wt_status_print_trailer(s
);
816 static void show_am_in_progress(struct wt_status
*s
,
817 struct wt_status_state
*state
,
820 status_printf_ln(s
, color
,
821 _("You are in the middle of an am session."));
822 if (state
->am_empty_patch
)
823 status_printf_ln(s
, color
,
824 _("The current patch is empty."));
825 if (advice_status_hints
) {
826 if (!state
->am_empty_patch
)
827 status_printf_ln(s
, color
,
828 _(" (fix conflicts and then run \"git am --continue\")"));
829 status_printf_ln(s
, color
,
830 _(" (use \"git am --skip\" to skip this patch)"));
831 status_printf_ln(s
, color
,
832 _(" (use \"git am --abort\" to restore the original branch)"));
834 wt_status_print_trailer(s
);
837 static char *read_line_from_git_path(const char *filename
)
839 struct strbuf buf
= STRBUF_INIT
;
840 FILE *fp
= fopen(git_path("%s", filename
), "r");
842 strbuf_release(&buf
);
845 strbuf_getline(&buf
, fp
, '\n');
847 return strbuf_detach(&buf
, NULL
);
849 strbuf_release(&buf
);
854 static int split_commit_in_progress(struct wt_status
*s
)
856 int split_in_progress
= 0;
857 char *head
= read_line_from_git_path("HEAD");
858 char *orig_head
= read_line_from_git_path("ORIG_HEAD");
859 char *rebase_amend
= read_line_from_git_path("rebase-merge/amend");
860 char *rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
862 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
||
863 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
864 return split_in_progress
;
866 if (!strcmp(rebase_amend
, rebase_orig_head
)) {
867 if (strcmp(head
, rebase_amend
))
868 split_in_progress
= 1;
869 } else if (strcmp(orig_head
, rebase_orig_head
)) {
870 split_in_progress
= 1;
873 if (!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
)
874 split_in_progress
= 0;
879 free(rebase_orig_head
);
880 return split_in_progress
;
883 static void show_rebase_in_progress(struct wt_status
*s
,
884 struct wt_status_state
*state
,
889 if (has_unmerged(s
)) {
891 status_printf_ln(s
, color
,
892 _("You are currently rebasing branch '%s' on '%s'."),
896 status_printf_ln(s
, color
,
897 _("You are currently rebasing."));
898 if (advice_status_hints
) {
899 status_printf_ln(s
, color
,
900 _(" (fix conflicts and then run \"git rebase --continue\")"));
901 status_printf_ln(s
, color
,
902 _(" (use \"git rebase --skip\" to skip this patch)"));
903 status_printf_ln(s
, color
,
904 _(" (use \"git rebase --abort\" to check out the original branch)"));
906 } else if (state
->rebase_in_progress
|| !stat(git_path("MERGE_MSG"), &st
)) {
908 status_printf_ln(s
, color
,
909 _("You are currently rebasing branch '%s' on '%s'."),
913 status_printf_ln(s
, color
,
914 _("You are currently rebasing."));
915 if (advice_status_hints
)
916 status_printf_ln(s
, color
,
917 _(" (all conflicts fixed: run \"git rebase --continue\")"));
918 } else if (split_commit_in_progress(s
)) {
920 status_printf_ln(s
, color
,
921 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
925 status_printf_ln(s
, color
,
926 _("You are currently splitting a commit during a rebase."));
927 if (advice_status_hints
)
928 status_printf_ln(s
, color
,
929 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
932 status_printf_ln(s
, color
,
933 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
937 status_printf_ln(s
, color
,
938 _("You are currently editing a commit during a rebase."));
939 if (advice_status_hints
&& !s
->amend
) {
940 status_printf_ln(s
, color
,
941 _(" (use \"git commit --amend\" to amend the current commit)"));
942 status_printf_ln(s
, color
,
943 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
946 wt_status_print_trailer(s
);
949 static void show_cherry_pick_in_progress(struct wt_status
*s
,
950 struct wt_status_state
*state
,
953 status_printf_ln(s
, color
, _("You are currently cherry-picking."));
954 if (advice_status_hints
) {
956 status_printf_ln(s
, color
,
957 _(" (fix conflicts and run \"git cherry-pick --continue\")"));
959 status_printf_ln(s
, color
,
960 _(" (all conflicts fixed: run \"git cherry-pick --continue\")"));
961 status_printf_ln(s
, color
,
962 _(" (use \"git cherry-pick --abort\" to cancel the cherry-pick operation)"));
964 wt_status_print_trailer(s
);
967 static void show_revert_in_progress(struct wt_status
*s
,
968 struct wt_status_state
*state
,
971 status_printf_ln(s
, color
, _("You are currently reverting commit %s."),
972 find_unique_abbrev(state
->revert_head_sha1
, DEFAULT_ABBREV
));
973 if (advice_status_hints
) {
975 status_printf_ln(s
, color
,
976 _(" (fix conflicts and run \"git revert --continue\")"));
978 status_printf_ln(s
, color
,
979 _(" (all conflicts fixed: run \"git revert --continue\")"));
980 status_printf_ln(s
, color
,
981 _(" (use \"git revert --abort\" to cancel the revert operation)"));
983 wt_status_print_trailer(s
);
986 static void show_bisect_in_progress(struct wt_status
*s
,
987 struct wt_status_state
*state
,
991 status_printf_ln(s
, color
,
992 _("You are currently bisecting, started from branch '%s'."),
995 status_printf_ln(s
, color
,
996 _("You are currently bisecting."));
997 if (advice_status_hints
)
998 status_printf_ln(s
, color
,
999 _(" (use \"git bisect reset\" to get back to the original branch)"));
1000 wt_status_print_trailer(s
);
1004 * Extract branch information from rebase/bisect
1006 static char *read_and_strip_branch(const char *path
)
1008 struct strbuf sb
= STRBUF_INIT
;
1009 unsigned char sha1
[20];
1011 if (strbuf_read_file(&sb
, git_path("%s", path
), 0) <= 0)
1014 while (&sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
1015 strbuf_setlen(&sb
, sb
.len
- 1);
1018 if (!prefixcmp(sb
.buf
, "refs/heads/"))
1019 strbuf_remove(&sb
,0, strlen("refs/heads/"));
1020 else if (!prefixcmp(sb
.buf
, "refs/"))
1022 else if (!get_sha1_hex(sb
.buf
, sha1
)) {
1024 abbrev
= find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
1026 strbuf_addstr(&sb
, abbrev
);
1027 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
1031 return strbuf_detach(&sb
, NULL
);
1034 strbuf_release(&sb
);
1038 struct grab_1st_switch_cbdata
{
1040 unsigned char nsha1
[20];
1043 static int grab_1st_switch(unsigned char *osha1
, unsigned char *nsha1
,
1044 const char *email
, unsigned long timestamp
, int tz
,
1045 const char *message
, void *cb_data
)
1047 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1048 const char *target
= NULL
, *end
;
1050 if (prefixcmp(message
, "checkout: moving from "))
1052 message
+= strlen("checkout: moving from ");
1053 target
= strstr(message
, " to ");
1056 target
+= strlen(" to ");
1057 strbuf_reset(&cb
->buf
);
1058 hashcpy(cb
->nsha1
, nsha1
);
1059 for (end
= target
; *end
&& *end
!= '\n'; end
++)
1061 strbuf_add(&cb
->buf
, target
, end
- target
);
1065 static void wt_status_get_detached_from(struct wt_status_state
*state
)
1067 struct grab_1st_switch_cbdata cb
;
1068 struct commit
*commit
;
1069 unsigned char sha1
[20];
1072 strbuf_init(&cb
.buf
, 0);
1073 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1074 strbuf_release(&cb
.buf
);
1078 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, sha1
, &ref
) == 1 &&
1079 /* sha1 is a commit? match without further lookup */
1080 (!hashcmp(cb
.nsha1
, sha1
) ||
1081 /* perhaps sha1 is a tag, try to dereference to a commit */
1082 ((commit
= lookup_commit_reference_gently(sha1
, 1)) != NULL
&&
1083 !hashcmp(cb
.nsha1
, commit
->object
.sha1
)))) {
1085 if (!prefixcmp(ref
, "refs/tags/"))
1086 ofs
= strlen("refs/tags/");
1087 else if (!prefixcmp(ref
, "refs/remotes/"))
1088 ofs
= strlen("refs/remotes/");
1091 state
->detached_from
= xstrdup(ref
+ ofs
);
1093 state
->detached_from
=
1094 xstrdup(find_unique_abbrev(cb
.nsha1
, DEFAULT_ABBREV
));
1095 hashcpy(state
->detached_sha1
, cb
.nsha1
);
1098 strbuf_release(&cb
.buf
);
1101 void wt_status_get_state(struct wt_status_state
*state
,
1102 int get_detached_from
)
1105 unsigned char sha1
[20];
1107 if (!stat(git_path("MERGE_HEAD"), &st
)) {
1108 state
->merge_in_progress
= 1;
1109 } else if (!stat(git_path("rebase-apply"), &st
)) {
1110 if (!stat(git_path("rebase-apply/applying"), &st
)) {
1111 state
->am_in_progress
= 1;
1112 if (!stat(git_path("rebase-apply/patch"), &st
) && !st
.st_size
)
1113 state
->am_empty_patch
= 1;
1115 state
->rebase_in_progress
= 1;
1116 state
->branch
= read_and_strip_branch("rebase-apply/head-name");
1117 state
->onto
= read_and_strip_branch("rebase-apply/onto");
1119 } else if (!stat(git_path("rebase-merge"), &st
)) {
1120 if (!stat(git_path("rebase-merge/interactive"), &st
))
1121 state
->rebase_interactive_in_progress
= 1;
1123 state
->rebase_in_progress
= 1;
1124 state
->branch
= read_and_strip_branch("rebase-merge/head-name");
1125 state
->onto
= read_and_strip_branch("rebase-merge/onto");
1126 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st
)) {
1127 state
->cherry_pick_in_progress
= 1;
1129 if (!stat(git_path("BISECT_LOG"), &st
)) {
1130 state
->bisect_in_progress
= 1;
1131 state
->branch
= read_and_strip_branch("BISECT_START");
1133 if (!stat(git_path("REVERT_HEAD"), &st
) &&
1134 !get_sha1("REVERT_HEAD", sha1
)) {
1135 state
->revert_in_progress
= 1;
1136 hashcpy(state
->revert_head_sha1
, sha1
);
1139 if (get_detached_from
)
1140 wt_status_get_detached_from(state
);
1143 static void wt_status_print_state(struct wt_status
*s
,
1144 struct wt_status_state
*state
)
1146 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1147 if (state
->merge_in_progress
)
1148 show_merge_in_progress(s
, state
, state_color
);
1149 else if (state
->am_in_progress
)
1150 show_am_in_progress(s
, state
, state_color
);
1151 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1152 show_rebase_in_progress(s
, state
, state_color
);
1153 else if (state
->cherry_pick_in_progress
)
1154 show_cherry_pick_in_progress(s
, state
, state_color
);
1155 else if (state
->revert_in_progress
)
1156 show_revert_in_progress(s
, state
, state_color
);
1157 if (state
->bisect_in_progress
)
1158 show_bisect_in_progress(s
, state
, state_color
);
1161 void wt_status_print(struct wt_status
*s
)
1163 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1164 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1165 struct wt_status_state state
;
1167 memset(&state
, 0, sizeof(state
));
1168 wt_status_get_state(&state
,
1169 s
->branch
&& !strcmp(s
->branch
, "HEAD"));
1172 const char *on_what
= _("On branch ");
1173 const char *branch_name
= s
->branch
;
1174 if (!prefixcmp(branch_name
, "refs/heads/"))
1176 else if (!strcmp(branch_name
, "HEAD")) {
1177 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1178 if (state
.rebase_in_progress
|| state
.rebase_interactive_in_progress
) {
1179 on_what
= _("rebase in progress; onto ");
1180 branch_name
= state
.onto
;
1181 } else if (state
.detached_from
) {
1182 unsigned char sha1
[20];
1183 branch_name
= state
.detached_from
;
1184 if (!get_sha1("HEAD", sha1
) &&
1185 !hashcmp(sha1
, state
.detached_sha1
))
1186 on_what
= _("HEAD detached at ");
1188 on_what
= _("HEAD detached from ");
1191 on_what
= _("Not currently on any branch.");
1194 status_printf(s
, color(WT_STATUS_HEADER
, s
), "");
1195 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1196 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1198 wt_status_print_tracking(s
);
1201 wt_status_print_state(s
, &state
);
1204 free(state
.detached_from
);
1206 if (s
->is_initial
) {
1207 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1208 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), _("Initial commit"));
1209 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1212 wt_status_print_updated(s
);
1213 wt_status_print_unmerged(s
);
1214 wt_status_print_changed(s
);
1215 if (s
->submodule_summary
&&
1216 (!s
->ignore_submodule_arg
||
1217 strcmp(s
->ignore_submodule_arg
, "all"))) {
1218 wt_status_print_submodule_summary(s
, 0); /* staged */
1219 wt_status_print_submodule_summary(s
, 1); /* unstaged */
1221 if (s
->show_untracked_files
) {
1222 wt_status_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1223 if (s
->show_ignored_files
)
1224 wt_status_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1225 if (advice_status_u_option
&& 2000 < s
->untracked_in_ms
) {
1226 status_printf_ln(s
, GIT_COLOR_NORMAL
, "");
1227 status_printf_ln(s
, GIT_COLOR_NORMAL
,
1228 _("It took %.2f seconds to enumerate untracked files. 'status -uno'\n"
1229 "may speed it up, but you have to be careful not to forget to add\n"
1230 "new files yourself (see 'git help status')."),
1231 s
->untracked_in_ms
/ 1000.0);
1233 } else if (s
->commitable
)
1234 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1236 ? _(" (use -u option to show untracked files)") : "");
1239 wt_status_print_verbose(s
);
1240 if (!s
->commitable
) {
1242 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1245 else if (s
->workdir_dirty
) {
1246 if (advice_status_hints
)
1247 printf(_("no changes added to commit "
1248 "(use \"git add\" and/or \"git commit -a\")\n"));
1250 printf(_("no changes added to commit\n"));
1251 } else if (s
->untracked
.nr
) {
1252 if (advice_status_hints
)
1253 printf(_("nothing added to commit but untracked files "
1254 "present (use \"git add\" to track)\n"));
1256 printf(_("nothing added to commit but untracked files present\n"));
1257 } else if (s
->is_initial
) {
1258 if (advice_status_hints
)
1259 printf(_("nothing to commit (create/copy files "
1260 "and use \"git add\" to track)\n"));
1262 printf(_("nothing to commit\n"));
1263 } else if (!s
->show_untracked_files
) {
1264 if (advice_status_hints
)
1265 printf(_("nothing to commit (use -u to show untracked files)\n"));
1267 printf(_("nothing to commit\n"));
1269 printf(_("nothing to commit, working directory clean\n"));
1273 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1274 struct wt_status
*s
)
1276 struct wt_status_change_data
*d
= it
->util
;
1277 const char *how
= "??";
1279 switch (d
->stagemask
) {
1280 case 1: how
= "DD"; break; /* both deleted */
1281 case 2: how
= "AU"; break; /* added by us */
1282 case 3: how
= "UD"; break; /* deleted by them */
1283 case 4: how
= "UA"; break; /* added by them */
1284 case 5: how
= "DU"; break; /* deleted by us */
1285 case 6: how
= "AA"; break; /* both added */
1286 case 7: how
= "UU"; break; /* both modified */
1288 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1289 if (s
->null_termination
) {
1290 fprintf(stdout
, " %s%c", it
->string
, 0);
1292 struct strbuf onebuf
= STRBUF_INIT
;
1294 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1295 printf(" %s\n", one
);
1296 strbuf_release(&onebuf
);
1300 static void wt_shortstatus_status(struct string_list_item
*it
,
1301 struct wt_status
*s
)
1303 struct wt_status_change_data
*d
= it
->util
;
1305 if (d
->index_status
)
1306 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1309 if (d
->worktree_status
)
1310 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1314 if (s
->null_termination
) {
1315 fprintf(stdout
, "%s%c", it
->string
, 0);
1317 fprintf(stdout
, "%s%c", d
->head_path
, 0);
1319 struct strbuf onebuf
= STRBUF_INIT
;
1322 one
= quote_path(d
->head_path
, -1, &onebuf
, s
->prefix
);
1323 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1325 strbuf_addch(&onebuf
, '"');
1328 printf("%s -> ", one
);
1329 strbuf_release(&onebuf
);
1331 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1332 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1334 strbuf_addch(&onebuf
, '"');
1337 printf("%s\n", one
);
1338 strbuf_release(&onebuf
);
1342 static void wt_shortstatus_other(struct string_list_item
*it
,
1343 struct wt_status
*s
, const char *sign
)
1345 if (s
->null_termination
) {
1346 fprintf(stdout
, "%s %s%c", sign
, it
->string
, 0);
1348 struct strbuf onebuf
= STRBUF_INIT
;
1350 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1351 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
1352 printf(" %s\n", one
);
1353 strbuf_release(&onebuf
);
1357 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
1359 struct branch
*branch
;
1360 const char *header_color
= color(WT_STATUS_HEADER
, s
);
1361 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
1362 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
1365 const char *branch_name
;
1366 int num_ours
, num_theirs
;
1368 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
1372 branch_name
= s
->branch
;
1374 if (!prefixcmp(branch_name
, "refs/heads/"))
1376 else if (!strcmp(branch_name
, "HEAD")) {
1377 branch_name
= _("HEAD (no branch)");
1378 branch_color_local
= color(WT_STATUS_NOBRANCH
, s
);
1381 branch
= branch_get(s
->branch
+ 11);
1383 color_fprintf(s
->fp
, header_color
, _("Initial commit on "));
1384 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
)) {
1385 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1386 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1390 base
= branch
->merge
[0]->dst
;
1391 base
= shorten_unambiguous_ref(base
, 0);
1392 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1393 color_fprintf(s
->fp
, header_color
, "...");
1394 color_fprintf(s
->fp
, branch_color_remote
, "%s", base
);
1396 color_fprintf(s
->fp
, header_color
, " [");
1398 color_fprintf(s
->fp
, header_color
, _("behind "));
1399 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1400 } else if (!num_theirs
) {
1401 color_fprintf(s
->fp
, header_color
, _("ahead "));
1402 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1404 color_fprintf(s
->fp
, header_color
, _("ahead "));
1405 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1406 color_fprintf(s
->fp
, header_color
, _(", behind "));
1407 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1410 color_fprintf(s
->fp
, header_color
, "]");
1411 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1414 void wt_shortstatus_print(struct wt_status
*s
)
1419 wt_shortstatus_print_tracking(s
);
1421 for (i
= 0; i
< s
->change
.nr
; i
++) {
1422 struct wt_status_change_data
*d
;
1423 struct string_list_item
*it
;
1425 it
= &(s
->change
.items
[i
]);
1428 wt_shortstatus_unmerged(it
, s
);
1430 wt_shortstatus_status(it
, s
);
1432 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
1433 struct string_list_item
*it
;
1435 it
= &(s
->untracked
.items
[i
]);
1436 wt_shortstatus_other(it
, s
, "??");
1438 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
1439 struct string_list_item
*it
;
1441 it
= &(s
->ignored
.items
[i
]);
1442 wt_shortstatus_other(it
, s
, "!!");
1446 void wt_porcelain_print(struct wt_status
*s
)
1449 s
->relative_paths
= 0;
1451 wt_shortstatus_print(s
);