@matheusbsbs / matheus-bagliano / commits / d7633578b5
Fork 0

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Nov 29, 2019 at 21:11 UTC d7633578b5ecf0d75e2793b01aa2e9afe645c186
1 file changed +42
add-interactive.c
+42
@@ -931,6 +931,47 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
931 return res;
932 }
933
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 +
975 static int run_help(struct add_i_state *s, const struct pathspec *unused_ps,
976 struct prefix_item_list *unused_files,
977 struct list_and_choose_options *unused_opts)
@@ -1029,6 +1070,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
1070 { "revert", run_revert },
1071 { "add untracked", run_add_untracked },
1072 { "patch", run_patch },
1073 + { "diff", run_diff },
1074 { "help", run_help },
1075 };
1076 struct prefix_item_list commands = PREFIX_ITEM_LIST_INIT;