Code

52ab41ceb6a5f839a3b0c9ffd69983ea5fdd60f7
[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";
26 static int parse_status_slot(const char *var, int offset)
27 {
28         if (!strcasecmp(var+offset, "header"))
29                 return WT_STATUS_HEADER;
30         if (!strcasecmp(var+offset, "updated")
31                 || !strcasecmp(var+offset, "added"))
32                 return WT_STATUS_UPDATED;
33         if (!strcasecmp(var+offset, "changed"))
34                 return WT_STATUS_CHANGED;
35         if (!strcasecmp(var+offset, "untracked"))
36                 return WT_STATUS_UNTRACKED;
37         die("bad config variable '%s'", var);
38 }
40 static const char* color(int slot)
41 {
42         return wt_status_use_color ? wt_status_colors[slot] : "";
43 }
45 void wt_status_prepare(struct wt_status *s)
46 {
47         unsigned char sha1[20];
48         const char *head;
50         memset(s, 0, sizeof(*s));
51         head = resolve_ref("HEAD", sha1, 0, NULL);
52         s->branch = head ? xstrdup(head) : NULL;
53         s->reference = "HEAD";
54         s->fp = stdout;
55         s->index_file = get_index_file();
56 }
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 }
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 }
79 static void wt_status_print_trailer(struct wt_status *s)
80 {
81         color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
82 }
84 static char *quote_path(const char *in, int len,
85                         struct strbuf *out, const char *prefix)
86 {
87         if (len < 0)
88                 len = strlen(in);
90         strbuf_grow(out, len);
91         strbuf_setlen(out, 0);
92         if (prefix) {
93                 int off = 0;
94                 while (prefix[off] && off < len && prefix[off] == in[off])
95                         if (prefix[off] == '/') {
96                                 prefix += off + 1;
97                                 in += off + 1;
98                                 len -= off + 1;
99                                 off = 0;
100                         } else
101                                 off++;
103                 for (; *prefix; prefix++)
104                         if (*prefix == '/')
105                                 strbuf_addstr(out, "../");
106         }
108         for ( ; len > 0; in++, len--) {
109                 int ch = *in;
111                 switch (ch) {
112                 case '\n':
113                         strbuf_addstr(out, "\\n");
114                         break;
115                 case '\r':
116                         strbuf_addstr(out, "\\r");
117                         break;
118                 default:
119                         strbuf_addch(out, ch);
120                         continue;
121                 }
122         }
124         return out->buf;
127 static void wt_status_print_filepair(struct wt_status *s,
128                                      int t, struct diff_filepair *p)
130         const char *c = color(t);
131         const char *one, *two;
132         struct strbuf onebuf, twobuf;
134         strbuf_init(&onebuf, 0);
135         strbuf_init(&twobuf, 0);
136         one = quote_path(p->one->path, -1, &onebuf, s->prefix);
137         two = quote_path(p->two->path, -1, &twobuf, s->prefix);
139         color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
140         switch (p->status) {
141         case DIFF_STATUS_ADDED:
142                 color_fprintf(s->fp, c, "new file:   %s", one);
143                 break;
144         case DIFF_STATUS_COPIED:
145                 color_fprintf(s->fp, c, "copied:     %s -> %s", one, two);
146                 break;
147         case DIFF_STATUS_DELETED:
148                 color_fprintf(s->fp, c, "deleted:    %s", one);
149                 break;
150         case DIFF_STATUS_MODIFIED:
151                 color_fprintf(s->fp, c, "modified:   %s", one);
152                 break;
153         case DIFF_STATUS_RENAMED:
154                 color_fprintf(s->fp, c, "renamed:    %s -> %s", one, two);
155                 break;
156         case DIFF_STATUS_TYPE_CHANGED:
157                 color_fprintf(s->fp, c, "typechange: %s", one);
158                 break;
159         case DIFF_STATUS_UNKNOWN:
160                 color_fprintf(s->fp, c, "unknown:    %s", one);
161                 break;
162         case DIFF_STATUS_UNMERGED:
163                 color_fprintf(s->fp, c, "unmerged:   %s", one);
164                 break;
165         default:
166                 die("bug: unhandled diff status %c", p->status);
167         }
168         fprintf(s->fp, "\n");
169         strbuf_release(&onebuf);
170         strbuf_release(&twobuf);
173 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
174                 struct diff_options *options,
175                 void *data)
177         struct wt_status *s = data;
178         int shown_header = 0;
179         int i;
180         for (i = 0; i < q->nr; i++) {
181                 if (q->queue[i]->status == 'U')
182                         continue;
183                 if (!shown_header) {
184                         wt_status_print_cached_header(s);
185                         s->commitable = 1;
186                         shown_header = 1;
187                 }
188                 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
189         }
190         if (shown_header)
191                 wt_status_print_trailer(s);
194 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
195                         struct diff_options *options,
196                         void *data)
198         struct wt_status *s = data;
199         int i;
200         if (q->nr) {
201                 const char *msg = use_add_msg;
202                 s->workdir_dirty = 1;
203                 for (i = 0; i < q->nr; i++)
204                         if (q->queue[i]->status == DIFF_STATUS_DELETED) {
205                                 msg = use_add_rm_msg;
206                                 break;
207                         }
208                 wt_status_print_header(s, "Changed but not updated", msg);
209         }
210         for (i = 0; i < q->nr; i++)
211                 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
212         if (q->nr)
213                 wt_status_print_trailer(s);
216 static void wt_read_cache(struct wt_status *s)
218         discard_cache();
219         read_cache_from(s->index_file);
222 static void wt_status_print_initial(struct wt_status *s)
224         int i;
225         struct strbuf buf;
227         strbuf_init(&buf, 0);
228         wt_read_cache(s);
229         if (active_nr) {
230                 s->commitable = 1;
231                 wt_status_print_cached_header(s);
232         }
233         for (i = 0; i < active_nr; i++) {
234                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
235                 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
236                                 quote_path(active_cache[i]->name, -1,
237                                            &buf, s->prefix));
238         }
239         if (active_nr)
240                 wt_status_print_trailer(s);
241         strbuf_release(&buf);
244 static void wt_status_print_updated(struct wt_status *s)
246         struct rev_info rev;
247         init_revisions(&rev, NULL);
248         setup_revisions(0, NULL, &rev, s->reference);
249         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
250         rev.diffopt.format_callback = wt_status_print_updated_cb;
251         rev.diffopt.format_callback_data = s;
252         rev.diffopt.detect_rename = 1;
253         rev.diffopt.rename_limit = 100;
254         wt_read_cache(s);
255         run_diff_index(&rev, 1);
258 static void wt_status_print_changed(struct wt_status *s)
260         struct rev_info rev;
261         init_revisions(&rev, "");
262         setup_revisions(0, NULL, &rev, NULL);
263         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
264         rev.diffopt.format_callback = wt_status_print_changed_cb;
265         rev.diffopt.format_callback_data = s;
266         wt_read_cache(s);
267         run_diff_files(&rev, 0);
270 static void wt_status_print_untracked(struct wt_status *s)
272         struct dir_struct dir;
273         int i;
274         int shown_header = 0;
275         struct strbuf buf;
277         strbuf_init(&buf, 0);
278         memset(&dir, 0, sizeof(dir));
280         if (!s->untracked) {
281                 dir.show_other_directories = 1;
282                 dir.hide_empty_directories = 1;
283         }
284         setup_standard_excludes(&dir);
286         read_directory(&dir, ".", "", 0, NULL);
287         for(i = 0; i < dir.nr; i++) {
288                 /* check for matching entry, which is unmerged; lifted from
289                  * builtin-ls-files:show_other_files */
290                 struct dir_entry *ent = dir.entries[i];
291                 int pos = cache_name_pos(ent->name, ent->len);
292                 struct cache_entry *ce;
293                 if (0 <= pos)
294                         die("bug in wt_status_print_untracked");
295                 pos = -pos - 1;
296                 if (pos < active_nr) {
297                         ce = active_cache[pos];
298                         if (ce_namelen(ce) == ent->len &&
299                             !memcmp(ce->name, ent->name, ent->len))
300                                 continue;
301                 }
302                 if (!shown_header) {
303                         s->workdir_untracked = 1;
304                         wt_status_print_header(s, "Untracked files",
305                                                use_add_to_include_msg);
306                         shown_header = 1;
307                 }
308                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
309                 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
310                                 quote_path(ent->name, ent->len,
311                                         &buf, s->prefix));
312         }
313         strbuf_release(&buf);
316 static void wt_status_print_verbose(struct wt_status *s)
318         struct rev_info rev;
319         int saved_stdout;
321         fflush(s->fp);
323         /* Sigh, the entire diff machinery is hardcoded to output to
324          * stdout.  Do the dup-dance...*/
325         saved_stdout = dup(STDOUT_FILENO);
326         if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
327                 die("couldn't redirect stdout\n");
329         init_revisions(&rev, NULL);
330         setup_revisions(0, NULL, &rev, s->reference);
331         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
332         rev.diffopt.detect_rename = 1;
333         wt_read_cache(s);
334         run_diff_index(&rev, 1);
336         fflush(stdout);
338         if (dup2(saved_stdout, STDOUT_FILENO) < 0)
339                 die("couldn't restore stdout\n");
340         close(saved_stdout);
343 void wt_status_print(struct wt_status *s)
345         unsigned char sha1[20];
346         s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
348         if (s->branch) {
349                 const char *on_what = "On branch ";
350                 const char *branch_name = s->branch;
351                 if (!prefixcmp(branch_name, "refs/heads/"))
352                         branch_name += 11;
353                 else if (!strcmp(branch_name, "HEAD")) {
354                         branch_name = "";
355                         on_what = "Not currently on any branch.";
356                 }
357                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
358                         "# %s%s", on_what, branch_name);
359         }
361         if (s->is_initial) {
362                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
363                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
364                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
365                 wt_status_print_initial(s);
366         }
367         else {
368                 wt_status_print_updated(s);
369         }
371         wt_status_print_changed(s);
372         wt_status_print_untracked(s);
374         if (s->verbose && !s->is_initial)
375                 wt_status_print_verbose(s);
376         if (!s->commitable) {
377                 if (s->amend)
378                         fprintf(s->fp, "# No changes\n");
379                 else if (s->workdir_dirty)
380                         printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
381                 else if (s->workdir_untracked)
382                         printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
383                 else if (s->is_initial)
384                         printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
385                 else
386                         printf("nothing to commit (working directory clean)\n");
387         }
390 int git_status_config(const char *k, const char *v)
392         if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
393                 wt_status_use_color = git_config_colorbool(k, v);
394                 return 0;
395         }
396         if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
397                 int slot = parse_status_slot(k, 13);
398                 color_parse(v, k, wt_status_colors[slot]);
399         }
400         return git_default_config(k, v);