Skip to content

Commit e490711

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 cbd10da commit e490711

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

add-interactive.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,47 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
931931
return res;
932932
}
933933

934+
static int run_diff(struct add_i_state *s, const struct pathspec *ps,
935+
struct prefix_item_list *files,
936+
struct list_and_choose_options *opts)
937+
{
938+
int res = 0;
939+
ssize_t count, i;
940+
941+
struct object_id oid;
942+
int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid,
943+
NULL);
944+
if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0)
945+
return -1;
946+
947+
if (!files->items.nr) {
948+
putchar('\n');
949+
return 0;
950+
}
951+
952+
opts->prompt = N_("Review diff");
953+
opts->flags = IMMEDIATE;
954+
count = list_and_choose(s, files, opts);
955+
opts->flags = 0;
956+
if (count >= 0) {
957+
struct argv_array args = ARGV_ARRAY_INIT;
958+
959+
argv_array_pushl(&args, "git", "diff", "-p", "--cached",
960+
oid_to_hex(!is_initial ? &oid :
961+
s->r->hash_algo->empty_tree),
962+
"--", NULL);
963+
for (i = 0; i < files->items.nr; i++)
964+
if (files->selected[i])
965+
argv_array_push(&args,
966+
files->items.items[i].string);
967+
res = run_command_v_opt(args.argv, 0);
968+
argv_array_clear(&args);
969+
}
970+
971+
putchar('\n');
972+
return res;
973+
}
974+
934975
static int run_help(struct add_i_state *s, const struct pathspec *unused_ps,
935976
struct prefix_item_list *unused_files,
936977
struct list_and_choose_options *unused_opts)
@@ -1029,6 +1070,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
10291070
{ "revert", run_revert },
10301071
{ "add untracked", run_add_untracked },
10311072
{ "patch", run_patch },
1073+
{ "diff", run_diff },
10321074
{ "help", run_help },
10331075
};
10341076
struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;

0 commit comments

Comments
 (0)