Provide git_config with a callback-data parameter
[git/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 #include "quote.h"
11 #include "run-command.h"
12
13 int wt_status_relative_paths = 1;
14 int wt_status_use_color = -1;
15 int wt_status_submodule_summary;
16 static char wt_status_colors[][COLOR_MAXLEN] = {
17 "", /* WT_STATUS_HEADER: normal */
18 "\033[32m", /* WT_STATUS_UPDATED: green */
19 "\033[31m", /* WT_STATUS_CHANGED: red */
20 "\033[31m", /* WT_STATUS_UNTRACKED: red */
21 };
22
23 static const char use_add_msg[] =
24 "use \"git add <file>...\" to update what will be committed";
25 static const char use_add_rm_msg[] =
26 "use \"git add/rm <file>...\" to update what will be committed";
27 static const char use_add_to_include_msg[] =
28 "use \"git add <file>...\" to include in what will be committed";
29
30 static int parse_status_slot(const char *var, int offset)
31 {
32 if (!strcasecmp(var+offset, "header"))
33 return WT_STATUS_HEADER;
34 if (!strcasecmp(var+offset, "updated")
35 || !strcasecmp(var+offset, "added"))
36 return WT_STATUS_UPDATED;
37 if (!strcasecmp(var+offset, "changed"))
38 return WT_STATUS_CHANGED;
39 if (!strcasecmp(var+offset, "untracked"))
40 return WT_STATUS_UNTRACKED;
41 die("bad config variable '%s'", var);
42 }
43
44 static const char* color(int slot)
45 {
46 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
47 }
48
49 void wt_status_prepare(struct wt_status *s)
50 {
51 unsigned char sha1[20];
52 const char *head;
53
54 memset(s, 0, sizeof(*s));
55 head = resolve_ref("HEAD", sha1, 0, NULL);
56 s->branch = head ? xstrdup(head) : NULL;
57 s->reference = "HEAD";
58 s->fp = stdout;
59 s->index_file = get_index_file();
60 }
61
62 static void wt_status_print_cached_header(struct wt_status *s)
63 {
64 const char *c = color(WT_STATUS_HEADER);
65 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
66 if (!s->is_initial) {
67 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
68 } else {
69 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
70 }
71 color_fprintf_ln(s->fp, c, "#");
72 }
73
74 static void wt_status_print_header(struct wt_status *s,
75 const char *main, const char *sub)
76 {
77 const char *c = color(WT_STATUS_HEADER);
78 color_fprintf_ln(s->fp, c, "# %s:", main);
79 color_fprintf_ln(s->fp, c, "# (%s)", sub);
80 color_fprintf_ln(s->fp, c, "#");
81 }
82
83 static void wt_status_print_trailer(struct wt_status *s)
84 {
85 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
86 }
87
88 #define quote_path quote_path_relative
89
90 static void wt_status_print_filepair(struct wt_status *s,
91 int t, struct diff_filepair *p)
92 {
93 const char *c = color(t);
94 const char *one, *two;
95 struct strbuf onebuf, twobuf;
96
97 strbuf_init(&onebuf, 0);
98 strbuf_init(&twobuf, 0);
99 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
100 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
101
102 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
103 switch (p->status) {
104 case DIFF_STATUS_ADDED:
105 color_fprintf(s->fp, c, "new file: %s", one);
106 break;
107 case DIFF_STATUS_COPIED:
108 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
109 break;
110 case DIFF_STATUS_DELETED:
111 color_fprintf(s->fp, c, "deleted: %s", one);
112 break;
113 case DIFF_STATUS_MODIFIED:
114 color_fprintf(s->fp, c, "modified: %s", one);
115 break;
116 case DIFF_STATUS_RENAMED:
117 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
118 break;
119 case DIFF_STATUS_TYPE_CHANGED:
120 color_fprintf(s->fp, c, "typechange: %s", one);
121 break;
122 case DIFF_STATUS_UNKNOWN:
123 color_fprintf(s->fp, c, "unknown: %s", one);
124 break;
125 case DIFF_STATUS_UNMERGED:
126 color_fprintf(s->fp, c, "unmerged: %s", one);
127 break;
128 default:
129 die("bug: unhandled diff status %c", p->status);
130 }
131 fprintf(s->fp, "\n");
132 strbuf_release(&onebuf);
133 strbuf_release(&twobuf);
134 }
135
136 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
137 struct diff_options *options,
138 void *data)
139 {
140 struct wt_status *s = data;
141 int shown_header = 0;
142 int i;
143 for (i = 0; i < q->nr; i++) {
144 if (q->queue[i]->status == 'U')
145 continue;
146 if (!shown_header) {
147 wt_status_print_cached_header(s);
148 s->commitable = 1;
149 shown_header = 1;
150 }
151 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
152 }
153 if (shown_header)
154 wt_status_print_trailer(s);
155 }
156
157 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
158 struct diff_options *options,
159 void *data)
160 {
161 struct wt_status *s = data;
162 int i;
163 if (q->nr) {
164 const char *msg = use_add_msg;
165 s->workdir_dirty = 1;
166 for (i = 0; i < q->nr; i++)
167 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
168 msg = use_add_rm_msg;
169 break;
170 }
171 wt_status_print_header(s, "Changed but not updated", msg);
172 }
173 for (i = 0; i < q->nr; i++)
174 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
175 if (q->nr)
176 wt_status_print_trailer(s);
177 }
178
179 static void wt_status_print_initial(struct wt_status *s)
180 {
181 int i;
182 struct strbuf buf;
183
184 strbuf_init(&buf, 0);
185 if (active_nr) {
186 s->commitable = 1;
187 wt_status_print_cached_header(s);
188 }
189 for (i = 0; i < active_nr; i++) {
190 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
191 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
192 quote_path(active_cache[i]->name, -1,
193 &buf, s->prefix));
194 }
195 if (active_nr)
196 wt_status_print_trailer(s);
197 strbuf_release(&buf);
198 }
199
200 static void wt_status_print_updated(struct wt_status *s)
201 {
202 struct rev_info rev;
203 init_revisions(&rev, NULL);
204 setup_revisions(0, NULL, &rev, s->reference);
205 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
206 rev.diffopt.format_callback = wt_status_print_updated_cb;
207 rev.diffopt.format_callback_data = s;
208 rev.diffopt.detect_rename = 1;
209 rev.diffopt.rename_limit = 100;
210 rev.diffopt.break_opt = 0;
211 run_diff_index(&rev, 1);
212 }
213
214 static void wt_status_print_changed(struct wt_status *s)
215 {
216 struct rev_info rev;
217 init_revisions(&rev, "");
218 setup_revisions(0, NULL, &rev, NULL);
219 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
220 rev.diffopt.format_callback = wt_status_print_changed_cb;
221 rev.diffopt.format_callback_data = s;
222 run_diff_files(&rev, 0);
223 }
224
225 static void wt_status_print_submodule_summary(struct wt_status *s)
226 {
227 struct child_process sm_summary;
228 char summary_limit[64];
229 char index[PATH_MAX];
230 const char *env[] = { index, NULL };
231 const char *argv[] = {
232 "submodule",
233 "summary",
234 "--cached",
235 "--for-status",
236 "--summary-limit",
237 summary_limit,
238 s->amend ? "HEAD^" : "HEAD",
239 NULL
240 };
241
242 sprintf(summary_limit, "%d", wt_status_submodule_summary);
243 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
244
245 memset(&sm_summary, 0, sizeof(sm_summary));
246 sm_summary.argv = argv;
247 sm_summary.env = env;
248 sm_summary.git_cmd = 1;
249 sm_summary.no_stdin = 1;
250 fflush(s->fp);
251 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
252 run_command(&sm_summary);
253 }
254
255 static void wt_status_print_untracked(struct wt_status *s)
256 {
257 struct dir_struct dir;
258 int i;
259 int shown_header = 0;
260 struct strbuf buf;
261
262 strbuf_init(&buf, 0);
263 memset(&dir, 0, sizeof(dir));
264
265 if (!s->untracked) {
266 dir.show_other_directories = 1;
267 dir.hide_empty_directories = 1;
268 }
269 setup_standard_excludes(&dir);
270
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 quote_path(ent->name, ent->len,
296 &buf, s->prefix));
297 }
298 strbuf_release(&buf);
299 }
300
301 static void wt_status_print_verbose(struct wt_status *s)
302 {
303 struct rev_info rev;
304
305 init_revisions(&rev, NULL);
306 setup_revisions(0, NULL, &rev, s->reference);
307 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
308 rev.diffopt.detect_rename = 1;
309 rev.diffopt.file = s->fp;
310 rev.diffopt.close_file = 0;
311 run_diff_index(&rev, 1);
312 }
313
314 void wt_status_print(struct wt_status *s)
315 {
316 unsigned char sha1[20];
317 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
318
319 if (s->branch) {
320 const char *on_what = "On branch ";
321 const char *branch_name = s->branch;
322 if (!prefixcmp(branch_name, "refs/heads/"))
323 branch_name += 11;
324 else if (!strcmp(branch_name, "HEAD")) {
325 branch_name = "";
326 on_what = "Not currently on any branch.";
327 }
328 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
329 "# %s%s", on_what, branch_name);
330 }
331
332 if (s->is_initial) {
333 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
334 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
335 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
336 wt_status_print_initial(s);
337 }
338 else {
339 wt_status_print_updated(s);
340 }
341
342 wt_status_print_changed(s);
343 if (wt_status_submodule_summary)
344 wt_status_print_submodule_summary(s);
345 wt_status_print_untracked(s);
346
347 if (s->verbose && !s->is_initial)
348 wt_status_print_verbose(s);
349 if (!s->commitable) {
350 if (s->amend)
351 fprintf(s->fp, "# No changes\n");
352 else if (s->nowarn)
353 ; /* nothing */
354 else if (s->workdir_dirty)
355 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
356 else if (s->workdir_untracked)
357 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
358 else if (s->is_initial)
359 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
360 else
361 printf("nothing to commit (working directory clean)\n");
362 }
363 }
364
365 int git_status_config(const char *k, const char *v, void *cb)
366 {
367 if (!strcmp(k, "status.submodulesummary")) {
368 int is_bool;
369 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
370 if (is_bool && wt_status_submodule_summary)
371 wt_status_submodule_summary = -1;
372 return 0;
373 }
374 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
375 wt_status_use_color = git_config_colorbool(k, v, -1);
376 return 0;
377 }
378 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
379 int slot = parse_status_slot(k, 13);
380 if (!v)
381 return config_error_nonbool(k);
382 color_parse(v, k, wt_status_colors[slot]);
383 return 0;
384 }
385 if (!strcmp(k, "status.relativepaths")) {
386 wt_status_relative_paths = git_config_bool(k, v);
387 return 0;
388 }
389 return git_color_default_config(k, v, cb);
390 }