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