]> git.ipfire.org Git - thirdparty/git.git/blame - wt-status.c
Support --amend on initial commit in status output
[thirdparty/git.git] / wt-status.c
CommitLineData
85023577 1#include "cache.h"
c91f0d92
JK
2#include "wt-status.h"
3#include "color.h"
c91f0d92
JK
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
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};
e54eef9e 18static const char* use_add_msg = "use \"git add <file>...\" to incrementally add content to commit";
c91f0d92
JK
19
20static int parse_status_slot(const char *var, int offset)
21{
22 if (!strcasecmp(var+offset, "header"))
23 return WT_STATUS_HEADER;
82dca848
SP
24 if (!strcasecmp(var+offset, "updated")
25 || !strcasecmp(var+offset, "added"))
c91f0d92
JK
26 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
8da19775 44 head = resolve_ref("HEAD", sha1, 0, NULL);
f62363fb 45 s->branch = head ? xstrdup(head) : NULL;
c91f0d92
JK
46
47 s->reference = "HEAD";
48 s->amend = 0;
49 s->verbose = 0;
50 s->commitable = 0;
2074cb0a 51 s->untracked = 0;
6e458bf6
JR
52
53 s->workdir_clean = 1;
c91f0d92
JK
54}
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
3a946802
JH
69static 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
c91f0d92
JK
98static void wt_status_print_filepair(int t, struct diff_filepair *p)
99{
100 const char *c = color(t);
3a946802
JH
101 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
c91f0d92
JK
107 color_printf(color(WT_STATUS_HEADER), "#\t");
108 switch (p->status) {
109 case DIFF_STATUS_ADDED:
3a946802
JH
110 color_printf(c, "new file: %s", one);
111 break;
c91f0d92 112 case DIFF_STATUS_COPIED:
3a946802 113 color_printf(c, "copied: %s -> %s", one, two);
c91f0d92
JK
114 break;
115 case DIFF_STATUS_DELETED:
3a946802
JH
116 color_printf(c, "deleted: %s", one);
117 break;
c91f0d92 118 case DIFF_STATUS_MODIFIED:
3a946802
JH
119 color_printf(c, "modified: %s", one);
120 break;
c91f0d92 121 case DIFF_STATUS_RENAMED:
3a946802 122 color_printf(c, "renamed: %s -> %s", one, two);
c91f0d92
JK
123 break;
124 case DIFF_STATUS_TYPE_CHANGED:
3a946802
JH
125 color_printf(c, "typechange: %s", one);
126 break;
c91f0d92 127 case DIFF_STATUS_UNKNOWN:
3a946802
JH
128 color_printf(c, "unknown: %s", one);
129 break;
c91f0d92 130 case DIFF_STATUS_UNMERGED:
3a946802
JH
131 color_printf(c, "unmerged: %s", one);
132 break;
c91f0d92
JK
133 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;
c91f0d92
JK
146 for (i = 0; i < q->nr; i++) {
147 if (q->queue[i]->status == 'U')
148 continue;
149 if (!shown_header) {
82dca848 150 wt_status_print_header("Added but not yet committed",
c91f0d92
JK
151 "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{
6e458bf6 165 struct wt_status *s = data;
c91f0d92 166 int i;
6e458bf6
JR
167 if (q->nr) {
168 s->workdir_clean = 0;
82dca848 169 wt_status_print_header("Changed but not added", use_add_msg);
6e458bf6 170 }
c91f0d92
JK
171 for (i = 0; i < q->nr; i++)
172 wt_status_print_filepair(WT_STATUS_CHANGED, q->queue[i]);
173 if (q->nr)
174 wt_status_print_trailer();
175}
176
177void wt_status_print_initial(struct wt_status *s)
178{
179 int i;
3a946802
JH
180 char buf[PATH_MAX];
181
c91f0d92
JK
182 read_cache();
183 if (active_nr) {
184 s->commitable = 1;
82dca848 185 wt_status_print_header("Added but not yet committed",
c91f0d92
JK
186 "will commit");
187 }
188 for (i = 0; i < active_nr; i++) {
189 color_printf(color(WT_STATUS_HEADER), "#\t");
190 color_printf_ln(color(WT_STATUS_UPDATED), "new file: %s",
3a946802
JH
191 quote_crlf(active_cache[i]->name,
192 buf, sizeof(buf)));
c91f0d92
JK
193 }
194 if (active_nr)
195 wt_status_print_trailer();
196}
197
198static void wt_status_print_updated(struct wt_status *s)
199{
200 struct rev_info rev;
c91f0d92 201 init_revisions(&rev, NULL);
49b8b292 202 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
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 run_diff_index(&rev, 1);
208}
209
210static void wt_status_print_changed(struct wt_status *s)
211{
212 struct rev_info rev;
c91f0d92 213 init_revisions(&rev, "");
49b8b292 214 setup_revisions(0, NULL, &rev, NULL);
c91f0d92
JK
215 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
216 rev.diffopt.format_callback = wt_status_print_changed_cb;
217 rev.diffopt.format_callback_data = s;
218 run_diff_files(&rev, 0);
219}
220
6e458bf6 221static void wt_status_print_untracked(struct wt_status *s)
c91f0d92
JK
222{
223 struct dir_struct dir;
224 const char *x;
225 int i;
226 int shown_header = 0;
227
228 memset(&dir, 0, sizeof(dir));
229
230 dir.exclude_per_dir = ".gitignore";
2074cb0a
JS
231 if (!s->untracked) {
232 dir.show_other_directories = 1;
233 dir.hide_empty_directories = 1;
234 }
c91f0d92
JK
235 x = git_path("info/exclude");
236 if (file_exists(x))
237 add_excludes_from_file(&dir, x);
238
239 read_directory(&dir, ".", "", 0);
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;
254 }
255 if (!shown_header) {
6e458bf6 256 s->workdir_clean = 0;
aeb80c70 257 wt_status_print_header("Untracked files", use_add_msg);
c91f0d92
JK
258 shown_header = 1;
259 }
260 color_printf(color(WT_STATUS_HEADER), "#\t");
261 color_printf_ln(color(WT_STATUS_UNTRACKED), "%.*s",
262 ent->len, ent->name);
263 }
264}
265
266static void wt_status_print_verbose(struct wt_status *s)
267{
268 struct rev_info rev;
c91f0d92 269 init_revisions(&rev, NULL);
49b8b292 270 setup_revisions(0, NULL, &rev, s->reference);
c91f0d92
JK
271 rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
272 rev.diffopt.detect_rename = 1;
273 run_diff_index(&rev, 1);
274}
275
276void wt_status_print(struct wt_status *s)
277{
98bf8a47
JR
278 unsigned char sha1[20];
279 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
280
a990999e 281 if (s->branch)
c91f0d92
JK
282 color_printf_ln(color(WT_STATUS_HEADER),
283 "# On branch %s", s->branch);
284
285 if (s->is_initial) {
286 color_printf_ln(color(WT_STATUS_HEADER), "#");
287 color_printf_ln(color(WT_STATUS_HEADER), "# Initial commit");
288 color_printf_ln(color(WT_STATUS_HEADER), "#");
289 wt_status_print_initial(s);
290 }
291 else {
292 wt_status_print_updated(s);
293 discard_cache();
294 }
295
296 wt_status_print_changed(s);
297 wt_status_print_untracked(s);
298
299 if (s->verbose && !s->is_initial)
300 wt_status_print_verbose(s);
6e458bf6
JR
301 if (!s->commitable) {
302 if (s->amend)
303 printf("# No changes\n");
304 else if (s->workdir_clean)
305 printf(s->is_initial
306 ? "nothing to commit\n"
307 : "nothing to commit (working directory matches HEAD)\n");
308 else
309 printf("no changes added to commit (use \"git add\" and/or \"git commit [-a|-i|-o]\")\n");
310 }
c91f0d92
JK
311}
312
313int git_status_config(const char *k, const char *v)
314{
a159ca0c 315 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
c91f0d92
JK
316 wt_status_use_color = git_config_colorbool(k, v);
317 return 0;
318 }
a159ca0c 319 if (!strncmp(k, "status.color.", 13) || !strncmp(k, "color.status", 13)) {
c91f0d92
JK
320 int slot = parse_status_slot(k, 13);
321 color_parse(v, k, wt_status_colors[slot]);
322 }
323 return git_default_config(k, v);
324}