git-commit.sh: convert run_status to a C builtin
[git/peff.git] / wt-status.c
blobec2c7286b1fdbddda8e305a32549ca1362e36b64
1 #include "wt-status.h"
2 #include "color.h"
3 #include "cache.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"
11 int wt_status_use_color = 0;
12 static char wt_status_colors[][COLOR_MAXLEN] = {
13 "", /* WT_STATUS_HEADER: normal */
14 "\033[32m", /* WT_STATUS_UPDATED: green */
15 "\033[31m", /* WT_STATUS_CHANGED: red */
16 "\033[31m", /* WT_STATUS_UNTRACKED: red */
19 static int parse_status_slot(const char *var, int offset)
21 if (!strcasecmp(var+offset, "header"))
22 return WT_STATUS_HEADER;
23 if (!strcasecmp(var+offset, "updated"))
24 return WT_STATUS_UPDATED;
25 if (!strcasecmp(var+offset, "changed"))
26 return WT_STATUS_CHANGED;
27 if (!strcasecmp(var+offset, "untracked"))
28 return WT_STATUS_UNTRACKED;
29 die("bad config variable '%s'", var);
32 static const char* color(int slot)
34 return wt_status_use_color ? wt_status_colors[slot] : "";
37 void wt_status_prepare(struct wt_status *s)
39 unsigned char sha1[20];
40 const char *head;
42 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
44 head = resolve_ref(git_path("HEAD"), sha1, 0);
45 s->branch = head ?
46 strdup(head + strlen(get_git_dir()) + 1) :
47 NULL;
49 s->reference = "HEAD";
50 s->amend = 0;
51 s->verbose = 0;
52 s->commitable = 0;
55 static void wt_status_print_header(const char *main, const char *sub)
57 const char *c = color(WT_STATUS_HEADER);
58 color_printf_ln(c, "# %s:", main);
59 color_printf_ln(c, "# (%s)", sub);
60 color_printf_ln(c, "#");
63 static void wt_status_print_trailer(void)
65 color_printf_ln(color(WT_STATUS_HEADER), "#");
68 static void wt_status_print_filepair(int t, struct diff_filepair *p)
70 const char *c = color(t);
71 color_printf(color(WT_STATUS_HEADER), "#\t");
72 switch (p->status) {
73 case DIFF_STATUS_ADDED:
74 color_printf(c, "new file: %s", p->one->path); break;
75 case DIFF_STATUS_COPIED:
76 color_printf(c, "copied: %s -> %s",
77 p->one->path, p->two->path);
78 break;
79 case DIFF_STATUS_DELETED:
80 color_printf_ln(c, "deleted: %s", p->one->path); break;
81 case DIFF_STATUS_MODIFIED:
82 color_printf(c, "modified: %s", p->one->path); break;
83 case DIFF_STATUS_RENAMED:
84 color_printf(c, "renamed: %s -> %s",
85 p->one->path, p->two->path);
86 break;
87 case DIFF_STATUS_TYPE_CHANGED:
88 color_printf(c, "typechange: %s", p->one->path); break;
89 case DIFF_STATUS_UNKNOWN:
90 color_printf(c, "unknown: %s", p->one->path); break;
91 case DIFF_STATUS_UNMERGED:
92 color_printf(c, "unmerged: %s", p->one->path); break;
93 default:
94 die("bug: unhandled diff status %c", p->status);
96 printf("\n");
99 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
100 struct diff_options *options,
101 void *data)
103 struct wt_status *s = data;
104 int shown_header = 0;
105 int i;
106 if (q->nr) {
108 for (i = 0; i < q->nr; i++) {
109 if (q->queue[i]->status == 'U')
110 continue;
111 if (!shown_header) {
112 wt_status_print_header("Updated but not checked in",
113 "will commit");
114 s->commitable = 1;
115 shown_header = 1;
117 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
119 if (shown_header)
120 wt_status_print_trailer();
123 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
124 struct diff_options *options,
125 void *data)
127 int i;
128 if (q->nr)
129 wt_status_print_header("Changed but not updated",
130 "use git-update-index to mark for commit");
131 for (i = 0; i < q->nr; i++)
132 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
133 if (q->nr)
134 wt_status_print_trailer();
137 void wt_status_print_initial(struct wt_status *s)
139 int i;
140 read_cache();
141 if (active_nr) {
142 s->commitable = 1;
143 wt_status_print_header("Updated but not checked in",
144 "will commit");
146 for (i = 0; i < active_nr; i++) {
147 color_printf(color(WT_STATUS_HEADER), "#\t");
148 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
149 active_cache[i]->name);
151 if (active_nr)
152 wt_status_print_trailer();
155 static void wt_status_print_updated(struct wt_status *s)
157 struct rev_info rev;
158 const char *argv[] = { NULL, NULL, NULL };
159 argv[1] = s->reference;
160 init_revisions(&rev, NULL);
161 setup_revisions(2, argv, &rev, NULL);
162 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
163 rev.diffopt.format_callback = wt_status_print_updated_cb;
164 rev.diffopt.format_callback_data = s;
165 rev.diffopt.detect_rename = 1;
166 run_diff_index(&rev, 1);
169 static void wt_status_print_changed(struct wt_status *s)
171 struct rev_info rev;
172 const char *argv[] = { NULL, NULL };
173 init_revisions(&rev, "");
174 setup_revisions(1, argv, &rev, NULL);
175 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
176 rev.diffopt.format_callback = wt_status_print_changed_cb;
177 rev.diffopt.format_callback_data = s;
178 run_diff_files(&rev, 0);
181 static void wt_status_print_untracked(const struct wt_status *s)
183 struct dir_struct dir;
184 const char *x;
185 int i;
186 int shown_header = 0;
188 memset(&dir, 0, sizeof(dir));
190 dir.exclude_per_dir = ".gitignore";
191 x = git_path("info/exclude");
192 if (file_exists(x))
193 add_excludes_from_file(&dir, x);
195 read_directory(&dir, ".", "", 0);
196 for(i = 0; i < dir.nr; i++) {
197 /* check for matching entry, which is unmerged; lifted from
198 * builtin-ls-files:show_other_files */
199 struct dir_entry *ent = dir.entries[i];
200 int pos = cache_name_pos(ent->name, ent->len);
201 struct cache_entry *ce;
202 if (0 <= pos)
203 die("bug in wt_status_print_untracked");
204 pos = -pos - 1;
205 if (pos < active_nr) {
206 ce = active_cache[pos];
207 if (ce_namelen(ce) == ent->len &&
208 !memcmp(ce->name, ent->name, ent->len))
209 continue;
211 if (!shown_header) {
212 wt_status_print_header("Untracked files",
213 "use \"git add\" to add to commit");
214 shown_header = 1;
216 color_printf(color(WT_STATUS_HEADER), "#\t");
217 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
218 ent->len, ent->name);
222 static void wt_status_print_verbose(struct wt_status *s)
224 struct rev_info rev;
225 const char *argv[] = { NULL, NULL, NULL };
226 argv[1] = s->reference;
227 init_revisions(&rev, NULL);
228 setup_revisions(2, argv, &rev, NULL);
229 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
230 rev.diffopt.detect_rename = 1;
231 run_diff_index(&rev, 1);
234 void wt_status_print(struct wt_status *s)
236 if (s->branch && strcmp(s->branch, "refs/heads/master"))
237 color_printf_ln(color(WT_STATUS_HEADER),
238 "# On branch %s", s->branch);
240 if (s->is_initial) {
241 color_printf_ln(color(WT_STATUS_HEADER), "#");
242 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
243 color_printf_ln(color(WT_STATUS_HEADER), "#");
244 wt_status_print_initial(s);
246 else {
247 wt_status_print_updated(s);
248 discard_cache();
251 wt_status_print_changed(s);
252 wt_status_print_untracked(s);
254 if (s->verbose && !s->is_initial)
255 wt_status_print_verbose(s);
256 if (!s->commitable)
257 printf("%s\n", s->amend ? "# No changes" : "nothing to commit");
260 int git_status_config(const char *k, const char *v)
262 if (!strcmp(k, "status.color")) {
263 wt_status_use_color = git_config_colorbool(k, v);
264 return 0;
266 if (!strncmp(k, "status.color.", 13)) {
267 int slot = parse_status_slot(k, 13);
268 color_parse(v, k, wt_status_colors[slot]);
270 return git_default_config(k, v);