Skip to content

Commit 499f3f1

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 <johannes.schindelin@gmx.de>
1 parent 0c8a71e commit 499f3f1

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
@@ -927,6 +927,47 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
927927
return res;
928928
}
929929

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

0 commit comments

Comments
 (0)