bash completion: Add completion for 'git init'
[alt-git.git] / wt-status.c
blob889e50f89fc24984f700d14f7033600fa9fdf642
1 #include "cache.h"
2 #include "wt-status.h"
3 #include "color.h"
4 #include "object.h"
5 #include "dir.h"
6 #include "commit.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "diffcore.h"
10 #include "quote.h"
11 #include "run-command.h"
12 #include "remote.h"
14 int wt_status_relative_paths = 1;
15 int wt_status_use_color = -1;
16 int wt_status_submodule_summary;
17 static 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 */
22 "\033[31m", /* WT_STATUS_NOBRANCH: red */
25 static const char use_add_msg[] =
26 "use \"git add <file>...\" to update what will be committed";
27 static const char use_add_rm_msg[] =
28 "use \"git add/rm <file>...\" to update what will be committed";
29 static const char use_add_to_include_msg[] =
30 "use \"git add <file>...\" to include in what will be committed";
31 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
33 static int parse_status_slot(const char *var, int offset)
35 if (!strcasecmp(var+offset, "header"))
36 return WT_STATUS_HEADER;
37 if (!strcasecmp(var+offset, "updated")
38 || !strcasecmp(var+offset, "added"))
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;
44 if (!strcasecmp(var+offset, "nobranch"))
45 return WT_STATUS_NOBRANCH;
46 die("bad config variable '%s'", var);
49 static const char* color(int slot)
51 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
54 void wt_status_prepare(struct wt_status *s)
56 unsigned char sha1[20];
57 const char *head;
59 memset(s, 0, sizeof(*s));
60 head = resolve_ref("HEAD", sha1, 0, NULL);
61 s->branch = head ? xstrdup(head) : NULL;
62 s->reference = "HEAD";
63 s->fp = stdout;
64 s->index_file = get_index_file();
67 static void wt_status_print_cached_header(struct wt_status *s)
69 const char *c = color(WT_STATUS_HEADER);
70 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
71 if (!s->is_initial) {
72 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
73 } else {
74 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
76 color_fprintf_ln(s->fp, c, "#");
79 static void wt_status_print_header(struct wt_status *s,
80 const char *main, const char *sub)
82 const char *c = color(WT_STATUS_HEADER);
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, "#");
88 static void wt_status_print_trailer(struct wt_status *s)
90 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
93 #define quote_path quote_path_relative
95 static void wt_status_print_filepair(struct wt_status *s,
96 int t, struct diff_filepair *p)
98 const char *c = color(t);
99 const char *one, *two;
100 struct strbuf onebuf, twobuf;
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);
107 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
108 switch (p->status) {
109 case DIFF_STATUS_ADDED:
110 color_fprintf(s->fp, c, "new file: %s", one);
111 break;
112 case DIFF_STATUS_COPIED:
113 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
114 break;
115 case DIFF_STATUS_DELETED:
116 color_fprintf(s->fp, c, "deleted: %s", one);
117 break;
118 case DIFF_STATUS_MODIFIED:
119 color_fprintf(s->fp, c, "modified: %s", one);
120 break;
121 case DIFF_STATUS_RENAMED:
122 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
123 break;
124 case DIFF_STATUS_TYPE_CHANGED:
125 color_fprintf(s->fp, c, "typechange: %s", one);
126 break;
127 case DIFF_STATUS_UNKNOWN:
128 color_fprintf(s->fp, c, "unknown: %s", one);
129 break;
130 case DIFF_STATUS_UNMERGED:
131 color_fprintf(s->fp, c, "unmerged: %s", one);
132 break;
133 default:
134 die("bug: unhandled diff status %c", p->status);
136 fprintf(s->fp, "\n");
137 strbuf_release(&onebuf);
138 strbuf_release(&twobuf);
141 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
142 struct diff_options *options,
143 void *data)
145 struct wt_status *s = data;
146 int shown_header = 0;
147 int i;
148 for (i = 0; i < q->nr; i++) {
149 if (q->queue[i]->status == 'U')
150 continue;
151 if (!shown_header) {
152 wt_status_print_cached_header(s);
153 s->commitable = 1;
154 shown_header = 1;
156 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
158 if (shown_header)
159 wt_status_print_trailer(s);
162 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
163 struct diff_options *options,
164 void *data)
166 struct wt_status *s = data;
167 int i;
168 if (q->nr) {
169 const char *msg = use_add_msg;
170 s->workdir_dirty = 1;
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;
176 wt_status_print_header(s, "Changed but not updated", msg);
178 for (i = 0; i < q->nr; i++)
179 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
180 if (q->nr)
181 wt_status_print_trailer(s);
184 static void wt_status_print_initial(struct wt_status *s)
186 int i;
187 struct strbuf buf;
189 strbuf_init(&buf, 0);
190 if (active_nr) {
191 s->commitable = 1;
192 wt_status_print_cached_header(s);
194 for (i = 0; i < active_nr; i++) {
195 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
196 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
197 quote_path(active_cache[i]->name, -1,
198 &buf, s->prefix));
200 if (active_nr)
201 wt_status_print_trailer(s);
202 strbuf_release(&buf);
205 static void wt_status_print_updated(struct wt_status *s)
207 struct rev_info rev;
208 init_revisions(&rev, NULL);
209 setup_revisions(0, NULL, &rev, s->reference);
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;
214 rev.diffopt.rename_limit = 200;
215 rev.diffopt.break_opt = 0;
216 run_diff_index(&rev, 1);
219 static void wt_status_print_changed(struct wt_status *s)
221 struct rev_info rev;
222 init_revisions(&rev, "");
223 setup_revisions(0, NULL, &rev, NULL);
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);
230 static void wt_status_print_submodule_summary(struct wt_status *s)
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
247 sprintf(summary_limit, "%d", wt_status_submodule_summary);
248 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
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);
260 static void wt_status_print_untracked(struct wt_status *s)
262 struct dir_struct dir;
263 int i;
264 int shown_header = 0;
265 struct strbuf buf;
267 strbuf_init(&buf, 0);
268 memset(&dir, 0, sizeof(dir));
270 if (!s->untracked) {
271 dir.show_other_directories = 1;
272 dir.hide_empty_directories = 1;
274 setup_standard_excludes(&dir);
276 read_directory(&dir, ".", "", 0, NULL);
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;
292 if (!shown_header) {
293 s->workdir_untracked = 1;
294 wt_status_print_header(s, "Untracked files",
295 use_add_to_include_msg);
296 shown_header = 1;
298 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
299 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
300 quote_path(ent->name, ent->len,
301 &buf, s->prefix));
303 strbuf_release(&buf);
306 static void wt_status_print_verbose(struct wt_status *s)
308 struct rev_info rev;
310 init_revisions(&rev, NULL);
311 setup_revisions(0, NULL, &rev, s->reference);
312 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
313 rev.diffopt.detect_rename = 1;
314 rev.diffopt.file = s->fp;
315 rev.diffopt.close_file = 0;
316 run_diff_index(&rev, 1);
319 static void wt_status_print_tracking(struct wt_status *s)
321 struct strbuf sb = STRBUF_INIT;
322 const char *cp, *ep;
323 struct branch *branch;
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;
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), "#");
338 void wt_status_print(struct wt_status *s)
340 unsigned char sha1[20];
341 const char *branch_color = color(WT_STATUS_HEADER);
343 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
344 if (s->branch) {
345 const char *on_what = "On branch ";
346 const char *branch_name = s->branch;
347 if (!prefixcmp(branch_name, "refs/heads/"))
348 branch_name += 11;
349 else if (!strcmp(branch_name, "HEAD")) {
350 branch_name = "";
351 branch_color = color(WT_STATUS_NOBRANCH);
352 on_what = "Not currently on any branch.";
354 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
355 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
356 if (!s->is_initial)
357 wt_status_print_tracking(s);
360 if (s->is_initial) {
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), "#");
364 wt_status_print_initial(s);
366 else {
367 wt_status_print_updated(s);
370 wt_status_print_changed(s);
371 if (wt_status_submodule_summary)
372 wt_status_print_submodule_summary(s);
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");
378 if (s->verbose && !s->is_initial)
379 wt_status_print_verbose(s);
380 if (!s->commitable) {
381 if (s->amend)
382 fprintf(s->fp, "# No changes\n");
383 else if (s->nowarn)
384 ; /* nothing */
385 else if (s->workdir_dirty)
386 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
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");
391 else if (!show_untracked_files)
392 printf("nothing to commit (use -u to show untracked files)\n");
393 else
394 printf("nothing to commit (working directory clean)\n");
398 int git_status_config(const char *k, const char *v, void *cb)
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;
407 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
408 wt_status_use_color = git_config_colorbool(k, v, -1);
409 return 0;
411 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
412 int slot = parse_status_slot(k, 13);
413 if (!v)
414 return config_error_nonbool(k);
415 color_parse(v, k, wt_status_colors[slot]);
416 return 0;
418 if (!strcmp(k, "status.relativepaths")) {
419 wt_status_relative_paths = git_config_bool(k, v);
420 return 0;
422 if (!strcmp(k, "status.showuntrackedfiles")) {
423 if (!v)
424 return config_error_nonbool(k);
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;
435 return git_color_default_config(k, v, cb);