11 #include "run-command.h"
14 int wt_status_relative_paths
= 1;
15 int wt_status_use_color
= -1;
16 int wt_status_submodule_summary
;
17 static char wt_status_colors
[][COLOR_MAXLEN
] = {
18 "", /* WT_STATUS_HEADER: normal */
19 "\033[32m", /* WT_STATUS_UPDATED: green */
20 "\033[31m", /* WT_STATUS_CHANGED: red */
21 "\033[31m", /* WT_STATUS_UNTRACKED: red */
22 "\033[31m", /* WT_STATUS_NOBRANCH: red */
25 static const char use_add_msg
[] =
26 "use \"git add <file>...\" to update what will be committed";
27 static const char use_add_rm_msg
[] =
28 "use \"git add/rm <file>...\" to update what will be committed";
29 static const char use_add_to_include_msg
[] =
30 "use \"git add <file>...\" to include in what will be committed";
31 enum untracked_status_type show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
33 static int parse_status_slot(const char *var
, int offset
)
35 if (!strcasecmp(var
+offset
, "header"))
36 return WT_STATUS_HEADER
;
37 if (!strcasecmp(var
+offset
, "updated")
38 || !strcasecmp(var
+offset
, "added"))
39 return WT_STATUS_UPDATED
;
40 if (!strcasecmp(var
+offset
, "changed"))
41 return WT_STATUS_CHANGED
;
42 if (!strcasecmp(var
+offset
, "untracked"))
43 return WT_STATUS_UNTRACKED
;
44 if (!strcasecmp(var
+offset
, "nobranch"))
45 return WT_STATUS_NOBRANCH
;
46 die("bad config variable '%s'", var
);
49 static const char* color(int slot
)
51 return wt_status_use_color
> 0 ? wt_status_colors
[slot
] : "";
54 void wt_status_prepare(struct wt_status
*s
)
56 unsigned char sha1
[20];
59 memset(s
, 0, sizeof(*s
));
60 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
61 s
->branch
= head
? xstrdup(head
) : NULL
;
62 s
->reference
= "HEAD";
64 s
->index_file
= get_index_file();
67 static void wt_status_print_cached_header(struct wt_status
*s
)
69 const char *c
= color(WT_STATUS_HEADER
);
70 color_fprintf_ln(s
->fp
, c
, "# Changes to be committed:");
72 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
74 color_fprintf_ln(s
->fp
, c
, "# (use \"git rm --cached <file>...\" to unstage)");
76 color_fprintf_ln(s
->fp
, c
, "#");
79 static void wt_status_print_header(struct wt_status
*s
,
80 const char *main
, const char *sub
)
82 const char *c
= color(WT_STATUS_HEADER
);
83 color_fprintf_ln(s
->fp
, c
, "# %s:", main
);
84 color_fprintf_ln(s
->fp
, c
, "# (%s)", sub
);
85 color_fprintf_ln(s
->fp
, c
, "#");
88 static void wt_status_print_trailer(struct wt_status
*s
)
90 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
93 #define quote_path quote_path_relative
95 static void wt_status_print_filepair(struct wt_status
*s
,
96 int t
, struct diff_filepair
*p
)
98 const char *c
= color(t
);
99 const char *one
, *two
;
100 struct strbuf onebuf
, twobuf
;
102 strbuf_init(&onebuf
, 0);
103 strbuf_init(&twobuf
, 0);
104 one
= quote_path(p
->one
->path
, -1, &onebuf
, s
->prefix
);
105 two
= quote_path(p
->two
->path
, -1, &twobuf
, s
->prefix
);
107 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
109 case DIFF_STATUS_ADDED
:
110 color_fprintf(s
->fp
, c
, "new file: %s", one
);
112 case DIFF_STATUS_COPIED
:
113 color_fprintf(s
->fp
, c
, "copied: %s -> %s", one
, two
);
115 case DIFF_STATUS_DELETED
:
116 color_fprintf(s
->fp
, c
, "deleted: %s", one
);
118 case DIFF_STATUS_MODIFIED
:
119 color_fprintf(s
->fp
, c
, "modified: %s", one
);
121 case DIFF_STATUS_RENAMED
:
122 color_fprintf(s
->fp
, c
, "renamed: %s -> %s", one
, two
);
124 case DIFF_STATUS_TYPE_CHANGED
:
125 color_fprintf(s
->fp
, c
, "typechange: %s", one
);
127 case DIFF_STATUS_UNKNOWN
:
128 color_fprintf(s
->fp
, c
, "unknown: %s", one
);
130 case DIFF_STATUS_UNMERGED
:
131 color_fprintf(s
->fp
, c
, "unmerged: %s", one
);
134 die("bug: unhandled diff status %c", p
->status
);
136 fprintf(s
->fp
, "\n");
137 strbuf_release(&onebuf
);
138 strbuf_release(&twobuf
);
141 static void wt_status_print_updated_cb(struct diff_queue_struct
*q
,
142 struct diff_options
*options
,
145 struct wt_status
*s
= data
;
146 int shown_header
= 0;
148 for (i
= 0; i
< q
->nr
; i
++) {
149 if (q
->queue
[i
]->status
== 'U')
152 wt_status_print_cached_header(s
);
156 wt_status_print_filepair(s
, WT_STATUS_UPDATED
, q
->queue
[i
]);
159 wt_status_print_trailer(s
);
162 static void wt_status_print_changed_cb(struct diff_queue_struct
*q
,
163 struct diff_options
*options
,
166 struct wt_status
*s
= data
;
169 const char *msg
= use_add_msg
;
170 s
->workdir_dirty
= 1;
171 for (i
= 0; i
< q
->nr
; i
++)
172 if (q
->queue
[i
]->status
== DIFF_STATUS_DELETED
) {
173 msg
= use_add_rm_msg
;
176 wt_status_print_header(s
, "Changed but not updated", msg
);
178 for (i
= 0; i
< q
->nr
; i
++)
179 wt_status_print_filepair(s
, WT_STATUS_CHANGED
, q
->queue
[i
]);
181 wt_status_print_trailer(s
);
184 static void wt_status_print_initial(struct wt_status
*s
)
189 strbuf_init(&buf
, 0);
192 wt_status_print_cached_header(s
);
194 for (i
= 0; i
< active_nr
; i
++) {
195 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
196 color_fprintf_ln(s
->fp
, color(WT_STATUS_UPDATED
), "new file: %s",
197 quote_path(active_cache
[i
]->name
, -1,
201 wt_status_print_trailer(s
);
202 strbuf_release(&buf
);
205 static void wt_status_print_updated(struct wt_status
*s
)
208 init_revisions(&rev
, NULL
);
209 setup_revisions(0, NULL
, &rev
, s
->reference
);
210 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
211 rev
.diffopt
.format_callback
= wt_status_print_updated_cb
;
212 rev
.diffopt
.format_callback_data
= s
;
213 rev
.diffopt
.detect_rename
= 1;
214 rev
.diffopt
.rename_limit
= 200;
215 rev
.diffopt
.break_opt
= 0;
216 run_diff_index(&rev
, 1);
219 static void wt_status_print_changed(struct wt_status
*s
)
222 init_revisions(&rev
, "");
223 setup_revisions(0, NULL
, &rev
, NULL
);
224 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
225 rev
.diffopt
.format_callback
= wt_status_print_changed_cb
;
226 rev
.diffopt
.format_callback_data
= s
;
227 run_diff_files(&rev
, 0);
230 static void wt_status_print_submodule_summary(struct wt_status
*s
)
232 struct child_process sm_summary
;
233 char summary_limit
[64];
234 char index
[PATH_MAX
];
235 const char *env
[] = { index
, NULL
};
236 const char *argv
[] = {
243 s
->amend
? "HEAD^" : "HEAD",
247 sprintf(summary_limit
, "%d", wt_status_submodule_summary
);
248 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
250 memset(&sm_summary
, 0, sizeof(sm_summary
));
251 sm_summary
.argv
= argv
;
252 sm_summary
.env
= env
;
253 sm_summary
.git_cmd
= 1;
254 sm_summary
.no_stdin
= 1;
256 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
257 run_command(&sm_summary
);
260 static void wt_status_print_untracked(struct wt_status
*s
)
262 struct dir_struct dir
;
264 int shown_header
= 0;
267 strbuf_init(&buf
, 0);
268 memset(&dir
, 0, sizeof(dir
));
271 dir
.show_other_directories
= 1;
272 dir
.hide_empty_directories
= 1;
274 setup_standard_excludes(&dir
);
276 read_directory(&dir
, ".", "", 0, NULL
);
277 for(i
= 0; i
< dir
.nr
; i
++) {
278 struct dir_entry
*ent
= dir
.entries
[i
];
279 if (!cache_name_is_other(ent
->name
, ent
->len
))
282 s
->workdir_untracked
= 1;
283 wt_status_print_header(s
, "Untracked files",
284 use_add_to_include_msg
);
287 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
288 color_fprintf_ln(s
->fp
, color(WT_STATUS_UNTRACKED
), "%s",
289 quote_path(ent
->name
, ent
->len
,
292 strbuf_release(&buf
);
295 static void wt_status_print_verbose(struct wt_status
*s
)
299 init_revisions(&rev
, NULL
);
300 setup_revisions(0, NULL
, &rev
, s
->reference
);
301 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
302 rev
.diffopt
.detect_rename
= 1;
303 rev
.diffopt
.file
= s
->fp
;
304 rev
.diffopt
.close_file
= 0;
305 run_diff_index(&rev
, 1);
308 static void wt_status_print_tracking(struct wt_status
*s
)
310 struct strbuf sb
= STRBUF_INIT
;
312 struct branch
*branch
;
314 assert(s
->branch
&& !s
->is_initial
);
315 if (prefixcmp(s
->branch
, "refs/heads/"))
317 branch
= branch_get(s
->branch
+ 11);
318 if (!format_tracking_info(branch
, &sb
))
321 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
322 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
),
323 "# %.*s", (int)(ep
- cp
), cp
);
324 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
327 void wt_status_print(struct wt_status
*s
)
329 unsigned char sha1
[20];
330 const char *branch_color
= color(WT_STATUS_HEADER
);
332 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
334 const char *on_what
= "On branch ";
335 const char *branch_name
= s
->branch
;
336 if (!prefixcmp(branch_name
, "refs/heads/"))
338 else if (!strcmp(branch_name
, "HEAD")) {
340 branch_color
= color(WT_STATUS_NOBRANCH
);
341 on_what
= "Not currently on any branch.";
343 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "# ");
344 color_fprintf_ln(s
->fp
, branch_color
, "%s%s", on_what
, branch_name
);
346 wt_status_print_tracking(s
);
350 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
351 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "# Initial commit");
352 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
353 wt_status_print_initial(s
);
356 wt_status_print_updated(s
);
359 wt_status_print_changed(s
);
360 if (wt_status_submodule_summary
)
361 wt_status_print_submodule_summary(s
);
362 if (show_untracked_files
)
363 wt_status_print_untracked(s
);
364 else if (s
->commitable
)
365 fprintf(s
->fp
, "# Untracked files not listed (use -u option to show untracked files)\n");
367 if (s
->verbose
&& !s
->is_initial
)
368 wt_status_print_verbose(s
);
369 if (!s
->commitable
) {
371 fprintf(s
->fp
, "# No changes\n");
374 else if (s
->workdir_dirty
)
375 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
376 else if (s
->workdir_untracked
)
377 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
378 else if (s
->is_initial
)
379 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
380 else if (!show_untracked_files
)
381 printf("nothing to commit (use -u to show untracked files)\n");
383 printf("nothing to commit (working directory clean)\n");
387 int git_status_config(const char *k
, const char *v
, void *cb
)
389 if (!strcmp(k
, "status.submodulesummary")) {
391 wt_status_submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
392 if (is_bool
&& wt_status_submodule_summary
)
393 wt_status_submodule_summary
= -1;
396 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
397 wt_status_use_color
= git_config_colorbool(k
, v
, -1);
400 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
401 int slot
= parse_status_slot(k
, 13);
403 return config_error_nonbool(k
);
404 color_parse(v
, k
, wt_status_colors
[slot
]);
407 if (!strcmp(k
, "status.relativepaths")) {
408 wt_status_relative_paths
= git_config_bool(k
, v
);
411 if (!strcmp(k
, "status.showuntrackedfiles")) {
413 return config_error_nonbool(k
);
414 else if (!strcmp(v
, "no"))
415 show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
416 else if (!strcmp(v
, "normal"))
417 show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
418 else if (!strcmp(v
, "all"))
419 show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
421 return error("Invalid untracked files mode '%s'", v
);
424 return git_color_default_config(k
, v
, cb
);