]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
git-submodule summary: --for-status option
[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"
a734d0b1 10#include "quote.h"
c91f0d92 11
46f721c8 12int wt_status_relative_paths = 1;
6b2f2d98 13int wt_status_use_color = -1;
c91f0d92
JK
14static char wt_status_colors[][COLOR_MAXLEN] = {
15 "", /* WT_STATUS_HEADER: normal */
16 "\033[32m", /* WT_STATUS_UPDATED: green */
17 "\033[31m", /* WT_STATUS_CHANGED: red */
18 "\033[31m", /* WT_STATUS_UNTRACKED: red */
19};
4d229653
JH
20
21static const char use_add_msg[] =
22"use \"git add <file>...\" to update what will be committed";
23static const char use_add_rm_msg[] =
24"use \"git add/rm <file>...\" to update what will be committed";
25static const char use_add_to_include_msg[] =
26"use \"git add <file>...\" to include in what will be committed";
c91f0d92
JK
27
28static int parse_status_slot(const char *var, int offset)
29{
30 if (!strcasecmp(var+offset, "header"))
31 return WT_STATUS_HEADER;
82dca848
SP
32 if (!strcasecmp(var+offset, "updated")
33 || !strcasecmp(var+offset, "added"))
c91f0d92
JK
34 return WT_STATUS_UPDATED;
35 if (!strcasecmp(var+offset, "changed"))
36 return WT_STATUS_CHANGED;
37 if (!strcasecmp(var+offset, "untracked"))
38 return WT_STATUS_UNTRACKED;
39 die("bad config variable '%s'", var);
40}
41
42static const char* color(int slot)
43{
6b2f2d98 44 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
c91f0d92
JK
45}
46
47void wt_status_prepare(struct wt_status *s)
48{
49 unsigned char sha1[20];
50 const char *head;
51
cc46a743 52 memset(s, 0, sizeof(*s));
8da19775 53 head = resolve_ref("HEAD", sha1, 0, NULL);
f62363fb 54 s->branch = head ? xstrdup(head) : NULL;
c91f0d92 55 s->reference = "HEAD";
f26a0012 56 s->fp = stdout;
0f729f21 57 s->index_file = get_index_file();
c91f0d92
JK
58}
59
f26a0012 60static void wt_status_print_cached_header(struct wt_status *s)
3c1eb9cb
JR
61{
62 const char *c = color(WT_STATUS_HEADER);
f26a0012 63 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
ff58b9aa 64 if (!s->is_initial) {
f26a0012 65 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
3c1eb9cb 66 } else {
f26a0012 67 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
3c1eb9cb 68 }
f26a0012 69 color_fprintf_ln(s->fp, c, "#");
3c1eb9cb
JR
70}
71
f26a0012
KH
72static void wt_status_print_header(struct wt_status *s,
73 const char *main, const char *sub)
c91f0d92
JK
74{
75 const char *c = color(WT_STATUS_HEADER);
f26a0012
KH
76 color_fprintf_ln(s->fp, c, "# %s:", main);
77 color_fprintf_ln(s->fp, c, "# (%s)", sub);
78 color_fprintf_ln(s->fp, c, "#");
c91f0d92
JK
79}
80
f26a0012 81static void wt_status_print_trailer(struct wt_status *s)
c91f0d92 82{
f26a0012 83 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
84}
85
a734d0b1 86#define quote_path quote_path_relative
3a946802 87
f26a0012
KH
88static void wt_status_print_filepair(struct wt_status *s,
89 int t, struct diff_filepair *p)
c91f0d92
JK
90{
91 const char *c = color(t);
3a946802 92 const char *one, *two;
367c9886 93 struct strbuf onebuf, twobuf;
3a946802 94
367c9886
JS
95 strbuf_init(&onebuf, 0);
96 strbuf_init(&twobuf, 0);
97 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
98 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
3a946802 99
f26a0012 100 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
c91f0d92
JK
101 switch (p->status) {
102 case DIFF_STATUS_ADDED:
f26a0012 103 color_fprintf(s->fp, c, "new file: %s", one);
3a946802 104 break;
c91f0d92 105 case DIFF_STATUS_COPIED:
f26a0012 106 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
c91f0d92
JK
107 break;
108 case DIFF_STATUS_DELETED:
f26a0012 109 color_fprintf(s->fp, c, "deleted: %s", one);
3a946802 110 break;
c91f0d92 111 case DIFF_STATUS_MODIFIED:
f26a0012 112 color_fprintf(s->fp, c, "modified: %s", one);
3a946802 113 break;
c91f0d92 114 case DIFF_STATUS_RENAMED:
f26a0012 115 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
c91f0d92
JK
116 break;
117 case DIFF_STATUS_TYPE_CHANGED:
f26a0012 118 color_fprintf(s->fp, c, "typechange: %s", one);
3a946802 119 break;
c91f0d92 120 case DIFF_STATUS_UNKNOWN:
f26a0012 121 color_fprintf(s->fp, c, "unknown: %s", one);
3a946802 122 break;
c91f0d92 123 case DIFF_STATUS_UNMERGED:
f26a0012 124 color_fprintf(s->fp, c, "unmerged: %s", one);
3a946802 125 break;
c91f0d92
JK
126 default:
127 die("bug: unhandled diff status %c", p->status);
128 }
f26a0012 129 fprintf(s->fp, "\n");
367c9886
JS
130 strbuf_release(&onebuf);
131 strbuf_release(&twobuf);
c91f0d92
JK
132}
133
134static void wt_status_print_updated_cb(struct diff_queue_struct *q,
135 struct diff_options *options,
136 void *data)
137{
138 struct wt_status *s = data;
139 int shown_header = 0;
140 int i;
c91f0d92
JK
141 for (i = 0; i < q->nr; i++) {
142 if (q->queue[i]->status == 'U')
143 continue;
144 if (!shown_header) {
f26a0012 145 wt_status_print_cached_header(s);
c91f0d92
JK
146 s->commitable = 1;
147 shown_header = 1;
148 }
f26a0012 149 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
c91f0d92
JK
150 }
151 if (shown_header)
f26a0012 152 wt_status_print_trailer(s);
c91f0d92
JK
153}
154
155static void wt_status_print_changed_cb(struct diff_queue_struct *q,
156 struct diff_options *options,
157 void *data)
158{
6e458bf6 159 struct wt_status *s = data;
c91f0d92 160 int i;
6e458bf6 161 if (q->nr) {
4d229653 162 const char *msg = use_add_msg;
2a3a3c24 163 s->workdir_dirty = 1;
4d229653
JH
164 for (i = 0; i < q->nr; i++)
165 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
166 msg = use_add_rm_msg;
167 break;
168 }
f26a0012 169 wt_status_print_header(s, "Changed but not updated", msg);
6e458bf6 170 }
c91f0d92 171 for (i = 0; i < q->nr; i++)
f26a0012 172 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
c91f0d92 173 if (q->nr)
f26a0012 174 wt_status_print_trailer(s);
c91f0d92
JK
175}
176
52fae7de 177static void wt_status_print_initial(struct wt_status *s)
c91f0d92
JK
178{
179 int i;
367c9886 180 struct strbuf buf;
3a946802 181
367c9886 182 strbuf_init(&buf, 0);
c91f0d92
JK
183 if (active_nr) {
184 s->commitable = 1;
f26a0012 185 wt_status_print_cached_header(s);
c91f0d92
JK
186 }
187 for (i = 0; i < active_nr; i++) {
f26a0012
KH
188 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
189 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
367c9886
JS
190 quote_path(active_cache[i]->name, -1,
191 &buf, s->prefix));
c91f0d92
JK
192 }
193 if (active_nr)
f26a0012 194 wt_status_print_trailer(s);
367c9886 195 strbuf_release(&buf);
c91f0d92
JK
196}
197
198static void wt_status_print_updated(struct wt_status *s)
199{
200 struct rev_info rev;
c91f0d92 201 init_revisions(&rev, NULL);
49b8b292 202 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
203 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
204 rev.diffopt.format_callback = wt_status_print_updated_cb;
205 rev.diffopt.format_callback_data = s;
206 rev.diffopt.detect_rename = 1;
0024a549 207 rev.diffopt.rename_limit = 100;
f714fb84 208 rev.diffopt.break_opt = 0;
c91f0d92
JK
209 run_diff_index(&rev, 1);
210}
211
212static void wt_status_print_changed(struct wt_status *s)
213{
214 struct rev_info rev;
c91f0d92 215 init_revisions(&rev, "");
49b8b292 216 setup_revisions(0, NULL, &rev, NULL);
c91f0d92
JK
217 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
218 rev.diffopt.format_callback = wt_status_print_changed_cb;
219 rev.diffopt.format_callback_data = s;
220 run_diff_files(&rev, 0);
221}
222
6e458bf6 223static void wt_status_print_untracked(struct wt_status *s)
c91f0d92
JK
224{
225 struct dir_struct dir;
c91f0d92
JK
226 int i;
227 int shown_header = 0;
367c9886 228 struct strbuf buf;
c91f0d92 229
367c9886 230 strbuf_init(&buf, 0);
c91f0d92
JK
231 memset(&dir, 0, sizeof(dir));
232
2074cb0a
JS
233 if (!s->untracked) {
234 dir.show_other_directories = 1;
235 dir.hide_empty_directories = 1;
236 }
039bc64e 237 setup_standard_excludes(&dir);
c91f0d92 238
9fc42d60 239 read_directory(&dir, ".", "", 0, NULL);
c91f0d92
JK
240 for(i = 0; i < dir.nr; i++) {
241 /* check for matching entry, which is unmerged; lifted from
242 * builtin-ls-files:show_other_files */
243 struct dir_entry *ent = dir.entries[i];
244 int pos = cache_name_pos(ent->name, ent->len);
245 struct cache_entry *ce;
246 if (0 <= pos)
247 die("bug in wt_status_print_untracked");
248 pos = -pos - 1;
249 if (pos < active_nr) {
250 ce = active_cache[pos];
251 if (ce_namelen(ce) == ent->len &&
252 !memcmp(ce->name, ent->name, ent->len))
253 continue;
254 }
255 if (!shown_header) {
2a3a3c24 256 s->workdir_untracked = 1;
f26a0012 257 wt_status_print_header(s, "Untracked files",
4d229653 258 use_add_to_include_msg);
c91f0d92
JK
259 shown_header = 1;
260 }
f26a0012 261 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
367c9886
JS
262 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
263 quote_path(ent->name, ent->len,
264 &buf, s->prefix));
c91f0d92 265 }
367c9886 266 strbuf_release(&buf);
c91f0d92
JK
267}
268
269static void wt_status_print_verbose(struct wt_status *s)
270{
271 struct rev_info rev;
99a12694 272
c91f0d92 273 init_revisions(&rev, NULL);
49b8b292 274 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
275 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
276 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
277 rev.diffopt.file = s->fp;
278 rev.diffopt.close_file = 0;
c91f0d92
JK
279 run_diff_index(&rev, 1);
280}
281
282void wt_status_print(struct wt_status *s)
283{
98bf8a47
JR
284 unsigned char sha1[20];
285 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
286
bda324cf
JH
287 if (s->branch) {
288 const char *on_what = "On branch ";
289 const char *branch_name = s->branch;
cc44c765 290 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
291 branch_name += 11;
292 else if (!strcmp(branch_name, "HEAD")) {
293 branch_name = "";
294 on_what = "Not currently on any branch.";
295 }
f26a0012 296 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
bda324cf
JH
297 "# %s%s", on_what, branch_name);
298 }
c91f0d92
JK
299
300 if (s->is_initial) {
f26a0012
KH
301 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
302 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
303 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
304 wt_status_print_initial(s);
305 }
306 else {
307 wt_status_print_updated(s);
c91f0d92
JK
308 }
309
310 wt_status_print_changed(s);
311 wt_status_print_untracked(s);
312
313 if (s->verbose && !s->is_initial)
314 wt_status_print_verbose(s);
6e458bf6
JR
315 if (!s->commitable) {
316 if (s->amend)
f26a0012 317 fprintf(s->fp, "# No changes\n");
37d07f8f
JH
318 else if (s->nowarn)
319 ; /* nothing */
2a3a3c24 320 else if (s->workdir_dirty)
dcbc7bbe 321 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
2a3a3c24
JR
322 else if (s->workdir_untracked)
323 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
324 else if (s->is_initial)
325 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
326 else
327 printf("nothing to commit (working directory clean)\n");
6e458bf6 328 }
c91f0d92
JK
329}
330
331int git_status_config(const char *k, const char *v)
332{
a159ca0c 333 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
0f6f5a40 334 wt_status_use_color = git_config_colorbool(k, v, -1);
c91f0d92
JK
335 return 0;
336 }
1968d77d 337 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
c91f0d92 338 int slot = parse_status_slot(k, 13);
451d36bb
JH
339 if (!v)
340 return config_error_nonbool(k);
c91f0d92 341 color_parse(v, k, wt_status_colors[slot]);
46f721c8
JK
342 return 0;
343 }
344 if (!strcmp(k, "status.relativepaths")) {
345 wt_status_relative_paths = git_config_bool(k, v);
346 return 0;
c91f0d92 347 }
6b2f2d98 348 return git_color_default_config(k, v);
c91f0d92 349}