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 if (isdigit(*(++endp
)))
330 to
= strtoul(endp
, &endp
, 10);
332 to
= items
->items
.nr
;
333 /* extra characters after the range? */
342 from
= find_unique(p
, items
);
347 if (from
< 0 || from
>= items
->items
.nr
||
348 (singleton
&& from
+ 1 != to
)) {
349 color_fprintf_ln(stdout
, s
->error_color
,
352 } else if (singleton
) {
357 if (to
> items
->items
.nr
)
358 to
= items
->items
.nr
;
360 for (; from
< to
; from
++)
361 if (items
->selected
[from
] != choose
) {
362 items
->selected
[from
] = choose
;
363 res
+= choose
? +1 : -1;
369 if ((immediate
&& res
!= LIST_AND_CHOOSE_ERROR
) ||
370 !strcmp(input
.buf
, "*"))
374 strbuf_release(&input
);
380 unsigned seen
:1, unmerged
:1, binary
:1;
384 size_t prefix_length
;
385 struct adddel index
, worktree
;
388 static void add_file_item(struct string_list
*files
, const char *name
)
390 struct file_item
*item
= xcalloc(sizeof(*item
), 1);
392 string_list_append(files
, name
)->util
= item
;
395 struct pathname_entry
{
396 struct hashmap_entry ent
;
398 struct file_item
*item
;
401 static int pathname_entry_cmp(const void *unused_cmp_data
,
402 const struct hashmap_entry
*he1
,
403 const struct hashmap_entry
*he2
,
406 const struct pathname_entry
*e1
=
407 container_of(he1
, const struct pathname_entry
, ent
);
408 const struct pathname_entry
*e2
=
409 container_of(he2
, const struct pathname_entry
, ent
);
411 return strcmp(e1
->name
, name
? (const char *)name
: e2
->name
);
414 struct collection_status
{
415 enum { FROM_WORKTREE
= 0, FROM_INDEX
= 1 } mode
;
417 const char *reference
;
419 unsigned skip_unseen
:1;
420 size_t unmerged_count
, binary_count
;
421 struct string_list
*files
;
422 struct hashmap file_map
;
425 static void collect_changes_cb(struct diff_queue_struct
*q
,
426 struct diff_options
*options
,
429 struct collection_status
*s
= data
;
430 struct diffstat_t stat
= { 0 };
436 compute_diffstat(options
, &stat
, q
);
438 for (i
= 0; i
< stat
.nr
; i
++) {
439 const char *name
= stat
.files
[i
]->name
;
440 int hash
= strhash(name
);
441 struct pathname_entry
*entry
;
442 struct file_item
*file_item
;
443 struct adddel
*adddel
, *other_adddel
;
445 entry
= hashmap_get_entry_from_hash(&s
->file_map
, hash
, name
,
446 struct pathname_entry
, ent
);
451 add_file_item(s
->files
, name
);
453 entry
= xcalloc(sizeof(*entry
), 1);
454 hashmap_entry_init(&entry
->ent
, hash
);
455 entry
->name
= s
->files
->items
[s
->files
->nr
- 1].string
;
456 entry
->item
= s
->files
->items
[s
->files
->nr
- 1].util
;
457 hashmap_add(&s
->file_map
, &entry
->ent
);
460 file_item
= entry
->item
;
461 adddel
= s
->mode
== FROM_INDEX
?
462 &file_item
->index
: &file_item
->worktree
;
463 other_adddel
= s
->mode
== FROM_INDEX
?
464 &file_item
->worktree
: &file_item
->index
;
466 adddel
->add
= stat
.files
[i
]->added
;
467 adddel
->del
= stat
.files
[i
]->deleted
;
468 if (stat
.files
[i
]->is_binary
) {
469 if (!other_adddel
->binary
)
473 if (stat
.files
[i
]->is_unmerged
) {
474 if (!other_adddel
->unmerged
)
476 adddel
->unmerged
= 1;
479 free_diffstat_info(&stat
);
482 enum modified_files_filter
{
488 static int get_modified_files(struct repository
*r
,
489 enum modified_files_filter filter
,
490 struct prefix_item_list
*files
,
491 const struct pathspec
*ps
,
492 size_t *unmerged_count
,
493 size_t *binary_count
)
495 struct object_id head_oid
;
496 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
498 struct collection_status s
= { 0 };
501 if (discard_index(r
->index
) < 0 ||
502 repo_read_index_preload(r
, ps
, 0) < 0)
503 return error(_("could not read index"));
505 prefix_item_list_clear(files
);
506 s
.files
= &files
->items
;
507 hashmap_init(&s
.file_map
, pathname_entry_cmp
, NULL
, 0);
509 for (i
= 0; i
< 2; i
++) {
511 struct setup_revision_opt opt
= { 0 };
513 if (filter
== INDEX_ONLY
)
514 s
.mode
= (i
== 0) ? FROM_INDEX
: FROM_WORKTREE
;
516 s
.mode
= (i
== 0) ? FROM_WORKTREE
: FROM_INDEX
;
517 s
.skip_unseen
= filter
&& i
;
519 opt
.def
= is_initial
?
520 empty_tree_oid_hex() : oid_to_hex(&head_oid
);
522 init_revisions(&rev
, NULL
);
523 setup_revisions(0, NULL
, &rev
, &opt
);
525 rev
.diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
526 rev
.diffopt
.format_callback
= collect_changes_cb
;
527 rev
.diffopt
.format_callback_data
= &s
;
530 copy_pathspec(&rev
.prune_data
, ps
);
532 if (s
.mode
== FROM_INDEX
)
533 run_diff_index(&rev
, 1);
535 rev
.diffopt
.flags
.ignore_dirty_submodules
= 1;
536 run_diff_files(&rev
, 0);
540 clear_pathspec(&rev
.prune_data
);
542 hashmap_free_entries(&s
.file_map
, struct pathname_entry
, ent
);
544 *unmerged_count
= s
.unmerged_count
;
546 *binary_count
= s
.binary_count
;
548 /* While the diffs are ordered already, we ran *two* diffs... */
549 string_list_sort(&files
->items
);
554 static void render_adddel(struct strbuf
*buf
,
555 struct adddel
*ad
, const char *no_changes
)
558 strbuf_addstr(buf
, _("binary"));
560 strbuf_addf(buf
, "+%"PRIuMAX
"/-%"PRIuMAX
,
561 (uintmax_t)ad
->add
, (uintmax_t)ad
->del
);
563 strbuf_addstr(buf
, no_changes
);
566 /* filters out prefixes which have special meaning to list_and_choose() */
567 static int is_valid_prefix(const char *prefix
, size_t prefix_len
)
569 return prefix_len
&& prefix
&&
571 * We expect `prefix` to be NUL terminated, therefore this
572 * `strcspn()` call is okay, even if it might do much more
573 * work than strictly necessary.
575 strcspn(prefix
, " \t\r\n,") >= prefix_len
&& /* separators */
576 *prefix
!= '-' && /* deselection */
577 !isdigit(*prefix
) && /* selection */
579 (*prefix
!= '*' && /* "all" wildcard */
580 *prefix
!= '?')); /* prompt help */
583 struct print_file_item_data
{
584 const char *modified_fmt
, *color
, *reset
;
585 struct strbuf buf
, name
, index
, worktree
;
586 unsigned only_names
:1;
589 static void print_file_item(int i
, int selected
, struct string_list_item
*item
,
590 void *print_file_item_data
)
592 struct file_item
*c
= item
->util
;
593 struct print_file_item_data
*d
= print_file_item_data
;
594 const char *highlighted
= NULL
;
596 strbuf_reset(&d
->index
);
597 strbuf_reset(&d
->worktree
);
598 strbuf_reset(&d
->buf
);
600 /* Format the item with the prefix highlighted. */
601 if (c
->prefix_length
> 0 &&
602 is_valid_prefix(item
->string
, c
->prefix_length
)) {
603 strbuf_reset(&d
->name
);
604 strbuf_addf(&d
->name
, "%s%.*s%s%s", d
->color
,
605 (int)c
->prefix_length
, item
->string
, d
->reset
,
606 item
->string
+ c
->prefix_length
);
607 highlighted
= d
->name
.buf
;
611 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1,
612 highlighted
? highlighted
: item
->string
);
616 render_adddel(&d
->worktree
, &c
->worktree
, _("nothing"));
617 render_adddel(&d
->index
, &c
->index
, _("unchanged"));
619 strbuf_addf(&d
->buf
, d
->modified_fmt
, d
->index
.buf
, d
->worktree
.buf
,
620 highlighted
? highlighted
: item
->string
);
622 printf("%c%2d: %s", selected
? '*' : ' ', i
+ 1, d
->buf
.buf
);
625 static int run_status(struct add_i_state
*s
, const struct pathspec
*ps
,
626 struct prefix_item_list
*files
,
627 struct list_and_choose_options
*opts
)
629 if (get_modified_files(s
->r
, NO_FILTER
, files
, ps
, NULL
, NULL
) < 0)
632 list(s
, &files
->items
, NULL
, &opts
->list_opts
);
638 static int run_update(struct add_i_state
*s
, const struct pathspec
*ps
,
639 struct prefix_item_list
*files
,
640 struct list_and_choose_options
*opts
)
644 struct lock_file index_lock
;
646 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
, NULL
, NULL
) < 0)
649 if (!files
->items
.nr
) {
654 opts
->prompt
= N_("Update");
655 count
= list_and_choose(s
, files
, opts
);
661 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
667 for (i
= 0; i
< files
->items
.nr
; i
++) {
668 const char *name
= files
->items
.items
[i
].string
;
669 if (files
->selected
[i
] &&
670 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
671 res
= error(_("could not stage '%s'"), name
);
676 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
677 res
= error(_("could not write index"));
680 printf(Q_("updated %d path\n",
681 "updated %d paths\n", count
), (int)count
);
687 static void revert_from_diff(struct diff_queue_struct
*q
,
688 struct diff_options
*opt
, void *data
)
690 int i
, add_flags
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
692 for (i
= 0; i
< q
->nr
; i
++) {
693 struct diff_filespec
*one
= q
->queue
[i
]->one
;
694 struct cache_entry
*ce
;
696 if (!(one
->mode
&& !is_null_oid(&one
->oid
))) {
697 remove_file_from_index(opt
->repo
->index
, one
->path
);
698 printf(_("note: %s is untracked now.\n"), one
->path
);
700 ce
= make_cache_entry(opt
->repo
->index
, one
->mode
,
701 &one
->oid
, one
->path
, 0, 0);
703 die(_("make_cache_entry failed for path '%s'"),
705 add_index_entry(opt
->repo
->index
, ce
, add_flags
);
710 static int run_revert(struct add_i_state
*s
, const struct pathspec
*ps
,
711 struct prefix_item_list
*files
,
712 struct list_and_choose_options
*opts
)
717 struct object_id oid
;
718 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
720 struct lock_file index_lock
;
723 struct diff_options diffopt
= { NULL
};
725 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
728 if (!files
->items
.nr
) {
733 opts
->prompt
= N_("Revert");
734 count
= list_and_choose(s
, files
, opts
);
738 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
745 oidcpy(&oid
, s
->r
->hash_algo
->empty_tree
);
747 tree
= parse_tree_indirect(&oid
);
749 res
= error(_("Could not parse HEAD^{tree}"));
752 oidcpy(&oid
, &tree
->object
.oid
);
755 ALLOC_ARRAY(paths
, count
+ 1);
756 for (i
= j
= 0; i
< files
->items
.nr
; i
++)
757 if (files
->selected
[i
])
758 paths
[j
++] = files
->items
.items
[i
].string
;
761 parse_pathspec(&diffopt
.pathspec
, 0,
762 PATHSPEC_PREFER_FULL
| PATHSPEC_LITERAL_PATH
,
765 diffopt
.output_format
= DIFF_FORMAT_CALLBACK
;
766 diffopt
.format_callback
= revert_from_diff
;
767 diffopt
.flags
.override_submodule_config
= 1;
770 if (do_diff_cache(&oid
, &diffopt
))
773 diffcore_std(&diffopt
);
774 diff_flush(&diffopt
);
777 clear_pathspec(&diffopt
.pathspec
);
779 if (!res
&& write_locked_index(s
->r
->index
, &index_lock
,
783 res
= repo_refresh_and_write_index(s
->r
, REFRESH_QUIET
, 0, 1,
787 printf(Q_("reverted %d path\n",
788 "reverted %d paths\n", count
), (int)count
);
795 static int get_untracked_files(struct repository
*r
,
796 struct prefix_item_list
*files
,
797 const struct pathspec
*ps
)
799 struct dir_struct dir
= { 0 };
801 struct strbuf buf
= STRBUF_INIT
;
803 if (repo_read_index(r
) < 0)
804 return error(_("could not read index"));
806 prefix_item_list_clear(files
);
807 setup_standard_excludes(&dir
);
808 add_pattern_list(&dir
, EXC_CMDL
, "--exclude option");
809 fill_directory(&dir
, r
->index
, ps
);
811 for (i
= 0; i
< dir
.nr
; i
++) {
812 struct dir_entry
*ent
= dir
.entries
[i
];
814 if (index_name_is_other(r
->index
, ent
->name
, ent
->len
)) {
816 strbuf_add(&buf
, ent
->name
, ent
->len
);
817 add_file_item(&files
->items
, buf
.buf
);
821 strbuf_release(&buf
);
825 static int run_add_untracked(struct add_i_state
*s
, const struct pathspec
*ps
,
826 struct prefix_item_list
*files
,
827 struct list_and_choose_options
*opts
)
829 struct print_file_item_data
*d
= opts
->list_opts
.print_item_data
;
832 struct lock_file index_lock
;
834 if (get_untracked_files(s
->r
, files
, ps
) < 0)
837 if (!files
->items
.nr
) {
838 printf(_("No untracked files.\n"));
839 goto finish_add_untracked
;
842 opts
->prompt
= N_("Add untracked");
844 count
= list_and_choose(s
, files
, opts
);
847 goto finish_add_untracked
;
849 fd
= repo_hold_locked_index(s
->r
, &index_lock
, LOCK_REPORT_ON_ERROR
);
852 goto finish_add_untracked
;
855 for (i
= 0; i
< files
->items
.nr
; i
++) {
856 const char *name
= files
->items
.items
[i
].string
;
857 if (files
->selected
[i
] &&
858 add_file_to_index(s
->r
->index
, name
, 0) < 0) {
859 res
= error(_("could not stage '%s'"), name
);
865 write_locked_index(s
->r
->index
, &index_lock
, COMMIT_LOCK
) < 0)
866 res
= error(_("could not write index"));
869 printf(Q_("added %d path\n",
870 "added %d paths\n", count
), (int)count
);
872 finish_add_untracked
:
877 static int run_patch(struct add_i_state
*s
, const struct pathspec
*ps
,
878 struct prefix_item_list
*files
,
879 struct list_and_choose_options
*opts
)
883 size_t unmerged_count
= 0, binary_count
= 0;
885 if (get_modified_files(s
->r
, WORKTREE_ONLY
, files
, ps
,
886 &unmerged_count
, &binary_count
) < 0)
889 if (unmerged_count
|| binary_count
) {
890 for (i
= j
= 0; i
< files
->items
.nr
; i
++) {
891 struct file_item
*item
= files
->items
.items
[i
].util
;
893 if (item
->index
.binary
|| item
->worktree
.binary
) {
895 free(files
->items
.items
[i
].string
);
896 } else if (item
->index
.unmerged
||
897 item
->worktree
.unmerged
) {
898 color_fprintf_ln(stderr
, s
->error_color
,
899 _("ignoring unmerged: %s"),
900 files
->items
.items
[i
].string
);
902 free(files
->items
.items
[i
].string
);
904 files
->items
.items
[j
++] = files
->items
.items
[i
];
909 if (!files
->items
.nr
) {
911 fprintf(stderr
, _("Only binary files changed.\n"));
913 fprintf(stderr
, _("No changes.\n"));
917 opts
->prompt
= N_("Patch update");
918 count
= list_and_choose(s
, files
, opts
);
920 struct argv_array args
= ARGV_ARRAY_INIT
;
921 struct pathspec ps_selected
= { 0 };
923 for (i
= 0; i
< files
->items
.nr
; i
++)
924 if (files
->selected
[i
])
925 argv_array_push(&args
,
926 files
->items
.items
[i
].string
);
927 parse_pathspec(&ps_selected
,
928 PATHSPEC_ALL_MAGIC
& ~PATHSPEC_LITERAL
,
929 PATHSPEC_LITERAL_PATH
, "", args
.argv
);
930 res
= run_add_p(s
->r
, ADD_P_ADD
, NULL
, &ps_selected
);
931 argv_array_clear(&args
);
932 clear_pathspec(&ps_selected
);
938 static int run_diff(struct add_i_state
*s
, const struct pathspec
*ps
,
939 struct prefix_item_list
*files
,
940 struct list_and_choose_options
*opts
)
945 struct object_id oid
;
946 int is_initial
= !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, &oid
,
948 if (get_modified_files(s
->r
, INDEX_ONLY
, files
, ps
, NULL
, NULL
) < 0)
951 if (!files
->items
.nr
) {
956 opts
->prompt
= N_("Review diff");
957 opts
->flags
= IMMEDIATE
;
958 count
= list_and_choose(s
, files
, opts
);
961 struct argv_array args
= ARGV_ARRAY_INIT
;
963 argv_array_pushl(&args
, "git", "diff", "-p", "--cached",
964 oid_to_hex(!is_initial
? &oid
:
965 s
->r
->hash_algo
->empty_tree
),
967 for (i
= 0; i
< files
->items
.nr
; i
++)
968 if (files
->selected
[i
])
969 argv_array_push(&args
,
970 files
->items
.items
[i
].string
);
971 res
= run_command_v_opt(args
.argv
, 0);
972 argv_array_clear(&args
);
979 static int run_help(struct add_i_state
*s
, const struct pathspec
*unused_ps
,
980 struct prefix_item_list
*unused_files
,
981 struct list_and_choose_options
*unused_opts
)
983 color_fprintf_ln(stdout
, s
->help_color
, "status - %s",
984 _("show paths with changes"));
985 color_fprintf_ln(stdout
, s
->help_color
, "update - %s",
986 _("add working tree state to the staged set of changes"));
987 color_fprintf_ln(stdout
, s
->help_color
, "revert - %s",
988 _("revert staged set of changes back to the HEAD version"));
989 color_fprintf_ln(stdout
, s
->help_color
, "patch - %s",
990 _("pick hunks and update selectively"));
991 color_fprintf_ln(stdout
, s
->help_color
, "diff - %s",
992 _("view diff between HEAD and index"));
993 color_fprintf_ln(stdout
, s
->help_color
, "add untracked - %s",
994 _("add contents of untracked files to the staged set of changes"));
999 static void choose_prompt_help(struct add_i_state
*s
)
1001 color_fprintf_ln(stdout
, s
->help_color
, "%s",
1003 color_fprintf_ln(stdout
, s
->help_color
, "1 - %s",
1004 _("select a single item"));
1005 color_fprintf_ln(stdout
, s
->help_color
, "3-5 - %s",
1006 _("select a range of items"));
1007 color_fprintf_ln(stdout
, s
->help_color
, "2-3,6-9 - %s",
1008 _("select multiple ranges"));
1009 color_fprintf_ln(stdout
, s
->help_color
, "foo - %s",
1010 _("select item based on unique prefix"));
1011 color_fprintf_ln(stdout
, s
->help_color
, "-... - %s",
1012 _("unselect specified items"));
1013 color_fprintf_ln(stdout
, s
->help_color
, "* - %s",
1014 _("choose all items"));
1015 color_fprintf_ln(stdout
, s
->help_color
, " - %s",
1016 _("(empty) finish selecting"));
1019 typedef int (*command_t
)(struct add_i_state
*s
, const struct pathspec
*ps
,
1020 struct prefix_item_list
*files
,
1021 struct list_and_choose_options
*opts
);
1023 struct command_item
{
1024 size_t prefix_length
;
1028 struct print_command_item_data
{
1029 const char *color
, *reset
;
1032 static void print_command_item(int i
, int selected
,
1033 struct string_list_item
*item
,
1034 void *print_command_item_data
)
1036 struct print_command_item_data
*d
= print_command_item_data
;
1037 struct command_item
*util
= item
->util
;
1039 if (!util
->prefix_length
||
1040 !is_valid_prefix(item
->string
, util
->prefix_length
))
1041 printf(" %2d: %s", i
+ 1, item
->string
);
1043 printf(" %2d: %s%.*s%s%s", i
+ 1,
1044 d
->color
, (int)util
->prefix_length
, item
->string
,
1045 d
->reset
, item
->string
+ util
->prefix_length
);
1048 static void command_prompt_help(struct add_i_state
*s
)
1050 const char *help_color
= s
->help_color
;
1051 color_fprintf_ln(stdout
, help_color
, "%s", _("Prompt help:"));
1052 color_fprintf_ln(stdout
, help_color
, "1 - %s",
1053 _("select a numbered item"));
1054 color_fprintf_ln(stdout
, help_color
, "foo - %s",
1055 _("select item based on unique prefix"));
1056 color_fprintf_ln(stdout
, help_color
, " - %s",
1057 _("(empty) select nothing"));
1060 int run_add_i(struct repository
*r
, const struct pathspec
*ps
)
1062 struct add_i_state s
= { NULL
};
1063 struct print_command_item_data data
= { "[", "]" };
1064 struct list_and_choose_options main_loop_opts
= {
1065 { 4, N_("*** Commands ***"), print_command_item
, &data
},
1066 N_("What now"), SINGLETON
| IMMEDIATE
, command_prompt_help
1071 } command_list
[] = {
1072 { "status", run_status
},
1073 { "update", run_update
},
1074 { "revert", run_revert
},
1075 { "add untracked", run_add_untracked
},
1076 { "patch", run_patch
},
1077 { "diff", run_diff
},
1079 { "help", run_help
},
1081 struct prefix_item_list commands
= PREFIX_ITEM_LIST_INIT
;
1083 struct print_file_item_data print_file_item_data
= {
1084 "%12s %12s %s", NULL
, NULL
,
1085 STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
, STRBUF_INIT
1087 struct list_and_choose_options opts
= {
1088 { 0, NULL
, print_file_item
, &print_file_item_data
},
1089 NULL
, 0, choose_prompt_help
1091 struct strbuf header
= STRBUF_INIT
;
1092 struct prefix_item_list files
= PREFIX_ITEM_LIST_INIT
;
1096 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
1097 struct command_item
*util
= xcalloc(sizeof(*util
), 1);
1098 util
->command
= command_list
[i
].command
;
1099 string_list_append(&commands
.items
, command_list
[i
].string
)
1103 init_add_i_state(&s
, r
);
1106 * When color was asked for, use the prompt color for
1107 * highlighting, otherwise use square brackets.
1110 data
.color
= s
.prompt_color
;
1111 data
.reset
= s
.reset_color
;
1113 print_file_item_data
.color
= data
.color
;
1114 print_file_item_data
.reset
= data
.reset
;
1116 strbuf_addstr(&header
, " ");
1117 strbuf_addf(&header
, print_file_item_data
.modified_fmt
,
1118 _("staged"), _("unstaged"), _("path"));
1119 opts
.list_opts
.header
= header
.buf
;
1121 if (discard_index(r
->index
) < 0 ||
1122 repo_read_index(r
) < 0 ||
1123 repo_refresh_and_write_index(r
, REFRESH_QUIET
, 0, 1,
1124 NULL
, NULL
, NULL
) < 0)
1125 warning(_("could not refresh index"));
1127 res
= run_status(&s
, ps
, &files
, &opts
);
1130 struct command_item
*util
;
1132 i
= list_and_choose(&s
, &commands
, &main_loop_opts
);
1133 if (i
< 0 || i
>= commands
.items
.nr
)
1136 util
= commands
.items
.items
[i
].util
;
1138 if (i
== LIST_AND_CHOOSE_QUIT
|| (util
&& !util
->command
)) {
1139 printf(_("Bye.\n"));
1145 res
= util
->command(&s
, ps
, &files
, &opts
);
1148 prefix_item_list_clear(&files
);
1149 strbuf_release(&print_file_item_data
.buf
);
1150 strbuf_release(&print_file_item_data
.name
);
1151 strbuf_release(&print_file_item_data
.index
);
1152 strbuf_release(&print_file_item_data
.worktree
);
1153 strbuf_release(&header
);
1154 prefix_item_list_clear(&commands
);