Skip to content

Commit 9fd483a

Browse files
committed
fix: getConfig always returns null despite values being present in configuration
Resolves an issue whereby `(await git.getConfig(key)).value` would always return `null`, despite `(await git.listConfig()).all[key]` having the correct value.
1 parent e5df878 commit 9fd483a

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/lib/responses/ConfigList.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function configListParser(text: string): ConfigList {
4949
const config = new ConfigList();
5050

5151
for (const item of configParser(text)) {
52-
config.addValue(item.file, item.key, item.value);
52+
config.addValue(item.file, String(item.key), item.value);
5353
}
5454

5555
return config;
@@ -60,7 +60,7 @@ export function configGetParser(text: string, key: string): ConfigGetResult {
6060
const values: string[] = [];
6161
const scopes: Map<string, string[]> = new Map();
6262

63-
for (const item of configParser(text)) {
63+
for (const item of configParser(text, key)) {
6464
if (item.key !== key) {
6565
continue;
6666
}
@@ -87,12 +87,20 @@ function configFilePath(filePath: string): string {
8787
return filePath.replace(/^(file):/, '');
8888
}
8989

90-
function* configParser(text: string) {
90+
function* configParser(text: string, requestedKey: string | null = null) {
9191
const lines = text.split('\0');
9292

9393
for (let i = 0, max = lines.length - 1; i < max;) {
9494
const file = configFilePath(lines[i++]);
95-
const [key, value] = splitOn(lines[i++], '\n');
95+
96+
let value = lines[i++];
97+
let key = requestedKey;
98+
99+
if (value.includes('\n')) {
100+
const line = splitOn(value, '\n');
101+
key = line[0];
102+
value = line[1];
103+
}
96104

97105
yield {file, key, value};
98106
}

test/integration/config.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createTestContext, newSimpleGit, setUpInit, SimpleGitTestContext } from '../__fixtures__';
2+
import { GitConfigScope } from '../..';
23
import { SimpleGit } from '../../typings';
34

45
describe('config', () => {
@@ -16,6 +17,26 @@ describe('config', () => {
1617
return config.split('\n').filter(line => line.includes(test));
1718
}
1819

20+
it('gets config', async () => {
21+
await git.addConfig('user.name', 'BAZ');
22+
expect((await git.getConfig('user.name')).value).toBe('BAZ');
23+
});
24+
25+
it('lists config', async () => {
26+
await git.addConfig('user.name', 'FOO');
27+
28+
const merged = await git.listConfig();
29+
const global = await git.listConfig(GitConfigScope.global);
30+
const local = await git.listConfig(GitConfigScope.local);
31+
32+
expect(global.all).toEqual(merged.values[merged.files[1]]);
33+
expect(local.all).toEqual(merged.values[merged.files[2]]);
34+
35+
expect(global.all['user.name']).not.toBe('FOO');
36+
expect(merged.all['user.name']).toBe('FOO');
37+
expect(local.all['user.name']).toBe('FOO');
38+
})
39+
1940
it('adds a configuration setting', async () => {
2041
await git.addConfig('user.name', 'FOO BAR');
2142

0 commit comments

Comments
 (0)