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