]>
Commit | Line | Data |
---|---|---|
85023577 | 1 | #include "cache.h" |
c91f0d92 JK |
2 | #include "wt-status.h" |
3 | #include "color.h" | |
c91f0d92 JK |
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 | ||
11 | int wt_status_use_color = 0; | |
12 | static char wt_status_colors[][COLOR_MAXLEN] = { | |
13 | "", /* WT_STATUS_HEADER: normal */ | |
14 | "\033[32m", /* WT_STATUS_UPDATED: green */ | |
15 | "\033[31m", /* WT_STATUS_CHANGED: red */ | |
16 | "\033[31m", /* WT_STATUS_UNTRACKED: red */ | |
17 | }; | |
4d229653 JH |
18 | |
19 | static const char use_add_msg[] = | |
20 | "use \"git add <file>...\" to update what will be committed"; | |
21 | static const char use_add_rm_msg[] = | |
22 | "use \"git add/rm <file>...\" to update what will be committed"; | |
23 | static const char use_add_to_include_msg[] = | |
24 | "use \"git add <file>...\" to include in what will be committed"; | |
c91f0d92 JK |
25 | |
26 | static int parse_status_slot(const char *var, int offset) | |
27 | { | |
28 | if (!strcasecmp(var+offset, "header")) | |
29 | return WT_STATUS_HEADER; | |
82dca848 SP |
30 | if (!strcasecmp(var+offset, "updated") |
31 | || !strcasecmp(var+offset, "added")) | |
c91f0d92 JK |
32 | return WT_STATUS_UPDATED; |
33 | if (!strcasecmp(var+offset, "changed")) | |
34 | return WT_STATUS_CHANGED; | |
35 | if (!strcasecmp(var+offset, "untracked")) | |
36 | return WT_STATUS_UNTRACKED; | |
37 | die("bad config variable '%s'", var); | |
38 | } | |
39 | ||
40 | static const char* color(int slot) | |
41 | { | |
42 | return wt_status_use_color ? wt_status_colors[slot] : ""; | |
43 | } | |
44 | ||
45 | void wt_status_prepare(struct wt_status *s) | |
46 | { | |
47 | unsigned char sha1[20]; | |
48 | const char *head; | |
49 | ||
cc46a743 | 50 | memset(s, 0, sizeof(*s)); |
8da19775 | 51 | head = resolve_ref("HEAD", sha1, 0, NULL); |
f62363fb | 52 | s->branch = head ? xstrdup(head) : NULL; |
c91f0d92 | 53 | s->reference = "HEAD"; |
f26a0012 | 54 | s->fp = stdout; |
0f729f21 | 55 | s->index_file = get_index_file(); |
c91f0d92 JK |
56 | } |
57 | ||
f26a0012 | 58 | static void wt_status_print_cached_header(struct wt_status *s) |
3c1eb9cb JR |
59 | { |
60 | const char *c = color(WT_STATUS_HEADER); | |
f26a0012 KH |
61 | color_fprintf_ln(s->fp, c, "# Changes to be committed:"); |
62 | if (s->reference) { | |
63 | color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference); | |
3c1eb9cb | 64 | } else { |
f26a0012 | 65 | color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)"); |
3c1eb9cb | 66 | } |
f26a0012 | 67 | color_fprintf_ln(s->fp, c, "#"); |
3c1eb9cb JR |
68 | } |
69 | ||
f26a0012 KH |
70 | static void wt_status_print_header(struct wt_status *s, |
71 | const char *main, const char *sub) | |
c91f0d92 JK |
72 | { |
73 | const char *c = color(WT_STATUS_HEADER); | |
f26a0012 KH |
74 | color_fprintf_ln(s->fp, c, "# %s:", main); |
75 | color_fprintf_ln(s->fp, c, "# (%s)", sub); | |
76 | color_fprintf_ln(s->fp, c, "#"); | |
c91f0d92 JK |
77 | } |
78 | ||
f26a0012 | 79 | static void wt_status_print_trailer(struct wt_status *s) |
c91f0d92 | 80 | { |
f26a0012 | 81 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); |
c91f0d92 JK |
82 | } |
83 | ||
367c9886 JS |
84 | static char *quote_path(const char *in, int len, |
85 | struct strbuf *out, const char *prefix) | |
3a946802 | 86 | { |
367c9886 JS |
87 | if (len > 0) |
88 | strbuf_grow(out, len); | |
89 | strbuf_setlen(out, 0); | |
3a946802 | 90 | |
367c9886 JS |
91 | if (prefix) { |
92 | int off = 0; | |
93 | while (prefix[off] && off < len && prefix[off] == in[off]) | |
94 | if (prefix[off] == '/') { | |
95 | prefix += off + 1; | |
96 | in += off + 1; | |
97 | len -= off + 1; | |
98 | off = 0; | |
99 | } else | |
100 | off++; | |
101 | ||
102 | for (; *prefix; prefix++) | |
103 | if (*prefix == '/') | |
104 | strbuf_addstr(out, "../"); | |
105 | } | |
106 | ||
107 | for (; (len < 0 && *in) || len > 0; in++, len--) { | |
108 | int ch = *in; | |
3a946802 JH |
109 | |
110 | switch (ch) { | |
111 | case '\n': | |
367c9886 | 112 | strbuf_addstr(out, "\\n"); |
3a946802 JH |
113 | break; |
114 | case '\r': | |
367c9886 | 115 | strbuf_addstr(out, "\\r"); |
3a946802 JH |
116 | break; |
117 | default: | |
367c9886 | 118 | strbuf_addch(out, ch); |
3a946802 JH |
119 | continue; |
120 | } | |
3a946802 | 121 | } |
367c9886 JS |
122 | |
123 | return out->buf; | |
3a946802 JH |
124 | } |
125 | ||
f26a0012 KH |
126 | static void wt_status_print_filepair(struct wt_status *s, |
127 | int t, struct diff_filepair *p) | |
c91f0d92 JK |
128 | { |
129 | const char *c = color(t); | |
3a946802 | 130 | const char *one, *two; |
367c9886 | 131 | struct strbuf onebuf, twobuf; |
3a946802 | 132 | |
367c9886 JS |
133 | strbuf_init(&onebuf, 0); |
134 | strbuf_init(&twobuf, 0); | |
135 | one = quote_path(p->one->path, -1, &onebuf, s->prefix); | |
136 | two = quote_path(p->two->path, -1, &twobuf, s->prefix); | |
3a946802 | 137 | |
f26a0012 | 138 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
c91f0d92 JK |
139 | switch (p->status) { |
140 | case DIFF_STATUS_ADDED: | |
f26a0012 | 141 | color_fprintf(s->fp, c, "new file: %s", one); |
3a946802 | 142 | break; |
c91f0d92 | 143 | case DIFF_STATUS_COPIED: |
f26a0012 | 144 | color_fprintf(s->fp, c, "copied: %s -> %s", one, two); |
c91f0d92 JK |
145 | break; |
146 | case DIFF_STATUS_DELETED: | |
f26a0012 | 147 | color_fprintf(s->fp, c, "deleted: %s", one); |
3a946802 | 148 | break; |
c91f0d92 | 149 | case DIFF_STATUS_MODIFIED: |
f26a0012 | 150 | color_fprintf(s->fp, c, "modified: %s", one); |
3a946802 | 151 | break; |
c91f0d92 | 152 | case DIFF_STATUS_RENAMED: |
f26a0012 | 153 | color_fprintf(s->fp, c, "renamed: %s -> %s", one, two); |
c91f0d92 JK |
154 | break; |
155 | case DIFF_STATUS_TYPE_CHANGED: | |
f26a0012 | 156 | color_fprintf(s->fp, c, "typechange: %s", one); |
3a946802 | 157 | break; |
c91f0d92 | 158 | case DIFF_STATUS_UNKNOWN: |
f26a0012 | 159 | color_fprintf(s->fp, c, "unknown: %s", one); |
3a946802 | 160 | break; |
c91f0d92 | 161 | case DIFF_STATUS_UNMERGED: |
f26a0012 | 162 | color_fprintf(s->fp, c, "unmerged: %s", one); |
3a946802 | 163 | break; |
c91f0d92 JK |
164 | default: |
165 | die("bug: unhandled diff status %c", p->status); | |
166 | } | |
f26a0012 | 167 | fprintf(s->fp, "\n"); |
367c9886 JS |
168 | strbuf_release(&onebuf); |
169 | strbuf_release(&twobuf); | |
c91f0d92 JK |
170 | } |
171 | ||
172 | static void wt_status_print_updated_cb(struct diff_queue_struct *q, | |
173 | struct diff_options *options, | |
174 | void *data) | |
175 | { | |
176 | struct wt_status *s = data; | |
177 | int shown_header = 0; | |
178 | int i; | |
c91f0d92 JK |
179 | for (i = 0; i < q->nr; i++) { |
180 | if (q->queue[i]->status == 'U') | |
181 | continue; | |
182 | if (!shown_header) { | |
f26a0012 | 183 | wt_status_print_cached_header(s); |
c91f0d92 JK |
184 | s->commitable = 1; |
185 | shown_header = 1; | |
186 | } | |
f26a0012 | 187 | wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]); |
c91f0d92 JK |
188 | } |
189 | if (shown_header) | |
f26a0012 | 190 | wt_status_print_trailer(s); |
c91f0d92 JK |
191 | } |
192 | ||
193 | static void wt_status_print_changed_cb(struct diff_queue_struct *q, | |
194 | struct diff_options *options, | |
195 | void *data) | |
196 | { | |
6e458bf6 | 197 | struct wt_status *s = data; |
c91f0d92 | 198 | int i; |
6e458bf6 | 199 | if (q->nr) { |
4d229653 | 200 | const char *msg = use_add_msg; |
2a3a3c24 | 201 | s->workdir_dirty = 1; |
4d229653 JH |
202 | for (i = 0; i < q->nr; i++) |
203 | if (q->queue[i]->status == DIFF_STATUS_DELETED) { | |
204 | msg = use_add_rm_msg; | |
205 | break; | |
206 | } | |
f26a0012 | 207 | wt_status_print_header(s, "Changed but not updated", msg); |
6e458bf6 | 208 | } |
c91f0d92 | 209 | for (i = 0; i < q->nr; i++) |
f26a0012 | 210 | wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]); |
c91f0d92 | 211 | if (q->nr) |
f26a0012 | 212 | wt_status_print_trailer(s); |
c91f0d92 JK |
213 | } |
214 | ||
b4e1e4a7 JH |
215 | static void wt_read_cache(struct wt_status *s) |
216 | { | |
217 | discard_cache(); | |
0f729f21 | 218 | read_cache_from(s->index_file); |
b4e1e4a7 JH |
219 | } |
220 | ||
52fae7de | 221 | static void wt_status_print_initial(struct wt_status *s) |
c91f0d92 JK |
222 | { |
223 | int i; | |
367c9886 | 224 | struct strbuf buf; |
3a946802 | 225 | |
367c9886 | 226 | strbuf_init(&buf, 0); |
b4e1e4a7 | 227 | wt_read_cache(s); |
c91f0d92 JK |
228 | if (active_nr) { |
229 | s->commitable = 1; | |
f26a0012 | 230 | wt_status_print_cached_header(s); |
c91f0d92 JK |
231 | } |
232 | for (i = 0; i < active_nr; i++) { | |
f26a0012 KH |
233 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
234 | color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s", | |
367c9886 JS |
235 | quote_path(active_cache[i]->name, -1, |
236 | &buf, s->prefix)); | |
c91f0d92 JK |
237 | } |
238 | if (active_nr) | |
f26a0012 | 239 | wt_status_print_trailer(s); |
367c9886 | 240 | strbuf_release(&buf); |
c91f0d92 JK |
241 | } |
242 | ||
243 | static void wt_status_print_updated(struct wt_status *s) | |
244 | { | |
245 | struct rev_info rev; | |
c91f0d92 | 246 | init_revisions(&rev, NULL); |
49b8b292 | 247 | setup_revisions(0, NULL, &rev, s->reference); |
c91f0d92 JK |
248 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
249 | rev.diffopt.format_callback = wt_status_print_updated_cb; | |
250 | rev.diffopt.format_callback_data = s; | |
251 | rev.diffopt.detect_rename = 1; | |
0024a549 | 252 | rev.diffopt.rename_limit = 100; |
b4e1e4a7 | 253 | wt_read_cache(s); |
c91f0d92 JK |
254 | run_diff_index(&rev, 1); |
255 | } | |
256 | ||
257 | static void wt_status_print_changed(struct wt_status *s) | |
258 | { | |
259 | struct rev_info rev; | |
c91f0d92 | 260 | init_revisions(&rev, ""); |
49b8b292 | 261 | setup_revisions(0, NULL, &rev, NULL); |
c91f0d92 JK |
262 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
263 | rev.diffopt.format_callback = wt_status_print_changed_cb; | |
264 | rev.diffopt.format_callback_data = s; | |
b4e1e4a7 | 265 | wt_read_cache(s); |
c91f0d92 JK |
266 | run_diff_files(&rev, 0); |
267 | } | |
268 | ||
6e458bf6 | 269 | static void wt_status_print_untracked(struct wt_status *s) |
c91f0d92 JK |
270 | { |
271 | struct dir_struct dir; | |
c91f0d92 JK |
272 | int i; |
273 | int shown_header = 0; | |
367c9886 | 274 | struct strbuf buf; |
c91f0d92 | 275 | |
367c9886 | 276 | strbuf_init(&buf, 0); |
c91f0d92 JK |
277 | memset(&dir, 0, sizeof(dir)); |
278 | ||
2074cb0a JS |
279 | if (!s->untracked) { |
280 | dir.show_other_directories = 1; | |
281 | dir.hide_empty_directories = 1; | |
282 | } | |
039bc64e | 283 | setup_standard_excludes(&dir); |
c91f0d92 | 284 | |
9fc42d60 | 285 | read_directory(&dir, ".", "", 0, NULL); |
c91f0d92 JK |
286 | for(i = 0; i < dir.nr; i++) { |
287 | /* check for matching entry, which is unmerged; lifted from | |
288 | * builtin-ls-files:show_other_files */ | |
289 | struct dir_entry *ent = dir.entries[i]; | |
290 | int pos = cache_name_pos(ent->name, ent->len); | |
291 | struct cache_entry *ce; | |
292 | if (0 <= pos) | |
293 | die("bug in wt_status_print_untracked"); | |
294 | pos = -pos - 1; | |
295 | if (pos < active_nr) { | |
296 | ce = active_cache[pos]; | |
297 | if (ce_namelen(ce) == ent->len && | |
298 | !memcmp(ce->name, ent->name, ent->len)) | |
299 | continue; | |
300 | } | |
301 | if (!shown_header) { | |
2a3a3c24 | 302 | s->workdir_untracked = 1; |
f26a0012 | 303 | wt_status_print_header(s, "Untracked files", |
4d229653 | 304 | use_add_to_include_msg); |
c91f0d92 JK |
305 | shown_header = 1; |
306 | } | |
f26a0012 | 307 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
367c9886 JS |
308 | color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s", |
309 | quote_path(ent->name, ent->len, | |
310 | &buf, s->prefix)); | |
c91f0d92 | 311 | } |
367c9886 | 312 | strbuf_release(&buf); |
c91f0d92 JK |
313 | } |
314 | ||
315 | static void wt_status_print_verbose(struct wt_status *s) | |
316 | { | |
317 | struct rev_info rev; | |
c91f0d92 | 318 | init_revisions(&rev, NULL); |
49b8b292 | 319 | setup_revisions(0, NULL, &rev, s->reference); |
c91f0d92 JK |
320 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
321 | rev.diffopt.detect_rename = 1; | |
b4e1e4a7 | 322 | wt_read_cache(s); |
c91f0d92 JK |
323 | run_diff_index(&rev, 1); |
324 | } | |
325 | ||
326 | void wt_status_print(struct wt_status *s) | |
327 | { | |
98bf8a47 JR |
328 | unsigned char sha1[20]; |
329 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; | |
330 | ||
bda324cf JH |
331 | if (s->branch) { |
332 | const char *on_what = "On branch "; | |
333 | const char *branch_name = s->branch; | |
cc44c765 | 334 | if (!prefixcmp(branch_name, "refs/heads/")) |
bda324cf JH |
335 | branch_name += 11; |
336 | else if (!strcmp(branch_name, "HEAD")) { | |
337 | branch_name = ""; | |
338 | on_what = "Not currently on any branch."; | |
339 | } | |
f26a0012 | 340 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), |
bda324cf JH |
341 | "# %s%s", on_what, branch_name); |
342 | } | |
c91f0d92 JK |
343 | |
344 | if (s->is_initial) { | |
f26a0012 KH |
345 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); |
346 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit"); | |
347 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); | |
c91f0d92 JK |
348 | wt_status_print_initial(s); |
349 | } | |
350 | else { | |
351 | wt_status_print_updated(s); | |
c91f0d92 JK |
352 | } |
353 | ||
354 | wt_status_print_changed(s); | |
355 | wt_status_print_untracked(s); | |
356 | ||
357 | if (s->verbose && !s->is_initial) | |
358 | wt_status_print_verbose(s); | |
6e458bf6 JR |
359 | if (!s->commitable) { |
360 | if (s->amend) | |
f26a0012 | 361 | fprintf(s->fp, "# No changes\n"); |
2a3a3c24 | 362 | else if (s->workdir_dirty) |
dcbc7bbe | 363 | printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n"); |
2a3a3c24 JR |
364 | else if (s->workdir_untracked) |
365 | printf("nothing added to commit but untracked files present (use \"git add\" to track)\n"); | |
366 | else if (s->is_initial) | |
367 | printf("nothing to commit (create/copy files and use \"git add\" to track)\n"); | |
368 | else | |
369 | printf("nothing to commit (working directory clean)\n"); | |
6e458bf6 | 370 | } |
c91f0d92 JK |
371 | } |
372 | ||
373 | int git_status_config(const char *k, const char *v) | |
374 | { | |
a159ca0c | 375 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
c91f0d92 JK |
376 | wt_status_use_color = git_config_colorbool(k, v); |
377 | return 0; | |
378 | } | |
1968d77d | 379 | if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { |
c91f0d92 JK |
380 | int slot = parse_status_slot(k, 13); |
381 | color_parse(v, k, wt_status_colors[slot]); | |
382 | } | |
383 | return git_default_config(k, v); | |
384 | } |