add "test-absolute-path" to .gitignore
[git/haiku.git] / wt-status.c
blob52054201c2a8729e036a5d97337a5f85bcafc782
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 */
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";
25 static const char *excludes_file;
27 static int parse_status_slot(const char *var, int offset)
29 if (!strcasecmp(var+offset, "header"))
30 return WT_STATUS_HEADER;
31 if (!strcasecmp(var+offset, "updated")
32 || !strcasecmp(var+offset, "added"))
33 return WT_STATUS_UPDATED;
34 if (!strcasecmp(var+offset, "changed"))
35 return WT_STATUS_CHANGED;
36 if (!strcasecmp(var+offset, "untracked"))
37 return WT_STATUS_UNTRACKED;
38 die("bad config variable '%s'", var);
41 static const char* color(int slot)
43 return wt_status_use_color ? wt_status_colors[slot] : "";
46 void wt_status_prepare(struct wt_status *s)
48 unsigned char sha1[20];
49 const char *head;
51 memset(s, 0, sizeof(*s));
52 head = resolve_ref("HEAD", sha1, 0, NULL);
53 s->branch = head ? xstrdup(head) : NULL;
54 s->reference = "HEAD";
57 static void wt_status_print_cached_header(const char *reference)
59 const char *c = color(WT_STATUS_HEADER);
60 color_printf_ln(c, "# Changes to be committed:");
61 if (reference) {
62 color_printf_ln(c, "# (use \"git reset %s <file>...\" to unstage)", reference);
63 } else {
64 color_printf_ln(c, "# (use \"git rm --cached <file>...\" to unstage)");
66 color_printf_ln(c, "#");
69 static void wt_status_print_header(const char *main, const char *sub)
71 const char *c = color(WT_STATUS_HEADER);
72 color_printf_ln(c, "# %s:", main);
73 color_printf_ln(c, "# (%s)", sub);
74 color_printf_ln(c, "#");
77 static void wt_status_print_trailer(void)
79 color_printf_ln(color(WT_STATUS_HEADER), "#");
82 static const char *quote_crlf(const char *in, char *buf, size_t sz)
84 const char *scan;
85 char *out;
86 const char *ret = in;
88 for (scan = in, out = buf; *scan; scan++) {
89 int ch = *scan;
90 int quoted;
92 switch (ch) {
93 case '\n':
94 quoted = 'n';
95 break;
96 case '\r':
97 quoted = 'r';
98 break;
99 default:
100 *out++ = ch;
101 continue;
103 *out++ = '\\';
104 *out++ = quoted;
105 ret = buf;
107 *out = '\0';
108 return ret;
111 static void wt_status_print_filepair(int t, struct diff_filepair *p)
113 const char *c = color(t);
114 const char *one, *two;
115 char onebuf[PATH_MAX], twobuf[PATH_MAX];
117 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
118 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
120 color_printf(color(WT_STATUS_HEADER), "#\t");
121 switch (p->status) {
122 case DIFF_STATUS_ADDED:
123 color_printf(c, "new file: %s", one);
124 break;
125 case DIFF_STATUS_COPIED:
126 color_printf(c, "copied: %s -> %s", one, two);
127 break;
128 case DIFF_STATUS_DELETED:
129 color_printf(c, "deleted: %s", one);
130 break;
131 case DIFF_STATUS_MODIFIED:
132 color_printf(c, "modified: %s", one);
133 break;
134 case DIFF_STATUS_RENAMED:
135 color_printf(c, "renamed: %s -> %s", one, two);
136 break;
137 case DIFF_STATUS_TYPE_CHANGED:
138 color_printf(c, "typechange: %s", one);
139 break;
140 case DIFF_STATUS_UNKNOWN:
141 color_printf(c, "unknown: %s", one);
142 break;
143 case DIFF_STATUS_UNMERGED:
144 color_printf(c, "unmerged: %s", one);
145 break;
146 default:
147 die("bug: unhandled diff status %c", p->status);
149 printf("\n");
152 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
153 struct diff_options *options,
154 void *data)
156 struct wt_status *s = data;
157 int shown_header = 0;
158 int i;
159 for (i = 0; i < q->nr; i++) {
160 if (q->queue[i]->status == 'U')
161 continue;
162 if (!shown_header) {
163 wt_status_print_cached_header(s->reference);
164 s->commitable = 1;
165 shown_header = 1;
167 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
169 if (shown_header)
170 wt_status_print_trailer();
173 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
174 struct diff_options *options,
175 void *data)
177 struct wt_status *s = data;
178 int i;
179 if (q->nr) {
180 const char *msg = use_add_msg;
181 s->workdir_dirty = 1;
182 for (i = 0; i < q->nr; i++)
183 if (q->queue[i]->status == DIFF_STATUS_DELETED) {
184 msg = use_add_rm_msg;
185 break;
187 wt_status_print_header("Changed but not updated", msg);
189 for (i = 0; i < q->nr; i++)
190 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
191 if (q->nr)
192 wt_status_print_trailer();
195 static void wt_read_cache(struct wt_status *s)
197 discard_cache();
198 read_cache();
201 static void wt_status_print_initial(struct wt_status *s)
203 int i;
204 char buf[PATH_MAX];
206 wt_read_cache(s);
207 if (active_nr) {
208 s->commitable = 1;
209 wt_status_print_cached_header(NULL);
211 for (i = 0; i < active_nr; i++) {
212 color_printf(color(WT_STATUS_HEADER), "#\t");
213 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
214 quote_crlf(active_cache[i]->name,
215 buf, sizeof(buf)));
217 if (active_nr)
218 wt_status_print_trailer();
221 static void wt_status_print_updated(struct wt_status *s)
223 struct rev_info rev;
224 init_revisions(&rev, NULL);
225 setup_revisions(0, NULL, &rev, s->reference);
226 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
227 rev.diffopt.format_callback = wt_status_print_updated_cb;
228 rev.diffopt.format_callback_data = s;
229 rev.diffopt.detect_rename = 1;
230 wt_read_cache(s);
231 run_diff_index(&rev, 1);
234 static void wt_status_print_changed(struct wt_status *s)
236 struct rev_info rev;
237 init_revisions(&rev, "");
238 setup_revisions(0, NULL, &rev, NULL);
239 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
240 rev.diffopt.format_callback = wt_status_print_changed_cb;
241 rev.diffopt.format_callback_data = s;
242 wt_read_cache(s);
243 run_diff_files(&rev, 0);
246 static void wt_status_print_untracked(struct wt_status *s)
248 struct dir_struct dir;
249 const char *x;
250 int i;
251 int shown_header = 0;
253 memset(&dir, 0, sizeof(dir));
255 dir.exclude_per_dir = ".gitignore";
256 if (!s->untracked) {
257 dir.show_other_directories = 1;
258 dir.hide_empty_directories = 1;
260 x = git_path("info/exclude");
261 if (file_exists(x))
262 add_excludes_from_file(&dir, x);
263 if (excludes_file && file_exists(excludes_file))
264 add_excludes_from_file(&dir, excludes_file);
266 read_directory(&dir, ".", "", 0, NULL);
267 for(i = 0; i < dir.nr; i++) {
268 /* check for matching entry, which is unmerged; lifted from
269 * builtin-ls-files:show_other_files */
270 struct dir_entry *ent = dir.entries[i];
271 int pos = cache_name_pos(ent->name, ent->len);
272 struct cache_entry *ce;
273 if (0 <= pos)
274 die("bug in wt_status_print_untracked");
275 pos = -pos - 1;
276 if (pos < active_nr) {
277 ce = active_cache[pos];
278 if (ce_namelen(ce) == ent->len &&
279 !memcmp(ce->name, ent->name, ent->len))
280 continue;
282 if (!shown_header) {
283 s->workdir_untracked = 1;
284 wt_status_print_header("Untracked files",
285 use_add_to_include_msg);
286 shown_header = 1;
288 color_printf(color(WT_STATUS_HEADER), "#\t");
289 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
290 ent->len, ent->name);
294 static void wt_status_print_verbose(struct wt_status *s)
296 struct rev_info rev;
297 init_revisions(&rev, NULL);
298 setup_revisions(0, NULL, &rev, s->reference);
299 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
300 rev.diffopt.detect_rename = 1;
301 wt_read_cache(s);
302 run_diff_index(&rev, 1);
305 void wt_status_print(struct wt_status *s)
307 unsigned char sha1[20];
308 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
310 if (s->branch) {
311 const char *on_what = "On branch ";
312 const char *branch_name = s->branch;
313 if (!prefixcmp(branch_name, "refs/heads/"))
314 branch_name += 11;
315 else if (!strcmp(branch_name, "HEAD")) {
316 branch_name = "";
317 on_what = "Not currently on any branch.";
319 color_printf_ln(color(WT_STATUS_HEADER),
320 "# %s%s", on_what, branch_name);
323 if (s->is_initial) {
324 color_printf_ln(color(WT_STATUS_HEADER), "#");
325 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
326 color_printf_ln(color(WT_STATUS_HEADER), "#");
327 wt_status_print_initial(s);
329 else {
330 wt_status_print_updated(s);
333 wt_status_print_changed(s);
334 wt_status_print_untracked(s);
336 if (s->verbose && !s->is_initial)
337 wt_status_print_verbose(s);
338 if (!s->commitable) {
339 if (s->amend)
340 printf("# No changes\n");
341 else if (s->workdir_dirty)
342 printf("no changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
343 else if (s->workdir_untracked)
344 printf("nothing added to commit but untracked files present (use \"git add\" to track)\n");
345 else if (s->is_initial)
346 printf("nothing to commit (create/copy files and use \"git add\" to track)\n");
347 else
348 printf("nothing to commit (working directory clean)\n");
352 int git_status_config(const char *k, const char *v)
354 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
355 wt_status_use_color = git_config_colorbool(k, v);
356 return 0;
358 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
359 int slot = parse_status_slot(k, 13);
360 color_parse(v, k, wt_status_colors[slot]);
362 if (!strcmp(k, "core.excludesfile")) {
363 if (!v)
364 die("core.excludesfile without value");
365 excludes_file = xstrdup(v);
366 return 0;
368 return git_default_config(k, v);