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