Code

Merge branch 'kh/fetch-optparse'
[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         rev.diffopt.break_opt = 0;
255         wt_read_cache(s);
256         run_diff_index(&rev, 1);
259 static void wt_status_print_changed(struct wt_status *s)
261         struct rev_info rev;
262         init_revisions(&rev, "");
263         setup_revisions(0, NULL, &rev, NULL);
264         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
265         rev.diffopt.format_callback = wt_status_print_changed_cb;
266         rev.diffopt.format_callback_data = s;
267         wt_read_cache(s);
268         run_diff_files(&rev, 0);
271 static void wt_status_print_untracked(struct wt_status *s)
273         struct dir_struct dir;
274         int i;
275         int shown_header = 0;
276         struct strbuf buf;
278         strbuf_init(&buf, 0);
279         memset(&dir, 0, sizeof(dir));
281         if (!s->untracked) {
282                 dir.show_other_directories = 1;
283                 dir.hide_empty_directories = 1;
284         }
285         setup_standard_excludes(&dir);
287         read_directory(&dir, ".", "", 0, NULL);
288         for(i = 0; i < dir.nr; i++) {
289                 /* check for matching entry, which is unmerged; lifted from
290                  * builtin-ls-files:show_other_files */
291                 struct dir_entry *ent = dir.entries[i];
292                 int pos = cache_name_pos(ent->name, ent->len);
293                 struct cache_entry *ce;
294                 if (0 <= pos)
295                         die("bug in wt_status_print_untracked");
296                 pos = -pos - 1;
297                 if (pos < active_nr) {
298                         ce = active_cache[pos];
299                         if (ce_namelen(ce) == ent->len &&
300                             !memcmp(ce->name, ent->name, ent->len))
301                                 continue;
302                 }
303                 if (!shown_header) {
304                         s->workdir_untracked = 1;
305                         wt_status_print_header(s, "Untracked files",
306                                                use_add_to_include_msg);
307                         shown_header = 1;
308                 }
309                 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
310                 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
311                                 quote_path(ent->name, ent->len,
312                                         &buf, s->prefix));
313         }
314         strbuf_release(&buf);
317 static void wt_status_print_verbose(struct wt_status *s)
319         struct rev_info rev;
320         int saved_stdout;
322         fflush(s->fp);
324         /* Sigh, the entire diff machinery is hardcoded to output to
325          * stdout.  Do the dup-dance...*/
326         saved_stdout = dup(STDOUT_FILENO);
327         if (saved_stdout < 0 ||dup2(fileno(s->fp), STDOUT_FILENO) < 0)
328                 die("couldn't redirect stdout\n");
330         init_revisions(&rev, NULL);
331         setup_revisions(0, NULL, &rev, s->reference);
332         rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
333         rev.diffopt.detect_rename = 1;
334         wt_read_cache(s);
335         run_diff_index(&rev, 1);
337         fflush(stdout);
339         if (dup2(saved_stdout, STDOUT_FILENO) < 0)
340                 die("couldn't restore stdout\n");
341         close(saved_stdout);
344 void wt_status_print(struct wt_status *s)
346         unsigned char sha1[20];
347         s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
349         if (s->branch) {
350                 const char *on_what = "On branch ";
351                 const char *branch_name = s->branch;
352                 if (!prefixcmp(branch_name, "refs/heads/"))
353                         branch_name += 11;
354                 else if (!strcmp(branch_name, "HEAD")) {
355                         branch_name = "";
356                         on_what = "Not currently on any branch.";
357                 }
358                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
359                         "# %s%s", on_what, branch_name);
360         }
362         if (s->is_initial) {
363                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
364                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
365                 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
366                 wt_status_print_initial(s);
367         }
368         else {
369                 wt_status_print_updated(s);
370         }
372         wt_status_print_changed(s);
373         wt_status_print_untracked(s);
375         if (s->verbose && !s->is_initial)
376                 wt_status_print_verbose(s);
377         if (!s->commitable) {
378                 if (s->amend)
379                         fprintf(s->fp, "# No changes\n");
380                 else if (s->workdir_dirty)
381                         printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
382                 else if (s->workdir_untracked)
383                         printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
384                 else if (s->is_initial)
385                         printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
386                 else
387                         printf("nothing to commit (working directory clean)\n");
388         }
391 int git_status_config(const char *k, const char *v)
393         if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
394                 wt_status_use_color = git_config_colorbool(k, v, -1);
395                 return 0;
396         }
397         if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
398                 int slot = parse_status_slot(k, 13);
399                 color_parse(v, k, wt_status_colors[slot]);
400         }
401         return git_default_config(k, v);