Add --add option to git-repo-config
[git/jrn.git] / wt-status.c
blob6e9414dbb07f1457eb436907a77d14476a6e6b1f
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"
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 int parse_status_slot(const char *var, int offset)
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);
32 static const char* color(int slot)
34 return wt_status_use_color ? wt_status_colors[slot] : "";
37 void wt_status_prepare(struct wt_status *s)
39 unsigned char sha1[20];
40 const char *head;
42 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
44 head = resolve_ref("HEAD", sha1, 0, NULL);
45 s->branch = head ? xstrdup(head) : NULL;
47 s->reference = "HEAD";
48 s->amend = 0;
49 s->verbose = 0;
50 s->commitable = 0;
51 s->untracked = 0;
54 static void wt_status_print_header(const char *main, const char *sub)
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, "#");
62 static void wt_status_print_trailer(void)
64 color_printf_ln(color(WT_STATUS_HEADER), "#");
67 static const char *quote_crlf(const char *in, char *buf, size_t sz)
69 const char *scan;
70 char *out;
71 const char *ret = in;
73 for (scan = in, out = buf; *scan; scan++) {
74 int ch = *scan;
75 int quoted;
77 switch (ch) {
78 case '\n':
79 quoted = 'n';
80 break;
81 case '\r':
82 quoted = 'r';
83 break;
84 default:
85 *out++ = ch;
86 continue;
88 *out++ = '\\';
89 *out++ = quoted;
90 ret = buf;
92 *out = '\0';
93 return ret;
96 static void wt_status_print_filepair(int t, struct diff_filepair *p)
98 const char *c = color(t);
99 const char *one, *two;
100 char onebuf[PATH_MAX], twobuf[PATH_MAX];
102 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
103 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
105 color_printf(color(WT_STATUS_HEADER), "#\t");
106 switch (p->status) {
107 case DIFF_STATUS_ADDED:
108 color_printf(c, "new file: %s", one);
109 break;
110 case DIFF_STATUS_COPIED:
111 color_printf(c, "copied: %s -> %s", one, two);
112 break;
113 case DIFF_STATUS_DELETED:
114 color_printf(c, "deleted: %s", one);
115 break;
116 case DIFF_STATUS_MODIFIED:
117 color_printf(c, "modified: %s", one);
118 break;
119 case DIFF_STATUS_RENAMED:
120 color_printf(c, "renamed: %s -> %s", one, two);
121 break;
122 case DIFF_STATUS_TYPE_CHANGED:
123 color_printf(c, "typechange: %s", one);
124 break;
125 case DIFF_STATUS_UNKNOWN:
126 color_printf(c, "unknown: %s", one);
127 break;
128 case DIFF_STATUS_UNMERGED:
129 color_printf(c, "unmerged: %s", one);
130 break;
131 default:
132 die("bug: unhandled diff status %c", p->status);
134 printf("\n");
137 static void wt_status_print_updated_cb(struct diff_queue_struct *q,
138 struct diff_options *options,
139 void *data)
141 struct wt_status *s = data;
142 int shown_header = 0;
143 int i;
144 for (i = 0; i < q->nr; i++) {
145 if (q->queue[i]->status == 'U')
146 continue;
147 if (!shown_header) {
148 wt_status_print_header("Updated but not checked in",
149 "will commit");
150 s->commitable = 1;
151 shown_header = 1;
153 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
155 if (shown_header)
156 wt_status_print_trailer();
159 static void wt_status_print_changed_cb(struct diff_queue_struct *q,
160 struct diff_options *options,
161 void *data)
163 int i;
164 if (q->nr)
165 wt_status_print_header("Changed but not updated",
166 "use git-add on files to include for commit");
167 for (i = 0; i < q->nr; i++)
168 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
169 if (q->nr)
170 wt_status_print_trailer();
173 void wt_status_print_initial(struct wt_status *s)
175 int i;
176 char buf[PATH_MAX];
178 read_cache();
179 if (active_nr) {
180 s->commitable = 1;
181 wt_status_print_header("Updated but not checked in",
182 "will commit");
184 for (i = 0; i < active_nr; i++) {
185 color_printf(color(WT_STATUS_HEADER), "#\t");
186 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
187 quote_crlf(active_cache[i]->name,
188 buf, sizeof(buf)));
190 if (active_nr)
191 wt_status_print_trailer();
194 static void wt_status_print_updated(struct wt_status *s)
196 struct rev_info rev;
197 init_revisions(&rev, NULL);
198 setup_revisions(0, NULL, &rev, s->reference);
199 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
200 rev.diffopt.format_callback = wt_status_print_updated_cb;
201 rev.diffopt.format_callback_data = s;
202 rev.diffopt.detect_rename = 1;
203 run_diff_index(&rev, 1);
206 static void wt_status_print_changed(struct wt_status *s)
208 struct rev_info rev;
209 init_revisions(&rev, "");
210 setup_revisions(0, NULL, &rev, NULL);
211 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
212 rev.diffopt.format_callback = wt_status_print_changed_cb;
213 rev.diffopt.format_callback_data = s;
214 run_diff_files(&rev, 0);
217 static void wt_status_print_untracked(const struct wt_status *s)
219 struct dir_struct dir;
220 const char *x;
221 int i;
222 int shown_header = 0;
224 memset(&dir, 0, sizeof(dir));
226 dir.exclude_per_dir = ".gitignore";
227 if (!s->untracked) {
228 dir.show_other_directories = 1;
229 dir.hide_empty_directories = 1;
231 x = git_path("info/exclude");
232 if (file_exists(x))
233 add_excludes_from_file(&dir, x);
235 read_directory(&dir, ".", "", 0);
236 for(i = 0; i < dir.nr; i++) {
237 /* check for matching entry, which is unmerged; lifted from
238 * builtin-ls-files:show_other_files */
239 struct dir_entry *ent = dir.entries[i];
240 int pos = cache_name_pos(ent->name, ent->len);
241 struct cache_entry *ce;
242 if (0 <= pos)
243 die("bug in wt_status_print_untracked");
244 pos = -pos - 1;
245 if (pos < active_nr) {
246 ce = active_cache[pos];
247 if (ce_namelen(ce) == ent->len &&
248 !memcmp(ce->name, ent->name, ent->len))
249 continue;
251 if (!shown_header) {
252 wt_status_print_header("Untracked files",
253 "use \"git add\" to add to commit");
254 shown_header = 1;
256 color_printf(color(WT_STATUS_HEADER), "#\t");
257 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
258 ent->len, ent->name);
262 static void wt_status_print_verbose(struct wt_status *s)
264 struct rev_info rev;
265 init_revisions(&rev, NULL);
266 setup_revisions(0, NULL, &rev, s->reference);
267 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
268 rev.diffopt.detect_rename = 1;
269 run_diff_index(&rev, 1);
272 void wt_status_print(struct wt_status *s)
274 if (s->branch && strcmp(s->branch, "refs/heads/master"))
275 color_printf_ln(color(WT_STATUS_HEADER),
276 "# On branch %s", s->branch);
278 if (s->is_initial) {
279 color_printf_ln(color(WT_STATUS_HEADER), "#");
280 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
281 color_printf_ln(color(WT_STATUS_HEADER), "#");
282 wt_status_print_initial(s);
284 else {
285 wt_status_print_updated(s);
286 discard_cache();
289 wt_status_print_changed(s);
290 wt_status_print_untracked(s);
292 if (s->verbose && !s->is_initial)
293 wt_status_print_verbose(s);
294 if (!s->commitable)
295 printf("%s\n", s->amend ? "# No changes" : "nothing to commit");
298 int git_status_config(const char *k, const char *v)
300 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
301 wt_status_use_color = git_config_colorbool(k, v);
302 return 0;
304 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
305 int slot = parse_status_slot(k, 13);
306 color_parse(v, k, wt_status_colors[slot]);
308 return git_default_config(k, v);