Skip to content

Commit

Permalink
fix: getConfig always returns null despite values being present in …
Browse files Browse the repository at this point in the history
…configuration

Resolves an issue whereby `(await git.getConfig(key)).value` would always return `null`, despite `(await git.listConfig()).all[key]` having the correct value.
  • Loading branch information
steveukx committed Aug 27, 2021
1 parent e5df878 commit 9fd483a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/lib/responses/ConfigList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function configListParser(text: string): ConfigList {
const config = new ConfigList();

for (const item of configParser(text)) {
config.addValue(item.file, item.key, item.value);
config.addValue(item.file, String(item.key), item.value);
}

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

for (const item of configParser(text)) {
for (const item of configParser(text, key)) {
if (item.key !== key) {
continue;
}
Expand All @@ -87,12 +87,20 @@ function configFilePath(filePath: string): string {
return filePath.replace(/^(file):/, '');
}

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

for (let i = 0, max = lines.length - 1; i < max;) {
const file = configFilePath(lines[i++]);
const [key, value] = splitOn(lines[i++], '\n');

let value = lines[i++];
let key = requestedKey;

if (value.includes('\n')) {
const line = splitOn(value, '\n');
key = line[0];
value = line[1];
}

yield {file, key, value};
}
Expand Down
21 changes: 21 additions & 0 deletions test/integration/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createTestContext, newSimpleGit, setUpInit, SimpleGitTestContext } from '../__fixtures__';
import { GitConfigScope } from '../..';
import { SimpleGit } from '../../typings';

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

it('gets config', async () => {
await git.addConfig('user.name', 'BAZ');
expect((await git.getConfig('user.name')).value).toBe('BAZ');
});

it('lists config', async () => {
await git.addConfig('user.name', 'FOO');

const merged = await git.listConfig();
const global = await git.listConfig(GitConfigScope.global);
const local = await git.listConfig(GitConfigScope.local);

expect(global.all).toEqual(merged.values[merged.files[1]]);
expect(local.all).toEqual(merged.values[merged.files[2]]);

expect(global.all['user.name']).not.toBe('FOO');
expect(merged.all['user.name']).toBe('FOO');
expect(local.all['user.name']).toBe('FOO');
})

it('adds a configuration setting', async () => {
await git.addConfig('user.name', 'FOO BAR');

Expand Down

0 comments on commit 9fd483a

Please sign in to comment.