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