]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
Merge branch 'ks/maint-1.6.0-mailinfo-folded' into maint-1.6.0
[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 277 for(i = 0; i < dir.nr; i++) {
c91f0d92 278 struct dir_entry *ent = dir.entries[i];
98fa4738
JK
279 if (!cache_name_is_other(ent->name, ent->len))
280 continue;
c91f0d92 281 if (!shown_header) {
2a3a3c24 282 s->workdir_untracked = 1;
f26a0012 283 wt_status_print_header(s, "Untracked files",
4d229653 284 use_add_to_include_msg);
c91f0d92
JK
285 shown_header = 1;
286 }
f26a0012 287 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
367c9886
JS
288 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
289 quote_path(ent->name, ent->len,
290 &buf, s->prefix));
c91f0d92 291 }
367c9886 292 strbuf_release(&buf);
c91f0d92
JK
293}
294
295static void wt_status_print_verbose(struct wt_status *s)
296{
297 struct rev_info rev;
99a12694 298
c91f0d92 299 init_revisions(&rev, NULL);
49b8b292 300 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
301 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
302 rev.diffopt.detect_rename = 1;
4ba0cb27
KH
303 rev.diffopt.file = s->fp;
304 rev.diffopt.close_file = 0;
c91f0d92
JK
305 run_diff_index(&rev, 1);
306}
307
b6975ab5
JH
308static void wt_status_print_tracking(struct wt_status *s)
309{
310 struct strbuf sb = STRBUF_INIT;
311 const char *cp, *ep;
312 struct branch *branch;
313
314 assert(s->branch && !s->is_initial);
315 if (prefixcmp(s->branch, "refs/heads/"))
316 return;
317 branch = branch_get(s->branch + 11);
318 if (!format_tracking_info(branch, &sb))
319 return;
320
321 for (cp = sb.buf; (ep = strchr(cp, '\n')) != NULL; cp = ep + 1)
322 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
323 "# %.*s", (int)(ep - cp), cp);
324 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
325}
326
c91f0d92
JK
327void wt_status_print(struct wt_status *s)
328{
98bf8a47 329 unsigned char sha1[20];
950ce2e2 330 const char *branch_color = color(WT_STATUS_HEADER);
98bf8a47 331
950ce2e2 332 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
bda324cf
JH
333 if (s->branch) {
334 const char *on_what = "On branch ";
335 const char *branch_name = s->branch;
cc44c765 336 if (!prefixcmp(branch_name, "refs/heads/"))
bda324cf
JH
337 branch_name += 11;
338 else if (!strcmp(branch_name, "HEAD")) {
339 branch_name = "";
950ce2e2 340 branch_color = color(WT_STATUS_NOBRANCH);
bda324cf
JH
341 on_what = "Not currently on any branch.";
342 }
950ce2e2
CP
343 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
344 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
b6975ab5
JH
345 if (!s->is_initial)
346 wt_status_print_tracking(s);
bda324cf 347 }
c91f0d92
JK
348
349 if (s->is_initial) {
f26a0012
KH
350 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
351 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
352 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
c91f0d92
JK
353 wt_status_print_initial(s);
354 }
355 else {
356 wt_status_print_updated(s);
c91f0d92
JK
357 }
358
359 wt_status_print_changed(s);
ac8d5afc
PY
360 if (wt_status_submodule_summary)
361 wt_status_print_submodule_summary(s);
6c2ce048
MSO
362 if (show_untracked_files)
363 wt_status_print_untracked(s);
364 else if (s->commitable)
365 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
c91f0d92
JK
366
367 if (s->verbose && !s->is_initial)
368 wt_status_print_verbose(s);
6e458bf6
JR
369 if (!s->commitable) {
370 if (s->amend)
f26a0012 371 fprintf(s->fp, "# No changes\n");
37d07f8f
JH
372 else if (s->nowarn)
373 ; /* nothing */
2a3a3c24 374 else if (s->workdir_dirty)
dcbc7bbe 375 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
2a3a3c24
JR
376 else if (s->workdir_untracked)
377 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
378 else if (s->is_initial)
379 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
6c2ce048
MSO
380 else if (!show_untracked_files)
381 printf("nothing to commit (use -u to show untracked files)\n");
2a3a3c24
JR
382 else
383 printf("nothing to commit (working directory clean)\n");
6e458bf6 384 }
c91f0d92
JK
385}
386
ef90d6d4 387int git_status_config(const char *k, const char *v, void *cb)
c91f0d92 388{
ac8d5afc
PY
389 if (!strcmp(k, "status.submodulesummary")) {
390 int is_bool;
391 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
392 if (is_bool && wt_status_submodule_summary)
393 wt_status_submodule_summary = -1;
394 return 0;
395 }
a159ca0c 396 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
0f6f5a40 397 wt_status_use_color = git_config_colorbool(k, v, -1);
c91f0d92
JK
398 return 0;
399 }
1968d77d 400 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
c91f0d92 401 int slot = parse_status_slot(k, 13);
451d36bb
JH
402 if (!v)
403 return config_error_nonbool(k);
c91f0d92 404 color_parse(v, k, wt_status_colors[slot]);
46f721c8
JK
405 return 0;
406 }
407 if (!strcmp(k, "status.relativepaths")) {
408 wt_status_relative_paths = git_config_bool(k, v);
409 return 0;
c91f0d92 410 }
d6293d1f
MSO
411 if (!strcmp(k, "status.showuntrackedfiles")) {
412 if (!v)
c96a6d36 413 return config_error_nonbool(k);
d6293d1f
MSO
414 else if (!strcmp(v, "no"))
415 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
416 else if (!strcmp(v, "normal"))
417 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
418 else if (!strcmp(v, "all"))
419 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
420 else
421 return error("Invalid untracked files mode '%s'", v);
422 return 0;
423 }
ef90d6d4 424 return git_color_default_config(k, v, cb);
c91f0d92 425}