]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
Remove unnecessary git-rm --cached reference from status output
[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};
e54eef9e 18static const char* use_add_msg = "use \"git add <file>...\" to incrementally add content to commit";
c91f0d92
JK
19
20static int parse_status_slot(const char *var, int offset)
21{
22 if (!strcasecmp(var+offset, "header"))
23 return WT_STATUS_HEADER;
82dca848
SP
24 if (!strcasecmp(var+offset, "updated")
25 || !strcasecmp(var+offset, "added"))
c91f0d92
JK
26 return WT_STATUS_UPDATED;
27 if (!strcasecmp(var+offset, "changed"))
28 return WT_STATUS_CHANGED;
29 if (!strcasecmp(var+offset, "untracked"))
30 return WT_STATUS_UNTRACKED;
31 die("bad config variable '%s'", var);
32}
33
34static const char* color(int slot)
35{
36 return wt_status_use_color ? wt_status_colors[slot] : "";
37}
38
39void wt_status_prepare(struct wt_status *s)
40{
41 unsigned char sha1[20];
42 const char *head;
43
8da19775 44 head = resolve_ref("HEAD", sha1, 0, NULL);
f62363fb 45 s->branch = head ? xstrdup(head) : NULL;
c91f0d92
JK
46
47 s->reference = "HEAD";
48 s->amend = 0;
49 s->verbose = 0;
50 s->commitable = 0;
2074cb0a 51 s->untracked = 0;
6e458bf6
JR
52
53 s->workdir_clean = 1;
c91f0d92
JK
54}
55
3c1eb9cb
JR
56static void wt_status_print_cached_header(const char *reference)
57{
58 const char *c = color(WT_STATUS_HEADER);
3368d11f 59 color_printf_ln(c, "# Changes to be committed:");
3c1eb9cb 60 if (reference) {
3368d11f 61 color_printf_ln(c, "# (use \"git reset %s <file>...\" to unstage)", reference);
3c1eb9cb
JR
62 } else {
63 color_printf_ln(c, "# (use \"git rm --cached <file>...\" to unstage)");
64 }
65 color_printf_ln(c, "#");
66}
67
c91f0d92
JK
68static void wt_status_print_header(const char *main, const char *sub)
69{
70 const char *c = color(WT_STATUS_HEADER);
71 color_printf_ln(c, "# %s:", main);
72 color_printf_ln(c, "# (%s)", sub);
73 color_printf_ln(c, "#");
74}
75
76static void wt_status_print_trailer(void)
77{
78 color_printf_ln(color(WT_STATUS_HEADER), "#");
79}
80
3a946802
JH
81static const char *quote_crlf(const char *in, char *buf, size_t sz)
82{
83 const char *scan;
84 char *out;
85 const char *ret = in;
86
87 for (scan = in, out = buf; *scan; scan++) {
88 int ch = *scan;
89 int quoted;
90
91 switch (ch) {
92 case '\n':
93 quoted = 'n';
94 break;
95 case '\r':
96 quoted = 'r';
97 break;
98 default:
99 *out++ = ch;
100 continue;
101 }
102 *out++ = '\\';
103 *out++ = quoted;
104 ret = buf;
105 }
106 *out = '\0';
107 return ret;
108}
109
c91f0d92
JK
110static void wt_status_print_filepair(int t, struct diff_filepair *p)
111{
112 const char *c = color(t);
3a946802
JH
113 const char *one, *two;
114 char onebuf[PATH_MAX], twobuf[PATH_MAX];
115
116 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
117 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
118
c91f0d92
JK
119 color_printf(color(WT_STATUS_HEADER), "#\t");
120 switch (p->status) {
121 case DIFF_STATUS_ADDED:
3a946802
JH
122 color_printf(c, "new file: %s", one);
123 break;
c91f0d92 124 case DIFF_STATUS_COPIED:
3a946802 125 color_printf(c, "copied: %s -> %s", one, two);
c91f0d92
JK
126 break;
127 case DIFF_STATUS_DELETED:
3a946802
JH
128 color_printf(c, "deleted: %s", one);
129 break;
c91f0d92 130 case DIFF_STATUS_MODIFIED:
3a946802
JH
131 color_printf(c, "modified: %s", one);
132 break;
c91f0d92 133 case DIFF_STATUS_RENAMED:
3a946802 134 color_printf(c, "renamed: %s -> %s", one, two);
c91f0d92
JK
135 break;
136 case DIFF_STATUS_TYPE_CHANGED:
3a946802
JH
137 color_printf(c, "typechange: %s", one);
138 break;
c91f0d92 139 case DIFF_STATUS_UNKNOWN:
3a946802
JH
140 color_printf(c, "unknown: %s", one);
141 break;
c91f0d92 142 case DIFF_STATUS_UNMERGED:
3a946802
JH
143 color_printf(c, "unmerged: %s", one);
144 break;
c91f0d92
JK
145 default:
146 die("bug: unhandled diff status %c", p->status);
147 }
148 printf("\n");
149}
150
151static void wt_status_print_updated_cb(struct diff_queue_struct *q,
152 struct diff_options *options,
153 void *data)
154{
155 struct wt_status *s = data;
156 int shown_header = 0;
157 int i;
c91f0d92
JK
158 for (i = 0; i < q->nr; i++) {
159 if (q->queue[i]->status == 'U')
160 continue;
161 if (!shown_header) {
3c1eb9cb 162 wt_status_print_cached_header(s->reference);
c91f0d92
JK
163 s->commitable = 1;
164 shown_header = 1;
165 }
166 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
167 }
168 if (shown_header)
169 wt_status_print_trailer();
170}
171
172static void wt_status_print_changed_cb(struct diff_queue_struct *q,
173 struct diff_options *options,
174 void *data)
175{
6e458bf6 176 struct wt_status *s = data;
c91f0d92 177 int i;
6e458bf6
JR
178 if (q->nr) {
179 s->workdir_clean = 0;
82dca848 180 wt_status_print_header("Changed but not added", use_add_msg);
6e458bf6 181 }
c91f0d92
JK
182 for (i = 0; i < q->nr; i++)
183 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
184 if (q->nr)
185 wt_status_print_trailer();
186}
187
188void wt_status_print_initial(struct wt_status *s)
189{
190 int i;
3a946802
JH
191 char buf[PATH_MAX];
192
c91f0d92
JK
193 read_cache();
194 if (active_nr) {
195 s->commitable = 1;
3c1eb9cb 196 wt_status_print_cached_header(NULL);
c91f0d92
JK
197 }
198 for (i = 0; i < active_nr; i++) {
199 color_printf(color(WT_STATUS_HEADER), "#\t");
200 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
3a946802
JH
201 quote_crlf(active_cache[i]->name,
202 buf, sizeof(buf)));
c91f0d92
JK
203 }
204 if (active_nr)
205 wt_status_print_trailer();
206}
207
208static void wt_status_print_updated(struct wt_status *s)
209{
210 struct rev_info rev;
c91f0d92 211 init_revisions(&rev, NULL);
49b8b292 212 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
213 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
214 rev.diffopt.format_callback = wt_status_print_updated_cb;
215 rev.diffopt.format_callback_data = s;
216 rev.diffopt.detect_rename = 1;
217 run_diff_index(&rev, 1);
218}
219
220static void wt_status_print_changed(struct wt_status *s)
221{
222 struct rev_info rev;
c91f0d92 223 init_revisions(&rev, "");
49b8b292 224 setup_revisions(0, NULL, &rev, NULL);
c91f0d92
JK
225 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
226 rev.diffopt.format_callback = wt_status_print_changed_cb;
227 rev.diffopt.format_callback_data = s;
228 run_diff_files(&rev, 0);
229}
230
6e458bf6 231static void wt_status_print_untracked(struct wt_status *s)
c91f0d92
JK
232{
233 struct dir_struct dir;
234 const char *x;
235 int i;
236 int shown_header = 0;
237
238 memset(&dir, 0, sizeof(dir));
239
240 dir.exclude_per_dir = ".gitignore";
2074cb0a
JS
241 if (!s->untracked) {
242 dir.show_other_directories = 1;
243 dir.hide_empty_directories = 1;
244 }
c91f0d92
JK
245 x = git_path("info/exclude");
246 if (file_exists(x))
247 add_excludes_from_file(&dir, x);
248
249 read_directory(&dir, ".", "", 0);
250 for(i = 0; i < dir.nr; i++) {
251 /* check for matching entry, which is unmerged; lifted from
252 * builtin-ls-files:show_other_files */
253 struct dir_entry *ent = dir.entries[i];
254 int pos = cache_name_pos(ent->name, ent->len);
255 struct cache_entry *ce;
256 if (0 <= pos)
257 die("bug in wt_status_print_untracked");
258 pos = -pos - 1;
259 if (pos < active_nr) {
260 ce = active_cache[pos];
261 if (ce_namelen(ce) == ent->len &&
262 !memcmp(ce->name, ent->name, ent->len))
263 continue;
264 }
265 if (!shown_header) {
6e458bf6 266 s->workdir_clean = 0;
aeb80c70 267 wt_status_print_header("Untracked files", use_add_msg);
c91f0d92
JK
268 shown_header = 1;
269 }
270 color_printf(color(WT_STATUS_HEADER), "#\t");
271 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
272 ent->len, ent->name);
273 }
274}
275
276static void wt_status_print_verbose(struct wt_status *s)
277{
278 struct rev_info rev;
c91f0d92 279 init_revisions(&rev, NULL);
49b8b292 280 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
281 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
282 rev.diffopt.detect_rename = 1;
283 run_diff_index(&rev, 1);
284}
285
286void wt_status_print(struct wt_status *s)
287{
98bf8a47
JR
288 unsigned char sha1[20];
289 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
290
a990999e 291 if (s->branch)
c91f0d92
JK
292 color_printf_ln(color(WT_STATUS_HEADER),
293 "# On branch %s", s->branch);
294
295 if (s->is_initial) {
296 color_printf_ln(color(WT_STATUS_HEADER), "#");
297 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
298 color_printf_ln(color(WT_STATUS_HEADER), "#");
299 wt_status_print_initial(s);
300 }
301 else {
302 wt_status_print_updated(s);
303 discard_cache();
304 }
305
306 wt_status_print_changed(s);
307 wt_status_print_untracked(s);
308
309 if (s->verbose && !s->is_initial)
310 wt_status_print_verbose(s);
6e458bf6
JR
311 if (!s->commitable) {
312 if (s->amend)
313 printf("# No changes\n");
314 else if (s->workdir_clean)
315 printf(s->is_initial
316 ? "nothing to commit\n"
317 : "nothing to commit (working directory matches HEAD)\n");
318 else
319 printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
320 }
c91f0d92
JK
321}
322
323int git_status_config(const char *k, const char *v)
324{
a159ca0c 325 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
c91f0d92
JK
326 wt_status_use_color = git_config_colorbool(k, v);
327 return 0;
328 }
a159ca0c 329 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
c91f0d92
JK
330 int slot = parse_status_slot(k, 13);
331 color_parse(v, k, wt_status_colors[slot]);
332 }
333 return git_default_config(k, v);
334}