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