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