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