Skip to content

Commit c7164e7

Browse files
committed
feat: allow setting the scope of git config add to work on the local, global or system configuration.
``` git.addConfig('user.name', 'My Name', false, GitConfigScope.global) ```
1 parent 9ced5e6 commit c7164e7

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

readme.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ For type details of the response for each of the tasks, please see the [TypeScri
236236

237237
## git config
238238

239-
- `.addConfig(key, value, append = false)` add a local configuration property, when `append` is set to `true` the
240-
configuration setting is appended to rather than set in the local config.
239+
- `.addConfig(key, value, append = false, scope = 'local')` add a local configuration property, when `append` is set to
240+
`true` the configuration setting is appended to rather than overwritten in the local config. Use the `scope` argument
241+
to pick where to save the new configuration setting (use the exported `GitConfigScope` enum, or equivalent string
242+
values - `worktree | local | global | system`).
241243
- `.listConfig()` reads the current configuration and returns a [ConfigListSummary](./src/lib/responses/ConfigList.ts)
242244

243245
## git hash-object

src/git.js

-24
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ const {branchTask, branchLocalTask, deleteBranchesTask, deleteBranchTask} = requ
2020
const {checkIgnoreTask} = require('./lib/tasks/check-ignore');
2121
const {checkIsRepoTask} = require('./lib/tasks/check-is-repo');
2222
const {cloneTask, cloneMirrorTask} = require('./lib/tasks/clone');
23-
const {addConfigTask, listConfigTask} = require('./lib/tasks/config');
2423
const {cleanWithOptionsTask, isCleanOptionsArray} = require('./lib/tasks/clean');
2524
const {commitTask} = require('./lib/tasks/commit');
2625
const {diffSummaryTask} = require('./lib/tasks/diff');
2726
const {fetchTask} = require('./lib/tasks/fetch');
28-
const {hashObjectTask} = require('./lib/tasks/hash-object');
29-
const {initTask} = require('./lib/tasks/init');
3027
const {logTask, parseLogOptions} = require('./lib/tasks/log');
31-
const {mergeTask} = require('./lib/tasks/merge');
3228
const {moveTask} = require("./lib/tasks/move");
3329
const {pullTask} = require('./lib/tasks/pull');
3430
const {pushTagsTask} = require('./lib/tasks/push');
3531
const {addRemoteTask, getRemotesTask, listRemotesTask, remoteTask, removeRemoteTask} = require('./lib/tasks/remote');
3632
const {getResetMode, resetTask} = require('./lib/tasks/reset');
3733
const {stashListTask} = require('./lib/tasks/stash-list');
38-
const {statusTask} = require('./lib/tasks/status');
3934
const {addSubModuleTask, initSubModuleTask, subModuleTask, updateSubModuleTask} = require('./lib/tasks/sub-module');
4035
const {addAnnotatedTagTask, addTagTask, tagListTask} = require('./lib/tasks/tag');
4136
const {straightThroughBufferTask, straightThroughStringTask} = require('./lib/tasks/task');
@@ -376,25 +371,6 @@ Git.prototype.branchLocal = function (then) {
376371
);
377372
};
378373

379-
/**
380-
* Add config to local git instance
381-
*
382-
* @param {string} key configuration key (e.g user.name)
383-
* @param {string} value for the given key (e.g your name)
384-
* @param {boolean} [append=false] optionally append the key/value pair (equivalent of passing `--add` option).
385-
* @param {Function} [then]
386-
*/
387-
Git.prototype.addConfig = function (key, value, append, then) {
388-
return this._runTask(
389-
addConfigTask(key, value, typeof append === "boolean" ? append : false),
390-
trailingFunctionArgument(arguments),
391-
);
392-
};
393-
394-
Git.prototype.listConfig = function () {
395-
return this._runTask(listConfigTask(), trailingFunctionArgument(arguments));
396-
};
397-
398374
/**
399375
* Executes any command against the git binary.
400376
*/

src/lib/api.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { GitResponseError } from './errors/git-response-error';
55
import { TaskConfigurationError } from './errors/task-configuration-error';
66
import { CheckRepoActions } from './tasks/check-is-repo';
77
import { CleanOptions } from './tasks/clean';
8+
import { GitConfigScope } from './tasks/config';
89
import { ResetMode } from './tasks/reset';
910

1011
const api = {
1112
CheckRepoActions,
1213
CleanOptions,
14+
GitConfigScope,
1315
GitConstructError,
1416
GitError,
1517
GitPluginError,

src/lib/simple-git-api.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SimpleGitBase } from '../../typings';
22
import { taskCallback } from './task-callback';
33
import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
4+
import config from './tasks/config';
45
import { hashObjectTask } from './tasks/hash-object';
56
import { initTask } from './tasks/init';
67
import { mergeTask } from './tasks/merge';
@@ -15,7 +16,7 @@ export class SimpleGitApi implements SimpleGitBase {
1516
constructor(private _executor: SimpleGitExecutor) {
1617
}
1718

18-
private _runTask<T>(task: SimpleGitTask<T>, then?: SimpleGitTaskCallback<T>) {
19+
protected _runTask<T>(task: SimpleGitTask<T>, then?: SimpleGitTaskCallback<T>) {
1920
const chain = this._executor.chain();
2021
const promise = chain.push(task);
2122

@@ -119,3 +120,5 @@ export class SimpleGitApi implements SimpleGitBase {
119120
return this._runTask(statusTask(getTrailingOptions(arguments)), trailingFunctionArgument(arguments));
120121
}
121122
}
123+
124+
Object.assign(SimpleGitApi.prototype, config());

src/lib/tasks/config.ts

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
import { ConfigListSummary } from '../../../typings';
2-
import { StringTask } from '../types';
1+
import { ConfigListSummary, SimpleGit } from '../../../typings';
32
import { configListParser } from '../responses/ConfigList';
3+
import { SimpleGitApi } from '../simple-git-api';
4+
import { StringTask } from '../types';
5+
import { trailingFunctionArgument } from '../utils';
6+
7+
export enum GitConfigScope {
8+
system = 'system',
9+
global = 'global',
10+
local = 'local',
11+
worktree = 'worktree',
12+
}
13+
14+
function asConfigScope(scope: GitConfigScope | unknown): GitConfigScope {
15+
if (typeof scope === 'string' && GitConfigScope.hasOwnProperty(scope)) {
16+
return scope as GitConfigScope;
17+
}
18+
return GitConfigScope.local;
19+
}
420

5-
export function addConfigTask(key: string, value: string, append = false): StringTask<string> {
6-
const commands: string[] = ['config', '--local'];
21+
function addConfigTask(key: string, value: string, append: boolean, scope: GitConfigScope): StringTask<string> {
22+
const commands: string[] = ['config', `--${scope}`];
723

824
if (append) {
925
commands.push('--add');
@@ -20,7 +36,7 @@ export function addConfigTask(key: string, value: string, append = false): Strin
2036
}
2137
}
2238

23-
export function listConfigTask(): StringTask<ConfigListSummary> {
39+
function listConfigTask(): StringTask<ConfigListSummary> {
2440
return {
2541
commands: ['config', '--list', '--show-origin', '--null'],
2642
format: 'utf-8',
@@ -29,3 +45,21 @@ export function listConfigTask(): StringTask<ConfigListSummary> {
2945
},
3046
}
3147
}
48+
49+
export default function (): Pick<SimpleGit, 'addConfig' | 'listConfig'> {
50+
return {
51+
addConfig(this: SimpleGitApi, key: string, value: string, ...rest: unknown[]) {
52+
return this._runTask(
53+
addConfigTask(key, value, rest[0] === true, asConfigScope(rest[1])),
54+
trailingFunctionArgument(arguments),
55+
);
56+
},
57+
58+
listConfig(this: SimpleGitApi) {
59+
return this._runTask(
60+
listConfigTask(),
61+
trailingFunctionArgument(arguments),
62+
);
63+
},
64+
};
65+
}

test/__fixtures__/setup-init.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { SimpleGitTestContext } from './create-test-context';
44
export const GIT_USER_NAME = 'Simple Git Tests';
55
export const GIT_USER_EMAIL = 'tests@simple-git.dev';
66

7-
export async function setUpInit ({git}: SimpleGitTestContext, bare = false) {
8-
await git.init(bare);
7+
export async function setUpInit ({git}: SimpleGitTestContext) {
8+
await git.raw('-c', 'init.defaultbranch=master', 'init');
99
await configureGitCommitter(git);
1010
}
1111

test/unit/config.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SimpleGit } from 'typings';
22
import { assertExecutedCommands, closeWithSuccess, newSimpleGit } from './__fixtures__';
3+
import { GitConfigScope } from '../..';
34
import { configListParser } from '../../src/lib/responses/ConfigList';
45

56
describe('config list parser', () => {
@@ -73,4 +74,18 @@ describe('config', () => {
7374
closeWithSuccess();
7475
}));
7576

77+
it.each([
78+
['system', '--system'],
79+
['global', '--global'],
80+
['local', '--local'],
81+
['worktree', '--worktree'],
82+
['blah', '--local'],
83+
[GitConfigScope.global, '--global'],
84+
])('allows custom scope scope: %s', async (scope, expected) => {
85+
git.addConfig('key', 'value', false, scope as GitConfigScope);
86+
await closeWithSuccess();
87+
88+
assertExecutedCommands('config', expected, 'key', 'value');
89+
});
90+
7691
});

typings/simple-git.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export interface SimpleGit extends SimpleGitBase {
118118
* Add config to local git instance for the specified `key` (eg: user.name) and value (eg: 'your name').
119119
* Set `append` to true to append to rather than overwrite the key
120120
*/
121+
addConfig(key: string, value: string, append?: boolean, scope?: 'system' | 'global' | 'local' | 'worktree', callback?: types.SimpleGitTaskCallback<string>): Response<string>;
122+
121123
addConfig(key: string, value: string, append?: boolean, callback?: types.SimpleGitTaskCallback<string>): Response<string>;
122124

123125
addConfig(key: string, value: string, callback?: types.SimpleGitTaskCallback<string>): Response<string>;

typings/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export {
88
export { CheckRepoActions } from '../src/lib/tasks/check-is-repo';
99
export { CleanOptions, CleanMode } from '../src/lib/tasks/clean';
1010
export { CloneOptions } from '../src/lib/tasks/clone';
11+
export { GitConfigScope } from '../src/lib/tasks/config';
1112
export { ApplyOptions } from '../src/lib/tasks/apply-patch';
1213
export { ResetOptions, ResetMode } from '../src/lib/tasks/reset';

0 commit comments

Comments
 (0)