|
| 1 | +#include "config_overrides.hpp" |
| 2 | + |
| 3 | +#ifdef EMSCRIPTEN |
| 4 | +# include <iterator> |
| 5 | + |
| 6 | +# include <git2/sys/config.h> |
| 7 | + |
| 8 | +# include "git_exception.hpp" |
| 9 | +#endif |
| 10 | + |
| 11 | +void apply_config_overrides([[maybe_unused]] git_repository* repo) |
| 12 | +{ |
| 13 | +#ifdef EMSCRIPTEN |
| 14 | + // Config values to override, each in "name=value" form. core.filemode is disabled because |
| 15 | + // the emscripten filesystem does support executable file permissions but some JupyterLite |
| 16 | + // drive implementations do not. |
| 17 | + const char* values[] = {"core.filemode=false"}; |
| 18 | + |
| 19 | + // Create an in-memory config backend holding these values. This backend is read-only, so |
| 20 | + // any config writes (e.g. `git config` or values set by clone/init) skip it and go to the |
| 21 | + // highest-priority writable backend instead, which is the repository's own config file. |
| 22 | + // The overrides are therefore never persisted to disk. backend_type is a label that |
| 23 | + // identifies where these entries came from when inspecting a config entry's origin. |
| 24 | + git_config_backend* backend = nullptr; |
| 25 | + git_config_backend_memory_options opts = GIT_CONFIG_BACKEND_MEMORY_OPTIONS_INIT; |
| 26 | + opts.backend_type = "git2cpp-overrides"; |
| 27 | + throw_if_error(git_config_backend_from_values(&backend, values, std::size(values), &opts)); |
| 28 | + |
| 29 | + // git_repository_config returns the repository's cached, shared config object rather than a |
| 30 | + // copy, so adding a backend to it affects all later config lookups through this repository. |
| 31 | + // GIT_CONFIG_LEVEL_APP is the highest priority level, above the local, global and system |
| 32 | + // config files, so these values win over any existing setting. The repo is passed so that |
| 33 | + // conditional includes can be evaluated, and force=0 means fail rather than replace if a |
| 34 | + // backend already exists at this level. |
| 35 | + git_config* cfg = nullptr; |
| 36 | + int error = git_repository_config(&cfg, repo); |
| 37 | + if (error == 0) |
| 38 | + { |
| 39 | + error = git_config_add_backend(cfg, backend, GIT_CONFIG_LEVEL_APP, repo, 0); |
| 40 | + // Only releases our reference; the repository still holds the config and its backends. |
| 41 | + git_config_free(cfg); |
| 42 | + } |
| 43 | + if (error < 0) |
| 44 | + { |
| 45 | + // On success ownership of the backend passes to the config, but on failure it is still |
| 46 | + // ours to free. |
| 47 | + backend->free(backend); |
| 48 | + throw_if_error(error); |
| 49 | + } |
| 50 | +#endif |
| 51 | +} |
0 commit comments