Skip to content

Commit 0f923ad

Browse files
committed
Correct showPrefix
1 parent 8be345a commit 0f923ad

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/model.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,15 +857,31 @@ export class GitExtension implements IGitExtension {
857857
* @throws {Git.GitResponseError} If the server response is not ok
858858
* @throws {ServerConnection.NetworkError} If the request cannot be made
859859
*/
860-
async showPrefix(path: string): Promise<Git.IShowPrefixResult> {
861-
return await this._taskHandler.execute<Git.IShowPrefixResult>(
862-
'git:fetch:prefix_path',
863-
async () => {
864-
return await requestAPI<Git.IShowPrefixResult>('show_prefix', 'POST', {
865-
current_path: path
866-
});
860+
async showPrefix(path: string): Promise<string | null> {
861+
try {
862+
const data = await this._taskHandler.execute<Git.IShowPrefixResult>(
863+
'git:fetch:prefix_path',
864+
async () => {
865+
return await requestAPI<Git.IShowPrefixResult>(
866+
'show_prefix',
867+
'POST',
868+
{
869+
current_path: path
870+
}
871+
);
872+
}
873+
);
874+
return data.under_repo_path || null;
875+
} catch (error) {
876+
if (
877+
error instanceof Git.GitResponseError &&
878+
error.response.status === 500 &&
879+
error.json.code === 128
880+
) {
881+
return null;
867882
}
868-
);
883+
throw error;
884+
}
869885
}
870886

871887
/**

src/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export interface IGitExtension extends IDisposable {
383383
* @throws {Git.GitResponseError} If the server response is not ok
384384
* @throws {ServerConnection.NetworkError} If the request cannot be made
385385
*/
386-
showPrefix(path: string): Promise<Git.IShowPrefixResult>;
386+
showPrefix(path: string): Promise<string | null>;
387387

388388
/**
389389
* Get the top level path of repository 'path'

tests/model.spec.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,44 @@ describe('IGitExtension', () => {
119119
});
120120
});
121121

122+
describe('#showPrefix', () => {
123+
it('should return a string if the folder is a git repository', async () => {
124+
const fakeRepo = '/repo';
125+
mockResponses['show_prefix'] = {
126+
body: () => {
127+
return { code: 0, under_repo_path: fakeRepo };
128+
}
129+
};
130+
const topLevel = await model.showPrefix('/repo/cwd');
131+
expect(topLevel).toEqual(fakeRepo);
132+
});
133+
134+
it('should return null if the repository is not a git repository', async () => {
135+
mockResponses['show_prefix'] = {
136+
body: () => {
137+
return { code: 128 };
138+
},
139+
status: 500
140+
};
141+
const topLevel = await model.showPrefix('/repo/cwd');
142+
expect(topLevel).toBeNull();
143+
});
144+
145+
it('should throw an exception if the server otherwise', async () => {
146+
mockResponses['show_prefix'] = {
147+
body: () => {
148+
return { code: 128 };
149+
},
150+
status: 401
151+
};
152+
try {
153+
await model.showPrefix('/repo/cwd');
154+
} catch (error) {
155+
expect(error).toBeInstanceOf(Git.GitResponseError);
156+
}
157+
});
158+
});
159+
122160
describe('#showTopLevel', () => {
123161
it('should return a string if the folder is a git repository', async () => {
124162
const fakeRepo = '/path/to/repo';

0 commit comments

Comments
 (0)