blob: db427384ff454aa07e38c588537cbaa6b835dee6 [file] [log] [blame]
Junio C Hamano85023572006-12-19 14:34:12 -08001#include "cache.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04002#include "wt-status.h"
3#include "color.h"
Jeff Kingc91f0d92006-09-08 04:05:34 -04004#include "object.h"
5#include "dir.h"
6#include "commit.h"
7#include "diff.h"
8#include "revision.h"
9#include "diffcore.h"
10
11int wt_status_use_color = 0;
12static 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 */
17};
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -050018static const char* use_add_msg = "use \"git add file1 file2\" to include for commit";
Jeff Kingc91f0d92006-09-08 04:05:34 -040019
20static int parse_status_slot(const char *var, int offset)
21{
22 if (!strcasecmp(var+offset, "header"))
23 return WT_STATUS_HEADER;
Shawn O. Pearce82dca842006-12-15 21:53:13 -050024 if (!strcasecmp(var+offset, "updated")
25 || !strcasecmp(var+offset, "added"))
Jeff Kingc91f0d92006-09-08 04:05:34 -040026 return WT_STATUS_UPDATED;
27 if (!strcasecmp(var+offset, "changed"))
28 return WT_STATUS_CHANGED;
29 if (!strcasecmp(var+offset, "untracked"))
30 return WT_STATUS_UNTRACKED;
31 die("bad config variable '%s'", var);
32}
33
34static const char* color(int slot)
35{
36 return wt_status_use_color ? wt_status_colors[slot] : "";
37}
38
39void wt_status_prepare(struct wt_status *s)
40{
41 unsigned char sha1[20];
42 const char *head;
43
44 s->is_initial = get_sha1("HEAD", sha1) ? 1 : 0;
45
Junio C Hamano8da19772006-09-20 22:02:01 -070046 head = resolve_ref("HEAD", sha1, 0, NULL);
Jeff Kingf62363f2006-09-17 19:13:56 -070047 s->branch = head ? xstrdup(head) : NULL;
Jeff Kingc91f0d92006-09-08 04:05:34 -040048
49 s->reference = "HEAD";
50 s->amend = 0;
51 s->verbose = 0;
52 s->commitable = 0;
Johannes Schindelin2074cb02006-09-12 22:45:12 +020053 s->untracked = 0;
Jeff Kingc91f0d92006-09-08 04:05:34 -040054}
55
56static void wt_status_print_header(const char *main, const char *sub)
57{
58 const char *c = color(WT_STATUS_HEADER);
59 color_printf_ln(c, "# %s:", main);
60 color_printf_ln(c, "# (%s)", sub);
61 color_printf_ln(c, "#");
62}
63
64static void wt_status_print_trailer(void)
65{
66 color_printf_ln(color(WT_STATUS_HEADER), "#");
67}
68
Junio C Hamano3a946802006-11-08 13:20:46 -080069static const char *quote_crlf(const char *in, char *buf, size_t sz)
70{
71 const char *scan;
72 char *out;
73 const char *ret = in;
74
75 for (scan = in, out = buf; *scan; scan++) {
76 int ch = *scan;
77 int quoted;
78
79 switch (ch) {
80 case '\n':
81 quoted = 'n';
82 break;
83 case '\r':
84 quoted = 'r';
85 break;
86 default:
87 *out++ = ch;
88 continue;
89 }
90 *out++ = '\\';
91 *out++ = quoted;
92 ret = buf;
93 }
94 *out = '\0';
95 return ret;
96}
97
Jeff Kingc91f0d92006-09-08 04:05:34 -040098static void wt_status_print_filepair(int t, struct diff_filepair *p)
99{
100 const char *c = color(t);
Junio C Hamano3a946802006-11-08 13:20:46 -0800101 const char *one, *two;
102 char onebuf[PATH_MAX], twobuf[PATH_MAX];
103
104 one = quote_crlf(p->one->path, onebuf, sizeof(onebuf));
105 two = quote_crlf(p->two->path, twobuf, sizeof(twobuf));
106
Jeff Kingc91f0d92006-09-08 04:05:34 -0400107 color_printf(color(WT_STATUS_HEADER), "#\t");
108 switch (p->status) {
109 case DIFF_STATUS_ADDED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800110 color_printf(c, "new file: %s", one);
111 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400112 case DIFF_STATUS_COPIED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800113 color_printf(c, "copied: %s -> %s", one, two);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400114 break;
115 case DIFF_STATUS_DELETED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800116 color_printf(c, "deleted: %s", one);
117 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400118 case DIFF_STATUS_MODIFIED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800119 color_printf(c, "modified: %s", one);
120 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400121 case DIFF_STATUS_RENAMED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800122 color_printf(c, "renamed: %s -> %s", one, two);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400123 break;
124 case DIFF_STATUS_TYPE_CHANGED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800125 color_printf(c, "typechange: %s", one);
126 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400127 case DIFF_STATUS_UNKNOWN:
Junio C Hamano3a946802006-11-08 13:20:46 -0800128 color_printf(c, "unknown: %s", one);
129 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400130 case DIFF_STATUS_UNMERGED:
Junio C Hamano3a946802006-11-08 13:20:46 -0800131 color_printf(c, "unmerged: %s", one);
132 break;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400133 default:
134 die("bug: unhandled diff status %c", p->status);
135 }
136 printf("\n");
137}
138
139static void wt_status_print_updated_cb(struct diff_queue_struct *q,
140 struct diff_options *options,
141 void *data)
142{
143 struct wt_status *s = data;
144 int shown_header = 0;
145 int i;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400146 for (i = 0; i < q->nr; i++) {
147 if (q->queue[i]->status == 'U')
148 continue;
149 if (!shown_header) {
Shawn O. Pearce82dca842006-12-15 21:53:13 -0500150 wt_status_print_header("Added but not yet committed",
Jeff Kingc91f0d92006-09-08 04:05:34 -0400151 "will commit");
152 s->commitable = 1;
153 shown_header = 1;
154 }
155 wt_status_print_filepair(WT_STATUS_UPDATED, q->queue[i]);
156 }
157 if (shown_header)
158 wt_status_print_trailer();
159}
160
161static void wt_status_print_changed_cb(struct diff_queue_struct *q,
162 struct diff_options *options,
163 void *data)
164{
165 int i;
166 if (q->nr)
Shawn O. Pearce82dca842006-12-15 21:53:13 -0500167 wt_status_print_header("Changed but not added", use_add_msg);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400168 for (i = 0; i < q->nr; i++)
169 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
170 if (q->nr)
171 wt_status_print_trailer();
172}
173
174void wt_status_print_initial(struct wt_status *s)
175{
176 int i;
Junio C Hamano3a946802006-11-08 13:20:46 -0800177 char buf[PATH_MAX];
178
Jeff Kingc91f0d92006-09-08 04:05:34 -0400179 read_cache();
180 if (active_nr) {
181 s->commitable = 1;
Shawn O. Pearce82dca842006-12-15 21:53:13 -0500182 wt_status_print_header("Added but not yet committed",
Jeff Kingc91f0d92006-09-08 04:05:34 -0400183 "will commit");
184 }
185 for (i = 0; i < active_nr; i++) {
186 color_printf(color(WT_STATUS_HEADER), "#\t");
187 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
Junio C Hamano3a946802006-11-08 13:20:46 -0800188 quote_crlf(active_cache[i]->name,
189 buf, sizeof(buf)));
Jeff Kingc91f0d92006-09-08 04:05:34 -0400190 }
191 if (active_nr)
192 wt_status_print_trailer();
193}
194
195static void wt_status_print_updated(struct wt_status *s)
196{
197 struct rev_info rev;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400198 init_revisions(&rev, NULL);
Jeff King49b8b292006-11-05 17:22:15 -0500199 setup_revisions(0, NULL, &rev, s->reference);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400200 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
201 rev.diffopt.format_callback = wt_status_print_updated_cb;
202 rev.diffopt.format_callback_data = s;
203 rev.diffopt.detect_rename = 1;
204 run_diff_index(&rev, 1);
205}
206
207static void wt_status_print_changed(struct wt_status *s)
208{
209 struct rev_info rev;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400210 init_revisions(&rev, "");
Jeff King49b8b292006-11-05 17:22:15 -0500211 setup_revisions(0, NULL, &rev, NULL);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400212 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
213 rev.diffopt.format_callback = wt_status_print_changed_cb;
214 rev.diffopt.format_callback_data = s;
215 run_diff_files(&rev, 0);
216}
217
218static void wt_status_print_untracked(const struct wt_status *s)
219{
220 struct dir_struct dir;
221 const char *x;
222 int i;
223 int shown_header = 0;
224
225 memset(&dir, 0, sizeof(dir));
226
227 dir.exclude_per_dir = ".gitignore";
Johannes Schindelin2074cb02006-09-12 22:45:12 +0200228 if (!s->untracked) {
229 dir.show_other_directories = 1;
230 dir.hide_empty_directories = 1;
231 }
Jeff Kingc91f0d92006-09-08 04:05:34 -0400232 x = git_path("info/exclude");
233 if (file_exists(x))
234 add_excludes_from_file(&dir, x);
235
236 read_directory(&dir, ".", "", 0);
237 for(i = 0; i < dir.nr; i++) {
238 /* check for matching entry, which is unmerged; lifted from
239 * builtin-ls-files:show_other_files */
240 struct dir_entry *ent = dir.entries[i];
241 int pos = cache_name_pos(ent->name, ent->len);
242 struct cache_entry *ce;
243 if (0 <= pos)
244 die("bug in wt_status_print_untracked");
245 pos = -pos - 1;
246 if (pos < active_nr) {
247 ce = active_cache[pos];
248 if (ce_namelen(ce) == ent->len &&
249 !memcmp(ce->name, ent->name, ent->len))
250 continue;
251 }
252 if (!shown_header) {
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500253 wt_status_print_header("Untracked files", use_add_msg);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400254 shown_header = 1;
255 }
256 color_printf(color(WT_STATUS_HEADER), "#\t");
257 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
258 ent->len, ent->name);
259 }
260}
261
262static void wt_status_print_verbose(struct wt_status *s)
263{
264 struct rev_info rev;
Jeff Kingc91f0d92006-09-08 04:05:34 -0400265 init_revisions(&rev, NULL);
Jeff King49b8b292006-11-05 17:22:15 -0500266 setup_revisions(0, NULL, &rev, s->reference);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400267 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
268 rev.diffopt.detect_rename = 1;
269 run_diff_index(&rev, 1);
270}
271
272void wt_status_print(struct wt_status *s)
273{
Andy Parkinsa9909992006-12-14 15:25:23 +0000274 if (s->branch)
Jeff Kingc91f0d92006-09-08 04:05:34 -0400275 color_printf_ln(color(WT_STATUS_HEADER),
276 "# On branch %s", s->branch);
277
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);
283 }
284 else {
285 wt_status_print_updated(s);
286 discard_cache();
287 }
288
289 wt_status_print_changed(s);
290 wt_status_print_untracked(s);
291
292 if (s->verbose && !s->is_initial)
293 wt_status_print_verbose(s);
294 if (!s->commitable)
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500295 printf("%s (%s)\n",
296 s->amend ? "# No changes" : "nothing to commit",
297 use_add_msg);
Jeff Kingc91f0d92006-09-08 04:05:34 -0400298}
299
300int git_status_config(const char *k, const char *v)
301{
Andy Parkinsa159ca02006-12-13 09:13:28 +0000302 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
Jeff Kingc91f0d92006-09-08 04:05:34 -0400303 wt_status_use_color = git_config_colorbool(k, v);
304 return 0;
305 }
Andy Parkinsa159ca02006-12-13 09:13:28 +0000306 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
Jeff Kingc91f0d92006-09-08 04:05:34 -0400307 int slot = parse_status_slot(k, 13);
308 color_parse(v, k, wt_status_colors[slot]);
309 }
310 return git_default_config(k, v);
311}