]>
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 | ||
3a946802 JH |
84 | static const char *quote_crlf(const char *in, char *buf, size_t sz) |
85 | { | |
86 | const char *scan; | |
87 | char *out; | |
88 | const char *ret = in; | |
89 | ||
90 | for (scan = in, out = buf; *scan; scan++) { | |
91 | int ch = *scan; | |
92 | int quoted; | |
93 | ||
94 | switch (ch) { | |
95 | case '\n': | |
96 | quoted = 'n'; | |
97 | break; | |
98 | case '\r': | |
99 | quoted = 'r'; | |
100 | break; | |
101 | default: | |
102 | *out++ = ch; | |
103 | continue; | |
104 | } | |
105 | *out++ = '\\'; | |
106 | *out++ = quoted; | |
107 | ret = buf; | |
108 | } | |
109 | *out = '\0'; | |
110 | return ret; | |
111 | } | |
112 | ||
f26a0012 KH |
113 | static void wt_status_print_filepair(struct wt_status *s, |
114 | int t, struct diff_filepair *p) | |
c91f0d92 JK |
115 | { |
116 | const char *c = color(t); | |
3a946802 JH |
117 | const char *one, *two; |
118 | char onebuf[PATH_MAX], twobuf[PATH_MAX]; | |
119 | ||
120 | one = quote_crlf(p->one->path, onebuf, sizeof(onebuf)); | |
121 | two = quote_crlf(p->two->path, twobuf, sizeof(twobuf)); | |
122 | ||
f26a0012 | 123 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
c91f0d92 JK |
124 | switch (p->status) { |
125 | case DIFF_STATUS_ADDED: | |
f26a0012 | 126 | color_fprintf(s->fp, c, "new file: %s", one); |
3a946802 | 127 | break; |
c91f0d92 | 128 | case DIFF_STATUS_COPIED: |
f26a0012 | 129 | color_fprintf(s->fp, c, "copied: %s -> %s", one, two); |
c91f0d92 JK |
130 | break; |
131 | case DIFF_STATUS_DELETED: | |
f26a0012 | 132 | color_fprintf(s->fp, c, "deleted: %s", one); |
3a946802 | 133 | break; |
c91f0d92 | 134 | case DIFF_STATUS_MODIFIED: |
f26a0012 | 135 | color_fprintf(s->fp, c, "modified: %s", one); |
3a946802 | 136 | break; |
c91f0d92 | 137 | case DIFF_STATUS_RENAMED: |
f26a0012 | 138 | color_fprintf(s->fp, c, "renamed: %s -> %s", one, two); |
c91f0d92 JK |
139 | break; |
140 | case DIFF_STATUS_TYPE_CHANGED: | |
f26a0012 | 141 | color_fprintf(s->fp, c, "typechange: %s", one); |
3a946802 | 142 | break; |
c91f0d92 | 143 | case DIFF_STATUS_UNKNOWN: |
f26a0012 | 144 | color_fprintf(s->fp, c, "unknown: %s", one); |
3a946802 | 145 | break; |
c91f0d92 | 146 | case DIFF_STATUS_UNMERGED: |
f26a0012 | 147 | color_fprintf(s->fp, c, "unmerged: %s", one); |
3a946802 | 148 | break; |
c91f0d92 JK |
149 | default: |
150 | die("bug: unhandled diff status %c", p->status); | |
151 | } | |
f26a0012 | 152 | fprintf(s->fp, "\n"); |
c91f0d92 JK |
153 | } |
154 | ||
155 | static void wt_status_print_updated_cb(struct diff_queue_struct *q, | |
156 | struct diff_options *options, | |
157 | void *data) | |
158 | { | |
159 | struct wt_status *s = data; | |
160 | int shown_header = 0; | |
161 | int i; | |
c91f0d92 JK |
162 | for (i = 0; i < q->nr; i++) { |
163 | if (q->queue[i]->status == 'U') | |
164 | continue; | |
165 | if (!shown_header) { | |
f26a0012 | 166 | wt_status_print_cached_header(s); |
c91f0d92 JK |
167 | s->commitable = 1; |
168 | shown_header = 1; | |
169 | } | |
f26a0012 | 170 | wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]); |
c91f0d92 JK |
171 | } |
172 | if (shown_header) | |
f26a0012 | 173 | wt_status_print_trailer(s); |
c91f0d92 JK |
174 | } |
175 | ||
176 | static void wt_status_print_changed_cb(struct diff_queue_struct *q, | |
177 | struct diff_options *options, | |
178 | void *data) | |
179 | { | |
6e458bf6 | 180 | struct wt_status *s = data; |
c91f0d92 | 181 | int i; |
6e458bf6 | 182 | if (q->nr) { |
4d229653 | 183 | const char *msg = use_add_msg; |
2a3a3c24 | 184 | s->workdir_dirty = 1; |
4d229653 JH |
185 | for (i = 0; i < q->nr; i++) |
186 | if (q->queue[i]->status == DIFF_STATUS_DELETED) { | |
187 | msg = use_add_rm_msg; | |
188 | break; | |
189 | } | |
f26a0012 | 190 | wt_status_print_header(s, "Changed but not updated", msg); |
6e458bf6 | 191 | } |
c91f0d92 | 192 | for (i = 0; i < q->nr; i++) |
f26a0012 | 193 | wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]); |
c91f0d92 | 194 | if (q->nr) |
f26a0012 | 195 | wt_status_print_trailer(s); |
c91f0d92 JK |
196 | } |
197 | ||
b4e1e4a7 JH |
198 | static void wt_read_cache(struct wt_status *s) |
199 | { | |
200 | discard_cache(); | |
0f729f21 | 201 | read_cache_from(s->index_file); |
b4e1e4a7 JH |
202 | } |
203 | ||
52fae7de | 204 | static void wt_status_print_initial(struct wt_status *s) |
c91f0d92 JK |
205 | { |
206 | int i; | |
3a946802 JH |
207 | char buf[PATH_MAX]; |
208 | ||
b4e1e4a7 | 209 | wt_read_cache(s); |
c91f0d92 JK |
210 | if (active_nr) { |
211 | s->commitable = 1; | |
f26a0012 | 212 | wt_status_print_cached_header(s); |
c91f0d92 JK |
213 | } |
214 | for (i = 0; i < active_nr; i++) { | |
f26a0012 KH |
215 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
216 | color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s", | |
3a946802 JH |
217 | quote_crlf(active_cache[i]->name, |
218 | buf, sizeof(buf))); | |
c91f0d92 JK |
219 | } |
220 | if (active_nr) | |
f26a0012 | 221 | wt_status_print_trailer(s); |
c91f0d92 JK |
222 | } |
223 | ||
224 | static void wt_status_print_updated(struct wt_status *s) | |
225 | { | |
226 | struct rev_info rev; | |
c91f0d92 | 227 | init_revisions(&rev, NULL); |
49b8b292 | 228 | setup_revisions(0, NULL, &rev, s->reference); |
c91f0d92 JK |
229 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
230 | rev.diffopt.format_callback = wt_status_print_updated_cb; | |
231 | rev.diffopt.format_callback_data = s; | |
232 | rev.diffopt.detect_rename = 1; | |
0024a549 | 233 | rev.diffopt.rename_limit = 100; |
b4e1e4a7 | 234 | wt_read_cache(s); |
c91f0d92 JK |
235 | run_diff_index(&rev, 1); |
236 | } | |
237 | ||
238 | static void wt_status_print_changed(struct wt_status *s) | |
239 | { | |
240 | struct rev_info rev; | |
c91f0d92 | 241 | init_revisions(&rev, ""); |
49b8b292 | 242 | setup_revisions(0, NULL, &rev, NULL); |
c91f0d92 JK |
243 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; |
244 | rev.diffopt.format_callback = wt_status_print_changed_cb; | |
245 | rev.diffopt.format_callback_data = s; | |
b4e1e4a7 | 246 | wt_read_cache(s); |
c91f0d92 JK |
247 | run_diff_files(&rev, 0); |
248 | } | |
249 | ||
6e458bf6 | 250 | static void wt_status_print_untracked(struct wt_status *s) |
c91f0d92 JK |
251 | { |
252 | struct dir_struct dir; | |
c91f0d92 JK |
253 | int i; |
254 | int shown_header = 0; | |
255 | ||
256 | memset(&dir, 0, sizeof(dir)); | |
257 | ||
2074cb0a JS |
258 | if (!s->untracked) { |
259 | dir.show_other_directories = 1; | |
260 | dir.hide_empty_directories = 1; | |
261 | } | |
039bc64e | 262 | setup_standard_excludes(&dir); |
c91f0d92 | 263 | |
9fc42d60 | 264 | read_directory(&dir, ".", "", 0, NULL); |
c91f0d92 JK |
265 | for(i = 0; i < dir.nr; i++) { |
266 | /* check for matching entry, which is unmerged; lifted from | |
267 | * builtin-ls-files:show_other_files */ | |
268 | struct dir_entry *ent = dir.entries[i]; | |
269 | int pos = cache_name_pos(ent->name, ent->len); | |
270 | struct cache_entry *ce; | |
271 | if (0 <= pos) | |
272 | die("bug in wt_status_print_untracked"); | |
273 | pos = -pos - 1; | |
274 | if (pos < active_nr) { | |
275 | ce = active_cache[pos]; | |
276 | if (ce_namelen(ce) == ent->len && | |
277 | !memcmp(ce->name, ent->name, ent->len)) | |
278 | continue; | |
279 | } | |
280 | if (!shown_header) { | |
2a3a3c24 | 281 | s->workdir_untracked = 1; |
f26a0012 | 282 | wt_status_print_header(s, "Untracked files", |
4d229653 | 283 | use_add_to_include_msg); |
c91f0d92 JK |
284 | shown_header = 1; |
285 | } | |
f26a0012 KH |
286 | color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t"); |
287 | color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%.*s", | |
c91f0d92 JK |
288 | ent->len, ent->name); |
289 | } | |
290 | } | |
291 | ||
292 | static void wt_status_print_verbose(struct wt_status *s) | |
293 | { | |
294 | struct rev_info rev; | |
c91f0d92 | 295 | init_revisions(&rev, NULL); |
49b8b292 | 296 | setup_revisions(0, NULL, &rev, s->reference); |
c91f0d92 JK |
297 | rev.diffopt.output_format |= DIFF_FORMAT_PATCH; |
298 | rev.diffopt.detect_rename = 1; | |
b4e1e4a7 | 299 | wt_read_cache(s); |
c91f0d92 JK |
300 | run_diff_index(&rev, 1); |
301 | } | |
302 | ||
303 | void wt_status_print(struct wt_status *s) | |
304 | { | |
98bf8a47 JR |
305 | unsigned char sha1[20]; |
306 | s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0; | |
307 | ||
bda324cf JH |
308 | if (s->branch) { |
309 | const char *on_what = "On branch "; | |
310 | const char *branch_name = s->branch; | |
cc44c765 | 311 | if (!prefixcmp(branch_name, "refs/heads/")) |
bda324cf JH |
312 | branch_name += 11; |
313 | else if (!strcmp(branch_name, "HEAD")) { | |
314 | branch_name = ""; | |
315 | on_what = "Not currently on any branch."; | |
316 | } | |
f26a0012 | 317 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), |
bda324cf JH |
318 | "# %s%s", on_what, branch_name); |
319 | } | |
c91f0d92 JK |
320 | |
321 | if (s->is_initial) { | |
f26a0012 KH |
322 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); |
323 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit"); | |
324 | color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#"); | |
c91f0d92 JK |
325 | wt_status_print_initial(s); |
326 | } | |
327 | else { | |
328 | wt_status_print_updated(s); | |
c91f0d92 JK |
329 | } |
330 | ||
331 | wt_status_print_changed(s); | |
332 | wt_status_print_untracked(s); | |
333 | ||
334 | if (s->verbose && !s->is_initial) | |
335 | wt_status_print_verbose(s); | |
6e458bf6 JR |
336 | if (!s->commitable) { |
337 | if (s->amend) | |
f26a0012 | 338 | fprintf(s->fp, "# No changes\n"); |
2a3a3c24 | 339 | else if (s->workdir_dirty) |
dcbc7bbe | 340 | printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n"); |
2a3a3c24 JR |
341 | else if (s->workdir_untracked) |
342 | printf("nothing added to commit but untracked files present (use \"git add\" to track)\n"); | |
343 | else if (s->is_initial) | |
344 | printf("nothing to commit (create/copy files and use \"git add\" to track)\n"); | |
345 | else | |
346 | printf("nothing to commit (working directory clean)\n"); | |
6e458bf6 | 347 | } |
c91f0d92 JK |
348 | } |
349 | ||
350 | int git_status_config(const char *k, const char *v) | |
351 | { | |
a159ca0c | 352 | if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) { |
c91f0d92 JK |
353 | wt_status_use_color = git_config_colorbool(k, v); |
354 | return 0; | |
355 | } | |
1968d77d | 356 | if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) { |
c91f0d92 JK |
357 | int slot = parse_status_slot(k, 13); |
358 | color_parse(v, k, wt_status_colors[slot]); | |
359 | } | |
360 | return git_default_config(k, v); | |
361 | } |