Merge branch 'maint'
[git/git.git] / wt-status.c
1 #include "wt-status.h"
2 #include "color.h"
3 #include "cache.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 int parse_status_slot(const char *var, int offset)
20 {
21 if (!strcasecmp(var+offset, "header"))
22 return WT_STATUS_HEADER;
23 if (!strcasecmp(var+offset, "updated"))
24 return WT_STATUS_UPDATED;
25 if (!strcasecmp(var+offset, "changed"))
26 return WT_STATUS_CHANGED;
27 if (!strcasecmp(var+offset, "untracked"))
28 return WT_STATUS_UNTRACKED;
29 die("bad config variable '%s'", var);
30 }
31
32 static const char* color(int slot)
33 {
34 return wt_status_use_color ? wt_status_colors[slot] : "";
35 }
36
37 void wt_status_prepare(struct wt_status *s)
38 {
39 unsigned char sha1[20];
40 const char *head;
41
42 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
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->commitable = 0;
51 s->untracked = 0;
52 }
53
54 static void wt_status_print_header(const char *main, const char *sub)
55 {
56 const char *c = color(WT_STATUS_HEADER);
57 color_printf_ln(c, "# %s:", main);
58 color_printf_ln(c, "# (%s)", sub);
59 color_printf_ln(c, "#");
60 }
61
62 static void wt_status_print_trailer(void)
63 {
64 color_printf_ln(color(WT_STATUS_HEADER), "#");
65 }
66
67 static void wt_status_print_filepair(int t, struct diff_filepair *p)
68 {
69 const char *c = color(t);
70 color_printf(color(WT_STATUS_HEADER), "#\t");
71 switch (p->status) {
72 case DIFF_STATUS_ADDED:
73 color_printf(c, "new file: %s", p->one->path); break;
74 case DIFF_STATUS_COPIED:
75 color_printf(c, "copied: %s -> %s",
76 p->one->path, p->two->path);
77 break;
78 case DIFF_STATUS_DELETED:
79 color_printf(c, "deleted: %s", p->one->path); break;
80 case DIFF_STATUS_MODIFIED:
81 color_printf(c, "modified: %s", p->one->path); break;
82 case DIFF_STATUS_RENAMED:
83 color_printf(c, "renamed: %s -> %s",
84 p->one->path, p->two->path);
85 break;
86 case DIFF_STATUS_TYPE_CHANGED:
87 color_printf(c, "typechange: %s", p->one->path); break;
88 case DIFF_STATUS_UNKNOWN:
89 color_printf(c, "unknown: %s", p->one->path); break;
90 case DIFF_STATUS_UNMERGED:
91 color_printf(c, "unmerged: %s", p->one->path); break;
92 default:
93 die("bug: unhandled diff status %c", p->status);
94 }
95 printf("\n");
96 }
97
98 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
99 struct diff_options *options,
100 void *data)
101 {
102 struct wt_status *s = data;
103 int shown_header = 0;
104 int i;
105 for (i = 0; i < q->nr; i++) {
106 if (q->queue[i]->status == 'U')
107 continue;
108 if (!shown_header) {
109 wt_status_print_header("Updated but not checked in",
110 "will commit");
111 s->commitable = 1;
112 shown_header = 1;
113 }
114 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
115 }
116 if (shown_header)
117 wt_status_print_trailer();
118 }
119
120 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
121 struct diff_options *options,
122 void *data)
123 {
124 int i;
125 if (q->nr)
126 wt_status_print_header("Changed but not updated",
127 "use git-update-index to mark for commit");
128 for (i = 0; i < q->nr; i++)
129 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
130 if (q->nr)
131 wt_status_print_trailer();
132 }
133
134 void wt_status_print_initial(struct wt_status *s)
135 {
136 int i;
137 read_cache();
138 if (active_nr) {
139 s->commitable = 1;
140 wt_status_print_header("Updated but not checked in",
141 "will commit");
142 }
143 for (i = 0; i < active_nr; i++) {
144 color_printf(color(WT_STATUS_HEADER), "#\t");
145 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
146 active_cache[i]->name);
147 }
148 if (active_nr)
149 wt_status_print_trailer();
150 }
151
152 static void wt_status_print_updated(struct wt_status *s)
153 {
154 struct rev_info rev;
155 init_revisions(&rev, NULL);
156 setup_revisions(0, NULL, &rev, s->reference);
157 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
158 rev.diffopt.format_callback = wt_status_print_updated_cb;
159 rev.diffopt.format_callback_data = s;
160 rev.diffopt.detect_rename = 1;
161 run_diff_index(&rev, 1);
162 }
163
164 static void wt_status_print_changed(struct wt_status *s)
165 {
166 struct rev_info rev;
167 init_revisions(&rev, "");
168 setup_revisions(0, NULL, &rev, NULL);
169 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
170 rev.diffopt.format_callback = wt_status_print_changed_cb;
171 rev.diffopt.format_callback_data = s;
172 run_diff_files(&rev, 0);
173 }
174
175 static void wt_status_print_untracked(const struct wt_status *s)
176 {
177 struct dir_struct dir;
178 const char *x;
179 int i;
180 int shown_header = 0;
181
182 memset(&dir, 0, sizeof(dir));
183
184 dir.exclude_per_dir = ".gitignore";
185 if (!s->untracked) {
186 dir.show_other_directories = 1;
187 dir.hide_empty_directories = 1;
188 }
189 x = git_path("info/exclude");
190 if (file_exists(x))
191 add_excludes_from_file(&dir, x);
192
193 read_directory(&dir, ".", "", 0);
194 for(i = 0; i < dir.nr; i++) {
195 /* check for matching entry, which is unmerged; lifted from
196 * builtin-ls-files:show_other_files */
197 struct dir_entry *ent = dir.entries[i];
198 int pos = cache_name_pos(ent->name, ent->len);
199 struct cache_entry *ce;
200 if (0 <= pos)
201 die("bug in wt_status_print_untracked");
202 pos = -pos - 1;
203 if (pos < active_nr) {
204 ce = active_cache[pos];
205 if (ce_namelen(ce) == ent->len &&
206 !memcmp(ce->name, ent->name, ent->len))
207 continue;
208 }
209 if (!shown_header) {
210 wt_status_print_header("Untracked files",
211 "use \"git add\" to add to commit");
212 shown_header = 1;
213 }
214 color_printf(color(WT_STATUS_HEADER), "#\t");
215 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
216 ent->len, ent->name);
217 }
218 }
219
220 static void wt_status_print_verbose(struct wt_status *s)
221 {
222 struct rev_info rev;
223 init_revisions(&rev, NULL);
224 setup_revisions(0, NULL, &rev, s->reference);
225 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
226 rev.diffopt.detect_rename = 1;
227 run_diff_index(&rev, 1);
228 }
229
230 void wt_status_print(struct wt_status *s)
231 {
232 if (s->branch && strcmp(s->branch, "refs/heads/master"))
233 color_printf_ln(color(WT_STATUS_HEADER),
234 "# On branch %s", s->branch);
235
236 if (s->is_initial) {
237 color_printf_ln(color(WT_STATUS_HEADER), "#");
238 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
239 color_printf_ln(color(WT_STATUS_HEADER), "#");
240 wt_status_print_initial(s);
241 }
242 else {
243 wt_status_print_updated(s);
244 discard_cache();
245 }
246
247 wt_status_print_changed(s);
248 wt_status_print_untracked(s);
249
250 if (s->verbose && !s->is_initial)
251 wt_status_print_verbose(s);
252 if (!s->commitable)
253 printf("%s\n", s->amend ? "# No changes" : "nothing to commit");
254 }
255
256 int git_status_config(const char *k, const char *v)
257 {
258 if (!strcmp(k, "status.color")) {
259 wt_status_use_color = git_config_colorbool(k, v);
260 return 0;
261 }
262 if (!strncmp(k, "status.color.", 13)) {
263 int slot = parse_status_slot(k, 13);
264 color_parse(v, k, wt_status_colors[slot]);
265 }
266 return git_default_config(k, v);
267 }