2 #include "add-interactive.h"
8 #include "string-list.h"
11 #include "run-command.h"
14 static void init_color(struct repository
*r
, struct add_i_state
*s
,
15 const char *section_and_slot
, char *dst
,
16 const char *default_color
)
18 char *key
= xstrfmt("color.%s", section_and_slot
);
23 else if (repo_config_get_value(r
, key
, &value
) ||
24 color_parse(value
, dst
))
25 strlcpy(dst
, default_color
, COLOR_MAXLEN
);
30 void init_add_i_state(struct add_i_state
*s
, struct repository
*r
)
36 if (repo_config_get_value(r
, "color.interactive", &value
))
40 git_config_colorbool("color.interactive", value
);
41 s
->use_color
= want_color(s
->use_color
);
43 init_color(r
, s
, "interactive.header", s
->header_color
, GIT_COLOR_BOLD
);
44 init_color(r
, s
, "interactive.help", s
->help_color
, GIT_COLOR_BOLD_RED
);
45 init_color(r
, s
, "interactive.prompt", s
->prompt_color
,
47 init_color(r
, s
, "interactive.error", s
->error_color
,
50 init_color(r
, s
, "diff.frag", s
->fraginfo_color
,
51 diff_get_color(s
->use_color
, DIFF_FRAGINFO
));
52 init_color(r
, s
, "diff.context", s
->context_color
, "fall back");
53 if (!strcmp(s
->context_color
, "fall back"))
54 init_color(r
, s
, "diff.plain", s
->context_color
,
55 diff_get_color(s
->use_color
, DIFF_CONTEXT
));
56 init_color(r
, s
, "diff.old", s
->file_old_color
,
57 diff_get_color(s
->use_color
, DIFF_FILE_OLD
));
58 init_color(r
, s
, "diff.new", s
->file_new_color
,
59 diff_get_color(s
->use_color
, DIFF_FILE_NEW
));
61 strlcpy(s
->reset_color
,
62 s
->use_color
? GIT_COLOR_RESET
: "", COLOR_MAXLEN
);
64 FREE_AND_NULL(s
->interactive_diff_filter
);
65 git_config_get_string("interactive.difffilter",
66 &s
->interactive_diff_filter
);
68 FREE_AND_NULL(s
->interactive_diff_algorithm
);
69 git_config_get_string("diff.algorithm",
70 &s
->interactive_diff_algorithm
);
72 git_config_get_bool("interactive.singlekey", &s
->use_single_key
);
75 void clear_add_i_state(struct add_i_state
*s
)
77 FREE_AND_NULL(s
->interactive_diff_filter
);
78 FREE_AND_NULL(s
->interactive_diff_algorithm
);
79 memset(s
, 0, sizeof(*s
));
84 * A "prefix item list" is a list of items that are identified by a string, and
85 * a unique prefix (if any) is determined for each item.
87 * It is implemented in the form of a pair of `string_list`s, the first one
88 * duplicating the strings, with the `util` field pointing at a structure whose
89 * first field must be `size_t prefix_length`.
91 * That `prefix_length` field will be computed by `find_unique_prefixes()`; It
92 * will be set to zero if no valid, unique prefix could be found.
94 * The second `string_list` is called `sorted` and does _not_ duplicate the
95 * strings but simply reuses the first one's, with the `util` field pointing at
96 * the `string_item_list` of the first `string_list`. It will be populated and
97 * sorted by `find_unique_prefixes()`.
99 struct prefix_item_list
{
100 struct string_list items
;
101 struct string_list sorted
;
102 int *selected
; /* for multi-selections */
103 size_t min_length
, max_length
;
105 #define PREFIX_ITEM_LIST_INIT { \
106 .items = STRING_LIST_INIT_DUP, \
107 .sorted = STRING_LIST_INIT_NODUP, \
112 static void prefix_item_list_clear(struct prefix_item_list
*list
)
114 string_list_clear(&list
->items
, 1);
115 string_list_clear(&list
->sorted
, 0);
116 FREE_AND_NULL(list
->selected
);
119 static void extend_prefix_length(struct string_list_item
*p
,
120 const char *other_string
, size_t max_length
)
122 size_t *len
= p
->util
;
124 if (!*len
|| memcmp(p
->string
, other_string
, *len
))
128 char c
= p
->string
[*len
];
131 * Is `p` a strict prefix of `other`? Or have we exhausted the
132 * maximal length of the prefix? Or is the current character a
133 * multi-byte UTF-8 one? If so, there is no valid, unique
136 if (!c
|| ++*len
> max_length
|| !isascii(c
)) {
141 if (c
!= other_string
[*len
- 1])
146 static void find_unique_prefixes(struct prefix_item_list
*list
)
150 if (list
->sorted
.nr
== list
->items
.nr
)
153 string_list_clear(&list
->sorted
, 0);
154 /* Avoid reallocating incrementally */
155 list
->sorted
.items
= xmalloc(st_mult(sizeof(*list
->sorted
.items
),
157 list
->sorted
.nr
= list
->sorted
.alloc
= list
->items
.nr
;
159 for (i
= 0; i
< list
->items
.nr
; i
++) {
160 list
->sorted
.items
[i
].string
= list
->items
.items
[i
].string
;
161 list
->sorted
.items
[i
].util
= list
->items
.items
+ i
;
164 string_list_sort(&list
->sorted
);
166 for (i
= 0; i
< list
->sorted
.nr
; i
++) {
167 struct string_list_item
*sorted_item
= list
->sorted
.items
+ i
;
168 struct string_list_item
*item
= sorted_item
->util
;
169 size_t *len
= item
->util
;
172 while (*len
< list
->min_length
) {
173 char c
= item
->string
[(*len
)++];
175 if (!c
|| !isascii(c
)) {
182 extend_prefix_length(item
, sorted_item
[-1].string
,
184 if (i
+ 1 < list
->sorted
.nr
)
185 extend_prefix_length(item
, sorted_item
[1].string
,
190 static ssize_t
find_unique(const char *string
, struct prefix_item_list
*list
)
192 int index
= string_list_find_insert_index(&list
->sorted
, string
, 1);
193 struct string_list_item
*item
;
195 if (list
->items
.nr
!= list
->sorted
.nr
)
196 BUG("prefix_item_list in inconsistent state (%"PRIuMAX
198 (uintmax_t)list
->items
.nr
, (uintmax_t)list
->sorted
.nr
);
201 item
= list
->sorted
.items
[-1 - index
].util
;
202 else if (index
> 0 &&
203 starts_with(list
->sorted
.items
[index
- 1].string
, string
))
205 else if (index
+ 1 < list
->sorted
.nr
&&
206 starts_with(list
->sorted
.items
[index
+ 1].string
, string
))
208 else if (index
< list
->sorted
.nr
&&
209 starts_with(list
->sorted
.items
[index
].string
, string
))
210 item
= list
->sorted
.items
[index
].util
;
213 return item
- list
->items
.items
;
216 struct list_options
{
219 void (*print_item
)(int i
, int selected
, struct string_list_item
*item
,
220 void *print_item_data
);
221 void *print_item_data
;
224 static void list(struct add_i_state
*s
, struct string_list
*list
, int *selected
,
225 struct list_options
*opts
)
233 color_fprintf_ln(stdout
, s
->header_color
,
236 for (i
= 0; i
< list
->nr
; i
++) {
237 opts
->print_item(i
, selected
? selected
[i
] : 0, list
->items
+ i
,
238 opts
->print_item_data
);
240 if ((opts
->columns
) && ((i
+ 1) % (opts
->columns
))) {
253 struct list_and_choose_options
{
254 struct list_options list_opts
;
261 void (*print_help
)(struct add_i_state
*s
);
264 #define LIST_AND_CHOOSE_ERROR (-1)
265 #define LIST_AND_CHOOSE_QUIT (-2)
268 * Returns the selected index in singleton mode, the number of selected items
271 * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
272 * `LIST_AND_CHOOSE_QUIT` is returned.
274 static ssize_t
list_and_choose(struct add_i_state
*s
,
275 struct prefix_item_list
*items
,
276 struct list_and_choose_options
*opts
)
278 int singleton
= opts
->flags
& SINGLETON
;
279 int immediate
= opts
->flags
& IMMEDIATE
;
281 struct strbuf input
= STRBUF_INIT
;
282 ssize_t res
= singleton
? LIST_AND_CHOOSE_ERROR
: 0;
285 free(items
->selected
);
286 CALLOC_ARRAY(items
->selected
, items
->items
.nr
);
289 if (singleton
&& !immediate
)
290 BUG("singleton requires immediate");
292 find_unique_prefixes(items
);
297 strbuf_reset(&input
);
299 list(s
, &items
->items
, items
->selected
, &opts
->list_opts
);
301 color_fprintf(stdout
, s
->prompt_color
, "%s", opts
->prompt
);
302 fputs(singleton
? "> " : ">> ", stdout
);
305 if (git_read_line_interactively(&input
) == EOF
) {
308 res
= LIST_AND_CHOOSE_QUIT
;
315 if (!strcmp(input
.buf
, "?")) {
322 size_t sep
= strcspn(p
, " \t\r\n,");
324 /* `from` is inclusive, `to` is exclusive */
325 ssize_t from
= -1, to
= -1;
334 /* Input that begins with '-'; de-select */
341 if (sep
== 1 && *p
== '*') {
343 to
= items
->items
.nr
;
344 } else if (isdigit(*p
)) {
347 * A range can be specified like 5-7 or 5-.
349 * Note: `from` is 0-based while the user input
350 * is 1-based, hence we have to decrement by
351 * one. We do not have to decrement `to` even
352 * if it is 0-based because it is an exclusive
355 from
= strtoul(p
, &endp
, 10) - 1;
358 else if (*endp
== '-') {
359 if (isdigit(*(++endp
)))
360 to
= strtoul(endp
, &endp
, 10);
362 to
= items
->items
.nr
;
363 /* extra characters after the range? */
372 from
= find_unique(p
, items
);
377 if (from
< 0 || from
>= items
->items
.nr
||
378 (singleton
&& from
+ 1 != to
)) {
379 color_fprintf_ln(stderr
, s
->error_color
,
382 } else if (singleton
) {
387 if (to
> items
->items
.nr
)
388 to
= items
->items
.nr
;
390 for (; from
< to
; from
++)
391 if (items
->selected
[from
] != choose
) {
392 items
->selected
[from
] = choose
;
393 res
+= choose
? +1 : -1;
399 if ((immediate
&& res
!= LIST_AND_CHOOSE_ERROR
) ||
400 !strcmp(input
.buf
, "*"))
404 strbuf_release(&input
);
410 unsigned seen
:1, unmerged
:1, binary
:1;
414 size_t prefix_length
;
415 struct adddel index
, worktree
;
418 static void add_file_item(struct string_list
*files
, const char *name
)
420 struct file_item
*item
= xcalloc(1, sizeof(*item
));
422 string_list_append(files
, name
)->util
= item
;
425 struct pathname_entry
{
426 struct hashmap_entry ent
;
428 struct file_item
*item
;
431 static int pathname_entry_cmp(const void *unused_cmp_data
,
432 const struct hashmap_entry
*he1
,
433 const struct hashmap_entry
*he2
,
436 const struct pathname_entry
*e1
=
437 container_of(he1
, const struct pathname_entry
, ent
);
438 const struct pathname_entry
*e2
=
439 container_of(he2
, const struct pathname_entry
, ent
);
441 return strcmp(e1
->name
, name
? (const char *)name
: e2
->name
);
444 struct collection_status
{
445 enum { FROM_WORKTREE
= 0, FROM_INDEX
= 1 } mode
;
447 const char *reference
;
449 unsigned skip_unseen
:1;
450 size_t unmerged_count
, binary_count
;
451 struct string_list
*files
;
452 struct hashmap file_map
;
455 static void collect_changes_cb(struct diff_queue_struct
*q
,
456 struct diff_options
*options
,
459 struct collection_status
*s
= data
;
460 struct diffstat_t stat
= { 0 };
466 compute_diffstat(options
, &stat
, q
);
468 for (i
= 0; i
< stat
.nr
; i
++) {
469 const char *name
= stat
.files
[i
]->name
;
470 int hash
= strhash(name
);
471 struct pathname_entry
*entry
;
472 struct file_item
*file_item
;
473 struct adddel
*adddel
, *other_adddel
;
475 entry
= hashmap_get_entry_from_hash(&s
->file_map
, hash
, name
,
476 struct pathname_entry
, ent
);
481 add_file_item(s
->files
, name
);
483 CALLOC_ARRAY(entry
, 1);
484 hashmap_entry_init(&entry
->ent
, hash
);
485 entry
->name
= s
->files
->items
[s
->files
->nr
- 1].string
;
486 entry
->item
= s
->files
->items
[s
->files
->nr
- 1].util
;
487 hashmap_add(&s
->file_map
, &entry
->ent
);
490 file_item
= entry
->item
;
491 adddel
= s
->mode
== FROM_INDEX
?
492 &file_item
->index
: &file_item
->worktree
;
493 other_adddel
= s
->mode
== FROM_INDEX
?
494 &file_item
->worktree
: &file_item
->index
;
496 adddel
->add
= stat
.files
[i
]->added
;
497 adddel
->del
= stat
.files
[i
]->deleted
;
498 if (stat
.files
[i
]->is_binary
) {
499 if (!other_adddel
->binary
)
503 if (stat
.files
[i
]->is_unmerged
) {
504 if (!other_adddel
->unmerged
)
506 adddel
->unmerged
= 1;
509 free_diffstat_info(&stat
);
512 enum modified_files_filter
{
518 static int get_modified_files(struct repository
*r
,
519 enum modified_files_filter filter
,
520 struct prefix_item_list
*files
,
521 const struct pathspec
*ps
,
522 size_t *unmerged_count
,
523 size_t *binary_count
)
525 struct object_id head_oid
;
526 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
528 struct collection_status s
= { 0 };
531 if (discard_index(r
->index
) < 0 ||
532 repo_read_index_preload(r
, ps
, 0) < 0)
533 return error(_("could not read index"));
535 prefix_item_list_clear(files
);
536 s
.files
= &files
->items
;
537 hashmap_init(&s
.file_map
, pathname_entry_cmp
, NULL
, 0);
539 for (i
= 0; i
< 2; i
++) {
541 struct setup_revision_opt opt
= { 0 };
543 if (filter
== INDEX_ONLY
)
544 s
.mode
= (i
== 0) ? FROM_INDEX
: FROM_WORKTREE
;
546 s
.mode
= (i
== 0) ? FROM_WORKTREE
: FROM_INDEX
;
547 s
.skip_unseen
= filter
&& i
;
549 opt
.def
= is_initial
?
550 empty_tree_oid_hex() : oid_to_hex(&head_oid
);
552 init_revisions(&rev
, NULL
);
553 setup_revisions(0, NULL
, &rev
, &opt
);
555 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
556 rev
.diffopt
.format_callback
= collect_changes_cb
;
557 rev
.diffopt
.format_callback_data
= &s
;
560 copy_pathspec(&rev
.prune_data
, ps
);
562 if (s
.mode
== FROM_INDEX
)
563 run_diff_index(&rev
, 1);
565 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
566 run_diff_files(&rev
, 0);
570 clear_pathspec(&rev
.prune_data
);
572 hashmap_clear_and_free(&s
.file_map
, struct pathname_entry
, ent
);
574 *unmerged_count
= s
.unmerged_count
;
576 *binary_count
= s
.binary_count
;
578 /* While the diffs are ordered already, we ran *two* diffs... */
579 string_list_sort(&files
->items
);
584 static void render_adddel(struct strbuf
*buf
,
585 struct adddel
*ad
, const char *no_changes
)
588 strbuf_addstr(buf
, _("binary"));
590 strbuf_addf(buf
, "+%"PRIuMAX
"/-%"PRIuMAX
,
591 (uintmax_t)ad
->add
, (uintmax_t)ad
->del
);
593 strbuf_addstr(buf
, no_changes
);
596 /* filters out prefixes which have special meaning to list_and_choose() */
597 static int is_valid_prefix(const char *prefix
, size_t prefix_len
)
599 return prefix_len
&& prefix
&&
601 * We expect `prefix` to be NUL terminated, therefore this
602 * `strcspn()` call is okay, even if it might do much more
603 * work than strictly necessary.
605 strcspn(prefix
, " \t\r\n,") >= prefix_len
&& /* separators */
606 *prefix
!= '-' && /* deselection */
607 !isdigit(*prefix
) && /* selection */
609 (*prefix
!= '*' && /* "all" wildcard */
610 *prefix
!= '?')); /* prompt help */
613 struct print_file_item_data
{
614 const char *modified_fmt
, *color
, *reset
;
615 struct strbuf buf
, name
, index
, worktree
;
616 unsigned only_names
:1;
619 static void print_file_item(int i
, int selected
, struct string_list_item
*item
,
620 void *print_file_item_data
)
622 struct file_item
*c
= item
->util
;
623 struct print_file_item_data
*d
= print_file_item_data
;
624 const char *highlighted
= NULL
;
626 strbuf_reset(&d
->index
);
627 strbuf_reset(&d
->worktree
);
628 strbuf_reset(&d
->buf
);
630 /* Format the item with the prefix highlighted. */
631 if (c
->prefix_length
> 0 &&
632 is_valid_prefix(item
->string
, c
->prefix_length
)) {
633 strbuf_reset(&d
->name
);
634 strbuf_addf(&d
->name
, "%s%.*s%s%s", d
->color
,
635 (int)c
->prefix_length
, item
->string
, d
->reset
,
636 item
->string
+ c
->prefix_length
);
637 highlighted
= d
->name
.buf
;
641 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1,
642 highlighted
? highlighted
: item
->string
);
646 render_adddel(&d
->worktree
, &c
->worktree
, _("nothing"));
647 render_adddel(&d
->index
, &c
->index
, _("unchanged"));
649 strbuf_addf(&d
->buf
, d
->modified_fmt
, d
->index
.buf
, d
->worktree
.buf
,
650 highlighted
? highlighted
: item
->string
);
652 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1, d
->buf
.buf
);
655 static int run_status(struct add_i_state
*s
, const struct pathspec
*ps
,
656 struct prefix_item_list
*files
,
657 struct list_and_choose_options
*opts
)
659 if (get_modified_files(s
->r
, NO_FILTER
, files
, ps
, NULL
, NULL
) < 0)
662 list(s
, &files
->items
, NULL
, &opts
->list_opts
);
668 static int run_update(struct add_i_state
*s
, const struct pathspec
*ps
,
669 struct prefix_item_list
*files
,
670 struct list_and_choose_options
*opts
)
674 struct lock_file index_lock
;
676 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
, NULL
, NULL
) < 0)
679 if (!files
->items
.nr
) {
684 opts
->prompt
= N_("Update");
685 count
= list_and_choose(s
, files
, opts
);
691 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
697 for (i
= 0; i
< files
->items
.nr
; i
++) {
698 const char *name
= files
->items
.items
[i
].string
;
699 if (files
->selected
[i
] &&
700 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
701 res
= error(_("could not stage '%s'"), name
);
706 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
707 res
= error(_("could not write index"));
710 printf(Q_("updated %d path\n",
711 "updated %d paths\n", count
), (int)count
);
717 static void revert_from_diff(struct diff_queue_struct
*q
,
718 struct diff_options
*opt
, void *data
)
720 int i
, add_flags
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
722 for (i
= 0; i
< q
->nr
; i
++) {
723 struct diff_filespec
*one
= q
->queue
[i
]->one
;
724 struct cache_entry
*ce
;
726 if (!(one
->mode
&& !is_null_oid(&one
->oid
))) {
727 remove_file_from_index(opt
->repo
->index
, one
->path
);
728 printf(_("note: %s is untracked now.\n"), one
->path
);
730 ce
= make_cache_entry(opt
->repo
->index
, one
->mode
,
731 &one
->oid
, one
->path
, 0, 0);
733 die(_("make_cache_entry failed for path '%s'"),
735 add_index_entry(opt
->repo
->index
, ce
, add_flags
);
740 static int run_revert(struct add_i_state
*s
, const struct pathspec
*ps
,
741 struct prefix_item_list
*files
,
742 struct list_and_choose_options
*opts
)
747 struct object_id oid
;
748 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
750 struct lock_file index_lock
;
753 struct diff_options diffopt
= { NULL
};
755 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
758 if (!files
->items
.nr
) {
763 opts
->prompt
= N_("Revert");
764 count
= list_and_choose(s
, files
, opts
);
768 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
775 oidcpy(&oid
, s
->r
->hash_algo
->empty_tree
);
777 tree
= parse_tree_indirect(&oid
);
779 res
= error(_("Could not parse HEAD^{tree}"));
782 oidcpy(&oid
, &tree
->object
.oid
);
785 ALLOC_ARRAY(paths
, count
+ 1);
786 for (i
= j
= 0; i
< files
->items
.nr
; i
++)
787 if (files
->selected
[i
])
788 paths
[j
++] = files
->items
.items
[i
].string
;
791 parse_pathspec(&diffopt
.pathspec
, 0,
792 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
,
795 diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
796 diffopt
.format_callback
= revert_from_diff
;
797 diffopt
.flags
.override_submodule_config
= 1;
800 if (do_diff_cache(&oid
, &diffopt
))
803 diffcore_std(&diffopt
);
804 diff_flush(&diffopt
);
807 clear_pathspec(&diffopt
.pathspec
);
809 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
,
813 res
= repo_refresh_and_write_index(s
->r
, REFRESH_QUIET
, 0, 1,
817 printf(Q_("reverted %d path\n",
818 "reverted %d paths\n", count
), (int)count
);
825 static int get_untracked_files(struct repository
*r
,
826 struct prefix_item_list
*files
,
827 const struct pathspec
*ps
)
829 struct dir_struct dir
= { 0 };
831 struct strbuf buf
= STRBUF_INIT
;
833 if (repo_read_index(r
) < 0)
834 return error(_("could not read index"));
836 prefix_item_list_clear(files
);
837 setup_standard_excludes(&dir
);
838 add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
839 fill_directory(&dir
, r
->index
, ps
);
841 for (i
= 0; i
< dir
.nr
; i
++) {
842 struct dir_entry
*ent
= dir
.entries
[i
];
844 if (index_name_is_other(r
->index
, ent
->name
, ent
->len
)) {
846 strbuf_add(&buf
, ent
->name
, ent
->len
);
847 add_file_item(&files
->items
, buf
.buf
);
851 strbuf_release(&buf
);
855 static int run_add_untracked(struct add_i_state
*s
, const struct pathspec
*ps
,
856 struct prefix_item_list
*files
,
857 struct list_and_choose_options
*opts
)
859 struct print_file_item_data
*d
= opts
->list_opts
.print_item_data
;
862 struct lock_file index_lock
;
864 if (get_untracked_files(s
->r
, files
, ps
) < 0)
867 if (!files
->items
.nr
) {
868 printf(_("No untracked files.\n"));
869 goto finish_add_untracked
;
872 opts
->prompt
= N_("Add untracked");
874 count
= list_and_choose(s
, files
, opts
);
877 goto finish_add_untracked
;
879 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
882 goto finish_add_untracked
;
885 for (i
= 0; i
< files
->items
.nr
; i
++) {
886 const char *name
= files
->items
.items
[i
].string
;
887 if (files
->selected
[i
] &&
888 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
889 res
= error(_("could not stage '%s'"), name
);
895 write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
896 res
= error(_("could not write index"));
899 printf(Q_("added %d path\n",
900 "added %d paths\n", count
), (int)count
);
902 finish_add_untracked
:
907 static int run_patch(struct add_i_state
*s
, const struct pathspec
*ps
,
908 struct prefix_item_list
*files
,
909 struct list_and_choose_options
*opts
)
913 size_t unmerged_count
= 0, binary_count
= 0;
915 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
,
916 &unmerged_count
, &binary_count
) < 0)
919 if (unmerged_count
|| binary_count
) {
920 for (i
= j
= 0; i
< files
->items
.nr
; i
++) {
921 struct file_item
*item
= files
->items
.items
[i
].util
;
923 if (item
->index
.binary
|| item
->worktree
.binary
) {
925 free(files
->items
.items
[i
].string
);
926 } else if (item
->index
.unmerged
||
927 item
->worktree
.unmerged
) {
928 color_fprintf_ln(stderr
, s
->error_color
,
929 _("ignoring unmerged: %s"),
930 files
->items
.items
[i
].string
);
932 free(files
->items
.items
[i
].string
);
934 files
->items
.items
[j
++] = files
->items
.items
[i
];
939 if (!files
->items
.nr
) {
941 fprintf(stderr
, _("Only binary files changed.\n"));
943 fprintf(stderr
, _("No changes.\n"));
947 opts
->prompt
= N_("Patch update");
948 count
= list_and_choose(s
, files
, opts
);
950 struct strvec args
= STRVEC_INIT
;
951 struct pathspec ps_selected
= { 0 };
953 for (i
= 0; i
< files
->items
.nr
; i
++)
954 if (files
->selected
[i
])
956 files
->items
.items
[i
].string
);
957 parse_pathspec(&ps_selected
,
958 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
959 PATHSPEC_LITERAL_PATH
, "", args
.v
);
960 res
= run_add_p(s
->r
, ADD_P_ADD
, NULL
, &ps_selected
);
962 clear_pathspec(&ps_selected
);
968 static int run_diff(struct add_i_state
*s
, const struct pathspec
*ps
,
969 struct prefix_item_list
*files
,
970 struct list_and_choose_options
*opts
)
975 struct object_id oid
;
976 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
978 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
981 if (!files
->items
.nr
) {
986 opts
->prompt
= N_("Review diff");
987 opts
->flags
= IMMEDIATE
;
988 count
= list_and_choose(s
, files
, opts
);
991 struct strvec args
= STRVEC_INIT
;
993 strvec_pushl(&args
, "git", "diff", "-p", "--cached",
994 oid_to_hex(!is_initial
? &oid
:
995 s
->r
->hash_algo
->empty_tree
),
997 for (i
= 0; i
< files
->items
.nr
; i
++)
998 if (files
->selected
[i
])
1000 files
->items
.items
[i
].string
);
1001 res
= run_command_v_opt(args
.v
, 0);
1002 strvec_clear(&args
);
1009 static int run_help(struct add_i_state
*s
, const struct pathspec
*unused_ps
,
1010 struct prefix_item_list
*unused_files
,
1011 struct list_and_choose_options
*unused_opts
)
1013 color_fprintf_ln(stdout
, s
->help_color
, "status - %s",
1014 _("show paths with changes"));
1015 color_fprintf_ln(stdout
, s
->help_color
, "update - %s",
1016 _("add working tree state to the staged set of changes"));
1017 color_fprintf_ln(stdout
, s
->help_color
, "revert - %s",
1018 _("revert staged set of changes back to the HEAD version"));
1019 color_fprintf_ln(stdout
, s
->help_color
, "patch - %s",
1020 _("pick hunks and update selectively"));
1021 color_fprintf_ln(stdout
, s
->help_color
, "diff - %s",
1022 _("view diff between HEAD and index"));
1023 color_fprintf_ln(stdout
, s
->help_color
, "add untracked - %s",
1024 _("add contents of untracked files to the staged set of changes"));
1029 static void choose_prompt_help(struct add_i_state
*s
)
1031 color_fprintf_ln(stdout
, s
->help_color
, "%s",
1033 color_fprintf_ln(stdout
, s
->help_color
, "1 - %s",
1034 _("select a single item"));
1035 color_fprintf_ln(stdout
, s
->help_color
, "3-5 - %s",
1036 _("select a range of items"));
1037 color_fprintf_ln(stdout
, s
->help_color
, "2-3,6-9 - %s",
1038 _("select multiple ranges"));
1039 color_fprintf_ln(stdout
, s
->help_color
, "foo - %s",
1040 _("select item based on unique prefix"));
1041 color_fprintf_ln(stdout
, s
->help_color
, "-... - %s",
1042 _("unselect specified items"));
1043 color_fprintf_ln(stdout
, s
->help_color
, "* - %s",
1044 _("choose all items"));
1045 color_fprintf_ln(stdout
, s
->help_color
, " - %s",
1046 _("(empty) finish selecting"));
1049 typedef int (*command_t
)(struct add_i_state
*s
, const struct pathspec
*ps
,
1050 struct prefix_item_list
*files
,
1051 struct list_and_choose_options
*opts
);
1053 struct command_item
{
1054 size_t prefix_length
;
1058 struct print_command_item_data
{
1059 const char *color
, *reset
;
1062 static void print_command_item(int i
, int selected
,
1063 struct string_list_item
*item
,
1064 void *print_command_item_data
)
1066 struct print_command_item_data
*d
= print_command_item_data
;
1067 struct command_item
*util
= item
->util
;
1069 if (!util
->prefix_length
||
1070 !is_valid_prefix(item
->string
, util
->prefix_length
))
1071 printf(" %2d: %s", i
+ 1, item
->string
);
1073 printf(" %2d: %s%.*s%s%s", i
+ 1,
1074 d
->color
, (int)util
->prefix_length
, item
->string
,
1075 d
->reset
, item
->string
+ util
->prefix_length
);
1078 static void command_prompt_help(struct add_i_state
*s
)
1080 const char *help_color
= s
->help_color
;
1081 color_fprintf_ln(stdout
, help_color
, "%s", _("Prompt help:"));
1082 color_fprintf_ln(stdout
, help_color
, "1 - %s",
1083 _("select a numbered item"));
1084 color_fprintf_ln(stdout
, help_color
, "foo - %s",
1085 _("select item based on unique prefix"));
1086 color_fprintf_ln(stdout
, help_color
, " - %s",
1087 _("(empty) select nothing"));
1090 int run_add_i(struct repository
*r
, const struct pathspec
*ps
)
1092 struct add_i_state s
= { NULL
};
1093 struct print_command_item_data data
= { "[", "]" };
1094 struct list_and_choose_options main_loop_opts
= {
1095 { 4, N_("*** Commands ***"), print_command_item
, &data
},
1096 N_("What now"), SINGLETON
| IMMEDIATE
, command_prompt_help
1101 } command_list
[] = {
1102 { "status", run_status
},
1103 { "update", run_update
},
1104 { "revert", run_revert
},
1105 { "add untracked", run_add_untracked
},
1106 { "patch", run_patch
},
1107 { "diff", run_diff
},
1109 { "help", run_help
},
1111 struct prefix_item_list commands
= PREFIX_ITEM_LIST_INIT
;
1113 struct print_file_item_data print_file_item_data
= {
1114 "%12s %12s %s", NULL
, NULL
,
1115 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1117 struct list_and_choose_options opts
= {
1118 { 0, NULL
, print_file_item
, &print_file_item_data
},
1119 NULL
, 0, choose_prompt_help
1121 struct strbuf header
= STRBUF_INIT
;
1122 struct prefix_item_list files
= PREFIX_ITEM_LIST_INIT
;
1126 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
1127 struct command_item
*util
= xcalloc(1, sizeof(*util
));
1128 util
->command
= command_list
[i
].command
;
1129 string_list_append(&commands
.items
, command_list
[i
].string
)
1133 init_add_i_state(&s
, r
);
1136 * When color was asked for, use the prompt color for
1137 * highlighting, otherwise use square brackets.
1140 data
.color
= s
.prompt_color
;
1141 data
.reset
= s
.reset_color
;
1143 print_file_item_data
.color
= data
.color
;
1144 print_file_item_data
.reset
= data
.reset
;
1146 strbuf_addstr(&header
, " ");
1147 strbuf_addf(&header
, print_file_item_data
.modified_fmt
,
1148 _("staged"), _("unstaged"), _("path"));
1149 opts
.list_opts
.header
= header
.buf
;
1151 if (discard_index(r
->index
) < 0 ||
1152 repo_read_index(r
) < 0 ||
1153 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1154 NULL
, NULL
, NULL
) < 0)
1155 warning(_("could not refresh index"));
1157 res
= run_status(&s
, ps
, &files
, &opts
);
1160 struct command_item
*util
;
1162 i
= list_and_choose(&s
, &commands
, &main_loop_opts
);
1163 if (i
< 0 || i
>= commands
.items
.nr
)
1166 util
= commands
.items
.items
[i
].util
;
1168 if (i
== LIST_AND_CHOOSE_QUIT
|| (util
&& !util
->command
)) {
1169 printf(_("Bye.\n"));
1175 res
= util
->command(&s
, ps
, &files
, &opts
);
1178 prefix_item_list_clear(&files
);
1179 strbuf_release(&print_file_item_data
.buf
);
1180 strbuf_release(&print_file_item_data
.name
);
1181 strbuf_release(&print_file_item_data
.index
);
1182 strbuf_release(&print_file_item_data
.worktree
);
1183 strbuf_release(&header
);
1184 prefix_item_list_clear(&commands
);
1185 clear_add_i_state(&s
);