Skip to content

Commit

Permalink
#376 When loading the Repository Settings Widget, no longer require a…
Browse files Browse the repository at this point in the history
… global .gitconfig file to exist.
  • Loading branch information
mhutchie committed Sep 9, 2020
1 parent c4e5507 commit d937c23
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,16 @@ export class DataSource extends Disposable {
* @returns An array of configuration records.
*/
private getConfigList(repo: string, location: GitConfigLocation) {
return this.spawnGit(['--no-pager', 'config', '--list', '--' + location], repo, (stdout) => stdout.split(EOL_REGEX));
return this.spawnGit(['--no-pager', 'config', '--list', '--' + location], repo, (stdout) => stdout.split(EOL_REGEX)).catch((errorMessage) => {
if (typeof errorMessage === 'string') {
const message = errorMessage.toLowerCase();
if (message.startsWith('fatal: unable to read config file') && message.endsWith('no such file or directory')) {
// If the Git command failed due to the configuration file not existing, return an empty list instead of throwing the exception
return <string[]>[];
}
}
throw errorMessage;
});
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/dataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3392,6 +3392,71 @@ describe('DataSource', () => {
expect(spyOnSpawn).toBeCalledWith('/path/to/git', ['remote'], expect.objectContaining({ cwd: '/path/to/repo' }));
});

it('Should return the repositories settings (ignoring Git exception when either the global or local .gitconfig file doesn\'t exist)', async () => {
// Setup
mockGitSuccessOnce(
'user.name=Local Name\n' +
'user.email=local@mhutchie.com\n' +
'remote.origin.url=https://github.com/mhutchie/vscode-git-graph.git\n' +
'remote.origin.pushurl=https://github.com/mhutchie/vscode-git-graph-push.git\n' +
'remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*\n'
);
mockGitThrowingErrorOnce('fatal: unable to read config file \'c:/users/michael/.gitconfig\': no such file or directory');
mockGitSuccessOnce('origin\n');

// Run
const result = await dataSource.getRepoSettings('/path/to/repo');

// Assert
expect(result).toStrictEqual({
settings: {
user: {
name: {
local: 'Local Name',
global: null
},
email: {
local: 'local@mhutchie.com',
global: null
}
},
remotes: [
{
name: 'origin',
url: 'https://github.com/mhutchie/vscode-git-graph.git',
pushUrl: 'https://github.com/mhutchie/vscode-git-graph-push.git'
}
]
},
error: null
});
expect(spyOnSpawn).toBeCalledWith('/path/to/git', ['--no-pager', 'config', '--list', '--local'], expect.objectContaining({ cwd: '/path/to/repo' }));
expect(spyOnSpawn).toBeCalledWith('/path/to/git', ['--no-pager', 'config', '--list', '--global'], expect.objectContaining({ cwd: '/path/to/repo' }));
expect(spyOnSpawn).toBeCalledWith('/path/to/git', ['remote'], expect.objectContaining({ cwd: '/path/to/repo' }));
});

it('Should return an error message thrown by git', async () => {
// Setup
const error = new Error();
mockGitSuccessOnce(
'user.email=local@mhutchie.com\n' +
'remote.origin.url=https://github.com/mhutchie/vscode-git-graph.git\n'
);
spyOnSpawn.mockImplementationOnce(() => {
throw error;
});
mockGitSuccessOnce('origin\n');

// Run
const result = await dataSource.getRepoSettings('/path/to/repo');

// Assert
expect(result).toStrictEqual({
settings: null,
error: error
});
});

it('Should return an error message thrown by git', async () => {
// Setup
mockGitThrowingErrorOnce();
Expand Down

0 comments on commit d937c23

Please sign in to comment.