1#include "cache.h"
2#include "wt-status.h"
3#include "color.h"
4#include "object.h"
5#include "dir.h"
6#include "commit.h"
7#include "diff.h"
8#include "revision.h"
9#include "diffcore.h"
10#include "quote.h"
11#include "run-command.h"
12#include "remote.h"
13
14int wt_status_relative_paths = 1;
15int wt_status_use_color = -1;
16int wt_status_submodule_summary;
17static 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 */
23};
24
25enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
26
27static int parse_status_slot(const char *var, int offset)
28{
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);
41}
42
43static const char* color(int slot)
44{
45 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
46}
47
48void wt_status_prepare(struct wt_status *s)
49{
50 unsigned char sha1[20];
51 const char *head;
52
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";
57 s->fp = stdout;
58 s->index_file = get_index_file();
59}
60
61static void wt_status_print_cached_header(struct wt_status *s)
62{
63 const char *c = color(WT_STATUS_HEADER);
64 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
65 if (!s->is_initial) {
66 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
67 } else {
68 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
69 }
70 color_fprintf_ln(s->fp, c, "#");
71}
72
73static void wt_status_print_dirty_header(struct wt_status *s,
74 int has_deleted)
75{
76 const char *c = color(WT_STATUS_HEADER);
77 color_fprintf_ln(s->fp, c, "# Changed but not updated:");
78 if (!has_deleted)
79 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to update what will be committed)");
80 else
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, "#");
83}
84
85static void wt_status_print_untracked_header(struct wt_status *s)
86{
87 const char *c = color(WT_STATUS_HEADER);
88 color_fprintf_ln(s->fp, c, "# Untracked files:");
89 color_fprintf_ln(s->fp, c, "# (use \"git add <file>...\" to include in what will be committed)");
90 color_fprintf_ln(s->fp, c, "#");
91}
92
93static void wt_status_print_trailer(struct wt_status *s)
94{
95 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
96}
97
98#define quote_path quote_path_relative
99
100static void wt_status_print_filepair(struct wt_status *s,
101 int t, struct diff_filepair *p)
102{
103 const char *c = color(t);
104 const char *one, *two;
105 struct strbuf onebuf, twobuf;
106
107 strbuf_init(&onebuf, 0);
108 strbuf_init(&twobuf, 0);
109 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
110 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
111
112 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
113 switch (p->status) {
114 case DIFF_STATUS_ADDED:
115 color_fprintf(s->fp, c, "new file: %s", one);
116 break;
117 case DIFF_STATUS_COPIED:
118 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
119 break;
120 case DIFF_STATUS_DELETED:
121 color_fprintf(s->fp, c, "deleted: %s", one);
122 break;
123 case DIFF_STATUS_MODIFIED:
124 color_fprintf(s->fp, c, "modified: %s", one);
125 break;
126 case DIFF_STATUS_RENAMED:
127 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
128 break;
129 case DIFF_STATUS_TYPE_CHANGED:
130 color_fprintf(s->fp, c, "typechange: %s", one);
131 break;
132 case DIFF_STATUS_UNKNOWN:
133 color_fprintf(s->fp, c, "unknown: %s", one);
134 break;
135 case DIFF_STATUS_UNMERGED:
136 color_fprintf(s->fp, c, "unmerged: %s", one);
137 break;
138 default:
139 die("bug: unhandled diff status %c", p->status);
140 }
141 fprintf(s->fp, "\n");
142 strbuf_release(&onebuf);
143 strbuf_release(&twobuf);
144}
145
146static void wt_status_print_updated_cb(struct diff_queue_struct *q,
147 struct diff_options *options,
148 void *data)
149{
150 struct wt_status *s = data;
151 int shown_header = 0;
152 int i;
153 for (i = 0; i < q->nr; i++) {
154 if (q->queue[i]->status == 'U')
155 continue;
156 if (!shown_header) {
157 wt_status_print_cached_header(s);
158 s->commitable = 1;
159 shown_header = 1;
160 }
161 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
162 }
163 if (shown_header)
164 wt_status_print_trailer(s);
165}
166
167static void wt_status_print_changed_cb(struct diff_queue_struct *q,
168 struct diff_options *options,
169 void *data)
170{
171 struct wt_status *s = data;
172 int i;
173 if (q->nr) {
174 int has_deleted = 0;
175 s->workdir_dirty = 1;
176 for (i = 0; i < q->nr; i++)
177 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
178 has_deleted = 1;
179 break;
180 }
181 wt_status_print_dirty_header(s, has_deleted);
182 }
183 for (i = 0; i < q->nr; i++)
184 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
185 if (q->nr)
186 wt_status_print_trailer(s);
187}
188
189static void wt_status_print_initial(struct wt_status *s)
190{
191 int i;
192 struct strbuf buf;
193
194 strbuf_init(&buf, 0);
195 if (active_nr) {
196 s->commitable = 1;
197 wt_status_print_cached_header(s);
198 }
199 for (i = 0; i < active_nr; i++) {
200 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
201 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
202 quote_path(active_cache[i]->name, -1,
203 &buf, s->prefix));
204 }
205 if (active_nr)
206 wt_status_print_trailer(s);
207 strbuf_release(&buf);
208}
209
210static void wt_status_print_updated(struct wt_status *s)
211{
212 struct rev_info rev;
213 init_revisions(&rev, NULL);
214 setup_revisions(0, NULL, &rev, s->reference);
215 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
216 rev.diffopt.format_callback = wt_status_print_updated_cb;
217 rev.diffopt.format_callback_data = s;
218 rev.diffopt.detect_rename = 1;
219 rev.diffopt.rename_limit = 200;
220 rev.diffopt.break_opt = 0;
221 run_diff_index(&rev, 1);
222}
223
224static void wt_status_print_changed(struct wt_status *s)
225{
226 struct rev_info rev;
227 init_revisions(&rev, "");
228 setup_revisions(0, NULL, &rev, NULL);
229 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
230 rev.diffopt.format_callback = wt_status_print_changed_cb;
231 rev.diffopt.format_callback_data = s;
232 run_diff_files(&rev, 0);
233}
234
235static void wt_status_print_submodule_summary(struct wt_status *s)
236{
237 struct child_process sm_summary;
238 char summary_limit[64];
239 char index[PATH_MAX];
240 const char *env[] = { index, NULL };
241 const char *argv[] = {
242 "submodule",
243 "summary",
244 "--cached",
245 "--for-status",
246 "--summary-limit",
247 summary_limit,
248 s->amend ? "HEAD^" : "HEAD",
249 NULL
250 };
251
252 sprintf(summary_limit, "%d", wt_status_submodule_summary);
253 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
254
255 memset(&sm_summary, 0, sizeof(sm_summary));
256 sm_summary.argv = argv;
257 sm_summary.env = env;
258 sm_summary.git_cmd = 1;
259 sm_summary.no_stdin = 1;
260 fflush(s->fp);
261 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
262 run_command(&sm_summary);
263}
264
265static void wt_status_print_untracked(struct wt_status *s)
266{
267 struct dir_struct dir;
268 int i;
269 int shown_header = 0;
270 struct strbuf buf;
271
272 strbuf_init(&buf, 0);
273 memset(&dir, 0, sizeof(dir));
274
275 if (!s->untracked) {
276 dir.show_other_directories = 1;
277 dir.hide_empty_directories = 1;
278 }
279 setup_standard_excludes(&dir);
280
281 read_directory(&dir, ".", "", 0, NULL);
282 for(i = 0; i < dir.nr; i++) {
283 /* check for matching entry, which is unmerged; lifted from
284 * builtin-ls-files:show_other_files */
285 struct dir_entry *ent = dir.entries[i];
286 int pos = cache_name_pos(ent->name, ent->len);
287 struct cache_entry *ce;
288 if (0 <= pos)
289 die("bug in wt_status_print_untracked");
290 pos = -pos - 1;
291 if (pos < active_nr) {
292 ce = active_cache[pos];
293 if (ce_namelen(ce) == ent->len &&
294 !memcmp(ce->name, ent->name, ent->len))
295 continue;
296 }
297 if (!shown_header) {
298 s->workdir_untracked = 1;
299 wt_status_print_untracked_header(s);
300 shown_header = 1;
301 }
302 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
303 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
304 quote_path(ent->name, ent->len,
305 &buf, s->prefix));
306 }
307 strbuf_release(&buf);
308}
309
310static void wt_status_print_verbose(struct wt_status *s)
311{
312 struct rev_info rev;
313
314 init_revisions(&rev, NULL);
315 setup_revisions(0, NULL, &rev, s->reference);
316 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
317 rev.diffopt.detect_rename = 1;
318 rev.diffopt.file = s->fp;
319 rev.diffopt.close_file = 0;
320 run_diff_index(&rev, 1);
321}
322
323static void wt_status_print_tracking(struct wt_status *s)
324{
325 struct strbuf sb = STRBUF_INIT;
326 const char *cp, *ep;
327 struct branch *branch;
328
329 assert(s->branch && !s->is_initial);
330 if (prefixcmp(s->branch, "refs/heads/"))
331 return;
332 branch = branch_get(s->branch + 11);
333 if (!format_tracking_info(branch, &sb))
334 return;
335
336 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
337 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
338 "# %.*s", (int)(ep - cp), cp);
339 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
340}
341
342void wt_status_print(struct wt_status *s)
343{
344 unsigned char sha1[20];
345 const char *branch_color = color(WT_STATUS_HEADER);
346
347 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
348 if (s->branch) {
349 const char *on_what = "On branch ";
350 const char *branch_name = s->branch;
351 if (!prefixcmp(branch_name, "refs/heads/"))
352 branch_name += 11;
353 else if (!strcmp(branch_name, "HEAD")) {
354 branch_name = "";
355 branch_color = color(WT_STATUS_NOBRANCH);
356 on_what = "Not currently on any branch.";
357 }
358 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
359 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
360 if (!s->is_initial)
361 wt_status_print_tracking(s);
362 }
363
364 if (s->is_initial) {
365 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
366 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
367 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
368 wt_status_print_initial(s);
369 }
370 else {
371 wt_status_print_updated(s);
372 }
373
374 wt_status_print_changed(s);
375 if (wt_status_submodule_summary)
376 wt_status_print_submodule_summary(s);
377 if (show_untracked_files)
378 wt_status_print_untracked(s);
379 else if (s->commitable)
380 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
381
382 if (s->verbose && !s->is_initial)
383 wt_status_print_verbose(s);
384 if (!s->commitable) {
385 if (s->amend)
386 fprintf(s->fp, "# No changes\n");
387 else if (s->nowarn)
388 ; /* nothing */
389 else if (s->workdir_dirty)
390 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
391 else if (s->workdir_untracked)
392 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
393 else if (s->is_initial)
394 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
395 else if (!show_untracked_files)
396 printf("nothing to commit (use -u to show untracked files)\n");
397 else
398 printf("nothing to commit (working directory clean)\n");
399 }
400}
401
402int git_status_config(const char *k, const char *v, void *cb)
403{
404 if (!strcmp(k, "status.submodulesummary")) {
405 int is_bool;
406 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
407 if (is_bool && wt_status_submodule_summary)
408 wt_status_submodule_summary = -1;
409 return 0;
410 }
411 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
412 wt_status_use_color = git_config_colorbool(k, v, -1);
413 return 0;
414 }
415 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
416 int slot = parse_status_slot(k, 13);
417 if (!v)
418 return config_error_nonbool(k);
419 color_parse(v, k, wt_status_colors[slot]);
420 return 0;
421 }
422 if (!strcmp(k, "status.relativepaths")) {
423 wt_status_relative_paths = git_config_bool(k, v);
424 return 0;
425 }
426 if (!strcmp(k, "status.showuntrackedfiles")) {
427 if (!v)
428 return config_error_nonbool(k);
429 else if (!strcmp(v, "no"))
430 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
431 else if (!strcmp(v, "normal"))
432 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
433 else if (!strcmp(v, "all"))
434 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
435 else
436 return error("Invalid untracked files mode '%s'", v);
437 return 0;
438 }
439 return git_color_default_config(k, v, cb);
440}