Skip to content

Commit f2367bf

Browse files
authored
Branch list is not updated after a branch is deleted (#1096)
* Branch list is not updated after a branch is deleted Fixes #833 * Fix unit test and extend interface
1 parent b61ab82 commit f2367bf

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

src/components/BranchMenu.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ export class BranchMenu extends React.Component<
284284
className={hiddenButtonStyle}
285285
icon={trashIcon}
286286
title={this.props.trans.__('Delete this branch locally')}
287-
onClick={(event: React.MouseEvent) => {
287+
onClick={async (event: React.MouseEvent) => {
288288
event.stopPropagation();
289-
this._onDeleteBranch(branch.name);
289+
await this._onDeleteBranch(branch.name);
290290
}}
291291
/>
292292
<ActionButton

src/components/GitPanel.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ export class GitPanel extends React.Component<IGitPanelProps, IGitPanelState> {
186186
nCommitsBehind: model.status.behind
187187
});
188188
}, this);
189+
model.branchesChanged.connect(async () => {
190+
await this.refreshBranches();
191+
}, this);
189192
model.headChanged.connect(async () => {
190-
await this.refreshBranch();
193+
await this.refreshCurrentBranch();
191194
if (this.state.tab === 1) {
192195
this.refreshHistory();
193196
}
@@ -215,11 +218,16 @@ export class GitPanel extends React.Component<IGitPanelProps, IGitPanelState> {
215218
Signal.clearData(this);
216219
}
217220

218-
refreshBranch = async (): Promise<void> => {
221+
refreshBranches = async (): Promise<void> => {
222+
this.setState({
223+
branches: this.props.model.branches
224+
});
225+
};
226+
227+
refreshCurrentBranch = async (): Promise<void> => {
219228
const { currentBranch } = this.props.model;
220229

221230
this.setState({
222-
branches: this.props.model.branches,
223231
currentBranch: currentBranch ? currentBranch.name : 'master'
224232
});
225233
};
@@ -246,7 +254,7 @@ export class GitPanel extends React.Component<IGitPanelProps, IGitPanelState> {
246254
*/
247255
refreshView = async (): Promise<void> => {
248256
if (this.props.model.pathRepository !== null) {
249-
await this.refreshBranch();
257+
await this.refreshBranches();
250258
await this.refreshHistory();
251259
}
252260
};

src/model.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IChangedArgs, PathExt, URLExt } from '@jupyterlab/coreutils';
22
import { IDocumentManager } from '@jupyterlab/docmanager';
33
import { DocumentRegistry } from '@jupyterlab/docregistry';
44
import { ISettingRegistry } from '@jupyterlab/settingregistry';
5-
import { JSONObject } from '@lumino/coreutils';
5+
import { JSONExt, JSONObject } from '@lumino/coreutils';
66
import { Poll } from '@lumino/polling';
77
import { ISignal, Signal } from '@lumino/signaling';
88
import { requestAPI } from './git';
@@ -206,6 +206,13 @@ export class GitExtension implements IGitExtension {
206206
return this._status;
207207
}
208208

209+
/**
210+
* A signal emitted when the branches of the Git repository changes.
211+
*/
212+
get branchesChanged(): ISignal<IGitExtension, void> {
213+
return this._branchesChanged;
214+
}
215+
209216
/**
210217
* A signal emitted when the `HEAD` of the Git repository changes.
211218
*/
@@ -912,7 +919,12 @@ export class GitExtension implements IGitExtension {
912919
this._currentBranch.top_commit !== data.current_branch.top_commit;
913920
}
914921

915-
this._branches = data.branches;
922+
const branchesChanged = !JSONExt.deepEqual(
923+
this._branches as any,
924+
(data.branches ?? []) as any
925+
);
926+
927+
this._branches = data.branches ?? [];
916928
this._currentBranch = data.current_branch;
917929
if (this._currentBranch) {
918930
// Set up the marker obj for the current (valid) repo/branch combination
@@ -921,6 +933,9 @@ export class GitExtension implements IGitExtension {
921933
if (headChanged) {
922934
this._headChanged.emit();
923935
}
936+
if (branchesChanged) {
937+
this._branchesChanged.emit();
938+
}
924939

925940
// Start fetch remotes if the repository has remote branches
926941
const hasRemote = this._branches.some(branch => branch.is_remote_branch);
@@ -930,13 +945,17 @@ export class GitExtension implements IGitExtension {
930945
this._fetchPoll.stop();
931946
}
932947
} catch (error) {
948+
const branchesChanged = this._branches.length > 0;
933949
const headChanged = this._currentBranch !== null;
934950
this._branches = [];
935951
this._currentBranch = null;
936952
this._fetchPoll.stop();
937953
if (headChanged) {
938954
this._headChanged.emit();
939955
}
956+
if (branchesChanged) {
957+
this._branchesChanged.emit();
958+
}
940959

941960
if (!(error instanceof Git.NotInRepository)) {
942961
throw error;
@@ -1610,6 +1629,7 @@ export class GitExtension implements IGitExtension {
16101629
private _selectedHistoryFile: Git.IStatusFile | null = null;
16111630
private _hasDirtyStagedFiles = false;
16121631

1632+
private _branchesChanged = new Signal<IGitExtension, void>(this);
16131633
private _headChanged = new Signal<IGitExtension, void>(this);
16141634
private _markChanged = new Signal<IGitExtension, void>(this);
16151635
private _selectedHistoryFileChanged = new Signal<

src/tokens.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export interface IGitExtension extends IDisposable {
2424
*/
2525
currentBranch: Git.IBranch;
2626

27+
/**
28+
* A signal emitted when the branches of the Git repository changes.
29+
*/
30+
readonly branchesChanged: ISignal<IGitExtension, void>;
31+
2732
/**
2833
* A signal emitted when the `HEAD` of the Git repository changes.
2934
*/

tests/test-components/GitPanel.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ describe('GitPanel', () => {
233233
} as any;
234234
props.model = {
235235
branches: [],
236+
branchesChanged: {
237+
connect: jest.fn()
238+
},
236239
headChanged: {
237240
connect: jest.fn()
238241
},

0 commit comments

Comments
 (0)