]> asedeno.scripts.mit.edu Git - git.git/blob - wt-status.c
Provide better feedback for the untracked only case in status output
[git.git] / wt-status.c
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
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 */
17 };
18 static const char* use_add_msg = "use \"git add <file>...\" to incrementally add content to commit";
19
20 static int parse_status_slot(const char *var, int offset)
21 {
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);
32 }
33
34 static const char* color(int slot)
35 {
36         return wt_status_use_color ? wt_status_colors[slot] : "";
37 }
38
39 void wt_status_prepare(struct wt_status *s)
40 {
41         unsigned char sha1[20];
42         const char *head;
43
44         head = resolve_ref("HEAD", sha1, 0, NULL);
45         s->branch = head ? xstrdup(head) : NULL;
46
47         s->reference = "HEAD";
48         s->amend = 0;
49         s->verbose = 0;
50         s->untracked = 0;
51
52         s->commitable = 0;
53         s->workdir_dirty = 0;
54         s->workdir_untracked = 0;
55 }
56
57 static void wt_status_print_cached_header(const char *reference)
58 {
59         const char *c = color(WT_STATUS_HEADER);
60         color_printf_ln(c, "# Changes to be committed:");
61         if (reference) {
62                 color_printf_ln(c, "#   (use \"git reset %s <file>...\" to unstage)", reference);
63         } else {
64                 color_printf_ln(c, "#   (use \"git rm --cached <file>...\" to unstage)");
65         }
66         color_printf_ln(c, "#");
67 }
68
69 static void wt_status_print_header(const char *main, const char *sub)
70 {
71         const char *c = color(WT_STATUS_HEADER);
72         color_printf_ln(c, "# %s:", main);
73         color_printf_ln(c, "#   (%s)", sub);
74         color_printf_ln(c, "#");
75 }
76
77 static void wt_status_print_trailer(void)
78 {
79         color_printf_ln(color(WT_STATUS_HEADER), "#");
80 }
81
82 static const char *quote_crlf(const char *in, char *buf, size_t sz)
83 {
84         const char *scan;
85         char *out;
86         const char *ret = in;
87
88         for (scan = in, out = buf; *scan; scan++) {
89                 int ch = *scan;
90                 int quoted;
91
92                 switch (ch) {
93                 case '\n':
94                         quoted = 'n';
95                         break;
96                 case '\r':
97                         quoted = 'r';
98                         break;
99                 default:
100                         *out++ = ch;
101                         continue;
102                 }
103                 *out++ = '\\';
104                 *out++ = quoted;
105                 ret = buf;
106         }
107         *out = '\0';
108         return ret;
109 }
110
111 static void wt_status_print_filepair(int t, struct diff_filepair *p)
112 {
113         const char *c = color(t);
114         const char *one, *two;
115         char onebuf[PATH_MAX], twobuf[PATH_MAX];
116
117         one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
118         two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
119
120         color_printf(color(WT_STATUS_HEADER), "#\t");
121         switch (p->status) {
122         case DIFF_STATUS_ADDED:
123                 color_printf(c, "new file:   %s", one);
124                 break;
125         case DIFF_STATUS_COPIED:
126                 color_printf(c, "copied:     %s -> %s", one, two);
127                 break;
128         case DIFF_STATUS_DELETED:
129                 color_printf(c, "deleted:    %s", one);
130                 break;
131         case DIFF_STATUS_MODIFIED:
132                 color_printf(c, "modified:   %s", one);
133                 break;
134         case DIFF_STATUS_RENAMED:
135                 color_printf(c, "renamed:    %s -> %s", one, two);
136                 break;
137         case DIFF_STATUS_TYPE_CHANGED:
138                 color_printf(c, "typechange: %s", one);
139                 break;
140         case DIFF_STATUS_UNKNOWN:
141                 color_printf(c, "unknown:    %s", one);
142                 break;
143         case DIFF_STATUS_UNMERGED:
144                 color_printf(c, "unmerged:   %s", one);
145                 break;
146         default:
147                 die("bug: unhandled diff status %c", p->status);
148         }
149         printf("\n");
150 }
151
152 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
153                 struct diff_options *options,
154                 void *data)
155 {
156         struct wt_status *s = data;
157         int shown_header = 0;
158         int i;
159         for (i = 0; i < q->nr; i++) {
160                 if (q->queue[i]->status == 'U')
161                         continue;
162                 if (!shown_header) {
163                         wt_status_print_cached_header(s->reference);
164                         s->commitable = 1;
165                         shown_header = 1;
166                 }
167                 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
168         }
169         if (shown_header)
170                 wt_status_print_trailer();
171 }
172
173 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
174                         struct diff_options *options,
175                         void *data)
176 {
177         struct wt_status *s = data;
178         int i;
179         if (q->nr) {
180                 s->workdir_dirty = 1;
181                 wt_status_print_header("Changed but not added", use_add_msg);
182         }
183         for (i = 0; i < q->nr; i++)
184                 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
185         if (q->nr)
186                 wt_status_print_trailer();
187 }
188
189 void wt_status_print_initial(struct wt_status *s)
190 {
191         int i;
192         char buf[PATH_MAX];
193
194         read_cache();
195         if (active_nr) {
196                 s->commitable = 1;
197                 wt_status_print_cached_header(NULL);
198         }
199         for (i = 0; i < active_nr; i++) {
200                 color_printf(color(WT_STATUS_HEADER), "#\t");
201                 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
202                                 quote_crlf(active_cache[i]->name,
203                                            buf, sizeof(buf)));
204         }
205         if (active_nr)
206                 wt_status_print_trailer();
207 }
208
209 static void wt_status_print_updated(struct wt_status *s)
210 {
211         struct rev_info rev;
212         init_revisions(&rev, NULL);
213         setup_revisions(0, NULL, &rev, s->reference);
214         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
215         rev.diffopt.format_callback = wt_status_print_updated_cb;
216         rev.diffopt.format_callback_data = s;
217         rev.diffopt.detect_rename = 1;
218         run_diff_index(&rev, 1);
219 }
220
221 static void wt_status_print_changed(struct wt_status *s)
222 {
223         struct rev_info rev;
224         init_revisions(&rev, "");
225         setup_revisions(0, NULL, &rev, NULL);
226         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
227         rev.diffopt.format_callback = wt_status_print_changed_cb;
228         rev.diffopt.format_callback_data = s;
229         run_diff_files(&rev, 0);
230 }
231
232 static void wt_status_print_untracked(struct wt_status *s)
233 {
234         struct dir_struct dir;
235         const char *x;
236         int i;
237         int shown_header = 0;
238
239         memset(&dir, 0, sizeof(dir));
240
241         dir.exclude_per_dir = ".gitignore";
242         if (!s->untracked) {
243                 dir.show_other_directories = 1;
244                 dir.hide_empty_directories = 1;
245         }
246         x = git_path("info/exclude");
247         if (file_exists(x))
248                 add_excludes_from_file(&dir, x);
249
250         read_directory(&dir, ".", "", 0);
251         for(i = 0; i < dir.nr; i++) {
252                 /* check for matching entry, which is unmerged; lifted from
253                  * builtin-ls-files:show_other_files */
254                 struct dir_entry *ent = dir.entries[i];
255                 int pos = cache_name_pos(ent->name, ent->len);
256                 struct cache_entry *ce;
257                 if (0 <= pos)
258                         die("bug in wt_status_print_untracked");
259                 pos = -pos - 1;
260                 if (pos < active_nr) {
261                         ce = active_cache[pos];
262                         if (ce_namelen(ce) == ent->len &&
263                             !memcmp(ce->name, ent->name, ent->len))
264                                 continue;
265                 }
266                 if (!shown_header) {
267                         s->workdir_untracked = 1;
268                         wt_status_print_header("Untracked files", use_add_msg);
269                         shown_header = 1;
270                 }
271                 color_printf(color(WT_STATUS_HEADER), "#\t");
272                 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
273                                 ent->len, ent->name);
274         }
275 }
276
277 static void wt_status_print_verbose(struct wt_status *s)
278 {
279         struct rev_info rev;
280         init_revisions(&rev, NULL);
281         setup_revisions(0, NULL, &rev, s->reference);
282         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
283         rev.diffopt.detect_rename = 1;
284         run_diff_index(&rev, 1);
285 }
286
287 void wt_status_print(struct wt_status *s)
288 {
289         unsigned char sha1[20];
290         s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
291
292         if (s->branch)
293                 color_printf_ln(color(WT_STATUS_HEADER),
294                         "# On branch %s", s->branch);
295
296         if (s->is_initial) {
297                 color_printf_ln(color(WT_STATUS_HEADER), "#");
298                 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
299                 color_printf_ln(color(WT_STATUS_HEADER), "#");
300                 wt_status_print_initial(s);
301         }
302         else {
303                 wt_status_print_updated(s);
304                 discard_cache();
305         }
306
307         wt_status_print_changed(s);
308         wt_status_print_untracked(s);
309
310         if (s->verbose && !s->is_initial)
311                 wt_status_print_verbose(s);
312         if (!s->commitable) {
313                 if (s->amend)
314                         printf("# No changes\n");
315                 else if (s->workdir_dirty)
316                         printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
317                 else if (s->workdir_untracked)
318                         printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
319                 else if (s->is_initial)
320                         printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
321                 else
322                         printf("nothing to commit (working directory clean)\n");
323         }
324 }
325
326 int git_status_config(const char *k, const char *v)
327 {
328         if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
329                 wt_status_use_color = git_config_colorbool(k, v);
330                 return 0;
331         }
332         if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
333                 int slot = parse_status_slot(k, 13);
334                 color_parse(v, k, wt_status_colors[slot]);
335         }
336         return git_default_config(k, v);
337 }