fix typo in tutorial
[git/git-bigfiles.git] / wt-status.c
blobb3fd57b79df3513d271c23caf3ab46b3ba405e40
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"
12 int wt_status_relative_paths = 1;
13 int wt_status_use_color = -1;
14 static char wt_status_colors[][COLOR_MAXLEN] = {
15 "", /* WT_STATUS_HEADER: normal */
16 "\033[32m", /* WT_STATUS_UPDATED: green */
17 "\033[31m", /* WT_STATUS_CHANGED: red */
18 "\033[31m", /* WT_STATUS_UNTRACKED: red */
21 static const char use_add_msg[] =
22 "use \"git add <file>...\" to update what will be committed";
23 static const char use_add_rm_msg[] =
24 "use \"git add/rm <file>...\" to update what will be committed";
25 static const char use_add_to_include_msg[] =
26 "use \"git add <file>...\" to include in what will be committed";
28 static int parse_status_slot(const char *var, int offset)
30 if (!strcasecmp(var+offset, "header"))
31 return WT_STATUS_HEADER;
32 if (!strcasecmp(var+offset, "updated")
33 || !strcasecmp(var+offset, "added"))
34 return WT_STATUS_UPDATED;
35 if (!strcasecmp(var+offset, "changed"))
36 return WT_STATUS_CHANGED;
37 if (!strcasecmp(var+offset, "untracked"))
38 return WT_STATUS_UNTRACKED;
39 die("bad config variable '%s'", var);
42 static const char* color(int slot)
44 return wt_status_use_color > 0 ? wt_status_colors[slot] : "";
47 void wt_status_prepare(struct wt_status *s)
49 unsigned char sha1[20];
50 const char *head;
52 memset(s, 0, sizeof(*s));
53 head = resolve_ref("HEAD", sha1, 0, NULL);
54 s->branch = head ? xstrdup(head) : NULL;
55 s->reference = "HEAD";
56 s->fp = stdout;
57 s->index_file = get_index_file();
60 static void wt_status_print_cached_header(struct wt_status *s)
62 const char *c = color(WT_STATUS_HEADER);
63 color_fprintf_ln(s->fp, c, "# Changes to be committed:");
64 if (!s->is_initial) {
65 color_fprintf_ln(s->fp, c, "# (use \"git reset %s <file>...\" to unstage)", s->reference);
66 } else {
67 color_fprintf_ln(s->fp, c, "# (use \"git rm --cached <file>...\" to unstage)");
69 color_fprintf_ln(s->fp, c, "#");
72 static void wt_status_print_header(struct wt_status *s,
73 const char *main, const char *sub)
75 const char *c = color(WT_STATUS_HEADER);
76 color_fprintf_ln(s->fp, c, "# %s:", main);
77 color_fprintf_ln(s->fp, c, "# (%s)", sub);
78 color_fprintf_ln(s->fp, c, "#");
81 static void wt_status_print_trailer(struct wt_status *s)
83 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
86 #define quote_path quote_path_relative
88 static void wt_status_print_filepair(struct wt_status *s,
89 int t, struct diff_filepair *p)
91 const char *c = color(t);
92 const char *one, *two;
93 struct strbuf onebuf, twobuf;
95 strbuf_init(&onebuf, 0);
96 strbuf_init(&twobuf, 0);
97 one = quote_path(p->one->path, -1, &onebuf, s->prefix);
98 two = quote_path(p->two->path, -1, &twobuf, s->prefix);
100 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
101 switch (p->status) {
102 case DIFF_STATUS_ADDED:
103 color_fprintf(s->fp, c, "new file: %s", one);
104 break;
105 case DIFF_STATUS_COPIED:
106 color_fprintf(s->fp, c, "copied: %s -> %s", one, two);
107 break;
108 case DIFF_STATUS_DELETED:
109 color_fprintf(s->fp, c, "deleted: %s", one);
110 break;
111 case DIFF_STATUS_MODIFIED:
112 color_fprintf(s->fp, c, "modified: %s", one);
113 break;
114 case DIFF_STATUS_RENAMED:
115 color_fprintf(s->fp, c, "renamed: %s -> %s", one, two);
116 break;
117 case DIFF_STATUS_TYPE_CHANGED:
118 color_fprintf(s->fp, c, "typechange: %s", one);
119 break;
120 case DIFF_STATUS_UNKNOWN:
121 color_fprintf(s->fp, c, "unknown: %s", one);
122 break;
123 case DIFF_STATUS_UNMERGED:
124 color_fprintf(s->fp, c, "unmerged: %s", one);
125 break;
126 default:
127 die("bug: unhandled diff status %c", p->status);
129 fprintf(s->fp, "\n");
130 strbuf_release(&onebuf);
131 strbuf_release(&twobuf);
134 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
135 struct diff_options *options,
136 void *data)
138 struct wt_status *s = data;
139 int shown_header = 0;
140 int i;
141 for (i = 0; i < q->nr; i++) {
142 if (q->queue[i]->status == 'U')
143 continue;
144 if (!shown_header) {
145 wt_status_print_cached_header(s);
146 s->commitable = 1;
147 shown_header = 1;
149 wt_status_print_filepair(s, WT_STATUS_UPDATED, q->queue[i]);
151 if (shown_header)
152 wt_status_print_trailer(s);
155 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
156 struct diff_options *options,
157 void *data)
159 struct wt_status *s = data;
160 int i;
161 if (q->nr) {
162 const char *msg = use_add_msg;
163 s->workdir_dirty = 1;
164 for (i = 0; i < q->nr; i++)
165 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
166 msg = use_add_rm_msg;
167 break;
169 wt_status_print_header(s, "Changed but not updated", msg);
171 for (i = 0; i < q->nr; i++)
172 wt_status_print_filepair(s, WT_STATUS_CHANGED, q->queue[i]);
173 if (q->nr)
174 wt_status_print_trailer(s);
177 static void wt_status_print_initial(struct wt_status *s)
179 int i;
180 struct strbuf buf;
182 strbuf_init(&buf, 0);
183 if (active_nr) {
184 s->commitable = 1;
185 wt_status_print_cached_header(s);
187 for (i = 0; i < active_nr; i++) {
188 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
189 color_fprintf_ln(s->fp, color(WT_STATUS_UPDATED), "new file: %s",
190 quote_path(active_cache[i]->name, -1,
191 &buf, s->prefix));
193 if (active_nr)
194 wt_status_print_trailer(s);
195 strbuf_release(&buf);
198 static void wt_status_print_updated(struct wt_status *s)
200 struct rev_info rev;
201 init_revisions(&rev, NULL);
202 setup_revisions(0, NULL, &rev, s->reference);
203 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
204 rev.diffopt.format_callback = wt_status_print_updated_cb;
205 rev.diffopt.format_callback_data = s;
206 rev.diffopt.detect_rename = 1;
207 rev.diffopt.rename_limit = 100;
208 rev.diffopt.break_opt = 0;
209 run_diff_index(&rev, 1);
212 static void wt_status_print_changed(struct wt_status *s)
214 struct rev_info rev;
215 init_revisions(&rev, "");
216 setup_revisions(0, NULL, &rev, NULL);
217 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
218 rev.diffopt.format_callback = wt_status_print_changed_cb;
219 rev.diffopt.format_callback_data = s;
220 run_diff_files(&rev, 0);
223 static void wt_status_print_untracked(struct wt_status *s)
225 struct dir_struct dir;
226 int i;
227 int shown_header = 0;
228 struct strbuf buf;
230 strbuf_init(&buf, 0);
231 memset(&dir, 0, sizeof(dir));
233 if (!s->untracked) {
234 dir.show_other_directories = 1;
235 dir.hide_empty_directories = 1;
237 setup_standard_excludes(&dir);
239 read_directory(&dir, ".", "", 0, NULL);
240 for(i = 0; i < dir.nr; i++) {
241 /* check for matching entry, which is unmerged; lifted from
242 * builtin-ls-files:show_other_files */
243 struct dir_entry *ent = dir.entries[i];
244 int pos = cache_name_pos(ent->name, ent->len);
245 struct cache_entry *ce;
246 if (0 <= pos)
247 die("bug in wt_status_print_untracked");
248 pos = -pos - 1;
249 if (pos < active_nr) {
250 ce = active_cache[pos];
251 if (ce_namelen(ce) == ent->len &&
252 !memcmp(ce->name, ent->name, ent->len))
253 continue;
255 if (!shown_header) {
256 s->workdir_untracked = 1;
257 wt_status_print_header(s, "Untracked files",
258 use_add_to_include_msg);
259 shown_header = 1;
261 color_fprintf(s->fp, color(WT_STATUS_HEADER), "#\t");
262 color_fprintf_ln(s->fp, color(WT_STATUS_UNTRACKED), "%s",
263 quote_path(ent->name, ent->len,
264 &buf, s->prefix));
266 strbuf_release(&buf);
269 static void wt_status_print_verbose(struct wt_status *s)
271 struct rev_info rev;
273 init_revisions(&rev, NULL);
274 setup_revisions(0, NULL, &rev, s->reference);
275 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
276 rev.diffopt.detect_rename = 1;
277 rev.diffopt.file = s->fp;
278 rev.diffopt.close_file = 0;
279 run_diff_index(&rev, 1);
282 void wt_status_print(struct wt_status *s)
284 unsigned char sha1[20];
285 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
287 if (s->branch) {
288 const char *on_what = "On branch ";
289 const char *branch_name = s->branch;
290 if (!prefixcmp(branch_name, "refs/heads/"))
291 branch_name += 11;
292 else if (!strcmp(branch_name, "HEAD")) {
293 branch_name = "";
294 on_what = "Not currently on any branch.";
296 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER),
297 "# %s%s", on_what, branch_name);
300 if (s->is_initial) {
301 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
302 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "# Initial commit");
303 color_fprintf_ln(s->fp, color(WT_STATUS_HEADER), "#");
304 wt_status_print_initial(s);
306 else {
307 wt_status_print_updated(s);
310 wt_status_print_changed(s);
311 wt_status_print_untracked(s);
313 if (s->verbose && !s->is_initial)
314 wt_status_print_verbose(s);
315 if (!s->commitable) {
316 if (s->amend)
317 fprintf(s->fp, "# No changes\n");
318 else if (s->nowarn)
319 ; /* nothing */
320 else if (s->workdir_dirty)
321 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
322 else if (s->workdir_untracked)
323 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
324 else if (s->is_initial)
325 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
326 else
327 printf("nothing to commit (working directory clean)\n");
331 int git_status_config(const char *k, const char *v)
333 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
334 wt_status_use_color = git_config_colorbool(k, v, -1);
335 return 0;
337 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
338 int slot = parse_status_slot(k, 13);
339 if (!v)
340 return config_error_nonbool(k);
341 color_parse(v, k, wt_status_colors[slot]);
342 return 0;
344 if (!strcmp(k, "status.relativepaths")) {
345 wt_status_relative_paths = git_config_bool(k, v);
346 return 0;
348 return git_color_default_config(k, v);