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 enum untracked_status_type show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
27 static int parse_status_slot(const char *var
, int offset
)
29 if (!strcasecmp(var
+offset
, "header"))
30 return WT_STATUS_HEADER
;
31 if (!strcasecmp(var
+offset
, "updated")
32 || !strcasecmp(var
+offset
, "added"))
33 return WT_STATUS_UPDATED
;
34 if (!strcasecmp(var
+offset
, "changed"))
35 return WT_STATUS_CHANGED
;
36 if (!strcasecmp(var
+offset
, "untracked"))
37 return WT_STATUS_UNTRACKED
;
38 if (!strcasecmp(var
+offset
, "nobranch"))
39 return WT_STATUS_NOBRANCH
;
40 die("bad config variable '%s'", var
);
43 static const char* color(int slot
)
45 return wt_status_use_color
> 0 ? wt_status_colors
[slot
] : "";
48 void wt_status_prepare(struct wt_status
*s
)
50 unsigned char sha1
[20];
53 memset(s
, 0, sizeof(*s
));
54 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
55 s
->branch
= head
? xstrdup(head
) : NULL
;
56 s
->reference
= "HEAD";
58 s
->index_file
= get_index_file();
61 static void wt_status_print_cached_header(struct wt_status
*s
)
63 const char *c
= color(WT_STATUS_HEADER
);
64 color_fprintf_ln(s
->fp
, c
, "# Changes to be committed:");
66 color_fprintf_ln(s
->fp
, c
, "# (use \"git reset %s <file>...\" to unstage)", s
->reference
);
68 color_fprintf_ln(s
->fp
, c
, "# (use \"git rm --cached <file>...\" to unstage)");
70 color_fprintf_ln(s
->fp
, c
, "#");
73 static void wt_status_print_dirty_header(struct wt_status
*s
,
76 const char *c
= color(WT_STATUS_HEADER
);
77 color_fprintf_ln(s
->fp
, c
, "# Changed but not updated:");
79 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to update what will be committed)");
81 color_fprintf_ln(s
->fp
, c
, "# (use \"git add/rm <file>...\" to update what will be committed)");
82 color_fprintf_ln(s
->fp
, c
, "# (use \"git checkout -- <file>...\" to discard changes in working directory)");
83 color_fprintf_ln(s
->fp
, c
, "#");
86 static void wt_status_print_untracked_header(struct wt_status
*s
)
88 const char *c
= color(WT_STATUS_HEADER
);
89 color_fprintf_ln(s
->fp
, c
, "# Untracked files:");
90 color_fprintf_ln(s
->fp
, c
, "# (use \"git add <file>...\" to include in what will be committed)");
91 color_fprintf_ln(s
->fp
, c
, "#");
94 static void wt_status_print_trailer(struct wt_status
*s
)
96 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
99 #define quote_path quote_path_relative
101 static void wt_status_print_filepair(struct wt_status
*s
,
102 int t
, struct diff_filepair
*p
)
104 const char *c
= color(t
);
105 const char *one
, *two
;
106 struct strbuf onebuf
= STRBUF_INIT
, twobuf
= STRBUF_INIT
;
108 one
= quote_path(p
->one
->path
, -1, &onebuf
, s
->prefix
);
109 two
= quote_path(p
->two
->path
, -1, &twobuf
, s
->prefix
);
111 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
113 case DIFF_STATUS_ADDED
:
114 color_fprintf(s
->fp
, c
, "new file: %s", one
);
116 case DIFF_STATUS_COPIED
:
117 color_fprintf(s
->fp
, c
, "copied: %s -> %s", one
, two
);
119 case DIFF_STATUS_DELETED
:
120 color_fprintf(s
->fp
, c
, "deleted: %s", one
);
122 case DIFF_STATUS_MODIFIED
:
123 color_fprintf(s
->fp
, c
, "modified: %s", one
);
125 case DIFF_STATUS_RENAMED
:
126 color_fprintf(s
->fp
, c
, "renamed: %s -> %s", one
, two
);
128 case DIFF_STATUS_TYPE_CHANGED
:
129 color_fprintf(s
->fp
, c
, "typechange: %s", one
);
131 case DIFF_STATUS_UNKNOWN
:
132 color_fprintf(s
->fp
, c
, "unknown: %s", one
);
134 case DIFF_STATUS_UNMERGED
:
135 color_fprintf(s
->fp
, c
, "unmerged: %s", one
);
138 die("bug: unhandled diff status %c", p
->status
);
140 fprintf(s
->fp
, "\n");
141 strbuf_release(&onebuf
);
142 strbuf_release(&twobuf
);
145 static void wt_status_print_updated_cb(struct diff_queue_struct
*q
,
146 struct diff_options
*options
,
149 struct wt_status
*s
= data
;
150 int shown_header
= 0;
152 for (i
= 0; i
< q
->nr
; i
++) {
153 if (q
->queue
[i
]->status
== 'U')
156 wt_status_print_cached_header(s
);
160 wt_status_print_filepair(s
, WT_STATUS_UPDATED
, q
->queue
[i
]);
163 wt_status_print_trailer(s
);
166 static void wt_status_print_changed_cb(struct diff_queue_struct
*q
,
167 struct diff_options
*options
,
170 struct wt_status
*s
= data
;
174 s
->workdir_dirty
= 1;
175 for (i
= 0; i
< q
->nr
; i
++)
176 if (q
->queue
[i
]->status
== DIFF_STATUS_DELETED
) {
180 wt_status_print_dirty_header(s
, has_deleted
);
182 for (i
= 0; i
< q
->nr
; i
++)
183 wt_status_print_filepair(s
, WT_STATUS_CHANGED
, q
->queue
[i
]);
185 wt_status_print_trailer(s
);
188 static void wt_status_print_updated(struct wt_status
*s
)
191 init_revisions(&rev
, NULL
);
192 setup_revisions(0, NULL
, &rev
,
193 s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
);
194 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
195 rev
.diffopt
.format_callback
= wt_status_print_updated_cb
;
196 rev
.diffopt
.format_callback_data
= s
;
197 rev
.diffopt
.detect_rename
= 1;
198 rev
.diffopt
.rename_limit
= 200;
199 rev
.diffopt
.break_opt
= 0;
200 run_diff_index(&rev
, 1);
203 static void wt_status_print_changed(struct wt_status
*s
)
206 init_revisions(&rev
, "");
207 setup_revisions(0, NULL
, &rev
, NULL
);
208 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
209 rev
.diffopt
.format_callback
= wt_status_print_changed_cb
;
210 rev
.diffopt
.format_callback_data
= s
;
211 run_diff_files(&rev
, 0);
214 static void wt_status_print_submodule_summary(struct wt_status
*s
)
216 struct child_process sm_summary
;
217 char summary_limit
[64];
218 char index
[PATH_MAX
];
219 const char *env
[] = { index
, NULL
};
220 const char *argv
[] = {
227 s
->amend
? "HEAD^" : "HEAD",
231 sprintf(summary_limit
, "%d", wt_status_submodule_summary
);
232 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", s
->index_file
);
234 memset(&sm_summary
, 0, sizeof(sm_summary
));
235 sm_summary
.argv
= argv
;
236 sm_summary
.env
= env
;
237 sm_summary
.git_cmd
= 1;
238 sm_summary
.no_stdin
= 1;
240 sm_summary
.out
= dup(fileno(s
->fp
)); /* run_command closes it */
241 run_command(&sm_summary
);
244 static void wt_status_print_untracked(struct wt_status
*s
)
246 struct dir_struct dir
;
248 int shown_header
= 0;
249 struct strbuf buf
= STRBUF_INIT
;
251 memset(&dir
, 0, sizeof(dir
));
254 dir
.show_other_directories
= 1;
255 dir
.hide_empty_directories
= 1;
257 setup_standard_excludes(&dir
);
259 read_directory(&dir
, ".", "", 0, NULL
);
260 for(i
= 0; i
< dir
.nr
; i
++) {
261 struct dir_entry
*ent
= dir
.entries
[i
];
262 if (!cache_name_is_other(ent
->name
, ent
->len
))
265 s
->workdir_untracked
= 1;
266 wt_status_print_untracked_header(s
);
269 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "#\t");
270 color_fprintf_ln(s
->fp
, color(WT_STATUS_UNTRACKED
), "%s",
271 quote_path(ent
->name
, ent
->len
,
274 strbuf_release(&buf
);
277 static void wt_status_print_verbose(struct wt_status
*s
)
281 init_revisions(&rev
, NULL
);
282 DIFF_OPT_SET(&rev
.diffopt
, ALLOW_TEXTCONV
);
283 setup_revisions(0, NULL
, &rev
,
284 s
->is_initial
? EMPTY_TREE_SHA1_HEX
: s
->reference
);
285 rev
.diffopt
.output_format
|= DIFF_FORMAT_PATCH
;
286 rev
.diffopt
.detect_rename
= 1;
287 rev
.diffopt
.file
= s
->fp
;
288 rev
.diffopt
.close_file
= 0;
290 * If we're not going to stdout, then we definitely don't
291 * want color, since we are going to the commit message
292 * file (and even the "auto" setting won't work, since it
293 * will have checked isatty on stdout).
296 DIFF_OPT_CLR(&rev
.diffopt
, COLOR_DIFF
);
297 run_diff_index(&rev
, 1);
300 static void wt_status_print_tracking(struct wt_status
*s
)
302 struct strbuf sb
= STRBUF_INIT
;
304 struct branch
*branch
;
306 assert(s
->branch
&& !s
->is_initial
);
307 if (prefixcmp(s
->branch
, "refs/heads/"))
309 branch
= branch_get(s
->branch
+ 11);
310 if (!format_tracking_info(branch
, &sb
))
313 for (cp
= sb
.buf
; (ep
= strchr(cp
, '\n')) != NULL
; cp
= ep
+ 1)
314 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
),
315 "# %.*s", (int)(ep
- cp
), cp
);
316 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
319 void wt_status_print(struct wt_status
*s
)
321 unsigned char sha1
[20];
322 const char *branch_color
= color(WT_STATUS_HEADER
);
324 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
326 const char *on_what
= "On branch ";
327 const char *branch_name
= s
->branch
;
328 if (!prefixcmp(branch_name
, "refs/heads/"))
330 else if (!strcmp(branch_name
, "HEAD")) {
332 branch_color
= color(WT_STATUS_NOBRANCH
);
333 on_what
= "Not currently on any branch.";
335 color_fprintf(s
->fp
, color(WT_STATUS_HEADER
), "# ");
336 color_fprintf_ln(s
->fp
, branch_color
, "%s%s", on_what
, branch_name
);
338 wt_status_print_tracking(s
);
342 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
343 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "# Initial commit");
344 color_fprintf_ln(s
->fp
, color(WT_STATUS_HEADER
), "#");
347 wt_status_print_updated(s
);
348 wt_status_print_changed(s
);
349 if (wt_status_submodule_summary
)
350 wt_status_print_submodule_summary(s
);
351 if (show_untracked_files
)
352 wt_status_print_untracked(s
);
353 else if (s
->commitable
)
354 fprintf(s
->fp
, "# Untracked files not listed (use -u option to show untracked files)\n");
357 wt_status_print_verbose(s
);
358 if (!s
->commitable
) {
360 fprintf(s
->fp
, "# No changes\n");
363 else if (s
->workdir_dirty
)
364 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
365 else if (s
->workdir_untracked
)
366 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
367 else if (s
->is_initial
)
368 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
369 else if (!show_untracked_files
)
370 printf("nothing to commit (use -u to show untracked files)\n");
372 printf("nothing to commit (working directory clean)\n");
376 int git_status_config(const char *k
, const char *v
, void *cb
)
378 if (!strcmp(k
, "status.submodulesummary")) {
380 wt_status_submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
381 if (is_bool
&& wt_status_submodule_summary
)
382 wt_status_submodule_summary
= -1;
385 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
386 wt_status_use_color
= git_config_colorbool(k
, v
, -1);
389 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
390 int slot
= parse_status_slot(k
, 13);
392 return config_error_nonbool(k
);
393 color_parse(v
, k
, wt_status_colors
[slot
]);
396 if (!strcmp(k
, "status.relativepaths")) {
397 wt_status_relative_paths
= git_config_bool(k
, v
);
400 if (!strcmp(k
, "status.showuntrackedfiles")) {
402 return config_error_nonbool(k
);
403 else if (!strcmp(v
, "no"))
404 show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
405 else if (!strcmp(v
, "normal"))
406 show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
407 else if (!strcmp(v
, "all"))
408 show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
410 return error("Invalid untracked files mode '%s'", v
);
413 return git_diff_ui_config(k
, v
, cb
);