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