Skip to content

Commit 8be345a

Browse files
committed
Fix path repository refresh bug
1 parent 5b0db68 commit 8be345a

File tree

4 files changed

+71
-35
lines changed

4 files changed

+71
-35
lines changed

src/model.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,18 @@ export class GitExtension implements IGitExtension {
137137
const currentReady = this._readyPromise;
138138
this._pendingReadyPromise += 1;
139139
this._readyPromise = Promise.all([currentReady, this.showTopLevel(v)])
140-
.then(r => {
141-
const results = r[1];
142-
if (results.code === 0) {
143-
this._pathRepository = results.top_repo_path;
144-
change.newValue = results.top_repo_path;
145-
} else {
146-
this._pathRepository = null;
147-
}
140+
.then(([_, path]) => {
141+
change.newValue = this._pathRepository = path;
148142

149143
if (change.newValue !== change.oldValue) {
150144
this.refresh().then(() => this._repositoryChanged.emit(change));
151145
}
146+
this._pendingReadyPromise -= 1;
152147
})
153148
.catch(reason => {
149+
this._pendingReadyPromise -= 1;
154150
console.error(`Fail to find Git top level for path ${v}.\n${reason}`);
155151
});
156-
157-
void this._readyPromise.then(() => {
158-
this._pendingReadyPromise -= 1;
159-
});
160152
}
161153
}
162154

@@ -885,19 +877,31 @@ export class GitExtension implements IGitExtension {
885877
* @throws {Git.GitResponseError} If the server response is not ok
886878
* @throws {ServerConnection.NetworkError} If the request cannot be made
887879
*/
888-
async showTopLevel(path: string): Promise<Git.IShowTopLevelResult> {
889-
return await this._taskHandler.execute<Git.IShowTopLevelResult>(
890-
'git:fetch:top_level_path',
891-
async () => {
892-
return await requestAPI<Git.IShowTopLevelResult>(
893-
'show_top_level',
894-
'POST',
895-
{
896-
current_path: path
897-
}
898-
);
880+
async showTopLevel(path: string): Promise<string | null> {
881+
try {
882+
const data = await this._taskHandler.execute<Git.IShowTopLevelResult>(
883+
'git:fetch:top_level_path',
884+
async () => {
885+
return await requestAPI<Git.IShowTopLevelResult>(
886+
'show_top_level',
887+
'POST',
888+
{
889+
current_path: path
890+
}
891+
);
892+
}
893+
);
894+
return data.top_repo_path || null;
895+
} catch (error) {
896+
if (
897+
error instanceof Git.GitResponseError &&
898+
error.response.status === 500 &&
899+
error.json.code === 128
900+
) {
901+
return null;
899902
}
900-
);
903+
throw error;
904+
}
901905
}
902906

903907
/**

src/tokens.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export interface IGitExtension extends IDisposable {
374374
revertCommit(message: string, hash: string): Promise<void>;
375375

376376
/**
377-
* Make request for the prefix path of a directory 'path',
377+
* Get the prefix path of a directory 'path',
378378
* with respect to the root directory of repository
379379
*
380380
* @param path Path for which the prefix is searched for
@@ -386,14 +386,14 @@ export interface IGitExtension extends IDisposable {
386386
showPrefix(path: string): Promise<Git.IShowPrefixResult>;
387387

388388
/**
389-
* Make request for top level path of repository 'path'
389+
* Get the top level path of repository 'path'
390390
*
391391
* @param path Path from which the top Git repository needs to be found
392392
*
393393
* @throws {Git.GitResponseError} If the server response is not ok
394394
* @throws {ServerConnection.NetworkError} If the request cannot be made
395395
*/
396-
showTopLevel(path: string): Promise<Git.IShowTopLevelResult>;
396+
showTopLevel(path: string): Promise<string | null>;
397397

398398
/**
399399
* Make request to list all the tags present in the remote repo

tests/GitExtension.spec.tsx renamed to 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('#showTopLevel', () => {
123+
it('should return a string if the folder is a git repository', async () => {
124+
const fakeRepo = '/path/to/repo';
125+
mockResponses['show_top_level'] = {
126+
body: () => {
127+
return { code: 0, top_repo_path: fakeRepo };
128+
}
129+
};
130+
const topLevel = await model.showTopLevel('/path/to/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_top_level'] = {
136+
body: () => {
137+
return { code: 128 };
138+
},
139+
status: 500
140+
};
141+
const topLevel = await model.showTopLevel('/path/to/repo/cwd');
142+
expect(topLevel).toBeNull();
143+
});
144+
145+
it('should throw an exception if the server otherwise', async () => {
146+
mockResponses['show_top_level'] = {
147+
body: () => {
148+
return { code: 128 };
149+
},
150+
status: 401
151+
};
152+
try {
153+
await model.showTopLevel('/path/to/repo/cwd');
154+
} catch (error) {
155+
expect(error).toBeInstanceOf(Git.GitResponseError);
156+
}
157+
});
158+
});
159+
122160
describe('#status', () => {
123161
it('should be an empty list if not in a git repository', async () => {
124162
let status: Git.IStatusFileResult[] = [];

tests/plugin.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { ISettingRegistry, SettingRegistry } from '@jupyterlab/settingregistry';
66
import { JupyterLab } from '@jupyterlab/application';
77
import { showErrorMessage } from '@jupyterlab/apputils';
88
import { URLExt } from '@jupyterlab/coreutils';
9-
import { ReadonlyJSONObject } from '@lumino/coreutils';
10-
import { mockedRequestAPI } from './utils';
9+
import { IMockedResponses, mockedRequestAPI } from './utils';
1110

1211
jest.mock('../src/git');
1312
jest.mock('@jupyterlab/application');
@@ -18,12 +17,7 @@ describe('plugin', () => {
1817
const mockGit = git as jest.Mocked<typeof git>;
1918
const fakeRoot = '/path/to/server';
2019
let app: jest.Mocked<JupyterLab>;
21-
let mockResponses: {
22-
[url: string]: {
23-
body?: (request: Object) => ReadonlyJSONObject;
24-
status?: number;
25-
};
26-
} = {};
20+
let mockResponses: IMockedResponses = {};
2721
let settingRegistry: jest.Mocked<ISettingRegistry>;
2822

2923
beforeAll(() => {

0 commit comments

Comments
 (0)