Skip to content

Commit 465a969

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d86921 commit 465a969

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

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

0 commit comments

Comments
 (0)