]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Start to implement a built-in version of `git add --interactive`
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Wed, 13 Nov 2019 12:40:57 +0000 (12:40 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 14 Nov 2019 02:10:04 +0000 (11:10 +0900)
Unlike previous conversions to C, where we started with a built-in
helper, we start this conversion by adding an interception in the
`run_add_interactive()` function when the new opt-in
`add.interactive.useBuiltin` config knob is turned on (or the
corresponding environment variable `GIT_TEST_ADD_I_USE_BUILTIN`), and
calling the new internal API function `run_add_i()` that is implemented
directly in libgit.a.

At this point, the built-in version of `git add -i` only states that it
cannot do anything yet. In subsequent patches/patch series, the
`run_add_i()` function will gain more and more functionality, until it
is feature complete. The whole arc of the conversion can be found in the
PRs #170-175 at https://github.com/gitgitgadget/git.

The "--helper approach" can unfortunately not be used here: on Windows
we face the very specific problem that a `system()` call in
Perl seems to close `stdin` in the parent process when the spawned
process consumes even one character from `stdin`. Which prevents us from
implementing the main loop in C and still trying to hand off to the Perl
script.

The very real downside of the approach we have to take here is that the
test suite won't pass with `GIT_TEST_ADD_I_USE_BUILTIN=true` until the
conversion is complete (the `--helper` approach would have let it pass,
even at each of the incremental conversion steps).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config/add.txt
Makefile
add-interactive.c [new file with mode: 0644]
add-interactive.h [new file with mode: 0644]
builtin/add.c
t/README

index 4d753f006ec1efd466b18d74841c8d43039d8ad9..c9f748f81cb1c7c3b4a2cc2298b310ac85cb1d5c 100644 (file)
@@ -5,3 +5,8 @@ add.ignore-errors (deprecated)::
        option of linkgit:git-add[1].  `add.ignore-errors` is deprecated,
        as it does not follow the usual naming convention for configuration
        variables.
+
+add.interactive.useBuiltin::
+       [EXPERIMENTAL] Set to `true` to use the experimental built-in
+       implementation of the interactive version of linkgit:git-add[1]
+       instead of the Perl script version. Is `false` by default.
index 58b92af54b2af48c132bf3d7beb8feaf05a450ed..6c4a1e0ee5c49e08831451209775c3ef106c7dec 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -823,6 +823,7 @@ LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentat
        -name '*.h' -print)))
 
 LIB_OBJS += abspath.o
+LIB_OBJS += add-interactive.o
 LIB_OBJS += advice.o
 LIB_OBJS += alias.o
 LIB_OBJS += alloc.o
diff --git a/add-interactive.c b/add-interactive.c
new file mode 100644 (file)
index 0000000..482e458
--- /dev/null
@@ -0,0 +1,7 @@
+#include "cache.h"
+#include "add-interactive.h"
+
+int run_add_i(struct repository *r, const struct pathspec *ps)
+{
+       die(_("No commands are available in the built-in `git add -i` yet!"));
+}
diff --git a/add-interactive.h b/add-interactive.h
new file mode 100644 (file)
index 0000000..7043b87
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef ADD_INTERACTIVE_H
+#define ADD_INTERACTIVE_H
+
+struct repository;
+struct pathspec;
+int run_add_i(struct repository *r, const struct pathspec *ps);
+
+#endif
index dd18e5c9b67038307401e8d33970d5c424c03cb7..d4686d5218a7b938788e9d78b15125bbd1206b5f 100644 (file)
@@ -20,6 +20,7 @@
 #include "bulk-checkin.h"
 #include "argv-array.h"
 #include "submodule.h"
+#include "add-interactive.h"
 
 static const char * const builtin_add_usage[] = {
        N_("git add [<options>] [--] <pathspec>..."),
@@ -185,6 +186,16 @@ int run_add_interactive(const char *revision, const char *patch_mode,
 {
        int status, i;
        struct argv_array argv = ARGV_ARRAY_INIT;
+       int use_builtin_add_i =
+               git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
+
+       if (!patch_mode) {
+               if (use_builtin_add_i < 0)
+                       git_config_get_bool("add.interactive.usebuiltin",
+                                           &use_builtin_add_i);
+               if (use_builtin_add_i == 1)
+                       return !!run_add_i(the_repository, pathspec);
+       }
 
        argv_array_push(&argv, "add--interactive");
        if (patch_mode)
@@ -319,6 +330,7 @@ static int add_config(const char *var, const char *value, void *cb)
                ignore_add_errors = git_config_bool(var, value);
                return 0;
        }
+
        return git_default_config(var, value, cb);
 }
 
index 60d5b77bccd952ffe8128a62827d7a15e728f614..5132ec83f8590b444cd149582f9bb2d6500303e5 100644 (file)
--- a/t/README
+++ b/t/README
@@ -397,6 +397,10 @@ GIT_TEST_STASH_USE_BUILTIN=<boolean>, when false, disables the
 built-in version of git-stash. See 'stash.useBuiltin' in
 git-config(1).
 
+GIT_TEST_ADD_I_USE_BUILTIN=<boolean>, when true, enables the
+built-in version of git add -i. See 'add.interactive.useBuiltin' in
+git-config(1).
+
 GIT_TEST_INDEX_THREADS=<n> enables exercising the multi-threaded loading
 of the index for the whole test suite by bypassing the default number of
 cache entries and thread minimums. Setting this to 1 will make the