10 #include "run-command.h"
13 #include "submodule.h"
17 static char default_wt_status_colors
[][COLOR_MAXLEN
] = {
18 GIT_COLOR_NORMAL
, /* WT_STATUS_HEADER */
19 GIT_COLOR_GREEN
, /* WT_STATUS_UPDATED */
20 GIT_COLOR_RED
, /* WT_STATUS_CHANGED */
21 GIT_COLOR_RED
, /* WT_STATUS_UNTRACKED */
22 GIT_COLOR_RED
, /* WT_STATUS_NOBRANCH */
23 GIT_COLOR_RED
, /* WT_STATUS_UNMERGED */
24 GIT_COLOR_GREEN
, /* WT_STATUS_LOCAL_BRANCH */
25 GIT_COLOR_RED
, /* WT_STATUS_REMOTE_BRANCH */
26 GIT_COLOR_NIL
, /* WT_STATUS_ONBRANCH */
29 static const char *color(int slot
, struct wt_status
*s
)
32 if (want_color(s
->use_color
))
33 c
= s
->color_palette
[slot
];
34 if (slot
== WT_STATUS_ONBRANCH
&& color_is_nil(c
))
35 c
= s
->color_palette
[WT_STATUS_HEADER
];
39 static void status_vprintf(struct wt_status
*s
, int at_bol
, const char *color
,
40 const char *fmt
, va_list ap
, const char *trail
)
42 struct strbuf sb
= STRBUF_INIT
;
43 struct strbuf linebuf
= STRBUF_INIT
;
44 const char *line
, *eol
;
46 strbuf_vaddf(&sb
, fmt
, ap
);
48 strbuf_addch(&sb
, '#');
50 strbuf_addch(&sb
, ' ');
51 color_print_strbuf(s
->fp
, color
, &sb
);
53 fprintf(s
->fp
, "%s", trail
);
57 for (line
= sb
.buf
; *line
; line
= eol
+ 1) {
58 eol
= strchr(line
, '\n');
60 strbuf_reset(&linebuf
);
62 strbuf_addch(&linebuf
, '#');
63 if (*line
!= '\n' && *line
!= '\t')
64 strbuf_addch(&linebuf
, ' ');
67 strbuf_add(&linebuf
, line
, eol
- line
);
69 strbuf_addstr(&linebuf
, line
);
70 color_print_strbuf(s
->fp
, color
, &linebuf
);
78 fprintf(s
->fp
, "%s", trail
);
79 strbuf_release(&linebuf
);
83 void status_printf_ln(struct wt_status
*s
, const char *color
,
89 status_vprintf(s
, 1, color
, fmt
, ap
, "\n");
93 void status_printf(struct wt_status
*s
, const char *color
,
99 status_vprintf(s
, 1, color
, fmt
, ap
, NULL
);
103 static void status_printf_more(struct wt_status
*s
, const char *color
,
104 const char *fmt
, ...)
109 status_vprintf(s
, 0, color
, fmt
, ap
, NULL
);
113 void wt_status_prepare(struct wt_status
*s
)
115 unsigned char sha1
[20];
117 memset(s
, 0, sizeof(*s
));
118 memcpy(s
->color_palette
, default_wt_status_colors
,
119 sizeof(default_wt_status_colors
));
120 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
122 s
->relative_paths
= 1;
123 s
->branch
= resolve_refdup("HEAD", sha1
, 0, NULL
);
124 s
->reference
= "HEAD";
126 s
->index_file
= get_index_file();
127 s
->change
.strdup_strings
= 1;
128 s
->untracked
.strdup_strings
= 1;
129 s
->ignored
.strdup_strings
= 1;
132 static void wt_status_print_unmerged_header(struct wt_status
*s
)
135 int del_mod_conflict
= 0;
136 int both_deleted
= 0;
138 const char *c
= color(WT_STATUS_HEADER
, s
);
140 status_printf_ln(s
, c
, _("Unmerged paths:"));
142 for (i
= 0; i
< s
->change
.nr
; i
++) {
143 struct string_list_item
*it
= &(s
->change
.items
[i
]);
144 struct wt_status_change_data
*d
= it
->util
;
146 switch (d
->stagemask
) {
154 del_mod_conflict
= 1;
162 if (!advice_status_hints
)
164 if (s
->whence
!= FROM_COMMIT
)
166 else if (!s
->is_initial
)
167 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
169 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
172 if (!del_mod_conflict
)
173 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to mark resolution)"));
175 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
176 } else if (!del_mod_conflict
&& !not_deleted
) {
177 status_printf_ln(s
, c
, _(" (use \"git rm <file>...\" to mark resolution)"));
179 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" as appropriate to mark resolution)"));
181 status_printf_ln(s
, c
, "");
184 static void wt_status_print_cached_header(struct wt_status
*s
)
186 const char *c
= color(WT_STATUS_HEADER
, s
);
188 status_printf_ln(s
, c
, _("Changes to be committed:"));
189 if (!advice_status_hints
)
191 if (s
->whence
!= FROM_COMMIT
)
192 ; /* NEEDSWORK: use "git reset --unresolve"??? */
193 else if (!s
->is_initial
)
194 status_printf_ln(s
, c
, _(" (use \"git reset %s <file>...\" to unstage)"), s
->reference
);
196 status_printf_ln(s
, c
, _(" (use \"git rm --cached <file>...\" to unstage)"));
197 status_printf_ln(s
, c
, "");
200 static void wt_status_print_dirty_header(struct wt_status
*s
,
202 int has_dirty_submodules
)
204 const char *c
= color(WT_STATUS_HEADER
, s
);
206 status_printf_ln(s
, c
, _("Changes not staged for commit:"));
207 if (!advice_status_hints
)
210 status_printf_ln(s
, c
, _(" (use \"git add <file>...\" to update what will be committed)"));
212 status_printf_ln(s
, c
, _(" (use \"git add/rm <file>...\" to update what will be committed)"));
213 status_printf_ln(s
, c
, _(" (use \"git checkout -- <file>...\" to discard changes in working directory)"));
214 if (has_dirty_submodules
)
215 status_printf_ln(s
, c
, _(" (commit or discard the untracked or modified content in submodules)"));
216 status_printf_ln(s
, c
, "");
219 static void wt_status_print_other_header(struct wt_status
*s
,
223 const char *c
= color(WT_STATUS_HEADER
, s
);
224 status_printf_ln(s
, c
, "%s:", what
);
225 if (!advice_status_hints
)
227 status_printf_ln(s
, c
, _(" (use \"git %s <file>...\" to include in what will be committed)"), how
);
228 status_printf_ln(s
, c
, "");
231 static void wt_status_print_trailer(struct wt_status
*s
)
233 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
236 #define quote_path quote_path_relative
238 static void wt_status_print_unmerged_data(struct wt_status
*s
,
239 struct string_list_item
*it
)
241 const char *c
= color(WT_STATUS_UNMERGED
, s
);
242 struct wt_status_change_data
*d
= it
->util
;
243 struct strbuf onebuf
= STRBUF_INIT
;
244 const char *one
, *how
= _("bug");
246 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
247 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
248 switch (d
->stagemask
) {
249 case 1: how
= _("both deleted:"); break;
250 case 2: how
= _("added by us:"); break;
251 case 3: how
= _("deleted by them:"); break;
252 case 4: how
= _("added by them:"); break;
253 case 5: how
= _("deleted by us:"); break;
254 case 6: how
= _("both added:"); break;
255 case 7: how
= _("both modified:"); break;
257 status_printf_more(s
, c
, "%-20s%s\n", how
, one
);
258 strbuf_release(&onebuf
);
261 static void wt_status_print_change_data(struct wt_status
*s
,
263 struct string_list_item
*it
)
265 struct wt_status_change_data
*d
= it
->util
;
266 const char *c
= color(change_type
, s
);
270 const char *one
, *two
;
271 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
272 struct strbuf extra
= STRBUF_INIT
;
274 one_name
= two_name
= it
->string
;
275 switch (change_type
) {
276 case WT_STATUS_UPDATED
:
277 status
= d
->index_status
;
279 one_name
= d
->head_path
;
281 case WT_STATUS_CHANGED
:
282 if (d
->new_submodule_commits
|| d
->dirty_submodule
) {
283 strbuf_addstr(&extra
, " (");
284 if (d
->new_submodule_commits
)
285 strbuf_addf(&extra
, _("new commits, "));
286 if (d
->dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
287 strbuf_addf(&extra
, _("modified content, "));
288 if (d
->dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
289 strbuf_addf(&extra
, _("untracked content, "));
290 strbuf_setlen(&extra
, extra
.len
- 2);
291 strbuf_addch(&extra
, ')');
293 status
= d
->worktree_status
;
297 one
= quote_path(one_name
, -1, &onebuf
, s
->prefix
);
298 two
= quote_path(two_name
, -1, &twobuf
, s
->prefix
);
300 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
302 case DIFF_STATUS_ADDED
:
303 status_printf_more(s
, c
, _("new file: %s"), one
);
305 case DIFF_STATUS_COPIED
:
306 status_printf_more(s
, c
, _("copied: %s -> %s"), one
, two
);
308 case DIFF_STATUS_DELETED
:
309 status_printf_more(s
, c
, _("deleted: %s"), one
);
311 case DIFF_STATUS_MODIFIED
:
312 status_printf_more(s
, c
, _("modified: %s"), one
);
314 case DIFF_STATUS_RENAMED
:
315 status_printf_more(s
, c
, _("renamed: %s -> %s"), one
, two
);
317 case DIFF_STATUS_TYPE_CHANGED
:
318 status_printf_more(s
, c
, _("typechange: %s"), one
);
320 case DIFF_STATUS_UNKNOWN
:
321 status_printf_more(s
, c
, _("unknown: %s"), one
);
323 case DIFF_STATUS_UNMERGED
:
324 status_printf_more(s
, c
, _("unmerged: %s"), one
);
327 die(_("bug: unhandled diff status %c"), status
);
330 status_printf_more(s
, color(WT_STATUS_HEADER
, s
), "%s", extra
.buf
);
331 strbuf_release(&extra
);
333 status_printf_more(s
, GIT_COLOR_NORMAL
, "\n");
334 strbuf_release(&onebuf
);
335 strbuf_release(&twobuf
);
338 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
339 struct diff_options
*options
,
342 struct wt_status
*s
= data
;
347 s
->workdir_dirty
= 1;
348 for (i
= 0; i
< q
->nr
; i
++) {
349 struct diff_filepair
*p
;
350 struct string_list_item
*it
;
351 struct wt_status_change_data
*d
;
354 it
= string_list_insert(&s
->change
, p
->one
->path
);
357 d
= xcalloc(1, sizeof(*d
));
360 if (!d
->worktree_status
)
361 d
->worktree_status
= p
->status
;
362 d
->dirty_submodule
= p
->two
->dirty_submodule
;
363 if (S_ISGITLINK(p
->two
->mode
))
364 d
->new_submodule_commits
= !!hashcmp(p
->one
->sha1
, p
->two
->sha1
);
368 static int unmerged_mask(const char *path
)
371 struct cache_entry
*ce
;
373 pos
= cache_name_pos(path
, strlen(path
));
379 while (pos
< active_nr
) {
380 ce
= active_cache
[pos
++];
381 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
383 mask
|= (1 << (ce_stage(ce
) - 1));
388 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
389 struct diff_options
*options
,
392 struct wt_status
*s
= data
;
395 for (i
= 0; i
< q
->nr
; i
++) {
396 struct diff_filepair
*p
;
397 struct string_list_item
*it
;
398 struct wt_status_change_data
*d
;
401 it
= string_list_insert(&s
->change
, p
->two
->path
);
404 d
= xcalloc(1, sizeof(*d
));
407 if (!d
->index_status
)
408 d
->index_status
= p
->status
;
410 case DIFF_STATUS_COPIED
:
411 case DIFF_STATUS_RENAMED
:
412 d
->head_path
= xstrdup(p
->one
->path
);
414 case DIFF_STATUS_UNMERGED
:
415 d
->stagemask
= unmerged_mask(p
->two
->path
);
421 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
425 init_revisions(&rev
, NULL
);
426 setup_revisions(0, NULL
, &rev
, NULL
);
427 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
428 DIFF_OPT_SET(&rev
.diffopt
, DIRTY_SUBMODULES
);
429 if (!s
->show_untracked_files
)
430 DIFF_OPT_SET(&rev
.diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
431 if (s
->ignore_submodule_arg
) {
432 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
433 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
435 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
436 rev
.diffopt
.format_callback_data
= s
;
437 init_pathspec(&rev
.prune_data
, s
->pathspec
);
438 run_diff_files(&rev
, 0);
441 static void wt_status_collect_changes_index(struct wt_status
*s
)
444 struct setup_revision_opt opt
;
446 init_revisions(&rev
, NULL
);
447 memset(&opt
, 0, sizeof(opt
));
448 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
449 setup_revisions(0, NULL
, &rev
, &opt
);
451 if (s
->ignore_submodule_arg
) {
452 DIFF_OPT_SET(&rev
.diffopt
, OVERRIDE_SUBMODULE_CONFIG
);
453 handle_ignore_submodules_arg(&rev
.diffopt
, s
->ignore_submodule_arg
);
456 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
457 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
458 rev
.diffopt
.format_callback_data
= s
;
459 rev
.diffopt
.detect_rename
= 1;
460 rev
.diffopt
.rename_limit
= 200;
461 rev
.diffopt
.break_opt
= 0;
462 init_pathspec(&rev
.prune_data
, s
->pathspec
);
463 run_diff_index(&rev
, 1);
466 static void wt_status_collect_changes_initial(struct wt_status
*s
)
468 struct pathspec pathspec
;
471 init_pathspec(&pathspec
, s
->pathspec
);
472 for (i
= 0; i
< active_nr
; i
++) {
473 struct string_list_item
*it
;
474 struct wt_status_change_data
*d
;
475 struct cache_entry
*ce
= active_cache
[i
];
477 if (!ce_path_match(ce
, &pathspec
))
479 it
= string_list_insert(&s
->change
, ce
->name
);
482 d
= xcalloc(1, sizeof(*d
));
486 d
->index_status
= DIFF_STATUS_UNMERGED
;
487 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
490 d
->index_status
= DIFF_STATUS_ADDED
;
492 free_pathspec(&pathspec
);
495 static void wt_status_collect_untracked(struct wt_status
*s
)
498 struct dir_struct dir
;
500 if (!s
->show_untracked_files
)
502 memset(&dir
, 0, sizeof(dir
));
503 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
505 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
506 setup_standard_excludes(&dir
);
508 fill_directory(&dir
, s
->pathspec
);
509 for (i
= 0; i
< dir
.nr
; i
++) {
510 struct dir_entry
*ent
= dir
.entries
[i
];
511 if (cache_name_is_other(ent
->name
, ent
->len
) &&
512 match_pathspec(s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
513 string_list_insert(&s
->untracked
, ent
->name
);
517 if (s
->show_ignored_files
) {
519 dir
.flags
= DIR_SHOW_IGNORED
;
520 if (s
->show_untracked_files
!= SHOW_ALL_UNTRACKED_FILES
)
521 dir
.flags
|= DIR_SHOW_OTHER_DIRECTORIES
;
522 fill_directory(&dir
, s
->pathspec
);
523 for (i
= 0; i
< dir
.nr
; i
++) {
524 struct dir_entry
*ent
= dir
.entries
[i
];
525 if (cache_name_is_other(ent
->name
, ent
->len
) &&
526 match_pathspec(s
->pathspec
, ent
->name
, ent
->len
, 0, NULL
))
527 string_list_insert(&s
->ignored
, ent
->name
);
535 void wt_status_collect(struct wt_status
*s
)
537 wt_status_collect_changes_worktree(s
);
540 wt_status_collect_changes_initial(s
);
542 wt_status_collect_changes_index(s
);
543 wt_status_collect_untracked(s
);
546 static void wt_status_print_unmerged(struct wt_status
*s
)
548 int shown_header
= 0;
551 for (i
= 0; i
< s
->change
.nr
; i
++) {
552 struct wt_status_change_data
*d
;
553 struct string_list_item
*it
;
554 it
= &(s
->change
.items
[i
]);
559 wt_status_print_unmerged_header(s
);
562 wt_status_print_unmerged_data(s
, it
);
565 wt_status_print_trailer(s
);
569 static void wt_status_print_updated(struct wt_status
*s
)
571 int shown_header
= 0;
574 for (i
= 0; i
< s
->change
.nr
; i
++) {
575 struct wt_status_change_data
*d
;
576 struct string_list_item
*it
;
577 it
= &(s
->change
.items
[i
]);
579 if (!d
->index_status
||
580 d
->index_status
== DIFF_STATUS_UNMERGED
)
583 wt_status_print_cached_header(s
);
587 wt_status_print_change_data(s
, WT_STATUS_UPDATED
, it
);
590 wt_status_print_trailer(s
);
596 * 1 : some change but no delete
598 static int wt_status_check_worktree_changes(struct wt_status
*s
,
599 int *dirty_submodules
)
604 *dirty_submodules
= 0;
606 for (i
= 0; i
< s
->change
.nr
; i
++) {
607 struct wt_status_change_data
*d
;
608 d
= s
->change
.items
[i
].util
;
609 if (!d
->worktree_status
||
610 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
614 if (d
->dirty_submodule
)
615 *dirty_submodules
= 1;
616 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
622 static void wt_status_print_changed(struct wt_status
*s
)
624 int i
, dirty_submodules
;
625 int worktree_changes
= wt_status_check_worktree_changes(s
, &dirty_submodules
);
627 if (!worktree_changes
)
630 wt_status_print_dirty_header(s
, worktree_changes
< 0, dirty_submodules
);
632 for (i
= 0; i
< s
->change
.nr
; i
++) {
633 struct wt_status_change_data
*d
;
634 struct string_list_item
*it
;
635 it
= &(s
->change
.items
[i
]);
637 if (!d
->worktree_status
||
638 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
640 wt_status_print_change_data(s
, WT_STATUS_CHANGED
, it
);
642 wt_status_print_trailer(s
);
645 static void wt_status_print_submodule_summary(struct wt_status
*s
, int uncommitted
)
647 struct child_process sm_summary
;
648 char summary_limit
[64];
649 char index
[PATH_MAX
];
650 const char *env
[] = { NULL
, NULL
};
654 argv
[0] = "submodule";
656 argv
[2] = uncommitted
? "--files" : "--cached";
657 argv
[3] = "--for-status";
658 argv
[4] = "--summary-limit";
659 argv
[5] = summary_limit
;
660 argv
[6] = uncommitted
? NULL
: (s
->amend
? "HEAD^" : "HEAD");
663 sprintf(summary_limit
, "%d", s
->submodule_summary
);
664 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
666 memset(&sm_summary
, 0, sizeof(sm_summary
));
667 sm_summary
.argv
= argv
;
668 sm_summary
.env
= env
;
669 sm_summary
.git_cmd
= 1;
670 sm_summary
.no_stdin
= 1;
672 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
673 run_command(&sm_summary
);
676 static void wt_status_print_other(struct wt_status
*s
,
677 struct string_list
*l
,
682 struct strbuf buf
= STRBUF_INIT
;
683 static struct string_list output
= STRING_LIST_INIT_DUP
;
684 struct column_options copts
;
689 wt_status_print_other_header(s
, what
, how
);
691 for (i
= 0; i
< l
->nr
; i
++) {
692 struct string_list_item
*it
;
695 path
= quote_path(it
->string
, strlen(it
->string
),
697 if (column_active(s
->colopts
)) {
698 string_list_append(&output
, path
);
701 status_printf(s
, color(WT_STATUS_HEADER
, s
), "\t");
702 status_printf_more(s
, color(WT_STATUS_UNTRACKED
, s
),
706 strbuf_release(&buf
);
707 if (!column_active(s
->colopts
))
710 strbuf_addf(&buf
, "%s#\t%s",
711 color(WT_STATUS_HEADER
, s
),
712 color(WT_STATUS_UNTRACKED
, s
));
713 memset(&copts
, 0, sizeof(copts
));
715 copts
.indent
= buf
.buf
;
716 if (want_color(s
->use_color
))
717 copts
.nl
= GIT_COLOR_RESET
"\n";
718 print_columns(&output
, s
->colopts
, &copts
);
719 string_list_clear(&output
, 0);
720 strbuf_release(&buf
);
723 static void wt_status_print_verbose(struct wt_status
*s
)
726 struct setup_revision_opt opt
;
728 init_revisions(&rev
, NULL
);
729 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
731 memset(&opt
, 0, sizeof(opt
));
732 opt
.def
= s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
;
733 setup_revisions(0, NULL
, &rev
, &opt
);
735 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
736 rev
.diffopt
.detect_rename
= 1;
737 rev
.diffopt
.file
= s
->fp
;
738 rev
.diffopt
.close_file
= 0;
740 * If we're not going to stdout, then we definitely don't
741 * want color, since we are going to the commit message
742 * file (and even the "auto" setting won't work, since it
743 * will have checked isatty on stdout).
746 rev
.diffopt
.use_color
= 0;
747 run_diff_index(&rev
, 1);
750 static void wt_status_print_tracking(struct wt_status
*s
)
752 struct strbuf sb
= STRBUF_INIT
;
754 struct branch
*branch
;
756 assert(s
->branch
&& !s
->is_initial
);
757 if (prefixcmp(s
->branch
, "refs/heads/"))
759 branch
= branch_get(s
->branch
+ 11);
760 if (!format_tracking_info(branch
, &sb
))
763 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
764 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
),
765 "# %.*s", (int)(ep
- cp
), cp
);
766 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
, s
), "#");
769 static int has_unmerged(struct wt_status
*s
)
773 for (i
= 0; i
< s
->change
.nr
; i
++) {
774 struct wt_status_change_data
*d
;
775 d
= s
->change
.items
[i
].util
;
782 static void show_merge_in_progress(struct wt_status
*s
,
783 struct wt_status_state
*state
,
786 if (has_unmerged(s
)) {
787 status_printf_ln(s
, color
, _("You have unmerged paths."));
788 if (advice_status_hints
)
789 status_printf_ln(s
, color
,
790 _(" (fix conflicts and run \"git commit\")"));
792 status_printf_ln(s
, color
,
793 _("All conflicts fixed but you are still merging."));
794 if (advice_status_hints
)
795 status_printf_ln(s
, color
,
796 _(" (use \"git commit\" to conclude merge)"));
798 wt_status_print_trailer(s
);
801 static void show_am_in_progress(struct wt_status
*s
,
802 struct wt_status_state
*state
,
805 status_printf_ln(s
, color
,
806 _("You are in the middle of an am session."));
807 if (state
->am_empty_patch
)
808 status_printf_ln(s
, color
,
809 _("The current patch is empty."));
810 if (advice_status_hints
) {
811 if (!state
->am_empty_patch
)
812 status_printf_ln(s
, color
,
813 _(" (fix conflicts and then run \"git am --resolved\")"));
814 status_printf_ln(s
, color
,
815 _(" (use \"git am --skip\" to skip this patch)"));
816 status_printf_ln(s
, color
,
817 _(" (use \"git am --abort\" to restore the original branch)"));
819 wt_status_print_trailer(s
);
822 static char *read_line_from_git_path(const char *filename
)
824 struct strbuf buf
= STRBUF_INIT
;
825 FILE *fp
= fopen(git_path("%s", filename
), "r");
827 strbuf_release(&buf
);
830 strbuf_getline(&buf
, fp
, '\n');
832 return strbuf_detach(&buf
, NULL
);
834 strbuf_release(&buf
);
839 static int split_commit_in_progress(struct wt_status
*s
)
841 int split_in_progress
= 0;
842 char *head
= read_line_from_git_path("HEAD");
843 char *orig_head
= read_line_from_git_path("ORIG_HEAD");
844 char *rebase_amend
= read_line_from_git_path("rebase-merge/amend");
845 char *rebase_orig_head
= read_line_from_git_path("rebase-merge/orig-head");
847 if (!head
|| !orig_head
|| !rebase_amend
|| !rebase_orig_head
||
848 !s
->branch
|| strcmp(s
->branch
, "HEAD"))
849 return split_in_progress
;
851 if (!strcmp(rebase_amend
, rebase_orig_head
)) {
852 if (strcmp(head
, rebase_amend
))
853 split_in_progress
= 1;
854 } else if (strcmp(orig_head
, rebase_orig_head
)) {
855 split_in_progress
= 1;
858 if (!s
->amend
&& !s
->nowarn
&& !s
->workdir_dirty
)
859 split_in_progress
= 0;
864 free(rebase_orig_head
);
865 return split_in_progress
;
868 static void show_rebase_in_progress(struct wt_status
*s
,
869 struct wt_status_state
*state
,
874 if (has_unmerged(s
)) {
876 status_printf_ln(s
, color
,
877 _("You are currently rebasing branch '%s' on '%s'."),
881 status_printf_ln(s
, color
,
882 _("You are currently rebasing."));
883 if (advice_status_hints
) {
884 status_printf_ln(s
, color
,
885 _(" (fix conflicts and then run \"git rebase --continue\")"));
886 status_printf_ln(s
, color
,
887 _(" (use \"git rebase --skip\" to skip this patch)"));
888 status_printf_ln(s
, color
,
889 _(" (use \"git rebase --abort\" to check out the original branch)"));
891 } else if (state
->rebase_in_progress
|| !stat(git_path("MERGE_MSG"), &st
)) {
893 status_printf_ln(s
, color
,
894 _("You are currently rebasing branch '%s' on '%s'."),
898 status_printf_ln(s
, color
,
899 _("You are currently rebasing."));
900 if (advice_status_hints
)
901 status_printf_ln(s
, color
,
902 _(" (all conflicts fixed: run \"git rebase --continue\")"));
903 } else if (split_commit_in_progress(s
)) {
905 status_printf_ln(s
, color
,
906 _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
910 status_printf_ln(s
, color
,
911 _("You are currently splitting a commit during a rebase."));
912 if (advice_status_hints
)
913 status_printf_ln(s
, color
,
914 _(" (Once your working directory is clean, run \"git rebase --continue\")"));
917 status_printf_ln(s
, color
,
918 _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
922 status_printf_ln(s
, color
,
923 _("You are currently editing a commit during a rebase."));
924 if (advice_status_hints
&& !s
->amend
) {
925 status_printf_ln(s
, color
,
926 _(" (use \"git commit --amend\" to amend the current commit)"));
927 status_printf_ln(s
, color
,
928 _(" (use \"git rebase --continue\" once you are satisfied with your changes)"));
931 wt_status_print_trailer(s
);
934 static void show_cherry_pick_in_progress(struct wt_status
*s
,
935 struct wt_status_state
*state
,
938 status_printf_ln(s
, color
, _("You are currently cherry-picking."));
939 if (advice_status_hints
) {
941 status_printf_ln(s
, color
,
942 _(" (fix conflicts and run \"git commit\")"));
944 status_printf_ln(s
, color
,
945 _(" (all conflicts fixed: run \"git commit\")"));
947 wt_status_print_trailer(s
);
950 static void show_bisect_in_progress(struct wt_status
*s
,
951 struct wt_status_state
*state
,
955 status_printf_ln(s
, color
,
956 _("You are currently bisecting, started from branch '%s'."),
959 status_printf_ln(s
, color
,
960 _("You are currently bisecting."));
961 if (advice_status_hints
)
962 status_printf_ln(s
, color
,
963 _(" (use \"git bisect reset\" to get back to the original branch)"));
964 wt_status_print_trailer(s
);
968 * Extract branch information from rebase/bisect
970 static char *read_and_strip_branch(const char *path
)
972 struct strbuf sb
= STRBUF_INIT
;
973 unsigned char sha1
[20];
975 if (strbuf_read_file(&sb
, git_path("%s", path
), 0) <= 0)
978 while (&sb
.len
&& sb
.buf
[sb
.len
- 1] == '\n')
979 strbuf_setlen(&sb
, sb
.len
- 1);
982 if (!prefixcmp(sb
.buf
, "refs/heads/"))
983 strbuf_remove(&sb
,0, strlen("refs/heads/"));
984 else if (!prefixcmp(sb
.buf
, "refs/"))
986 else if (!get_sha1_hex(sb
.buf
, sha1
)) {
988 abbrev
= find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
990 strbuf_addstr(&sb
, abbrev
);
991 } else if (!strcmp(sb
.buf
, "detached HEAD")) /* rebase */
995 return strbuf_detach(&sb
, NULL
);
1002 struct grab_1st_switch_cbdata
{
1005 unsigned char nsha1
[20];
1008 static int grab_1st_switch(unsigned char *osha1
, unsigned char *nsha1
,
1009 const char *email
, unsigned long timestamp
, int tz
,
1010 const char *message
, void *cb_data
)
1012 struct grab_1st_switch_cbdata
*cb
= cb_data
;
1013 const char *target
= NULL
, *end
;
1015 if (prefixcmp(message
, "checkout: moving from "))
1017 message
+= strlen("checkout: moving from ");
1018 target
= strstr(message
, " to ");
1021 target
+= strlen(" to ");
1022 strbuf_reset(&cb
->buf
);
1023 hashcpy(cb
->nsha1
, nsha1
);
1024 for (end
= target
; *end
&& *end
!= '\n'; end
++)
1026 strbuf_add(&cb
->buf
, target
, end
- target
);
1031 static void wt_status_get_detached_from(struct wt_status_state
*state
)
1033 struct grab_1st_switch_cbdata cb
;
1034 struct commit
*commit
;
1035 unsigned char sha1
[20];
1038 strbuf_init(&cb
.buf
, 0);
1039 if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch
, &cb
) <= 0) {
1040 strbuf_release(&cb
.buf
);
1044 if (dwim_ref(cb
.buf
.buf
, cb
.buf
.len
, sha1
, &ref
) == 1 &&
1045 /* sha1 is a commit? match without further lookup */
1046 (!hashcmp(cb
.nsha1
, sha1
) ||
1047 /* perhaps sha1 is a tag, try to dereference to a commit */
1048 ((commit
= lookup_commit_reference_gently(sha1
, 1)) != NULL
&&
1049 !hashcmp(cb
.nsha1
, commit
->object
.sha1
)))) {
1051 if (!prefixcmp(ref
, "refs/tags/"))
1052 ofs
= strlen("refs/tags/");
1053 else if (!prefixcmp(ref
, "refs/remotes/"))
1054 ofs
= strlen("refs/remotes/");
1057 state
->detached_from
= xstrdup(ref
+ ofs
);
1059 state
->detached_from
=
1060 xstrdup(find_unique_abbrev(cb
.nsha1
, DEFAULT_ABBREV
));
1061 hashcpy(state
->detached_sha1
, cb
.nsha1
);
1064 strbuf_release(&cb
.buf
);
1067 void wt_status_get_state(struct wt_status_state
*state
,
1068 int get_detached_from
)
1072 if (!stat(git_path("MERGE_HEAD"), &st
)) {
1073 state
->merge_in_progress
= 1;
1074 } else if (!stat(git_path("rebase-apply"), &st
)) {
1075 if (!stat(git_path("rebase-apply/applying"), &st
)) {
1076 state
->am_in_progress
= 1;
1077 if (!stat(git_path("rebase-apply/patch"), &st
) && !st
.st_size
)
1078 state
->am_empty_patch
= 1;
1080 state
->rebase_in_progress
= 1;
1081 state
->branch
= read_and_strip_branch("rebase-apply/head-name");
1082 state
->onto
= read_and_strip_branch("rebase-apply/onto");
1084 } else if (!stat(git_path("rebase-merge"), &st
)) {
1085 if (!stat(git_path("rebase-merge/interactive"), &st
))
1086 state
->rebase_interactive_in_progress
= 1;
1088 state
->rebase_in_progress
= 1;
1089 state
->branch
= read_and_strip_branch("rebase-merge/head-name");
1090 state
->onto
= read_and_strip_branch("rebase-merge/onto");
1091 } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st
)) {
1092 state
->cherry_pick_in_progress
= 1;
1094 if (!stat(git_path("BISECT_LOG"), &st
)) {
1095 state
->bisect_in_progress
= 1;
1096 state
->branch
= read_and_strip_branch("BISECT_START");
1099 if (get_detached_from
)
1100 wt_status_get_detached_from(state
);
1103 static void wt_status_print_state(struct wt_status
*s
,
1104 struct wt_status_state
*state
)
1106 const char *state_color
= color(WT_STATUS_HEADER
, s
);
1107 if (state
->merge_in_progress
)
1108 show_merge_in_progress(s
, state
, state_color
);
1109 else if (state
->am_in_progress
)
1110 show_am_in_progress(s
, state
, state_color
);
1111 else if (state
->rebase_in_progress
|| state
->rebase_interactive_in_progress
)
1112 show_rebase_in_progress(s
, state
, state_color
);
1113 else if (state
->cherry_pick_in_progress
)
1114 show_cherry_pick_in_progress(s
, state
, state_color
);
1115 if (state
->bisect_in_progress
)
1116 show_bisect_in_progress(s
, state
, state_color
);
1119 void wt_status_print(struct wt_status
*s
)
1121 const char *branch_color
= color(WT_STATUS_ONBRANCH
, s
);
1122 const char *branch_status_color
= color(WT_STATUS_HEADER
, s
);
1123 struct wt_status_state state
;
1125 memset(&state
, 0, sizeof(state
));
1126 wt_status_get_state(&state
,
1127 s
->branch
&& !strcmp(s
->branch
, "HEAD"));
1130 const char *on_what
= _("On branch ");
1131 const char *branch_name
= s
->branch
;
1132 if (!prefixcmp(branch_name
, "refs/heads/"))
1134 else if (!strcmp(branch_name
, "HEAD")) {
1135 branch_status_color
= color(WT_STATUS_NOBRANCH
, s
);
1136 if (state
.detached_from
) {
1137 unsigned char sha1
[20];
1138 branch_name
= state
.detached_from
;
1139 if (!get_sha1("HEAD", sha1
) &&
1140 !hashcmp(sha1
, state
.detached_sha1
))
1141 on_what
= _("HEAD detached at ");
1143 on_what
= _("HEAD detached from ");
1146 on_what
= _("Not currently on any branch.");
1149 status_printf(s
, color(WT_STATUS_HEADER
, s
), "");
1150 status_printf_more(s
, branch_status_color
, "%s", on_what
);
1151 status_printf_more(s
, branch_color
, "%s\n", branch_name
);
1153 wt_status_print_tracking(s
);
1156 wt_status_print_state(s
, &state
);
1159 free(state
.detached_from
);
1161 if (s
->is_initial
) {
1162 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1163 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), _("Initial commit"));
1164 status_printf_ln(s
, color(WT_STATUS_HEADER
, s
), "");
1167 wt_status_print_updated(s
);
1168 wt_status_print_unmerged(s
);
1169 wt_status_print_changed(s
);
1170 if (s
->submodule_summary
&&
1171 (!s
->ignore_submodule_arg
||
1172 strcmp(s
->ignore_submodule_arg
, "all"))) {
1173 wt_status_print_submodule_summary(s
, 0); /* staged */
1174 wt_status_print_submodule_summary(s
, 1); /* unstaged */
1176 if (s
->show_untracked_files
) {
1177 wt_status_print_other(s
, &s
->untracked
, _("Untracked files"), "add");
1178 if (s
->show_ignored_files
)
1179 wt_status_print_other(s
, &s
->ignored
, _("Ignored files"), "add -f");
1180 } else if (s
->commitable
)
1181 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("Untracked files not listed%s"),
1183 ? _(" (use -u option to show untracked files)") : "");
1186 wt_status_print_verbose(s
);
1187 if (!s
->commitable
) {
1189 status_printf_ln(s
, GIT_COLOR_NORMAL
, _("No changes"));
1192 else if (s
->workdir_dirty
) {
1193 if (advice_status_hints
)
1194 printf(_("no changes added to commit "
1195 "(use \"git add\" and/or \"git commit -a\")\n"));
1197 printf(_("no changes added to commit\n"));
1198 } else if (s
->untracked
.nr
) {
1199 if (advice_status_hints
)
1200 printf(_("nothing added to commit but untracked files "
1201 "present (use \"git add\" to track)\n"));
1203 printf(_("nothing added to commit but untracked files present\n"));
1204 } else if (s
->is_initial
) {
1205 if (advice_status_hints
)
1206 printf(_("nothing to commit (create/copy files "
1207 "and use \"git add\" to track)\n"));
1209 printf(_("nothing to commit\n"));
1210 } else if (!s
->show_untracked_files
) {
1211 if (advice_status_hints
)
1212 printf(_("nothing to commit (use -u to show untracked files)\n"));
1214 printf(_("nothing to commit\n"));
1216 printf(_("nothing to commit, working directory clean\n"));
1220 static void wt_shortstatus_unmerged(struct string_list_item
*it
,
1221 struct wt_status
*s
)
1223 struct wt_status_change_data
*d
= it
->util
;
1224 const char *how
= "??";
1226 switch (d
->stagemask
) {
1227 case 1: how
= "DD"; break; /* both deleted */
1228 case 2: how
= "AU"; break; /* added by us */
1229 case 3: how
= "UD"; break; /* deleted by them */
1230 case 4: how
= "UA"; break; /* added by them */
1231 case 5: how
= "DU"; break; /* deleted by us */
1232 case 6: how
= "AA"; break; /* both added */
1233 case 7: how
= "UU"; break; /* both modified */
1235 color_fprintf(s
->fp
, color(WT_STATUS_UNMERGED
, s
), "%s", how
);
1236 if (s
->null_termination
) {
1237 fprintf(stdout
, " %s%c", it
->string
, 0);
1239 struct strbuf onebuf
= STRBUF_INIT
;
1241 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1242 printf(" %s\n", one
);
1243 strbuf_release(&onebuf
);
1247 static void wt_shortstatus_status(struct string_list_item
*it
,
1248 struct wt_status
*s
)
1250 struct wt_status_change_data
*d
= it
->util
;
1252 if (d
->index_status
)
1253 color_fprintf(s
->fp
, color(WT_STATUS_UPDATED
, s
), "%c", d
->index_status
);
1256 if (d
->worktree_status
)
1257 color_fprintf(s
->fp
, color(WT_STATUS_CHANGED
, s
), "%c", d
->worktree_status
);
1261 if (s
->null_termination
) {
1262 fprintf(stdout
, "%s%c", it
->string
, 0);
1264 fprintf(stdout
, "%s%c", d
->head_path
, 0);
1266 struct strbuf onebuf
= STRBUF_INIT
;
1269 one
= quote_path(d
->head_path
, -1, &onebuf
, s
->prefix
);
1270 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1272 strbuf_addch(&onebuf
, '"');
1275 printf("%s -> ", one
);
1276 strbuf_release(&onebuf
);
1278 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1279 if (*one
!= '"' && strchr(one
, ' ') != NULL
) {
1281 strbuf_addch(&onebuf
, '"');
1284 printf("%s\n", one
);
1285 strbuf_release(&onebuf
);
1289 static void wt_shortstatus_other(struct string_list_item
*it
,
1290 struct wt_status
*s
, const char *sign
)
1292 if (s
->null_termination
) {
1293 fprintf(stdout
, "%s %s%c", sign
, it
->string
, 0);
1295 struct strbuf onebuf
= STRBUF_INIT
;
1297 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
1298 color_fprintf(s
->fp
, color(WT_STATUS_UNTRACKED
, s
), "%s", sign
);
1299 printf(" %s\n", one
);
1300 strbuf_release(&onebuf
);
1304 static void wt_shortstatus_print_tracking(struct wt_status
*s
)
1306 struct branch
*branch
;
1307 const char *header_color
= color(WT_STATUS_HEADER
, s
);
1308 const char *branch_color_local
= color(WT_STATUS_LOCAL_BRANCH
, s
);
1309 const char *branch_color_remote
= color(WT_STATUS_REMOTE_BRANCH
, s
);
1312 const char *branch_name
;
1313 int num_ours
, num_theirs
;
1315 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
, s
), "## ");
1319 branch_name
= s
->branch
;
1321 if (!prefixcmp(branch_name
, "refs/heads/"))
1323 else if (!strcmp(branch_name
, "HEAD")) {
1324 branch_name
= _("HEAD (no branch)");
1325 branch_color_local
= color(WT_STATUS_NOBRANCH
, s
);
1328 branch
= branch_get(s
->branch
+ 11);
1330 color_fprintf(s
->fp
, header_color
, _("Initial commit on "));
1331 if (!stat_tracking_info(branch
, &num_ours
, &num_theirs
)) {
1332 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1333 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1337 base
= branch
->merge
[0]->dst
;
1338 base
= shorten_unambiguous_ref(base
, 0);
1339 color_fprintf(s
->fp
, branch_color_local
, "%s", branch_name
);
1340 color_fprintf(s
->fp
, header_color
, "...");
1341 color_fprintf(s
->fp
, branch_color_remote
, "%s", base
);
1343 color_fprintf(s
->fp
, header_color
, " [");
1345 color_fprintf(s
->fp
, header_color
, _("behind "));
1346 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1347 } else if (!num_theirs
) {
1348 color_fprintf(s
->fp
, header_color
, _("ahead "));
1349 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1351 color_fprintf(s
->fp
, header_color
, _("ahead "));
1352 color_fprintf(s
->fp
, branch_color_local
, "%d", num_ours
);
1353 color_fprintf(s
->fp
, header_color
, _(", behind "));
1354 color_fprintf(s
->fp
, branch_color_remote
, "%d", num_theirs
);
1357 color_fprintf(s
->fp
, header_color
, "]");
1358 fputc(s
->null_termination
? '\0' : '\n', s
->fp
);
1361 void wt_shortstatus_print(struct wt_status
*s
)
1366 wt_shortstatus_print_tracking(s
);
1368 for (i
= 0; i
< s
->change
.nr
; i
++) {
1369 struct wt_status_change_data
*d
;
1370 struct string_list_item
*it
;
1372 it
= &(s
->change
.items
[i
]);
1375 wt_shortstatus_unmerged(it
, s
);
1377 wt_shortstatus_status(it
, s
);
1379 for (i
= 0; i
< s
->untracked
.nr
; i
++) {
1380 struct string_list_item
*it
;
1382 it
= &(s
->untracked
.items
[i
]);
1383 wt_shortstatus_other(it
, s
, "??");
1385 for (i
= 0; i
< s
->ignored
.nr
; i
++) {
1386 struct string_list_item
*it
;
1388 it
= &(s
->ignored
.items
[i
]);
1389 wt_shortstatus_other(it
, s
, "!!");
1393 void wt_porcelain_print(struct wt_status
*s
)
1396 s
->relative_paths
= 0;
1398 wt_shortstatus_print(s
);