2 #include "add-interactive.h"
8 #include "string-list.h"
11 #include "run-command.h"
13 static void init_color(struct repository
*r
, struct add_i_state
*s
,
14 const char *slot_name
, char *dst
,
15 const char *default_color
)
17 char *key
= xstrfmt("color.interactive.%s", slot_name
);
22 else if (repo_config_get_value(r
, key
, &value
) ||
23 color_parse(value
, dst
))
24 strlcpy(dst
, default_color
, COLOR_MAXLEN
);
29 void init_add_i_state(struct add_i_state
*s
, struct repository
*r
)
35 if (repo_config_get_value(r
, "color.interactive", &value
))
39 git_config_colorbool("color.interactive", value
);
40 s
->use_color
= want_color(s
->use_color
);
42 init_color(r
, s
, "header", s
->header_color
, GIT_COLOR_BOLD
);
43 init_color(r
, s
, "help", s
->help_color
, GIT_COLOR_BOLD_RED
);
44 init_color(r
, s
, "prompt", s
->prompt_color
, GIT_COLOR_BOLD_BLUE
);
45 init_color(r
, s
, "error", s
->error_color
, GIT_COLOR_BOLD_RED
);
46 init_color(r
, s
, "reset", s
->reset_color
, GIT_COLOR_RESET
);
47 init_color(r
, s
, "fraginfo", s
->fraginfo_color
,
48 diff_get_color(s
->use_color
, DIFF_FRAGINFO
));
49 init_color(r
, s
, "context", s
->context_color
,
50 diff_get_color(s
->use_color
, DIFF_CONTEXT
));
51 init_color(r
, s
, "old", s
->file_old_color
,
52 diff_get_color(s
->use_color
, DIFF_FILE_OLD
));
53 init_color(r
, s
, "new", s
->file_new_color
,
54 diff_get_color(s
->use_color
, DIFF_FILE_NEW
));
58 * A "prefix item list" is a list of items that are identified by a string, and
59 * a unique prefix (if any) is determined for each item.
61 * It is implemented in the form of a pair of `string_list`s, the first one
62 * duplicating the strings, with the `util` field pointing at a structure whose
63 * first field must be `size_t prefix_length`.
65 * That `prefix_length` field will be computed by `find_unique_prefixes()`; It
66 * will be set to zero if no valid, unique prefix could be found.
68 * The second `string_list` is called `sorted` and does _not_ duplicate the
69 * strings but simply reuses the first one's, with the `util` field pointing at
70 * the `string_item_list` of the first `string_list`. It will be populated and
71 * sorted by `find_unique_prefixes()`.
73 struct prefix_item_list
{
74 struct string_list items
;
75 struct string_list sorted
;
76 int *selected
; /* for multi-selections */
77 size_t min_length
, max_length
;
79 #define PREFIX_ITEM_LIST_INIT \
80 { STRING_LIST_INIT_DUP, STRING_LIST_INIT_NODUP, NULL, 1, 4 }
82 static void prefix_item_list_clear(struct prefix_item_list
*list
)
84 string_list_clear(&list
->items
, 1);
85 string_list_clear(&list
->sorted
, 0);
86 FREE_AND_NULL(list
->selected
);
89 static void extend_prefix_length(struct string_list_item
*p
,
90 const char *other_string
, size_t max_length
)
92 size_t *len
= p
->util
;
94 if (!*len
|| memcmp(p
->string
, other_string
, *len
))
98 char c
= p
->string
[*len
];
101 * Is `p` a strict prefix of `other`? Or have we exhausted the
102 * maximal length of the prefix? Or is the current character a
103 * multi-byte UTF-8 one? If so, there is no valid, unique
106 if (!c
|| ++*len
> max_length
|| !isascii(c
)) {
111 if (c
!= other_string
[*len
- 1])
116 static void find_unique_prefixes(struct prefix_item_list
*list
)
120 if (list
->sorted
.nr
== list
->items
.nr
)
123 string_list_clear(&list
->sorted
, 0);
124 /* Avoid reallocating incrementally */
125 list
->sorted
.items
= xmalloc(st_mult(sizeof(*list
->sorted
.items
),
127 list
->sorted
.nr
= list
->sorted
.alloc
= list
->items
.nr
;
129 for (i
= 0; i
< list
->items
.nr
; i
++) {
130 list
->sorted
.items
[i
].string
= list
->items
.items
[i
].string
;
131 list
->sorted
.items
[i
].util
= list
->items
.items
+ i
;
134 string_list_sort(&list
->sorted
);
136 for (i
= 0; i
< list
->sorted
.nr
; i
++) {
137 struct string_list_item
*sorted_item
= list
->sorted
.items
+ i
;
138 struct string_list_item
*item
= sorted_item
->util
;
139 size_t *len
= item
->util
;
142 while (*len
< list
->min_length
) {
143 char c
= item
->string
[(*len
)++];
145 if (!c
|| !isascii(c
)) {
152 extend_prefix_length(item
, sorted_item
[-1].string
,
154 if (i
+ 1 < list
->sorted
.nr
)
155 extend_prefix_length(item
, sorted_item
[1].string
,
160 static ssize_t
find_unique(const char *string
, struct prefix_item_list
*list
)
162 int index
= string_list_find_insert_index(&list
->sorted
, string
, 1);
163 struct string_list_item
*item
;
165 if (list
->items
.nr
!= list
->sorted
.nr
)
166 BUG("prefix_item_list in inconsistent state (%"PRIuMAX
168 (uintmax_t)list
->items
.nr
, (uintmax_t)list
->sorted
.nr
);
171 item
= list
->sorted
.items
[-1 - index
].util
;
172 else if (index
> 0 &&
173 starts_with(list
->sorted
.items
[index
- 1].string
, string
))
175 else if (index
+ 1 < list
->sorted
.nr
&&
176 starts_with(list
->sorted
.items
[index
+ 1].string
, string
))
178 else if (index
< list
->sorted
.nr
)
179 item
= list
->sorted
.items
[index
].util
;
182 return item
- list
->items
.items
;
185 struct list_options
{
188 void (*print_item
)(int i
, int selected
, struct string_list_item
*item
,
189 void *print_item_data
);
190 void *print_item_data
;
193 static void list(struct add_i_state
*s
, struct string_list
*list
, int *selected
,
194 struct list_options
*opts
)
202 color_fprintf_ln(stdout
, s
->header_color
,
205 for (i
= 0; i
< list
->nr
; i
++) {
206 opts
->print_item(i
, selected
? selected
[i
] : 0, list
->items
+ i
,
207 opts
->print_item_data
);
209 if ((opts
->columns
) && ((i
+ 1) % (opts
->columns
))) {
222 struct list_and_choose_options
{
223 struct list_options list_opts
;
230 void (*print_help
)(struct add_i_state
*s
);
233 #define LIST_AND_CHOOSE_ERROR (-1)
234 #define LIST_AND_CHOOSE_QUIT (-2)
237 * Returns the selected index in singleton mode, the number of selected items
240 * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
241 * `LIST_AND_CHOOSE_QUIT` is returned.
243 static ssize_t
list_and_choose(struct add_i_state
*s
,
244 struct prefix_item_list
*items
,
245 struct list_and_choose_options
*opts
)
247 int singleton
= opts
->flags
& SINGLETON
;
248 int immediate
= opts
->flags
& IMMEDIATE
;
250 struct strbuf input
= STRBUF_INIT
;
251 ssize_t res
= singleton
? LIST_AND_CHOOSE_ERROR
: 0;
254 free(items
->selected
);
255 CALLOC_ARRAY(items
->selected
, items
->items
.nr
);
258 if (singleton
&& !immediate
)
259 BUG("singleton requires immediate");
261 find_unique_prefixes(items
);
266 strbuf_reset(&input
);
268 list(s
, &items
->items
, items
->selected
, &opts
->list_opts
);
270 color_fprintf(stdout
, s
->prompt_color
, "%s", opts
->prompt
);
271 fputs(singleton
? "> " : ">> ", stdout
);
274 if (strbuf_getline(&input
, stdin
) == EOF
) {
277 res
= LIST_AND_CHOOSE_QUIT
;
285 if (!strcmp(input
.buf
, "?")) {
292 size_t sep
= strcspn(p
, " \t\r\n,");
294 /* `from` is inclusive, `to` is exclusive */
295 ssize_t from
= -1, to
= -1;
304 /* Input that begins with '-'; de-select */
311 if (sep
== 1 && *p
== '*') {
313 to
= items
->items
.nr
;
314 } else if (isdigit(*p
)) {
317 * A range can be specified like 5-7 or 5-.
319 * Note: `from` is 0-based while the user input
320 * is 1-based, hence we have to decrement by
321 * one. We do not have to decrement `to` even
322 * if it is 0-based because it is an exclusive
325 from
= strtoul(p
, &endp
, 10) - 1;
328 else if (*endp
== '-') {
329 to
= strtoul(++endp
, &endp
, 10);
330 /* extra characters after the range? */
339 from
= find_unique(p
, items
);
344 if (from
< 0 || from
>= items
->items
.nr
||
345 (singleton
&& from
+ 1 != to
)) {
346 color_fprintf_ln(stdout
, s
->error_color
,
349 } else if (singleton
) {
354 if (to
> items
->items
.nr
)
355 to
= items
->items
.nr
;
357 for (; from
< to
; from
++)
358 if (items
->selected
[from
] != choose
) {
359 items
->selected
[from
] = choose
;
360 res
+= choose
? +1 : -1;
366 if ((immediate
&& res
!= LIST_AND_CHOOSE_ERROR
) ||
367 !strcmp(input
.buf
, "*"))
371 strbuf_release(&input
);
377 unsigned seen
:1, unmerged
:1, binary
:1;
381 size_t prefix_length
;
382 struct adddel index
, worktree
;
385 static void add_file_item(struct string_list
*files
, const char *name
)
387 struct file_item
*item
= xcalloc(sizeof(*item
), 1);
389 string_list_append(files
, name
)->util
= item
;
392 struct pathname_entry
{
393 struct hashmap_entry ent
;
395 struct file_item
*item
;
398 static int pathname_entry_cmp(const void *unused_cmp_data
,
399 const struct hashmap_entry
*he1
,
400 const struct hashmap_entry
*he2
,
403 const struct pathname_entry
*e1
=
404 container_of(he1
, const struct pathname_entry
, ent
);
405 const struct pathname_entry
*e2
=
406 container_of(he2
, const struct pathname_entry
, ent
);
408 return strcmp(e1
->name
, name
? (const char *)name
: e2
->name
);
411 struct collection_status
{
412 enum { FROM_WORKTREE
= 0, FROM_INDEX
= 1 } mode
;
414 const char *reference
;
416 unsigned skip_unseen
:1;
417 size_t unmerged_count
, binary_count
;
418 struct string_list
*files
;
419 struct hashmap file_map
;
422 static void collect_changes_cb(struct diff_queue_struct
*q
,
423 struct diff_options
*options
,
426 struct collection_status
*s
= data
;
427 struct diffstat_t stat
= { 0 };
433 compute_diffstat(options
, &stat
, q
);
435 for (i
= 0; i
< stat
.nr
; i
++) {
436 const char *name
= stat
.files
[i
]->name
;
437 int hash
= strhash(name
);
438 struct pathname_entry
*entry
;
439 struct file_item
*file_item
;
440 struct adddel
*adddel
, *other_adddel
;
442 entry
= hashmap_get_entry_from_hash(&s
->file_map
, hash
, name
,
443 struct pathname_entry
, ent
);
448 add_file_item(s
->files
, name
);
450 entry
= xcalloc(sizeof(*entry
), 1);
451 hashmap_entry_init(&entry
->ent
, hash
);
452 entry
->name
= s
->files
->items
[s
->files
->nr
- 1].string
;
453 entry
->item
= s
->files
->items
[s
->files
->nr
- 1].util
;
454 hashmap_add(&s
->file_map
, &entry
->ent
);
457 file_item
= entry
->item
;
458 adddel
= s
->mode
== FROM_INDEX
?
459 &file_item
->index
: &file_item
->worktree
;
460 other_adddel
= s
->mode
== FROM_INDEX
?
461 &file_item
->worktree
: &file_item
->index
;
463 adddel
->add
= stat
.files
[i
]->added
;
464 adddel
->del
= stat
.files
[i
]->deleted
;
465 if (stat
.files
[i
]->is_binary
) {
466 if (!other_adddel
->binary
)
470 if (stat
.files
[i
]->is_unmerged
) {
471 if (!other_adddel
->unmerged
)
473 adddel
->unmerged
= 1;
476 free_diffstat_info(&stat
);
479 enum modified_files_filter
{
485 static int get_modified_files(struct repository
*r
,
486 enum modified_files_filter filter
,
487 struct prefix_item_list
*files
,
488 const struct pathspec
*ps
,
489 size_t *unmerged_count
,
490 size_t *binary_count
)
492 struct object_id head_oid
;
493 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
495 struct collection_status s
= { 0 };
498 if (discard_index(r
->index
) < 0 ||
499 repo_read_index_preload(r
, ps
, 0) < 0)
500 return error(_("could not read index"));
502 prefix_item_list_clear(files
);
503 s
.files
= &files
->items
;
504 hashmap_init(&s
.file_map
, pathname_entry_cmp
, NULL
, 0);
506 for (i
= 0; i
< 2; i
++) {
508 struct setup_revision_opt opt
= { 0 };
510 if (filter
== INDEX_ONLY
)
511 s
.mode
= (i
== 0) ? FROM_INDEX
: FROM_WORKTREE
;
513 s
.mode
= (i
== 0) ? FROM_WORKTREE
: FROM_INDEX
;
514 s
.skip_unseen
= filter
&& i
;
516 opt
.def
= is_initial
?
517 empty_tree_oid_hex() : oid_to_hex(&head_oid
);
519 init_revisions(&rev
, NULL
);
520 setup_revisions(0, NULL
, &rev
, &opt
);
522 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
523 rev
.diffopt
.format_callback
= collect_changes_cb
;
524 rev
.diffopt
.format_callback_data
= &s
;
527 copy_pathspec(&rev
.prune_data
, ps
);
529 if (s
.mode
== FROM_INDEX
)
530 run_diff_index(&rev
, 1);
532 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
533 run_diff_files(&rev
, 0);
537 clear_pathspec(&rev
.prune_data
);
539 hashmap_free_entries(&s
.file_map
, struct pathname_entry
, ent
);
541 *unmerged_count
= s
.unmerged_count
;
543 *binary_count
= s
.binary_count
;
545 /* While the diffs are ordered already, we ran *two* diffs... */
546 string_list_sort(&files
->items
);
551 static void render_adddel(struct strbuf
*buf
,
552 struct adddel
*ad
, const char *no_changes
)
555 strbuf_addstr(buf
, _("binary"));
557 strbuf_addf(buf
, "+%"PRIuMAX
"/-%"PRIuMAX
,
558 (uintmax_t)ad
->add
, (uintmax_t)ad
->del
);
560 strbuf_addstr(buf
, no_changes
);
563 /* filters out prefixes which have special meaning to list_and_choose() */
564 static int is_valid_prefix(const char *prefix
, size_t prefix_len
)
566 return prefix_len
&& prefix
&&
568 * We expect `prefix` to be NUL terminated, therefore this
569 * `strcspn()` call is okay, even if it might do much more
570 * work than strictly necessary.
572 strcspn(prefix
, " \t\r\n,") >= prefix_len
&& /* separators */
573 *prefix
!= '-' && /* deselection */
574 !isdigit(*prefix
) && /* selection */
576 (*prefix
!= '*' && /* "all" wildcard */
577 *prefix
!= '?')); /* prompt help */
580 struct print_file_item_data
{
581 const char *modified_fmt
, *color
, *reset
;
582 struct strbuf buf
, name
, index
, worktree
;
583 unsigned only_names
:1;
586 static void print_file_item(int i
, int selected
, struct string_list_item
*item
,
587 void *print_file_item_data
)
589 struct file_item
*c
= item
->util
;
590 struct print_file_item_data
*d
= print_file_item_data
;
591 const char *highlighted
= NULL
;
593 strbuf_reset(&d
->index
);
594 strbuf_reset(&d
->worktree
);
595 strbuf_reset(&d
->buf
);
597 /* Format the item with the prefix highlighted. */
598 if (c
->prefix_length
> 0 &&
599 is_valid_prefix(item
->string
, c
->prefix_length
)) {
600 strbuf_reset(&d
->name
);
601 strbuf_addf(&d
->name
, "%s%.*s%s%s", d
->color
,
602 (int)c
->prefix_length
, item
->string
, d
->reset
,
603 item
->string
+ c
->prefix_length
);
604 highlighted
= d
->name
.buf
;
608 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1,
609 highlighted
? highlighted
: item
->string
);
613 render_adddel(&d
->worktree
, &c
->worktree
, _("nothing"));
614 render_adddel(&d
->index
, &c
->index
, _("unchanged"));
616 strbuf_addf(&d
->buf
, d
->modified_fmt
, d
->index
.buf
, d
->worktree
.buf
,
617 highlighted
? highlighted
: item
->string
);
619 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1, d
->buf
.buf
);
622 static int run_status(struct add_i_state
*s
, const struct pathspec
*ps
,
623 struct prefix_item_list
*files
,
624 struct list_and_choose_options
*opts
)
626 if (get_modified_files(s
->r
, NO_FILTER
, files
, ps
, NULL
, NULL
) < 0)
629 list(s
, &files
->items
, NULL
, &opts
->list_opts
);
635 static int run_update(struct add_i_state
*s
, const struct pathspec
*ps
,
636 struct prefix_item_list
*files
,
637 struct list_and_choose_options
*opts
)
641 struct lock_file index_lock
;
643 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
, NULL
, NULL
) < 0)
646 if (!files
->items
.nr
) {
651 opts
->prompt
= N_("Update");
652 count
= list_and_choose(s
, files
, opts
);
658 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
664 for (i
= 0; i
< files
->items
.nr
; i
++) {
665 const char *name
= files
->items
.items
[i
].string
;
666 if (files
->selected
[i
] &&
667 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
668 res
= error(_("could not stage '%s'"), name
);
673 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
674 res
= error(_("could not write index"));
677 printf(Q_("updated %d path\n",
678 "updated %d paths\n", count
), (int)count
);
684 static void revert_from_diff(struct diff_queue_struct
*q
,
685 struct diff_options
*opt
, void *data
)
687 int i
, add_flags
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
689 for (i
= 0; i
< q
->nr
; i
++) {
690 struct diff_filespec
*one
= q
->queue
[i
]->one
;
691 struct cache_entry
*ce
;
693 if (!(one
->mode
&& !is_null_oid(&one
->oid
))) {
694 remove_file_from_index(opt
->repo
->index
, one
->path
);
695 printf(_("note: %s is untracked now.\n"), one
->path
);
697 ce
= make_cache_entry(opt
->repo
->index
, one
->mode
,
698 &one
->oid
, one
->path
, 0, 0);
700 die(_("make_cache_entry failed for path '%s'"),
702 add_index_entry(opt
->repo
->index
, ce
, add_flags
);
707 static int run_revert(struct add_i_state
*s
, const struct pathspec
*ps
,
708 struct prefix_item_list
*files
,
709 struct list_and_choose_options
*opts
)
714 struct object_id oid
;
715 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
717 struct lock_file index_lock
;
720 struct diff_options diffopt
= { NULL
};
722 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
725 if (!files
->items
.nr
) {
730 opts
->prompt
= N_("Revert");
731 count
= list_and_choose(s
, files
, opts
);
735 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
742 oidcpy(&oid
, s
->r
->hash_algo
->empty_tree
);
744 tree
= parse_tree_indirect(&oid
);
746 res
= error(_("Could not parse HEAD^{tree}"));
749 oidcpy(&oid
, &tree
->object
.oid
);
752 ALLOC_ARRAY(paths
, count
+ 1);
753 for (i
= j
= 0; i
< files
->items
.nr
; i
++)
754 if (files
->selected
[i
])
755 paths
[j
++] = files
->items
.items
[i
].string
;
758 parse_pathspec(&diffopt
.pathspec
, 0,
759 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
,
762 diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
763 diffopt
.format_callback
= revert_from_diff
;
764 diffopt
.flags
.override_submodule_config
= 1;
767 if (do_diff_cache(&oid
, &diffopt
))
770 diffcore_std(&diffopt
);
771 diff_flush(&diffopt
);
774 clear_pathspec(&diffopt
.pathspec
);
776 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
,
780 res
= repo_refresh_and_write_index(s
->r
, REFRESH_QUIET
, 0, 1,
784 printf(Q_("reverted %d path\n",
785 "reverted %d paths\n", count
), (int)count
);
792 static int get_untracked_files(struct repository
*r
,
793 struct prefix_item_list
*files
,
794 const struct pathspec
*ps
)
796 struct dir_struct dir
= { 0 };
798 struct strbuf buf
= STRBUF_INIT
;
800 if (repo_read_index(r
) < 0)
801 return error(_("could not read index"));
803 prefix_item_list_clear(files
);
804 setup_standard_excludes(&dir
);
805 add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
806 fill_directory(&dir
, r
->index
, ps
);
808 for (i
= 0; i
< dir
.nr
; i
++) {
809 struct dir_entry
*ent
= dir
.entries
[i
];
811 if (index_name_is_other(r
->index
, ent
->name
, ent
->len
)) {
813 strbuf_add(&buf
, ent
->name
, ent
->len
);
814 add_file_item(&files
->items
, buf
.buf
);
818 strbuf_release(&buf
);
822 static int run_add_untracked(struct add_i_state
*s
, const struct pathspec
*ps
,
823 struct prefix_item_list
*files
,
824 struct list_and_choose_options
*opts
)
826 struct print_file_item_data
*d
= opts
->list_opts
.print_item_data
;
829 struct lock_file index_lock
;
831 if (get_untracked_files(s
->r
, files
, ps
) < 0)
834 if (!files
->items
.nr
) {
835 printf(_("No untracked files.\n"));
836 goto finish_add_untracked
;
839 opts
->prompt
= N_("Add untracked");
841 count
= list_and_choose(s
, files
, opts
);
844 goto finish_add_untracked
;
846 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
849 goto finish_add_untracked
;
852 for (i
= 0; i
< files
->items
.nr
; i
++) {
853 const char *name
= files
->items
.items
[i
].string
;
854 if (files
->selected
[i
] &&
855 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
856 res
= error(_("could not stage '%s'"), name
);
862 write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
863 res
= error(_("could not write index"));
866 printf(Q_("added %d path\n",
867 "added %d paths\n", count
), (int)count
);
869 finish_add_untracked
:
874 static int run_patch(struct add_i_state
*s
, const struct pathspec
*ps
,
875 struct prefix_item_list
*files
,
876 struct list_and_choose_options
*opts
)
880 size_t unmerged_count
= 0, binary_count
= 0;
882 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
,
883 &unmerged_count
, &binary_count
) < 0)
886 if (unmerged_count
|| binary_count
) {
887 for (i
= j
= 0; i
< files
->items
.nr
; i
++) {
888 struct file_item
*item
= files
->items
.items
[i
].util
;
890 if (item
->index
.binary
|| item
->worktree
.binary
) {
892 free(files
->items
.items
[i
].string
);
893 } else if (item
->index
.unmerged
||
894 item
->worktree
.unmerged
) {
895 color_fprintf_ln(stderr
, s
->error_color
,
896 _("ignoring unmerged: %s"),
897 files
->items
.items
[i
].string
);
899 free(files
->items
.items
[i
].string
);
901 files
->items
.items
[j
++] = files
->items
.items
[i
];
906 if (!files
->items
.nr
) {
908 fprintf(stderr
, _("Only binary files changed.\n"));
910 fprintf(stderr
, _("No changes.\n"));
914 opts
->prompt
= N_("Patch update");
915 count
= list_and_choose(s
, files
, opts
);
917 struct argv_array args
= ARGV_ARRAY_INIT
;
918 struct pathspec ps_selected
= { 0 };
920 for (i
= 0; i
< files
->items
.nr
; i
++)
921 if (files
->selected
[i
])
922 argv_array_push(&args
,
923 files
->items
.items
[i
].string
);
924 parse_pathspec(&ps_selected
,
925 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
926 PATHSPEC_LITERAL_PATH
, "", args
.argv
);
927 res
= run_add_p(s
->r
, &ps_selected
);
928 argv_array_clear(&args
);
929 clear_pathspec(&ps_selected
);
935 static int run_diff(struct add_i_state
*s
, const struct pathspec
*ps
,
936 struct prefix_item_list
*files
,
937 struct list_and_choose_options
*opts
)
942 struct object_id oid
;
943 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
945 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
948 if (!files
->items
.nr
) {
953 opts
->prompt
= N_("Review diff");
954 opts
->flags
= IMMEDIATE
;
955 count
= list_and_choose(s
, files
, opts
);
958 struct argv_array args
= ARGV_ARRAY_INIT
;
960 argv_array_pushl(&args
, "git", "diff", "-p", "--cached",
961 oid_to_hex(!is_initial
? &oid
:
962 s
->r
->hash_algo
->empty_tree
),
964 for (i
= 0; i
< files
->items
.nr
; i
++)
965 if (files
->selected
[i
])
966 argv_array_push(&args
,
967 files
->items
.items
[i
].string
);
968 res
= run_command_v_opt(args
.argv
, 0);
969 argv_array_clear(&args
);
976 static int run_help(struct add_i_state
*s
, const struct pathspec
*unused_ps
,
977 struct prefix_item_list
*unused_files
,
978 struct list_and_choose_options
*unused_opts
)
980 color_fprintf_ln(stdout
, s
->help_color
, "status - %s",
981 _("show paths with changes"));
982 color_fprintf_ln(stdout
, s
->help_color
, "update - %s",
983 _("add working tree state to the staged set of changes"));
984 color_fprintf_ln(stdout
, s
->help_color
, "revert - %s",
985 _("revert staged set of changes back to the HEAD version"));
986 color_fprintf_ln(stdout
, s
->help_color
, "patch - %s",
987 _("pick hunks and update selectively"));
988 color_fprintf_ln(stdout
, s
->help_color
, "diff - %s",
989 _("view diff between HEAD and index"));
990 color_fprintf_ln(stdout
, s
->help_color
, "add untracked - %s",
991 _("add contents of untracked files to the staged set of changes"));
996 static void choose_prompt_help(struct add_i_state
*s
)
998 color_fprintf_ln(stdout
, s
->help_color
, "%s",
1000 color_fprintf_ln(stdout
, s
->help_color
, "1 - %s",
1001 _("select a single item"));
1002 color_fprintf_ln(stdout
, s
->help_color
, "3-5 - %s",
1003 _("select a range of items"));
1004 color_fprintf_ln(stdout
, s
->help_color
, "2-3,6-9 - %s",
1005 _("select multiple ranges"));
1006 color_fprintf_ln(stdout
, s
->help_color
, "foo - %s",
1007 _("select item based on unique prefix"));
1008 color_fprintf_ln(stdout
, s
->help_color
, "-... - %s",
1009 _("unselect specified items"));
1010 color_fprintf_ln(stdout
, s
->help_color
, "* - %s",
1011 _("choose all items"));
1012 color_fprintf_ln(stdout
, s
->help_color
, " - %s",
1013 _("(empty) finish selecting"));
1016 typedef int (*command_t
)(struct add_i_state
*s
, const struct pathspec
*ps
,
1017 struct prefix_item_list
*files
,
1018 struct list_and_choose_options
*opts
);
1020 struct command_item
{
1021 size_t prefix_length
;
1025 struct print_command_item_data
{
1026 const char *color
, *reset
;
1029 static void print_command_item(int i
, int selected
,
1030 struct string_list_item
*item
,
1031 void *print_command_item_data
)
1033 struct print_command_item_data
*d
= print_command_item_data
;
1034 struct command_item
*util
= item
->util
;
1036 if (!util
->prefix_length
||
1037 !is_valid_prefix(item
->string
, util
->prefix_length
))
1038 printf(" %2d: %s", i
+ 1, item
->string
);
1040 printf(" %2d: %s%.*s%s%s", i
+ 1,
1041 d
->color
, (int)util
->prefix_length
, item
->string
,
1042 d
->reset
, item
->string
+ util
->prefix_length
);
1045 static void command_prompt_help(struct add_i_state
*s
)
1047 const char *help_color
= s
->help_color
;
1048 color_fprintf_ln(stdout
, help_color
, "%s", _("Prompt help:"));
1049 color_fprintf_ln(stdout
, help_color
, "1 - %s",
1050 _("select a numbered item"));
1051 color_fprintf_ln(stdout
, help_color
, "foo - %s",
1052 _("select item based on unique prefix"));
1053 color_fprintf_ln(stdout
, help_color
, " - %s",
1054 _("(empty) select nothing"));
1057 int run_add_i(struct repository
*r
, const struct pathspec
*ps
)
1059 struct add_i_state s
= { NULL
};
1060 struct print_command_item_data data
= { "[", "]" };
1061 struct list_and_choose_options main_loop_opts
= {
1062 { 4, N_("*** Commands ***"), print_command_item
, &data
},
1063 N_("What now"), SINGLETON
| IMMEDIATE
, command_prompt_help
1068 } command_list
[] = {
1069 { "status", run_status
},
1070 { "update", run_update
},
1071 { "revert", run_revert
},
1072 { "add untracked", run_add_untracked
},
1073 { "patch", run_patch
},
1074 { "diff", run_diff
},
1076 { "help", run_help
},
1078 struct prefix_item_list commands
= PREFIX_ITEM_LIST_INIT
;
1080 struct print_file_item_data print_file_item_data
= {
1081 "%12s %12s %s", NULL
, NULL
,
1082 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1084 struct list_and_choose_options opts
= {
1085 { 0, NULL
, print_file_item
, &print_file_item_data
},
1086 NULL
, 0, choose_prompt_help
1088 struct strbuf header
= STRBUF_INIT
;
1089 struct prefix_item_list files
= PREFIX_ITEM_LIST_INIT
;
1093 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
1094 struct command_item
*util
= xcalloc(sizeof(*util
), 1);
1095 util
->command
= command_list
[i
].command
;
1096 string_list_append(&commands
.items
, command_list
[i
].string
)
1100 init_add_i_state(&s
, r
);
1103 * When color was asked for, use the prompt color for
1104 * highlighting, otherwise use square brackets.
1107 data
.color
= s
.prompt_color
;
1108 data
.reset
= s
.reset_color
;
1110 print_file_item_data
.color
= data
.color
;
1111 print_file_item_data
.reset
= data
.reset
;
1113 strbuf_addstr(&header
, " ");
1114 strbuf_addf(&header
, print_file_item_data
.modified_fmt
,
1115 _("staged"), _("unstaged"), _("path"));
1116 opts
.list_opts
.header
= header
.buf
;
1118 if (discard_index(r
->index
) < 0 ||
1119 repo_read_index(r
) < 0 ||
1120 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1121 NULL
, NULL
, NULL
) < 0)
1122 warning(_("could not refresh index"));
1124 res
= run_status(&s
, ps
, &files
, &opts
);
1127 struct command_item
*util
;
1129 i
= list_and_choose(&s
, &commands
, &main_loop_opts
);
1130 if (i
< 0 || i
>= commands
.items
.nr
)
1133 util
= commands
.items
.items
[i
].util
;
1135 if (i
== LIST_AND_CHOOSE_QUIT
|| (util
&& !util
->command
)) {
1136 printf(_("Bye.\n"));
1142 res
= util
->command(&s
, ps
, &files
, &opts
);
1145 prefix_item_list_clear(&files
);
1146 strbuf_release(&print_file_item_data
.buf
);
1147 strbuf_release(&print_file_item_data
.name
);
1148 strbuf_release(&print_file_item_data
.index
);
1149 strbuf_release(&print_file_item_data
.worktree
);
1150 strbuf_release(&header
);
1151 prefix_item_list_clear(&commands
);