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