built-in add -i: color the header in the `status` command
[git] / add-interactive.c
1 #include "cache.h"
2 #include "add-interactive.h"
3 #include "color.h"
4 #include "config.h"
5 #include "diffcore.h"
6 #include "revision.h"
7 #include "refs.h"
8 #include "string-list.h"
9
10 struct add_i_state {
11         struct repository *r;
12         int use_color;
13         char header_color[COLOR_MAXLEN];
14 };
15
16 static void init_color(struct repository *r, struct add_i_state *s,
17                        const char *slot_name, char *dst,
18                        const char *default_color)
19 {
20         char *key = xstrfmt("color.interactive.%s", slot_name);
21         const char *value;
22
23         if (!s->use_color)
24                 dst[0] = '\0';
25         else if (repo_config_get_value(r, key, &value) ||
26                  color_parse(value, dst))
27                 strlcpy(dst, default_color, COLOR_MAXLEN);
28
29         free(key);
30 }
31
32 static void init_add_i_state(struct add_i_state *s, struct repository *r)
33 {
34         const char *value;
35
36         s->r = r;
37
38         if (repo_config_get_value(r, "color.interactive", &value))
39                 s->use_color = -1;
40         else
41                 s->use_color =
42                         git_config_colorbool("color.interactive", value);
43         s->use_color = want_color(s->use_color);
44
45         init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
46 }
47
48 struct list_options {
49         const char *header;
50         void (*print_item)(int i, struct string_list_item *item, void *print_item_data);
51         void *print_item_data;
52 };
53
54 static void list(struct add_i_state *s, struct string_list *list,
55                  struct list_options *opts)
56 {
57         int i;
58
59         if (!list->nr)
60                 return;
61
62         if (opts->header)
63                 color_fprintf_ln(stdout, s->header_color,
64                                  "%s", opts->header);
65
66         for (i = 0; i < list->nr; i++) {
67                 opts->print_item(i, list->items + i, opts->print_item_data);
68                 putchar('\n');
69         }
70 }
71
72 struct adddel {
73         uintmax_t add, del;
74         unsigned seen:1, binary:1;
75 };
76
77 struct file_item {
78         struct adddel index, worktree;
79 };
80
81 static void add_file_item(struct string_list *files, const char *name)
82 {
83         struct file_item *item = xcalloc(sizeof(*item), 1);
84
85         string_list_append(files, name)->util = item;
86 }
87
88 struct pathname_entry {
89         struct hashmap_entry ent;
90         const char *name;
91         struct file_item *item;
92 };
93
94 static int pathname_entry_cmp(const void *unused_cmp_data,
95                               const struct hashmap_entry *he1,
96                               const struct hashmap_entry *he2,
97                               const void *name)
98 {
99         const struct pathname_entry *e1 =
100                 container_of(he1, const struct pathname_entry, ent);
101         const struct pathname_entry *e2 =
102                 container_of(he2, const struct pathname_entry, ent);
103
104         return strcmp(e1->name, name ? (const char *)name : e2->name);
105 }
106
107 struct collection_status {
108         enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase;
109
110         const char *reference;
111
112         struct string_list *files;
113         struct hashmap file_map;
114 };
115
116 static void collect_changes_cb(struct diff_queue_struct *q,
117                                struct diff_options *options,
118                                void *data)
119 {
120         struct collection_status *s = data;
121         struct diffstat_t stat = { 0 };
122         int i;
123
124         if (!q->nr)
125                 return;
126
127         compute_diffstat(options, &stat, q);
128
129         for (i = 0; i < stat.nr; i++) {
130                 const char *name = stat.files[i]->name;
131                 int hash = strhash(name);
132                 struct pathname_entry *entry;
133                 struct file_item *file_item;
134                 struct adddel *adddel;
135
136                 entry = hashmap_get_entry_from_hash(&s->file_map, hash, name,
137                                                     struct pathname_entry, ent);
138                 if (!entry) {
139                         add_file_item(s->files, name);
140
141                         entry = xcalloc(sizeof(*entry), 1);
142                         hashmap_entry_init(&entry->ent, hash);
143                         entry->name = s->files->items[s->files->nr - 1].string;
144                         entry->item = s->files->items[s->files->nr - 1].util;
145                         hashmap_add(&s->file_map, &entry->ent);
146                 }
147
148                 file_item = entry->item;
149                 adddel = s->phase == FROM_INDEX ?
150                         &file_item->index : &file_item->worktree;
151                 adddel->seen = 1;
152                 adddel->add = stat.files[i]->added;
153                 adddel->del = stat.files[i]->deleted;
154                 if (stat.files[i]->is_binary)
155                         adddel->binary = 1;
156         }
157         free_diffstat_info(&stat);
158 }
159
160 static int get_modified_files(struct repository *r, struct string_list *files,
161                               const struct pathspec *ps)
162 {
163         struct object_id head_oid;
164         int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
165                                              &head_oid, NULL);
166         struct collection_status s = { FROM_WORKTREE };
167
168         if (discard_index(r->index) < 0 ||
169             repo_read_index_preload(r, ps, 0) < 0)
170                 return error(_("could not read index"));
171
172         string_list_clear(files, 1);
173         s.files = files;
174         hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
175
176         for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
177                 struct rev_info rev;
178                 struct setup_revision_opt opt = { 0 };
179
180                 opt.def = is_initial ?
181                         empty_tree_oid_hex() : oid_to_hex(&head_oid);
182
183                 init_revisions(&rev, NULL);
184                 setup_revisions(0, NULL, &rev, &opt);
185
186                 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
187                 rev.diffopt.format_callback = collect_changes_cb;
188                 rev.diffopt.format_callback_data = &s;
189
190                 if (ps)
191                         copy_pathspec(&rev.prune_data, ps);
192
193                 if (s.phase == FROM_INDEX)
194                         run_diff_index(&rev, 1);
195                 else {
196                         rev.diffopt.flags.ignore_dirty_submodules = 1;
197                         run_diff_files(&rev, 0);
198                 }
199         }
200         hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
201
202         /* While the diffs are ordered already, we ran *two* diffs... */
203         string_list_sort(files);
204
205         return 0;
206 }
207
208 static void render_adddel(struct strbuf *buf,
209                                 struct adddel *ad, const char *no_changes)
210 {
211         if (ad->binary)
212                 strbuf_addstr(buf, _("binary"));
213         else if (ad->seen)
214                 strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
215                             (uintmax_t)ad->add, (uintmax_t)ad->del);
216         else
217                 strbuf_addstr(buf, no_changes);
218 }
219
220 struct print_file_item_data {
221         const char *modified_fmt;
222         struct strbuf buf, index, worktree;
223 };
224
225 static void print_file_item(int i, struct string_list_item *item,
226                             void *print_file_item_data)
227 {
228         struct file_item *c = item->util;
229         struct print_file_item_data *d = print_file_item_data;
230
231         strbuf_reset(&d->index);
232         strbuf_reset(&d->worktree);
233         strbuf_reset(&d->buf);
234
235         render_adddel(&d->worktree, &c->worktree, _("nothing"));
236         render_adddel(&d->index, &c->index, _("unchanged"));
237         strbuf_addf(&d->buf, d->modified_fmt,
238                     d->index.buf, d->worktree.buf, item->string);
239
240         printf(" %2d: %s", i + 1, d->buf.buf);
241 }
242
243 static int run_status(struct add_i_state *s, const struct pathspec *ps,
244                       struct string_list *files, struct list_options *opts)
245 {
246         if (get_modified_files(s->r, files, ps) < 0)
247                 return -1;
248
249         list(s, files, opts);
250         putchar('\n');
251
252         return 0;
253 }
254
255 int run_add_i(struct repository *r, const struct pathspec *ps)
256 {
257         struct add_i_state s = { NULL };
258         struct print_file_item_data print_file_item_data = {
259                 "%12s %12s %s", STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
260         };
261         struct list_options opts = {
262                 NULL, print_file_item, &print_file_item_data
263         };
264         struct strbuf header = STRBUF_INIT;
265         struct string_list files = STRING_LIST_INIT_DUP;
266         int res = 0;
267
268         init_add_i_state(&s, r);
269         strbuf_addstr(&header, "      ");
270         strbuf_addf(&header, print_file_item_data.modified_fmt,
271                     _("staged"), _("unstaged"), _("path"));
272         opts.header = header.buf;
273
274         if (discard_index(r->index) < 0 ||
275             repo_read_index(r) < 0 ||
276             repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1,
277                                          NULL, NULL, NULL) < 0)
278                 warning(_("could not refresh index"));
279
280         res = run_status(&s, ps, &files, &opts);
281
282         string_list_clear(&files, 1);
283         strbuf_release(&print_file_item_data.buf);
284         strbuf_release(&print_file_item_data.index);
285         strbuf_release(&print_file_item_data.worktree);
286         strbuf_release(&header);
287
288         return res;
289 }