11 #include "run-command.h"
14 int wt_status_relative_paths
= 1;
15 int wt_status_use_color
= -1;
16 static int wt_status_submodule_summary
;
17 static char 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 */
26 enum untracked_status_type show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
28 static int parse_status_slot(const char *var
, int offset
)
30 if (!strcasecmp(var
+offset
, "header"))
31 return WT_STATUS_HEADER
;
32 if (!strcasecmp(var
+offset
, "updated")
33 || !strcasecmp(var
+offset
, "added"))
34 return WT_STATUS_UPDATED
;
35 if (!strcasecmp(var
+offset
, "changed"))
36 return WT_STATUS_CHANGED
;
37 if (!strcasecmp(var
+offset
, "untracked"))
38 return WT_STATUS_UNTRACKED
;
39 if (!strcasecmp(var
+offset
, "nobranch"))
40 return WT_STATUS_NOBRANCH
;
41 if (!strcasecmp(var
+offset
, "unmerged"))
42 return WT_STATUS_UNMERGED
;
43 die("bad config variable '%s'", var
);
46 static const char *color(int slot
)
48 return wt_status_use_color
> 0 ? wt_status_colors
[slot
] : "";
51 void wt_status_prepare(struct wt_status
*s
)
53 unsigned char sha1
[20];
56 memset(s
, 0, sizeof(*s
));
57 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
58 s
->branch
= head
? xstrdup(head
) : NULL
;
59 s
->reference
= "HEAD";
61 s
->index_file
= get_index_file();
62 s
->change
.strdup_strings
= 1;
65 static void wt_status_print_unmerged_header(struct wt_status
*s
)
67 const char *c
= color(WT_STATUS_HEADER
);
68 color_fprintf_ln(s
->fp
, c
, "# Unmerged paths:");
70 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
72 color_fprintf_ln(s
->fp
, c
, "# (use \"git rm --cached <file>...\" to unstage)");
73 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to mark resolution)");
74 color_fprintf_ln(s
->fp
, c
, "#");
77 static void wt_status_print_cached_header(struct wt_status
*s
)
79 const char *c
= color(WT_STATUS_HEADER
);
80 color_fprintf_ln(s
->fp
, c
, "# Changes to be committed:");
82 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
84 color_fprintf_ln(s
->fp
, c
, "# (use \"git rm --cached <file>...\" to unstage)");
86 color_fprintf_ln(s
->fp
, c
, "#");
89 static void wt_status_print_dirty_header(struct wt_status
*s
,
92 const char *c
= color(WT_STATUS_HEADER
);
93 color_fprintf_ln(s
->fp
, c
, "# Changed but not updated:");
95 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to update what will be committed)");
97 color_fprintf_ln(s
->fp
, c
, "# (use \"git add/rm <file>...\" to update what will be committed)");
98 color_fprintf_ln(s
->fp
, c
, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
99 color_fprintf_ln(s
->fp
, c
, "#");
102 static void wt_status_print_untracked_header(struct wt_status
*s
)
104 const char *c
= color(WT_STATUS_HEADER
);
105 color_fprintf_ln(s
->fp
, c
, "# Untracked files:");
106 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to include in what will be committed)");
107 color_fprintf_ln(s
->fp
, c
, "#");
110 static void wt_status_print_trailer(struct wt_status
*s
)
112 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
115 #define quote_path quote_path_relative
117 static void wt_status_print_unmerged_data(struct wt_status
*s
,
118 struct string_list_item
*it
)
120 const char *c
= color(WT_STATUS_UNMERGED
);
121 struct wt_status_change_data
*d
= it
->util
;
122 struct strbuf onebuf
= STRBUF_INIT
;
123 const char *one
, *how
= "bug";
125 one
= quote_path(it
->string
, -1, &onebuf
, s
->prefix
);
126 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
127 switch (d
->stagemask
) {
128 case 1: how
= "both deleted:"; break;
129 case 2: how
= "added by us:"; break;
130 case 3: how
= "deleted by them:"; break;
131 case 4: how
= "added by them:"; break;
132 case 5: how
= "deleted by us:"; break;
133 case 6: how
= "both added:"; break;
134 case 7: how
= "both modified:"; break;
136 color_fprintf(s
->fp
, c
, "%-20s%s\n", how
, one
);
137 strbuf_release(&onebuf
);
140 static void wt_status_print_change_data(struct wt_status
*s
,
142 struct string_list_item
*it
)
144 struct wt_status_change_data
*d
= it
->util
;
145 const char *c
= color(change_type
);
149 const char *one
, *two
;
150 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
152 one_name
= two_name
= it
->string
;
153 switch (change_type
) {
154 case WT_STATUS_UPDATED
:
155 status
= d
->index_status
;
157 one_name
= d
->head_path
;
159 case WT_STATUS_CHANGED
:
160 status
= d
->worktree_status
;
164 one
= quote_path(one_name
, -1, &onebuf
, s
->prefix
);
165 two
= quote_path(two_name
, -1, &twobuf
, s
->prefix
);
167 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
169 case DIFF_STATUS_ADDED
:
170 color_fprintf(s
->fp
, c
, "new file: %s", one
);
172 case DIFF_STATUS_COPIED
:
173 color_fprintf(s
->fp
, c
, "copied: %s -> %s", one
, two
);
175 case DIFF_STATUS_DELETED
:
176 color_fprintf(s
->fp
, c
, "deleted: %s", one
);
178 case DIFF_STATUS_MODIFIED
:
179 color_fprintf(s
->fp
, c
, "modified: %s", one
);
181 case DIFF_STATUS_RENAMED
:
182 color_fprintf(s
->fp
, c
, "renamed: %s -> %s", one
, two
);
184 case DIFF_STATUS_TYPE_CHANGED
:
185 color_fprintf(s
->fp
, c
, "typechange: %s", one
);
187 case DIFF_STATUS_UNKNOWN
:
188 color_fprintf(s
->fp
, c
, "unknown: %s", one
);
190 case DIFF_STATUS_UNMERGED
:
191 color_fprintf(s
->fp
, c
, "unmerged: %s", one
);
194 die("bug: unhandled diff status %c", status
);
196 fprintf(s
->fp
, "\n");
197 strbuf_release(&onebuf
);
198 strbuf_release(&twobuf
);
201 static void wt_status_collect_changed_cb(struct diff_queue_struct
*q
,
202 struct diff_options
*options
,
205 struct wt_status
*s
= data
;
210 s
->workdir_dirty
= 1;
211 for (i
= 0; i
< q
->nr
; i
++) {
212 struct diff_filepair
*p
;
213 struct string_list_item
*it
;
214 struct wt_status_change_data
*d
;
217 it
= string_list_insert(p
->one
->path
, &s
->change
);
220 d
= xcalloc(1, sizeof(*d
));
223 if (!d
->worktree_status
)
224 d
->worktree_status
= p
->status
;
228 static int unmerged_mask(const char *path
)
231 struct cache_entry
*ce
;
233 pos
= cache_name_pos(path
, strlen(path
));
239 while (pos
< active_nr
) {
240 ce
= active_cache
[pos
++];
241 if (strcmp(ce
->name
, path
) || !ce_stage(ce
))
243 mask
|= (1 << (ce_stage(ce
) - 1));
248 static void wt_status_collect_updated_cb(struct diff_queue_struct
*q
,
249 struct diff_options
*options
,
252 struct wt_status
*s
= data
;
255 for (i
= 0; i
< q
->nr
; i
++) {
256 struct diff_filepair
*p
;
257 struct string_list_item
*it
;
258 struct wt_status_change_data
*d
;
261 it
= string_list_insert(p
->two
->path
, &s
->change
);
264 d
= xcalloc(1, sizeof(*d
));
267 if (!d
->index_status
)
268 d
->index_status
= p
->status
;
270 case DIFF_STATUS_COPIED
:
271 case DIFF_STATUS_RENAMED
:
272 d
->head_path
= xstrdup(p
->one
->path
);
274 case DIFF_STATUS_UNMERGED
:
275 d
->stagemask
= unmerged_mask(p
->two
->path
);
281 static void wt_status_collect_changes_worktree(struct wt_status
*s
)
285 init_revisions(&rev
, NULL
);
286 setup_revisions(0, NULL
, &rev
, NULL
);
287 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
288 rev
.diffopt
.format_callback
= wt_status_collect_changed_cb
;
289 rev
.diffopt
.format_callback_data
= s
;
290 run_diff_files(&rev
, 0);
293 static void wt_status_collect_changes_index(struct wt_status
*s
)
297 init_revisions(&rev
, NULL
);
298 setup_revisions(0, NULL
, &rev
,
299 s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
);
300 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
301 rev
.diffopt
.format_callback
= wt_status_collect_updated_cb
;
302 rev
.diffopt
.format_callback_data
= s
;
303 rev
.diffopt
.detect_rename
= 1;
304 rev
.diffopt
.rename_limit
= 200;
305 rev
.diffopt
.break_opt
= 0;
306 run_diff_index(&rev
, 1);
309 static void wt_status_collect_changes_initial(struct wt_status
*s
)
313 for (i
= 0; i
< active_nr
; i
++) {
314 struct string_list_item
*it
;
315 struct wt_status_change_data
*d
;
316 struct cache_entry
*ce
= active_cache
[i
];
318 it
= string_list_insert(ce
->name
, &s
->change
);
321 d
= xcalloc(1, sizeof(*d
));
325 d
->index_status
= DIFF_STATUS_UNMERGED
;
326 d
->stagemask
|= (1 << (ce_stage(ce
) - 1));
329 d
->index_status
= DIFF_STATUS_ADDED
;
333 void wt_status_collect_changes(struct wt_status
*s
)
335 wt_status_collect_changes_worktree(s
);
338 wt_status_collect_changes_initial(s
);
340 wt_status_collect_changes_index(s
);
343 static void wt_status_print_unmerged(struct wt_status
*s
)
345 int shown_header
= 0;
348 for (i
= 0; i
< s
->change
.nr
; i
++) {
349 struct wt_status_change_data
*d
;
350 struct string_list_item
*it
;
351 it
= &(s
->change
.items
[i
]);
356 wt_status_print_unmerged_header(s
);
359 wt_status_print_unmerged_data(s
, it
);
362 wt_status_print_trailer(s
);
366 static void wt_status_print_updated(struct wt_status
*s
)
368 int shown_header
= 0;
371 for (i
= 0; i
< s
->change
.nr
; i
++) {
372 struct wt_status_change_data
*d
;
373 struct string_list_item
*it
;
374 it
= &(s
->change
.items
[i
]);
376 if (!d
->index_status
||
377 d
->index_status
== DIFF_STATUS_UNMERGED
)
380 wt_status_print_cached_header(s
);
384 wt_status_print_change_data(s
, WT_STATUS_UPDATED
, it
);
387 wt_status_print_trailer(s
);
393 * 1 : some change but no delete
395 static int wt_status_check_worktree_changes(struct wt_status
*s
)
400 for (i
= 0; i
< s
->change
.nr
; i
++) {
401 struct wt_status_change_data
*d
;
402 d
= s
->change
.items
[i
].util
;
403 if (!d
->worktree_status
||
404 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
407 if (d
->worktree_status
== DIFF_STATUS_DELETED
)
413 static void wt_status_print_changed(struct wt_status
*s
)
416 int worktree_changes
= wt_status_check_worktree_changes(s
);
418 if (!worktree_changes
)
421 wt_status_print_dirty_header(s
, worktree_changes
< 0);
423 for (i
= 0; i
< s
->change
.nr
; i
++) {
424 struct wt_status_change_data
*d
;
425 struct string_list_item
*it
;
426 it
= &(s
->change
.items
[i
]);
428 if (!d
->worktree_status
||
429 d
->worktree_status
== DIFF_STATUS_UNMERGED
)
431 wt_status_print_change_data(s
, WT_STATUS_CHANGED
, it
);
433 wt_status_print_trailer(s
);
436 static void wt_status_print_submodule_summary(struct wt_status
*s
)
438 struct child_process sm_summary
;
439 char summary_limit
[64];
440 char index
[PATH_MAX
];
441 const char *env
[] = { index
, NULL
};
442 const char *argv
[] = {
449 s
->amend
? "HEAD^" : "HEAD",
453 sprintf(summary_limit
, "%d", wt_status_submodule_summary
);
454 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
456 memset(&sm_summary
, 0, sizeof(sm_summary
));
457 sm_summary
.argv
= argv
;
458 sm_summary
.env
= env
;
459 sm_summary
.git_cmd
= 1;
460 sm_summary
.no_stdin
= 1;
462 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
463 run_command(&sm_summary
);
466 static void wt_status_print_untracked(struct wt_status
*s
)
468 struct dir_struct dir
;
470 int shown_header
= 0;
471 struct strbuf buf
= STRBUF_INIT
;
473 memset(&dir
, 0, sizeof(dir
));
477 DIR_SHOW_OTHER_DIRECTORIES
| DIR_HIDE_EMPTY_DIRECTORIES
;
478 setup_standard_excludes(&dir
);
480 fill_directory(&dir
, NULL
);
481 for(i
= 0; i
< dir
.nr
; i
++) {
482 struct dir_entry
*ent
= dir
.entries
[i
];
483 if (!cache_name_is_other(ent
->name
, ent
->len
))
486 s
->workdir_untracked
= 1;
487 wt_status_print_untracked_header(s
);
490 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
491 color_fprintf_ln(s
->fp
, color(WT_STATUS_UNTRACKED
), "%s",
492 quote_path(ent
->name
, ent
->len
,
495 strbuf_release(&buf
);
498 static void wt_status_print_verbose(struct wt_status
*s
)
502 init_revisions(&rev
, NULL
);
503 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
504 setup_revisions(0, NULL
, &rev
,
505 s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
);
506 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
507 rev
.diffopt
.detect_rename
= 1;
508 rev
.diffopt
.file
= s
->fp
;
509 rev
.diffopt
.close_file
= 0;
511 * If we're not going to stdout, then we definitely don't
512 * want color, since we are going to the commit message
513 * file (and even the "auto" setting won't work, since it
514 * will have checked isatty on stdout).
517 DIFF_OPT_CLR(&rev
.diffopt
, COLOR_DIFF
);
518 run_diff_index(&rev
, 1);
521 static void wt_status_print_tracking(struct wt_status
*s
)
523 struct strbuf sb
= STRBUF_INIT
;
525 struct branch
*branch
;
527 assert(s
->branch
&& !s
->is_initial
);
528 if (prefixcmp(s
->branch
, "refs/heads/"))
530 branch
= branch_get(s
->branch
+ 11);
531 if (!format_tracking_info(branch
, &sb
))
534 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
535 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
),
536 "# %.*s", (int)(ep
- cp
), cp
);
537 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
540 void wt_status_print(struct wt_status
*s
)
542 unsigned char sha1
[20];
543 const char *branch_color
= color(WT_STATUS_HEADER
);
545 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
547 const char *on_what
= "On branch ";
548 const char *branch_name
= s
->branch
;
549 if (!prefixcmp(branch_name
, "refs/heads/"))
551 else if (!strcmp(branch_name
, "HEAD")) {
553 branch_color
= color(WT_STATUS_NOBRANCH
);
554 on_what
= "Not currently on any branch.";
556 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "# ");
557 color_fprintf_ln(s
->fp
, branch_color
, "%s%s", on_what
, branch_name
);
559 wt_status_print_tracking(s
);
562 wt_status_collect_changes(s
);
565 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
566 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "# Initial commit");
567 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
570 wt_status_print_unmerged(s
);
571 wt_status_print_updated(s
);
572 wt_status_print_changed(s
);
573 if (wt_status_submodule_summary
)
574 wt_status_print_submodule_summary(s
);
575 if (show_untracked_files
)
576 wt_status_print_untracked(s
);
577 else if (s
->commitable
)
578 fprintf(s
->fp
, "# Untracked files not listed (use -u option to show untracked files)\n");
581 wt_status_print_verbose(s
);
582 if (!s
->commitable
) {
584 fprintf(s
->fp
, "# No changes\n");
587 else if (s
->workdir_dirty
)
588 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
589 else if (s
->workdir_untracked
)
590 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
591 else if (s
->is_initial
)
592 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
593 else if (!show_untracked_files
)
594 printf("nothing to commit (use -u to show untracked files)\n");
596 printf("nothing to commit (working directory clean)\n");
600 int git_status_config(const char *k
, const char *v
, void *cb
)
602 if (!strcmp(k
, "status.submodulesummary")) {
604 wt_status_submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
605 if (is_bool
&& wt_status_submodule_summary
)
606 wt_status_submodule_summary
= -1;
609 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
610 wt_status_use_color
= git_config_colorbool(k
, v
, -1);
613 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
614 int slot
= parse_status_slot(k
, 13);
616 return config_error_nonbool(k
);
617 color_parse(v
, k
, wt_status_colors
[slot
]);
620 if (!strcmp(k
, "status.relativepaths")) {
621 wt_status_relative_paths
= git_config_bool(k
, v
);
624 if (!strcmp(k
, "status.showuntrackedfiles")) {
626 return config_error_nonbool(k
);
627 else if (!strcmp(v
, "no"))
628 show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
629 else if (!strcmp(v
, "normal"))
630 show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
631 else if (!strcmp(v
, "all"))
632 show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
634 return error("Invalid untracked files mode '%s'", v
);
637 return git_diff_ui_config(k
, v
, cb
);