Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ When passing options objects, the following special keys are supported:
- `$nullOnError`: Return null instead of throwing on error
- `$onStdout`: Callback for stdout in spawn mode
- `$onStderr`: Callback for stderr in spawn mode
- `$env`: Object of environment variables to set on the spawned process
- `$config`: Object of `key=value` config overrides injected as `-c` flags before the subcommand (e.g. `{ $config: { 'gc.auto': '0' } }`)

### Common Methods

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ declare module 'git-client' {
$cwd?: string;
/** Custom environment variables */
$env?: Record<string, string>;
/** Inject `-c key=value` config overrides before the subcommand */
$config?: Record<string, string>;
/** Additional options */
$options?: Record<string, any>;
/** Any git command options */
Expand Down
15 changes: 15 additions & 0 deletions lib/Git.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ class Git {
delete arg.$env;
}

if ('$config' in arg) {
execOptions.config = Object.assign(execOptions.config || {}, arg.$config);
delete arg.$config;
}

if ('$preserveEnv' in arg) {
execOptions.preserveEnv = arg.$preserveEnv;
delete arg.$preserveEnv;
Expand Down Expand Up @@ -765,6 +770,16 @@ class Git {
gitOptions['work-tree'] = execOptions.workTree;
}

if (execOptions.config) {
const configArgs = [];
for (const key in execOptions.config) {
configArgs.push(`${key}=${execOptions.config[key]}`);
}
if (configArgs.length) {
gitOptions['c'] = configArgs;
}
}

commandArgs.unshift.apply(commandArgs, Git.cliOptionsToArgs(gitOptions));


Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions test/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ test('other git executes with correct git dir with override', async t => {
t.is(await fs.realpath(gitDir), repo1Dir);
});

test('$config injects -c <key>=<value> before subcommand', async t => {
// Read back a config value that only exists via $config injection
const value = await git.config(
{ $config: { 'holo.injected': 'yes' } },
'--get',
'holo.injected',
);

t.is(value, 'yes');
});

test('$config supports multiple entries', async t => {
const first = await git.config(
{ $config: { 'holo.first': '1', 'holo.second': '2' } },
'--get',
'holo.first',
);
const second = await git.config(
{ $config: { 'holo.first': '1', 'holo.second': '2' } },
'--get',
'holo.second',
);

t.is(first, '1');
t.is(second, '2');
});

test('checkout git repo to temporary directory', async t => {
const [tmpWorkTree, tmpIndexFilePath] = await Promise.all([tmp.dir(), tmp.tmpName()]);

Expand Down
Loading