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