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