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