Skip to content

Commit 1311bb4

Browse files
authored
Merge pull request #450 from telamonian/fix-switch-new-branch
fixes #386: turn off by default overly agressive disabling of create branch/switch branch
2 parents b83568a + eaf6dbd commit 1311bb4

File tree

5 files changed

+37
-36
lines changed

5 files changed

+37
-36
lines changed

schema/plugin.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@
55
"description": "jupyterlab-git settings.",
66
"type": "object",
77
"properties": {
8+
"disableBranchWithChanges": {
9+
"type": "boolean",
10+
"title": "Disable branch with changes",
11+
"description": "Disable all branch operations (new, switch) when there are changed/staged files",
12+
"default": false
13+
},
814
"historyCount": {
915
"type": "integer",
1016
"title": "History count",
1117
"description": "Number of (most recent) commits shown in the history log",
1218
"default": 25
1319
},
14-
"simpleStaging": {
15-
"type": "boolean",
16-
"title": "Simple staging flag",
17-
"description": "If true, use a simplified concept of staging. Only files with changes are shown (instead of showing staged/changed/untracked), and all files with changes will be automatically staged",
18-
"default": false
19-
},
2020
"refreshInterval": {
2121
"type": "integer",
2222
"title": "Refresh interval",
2323
"description": "Number of milliseconds between polling the file system for changes.",
2424
"default": 3000
25+
},
26+
"simpleStaging": {
27+
"type": "boolean",
28+
"title": "Simple staging flag",
29+
"description": "If true, use a simplified concept of staging. Only files with changes are shown (instead of showing staged/changed/untracked), and all files with changes will be automatically staged",
30+
"default": false
2531
}
2632
}
2733
}

src/components/BranchHeader.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,23 @@ export class BranchHeader extends React.Component<
5050

5151
/** Switch current working branch */
5252
async switchBranch(branchName: string) {
53-
await this.props.model.checkout({ branchname: branchName });
53+
const result = await this.props.model.checkout({ branchname: branchName });
54+
if (result.code !== 0) {
55+
showErrorMessage('Error switching branch', result.message);
56+
}
57+
5458
this.toggleSelect();
5559
}
5660

5761
createNewBranch = async (branchName: string) => {
58-
await this.props.model.checkout({
62+
const result = await this.props.model.checkout({
5963
newBranch: true,
6064
branchname: branchName
6165
});
66+
if (result.code !== 0) {
67+
showErrorMessage('Error creating new branch', result.message);
68+
}
69+
6270
this.toggleNewBranchBox();
6371
};
6472

src/components/GitPanel.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export interface IGitSessionNodeState {
2727
stagedFiles: Git.IStatusFileResult[];
2828
unstagedFiles: Git.IStatusFileResult[];
2929
untrackedFiles: Git.IStatusFileResult[];
30-
hasChangedFiles: boolean;
3130

3231
isHistoryVisible: boolean;
3332
}
@@ -51,7 +50,6 @@ export class GitPanel extends React.Component<
5150
branches: [],
5251
currentBranch: '',
5352
upstreamBranch: '',
54-
hasChangedFiles: false,
5553
pastCommits: [],
5654
stagedFiles: [],
5755
unstagedFiles: [],
@@ -114,19 +112,13 @@ export class GitPanel extends React.Component<
114112
let stagedFiles = new Array<Git.IStatusFileResult>();
115113
let unstagedFiles = new Array<Git.IStatusFileResult>();
116114
let untrackedFiles = new Array<Git.IStatusFileResult>();
117-
let changedFiles = 0;
118115
let statusFiles = this.props.model.status;
119116
if (statusFiles.length > 0) {
120117
for (let i = 0; i < statusFiles.length; i++) {
121118
const file = statusFiles[i];
122119
const { x, y } = file;
123120
const stage = decodeStage(x, y);
124121

125-
// If file has been changed
126-
if (x !== '?' && x !== '!') {
127-
changedFiles++;
128-
}
129-
130122
// If file is untracked
131123
if (stage === 'untracked') {
132124
untrackedFiles.push(file);
@@ -141,8 +133,7 @@ export class GitPanel extends React.Component<
141133
this.setState({
142134
stagedFiles: stagedFiles,
143135
unstagedFiles: unstagedFiles,
144-
untrackedFiles: untrackedFiles,
145-
hasChangedFiles: changedFiles > 0
136+
untrackedFiles: untrackedFiles
146137
});
147138
}
148139
};
@@ -206,10 +197,11 @@ export class GitPanel extends React.Component<
206197
upstreamBranch={this.state.upstreamBranch}
207198
stagedFiles={this.state.stagedFiles}
208199
data={this.state.branches}
209-
// No uncommitted changed files, allow switching branches
210-
// No committed files ever, disable switching branches
211200
disabled={
212-
this.state.hasChangedFiles || this.state.pastCommits.length === 0
201+
this.state.pastCommits.length === 0 ||
202+
(this.props.settings.composite[
203+
'disableBranchWithChanges'
204+
] as boolean)
213205
}
214206
toggleSidebar={this.toggleSidebar}
215207
sideBarExpanded={this.state.isHistoryVisible}

src/model.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,19 +601,15 @@ export class GitExtension implements IGitExtension, IDisposable {
601601
* If a filename is provided, check the file out
602602
* If nothing is provided, check all files out
603603
*/
604-
async checkout(options?: Git.ICheckoutOptions): Promise<Response> {
604+
async checkout(options?: Git.ICheckoutOptions): Promise<Git.ICheckoutResult> {
605605
await this.ready;
606606
const path = this.pathRepository;
607607

608608
if (path === null) {
609-
return Promise.resolve(
610-
new Response(
611-
JSON.stringify({
612-
code: -1,
613-
message: 'Not in a git repository.'
614-
})
615-
)
616-
);
609+
return Promise.resolve({
610+
code: -1,
611+
message: 'Not in a git repository.'
612+
});
617613
}
618614

619615
const body = {
@@ -650,7 +646,7 @@ export class GitExtension implements IGitExtension, IDisposable {
650646
} else {
651647
this.refreshStatus();
652648
}
653-
return response;
649+
return response.json();
654650
} catch (err) {
655651
throw new ServerConnection.NetworkError(err);
656652
}

src/tokens.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export interface IGitExtension {
147147
*
148148
* @param options Checkout options
149149
*/
150-
checkout(options?: Git.ICheckoutOptions): Promise<Response>;
150+
checkout(options?: Git.ICheckoutOptions): Promise<Git.ICheckoutResult>;
151151

152152
/**
153153
* Make request to commit all staged files in repository
@@ -300,9 +300,8 @@ export namespace Git {
300300
filename?: string;
301301
}
302302

303-
/** Interface for GitShowPrefix request result,
304-
* has the prefix path of a directory in a repository,
305-
* with respect to the root directory.
303+
/** Interface for GitCheckout request result.
304+
* For reporting errors in checkout
306305
*/
307306
export interface ICheckoutResult {
308307
code: number;
@@ -322,7 +321,7 @@ export namespace Git {
322321
}
323322

324323
/** Interface for GitBranch request result,
325-
* has the result of changing the current working branch
324+
* has the result of fetching info on all branches
326325
*/
327326
export interface IBranchResult {
328327
code: number;

0 commit comments

Comments
 (0)