Skip to content

Commit dc66d6c

Browse files
committed
built-in add -i: re-implement the diff command
It is not only laziness that we simply spawn `git diff -p --cached` here: this command needs to use the pager, and the pager needs to exit when the diff is done. Currently we do not have any way to make that happen if we run the diff in-process. So let's just spawn. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c6b4de4 commit dc66d6c

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

add-interactive.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,51 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
852852
return res;
853853
}
854854

855+
static int run_diff(struct add_i_state *s, const struct pathspec *ps,
856+
struct file_list *files,
857+
struct list_and_choose_options *opts)
858+
{
859+
struct prefix_item **items = (struct prefix_item **)files->file;
860+
int res = 0, *selected = NULL;
861+
ssize_t count, i;
862+
863+
struct object_id oid;
864+
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid,
865+
NULL);
866+
reset_file_list(files);
867+
if (get_modified_files(s->r, INDEX_ONLY, NULL, NULL, files, ps) < 0)
868+
return -1;
869+
870+
if (!files->nr) {
871+
putchar('\n');
872+
return 0;
873+
}
874+
875+
opts->prompt = N_("Review diff");
876+
CALLOC_ARRAY(selected, files->nr);
877+
878+
opts->flags = IMMEDIATE;
879+
count = list_and_choose(items, selected, files->nr, s, opts);
880+
opts->flags = 0;
881+
if (count >= 0) {
882+
struct argv_array args = ARGV_ARRAY_INIT;
883+
884+
argv_array_pushl(&args, "git", "diff", "-p", "--cached",
885+
oid_to_hex(!is_initial ? &oid :
886+
s->r->hash_algo->empty_tree),
887+
"--", NULL);
888+
for (i = 0; i < files->nr; i++)
889+
if (selected[i])
890+
argv_array_push(&args, items[i]->name);
891+
res = run_command_v_opt(args.argv, 0);
892+
argv_array_clear(&args);
893+
}
894+
895+
putchar('\n');
896+
free(selected);
897+
return res;
898+
}
899+
855900
static int run_help(struct add_i_state *s, const struct pathspec *ps,
856901
struct file_list *files,
857902
struct list_and_choose_options *opts)
@@ -946,10 +991,11 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
946991
revert = { { "revert" }, run_revert },
947992
add_untracked = { { "add untracked" }, run_add_untracked },
948993
patch = { { "patch" }, run_patch },
994+
diff = { { "diff" }, run_diff },
949995
help = { { "help" }, run_help };
950996
struct command_item *commands[] = {
951997
&status, &update, &revert, &add_untracked,
952-
&patch, &help
998+
&patch, &diff, &help
953999
};
9541000

9551001
struct print_file_item_data print_file_item_data = {

0 commit comments

Comments
 (0)