Skip to content

Commit

Permalink
feat: support scope argument in listConfig to return a specific s…
Browse files Browse the repository at this point in the history
…cope's configuration
  • Loading branch information
steveukx committed Jul 31, 2021
1 parent 0712f86 commit 0685a8b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ For type details of the response for each of the tasks, please see the [TypeScri
`true` the configuration setting is appended to rather than overwritten in the local config. Use the `scope` argument
to pick where to save the new configuration setting (use the exported `GitConfigScope` enum, or equivalent string
values - `worktree | local | global | system`).

- `.listConfig()` reads the current configuration and returns a [ConfigListSummary](./src/lib/responses/ConfigList.ts)
- `.listConfig(scope: GitConfigScope)` as with `listConfig` but returns only those items in a specified scope (note that configuration values are overlaid on top of each other to build the config `git` will actually use - to resolve the configuration you are using use `(await listConfig()).all` without the scope argument)

## git hash-object

Expand Down
20 changes: 13 additions & 7 deletions src/lib/tasks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export enum GitConfigScope {
worktree = 'worktree',
}

function asConfigScope(scope: GitConfigScope | unknown): GitConfigScope {
function asConfigScope<T extends GitConfigScope | undefined>(scope: GitConfigScope | unknown, fallback: T): GitConfigScope | T {
if (typeof scope === 'string' && GitConfigScope.hasOwnProperty(scope)) {
return scope as GitConfigScope;
}
return GitConfigScope.local;
return fallback;
}

function addConfigTask(key: string, value: string, append: boolean, scope: GitConfigScope): StringTask<string> {
Expand All @@ -36,9 +36,15 @@ function addConfigTask(key: string, value: string, append: boolean, scope: GitCo
}
}

function listConfigTask(): StringTask<ConfigListSummary> {
function listConfigTask(scope?: GitConfigScope): StringTask<ConfigListSummary> {
const commands = ['config', '--list', '--show-origin', '--null'];

if (scope) {
commands.push(`--${scope}`);
}

return {
commands: ['config', '--list', '--show-origin', '--null'],
commands,
format: 'utf-8',
parser(text: string): any {
return configListParser(text);
Expand All @@ -50,14 +56,14 @@ export default function (): Pick<SimpleGit, 'addConfig' | 'listConfig'> {
return {
addConfig(this: SimpleGitApi, key: string, value: string, ...rest: unknown[]) {
return this._runTask(
addConfigTask(key, value, rest[0] === true, asConfigScope(rest[1])),
addConfigTask(key, value, rest[0] === true, asConfigScope(rest[1], GitConfigScope.local)),
trailingFunctionArgument(arguments),
);
},

listConfig(this: SimpleGitApi) {
listConfig(this: SimpleGitApi, ...rest: unknown[]) {
return this._runTask(
listConfigTask(),
listConfigTask(asConfigScope(rest[0], undefined)),
trailingFunctionArgument(arguments),
);
},
Expand Down
48 changes: 47 additions & 1 deletion test/unit/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SimpleGit } from 'typings';
import { assertExecutedCommands, closeWithSuccess, newSimpleGit } from './__fixtures__';
import { assertExecutedCommands, closeWithSuccess, like, newSimpleGit } from './__fixtures__';
import { GitConfigScope } from '../..';
import { configListParser } from '../../src/lib/responses/ConfigList';

Expand Down Expand Up @@ -88,4 +88,50 @@ describe('config', () => {
assertExecutedCommands('config', expected, 'key', 'value');
});

it('lists', async () => {
const queue = git.listConfig();
await closeWithSuccess(`file:/Users/me/.gitconfig\0user.email
steve@mydev.co\0file:/Users/me/.gitconfig\0init.defaultbranch
main\0file:.git/config\0core.bare
false\0file:.git/config\0user.email
custom@mydev.co\0`);

assertExecutedCommands('config', '--list', '--show-origin', '--null')
expect(await queue).toEqual(like({
files: [
'/Users/me/.gitconfig',
'.git/config',
],
all: {
'user.email': 'custom@mydev.co',
'init.defaultbranch': 'main',
'core.bare': 'false',
},
values: {
'/Users/me/.gitconfig': {
'user.email': 'steve@mydev.co',
'init.defaultbranch': 'main',
},
'.git/config': {
'user.email': 'custom@mydev.co',
'core.bare': 'false',
},
}
}))
});

it('lists with string scope', async () => {
git.listConfig('local');
await closeWithSuccess();

assertExecutedCommands('config', '--list', '--show-origin', '--null', '--local');
});

it('lists with scope', async () => {
git.listConfig(GitConfigScope.system);
await closeWithSuccess();

assertExecutedCommands('config', '--list', '--show-origin', '--null', '--system');
});

});
2 changes: 2 additions & 0 deletions typings/simple-git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export interface SimpleGit extends SimpleGitBase {
/**
* Configuration values visible to git in the current working directory
*/
listConfig(scope: types.GitConfigScope | string, callback?: types.SimpleGitTaskCallback<resp.ConfigListSummary>): Response<resp.ConfigListSummary>;

listConfig(callback?: types.SimpleGitTaskCallback<resp.ConfigListSummary>): Response<resp.ConfigListSummary>;

/**
Expand Down

0 comments on commit 0685a8b

Please sign in to comment.