2 #include "add-interactive.h"
8 #include "string-list.h"
13 char header_color[COLOR_MAXLEN];
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)
20 char *key = xstrfmt("color.interactive.%s", slot_name);
25 else if (repo_config_get_value(r, key, &value) ||
26 color_parse(value, dst))
27 strlcpy(dst, default_color, COLOR_MAXLEN);
32 static void init_add_i_state(struct add_i_state *s, struct repository *r)
38 if (repo_config_get_value(r, "color.interactive", &value))
42 git_config_colorbool("color.interactive", value);
43 s->use_color = want_color(s->use_color);
45 init_color(r, s, "header", s->header_color, GIT_COLOR_BOLD);
50 void (*print_item)(int i, struct string_list_item *item, void *print_item_data);
51 void *print_item_data;
54 static void list(struct add_i_state *s, struct string_list *list,
55 struct list_options *opts)
63 color_fprintf_ln(stdout, s->header_color,
66 for (i = 0; i < list->nr; i++) {
67 opts->print_item(i, list->items + i, opts->print_item_data);
74 unsigned seen:1, binary:1;
78 struct adddel index, worktree;
81 static void add_file_item(struct string_list *files, const char *name)
83 struct file_item *item = xcalloc(sizeof(*item), 1);
85 string_list_append(files, name)->util = item;
88 struct pathname_entry {
89 struct hashmap_entry ent;
91 struct file_item *item;
94 static int pathname_entry_cmp(const void *unused_cmp_data,
95 const struct hashmap_entry *he1,
96 const struct hashmap_entry *he2,
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);
104 return strcmp(e1->name, name ? (const char *)name : e2->name);
107 struct collection_status {
108 enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } phase;
110 const char *reference;
112 struct string_list *files;
113 struct hashmap file_map;
116 static void collect_changes_cb(struct diff_queue_struct *q,
117 struct diff_options *options,
120 struct collection_status *s = data;
121 struct diffstat_t stat = { 0 };
127 compute_diffstat(options, &stat, q);
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;
136 entry = hashmap_get_entry_from_hash(&s->file_map, hash, name,
137 struct pathname_entry, ent);
139 add_file_item(s->files, name);
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);
148 file_item = entry->item;
149 adddel = s->phase == FROM_INDEX ?
150 &file_item->index : &file_item->worktree;
152 adddel->add = stat.files[i]->added;
153 adddel->del = stat.files[i]->deleted;
154 if (stat.files[i]->is_binary)
157 free_diffstat_info(&stat);
160 static int get_modified_files(struct repository *r, struct string_list *files,
161 const struct pathspec *ps)
163 struct object_id head_oid;
164 int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
166 struct collection_status s = { FROM_WORKTREE };
168 if (discard_index(r->index) < 0 ||
169 repo_read_index_preload(r, ps, 0) < 0)
170 return error(_("could not read index"));
172 string_list_clear(files, 1);
174 hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0);
176 for (s.phase = FROM_WORKTREE; s.phase <= FROM_INDEX; s.phase++) {
178 struct setup_revision_opt opt = { 0 };
180 opt.def = is_initial ?
181 empty_tree_oid_hex() : oid_to_hex(&head_oid);
183 init_revisions(&rev, NULL);
184 setup_revisions(0, NULL, &rev, &opt);
186 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
187 rev.diffopt.format_callback = collect_changes_cb;
188 rev.diffopt.format_callback_data = &s;
191 copy_pathspec(&rev.prune_data, ps);
193 if (s.phase == FROM_INDEX)
194 run_diff_index(&rev, 1);
196 rev.diffopt.flags.ignore_dirty_submodules = 1;
197 run_diff_files(&rev, 0);
200 hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
202 /* While the diffs are ordered already, we ran *two* diffs... */
203 string_list_sort(files);
208 static void render_adddel(struct strbuf *buf,
209 struct adddel *ad, const char *no_changes)
212 strbuf_addstr(buf, _("binary"));
214 strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX,
215 (uintmax_t)ad->add, (uintmax_t)ad->del);
217 strbuf_addstr(buf, no_changes);
220 struct print_file_item_data {
221 const char *modified_fmt;
222 struct strbuf buf, index, worktree;
225 static void print_file_item(int i, struct string_list_item *item,
226 void *print_file_item_data)
228 struct file_item *c = item->util;
229 struct print_file_item_data *d = print_file_item_data;
231 strbuf_reset(&d->index);
232 strbuf_reset(&d->worktree);
233 strbuf_reset(&d->buf);
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);
240 printf(" %2d: %s", i + 1, d->buf.buf);
243 static int run_status(struct add_i_state *s, const struct pathspec *ps,
244 struct string_list *files, struct list_options *opts)
246 if (get_modified_files(s->r, files, ps) < 0)
249 list(s, files, opts);
255 int run_add_i(struct repository *r, const struct pathspec *ps)
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
261 struct list_options opts = {
262 NULL, print_file_item, &print_file_item_data
264 struct strbuf header = STRBUF_INIT;
265 struct string_list files = STRING_LIST_INIT_DUP;
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;
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"));
280 res = run_status(&s, ps, &files, &opts);
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);