]>
Commit | Line | Data |
---|---|---|
f83dff60 JS |
1 | #include "cache.h" |
2 | #include "add-interactive.h" | |
1daaebca SĐ |
3 | #include "color.h" |
4 | #include "config.h" | |
5e82b9e4 DF |
5 | #include "diffcore.h" |
6 | #include "revision.h" | |
7 | #include "refs.h" | |
8 | #include "string-list.h" | |
a8c45be9 | 9 | #include "lockfile.h" |
ab1e1ccc | 10 | #include "dir.h" |
8746e072 | 11 | #include "run-command.h" |
08d383f2 | 12 | #include "prompt.h" |
5e82b9e4 | 13 | |
1daaebca | 14 | static void init_color(struct repository *r, struct add_i_state *s, |
25d9e5cc | 15 | const char *section_and_slot, char *dst, |
1daaebca SĐ |
16 | const char *default_color) |
17 | { | |
25d9e5cc | 18 | char *key = xstrfmt("color.%s", section_and_slot); |
1daaebca SĐ |
19 | const char *value; |
20 | ||
21 | if (!s->use_color) | |
22 | dst[0] = '\0'; | |
23 | else if (repo_config_get_value(r, key, &value) || | |
24 | color_parse(value, dst)) | |
25 | strlcpy(dst, default_color, COLOR_MAXLEN); | |
26 | ||
27 | free(key); | |
28 | } | |
29 | ||
25ea47af | 30 | void init_add_i_state(struct add_i_state *s, struct repository *r) |
5e82b9e4 | 31 | { |
1daaebca SĐ |
32 | const char *value; |
33 | ||
34 | s->r = r; | |
35 | ||
36 | if (repo_config_get_value(r, "color.interactive", &value)) | |
37 | s->use_color = -1; | |
38 | else | |
39 | s->use_color = | |
40 | git_config_colorbool("color.interactive", value); | |
41 | s->use_color = want_color(s->use_color); | |
42 | ||
25d9e5cc JS |
43 | init_color(r, s, "interactive.header", s->header_color, GIT_COLOR_BOLD); |
44 | init_color(r, s, "interactive.help", s->help_color, GIT_COLOR_BOLD_RED); | |
45 | init_color(r, s, "interactive.prompt", s->prompt_color, | |
46 | GIT_COLOR_BOLD_BLUE); | |
47 | init_color(r, s, "interactive.error", s->error_color, | |
48 | GIT_COLOR_BOLD_RED); | |
49 | ||
50 | init_color(r, s, "diff.frag", s->fraginfo_color, | |
25ea47af | 51 | diff_get_color(s->use_color, DIFF_FRAGINFO)); |
890b68b2 JS |
52 | init_color(r, s, "diff.context", s->context_color, "fall back"); |
53 | if (!strcmp(s->context_color, "fall back")) | |
54 | init_color(r, s, "diff.plain", s->context_color, | |
55 | diff_get_color(s->use_color, DIFF_CONTEXT)); | |
25d9e5cc | 56 | init_color(r, s, "diff.old", s->file_old_color, |
bcdd297b | 57 | diff_get_color(s->use_color, DIFF_FILE_OLD)); |
25d9e5cc | 58 | init_color(r, s, "diff.new", s->file_new_color, |
bcdd297b | 59 | diff_get_color(s->use_color, DIFF_FILE_NEW)); |
180f48df | 60 | |
c62cd172 JS |
61 | strlcpy(s->reset_color, |
62 | s->use_color ? GIT_COLOR_RESET : "", COLOR_MAXLEN); | |
63 | ||
180f48df JS |
64 | FREE_AND_NULL(s->interactive_diff_filter); |
65 | git_config_get_string("interactive.difffilter", | |
66 | &s->interactive_diff_filter); | |
08b1ea4c JS |
67 | |
68 | FREE_AND_NULL(s->interactive_diff_algorithm); | |
69 | git_config_get_string("diff.algorithm", | |
70 | &s->interactive_diff_algorithm); | |
04f816b1 JS |
71 | |
72 | git_config_get_bool("interactive.singlekey", &s->use_single_key); | |
ac618c41 PW |
73 | if (s->use_single_key) |
74 | setbuf(stdin, NULL); | |
180f48df JS |
75 | } |
76 | ||
77 | void clear_add_i_state(struct add_i_state *s) | |
78 | { | |
79 | FREE_AND_NULL(s->interactive_diff_filter); | |
08b1ea4c | 80 | FREE_AND_NULL(s->interactive_diff_algorithm); |
180f48df JS |
81 | memset(s, 0, sizeof(*s)); |
82 | s->use_color = -1; | |
5e82b9e4 DF |
83 | } |
84 | ||
76b74323 JS |
85 | /* |
86 | * A "prefix item list" is a list of items that are identified by a string, and | |
87 | * a unique prefix (if any) is determined for each item. | |
88 | * | |
89 | * It is implemented in the form of a pair of `string_list`s, the first one | |
90 | * duplicating the strings, with the `util` field pointing at a structure whose | |
91 | * first field must be `size_t prefix_length`. | |
92 | * | |
93 | * That `prefix_length` field will be computed by `find_unique_prefixes()`; It | |
94 | * will be set to zero if no valid, unique prefix could be found. | |
95 | * | |
96 | * The second `string_list` is called `sorted` and does _not_ duplicate the | |
97 | * strings but simply reuses the first one's, with the `util` field pointing at | |
98 | * the `string_item_list` of the first `string_list`. It will be populated and | |
99 | * sorted by `find_unique_prefixes()`. | |
100 | */ | |
101 | struct prefix_item_list { | |
102 | struct string_list items; | |
103 | struct string_list sorted; | |
f37c2264 | 104 | int *selected; /* for multi-selections */ |
76b74323 JS |
105 | size_t min_length, max_length; |
106 | }; | |
f69a6e4f ÆAB |
107 | #define PREFIX_ITEM_LIST_INIT { \ |
108 | .items = STRING_LIST_INIT_DUP, \ | |
109 | .sorted = STRING_LIST_INIT_NODUP, \ | |
110 | .min_length = 1, \ | |
111 | .max_length = 4, \ | |
112 | } | |
76b74323 JS |
113 | |
114 | static void prefix_item_list_clear(struct prefix_item_list *list) | |
115 | { | |
116 | string_list_clear(&list->items, 1); | |
117 | string_list_clear(&list->sorted, 0); | |
f37c2264 | 118 | FREE_AND_NULL(list->selected); |
76b74323 JS |
119 | } |
120 | ||
121 | static void extend_prefix_length(struct string_list_item *p, | |
122 | const char *other_string, size_t max_length) | |
123 | { | |
124 | size_t *len = p->util; | |
125 | ||
126 | if (!*len || memcmp(p->string, other_string, *len)) | |
127 | return; | |
128 | ||
129 | for (;;) { | |
130 | char c = p->string[*len]; | |
131 | ||
132 | /* | |
133 | * Is `p` a strict prefix of `other`? Or have we exhausted the | |
134 | * maximal length of the prefix? Or is the current character a | |
135 | * multi-byte UTF-8 one? If so, there is no valid, unique | |
136 | * prefix. | |
137 | */ | |
138 | if (!c || ++*len > max_length || !isascii(c)) { | |
139 | *len = 0; | |
140 | break; | |
141 | } | |
142 | ||
143 | if (c != other_string[*len - 1]) | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
148 | static void find_unique_prefixes(struct prefix_item_list *list) | |
149 | { | |
150 | size_t i; | |
151 | ||
152 | if (list->sorted.nr == list->items.nr) | |
153 | return; | |
154 | ||
155 | string_list_clear(&list->sorted, 0); | |
156 | /* Avoid reallocating incrementally */ | |
157 | list->sorted.items = xmalloc(st_mult(sizeof(*list->sorted.items), | |
158 | list->items.nr)); | |
159 | list->sorted.nr = list->sorted.alloc = list->items.nr; | |
160 | ||
161 | for (i = 0; i < list->items.nr; i++) { | |
162 | list->sorted.items[i].string = list->items.items[i].string; | |
163 | list->sorted.items[i].util = list->items.items + i; | |
164 | } | |
165 | ||
166 | string_list_sort(&list->sorted); | |
167 | ||
168 | for (i = 0; i < list->sorted.nr; i++) { | |
169 | struct string_list_item *sorted_item = list->sorted.items + i; | |
170 | struct string_list_item *item = sorted_item->util; | |
171 | size_t *len = item->util; | |
172 | ||
173 | *len = 0; | |
174 | while (*len < list->min_length) { | |
175 | char c = item->string[(*len)++]; | |
176 | ||
177 | if (!c || !isascii(c)) { | |
178 | *len = 0; | |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | if (i > 0) | |
184 | extend_prefix_length(item, sorted_item[-1].string, | |
185 | list->max_length); | |
186 | if (i + 1 < list->sorted.nr) | |
187 | extend_prefix_length(item, sorted_item[1].string, | |
188 | list->max_length); | |
189 | } | |
190 | } | |
191 | ||
192 | static ssize_t find_unique(const char *string, struct prefix_item_list *list) | |
193 | { | |
194 | int index = string_list_find_insert_index(&list->sorted, string, 1); | |
195 | struct string_list_item *item; | |
196 | ||
197 | if (list->items.nr != list->sorted.nr) | |
198 | BUG("prefix_item_list in inconsistent state (%"PRIuMAX | |
199 | " vs %"PRIuMAX")", | |
200 | (uintmax_t)list->items.nr, (uintmax_t)list->sorted.nr); | |
201 | ||
202 | if (index < 0) | |
203 | item = list->sorted.items[-1 - index].util; | |
204 | else if (index > 0 && | |
205 | starts_with(list->sorted.items[index - 1].string, string)) | |
206 | return -1; | |
207 | else if (index + 1 < list->sorted.nr && | |
208 | starts_with(list->sorted.items[index + 1].string, string)) | |
209 | return -1; | |
d34e4502 JS |
210 | else if (index < list->sorted.nr && |
211 | starts_with(list->sorted.items[index].string, string)) | |
76b74323 JS |
212 | item = list->sorted.items[index].util; |
213 | else | |
214 | return -1; | |
215 | return item - list->items.items; | |
216 | } | |
217 | ||
5e82b9e4 | 218 | struct list_options { |
6348bfba | 219 | int columns; |
5e82b9e4 | 220 | const char *header; |
f37c2264 JS |
221 | void (*print_item)(int i, int selected, struct string_list_item *item, |
222 | void *print_item_data); | |
5e82b9e4 DF |
223 | void *print_item_data; |
224 | }; | |
225 | ||
f37c2264 | 226 | static void list(struct add_i_state *s, struct string_list *list, int *selected, |
1daaebca | 227 | struct list_options *opts) |
5e82b9e4 | 228 | { |
6348bfba | 229 | int i, last_lf = 0; |
5e82b9e4 DF |
230 | |
231 | if (!list->nr) | |
232 | return; | |
233 | ||
234 | if (opts->header) | |
1daaebca SĐ |
235 | color_fprintf_ln(stdout, s->header_color, |
236 | "%s", opts->header); | |
5e82b9e4 DF |
237 | |
238 | for (i = 0; i < list->nr; i++) { | |
f37c2264 JS |
239 | opts->print_item(i, selected ? selected[i] : 0, list->items + i, |
240 | opts->print_item_data); | |
6348bfba JS |
241 | |
242 | if ((opts->columns) && ((i + 1) % (opts->columns))) { | |
243 | putchar('\t'); | |
244 | last_lf = 0; | |
245 | } | |
246 | else { | |
247 | putchar('\n'); | |
248 | last_lf = 1; | |
249 | } | |
250 | } | |
251 | ||
252 | if (!last_lf) | |
5e82b9e4 | 253 | putchar('\n'); |
6348bfba JS |
254 | } |
255 | struct list_and_choose_options { | |
256 | struct list_options list_opts; | |
257 | ||
258 | const char *prompt; | |
f37c2264 JS |
259 | enum { |
260 | SINGLETON = (1<<0), | |
261 | IMMEDIATE = (1<<1), | |
262 | } flags; | |
68db1cbf | 263 | void (*print_help)(struct add_i_state *s); |
6348bfba JS |
264 | }; |
265 | ||
266 | #define LIST_AND_CHOOSE_ERROR (-1) | |
267 | #define LIST_AND_CHOOSE_QUIT (-2) | |
268 | ||
269 | /* | |
f37c2264 JS |
270 | * Returns the selected index in singleton mode, the number of selected items |
271 | * otherwise. | |
6348bfba JS |
272 | * |
273 | * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF, | |
274 | * `LIST_AND_CHOOSE_QUIT` is returned. | |
275 | */ | |
76b74323 JS |
276 | static ssize_t list_and_choose(struct add_i_state *s, |
277 | struct prefix_item_list *items, | |
6348bfba JS |
278 | struct list_and_choose_options *opts) |
279 | { | |
f37c2264 JS |
280 | int singleton = opts->flags & SINGLETON; |
281 | int immediate = opts->flags & IMMEDIATE; | |
282 | ||
6348bfba | 283 | struct strbuf input = STRBUF_INIT; |
f37c2264 JS |
284 | ssize_t res = singleton ? LIST_AND_CHOOSE_ERROR : 0; |
285 | ||
286 | if (!singleton) { | |
287 | free(items->selected); | |
288 | CALLOC_ARRAY(items->selected, items->items.nr); | |
289 | } | |
290 | ||
291 | if (singleton && !immediate) | |
292 | BUG("singleton requires immediate"); | |
6348bfba | 293 | |
76b74323 JS |
294 | find_unique_prefixes(items); |
295 | ||
6348bfba JS |
296 | for (;;) { |
297 | char *p; | |
298 | ||
299 | strbuf_reset(&input); | |
300 | ||
f37c2264 | 301 | list(s, &items->items, items->selected, &opts->list_opts); |
6348bfba | 302 | |
3d965c76 | 303 | color_fprintf(stdout, s->prompt_color, "%s", opts->prompt); |
f37c2264 | 304 | fputs(singleton ? "> " : ">> ", stdout); |
6348bfba JS |
305 | fflush(stdout); |
306 | ||
08d383f2 | 307 | if (git_read_line_interactively(&input) == EOF) { |
6348bfba | 308 | putchar('\n'); |
f37c2264 JS |
309 | if (immediate) |
310 | res = LIST_AND_CHOOSE_QUIT; | |
6348bfba JS |
311 | break; |
312 | } | |
6348bfba JS |
313 | |
314 | if (!input.len) | |
315 | break; | |
316 | ||
68db1cbf JS |
317 | if (!strcmp(input.buf, "?")) { |
318 | opts->print_help(s); | |
319 | continue; | |
320 | } | |
321 | ||
6348bfba JS |
322 | p = input.buf; |
323 | for (;;) { | |
324 | size_t sep = strcspn(p, " \t\r\n,"); | |
f37c2264 JS |
325 | int choose = 1; |
326 | /* `from` is inclusive, `to` is exclusive */ | |
327 | ssize_t from = -1, to = -1; | |
6348bfba JS |
328 | |
329 | if (!sep) { | |
330 | if (!*p) | |
331 | break; | |
332 | p++; | |
333 | continue; | |
334 | } | |
335 | ||
f37c2264 JS |
336 | /* Input that begins with '-'; de-select */ |
337 | if (*p == '-') { | |
338 | choose = 0; | |
339 | p++; | |
340 | sep--; | |
341 | } | |
342 | ||
343 | if (sep == 1 && *p == '*') { | |
344 | from = 0; | |
345 | to = items->items.nr; | |
346 | } else if (isdigit(*p)) { | |
6348bfba | 347 | char *endp; |
f37c2264 JS |
348 | /* |
349 | * A range can be specified like 5-7 or 5-. | |
350 | * | |
351 | * Note: `from` is 0-based while the user input | |
352 | * is 1-based, hence we have to decrement by | |
353 | * one. We do not have to decrement `to` even | |
354 | * if it is 0-based because it is an exclusive | |
355 | * boundary. | |
356 | */ | |
357 | from = strtoul(p, &endp, 10) - 1; | |
358 | if (endp == p + sep) | |
359 | to = from + 1; | |
360 | else if (*endp == '-') { | |
849e43cc JS |
361 | if (isdigit(*(++endp))) |
362 | to = strtoul(endp, &endp, 10); | |
363 | else | |
364 | to = items->items.nr; | |
f37c2264 JS |
365 | /* extra characters after the range? */ |
366 | if (endp != p + sep) | |
367 | from = -1; | |
368 | } | |
6348bfba JS |
369 | } |
370 | ||
371 | if (p[sep]) | |
372 | p[sep++] = '\0'; | |
f37c2264 JS |
373 | if (from < 0) { |
374 | from = find_unique(p, items); | |
375 | if (from >= 0) | |
376 | to = from + 1; | |
377 | } | |
76b74323 | 378 | |
f37c2264 JS |
379 | if (from < 0 || from >= items->items.nr || |
380 | (singleton && from + 1 != to)) { | |
cb581b16 | 381 | color_fprintf_ln(stderr, s->error_color, |
3d965c76 | 382 | _("Huh (%s)?"), p); |
f37c2264 JS |
383 | break; |
384 | } else if (singleton) { | |
385 | res = from; | |
6348bfba JS |
386 | break; |
387 | } | |
388 | ||
f37c2264 JS |
389 | if (to > items->items.nr) |
390 | to = items->items.nr; | |
391 | ||
392 | for (; from < to; from++) | |
393 | if (items->selected[from] != choose) { | |
394 | items->selected[from] = choose; | |
395 | res += choose ? +1 : -1; | |
396 | } | |
397 | ||
6348bfba JS |
398 | p += sep; |
399 | } | |
400 | ||
f37c2264 JS |
401 | if ((immediate && res != LIST_AND_CHOOSE_ERROR) || |
402 | !strcmp(input.buf, "*")) | |
6348bfba | 403 | break; |
5e82b9e4 | 404 | } |
6348bfba JS |
405 | |
406 | strbuf_release(&input); | |
407 | return res; | |
5e82b9e4 DF |
408 | } |
409 | ||
410 | struct adddel { | |
411 | uintmax_t add, del; | |
8746e072 | 412 | unsigned seen:1, unmerged:1, binary:1; |
5e82b9e4 DF |
413 | }; |
414 | ||
415 | struct file_item { | |
a8c45be9 | 416 | size_t prefix_length; |
5e82b9e4 DF |
417 | struct adddel index, worktree; |
418 | }; | |
419 | ||
420 | static void add_file_item(struct string_list *files, const char *name) | |
421 | { | |
241b5d3e | 422 | struct file_item *item = xcalloc(1, sizeof(*item)); |
5e82b9e4 DF |
423 | |
424 | string_list_append(files, name)->util = item; | |
425 | } | |
426 | ||
427 | struct pathname_entry { | |
428 | struct hashmap_entry ent; | |
429 | const char *name; | |
430 | struct file_item *item; | |
431 | }; | |
432 | ||
5cf88fd8 | 433 | static int pathname_entry_cmp(const void *cmp_data UNUSED, |
5e82b9e4 DF |
434 | const struct hashmap_entry *he1, |
435 | const struct hashmap_entry *he2, | |
436 | const void *name) | |
437 | { | |
438 | const struct pathname_entry *e1 = | |
439 | container_of(he1, const struct pathname_entry, ent); | |
440 | const struct pathname_entry *e2 = | |
441 | container_of(he2, const struct pathname_entry, ent); | |
442 | ||
443 | return strcmp(e1->name, name ? (const char *)name : e2->name); | |
444 | } | |
445 | ||
446 | struct collection_status { | |
c08171d1 | 447 | enum { FROM_WORKTREE = 0, FROM_INDEX = 1 } mode; |
5e82b9e4 DF |
448 | |
449 | const char *reference; | |
450 | ||
c08171d1 | 451 | unsigned skip_unseen:1; |
8746e072 | 452 | size_t unmerged_count, binary_count; |
5e82b9e4 DF |
453 | struct string_list *files; |
454 | struct hashmap file_map; | |
455 | }; | |
456 | ||
457 | static void collect_changes_cb(struct diff_queue_struct *q, | |
458 | struct diff_options *options, | |
459 | void *data) | |
460 | { | |
461 | struct collection_status *s = data; | |
462 | struct diffstat_t stat = { 0 }; | |
463 | int i; | |
464 | ||
465 | if (!q->nr) | |
466 | return; | |
467 | ||
468 | compute_diffstat(options, &stat, q); | |
469 | ||
470 | for (i = 0; i < stat.nr; i++) { | |
471 | const char *name = stat.files[i]->name; | |
472 | int hash = strhash(name); | |
473 | struct pathname_entry *entry; | |
474 | struct file_item *file_item; | |
8746e072 | 475 | struct adddel *adddel, *other_adddel; |
5e82b9e4 DF |
476 | |
477 | entry = hashmap_get_entry_from_hash(&s->file_map, hash, name, | |
478 | struct pathname_entry, ent); | |
479 | if (!entry) { | |
c08171d1 JS |
480 | if (s->skip_unseen) |
481 | continue; | |
482 | ||
5e82b9e4 DF |
483 | add_file_item(s->files, name); |
484 | ||
ca56dadb | 485 | CALLOC_ARRAY(entry, 1); |
5e82b9e4 DF |
486 | hashmap_entry_init(&entry->ent, hash); |
487 | entry->name = s->files->items[s->files->nr - 1].string; | |
488 | entry->item = s->files->items[s->files->nr - 1].util; | |
489 | hashmap_add(&s->file_map, &entry->ent); | |
490 | } | |
491 | ||
492 | file_item = entry->item; | |
c08171d1 | 493 | adddel = s->mode == FROM_INDEX ? |
5e82b9e4 | 494 | &file_item->index : &file_item->worktree; |
8746e072 JS |
495 | other_adddel = s->mode == FROM_INDEX ? |
496 | &file_item->worktree : &file_item->index; | |
5e82b9e4 DF |
497 | adddel->seen = 1; |
498 | adddel->add = stat.files[i]->added; | |
499 | adddel->del = stat.files[i]->deleted; | |
8746e072 JS |
500 | if (stat.files[i]->is_binary) { |
501 | if (!other_adddel->binary) | |
502 | s->binary_count++; | |
5e82b9e4 | 503 | adddel->binary = 1; |
8746e072 JS |
504 | } |
505 | if (stat.files[i]->is_unmerged) { | |
506 | if (!other_adddel->unmerged) | |
507 | s->unmerged_count++; | |
508 | adddel->unmerged = 1; | |
509 | } | |
5e82b9e4 DF |
510 | } |
511 | free_diffstat_info(&stat); | |
512 | } | |
513 | ||
c08171d1 JS |
514 | enum modified_files_filter { |
515 | NO_FILTER = 0, | |
516 | WORKTREE_ONLY = 1, | |
517 | INDEX_ONLY = 2, | |
518 | }; | |
519 | ||
520 | static int get_modified_files(struct repository *r, | |
521 | enum modified_files_filter filter, | |
a8c45be9 | 522 | struct prefix_item_list *files, |
8746e072 JS |
523 | const struct pathspec *ps, |
524 | size_t *unmerged_count, | |
525 | size_t *binary_count) | |
5e82b9e4 DF |
526 | { |
527 | struct object_id head_oid; | |
528 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, | |
529 | &head_oid, NULL); | |
c08171d1 JS |
530 | struct collection_status s = { 0 }; |
531 | int i; | |
5e82b9e4 DF |
532 | |
533 | if (discard_index(r->index) < 0 || | |
534 | repo_read_index_preload(r, ps, 0) < 0) | |
535 | return error(_("could not read index")); | |
536 | ||
a8c45be9 JS |
537 | prefix_item_list_clear(files); |
538 | s.files = &files->items; | |
5e82b9e4 DF |
539 | hashmap_init(&s.file_map, pathname_entry_cmp, NULL, 0); |
540 | ||
c08171d1 | 541 | for (i = 0; i < 2; i++) { |
5e82b9e4 DF |
542 | struct rev_info rev; |
543 | struct setup_revision_opt opt = { 0 }; | |
544 | ||
c08171d1 JS |
545 | if (filter == INDEX_ONLY) |
546 | s.mode = (i == 0) ? FROM_INDEX : FROM_WORKTREE; | |
547 | else | |
548 | s.mode = (i == 0) ? FROM_WORKTREE : FROM_INDEX; | |
549 | s.skip_unseen = filter && i; | |
550 | ||
5e82b9e4 DF |
551 | opt.def = is_initial ? |
552 | empty_tree_oid_hex() : oid_to_hex(&head_oid); | |
553 | ||
554 | init_revisions(&rev, NULL); | |
555 | setup_revisions(0, NULL, &rev, &opt); | |
556 | ||
557 | rev.diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
558 | rev.diffopt.format_callback = collect_changes_cb; | |
559 | rev.diffopt.format_callback_data = &s; | |
560 | ||
561 | if (ps) | |
562 | copy_pathspec(&rev.prune_data, ps); | |
563 | ||
c08171d1 | 564 | if (s.mode == FROM_INDEX) |
5e82b9e4 DF |
565 | run_diff_index(&rev, 1); |
566 | else { | |
567 | rev.diffopt.flags.ignore_dirty_submodules = 1; | |
568 | run_diff_files(&rev, 0); | |
569 | } | |
0c3944a6 | 570 | |
2108fe4a | 571 | release_revisions(&rev); |
5e82b9e4 | 572 | } |
6da1a258 | 573 | hashmap_clear_and_free(&s.file_map, struct pathname_entry, ent); |
8746e072 JS |
574 | if (unmerged_count) |
575 | *unmerged_count = s.unmerged_count; | |
576 | if (binary_count) | |
577 | *binary_count = s.binary_count; | |
5e82b9e4 DF |
578 | |
579 | /* While the diffs are ordered already, we ran *two* diffs... */ | |
a8c45be9 | 580 | string_list_sort(&files->items); |
5e82b9e4 DF |
581 | |
582 | return 0; | |
583 | } | |
584 | ||
585 | static void render_adddel(struct strbuf *buf, | |
586 | struct adddel *ad, const char *no_changes) | |
587 | { | |
588 | if (ad->binary) | |
589 | strbuf_addstr(buf, _("binary")); | |
590 | else if (ad->seen) | |
591 | strbuf_addf(buf, "+%"PRIuMAX"/-%"PRIuMAX, | |
592 | (uintmax_t)ad->add, (uintmax_t)ad->del); | |
593 | else | |
594 | strbuf_addstr(buf, no_changes); | |
595 | } | |
596 | ||
76b74323 JS |
597 | /* filters out prefixes which have special meaning to list_and_choose() */ |
598 | static int is_valid_prefix(const char *prefix, size_t prefix_len) | |
599 | { | |
600 | return prefix_len && prefix && | |
601 | /* | |
602 | * We expect `prefix` to be NUL terminated, therefore this | |
603 | * `strcspn()` call is okay, even if it might do much more | |
604 | * work than strictly necessary. | |
605 | */ | |
606 | strcspn(prefix, " \t\r\n,") >= prefix_len && /* separators */ | |
607 | *prefix != '-' && /* deselection */ | |
608 | !isdigit(*prefix) && /* selection */ | |
609 | (prefix_len != 1 || | |
610 | (*prefix != '*' && /* "all" wildcard */ | |
611 | *prefix != '?')); /* prompt help */ | |
612 | } | |
613 | ||
5e82b9e4 | 614 | struct print_file_item_data { |
a8c45be9 JS |
615 | const char *modified_fmt, *color, *reset; |
616 | struct strbuf buf, name, index, worktree; | |
ab1e1ccc | 617 | unsigned only_names:1; |
5e82b9e4 DF |
618 | }; |
619 | ||
f37c2264 | 620 | static void print_file_item(int i, int selected, struct string_list_item *item, |
5e82b9e4 DF |
621 | void *print_file_item_data) |
622 | { | |
623 | struct file_item *c = item->util; | |
624 | struct print_file_item_data *d = print_file_item_data; | |
a8c45be9 | 625 | const char *highlighted = NULL; |
5e82b9e4 DF |
626 | |
627 | strbuf_reset(&d->index); | |
628 | strbuf_reset(&d->worktree); | |
629 | strbuf_reset(&d->buf); | |
630 | ||
a8c45be9 JS |
631 | /* Format the item with the prefix highlighted. */ |
632 | if (c->prefix_length > 0 && | |
633 | is_valid_prefix(item->string, c->prefix_length)) { | |
634 | strbuf_reset(&d->name); | |
635 | strbuf_addf(&d->name, "%s%.*s%s%s", d->color, | |
636 | (int)c->prefix_length, item->string, d->reset, | |
637 | item->string + c->prefix_length); | |
638 | highlighted = d->name.buf; | |
639 | } | |
640 | ||
ab1e1ccc JS |
641 | if (d->only_names) { |
642 | printf("%c%2d: %s", selected ? '*' : ' ', i + 1, | |
643 | highlighted ? highlighted : item->string); | |
644 | return; | |
645 | } | |
646 | ||
5e82b9e4 DF |
647 | render_adddel(&d->worktree, &c->worktree, _("nothing")); |
648 | render_adddel(&d->index, &c->index, _("unchanged")); | |
a8c45be9 JS |
649 | |
650 | strbuf_addf(&d->buf, d->modified_fmt, d->index.buf, d->worktree.buf, | |
651 | highlighted ? highlighted : item->string); | |
5e82b9e4 | 652 | |
f37c2264 | 653 | printf("%c%2d: %s", selected ? '*' : ' ', i + 1, d->buf.buf); |
5e82b9e4 DF |
654 | } |
655 | ||
656 | static int run_status(struct add_i_state *s, const struct pathspec *ps, | |
a8c45be9 JS |
657 | struct prefix_item_list *files, |
658 | struct list_and_choose_options *opts) | |
5e82b9e4 | 659 | { |
8746e072 | 660 | if (get_modified_files(s->r, NO_FILTER, files, ps, NULL, NULL) < 0) |
5e82b9e4 DF |
661 | return -1; |
662 | ||
a8c45be9 | 663 | list(s, &files->items, NULL, &opts->list_opts); |
5e82b9e4 DF |
664 | putchar('\n'); |
665 | ||
666 | return 0; | |
667 | } | |
f83dff60 | 668 | |
a8c45be9 JS |
669 | static int run_update(struct add_i_state *s, const struct pathspec *ps, |
670 | struct prefix_item_list *files, | |
671 | struct list_and_choose_options *opts) | |
672 | { | |
673 | int res = 0, fd; | |
674 | size_t count, i; | |
675 | struct lock_file index_lock; | |
676 | ||
8746e072 | 677 | if (get_modified_files(s->r, WORKTREE_ONLY, files, ps, NULL, NULL) < 0) |
a8c45be9 JS |
678 | return -1; |
679 | ||
680 | if (!files->items.nr) { | |
681 | putchar('\n'); | |
682 | return 0; | |
683 | } | |
684 | ||
685 | opts->prompt = N_("Update"); | |
686 | count = list_and_choose(s, files, opts); | |
687 | if (count <= 0) { | |
688 | putchar('\n'); | |
689 | return 0; | |
690 | } | |
691 | ||
692 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
693 | if (fd < 0) { | |
694 | putchar('\n'); | |
695 | return -1; | |
696 | } | |
697 | ||
698 | for (i = 0; i < files->items.nr; i++) { | |
699 | const char *name = files->items.items[i].string; | |
4788e8b2 JS |
700 | struct stat st; |
701 | ||
702 | if (!files->selected[i]) | |
703 | continue; | |
704 | if (lstat(name, &st) && is_missing_file_error(errno)) { | |
705 | if (remove_file_from_index(s->r->index, name) < 0) { | |
706 | res = error(_("could not stage '%s'"), name); | |
707 | break; | |
708 | } | |
709 | } else if (add_file_to_index(s->r->index, name, 0) < 0) { | |
a8c45be9 JS |
710 | res = error(_("could not stage '%s'"), name); |
711 | break; | |
712 | } | |
713 | } | |
714 | ||
715 | if (!res && write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0) | |
716 | res = error(_("could not write index")); | |
717 | ||
718 | if (!res) | |
719 | printf(Q_("updated %d path\n", | |
720 | "updated %d paths\n", count), (int)count); | |
721 | ||
722 | putchar('\n'); | |
723 | return res; | |
724 | } | |
725 | ||
c54ef5e4 JS |
726 | static void revert_from_diff(struct diff_queue_struct *q, |
727 | struct diff_options *opt, void *data) | |
728 | { | |
729 | int i, add_flags = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE; | |
730 | ||
731 | for (i = 0; i < q->nr; i++) { | |
732 | struct diff_filespec *one = q->queue[i]->one; | |
733 | struct cache_entry *ce; | |
734 | ||
735 | if (!(one->mode && !is_null_oid(&one->oid))) { | |
736 | remove_file_from_index(opt->repo->index, one->path); | |
737 | printf(_("note: %s is untracked now.\n"), one->path); | |
738 | } else { | |
739 | ce = make_cache_entry(opt->repo->index, one->mode, | |
740 | &one->oid, one->path, 0, 0); | |
741 | if (!ce) | |
742 | die(_("make_cache_entry failed for path '%s'"), | |
743 | one->path); | |
744 | add_index_entry(opt->repo->index, ce, add_flags); | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
749 | static int run_revert(struct add_i_state *s, const struct pathspec *ps, | |
750 | struct prefix_item_list *files, | |
751 | struct list_and_choose_options *opts) | |
752 | { | |
753 | int res = 0, fd; | |
754 | size_t count, i, j; | |
755 | ||
756 | struct object_id oid; | |
757 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, | |
758 | NULL); | |
759 | struct lock_file index_lock; | |
760 | const char **paths; | |
761 | struct tree *tree; | |
762 | struct diff_options diffopt = { NULL }; | |
763 | ||
8746e072 | 764 | if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0) |
c54ef5e4 JS |
765 | return -1; |
766 | ||
767 | if (!files->items.nr) { | |
768 | putchar('\n'); | |
769 | return 0; | |
770 | } | |
771 | ||
772 | opts->prompt = N_("Revert"); | |
773 | count = list_and_choose(s, files, opts); | |
774 | if (count <= 0) | |
775 | goto finish_revert; | |
776 | ||
777 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
778 | if (fd < 0) { | |
779 | res = -1; | |
780 | goto finish_revert; | |
781 | } | |
782 | ||
783 | if (is_initial) | |
784 | oidcpy(&oid, s->r->hash_algo->empty_tree); | |
785 | else { | |
786 | tree = parse_tree_indirect(&oid); | |
787 | if (!tree) { | |
788 | res = error(_("Could not parse HEAD^{tree}")); | |
789 | goto finish_revert; | |
790 | } | |
791 | oidcpy(&oid, &tree->object.oid); | |
792 | } | |
793 | ||
794 | ALLOC_ARRAY(paths, count + 1); | |
795 | for (i = j = 0; i < files->items.nr; i++) | |
796 | if (files->selected[i]) | |
797 | paths[j++] = files->items.items[i].string; | |
798 | paths[j] = NULL; | |
799 | ||
800 | parse_pathspec(&diffopt.pathspec, 0, | |
801 | PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, | |
802 | NULL, paths); | |
803 | ||
804 | diffopt.output_format = DIFF_FORMAT_CALLBACK; | |
805 | diffopt.format_callback = revert_from_diff; | |
806 | diffopt.flags.override_submodule_config = 1; | |
807 | diffopt.repo = s->r; | |
808 | ||
244c2724 ÆAB |
809 | if (do_diff_cache(&oid, &diffopt)) { |
810 | diff_free(&diffopt); | |
c54ef5e4 | 811 | res = -1; |
244c2724 | 812 | } else { |
c54ef5e4 JS |
813 | diffcore_std(&diffopt); |
814 | diff_flush(&diffopt); | |
815 | } | |
816 | free(paths); | |
c54ef5e4 JS |
817 | |
818 | if (!res && write_locked_index(s->r->index, &index_lock, | |
819 | COMMIT_LOCK) < 0) | |
820 | res = -1; | |
821 | else | |
822 | res = repo_refresh_and_write_index(s->r, REFRESH_QUIET, 0, 1, | |
823 | NULL, NULL, NULL); | |
824 | ||
825 | if (!res) | |
826 | printf(Q_("reverted %d path\n", | |
827 | "reverted %d paths\n", count), (int)count); | |
828 | ||
829 | finish_revert: | |
830 | putchar('\n'); | |
831 | return res; | |
832 | } | |
833 | ||
ab1e1ccc JS |
834 | static int get_untracked_files(struct repository *r, |
835 | struct prefix_item_list *files, | |
836 | const struct pathspec *ps) | |
837 | { | |
838 | struct dir_struct dir = { 0 }; | |
839 | size_t i; | |
840 | struct strbuf buf = STRBUF_INIT; | |
841 | ||
842 | if (repo_read_index(r) < 0) | |
843 | return error(_("could not read index")); | |
844 | ||
845 | prefix_item_list_clear(files); | |
846 | setup_standard_excludes(&dir); | |
847 | add_pattern_list(&dir, EXC_CMDL, "--exclude option"); | |
848 | fill_directory(&dir, r->index, ps); | |
849 | ||
850 | for (i = 0; i < dir.nr; i++) { | |
851 | struct dir_entry *ent = dir.entries[i]; | |
852 | ||
853 | if (index_name_is_other(r->index, ent->name, ent->len)) { | |
854 | strbuf_reset(&buf); | |
855 | strbuf_add(&buf, ent->name, ent->len); | |
856 | add_file_item(&files->items, buf.buf); | |
857 | } | |
858 | } | |
859 | ||
860 | strbuf_release(&buf); | |
861 | return 0; | |
862 | } | |
863 | ||
864 | static int run_add_untracked(struct add_i_state *s, const struct pathspec *ps, | |
865 | struct prefix_item_list *files, | |
866 | struct list_and_choose_options *opts) | |
867 | { | |
868 | struct print_file_item_data *d = opts->list_opts.print_item_data; | |
869 | int res = 0, fd; | |
870 | size_t count, i; | |
871 | struct lock_file index_lock; | |
872 | ||
873 | if (get_untracked_files(s->r, files, ps) < 0) | |
874 | return -1; | |
875 | ||
876 | if (!files->items.nr) { | |
877 | printf(_("No untracked files.\n")); | |
878 | goto finish_add_untracked; | |
879 | } | |
880 | ||
881 | opts->prompt = N_("Add untracked"); | |
882 | d->only_names = 1; | |
883 | count = list_and_choose(s, files, opts); | |
884 | d->only_names = 0; | |
885 | if (count <= 0) | |
886 | goto finish_add_untracked; | |
887 | ||
888 | fd = repo_hold_locked_index(s->r, &index_lock, LOCK_REPORT_ON_ERROR); | |
889 | if (fd < 0) { | |
890 | res = -1; | |
891 | goto finish_add_untracked; | |
892 | } | |
893 | ||
894 | for (i = 0; i < files->items.nr; i++) { | |
895 | const char *name = files->items.items[i].string; | |
896 | if (files->selected[i] && | |
897 | add_file_to_index(s->r->index, name, 0) < 0) { | |
898 | res = error(_("could not stage '%s'"), name); | |
899 | break; | |
900 | } | |
901 | } | |
902 | ||
903 | if (!res && | |
904 | write_locked_index(s->r->index, &index_lock, COMMIT_LOCK) < 0) | |
905 | res = error(_("could not write index")); | |
906 | ||
907 | if (!res) | |
908 | printf(Q_("added %d path\n", | |
909 | "added %d paths\n", count), (int)count); | |
910 | ||
911 | finish_add_untracked: | |
912 | putchar('\n'); | |
913 | return res; | |
914 | } | |
915 | ||
8746e072 JS |
916 | static int run_patch(struct add_i_state *s, const struct pathspec *ps, |
917 | struct prefix_item_list *files, | |
918 | struct list_and_choose_options *opts) | |
919 | { | |
920 | int res = 0; | |
921 | ssize_t count, i, j; | |
922 | size_t unmerged_count = 0, binary_count = 0; | |
923 | ||
924 | if (get_modified_files(s->r, WORKTREE_ONLY, files, ps, | |
925 | &unmerged_count, &binary_count) < 0) | |
926 | return -1; | |
927 | ||
928 | if (unmerged_count || binary_count) { | |
929 | for (i = j = 0; i < files->items.nr; i++) { | |
930 | struct file_item *item = files->items.items[i].util; | |
931 | ||
932 | if (item->index.binary || item->worktree.binary) { | |
933 | free(item); | |
934 | free(files->items.items[i].string); | |
935 | } else if (item->index.unmerged || | |
936 | item->worktree.unmerged) { | |
937 | color_fprintf_ln(stderr, s->error_color, | |
938 | _("ignoring unmerged: %s"), | |
939 | files->items.items[i].string); | |
940 | free(item); | |
941 | free(files->items.items[i].string); | |
942 | } else | |
943 | files->items.items[j++] = files->items.items[i]; | |
944 | } | |
945 | files->items.nr = j; | |
946 | } | |
947 | ||
948 | if (!files->items.nr) { | |
949 | if (binary_count) | |
950 | fprintf(stderr, _("Only binary files changed.\n")); | |
951 | else | |
952 | fprintf(stderr, _("No changes.\n")); | |
953 | return 0; | |
954 | } | |
955 | ||
956 | opts->prompt = N_("Patch update"); | |
957 | count = list_and_choose(s, files, opts); | |
d660a30c | 958 | if (count > 0) { |
ef8d7ac4 | 959 | struct strvec args = STRVEC_INIT; |
1942ee44 | 960 | struct pathspec ps_selected = { 0 }; |
8746e072 | 961 | |
8746e072 JS |
962 | for (i = 0; i < files->items.nr; i++) |
963 | if (files->selected[i]) | |
ef8d7ac4 | 964 | strvec_push(&args, |
f6d8942b | 965 | files->items.items[i].string); |
1942ee44 JS |
966 | parse_pathspec(&ps_selected, |
967 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, | |
d70a9eb6 | 968 | PATHSPEC_LITERAL_PATH, "", args.v); |
d2a233cb | 969 | res = run_add_p(s->r, ADD_P_ADD, NULL, &ps_selected); |
ef8d7ac4 | 970 | strvec_clear(&args); |
1942ee44 | 971 | clear_pathspec(&ps_selected); |
8746e072 JS |
972 | } |
973 | ||
974 | return res; | |
975 | } | |
976 | ||
d7633578 JS |
977 | static int run_diff(struct add_i_state *s, const struct pathspec *ps, |
978 | struct prefix_item_list *files, | |
979 | struct list_and_choose_options *opts) | |
980 | { | |
981 | int res = 0; | |
982 | ssize_t count, i; | |
983 | ||
984 | struct object_id oid; | |
985 | int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, | |
986 | NULL); | |
987 | if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0) | |
988 | return -1; | |
989 | ||
990 | if (!files->items.nr) { | |
991 | putchar('\n'); | |
992 | return 0; | |
993 | } | |
994 | ||
995 | opts->prompt = N_("Review diff"); | |
996 | opts->flags = IMMEDIATE; | |
997 | count = list_and_choose(s, files, opts); | |
998 | opts->flags = 0; | |
d660a30c | 999 | if (count > 0) { |
ef8d7ac4 | 1000 | struct strvec args = STRVEC_INIT; |
d7633578 | 1001 | |
ef8d7ac4 | 1002 | strvec_pushl(&args, "git", "diff", "-p", "--cached", |
f6d8942b JK |
1003 | oid_to_hex(!is_initial ? &oid : |
1004 | s->r->hash_algo->empty_tree), | |
1005 | "--", NULL); | |
d7633578 JS |
1006 | for (i = 0; i < files->items.nr; i++) |
1007 | if (files->selected[i]) | |
ef8d7ac4 | 1008 | strvec_push(&args, |
f6d8942b | 1009 | files->items.items[i].string); |
d70a9eb6 | 1010 | res = run_command_v_opt(args.v, 0); |
ef8d7ac4 | 1011 | strvec_clear(&args); |
d7633578 JS |
1012 | } |
1013 | ||
1014 | putchar('\n'); | |
1015 | return res; | |
1016 | } | |
1017 | ||
8c159044 | 1018 | static int run_help(struct add_i_state *s, const struct pathspec *unused_ps, |
a8c45be9 JS |
1019 | struct prefix_item_list *unused_files, |
1020 | struct list_and_choose_options *unused_opts) | |
8c159044 SĐ |
1021 | { |
1022 | color_fprintf_ln(stdout, s->help_color, "status - %s", | |
1023 | _("show paths with changes")); | |
1024 | color_fprintf_ln(stdout, s->help_color, "update - %s", | |
1025 | _("add working tree state to the staged set of changes")); | |
1026 | color_fprintf_ln(stdout, s->help_color, "revert - %s", | |
1027 | _("revert staged set of changes back to the HEAD version")); | |
1028 | color_fprintf_ln(stdout, s->help_color, "patch - %s", | |
1029 | _("pick hunks and update selectively")); | |
1030 | color_fprintf_ln(stdout, s->help_color, "diff - %s", | |
1031 | _("view diff between HEAD and index")); | |
1032 | color_fprintf_ln(stdout, s->help_color, "add untracked - %s", | |
1033 | _("add contents of untracked files to the staged set of changes")); | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
a8c45be9 JS |
1038 | static void choose_prompt_help(struct add_i_state *s) |
1039 | { | |
1040 | color_fprintf_ln(stdout, s->help_color, "%s", | |
1041 | _("Prompt help:")); | |
1042 | color_fprintf_ln(stdout, s->help_color, "1 - %s", | |
1043 | _("select a single item")); | |
1044 | color_fprintf_ln(stdout, s->help_color, "3-5 - %s", | |
1045 | _("select a range of items")); | |
1046 | color_fprintf_ln(stdout, s->help_color, "2-3,6-9 - %s", | |
1047 | _("select multiple ranges")); | |
1048 | color_fprintf_ln(stdout, s->help_color, "foo - %s", | |
1049 | _("select item based on unique prefix")); | |
1050 | color_fprintf_ln(stdout, s->help_color, "-... - %s", | |
1051 | _("unselect specified items")); | |
1052 | color_fprintf_ln(stdout, s->help_color, "* - %s", | |
1053 | _("choose all items")); | |
1054 | color_fprintf_ln(stdout, s->help_color, " - %s", | |
1055 | _("(empty) finish selecting")); | |
1056 | } | |
1057 | ||
6348bfba | 1058 | typedef int (*command_t)(struct add_i_state *s, const struct pathspec *ps, |
a8c45be9 JS |
1059 | struct prefix_item_list *files, |
1060 | struct list_and_choose_options *opts); | |
6348bfba | 1061 | |
76b74323 JS |
1062 | struct command_item { |
1063 | size_t prefix_length; | |
1064 | command_t command; | |
1065 | }; | |
1066 | ||
3d965c76 SĐ |
1067 | struct print_command_item_data { |
1068 | const char *color, *reset; | |
1069 | }; | |
1070 | ||
f37c2264 JS |
1071 | static void print_command_item(int i, int selected, |
1072 | struct string_list_item *item, | |
6348bfba JS |
1073 | void *print_command_item_data) |
1074 | { | |
3d965c76 | 1075 | struct print_command_item_data *d = print_command_item_data; |
76b74323 JS |
1076 | struct command_item *util = item->util; |
1077 | ||
1078 | if (!util->prefix_length || | |
1079 | !is_valid_prefix(item->string, util->prefix_length)) | |
1080 | printf(" %2d: %s", i + 1, item->string); | |
1081 | else | |
3d965c76 SĐ |
1082 | printf(" %2d: %s%.*s%s%s", i + 1, |
1083 | d->color, (int)util->prefix_length, item->string, | |
1084 | d->reset, item->string + util->prefix_length); | |
6348bfba JS |
1085 | } |
1086 | ||
68db1cbf JS |
1087 | static void command_prompt_help(struct add_i_state *s) |
1088 | { | |
1089 | const char *help_color = s->help_color; | |
1090 | color_fprintf_ln(stdout, help_color, "%s", _("Prompt help:")); | |
1091 | color_fprintf_ln(stdout, help_color, "1 - %s", | |
1092 | _("select a numbered item")); | |
1093 | color_fprintf_ln(stdout, help_color, "foo - %s", | |
1094 | _("select item based on unique prefix")); | |
1095 | color_fprintf_ln(stdout, help_color, " - %s", | |
1096 | _("(empty) select nothing")); | |
1097 | } | |
1098 | ||
f83dff60 JS |
1099 | int run_add_i(struct repository *r, const struct pathspec *ps) |
1100 | { | |
5e82b9e4 | 1101 | struct add_i_state s = { NULL }; |
3d965c76 | 1102 | struct print_command_item_data data = { "[", "]" }; |
6348bfba | 1103 | struct list_and_choose_options main_loop_opts = { |
3d965c76 | 1104 | { 4, N_("*** Commands ***"), print_command_item, &data }, |
f37c2264 | 1105 | N_("What now"), SINGLETON | IMMEDIATE, command_prompt_help |
6348bfba JS |
1106 | }; |
1107 | struct { | |
1108 | const char *string; | |
1109 | command_t command; | |
1110 | } command_list[] = { | |
1111 | { "status", run_status }, | |
a8c45be9 | 1112 | { "update", run_update }, |
c54ef5e4 | 1113 | { "revert", run_revert }, |
ab1e1ccc | 1114 | { "add untracked", run_add_untracked }, |
8746e072 | 1115 | { "patch", run_patch }, |
d7633578 | 1116 | { "diff", run_diff }, |
2e697ced | 1117 | { "quit", NULL }, |
8c159044 | 1118 | { "help", run_help }, |
6348bfba | 1119 | }; |
76b74323 | 1120 | struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT; |
6348bfba | 1121 | |
5e82b9e4 | 1122 | struct print_file_item_data print_file_item_data = { |
a8c45be9 JS |
1123 | "%12s %12s %s", NULL, NULL, |
1124 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT | |
5e82b9e4 | 1125 | }; |
a8c45be9 JS |
1126 | struct list_and_choose_options opts = { |
1127 | { 0, NULL, print_file_item, &print_file_item_data }, | |
1128 | NULL, 0, choose_prompt_help | |
5e82b9e4 DF |
1129 | }; |
1130 | struct strbuf header = STRBUF_INIT; | |
a8c45be9 | 1131 | struct prefix_item_list files = PREFIX_ITEM_LIST_INIT; |
6348bfba | 1132 | ssize_t i; |
5e82b9e4 DF |
1133 | int res = 0; |
1134 | ||
76b74323 | 1135 | for (i = 0; i < ARRAY_SIZE(command_list); i++) { |
241b5d3e | 1136 | struct command_item *util = xcalloc(1, sizeof(*util)); |
76b74323 JS |
1137 | util->command = command_list[i].command; |
1138 | string_list_append(&commands.items, command_list[i].string) | |
1139 | ->util = util; | |
1140 | } | |
6348bfba | 1141 | |
5e82b9e4 | 1142 | init_add_i_state(&s, r); |
6348bfba | 1143 | |
3d965c76 SĐ |
1144 | /* |
1145 | * When color was asked for, use the prompt color for | |
1146 | * highlighting, otherwise use square brackets. | |
1147 | */ | |
1148 | if (s.use_color) { | |
1149 | data.color = s.prompt_color; | |
1150 | data.reset = s.reset_color; | |
1151 | } | |
a8c45be9 JS |
1152 | print_file_item_data.color = data.color; |
1153 | print_file_item_data.reset = data.reset; | |
3d965c76 | 1154 | |
afae3cb6 | 1155 | strbuf_addstr(&header, " "); |
5e82b9e4 DF |
1156 | strbuf_addf(&header, print_file_item_data.modified_fmt, |
1157 | _("staged"), _("unstaged"), _("path")); | |
a8c45be9 | 1158 | opts.list_opts.header = header.buf; |
5e82b9e4 DF |
1159 | |
1160 | if (discard_index(r->index) < 0 || | |
1161 | repo_read_index(r) < 0 || | |
1162 | repo_refresh_and_write_index(r, REFRESH_QUIET, 0, 1, | |
1163 | NULL, NULL, NULL) < 0) | |
1164 | warning(_("could not refresh index")); | |
1165 | ||
1166 | res = run_status(&s, ps, &files, &opts); | |
1167 | ||
6348bfba | 1168 | for (;;) { |
2e697ced JS |
1169 | struct command_item *util; |
1170 | ||
6348bfba | 1171 | i = list_and_choose(&s, &commands, &main_loop_opts); |
2e697ced JS |
1172 | if (i < 0 || i >= commands.items.nr) |
1173 | util = NULL; | |
1174 | else | |
1175 | util = commands.items.items[i].util; | |
1176 | ||
1177 | if (i == LIST_AND_CHOOSE_QUIT || (util && !util->command)) { | |
6348bfba JS |
1178 | printf(_("Bye.\n")); |
1179 | res = 0; | |
1180 | break; | |
1181 | } | |
2e697ced JS |
1182 | |
1183 | if (util) | |
76b74323 | 1184 | res = util->command(&s, ps, &files, &opts); |
6348bfba JS |
1185 | } |
1186 | ||
a8c45be9 | 1187 | prefix_item_list_clear(&files); |
5e82b9e4 | 1188 | strbuf_release(&print_file_item_data.buf); |
a8c45be9 | 1189 | strbuf_release(&print_file_item_data.name); |
5e82b9e4 DF |
1190 | strbuf_release(&print_file_item_data.index); |
1191 | strbuf_release(&print_file_item_data.worktree); | |
1192 | strbuf_release(&header); | |
76b74323 | 1193 | prefix_item_list_clear(&commands); |
180f48df | 1194 | clear_add_i_state(&s); |
5e82b9e4 DF |
1195 | |
1196 | return res; | |
f83dff60 | 1197 | } |