diff --git a/src/model.ts b/src/model.ts index 926bf40db..61eb42b76 100644 --- a/src/model.ts +++ b/src/model.ts @@ -857,15 +857,31 @@ export class GitExtension implements IGitExtension { * @throws {Git.GitResponseError} If the server response is not ok * @throws {ServerConnection.NetworkError} If the request cannot be made */ - async showPrefix(path: string): Promise { - return await this._taskHandler.execute( - 'git:fetch:prefix_path', - async () => { - return await requestAPI('show_prefix', 'POST', { - current_path: path - }); + async showPrefix(path: string): Promise { + try { + const data = await this._taskHandler.execute( + 'git:fetch:prefix_path', + async () => { + return await requestAPI( + 'show_prefix', + 'POST', + { + current_path: path + } + ); + } + ); + return data.under_repo_path || null; + } catch (error) { + if ( + error instanceof Git.GitResponseError && + error.response.status === 500 && + error.json.code === 128 + ) { + return null; } - ); + throw error; + } } /** diff --git a/src/tokens.ts b/src/tokens.ts index 8ef0a2081..def8d9068 100644 --- a/src/tokens.ts +++ b/src/tokens.ts @@ -383,7 +383,7 @@ export interface IGitExtension extends IDisposable { * @throws {Git.GitResponseError} If the server response is not ok * @throws {ServerConnection.NetworkError} If the request cannot be made */ - showPrefix(path: string): Promise; + showPrefix(path: string): Promise; /** * Get the top level path of repository 'path' diff --git a/tests/model.spec.tsx b/tests/model.spec.tsx index 6545c564f..27cbe302e 100644 --- a/tests/model.spec.tsx +++ b/tests/model.spec.tsx @@ -119,6 +119,44 @@ describe('IGitExtension', () => { }); }); + describe('#showPrefix', () => { + it('should return a string if the folder is a git repository', async () => { + const fakeRepo = '/repo'; + mockResponses['show_prefix'] = { + body: () => { + return { code: 0, under_repo_path: fakeRepo }; + } + }; + const topLevel = await model.showPrefix('/repo/cwd'); + expect(topLevel).toEqual(fakeRepo); + }); + + it('should return null if the repository is not a git repository', async () => { + mockResponses['show_prefix'] = { + body: () => { + return { code: 128 }; + }, + status: 500 + }; + const topLevel = await model.showPrefix('/repo/cwd'); + expect(topLevel).toBeNull(); + }); + + it('should throw an exception if the server otherwise', async () => { + mockResponses['show_prefix'] = { + body: () => { + return { code: 128 }; + }, + status: 401 + }; + try { + await model.showPrefix('/repo/cwd'); + } catch (error) { + expect(error).toBeInstanceOf(Git.GitResponseError); + } + }); + }); + describe('#showTopLevel', () => { it('should return a string if the folder is a git repository', async () => { const fakeRepo = '/path/to/repo';