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