Code

Remove hint to use "git help -a"
[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"
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 };
19 static const char use_add_msg[] =
20 "use \"git add <file>...\" to update what will be committed";
21 static const char use_add_rm_msg[] =
22 "use \"git add/rm <file>...\" to update what will be committed";
23 static const char use_add_to_include_msg[] =
24 "use \"git add <file>...\" to include in what will be committed";
25 static const char *excludes_file;
27 static int parse_status_slot(const char *var, int offset)
28 {
29         if (!strcasecmp(var+offset, "header"))
30                 return WT_STATUS_HEADER;
31         if (!strcasecmp(var+offset, "updated")
32                 || !strcasecmp(var+offset, "added"))
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;
38         die("bad config variable '%s'", var);
39 }
41 static const char* color(int slot)
42 {
43         return wt_status_use_color ? wt_status_colors[slot] : "";
44 }
46 void wt_status_prepare(struct wt_status *s)
47 {
48         unsigned char sha1[20];
49         const char *head;
51         memset(s, 0, sizeof(*s));
52         head = resolve_ref("HEAD", sha1, 0, NULL);
53         s->branch = head ? xstrdup(head) : NULL;
54         s->reference = "HEAD";
55         s->fp = stdout;
56         s->index_file = get_index_file();
57 }
59 static void wt_status_print_cached_header(struct wt_status *s)
60 {
61         const char *c = color(WT_STATUS_HEADER);
62         color_fprintf_ln(s->fp, c, "# Changes to be committed:");
63         if (s->reference) {
64                 color_fprintf_ln(s->fp, c, "#   (use \"git reset %s <file>...\" to unstage)", s->reference);
65         } else {
66                 color_fprintf_ln(s->fp, c, "#   (use \"git rm --cached <file>...\" to unstage)");
67         }
68         color_fprintf_ln(s->fp, c, "#");
69 }
71 static void wt_status_print_header(struct wt_status *s,
72                                    const char *main, const char *sub)
73 {
74         const char *c = color(WT_STATUS_HEADER);
75         color_fprintf_ln(s->fp, c, "# %s:", main);
76         color_fprintf_ln(s->fp, c, "#   (%s)", sub);
77         color_fprintf_ln(s->fp, c, "#");
78 }
80 static void wt_status_print_trailer(struct wt_status *s)
81 {
82         color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
83 }
85 static const char *quote_crlf(const char *in, char *buf, size_t sz)
86 {
87         const char *scan;
88         char *out;
89         const char *ret = in;
91         for (scan = in, out = buf; *scan; scan++) {
92                 int ch = *scan;
93                 int quoted;
95                 switch (ch) {
96                 case '\n':
97                         quoted = 'n';
98                         break;
99                 case '\r':
100                         quoted = 'r';
101                         break;
102                 default:
103                         *out++ = ch;
104                         continue;
105                 }
106                 *out++ = '\\';
107                 *out++ = quoted;
108                 ret = buf;
109         }
110         *out = '\0';
111         return ret;
114 static void wt_status_print_filepair(struct wt_status *s,
115                                      int t, struct diff_filepair *p)
117         const char *c = color(t);
118         const char *one, *two;
119         char onebuf[PATH_MAX], twobuf[PATH_MAX];
121         one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
122         two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
124         color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
125         switch (p->status) {
126         case DIFF_STATUS_ADDED:
127                 color_fprintf(s->fp, c, "new file:   %s", one);
128                 break;
129         case DIFF_STATUS_COPIED:
130                 color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
131                 break;
132         case DIFF_STATUS_DELETED:
133                 color_fprintf(s->fp, c, "deleted:    %s", one);
134                 break;
135         case DIFF_STATUS_MODIFIED:
136                 color_fprintf(s->fp, c, "modified:   %s", one);
137                 break;
138         case DIFF_STATUS_RENAMED:
139                 color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
140                 break;
141         case DIFF_STATUS_TYPE_CHANGED:
142                 color_fprintf(s->fp, c, "typechange: %s", one);
143                 break;
144         case DIFF_STATUS_UNKNOWN:
145                 color_fprintf(s->fp, c, "unknown:    %s", one);
146                 break;
147         case DIFF_STATUS_UNMERGED:
148                 color_fprintf(s->fp, c, "unmerged:   %s", one);
149                 break;
150         default:
151                 die("bug: unhandled diff status %c", p->status);
152         }
153         fprintf(s->fp, "\n");
156 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
157                 struct diff_options *options,
158                 void *data)
160         struct wt_status *s = data;
161         int shown_header = 0;
162         int i;
163         for (i = 0; i < q->nr; i++) {
164                 if (q->queue[i]->status == 'U')
165                         continue;
166                 if (!shown_header) {
167                         wt_status_print_cached_header(s);
168                         s->commitable = 1;
169                         shown_header = 1;
170                 }
171                 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
172         }
173         if (shown_header)
174                 wt_status_print_trailer(s);
177 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
178                         struct diff_options *options,
179                         void *data)
181         struct wt_status *s = data;
182         int i;
183         if (q->nr) {
184                 const char *msg = use_add_msg;
185                 s->workdir_dirty = 1;
186                 for (i = 0; i < q->nr; i++)
187                         if (q->queue[i]->status == DIFF_STATUS_DELETED) {
188                                 msg = use_add_rm_msg;
189                                 break;
190                         }
191                 wt_status_print_header(s, "Changed but not updated", msg);
192         }
193         for (i = 0; i < q->nr; i++)
194                 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
195         if (q->nr)
196                 wt_status_print_trailer(s);
199 static void wt_read_cache(struct wt_status *s)
201         discard_cache();
202         read_cache_from(s->index_file);
205 static void wt_status_print_initial(struct wt_status *s)
207         int i;
208         char buf[PATH_MAX];
210         wt_read_cache(s);
211         if (active_nr) {
212                 s->commitable = 1;
213                 wt_status_print_cached_header(s);
214         }
215         for (i = 0; i < active_nr; i++) {
216                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
217                 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
218                                 quote_crlf(active_cache[i]->name,
219                                            buf, sizeof(buf)));
220         }
221         if (active_nr)
222                 wt_status_print_trailer(s);
225 static void wt_status_print_updated(struct wt_status *s)
227         struct rev_info rev;
228         init_revisions(&rev, NULL);
229         setup_revisions(0, NULL, &rev, s->reference);
230         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
231         rev.diffopt.format_callback = wt_status_print_updated_cb;
232         rev.diffopt.format_callback_data = s;
233         rev.diffopt.detect_rename = 1;
234         rev.diffopt.rename_limit = 100;
235         wt_read_cache(s);
236         run_diff_index(&rev, 1);
239 static void wt_status_print_changed(struct wt_status *s)
241         struct rev_info rev;
242         init_revisions(&rev, "");
243         setup_revisions(0, NULL, &rev, NULL);
244         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
245         rev.diffopt.format_callback = wt_status_print_changed_cb;
246         rev.diffopt.format_callback_data = s;
247         wt_read_cache(s);
248         run_diff_files(&rev, 0);
251 static void wt_status_print_untracked(struct wt_status *s)
253         struct dir_struct dir;
254         const char *x;
255         int i;
256         int shown_header = 0;
258         memset(&dir, 0, sizeof(dir));
260         dir.exclude_per_dir = ".gitignore";
261         if (!s->untracked) {
262                 dir.show_other_directories = 1;
263                 dir.hide_empty_directories = 1;
264         }
265         x = git_path("info/exclude");
266         if (file_exists(x))
267                 add_excludes_from_file(&dir, x);
268         if (excludes_file && file_exists(excludes_file))
269                 add_excludes_from_file(&dir, excludes_file);
271         read_directory(&dir, ".", "", 0, NULL);
272         for(i = 0; i < dir.nr; i++) {
273                 /* check for matching entry, which is unmerged; lifted from
274                  * builtin-ls-files:show_other_files */
275                 struct dir_entry *ent = dir.entries[i];
276                 int pos = cache_name_pos(ent->name, ent->len);
277                 struct cache_entry *ce;
278                 if (0 <= pos)
279                         die("bug in wt_status_print_untracked");
280                 pos = -pos - 1;
281                 if (pos < active_nr) {
282                         ce = active_cache[pos];
283                         if (ce_namelen(ce) == ent->len &&
284                             !memcmp(ce->name, ent->name, ent->len))
285                                 continue;
286                 }
287                 if (!shown_header) {
288                         s->workdir_untracked = 1;
289                         wt_status_print_header(s, "Untracked files",
290                                                use_add_to_include_msg);
291                         shown_header = 1;
292                 }
293                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
294                 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%.*s",
295                                 ent->len, ent->name);
296         }
299 static void wt_status_print_verbose(struct wt_status *s)
301         struct rev_info rev;
302         init_revisions(&rev, NULL);
303         setup_revisions(0, NULL, &rev, s->reference);
304         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
305         rev.diffopt.detect_rename = 1;
306         wt_read_cache(s);
307         run_diff_index(&rev, 1);
310 void wt_status_print(struct wt_status *s)
312         unsigned char sha1[20];
313         s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
315         if (s->branch) {
316                 const char *on_what = "On branch ";
317                 const char *branch_name = s->branch;
318                 if (!prefixcmp(branch_name, "refs/heads/"))
319                         branch_name += 11;
320                 else if (!strcmp(branch_name, "HEAD")) {
321                         branch_name = "";
322                         on_what = "Not currently on any branch.";
323                 }
324                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
325                         "# %s%s", on_what, branch_name);
326         }
328         if (s->is_initial) {
329                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
330                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
331                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
332                 wt_status_print_initial(s);
333         }
334         else {
335                 wt_status_print_updated(s);
336         }
338         wt_status_print_changed(s);
339         wt_status_print_untracked(s);
341         if (s->verbose && !s->is_initial)
342                 wt_status_print_verbose(s);
343         if (!s->commitable) {
344                 if (s->amend)
345                         fprintf(s->fp, "# No changes\n");
346                 else if (s->workdir_dirty)
347                         printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
348                 else if (s->workdir_untracked)
349                         printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
350                 else if (s->is_initial)
351                         printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
352                 else
353                         printf("nothing to commit (working directory clean)\n");
354         }
357 int git_status_config(const char *k, const char *v)
359         if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
360                 wt_status_use_color = git_config_colorbool(k, v);
361                 return 0;
362         }
363         if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
364                 int slot = parse_status_slot(k, 13);
365                 color_parse(v, k, wt_status_colors[slot]);
366         }
367         if (!strcmp(k, "core.excludesfile")) {
368                 if (!v)
369                         die("core.excludesfile without value");
370                 excludes_file = xstrdup(v);
371                 return 0;
372         }
373         return git_default_config(k, v);