Skip to content

Commit 9dbee64

Browse files
committed
Start to implement a built-in version of git add --interactive
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 <[email protected]>
1 parent ea8effe commit 9dbee64

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

Documentation/config/add.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ add.ignore-errors (deprecated)::
55
option of linkgit:git-add[1]. `add.ignore-errors` is deprecated,
66
as it does not follow the usual naming convention for configuration
77
variables.
8+
9+
add.interactive.useBuiltin::
10+
[EXPERIMENTAL] Set to `true` to use the experimental built-in
11+
implementation of the interactive version of linkgit:git-add[1]
12+
instead of the Perl script version. Is `false` by default.

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ LIB_H := $(sort $(patsubst ./%,%,$(shell git ls-files '*.h' ':!t/' ':!Documentat
823823
-name '*.h' -print)))
824824

825825
LIB_OBJS += abspath.o
826+
LIB_OBJS += add-interactive.o
826827
LIB_OBJS += advice.o
827828
LIB_OBJS += alias.o
828829
LIB_OBJS += alloc.o

add-interactive.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "cache.h"
2+
#include "add-interactive.h"
3+
4+
int run_add_i(struct repository *r, const struct pathspec *ps)
5+
{
6+
die(_("No commands are available in the built-in `git add -i` yet!"));
7+
}

add-interactive.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef ADD_INTERACTIVE_H
2+
#define ADD_INTERACTIVE_H
3+
4+
struct repository;
5+
struct pathspec;
6+
int run_add_i(struct repository *r, const struct pathspec *ps);
7+
8+
#endif

builtin/add.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "bulk-checkin.h"
2121
#include "argv-array.h"
2222
#include "submodule.h"
23+
#include "add-interactive.h"
2324

2425
static const char * const builtin_add_usage[] = {
2526
N_("git add [<options>] [--] <pathspec>..."),
@@ -185,6 +186,11 @@ int run_add_interactive(const char *revision, const char *patch_mode,
185186
{
186187
int status, i;
187188
struct argv_array argv = ARGV_ARRAY_INIT;
189+
int use_builtin_add_i =
190+
git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
191+
192+
if (use_builtin_add_i == 1 && !patch_mode)
193+
return !!run_add_i(the_repository, pathspec);
188194

189195
argv_array_push(&argv, "add--interactive");
190196
if (patch_mode)
@@ -319,6 +325,7 @@ static int add_config(const char *var, const char *value, void *cb)
319325
ignore_add_errors = git_config_bool(var, value);
320326
return 0;
321327
}
328+
322329
return git_default_config(var, value, cb);
323330
}
324331

t/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ GIT_TEST_STASH_USE_BUILTIN=<boolean>, when false, disables the
397397
built-in version of git-stash. See 'stash.useBuiltin' in
398398
git-config(1).
399399

400+
GIT_TEST_ADD_I_USE_BUILTIN=<boolean>, when true, enables the
401+
built-in version of git add -i. See 'add.interactive.useBuiltin' in
402+
git-config(1).
403+
400404
GIT_TEST_INDEX_THREADS=<n> enables exercising the multi-threaded loading
401405
of the index for the whole test suite by bypassing the default number of
402406
cache entries and thread minimums. Setting this to 1 will make the

0 commit comments

Comments
 (0)