Improve "nothing to commit" part of status output
[git/jr.git] / wt-status.c
blobca4690e86b8684eb38909d455f543cf5914e9098
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"
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 */
18 static const char* use_add_msg = "use \"git add <file>...\" to incrementally add content to commit";
20 static int parse_status_slot(const char *var, int offset)
22 if (!strcasecmp(var+offset, "header"))
23 return WT_STATUS_HEADER;
24 if (!strcasecmp(var+offset, "updated")
25 || !strcasecmp(var+offset, "added"))
26 return WT_STATUS_UPDATED;
27 if (!strcasecmp(var+offset, "changed"))
28 return WT_STATUS_CHANGED;
29 if (!strcasecmp(var+offset, "untracked"))
30 return WT_STATUS_UNTRACKED;
31 die("bad config variable '%s'", var);
34 static const char* color(int slot)
36 return wt_status_use_color ? wt_status_colors[slot] : "";
39 void wt_status_prepare(struct wt_status *s)
41 unsigned char sha1[20];
42 const char *head;
44 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
46 head = resolve_ref("HEAD", sha1, 0, NULL);
47 s->branch = head ? xstrdup(head) : NULL;
49 s->reference = "HEAD";
50 s->amend = 0;
51 s->verbose = 0;
52 s->commitable = 0;
53 s->untracked = 0;
55 s->workdir_clean = 1;
58 static void wt_status_print_header(const char *main, const char *sub)
60 const char *c = color(WT_STATUS_HEADER);
61 color_printf_ln(c, "# %s:", main);
62 color_printf_ln(c, "# (%s)", sub);
63 color_printf_ln(c, "#");
66 static void wt_status_print_trailer(void)
68 color_printf_ln(color(WT_STATUS_HEADER), "#");
71 static const char *quote_crlf(const char *in, char *buf, size_t sz)
73 const char *scan;
74 char *out;
75 const char *ret = in;
77 for (scan = in, out = buf; *scan; scan++) {
78 int ch = *scan;
79 int quoted;
81 switch (ch) {
82 case '\n':
83 quoted = 'n';
84 break;
85 case '\r':
86 quoted = 'r';
87 break;
88 default:
89 *out++ = ch;
90 continue;
92 *out++ = '\\';
93 *out++ = quoted;
94 ret = buf;
96 *out = '\0';
97 return ret;
100 static void wt_status_print_filepair(int t, struct diff_filepair *p)
102 const char *c = color(t);
103 const char *one, *two;
104 char onebuf[PATH_MAX], twobuf[PATH_MAX];
106 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
107 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
109 color_printf(color(WT_STATUS_HEADER), "#\t");
110 switch (p->status) {
111 case DIFF_STATUS_ADDED:
112 color_printf(c, "new file: %s", one);
113 break;
114 case DIFF_STATUS_COPIED:
115 color_printf(c, "copied: %s -> %s", one, two);
116 break;
117 case DIFF_STATUS_DELETED:
118 color_printf(c, "deleted: %s", one);
119 break;
120 case DIFF_STATUS_MODIFIED:
121 color_printf(c, "modified: %s", one);
122 break;
123 case DIFF_STATUS_RENAMED:
124 color_printf(c, "renamed: %s -> %s", one, two);
125 break;
126 case DIFF_STATUS_TYPE_CHANGED:
127 color_printf(c, "typechange: %s", one);
128 break;
129 case DIFF_STATUS_UNKNOWN:
130 color_printf(c, "unknown: %s", one);
131 break;
132 case DIFF_STATUS_UNMERGED:
133 color_printf(c, "unmerged: %s", one);
134 break;
135 default:
136 die("bug: unhandled diff status %c", p->status);
138 printf("\n");
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_header("Added but not yet committed",
153 "will commit");
154 s->commitable = 1;
155 shown_header = 1;
157 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
159 if (shown_header)
160 wt_status_print_trailer();
163 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
164 struct diff_options *options,
165 void *data)
167 struct wt_status *s = data;
168 int i;
169 if (q->nr) {
170 s->workdir_clean = 0;
171 wt_status_print_header("Changed but not added", use_add_msg);
173 for (i = 0; i < q->nr; i++)
174 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
175 if (q->nr)
176 wt_status_print_trailer();
179 void wt_status_print_initial(struct wt_status *s)
181 int i;
182 char buf[PATH_MAX];
184 read_cache();
185 if (active_nr) {
186 s->commitable = 1;
187 wt_status_print_header("Added but not yet committed",
188 "will commit");
190 for (i = 0; i < active_nr; i++) {
191 color_printf(color(WT_STATUS_HEADER), "#\t");
192 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
193 quote_crlf(active_cache[i]->name,
194 buf, sizeof(buf)));
196 if (active_nr)
197 wt_status_print_trailer();
200 static void wt_status_print_updated(struct wt_status *s)
202 struct rev_info rev;
203 init_revisions(&rev, NULL);
204 setup_revisions(0, NULL, &rev, s->reference);
205 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
206 rev.diffopt.format_callback = wt_status_print_updated_cb;
207 rev.diffopt.format_callback_data = s;
208 rev.diffopt.detect_rename = 1;
209 run_diff_index(&rev, 1);
212 static void wt_status_print_changed(struct wt_status *s)
214 struct rev_info rev;
215 init_revisions(&rev, "");
216 setup_revisions(0, NULL, &rev, NULL);
217 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
218 rev.diffopt.format_callback = wt_status_print_changed_cb;
219 rev.diffopt.format_callback_data = s;
220 run_diff_files(&rev, 0);
223 static void wt_status_print_untracked(struct wt_status *s)
225 struct dir_struct dir;
226 const char *x;
227 int i;
228 int shown_header = 0;
230 memset(&dir, 0, sizeof(dir));
232 dir.exclude_per_dir = ".gitignore";
233 if (!s->untracked) {
234 dir.show_other_directories = 1;
235 dir.hide_empty_directories = 1;
237 x = git_path("info/exclude");
238 if (file_exists(x))
239 add_excludes_from_file(&dir, x);
241 read_directory(&dir, ".", "", 0);
242 for(i = 0; i < dir.nr; i++) {
243 /* check for matching entry, which is unmerged; lifted from
244 * builtin-ls-files:show_other_files */
245 struct dir_entry *ent = dir.entries[i];
246 int pos = cache_name_pos(ent->name, ent->len);
247 struct cache_entry *ce;
248 if (0 <= pos)
249 die("bug in wt_status_print_untracked");
250 pos = -pos - 1;
251 if (pos < active_nr) {
252 ce = active_cache[pos];
253 if (ce_namelen(ce) == ent->len &&
254 !memcmp(ce->name, ent->name, ent->len))
255 continue;
257 if (!shown_header) {
258 s->workdir_clean = 0;
259 wt_status_print_header("Untracked files", use_add_msg);
260 shown_header = 1;
262 color_printf(color(WT_STATUS_HEADER), "#\t");
263 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
264 ent->len, ent->name);
268 static void wt_status_print_verbose(struct wt_status *s)
270 struct rev_info rev;
271 init_revisions(&rev, NULL);
272 setup_revisions(0, NULL, &rev, s->reference);
273 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
274 rev.diffopt.detect_rename = 1;
275 run_diff_index(&rev, 1);
278 void wt_status_print(struct wt_status *s)
280 if (s->branch)
281 color_printf_ln(color(WT_STATUS_HEADER),
282 "# On branch %s", s->branch);
284 if (s->is_initial) {
285 color_printf_ln(color(WT_STATUS_HEADER), "#");
286 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
287 color_printf_ln(color(WT_STATUS_HEADER), "#");
288 wt_status_print_initial(s);
290 else {
291 wt_status_print_updated(s);
292 discard_cache();
295 wt_status_print_changed(s);
296 wt_status_print_untracked(s);
298 if (s->verbose && !s->is_initial)
299 wt_status_print_verbose(s);
300 if (!s->commitable) {
301 if (s->amend)
302 printf("# No changes\n");
303 else if (s->workdir_clean)
304 printf(s->is_initial
305 ? "nothing to commit\n"
306 : "nothing to commit (working directory matches HEAD)\n");
307 else
308 printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
312 int git_status_config(const char *k, const char *v)
314 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
315 wt_status_use_color = git_config_colorbool(k, v);
316 return 0;
318 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
319 int slot = parse_status_slot(k, 13);
320 color_parse(v, k, wt_status_colors[slot]);
322 return git_default_config(k, v);