git-daemon(1): don't assume git-daemon is in /usr/bin
[git/debian.git] / wt-status.c
blob28c9e637e3dd42a2c2364dd7b9f099f4671861cb
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"
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 "\033[31m", /* WT_STATUS_NOBRANCH: red */
24 static const char use_add_msg[] =
25 "use \"git add <file>...\" to update what will be committed";
26 static const char use_add_rm_msg[] =
27 "use \"git add/rm <file>...\" to update what will be committed";
28 static const char use_add_to_include_msg[] =
29 "use \"git add <file>...\" to include in what will be committed";
30 enum untracked_status_type show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
32 static int parse_status_slot(const char *var, int offset)
34 if (!strcasecmp(var+offset, "header"))
35 return WT_STATUS_HEADER;
36 if (!strcasecmp(var+offset, "updated")
37 || !strcasecmp(var+offset, "added"))
38 return WT_STATUS_UPDATED;
39 if (!strcasecmp(var+offset, "changed"))
40 return WT_STATUS_CHANGED;
41 if (!strcasecmp(var+offset, "untracked"))
42 return WT_STATUS_UNTRACKED;
43 if (!strcasecmp(var+offset, "nobranch"))
44 return WT_STATUS_NOBRANCH;
45 die("bad config variable '%s'", var);
48 static const char* color(int slot)
50 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
53 void wt_status_prepare(struct wt_status *s)
55 unsigned char sha1[20];
56 const char *head;
58 memset(s, 0, sizeof(*s));
59 head = resolve_ref("HEAD", sha1, 0, NULL);
60 s->branch = head ? xstrdup(head) : NULL;
61 s->reference = "HEAD";
62 s->fp = stdout;
63 s->index_file = get_index_file();
66 static void wt_status_print_cached_header(struct wt_status *s)
68 const char *c = color(WT_STATUS_HEADER);
69 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
70 if (!s->is_initial) {
71 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
72 } else {
73 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
75 color_fprintf_ln(s->fp, c, "#");
78 static void wt_status_print_header(struct wt_status *s,
79 const char *main, const char *sub)
81 const char *c = color(WT_STATUS_HEADER);
82 color_fprintf_ln(s->fp, c, "# %s:", main);
83 color_fprintf_ln(s->fp, c, "# (%s)", sub);
84 color_fprintf_ln(s->fp, c, "#");
87 static void wt_status_print_trailer(struct wt_status *s)
89 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
92 #define quote_path quote_path_relative
94 static void wt_status_print_filepair(struct wt_status *s,
95 int t, struct diff_filepair *p)
97 const char *c = color(t);
98 const char *one, *two;
99 struct strbuf onebuf, twobuf;
101 strbuf_init(&onebuf, 0);
102 strbuf_init(&twobuf, 0);
103 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
104 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
106 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
107 switch (p->status) {
108 case DIFF_STATUS_ADDED:
109 color_fprintf(s->fp, c, "new file: %s", one);
110 break;
111 case DIFF_STATUS_COPIED:
112 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
113 break;
114 case DIFF_STATUS_DELETED:
115 color_fprintf(s->fp, c, "deleted: %s", one);
116 break;
117 case DIFF_STATUS_MODIFIED:
118 color_fprintf(s->fp, c, "modified: %s", one);
119 break;
120 case DIFF_STATUS_RENAMED:
121 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
122 break;
123 case DIFF_STATUS_TYPE_CHANGED:
124 color_fprintf(s->fp, c, "typechange: %s", one);
125 break;
126 case DIFF_STATUS_UNKNOWN:
127 color_fprintf(s->fp, c, "unknown: %s", one);
128 break;
129 case DIFF_STATUS_UNMERGED:
130 color_fprintf(s->fp, c, "unmerged: %s", one);
131 break;
132 default:
133 die("bug: unhandled diff status %c", p->status);
135 fprintf(s->fp, "\n");
136 strbuf_release(&onebuf);
137 strbuf_release(&twobuf);
140 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
141 struct diff_options *options,
142 void *data)
144 struct wt_status *s = data;
145 int shown_header = 0;
146 int i;
147 for (i = 0; i < q->nr; i++) {
148 if (q->queue[i]->status == 'U')
149 continue;
150 if (!shown_header) {
151 wt_status_print_cached_header(s);
152 s->commitable = 1;
153 shown_header = 1;
155 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
157 if (shown_header)
158 wt_status_print_trailer(s);
161 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
162 struct diff_options *options,
163 void *data)
165 struct wt_status *s = data;
166 int i;
167 if (q->nr) {
168 const char *msg = use_add_msg;
169 s->workdir_dirty = 1;
170 for (i = 0; i < q->nr; i++)
171 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
172 msg = use_add_rm_msg;
173 break;
175 wt_status_print_header(s, "Changed but not updated", msg);
177 for (i = 0; i < q->nr; i++)
178 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
179 if (q->nr)
180 wt_status_print_trailer(s);
183 static void wt_status_print_initial(struct wt_status *s)
185 int i;
186 struct strbuf buf;
188 strbuf_init(&buf, 0);
189 if (active_nr) {
190 s->commitable = 1;
191 wt_status_print_cached_header(s);
193 for (i = 0; i < active_nr; i++) {
194 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
195 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
196 quote_path(active_cache[i]->name, -1,
197 &buf, s->prefix));
199 if (active_nr)
200 wt_status_print_trailer(s);
201 strbuf_release(&buf);
204 static void wt_status_print_updated(struct wt_status *s)
206 struct rev_info rev;
207 init_revisions(&rev, NULL);
208 setup_revisions(0, NULL, &rev, s->reference);
209 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
210 rev.diffopt.format_callback = wt_status_print_updated_cb;
211 rev.diffopt.format_callback_data = s;
212 rev.diffopt.detect_rename = 1;
213 rev.diffopt.rename_limit = 200;
214 rev.diffopt.break_opt = 0;
215 run_diff_index(&rev, 1);
218 static void wt_status_print_changed(struct wt_status *s)
220 struct rev_info rev;
221 init_revisions(&rev, "");
222 setup_revisions(0, NULL, &rev, NULL);
223 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
224 rev.diffopt.format_callback = wt_status_print_changed_cb;
225 rev.diffopt.format_callback_data = s;
226 run_diff_files(&rev, 0);
229 static void wt_status_print_submodule_summary(struct wt_status *s)
231 struct child_process sm_summary;
232 char summary_limit[64];
233 char index[PATH_MAX];
234 const char *env[] = { index, NULL };
235 const char *argv[] = {
236 "submodule",
237 "summary",
238 "--cached",
239 "--for-status",
240 "--summary-limit",
241 summary_limit,
242 s->amend ? "HEAD^" : "HEAD",
243 NULL
246 sprintf(summary_limit, "%d", wt_status_submodule_summary);
247 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", s->index_file);
249 memset(&sm_summary, 0, sizeof(sm_summary));
250 sm_summary.argv = argv;
251 sm_summary.env = env;
252 sm_summary.git_cmd = 1;
253 sm_summary.no_stdin = 1;
254 fflush(s->fp);
255 sm_summary.out = dup(fileno(s->fp)); /* run_command closes it */
256 run_command(&sm_summary);
259 static void wt_status_print_untracked(struct wt_status *s)
261 struct dir_struct dir;
262 int i;
263 int shown_header = 0;
264 struct strbuf buf;
266 strbuf_init(&buf, 0);
267 memset(&dir, 0, sizeof(dir));
269 if (!s->untracked) {
270 dir.show_other_directories = 1;
271 dir.hide_empty_directories = 1;
273 setup_standard_excludes(&dir);
275 read_directory(&dir, ".", "", 0, NULL);
276 for(i = 0; i < dir.nr; i++) {
277 /* check for matching entry, which is unmerged; lifted from
278 * builtin-ls-files:show_other_files */
279 struct dir_entry *ent = dir.entries[i];
280 int pos = cache_name_pos(ent->name, ent->len);
281 struct cache_entry *ce;
282 if (0 <= pos)
283 die("bug in wt_status_print_untracked");
284 pos = -pos - 1;
285 if (pos < active_nr) {
286 ce = active_cache[pos];
287 if (ce_namelen(ce) == ent->len &&
288 !memcmp(ce->name, ent->name, ent->len))
289 continue;
291 if (!shown_header) {
292 s->workdir_untracked = 1;
293 wt_status_print_header(s, "Untracked files",
294 use_add_to_include_msg);
295 shown_header = 1;
297 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
298 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
299 quote_path(ent->name, ent->len,
300 &buf, s->prefix));
302 strbuf_release(&buf);
305 static void wt_status_print_verbose(struct wt_status *s)
307 struct rev_info rev;
309 init_revisions(&rev, NULL);
310 setup_revisions(0, NULL, &rev, s->reference);
311 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
312 rev.diffopt.detect_rename = 1;
313 rev.diffopt.file = s->fp;
314 rev.diffopt.close_file = 0;
315 run_diff_index(&rev, 1);
318 void wt_status_print(struct wt_status *s)
320 unsigned char sha1[20];
321 const char *branch_color = color(WT_STATUS_HEADER);
323 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
324 if (s->branch) {
325 const char *on_what = "On branch ";
326 const char *branch_name = s->branch;
327 if (!prefixcmp(branch_name, "refs/heads/"))
328 branch_name += 11;
329 else if (!strcmp(branch_name, "HEAD")) {
330 branch_name = "";
331 branch_color = color(WT_STATUS_NOBRANCH);
332 on_what = "Not currently on any branch.";
334 color_fprintf(s->fp, color(WT_STATUS_HEADER), "# ");
335 color_fprintf_ln(s->fp, branch_color, "%s%s", on_what, branch_name);
338 if (s->is_initial) {
339 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
340 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
341 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
342 wt_status_print_initial(s);
344 else {
345 wt_status_print_updated(s);
348 wt_status_print_changed(s);
349 if (wt_status_submodule_summary)
350 wt_status_print_submodule_summary(s);
351 if (show_untracked_files)
352 wt_status_print_untracked(s);
353 else if (s->commitable)
354 fprintf(s->fp, "# Untracked files not listed (use -u option to show untracked files)\n");
356 if (s->verbose && !s->is_initial)
357 wt_status_print_verbose(s);
358 if (!s->commitable) {
359 if (s->amend)
360 fprintf(s->fp, "# No changes\n");
361 else if (s->nowarn)
362 ; /* nothing */
363 else if (s->workdir_dirty)
364 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
365 else if (s->workdir_untracked)
366 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
367 else if (s->is_initial)
368 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
369 else if (!show_untracked_files)
370 printf("nothing to commit (use -u to show untracked files)\n");
371 else
372 printf("nothing to commit (working directory clean)\n");
376 int git_status_config(const char *k, const char *v, void *cb)
378 if (!strcmp(k, "status.submodulesummary")) {
379 int is_bool;
380 wt_status_submodule_summary = git_config_bool_or_int(k, v, &is_bool);
381 if (is_bool && wt_status_submodule_summary)
382 wt_status_submodule_summary = -1;
383 return 0;
385 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
386 wt_status_use_color = git_config_colorbool(k, v, -1);
387 return 0;
389 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
390 int slot = parse_status_slot(k, 13);
391 if (!v)
392 return config_error_nonbool(k);
393 color_parse(v, k, wt_status_colors[slot]);
394 return 0;
396 if (!strcmp(k, "status.relativepaths")) {
397 wt_status_relative_paths = git_config_bool(k, v);
398 return 0;
400 if (!strcmp(k, "status.showuntrackedfiles")) {
401 if (!v)
402 return config_error_nonbool(v);
403 else if (!strcmp(v, "no"))
404 show_untracked_files = SHOW_NO_UNTRACKED_FILES;
405 else if (!strcmp(v, "normal"))
406 show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
407 else if (!strcmp(v, "all"))
408 show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
409 else
410 return error("Invalid untracked files mode '%s'", v);
411 return 0;
413 return git_color_default_config(k, v, cb);