]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
Revert part of d089eba (setup: sanitize absolute and funny paths in get_pathspec())
[thirdparty/git.git] / wt-status.c
CommitLineData
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
46f721c8 11int wt_status_relative_paths = 1;
6b2f2d98 12int wt_status_use_color = -1;
c91f0d92
JK
13static char wt_status_colors[][COLOR_MAXLEN] = {
14 "", /* WT_STATUS_HEADER: normal */
15 "\033[32m", /* WT_STATUS_UPDATED: green */
16 "\033[31m", /* WT_STATUS_CHANGED: red */
17 "\033[31m", /* WT_STATUS_UNTRACKED: red */
18};
4d229653
JH
19
20static const char use_add_msg[] =
21"use \"git add <file>...\" to update what will be committed";
22static const char use_add_rm_msg[] =
23"use \"git add/rm <file>...\" to update what will be committed";
24static const char use_add_to_include_msg[] =
25"use \"git add <file>...\" to include in what will be committed";
c91f0d92
JK
26
27static 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;
38 die("bad config variable '%s'", var);
39}
40
41static const char* color(int slot)
42{
6b2f2d98 43 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
c91f0d92
JK
44}
45
46void wt_status_prepare(struct wt_status *s)
47{
48 unsigned char sha1[20];
49 const char *head;
50
cc46a743 51 memset(s, 0, sizeof(*s));
8da19775 52 head = resolve_ref("HEAD", sha1, 0, NULL);
f62363fb 53 s->branch = head ? xstrdup(head) : NULL;
c91f0d92 54 s->reference = "HEAD";
f26a0012 55 s->fp = stdout;
0f729f21 56 s->index_file = get_index_file();
c91f0d92
JK
57}
58
f26a0012 59static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb
JR
60{
61 const char *c = color(WT_STATUS_HEADER);
f26a0012 62 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
ff58b9aa 63 if (!s->is_initial) {
f26a0012 64 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
3c1eb9cb 65 } else {
f26a0012 66 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
3c1eb9cb 67 }
f26a0012 68 color_fprintf_ln(s->fp, c, "#");
3c1eb9cb
JR
69}
70
f26a0012
KH
71static void wt_status_print_header(struct wt_status *s,
72 const char *main, const char *sub)
c91f0d92
JK
73{
74 const char *c = color(WT_STATUS_HEADER);
f26a0012
KH
75 color_fprintf_ln(s->fp, c, "# %s:", main);
76 color_fprintf_ln(s->fp, c, "# (%s)", sub);
77 color_fprintf_ln(s->fp, c, "#");
c91f0d92
JK
78}
79
f26a0012 80static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 81{
f26a0012 82 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
83}
84
367c9886 85static char *quote_path(const char *in, int len,
69e74918 86 struct strbuf *out, const char *prefix)
3a946802 87{
69e74918
JK
88 if (len < 0)
89 len = strlen(in);
3a946802 90
69e74918
JK
91 strbuf_grow(out, len);
92 strbuf_setlen(out, 0);
367c9886
JS
93 if (prefix) {
94 int off = 0;
95 while (prefix[off] && off < len && prefix[off] == in[off])
96 if (prefix[off] == '/') {
97 prefix += off + 1;
98 in += off + 1;
99 len -= off + 1;
100 off = 0;
101 } else
102 off++;
103
104 for (; *prefix; prefix++)
105 if (*prefix == '/')
106 strbuf_addstr(out, "../");
107 }
108
69e74918 109 for ( ; len > 0; in++, len--) {
367c9886 110 int ch = *in;
3a946802
JH
111
112 switch (ch) {
113 case '\n':
367c9886 114 strbuf_addstr(out, "\\n");
3a946802
JH
115 break;
116 case '\r':
367c9886 117 strbuf_addstr(out, "\\r");
3a946802
JH
118 break;
119 default:
367c9886 120 strbuf_addch(out, ch);
3a946802
JH
121 continue;
122 }
3a946802 123 }
367c9886 124
c3ce3261
JK
125 if (!out->len)
126 strbuf_addstr(out, "./");
127
367c9886 128 return out->buf;
3a946802
JH
129}
130
f26a0012
KH
131static void wt_status_print_filepair(struct wt_status *s,
132 int t, struct diff_filepair *p)
c91f0d92
JK
133{
134 const char *c = color(t);
3a946802 135 const char *one, *two;
367c9886 136 struct strbuf onebuf, twobuf;
3a946802 137
367c9886
JS
138 strbuf_init(&onebuf, 0);
139 strbuf_init(&twobuf, 0);
140 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
141 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
3a946802 142
f26a0012 143 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
c91f0d92
JK
144 switch (p->status) {
145 case DIFF_STATUS_ADDED:
f26a0012 146 color_fprintf(s->fp, c, "new file: %s", one);
3a946802 147 break;
c91f0d92 148 case DIFF_STATUS_COPIED:
f26a0012 149 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
c91f0d92
JK
150 break;
151 case DIFF_STATUS_DELETED:
f26a0012 152 color_fprintf(s->fp, c, "deleted: %s", one);
3a946802 153 break;
c91f0d92 154 case DIFF_STATUS_MODIFIED:
f26a0012 155 color_fprintf(s->fp, c, "modified: %s", one);
3a946802 156 break;
c91f0d92 157 case DIFF_STATUS_RENAMED:
f26a0012 158 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
c91f0d92
JK
159 break;
160 case DIFF_STATUS_TYPE_CHANGED:
f26a0012 161 color_fprintf(s->fp, c, "typechange: %s", one);
3a946802 162 break;
c91f0d92 163 case DIFF_STATUS_UNKNOWN:
f26a0012 164 color_fprintf(s->fp, c, "unknown: %s", one);
3a946802 165 break;
c91f0d92 166 case DIFF_STATUS_UNMERGED:
f26a0012 167 color_fprintf(s->fp, c, "unmerged: %s", one);
3a946802 168 break;
c91f0d92
JK
169 default:
170 die("bug: unhandled diff status %c", p->status);
171 }
f26a0012 172 fprintf(s->fp, "\n");
367c9886
JS
173 strbuf_release(&onebuf);
174 strbuf_release(&twobuf);
c91f0d92
JK
175}
176
177static void wt_status_print_updated_cb(struct diff_queue_struct *q,
178 struct diff_options *options,
179 void *data)
180{
181 struct wt_status *s = data;
182 int shown_header = 0;
183 int i;
c91f0d92
JK
184 for (i = 0; i < q->nr; i++) {
185 if (q->queue[i]->status == 'U')
186 continue;
187 if (!shown_header) {
f26a0012 188 wt_status_print_cached_header(s);
c91f0d92
JK
189 s->commitable = 1;
190 shown_header = 1;
191 }
f26a0012 192 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
c91f0d92
JK
193 }
194 if (shown_header)
f26a0012 195 wt_status_print_trailer(s);
c91f0d92
JK
196}
197
198static void wt_status_print_changed_cb(struct diff_queue_struct *q,
199 struct diff_options *options,
200 void *data)
201{
6e458bf6 202 struct wt_status *s = data;
c91f0d92 203 int i;
6e458bf6 204 if (q->nr) {
4d229653 205 const char *msg = use_add_msg;
2a3a3c24 206 s->workdir_dirty = 1;
4d229653
JH
207 for (i = 0; i < q->nr; i++)
208 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
209 msg = use_add_rm_msg;
210 break;
211 }
f26a0012 212 wt_status_print_header(s, "Changed but not updated", msg);
6e458bf6 213 }
c91f0d92 214 for (i = 0; i < q->nr; i++)
f26a0012 215 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
c91f0d92 216 if (q->nr)
f26a0012 217 wt_status_print_trailer(s);
c91f0d92
JK
218}
219
52fae7de 220static void wt_status_print_initial(struct wt_status *s)
c91f0d92
JK
221{
222 int i;
367c9886 223 struct strbuf buf;
3a946802 224
367c9886 225 strbuf_init(&buf, 0);
c91f0d92
JK
226 if (active_nr) {
227 s->commitable = 1;
f26a0012 228 wt_status_print_cached_header(s);
c91f0d92
JK
229 }
230 for (i = 0; i < active_nr; i++) {
f26a0012
KH
231 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
232 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
367c9886
JS
233 quote_path(active_cache[i]->name, -1,
234 &buf, s->prefix));
c91f0d92
JK
235 }
236 if (active_nr)
f26a0012 237 wt_status_print_trailer(s);
367c9886 238 strbuf_release(&buf);
c91f0d92
JK
239}
240
241static void wt_status_print_updated(struct wt_status *s)
242{
243 struct rev_info rev;
c91f0d92 244 init_revisions(&rev, NULL);
49b8b292 245 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
246 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
247 rev.diffopt.format_callback = wt_status_print_updated_cb;
248 rev.diffopt.format_callback_data = s;
249 rev.diffopt.detect_rename = 1;
0024a549 250 rev.diffopt.rename_limit = 100;
f714fb84 251 rev.diffopt.break_opt = 0;
c91f0d92
JK
252 run_diff_index(&rev, 1);
253}
254
255static void wt_status_print_changed(struct wt_status *s)
256{
257 struct rev_info rev;
c91f0d92 258 init_revisions(&rev, "");
49b8b292 259 setup_revisions(0, NULL, &rev, NULL);
c91f0d92
JK
260 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
261 rev.diffopt.format_callback = wt_status_print_changed_cb;
262 rev.diffopt.format_callback_data = s;
263 run_diff_files(&rev, 0);
264}
265
6e458bf6 266static 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;
f26a0012 300 wt_status_print_header(s, "Untracked files",
4d229653 301 use_add_to_include_msg);
c91f0d92
JK
302 shown_header = 1;
303 }
f26a0012 304 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
367c9886
JS
305 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
306 quote_path(ent->name, ent->len,
307 &buf, s->prefix));
c91f0d92 308 }
367c9886 309 strbuf_release(&buf);
c91f0d92
JK
310}
311
312static void wt_status_print_verbose(struct wt_status *s)
313{
314 struct rev_info rev;
99a12694
KH
315 int saved_stdout;
316
317 fflush(s->fp);
318
319 /* Sigh, the entire diff machinery is hardcoded to output to
320 * stdout. Do the dup-dance...*/
321 saved_stdout = dup(STDOUT_FILENO);
322 if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
323 die("couldn't redirect stdout\n");
324
c91f0d92 325 init_revisions(&rev, NULL);
49b8b292 326 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
327 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
328 rev.diffopt.detect_rename = 1;
329 run_diff_index(&rev, 1);
99a12694
KH
330
331 fflush(stdout);
332
333 if (dup2(saved_stdout, STDOUT_FILENO) < 0)
334 die("couldn't restore stdout\n");
335 close(saved_stdout);
c91f0d92
JK
336}
337
338void wt_status_print(struct wt_status *s)
339{
98bf8a47
JR
340 unsigned char sha1[20];
341 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
342
bda324cf
JH
343 if (s->branch) {
344 const char *on_what = "On branch ";
345 const char *branch_name = s->branch;
cc44c765 346 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
347 branch_name += 11;
348 else if (!strcmp(branch_name, "HEAD")) {
349 branch_name = "";
350 on_what = "Not currently on any branch.";
351 }
f26a0012 352 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
bda324cf
JH
353 "# %s%s", on_what, branch_name);
354 }
c91f0d92
JK
355
356 if (s->is_initial) {
f26a0012
KH
357 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
358 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
359 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
360 wt_status_print_initial(s);
361 }
362 else {
363 wt_status_print_updated(s);
c91f0d92
JK
364 }
365
366 wt_status_print_changed(s);
367 wt_status_print_untracked(s);
368
369 if (s->verbose && !s->is_initial)
370 wt_status_print_verbose(s);
6e458bf6
JR
371 if (!s->commitable) {
372 if (s->amend)
f26a0012 373 fprintf(s->fp, "# No changes\n");
37d07f8f
JH
374 else if (s->nowarn)
375 ; /* nothing */
2a3a3c24 376 else if (s->workdir_dirty)
dcbc7bbe 377 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
2a3a3c24
JR
378 else if (s->workdir_untracked)
379 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
380 else if (s->is_initial)
381 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
382 else
383 printf("nothing to commit (working directory clean)\n");
6e458bf6 384 }
c91f0d92
JK
385}
386
387int git_status_config(const char *k, const char *v)
388{
a159ca0c 389 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
0f6f5a40 390 wt_status_use_color = git_config_colorbool(k, v, -1);
c91f0d92
JK
391 return 0;
392 }
1968d77d 393 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
c91f0d92 394 int slot = parse_status_slot(k, 13);
451d36bb
JH
395 if (!v)
396 return config_error_nonbool(k);
c91f0d92 397 color_parse(v, k, wt_status_colors[slot]);
46f721c8
JK
398 return 0;
399 }
400 if (!strcmp(k, "status.relativepaths")) {
401 wt_status_relative_paths = git_config_bool(k, v);
402 return 0;
c91f0d92 403 }
6b2f2d98 404 return git_color_default_config(k, v);
c91f0d92 405}