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
);
73 if (s
->use_single_key
)
77 void clear_add_i_state(struct add_i_state
*s
)
79 FREE_AND_NULL(s
->interactive_diff_filter
);
80 FREE_AND_NULL(s
->interactive_diff_algorithm
);
81 memset(s
, 0, sizeof(*s
));
86 * A "prefix item list" is a list of items that are identified by a string, and
87 * a unique prefix (if any) is determined for each item.
89 * It is implemented in the form of a pair of `string_list`s, the first one
90 * duplicating the strings, with the `util` field pointing at a structure whose
91 * first field must be `size_t prefix_length`.
93 * That `prefix_length` field will be computed by `find_unique_prefixes()`; It
94 * will be set to zero if no valid, unique prefix could be found.
96 * The second `string_list` is called `sorted` and does _not_ duplicate the
97 * strings but simply reuses the first one's, with the `util` field pointing at
98 * the `string_item_list` of the first `string_list`. It will be populated and
99 * sorted by `find_unique_prefixes()`.
101 struct prefix_item_list
{
102 struct string_list items
;
103 struct string_list sorted
;
104 int *selected
; /* for multi-selections */
105 size_t min_length
, max_length
;
107 #define PREFIX_ITEM_LIST_INIT { \
108 .items = STRING_LIST_INIT_DUP, \
109 .sorted = STRING_LIST_INIT_NODUP, \
114 static void prefix_item_list_clear(struct prefix_item_list
*list
)
116 string_list_clear(&list
->items
, 1);
117 string_list_clear(&list
->sorted
, 0);
118 FREE_AND_NULL(list
->selected
);
121 static void extend_prefix_length(struct string_list_item
*p
,
122 const char *other_string
, size_t max_length
)
124 size_t *len
= p
->util
;
126 if (!*len
|| memcmp(p
->string
, other_string
, *len
))
130 char c
= p
->string
[*len
];
133 * Is `p` a strict prefix of `other`? Or have we exhausted the
134 * maximal length of the prefix? Or is the current character a
135 * multi-byte UTF-8 one? If so, there is no valid, unique
138 if (!c
|| ++*len
> max_length
|| !isascii(c
)) {
143 if (c
!= other_string
[*len
- 1])
148 static void find_unique_prefixes(struct prefix_item_list
*list
)
152 if (list
->sorted
.nr
== list
->items
.nr
)
155 string_list_clear(&list
->sorted
, 0);
156 /* Avoid reallocating incrementally */
157 list
->sorted
.items
= xmalloc(st_mult(sizeof(*list
->sorted
.items
),
159 list
->sorted
.nr
= list
->sorted
.alloc
= list
->items
.nr
;
161 for (i
= 0; i
< list
->items
.nr
; i
++) {
162 list
->sorted
.items
[i
].string
= list
->items
.items
[i
].string
;
163 list
->sorted
.items
[i
].util
= list
->items
.items
+ i
;
166 string_list_sort(&list
->sorted
);
168 for (i
= 0; i
< list
->sorted
.nr
; i
++) {
169 struct string_list_item
*sorted_item
= list
->sorted
.items
+ i
;
170 struct string_list_item
*item
= sorted_item
->util
;
171 size_t *len
= item
->util
;
174 while (*len
< list
->min_length
) {
175 char c
= item
->string
[(*len
)++];
177 if (!c
|| !isascii(c
)) {
184 extend_prefix_length(item
, sorted_item
[-1].string
,
186 if (i
+ 1 < list
->sorted
.nr
)
187 extend_prefix_length(item
, sorted_item
[1].string
,
192 static ssize_t
find_unique(const char *string
, struct prefix_item_list
*list
)
194 int index
= string_list_find_insert_index(&list
->sorted
, string
, 1);
195 struct string_list_item
*item
;
197 if (list
->items
.nr
!= list
->sorted
.nr
)
198 BUG("prefix_item_list in inconsistent state (%"PRIuMAX
200 (uintmax_t)list
->items
.nr
, (uintmax_t)list
->sorted
.nr
);
203 item
= list
->sorted
.items
[-1 - index
].util
;
204 else if (index
> 0 &&
205 starts_with(list
->sorted
.items
[index
- 1].string
, string
))
207 else if (index
+ 1 < list
->sorted
.nr
&&
208 starts_with(list
->sorted
.items
[index
+ 1].string
, string
))
210 else if (index
< list
->sorted
.nr
&&
211 starts_with(list
->sorted
.items
[index
].string
, string
))
212 item
= list
->sorted
.items
[index
].util
;
215 return item
- list
->items
.items
;
218 struct list_options
{
221 void (*print_item
)(int i
, int selected
, struct string_list_item
*item
,
222 void *print_item_data
);
223 void *print_item_data
;
226 static void list(struct add_i_state
*s
, struct string_list
*list
, int *selected
,
227 struct list_options
*opts
)
235 color_fprintf_ln(stdout
, s
->header_color
,
238 for (i
= 0; i
< list
->nr
; i
++) {
239 opts
->print_item(i
, selected
? selected
[i
] : 0, list
->items
+ i
,
240 opts
->print_item_data
);
242 if ((opts
->columns
) && ((i
+ 1) % (opts
->columns
))) {
255 struct list_and_choose_options
{
256 struct list_options list_opts
;
263 void (*print_help
)(struct add_i_state
*s
);
266 #define LIST_AND_CHOOSE_ERROR (-1)
267 #define LIST_AND_CHOOSE_QUIT (-2)
270 * Returns the selected index in singleton mode, the number of selected items
273 * If an error occurred, returns `LIST_AND_CHOOSE_ERROR`. Upon EOF,
274 * `LIST_AND_CHOOSE_QUIT` is returned.
276 static ssize_t
list_and_choose(struct add_i_state
*s
,
277 struct prefix_item_list
*items
,
278 struct list_and_choose_options
*opts
)
280 int singleton
= opts
->flags
& SINGLETON
;
281 int immediate
= opts
->flags
& IMMEDIATE
;
283 struct strbuf input
= STRBUF_INIT
;
284 ssize_t res
= singleton
? LIST_AND_CHOOSE_ERROR
: 0;
287 free(items
->selected
);
288 CALLOC_ARRAY(items
->selected
, items
->items
.nr
);
291 if (singleton
&& !immediate
)
292 BUG("singleton requires immediate");
294 find_unique_prefixes(items
);
299 strbuf_reset(&input
);
301 list(s
, &items
->items
, items
->selected
, &opts
->list_opts
);
303 color_fprintf(stdout
, s
->prompt_color
, "%s", opts
->prompt
);
304 fputs(singleton
? "> " : ">> ", stdout
);
307 if (git_read_line_interactively(&input
) == EOF
) {
310 res
= LIST_AND_CHOOSE_QUIT
;
317 if (!strcmp(input
.buf
, "?")) {
324 size_t sep
= strcspn(p
, " \t\r\n,");
326 /* `from` is inclusive, `to` is exclusive */
327 ssize_t from
= -1, to
= -1;
336 /* Input that begins with '-'; de-select */
343 if (sep
== 1 && *p
== '*') {
345 to
= items
->items
.nr
;
346 } else if (isdigit(*p
)) {
349 * A range can be specified like 5-7 or 5-.
351 * Note: `from` is 0-based while the user input
352 * is 1-based, hence we have to decrement by
353 * one. We do not have to decrement `to` even
354 * if it is 0-based because it is an exclusive
357 from
= strtoul(p
, &endp
, 10) - 1;
360 else if (*endp
== '-') {
361 if (isdigit(*(++endp
)))
362 to
= strtoul(endp
, &endp
, 10);
364 to
= items
->items
.nr
;
365 /* extra characters after the range? */
374 from
= find_unique(p
, items
);
379 if (from
< 0 || from
>= items
->items
.nr
||
380 (singleton
&& from
+ 1 != to
)) {
381 color_fprintf_ln(stderr
, s
->error_color
,
384 } else if (singleton
) {
389 if (to
> items
->items
.nr
)
390 to
= items
->items
.nr
;
392 for (; from
< to
; from
++)
393 if (items
->selected
[from
] != choose
) {
394 items
->selected
[from
] = choose
;
395 res
+= choose
? +1 : -1;
401 if ((immediate
&& res
!= LIST_AND_CHOOSE_ERROR
) ||
402 !strcmp(input
.buf
, "*"))
406 strbuf_release(&input
);
412 unsigned seen
:1, unmerged
:1, binary
:1;
416 size_t prefix_length
;
417 struct adddel index
, worktree
;
420 static void add_file_item(struct string_list
*files
, const char *name
)
422 struct file_item
*item
= xcalloc(1, sizeof(*item
));
424 string_list_append(files
, name
)->util
= item
;
427 struct pathname_entry
{
428 struct hashmap_entry ent
;
430 struct file_item
*item
;
433 static int pathname_entry_cmp(const void *cmp_data UNUSED
,
434 const struct hashmap_entry
*he1
,
435 const struct hashmap_entry
*he2
,
438 const struct pathname_entry
*e1
=
439 container_of(he1
, const struct pathname_entry
, ent
);
440 const struct pathname_entry
*e2
=
441 container_of(he2
, const struct pathname_entry
, ent
);
443 return strcmp(e1
->name
, name
? (const char *)name
: e2
->name
);
446 struct collection_status
{
447 enum { FROM_WORKTREE
= 0, FROM_INDEX
= 1 } mode
;
449 const char *reference
;
451 unsigned skip_unseen
:1;
452 size_t unmerged_count
, binary_count
;
453 struct string_list
*files
;
454 struct hashmap file_map
;
457 static void collect_changes_cb(struct diff_queue_struct
*q
,
458 struct diff_options
*options
,
461 struct collection_status
*s
= data
;
462 struct diffstat_t stat
= { 0 };
468 compute_diffstat(options
, &stat
, q
);
470 for (i
= 0; i
< stat
.nr
; i
++) {
471 const char *name
= stat
.files
[i
]->name
;
472 int hash
= strhash(name
);
473 struct pathname_entry
*entry
;
474 struct file_item
*file_item
;
475 struct adddel
*adddel
, *other_adddel
;
477 entry
= hashmap_get_entry_from_hash(&s
->file_map
, hash
, name
,
478 struct pathname_entry
, ent
);
483 add_file_item(s
->files
, name
);
485 CALLOC_ARRAY(entry
, 1);
486 hashmap_entry_init(&entry
->ent
, hash
);
487 entry
->name
= s
->files
->items
[s
->files
->nr
- 1].string
;
488 entry
->item
= s
->files
->items
[s
->files
->nr
- 1].util
;
489 hashmap_add(&s
->file_map
, &entry
->ent
);
492 file_item
= entry
->item
;
493 adddel
= s
->mode
== FROM_INDEX
?
494 &file_item
->index
: &file_item
->worktree
;
495 other_adddel
= s
->mode
== FROM_INDEX
?
496 &file_item
->worktree
: &file_item
->index
;
498 adddel
->add
= stat
.files
[i
]->added
;
499 adddel
->del
= stat
.files
[i
]->deleted
;
500 if (stat
.files
[i
]->is_binary
) {
501 if (!other_adddel
->binary
)
505 if (stat
.files
[i
]->is_unmerged
) {
506 if (!other_adddel
->unmerged
)
508 adddel
->unmerged
= 1;
511 free_diffstat_info(&stat
);
514 enum modified_files_filter
{
520 static int get_modified_files(struct repository
*r
,
521 enum modified_files_filter filter
,
522 struct prefix_item_list
*files
,
523 const struct pathspec
*ps
,
524 size_t *unmerged_count
,
525 size_t *binary_count
)
527 struct object_id head_oid
;
528 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
530 struct collection_status s
= { 0 };
533 discard_index(r
->index
);
534 if (repo_read_index_preload(r
, ps
, 0) < 0)
535 return error(_("could not read index"));
537 prefix_item_list_clear(files
);
538 s
.files
= &files
->items
;
539 hashmap_init(&s
.file_map
, pathname_entry_cmp
, NULL
, 0);
541 for (i
= 0; i
< 2; i
++) {
543 struct setup_revision_opt opt
= { 0 };
545 if (filter
== INDEX_ONLY
)
546 s
.mode
= (i
== 0) ? FROM_INDEX
: FROM_WORKTREE
;
548 s
.mode
= (i
== 0) ? FROM_WORKTREE
: FROM_INDEX
;
549 s
.skip_unseen
= filter
&& i
;
551 opt
.def
= is_initial
?
552 empty_tree_oid_hex() : oid_to_hex(&head_oid
);
554 init_revisions(&rev
, NULL
);
555 setup_revisions(0, NULL
, &rev
, &opt
);
557 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
558 rev
.diffopt
.format_callback
= collect_changes_cb
;
559 rev
.diffopt
.format_callback_data
= &s
;
562 copy_pathspec(&rev
.prune_data
, ps
);
564 if (s
.mode
== FROM_INDEX
)
565 run_diff_index(&rev
, 1);
567 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
568 run_diff_files(&rev
, 0);
571 release_revisions(&rev
);
573 hashmap_clear_and_free(&s
.file_map
, struct pathname_entry
, ent
);
575 *unmerged_count
= s
.unmerged_count
;
577 *binary_count
= s
.binary_count
;
579 /* While the diffs are ordered already, we ran *two* diffs... */
580 string_list_sort(&files
->items
);
585 static void render_adddel(struct strbuf
*buf
,
586 struct adddel
*ad
, const char *no_changes
)
589 strbuf_addstr(buf
, _("binary"));
591 strbuf_addf(buf
, "+%"PRIuMAX
"/-%"PRIuMAX
,
592 (uintmax_t)ad
->add
, (uintmax_t)ad
->del
);
594 strbuf_addstr(buf
, no_changes
);
597 /* filters out prefixes which have special meaning to list_and_choose() */
598 static int is_valid_prefix(const char *prefix
, size_t prefix_len
)
600 return prefix_len
&& prefix
&&
602 * We expect `prefix` to be NUL terminated, therefore this
603 * `strcspn()` call is okay, even if it might do much more
604 * work than strictly necessary.
606 strcspn(prefix
, " \t\r\n,") >= prefix_len
&& /* separators */
607 *prefix
!= '-' && /* deselection */
608 !isdigit(*prefix
) && /* selection */
610 (*prefix
!= '*' && /* "all" wildcard */
611 *prefix
!= '?')); /* prompt help */
614 struct print_file_item_data
{
615 const char *modified_fmt
, *color
, *reset
;
616 struct strbuf buf
, name
, index
, worktree
;
617 unsigned only_names
:1;
620 static void print_file_item(int i
, int selected
, struct string_list_item
*item
,
621 void *print_file_item_data
)
623 struct file_item
*c
= item
->util
;
624 struct print_file_item_data
*d
= print_file_item_data
;
625 const char *highlighted
= NULL
;
627 strbuf_reset(&d
->index
);
628 strbuf_reset(&d
->worktree
);
629 strbuf_reset(&d
->buf
);
631 /* Format the item with the prefix highlighted. */
632 if (c
->prefix_length
> 0 &&
633 is_valid_prefix(item
->string
, c
->prefix_length
)) {
634 strbuf_reset(&d
->name
);
635 strbuf_addf(&d
->name
, "%s%.*s%s%s", d
->color
,
636 (int)c
->prefix_length
, item
->string
, d
->reset
,
637 item
->string
+ c
->prefix_length
);
638 highlighted
= d
->name
.buf
;
642 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1,
643 highlighted
? highlighted
: item
->string
);
647 render_adddel(&d
->worktree
, &c
->worktree
, _("nothing"));
648 render_adddel(&d
->index
, &c
->index
, _("unchanged"));
650 strbuf_addf(&d
->buf
, d
->modified_fmt
, d
->index
.buf
, d
->worktree
.buf
,
651 highlighted
? highlighted
: item
->string
);
653 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1, d
->buf
.buf
);
656 static int run_status(struct add_i_state
*s
, const struct pathspec
*ps
,
657 struct prefix_item_list
*files
,
658 struct list_and_choose_options
*opts
)
660 if (get_modified_files(s
->r
, NO_FILTER
, files
, ps
, NULL
, NULL
) < 0)
663 list(s
, &files
->items
, NULL
, &opts
->list_opts
);
669 static int run_update(struct add_i_state
*s
, const struct pathspec
*ps
,
670 struct prefix_item_list
*files
,
671 struct list_and_choose_options
*opts
)
675 struct lock_file index_lock
;
677 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
, NULL
, NULL
) < 0)
680 if (!files
->items
.nr
) {
685 opts
->prompt
= N_("Update");
686 count
= list_and_choose(s
, files
, opts
);
692 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
698 for (i
= 0; i
< files
->items
.nr
; i
++) {
699 const char *name
= files
->items
.items
[i
].string
;
702 if (!files
->selected
[i
])
704 if (lstat(name
, &st
) && is_missing_file_error(errno
)) {
705 if (remove_file_from_index(s
->r
->index
, name
) < 0) {
706 res
= error(_("could not stage '%s'"), name
);
709 } else if (add_file_to_index(s
->r
->index
, name
, 0) < 0) {
710 res
= error(_("could not stage '%s'"), name
);
715 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
716 res
= error(_("could not write index"));
719 printf(Q_("updated %d path\n",
720 "updated %d paths\n", count
), (int)count
);
726 static void revert_from_diff(struct diff_queue_struct
*q
,
727 struct diff_options
*opt
, void *data UNUSED
)
729 int i
, add_flags
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
731 for (i
= 0; i
< q
->nr
; i
++) {
732 struct diff_filespec
*one
= q
->queue
[i
]->one
;
733 struct cache_entry
*ce
;
735 if (!(one
->mode
&& !is_null_oid(&one
->oid
))) {
736 remove_file_from_index(opt
->repo
->index
, one
->path
);
737 printf(_("note: %s is untracked now.\n"), one
->path
);
739 ce
= make_cache_entry(opt
->repo
->index
, one
->mode
,
740 &one
->oid
, one
->path
, 0, 0);
742 die(_("make_cache_entry failed for path '%s'"),
744 add_index_entry(opt
->repo
->index
, ce
, add_flags
);
749 static int run_revert(struct add_i_state
*s
, const struct pathspec
*ps
,
750 struct prefix_item_list
*files
,
751 struct list_and_choose_options
*opts
)
756 struct object_id oid
;
757 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
759 struct lock_file index_lock
;
762 struct diff_options diffopt
= { NULL
};
764 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
767 if (!files
->items
.nr
) {
772 opts
->prompt
= N_("Revert");
773 count
= list_and_choose(s
, files
, opts
);
777 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
784 oidcpy(&oid
, s
->r
->hash_algo
->empty_tree
);
786 tree
= parse_tree_indirect(&oid
);
788 res
= error(_("Could not parse HEAD^{tree}"));
791 oidcpy(&oid
, &tree
->object
.oid
);
794 ALLOC_ARRAY(paths
, count
+ 1);
795 for (i
= j
= 0; i
< files
->items
.nr
; i
++)
796 if (files
->selected
[i
])
797 paths
[j
++] = files
->items
.items
[i
].string
;
800 parse_pathspec(&diffopt
.pathspec
, 0,
801 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
,
804 diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
805 diffopt
.format_callback
= revert_from_diff
;
806 diffopt
.flags
.override_submodule_config
= 1;
809 if (do_diff_cache(&oid
, &diffopt
)) {
813 diffcore_std(&diffopt
);
814 diff_flush(&diffopt
);
818 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
,
822 res
= repo_refresh_and_write_index(s
->r
, REFRESH_QUIET
, 0, 1,
826 printf(Q_("reverted %d path\n",
827 "reverted %d paths\n", count
), (int)count
);
834 static int get_untracked_files(struct repository
*r
,
835 struct prefix_item_list
*files
,
836 const struct pathspec
*ps
)
838 struct dir_struct dir
= { 0 };
840 struct strbuf buf
= STRBUF_INIT
;
842 if (repo_read_index(r
) < 0)
843 return error(_("could not read index"));
845 prefix_item_list_clear(files
);
846 setup_standard_excludes(&dir
);
847 add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
848 fill_directory(&dir
, r
->index
, ps
);
850 for (i
= 0; i
< dir
.nr
; i
++) {
851 struct dir_entry
*ent
= dir
.entries
[i
];
853 if (index_name_is_other(r
->index
, ent
->name
, ent
->len
)) {
855 strbuf_add(&buf
, ent
->name
, ent
->len
);
856 add_file_item(&files
->items
, buf
.buf
);
860 strbuf_release(&buf
);
864 static int run_add_untracked(struct add_i_state
*s
, const struct pathspec
*ps
,
865 struct prefix_item_list
*files
,
866 struct list_and_choose_options
*opts
)
868 struct print_file_item_data
*d
= opts
->list_opts
.print_item_data
;
871 struct lock_file index_lock
;
873 if (get_untracked_files(s
->r
, files
, ps
) < 0)
876 if (!files
->items
.nr
) {
877 printf(_("No untracked files.\n"));
878 goto finish_add_untracked
;
881 opts
->prompt
= N_("Add untracked");
883 count
= list_and_choose(s
, files
, opts
);
886 goto finish_add_untracked
;
888 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
891 goto finish_add_untracked
;
894 for (i
= 0; i
< files
->items
.nr
; i
++) {
895 const char *name
= files
->items
.items
[i
].string
;
896 if (files
->selected
[i
] &&
897 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
898 res
= error(_("could not stage '%s'"), name
);
904 write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
905 res
= error(_("could not write index"));
908 printf(Q_("added %d path\n",
909 "added %d paths\n", count
), (int)count
);
911 finish_add_untracked
:
916 static int run_patch(struct add_i_state
*s
, const struct pathspec
*ps
,
917 struct prefix_item_list
*files
,
918 struct list_and_choose_options
*opts
)
922 size_t unmerged_count
= 0, binary_count
= 0;
924 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
,
925 &unmerged_count
, &binary_count
) < 0)
928 if (unmerged_count
|| binary_count
) {
929 for (i
= j
= 0; i
< files
->items
.nr
; i
++) {
930 struct file_item
*item
= files
->items
.items
[i
].util
;
932 if (item
->index
.binary
|| item
->worktree
.binary
) {
934 free(files
->items
.items
[i
].string
);
935 } else if (item
->index
.unmerged
||
936 item
->worktree
.unmerged
) {
937 color_fprintf_ln(stderr
, s
->error_color
,
938 _("ignoring unmerged: %s"),
939 files
->items
.items
[i
].string
);
941 free(files
->items
.items
[i
].string
);
943 files
->items
.items
[j
++] = files
->items
.items
[i
];
948 if (!files
->items
.nr
) {
950 fprintf(stderr
, _("Only binary files changed.\n"));
952 fprintf(stderr
, _("No changes.\n"));
956 opts
->prompt
= N_("Patch update");
957 count
= list_and_choose(s
, files
, opts
);
959 struct strvec args
= STRVEC_INIT
;
960 struct pathspec ps_selected
= { 0 };
962 for (i
= 0; i
< files
->items
.nr
; i
++)
963 if (files
->selected
[i
])
965 files
->items
.items
[i
].string
);
966 parse_pathspec(&ps_selected
,
967 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
968 PATHSPEC_LITERAL_PATH
, "", args
.v
);
969 res
= run_add_p(s
->r
, ADD_P_ADD
, NULL
, &ps_selected
);
971 clear_pathspec(&ps_selected
);
977 static int run_diff(struct add_i_state
*s
, const struct pathspec
*ps
,
978 struct prefix_item_list
*files
,
979 struct list_and_choose_options
*opts
)
984 struct object_id oid
;
985 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
987 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
990 if (!files
->items
.nr
) {
995 opts
->prompt
= N_("Review diff");
996 opts
->flags
= IMMEDIATE
;
997 count
= list_and_choose(s
, files
, opts
);
1000 struct child_process cmd
= CHILD_PROCESS_INIT
;
1002 strvec_pushl(&cmd
.args
, "git", "diff", "-p", "--cached",
1003 oid_to_hex(!is_initial
? &oid
:
1004 s
->r
->hash_algo
->empty_tree
),
1006 for (i
= 0; i
< files
->items
.nr
; i
++)
1007 if (files
->selected
[i
])
1008 strvec_push(&cmd
.args
,
1009 files
->items
.items
[i
].string
);
1010 res
= run_command(&cmd
);
1017 static int run_help(struct add_i_state
*s
, const struct pathspec
*unused_ps
,
1018 struct prefix_item_list
*unused_files
,
1019 struct list_and_choose_options
*unused_opts
)
1021 color_fprintf_ln(stdout
, s
->help_color
, "status - %s",
1022 _("show paths with changes"));
1023 color_fprintf_ln(stdout
, s
->help_color
, "update - %s",
1024 _("add working tree state to the staged set of changes"));
1025 color_fprintf_ln(stdout
, s
->help_color
, "revert - %s",
1026 _("revert staged set of changes back to the HEAD version"));
1027 color_fprintf_ln(stdout
, s
->help_color
, "patch - %s",
1028 _("pick hunks and update selectively"));
1029 color_fprintf_ln(stdout
, s
->help_color
, "diff - %s",
1030 _("view diff between HEAD and index"));
1031 color_fprintf_ln(stdout
, s
->help_color
, "add untracked - %s",
1032 _("add contents of untracked files to the staged set of changes"));
1037 static void choose_prompt_help(struct add_i_state
*s
)
1039 color_fprintf_ln(stdout
, s
->help_color
, "%s",
1041 color_fprintf_ln(stdout
, s
->help_color
, "1 - %s",
1042 _("select a single item"));
1043 color_fprintf_ln(stdout
, s
->help_color
, "3-5 - %s",
1044 _("select a range of items"));
1045 color_fprintf_ln(stdout
, s
->help_color
, "2-3,6-9 - %s",
1046 _("select multiple ranges"));
1047 color_fprintf_ln(stdout
, s
->help_color
, "foo - %s",
1048 _("select item based on unique prefix"));
1049 color_fprintf_ln(stdout
, s
->help_color
, "-... - %s",
1050 _("unselect specified items"));
1051 color_fprintf_ln(stdout
, s
->help_color
, "* - %s",
1052 _("choose all items"));
1053 color_fprintf_ln(stdout
, s
->help_color
, " - %s",
1054 _("(empty) finish selecting"));
1057 typedef int (*command_t
)(struct add_i_state
*s
, const struct pathspec
*ps
,
1058 struct prefix_item_list
*files
,
1059 struct list_and_choose_options
*opts
);
1061 struct command_item
{
1062 size_t prefix_length
;
1066 struct print_command_item_data
{
1067 const char *color
, *reset
;
1070 static void print_command_item(int i
, int selected
,
1071 struct string_list_item
*item
,
1072 void *print_command_item_data
)
1074 struct print_command_item_data
*d
= print_command_item_data
;
1075 struct command_item
*util
= item
->util
;
1077 if (!util
->prefix_length
||
1078 !is_valid_prefix(item
->string
, util
->prefix_length
))
1079 printf(" %2d: %s", i
+ 1, item
->string
);
1081 printf(" %2d: %s%.*s%s%s", i
+ 1,
1082 d
->color
, (int)util
->prefix_length
, item
->string
,
1083 d
->reset
, item
->string
+ util
->prefix_length
);
1086 static void command_prompt_help(struct add_i_state
*s
)
1088 const char *help_color
= s
->help_color
;
1089 color_fprintf_ln(stdout
, help_color
, "%s", _("Prompt help:"));
1090 color_fprintf_ln(stdout
, help_color
, "1 - %s",
1091 _("select a numbered item"));
1092 color_fprintf_ln(stdout
, help_color
, "foo - %s",
1093 _("select item based on unique prefix"));
1094 color_fprintf_ln(stdout
, help_color
, " - %s",
1095 _("(empty) select nothing"));
1098 int run_add_i(struct repository
*r
, const struct pathspec
*ps
)
1100 struct add_i_state s
= { NULL
};
1101 struct print_command_item_data data
= { "[", "]" };
1102 struct list_and_choose_options main_loop_opts
= {
1103 { 4, N_("*** Commands ***"), print_command_item
, &data
},
1104 N_("What now"), SINGLETON
| IMMEDIATE
, command_prompt_help
1109 } command_list
[] = {
1110 { "status", run_status
},
1111 { "update", run_update
},
1112 { "revert", run_revert
},
1113 { "add untracked", run_add_untracked
},
1114 { "patch", run_patch
},
1115 { "diff", run_diff
},
1117 { "help", run_help
},
1119 struct prefix_item_list commands
= PREFIX_ITEM_LIST_INIT
;
1121 struct print_file_item_data print_file_item_data
= {
1122 "%12s %12s %s", NULL
, NULL
,
1123 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1125 struct list_and_choose_options opts
= {
1126 { 0, NULL
, print_file_item
, &print_file_item_data
},
1127 NULL
, 0, choose_prompt_help
1129 struct strbuf header
= STRBUF_INIT
;
1130 struct prefix_item_list files
= PREFIX_ITEM_LIST_INIT
;
1134 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
1135 struct command_item
*util
= xcalloc(1, sizeof(*util
));
1136 util
->command
= command_list
[i
].command
;
1137 string_list_append(&commands
.items
, command_list
[i
].string
)
1141 init_add_i_state(&s
, r
);
1144 * When color was asked for, use the prompt color for
1145 * highlighting, otherwise use square brackets.
1148 data
.color
= s
.prompt_color
;
1149 data
.reset
= s
.reset_color
;
1151 print_file_item_data
.color
= data
.color
;
1152 print_file_item_data
.reset
= data
.reset
;
1154 strbuf_addstr(&header
, " ");
1155 strbuf_addf(&header
, print_file_item_data
.modified_fmt
,
1156 _("staged"), _("unstaged"), _("path"));
1157 opts
.list_opts
.header
= header
.buf
;
1159 discard_index(r
->index
);
1160 if (repo_read_index(r
) < 0 ||
1161 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1162 NULL
, NULL
, NULL
) < 0)
1163 warning(_("could not refresh index"));
1165 res
= run_status(&s
, ps
, &files
, &opts
);
1168 struct command_item
*util
;
1170 i
= list_and_choose(&s
, &commands
, &main_loop_opts
);
1171 if (i
< 0 || i
>= commands
.items
.nr
)
1174 util
= commands
.items
.items
[i
].util
;
1176 if (i
== LIST_AND_CHOOSE_QUIT
|| (util
&& !util
->command
)) {
1177 printf(_("Bye.\n"));
1183 res
= util
->command(&s
, ps
, &files
, &opts
);
1186 prefix_item_list_clear(&files
);
1187 strbuf_release(&print_file_item_data
.buf
);
1188 strbuf_release(&print_file_item_data
.name
);
1189 strbuf_release(&print_file_item_data
.index
);
1190 strbuf_release(&print_file_item_data
.worktree
);
1191 strbuf_release(&header
);
1192 prefix_item_list_clear(&commands
);
1193 clear_add_i_state(&s
);