Skip to content

Commit b78137c

Browse files
author
Marc Udoff
committed
Fix review comments
1 parent 34ca19e commit b78137c

File tree

4 files changed

+91
-58
lines changed

4 files changed

+91
-58
lines changed

src/components/Toolbar.tsx

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Dialog, showDialog } from '@jupyterlab/apputils';
21
import { PathExt } from '@jupyterlab/coreutils';
32
import {
43
caretDownIcon,
@@ -22,52 +21,9 @@ import {
2221
toolbarNavClass
2322
} from '../style/Toolbar';
2423
import { IGitExtension } from '../tokens';
25-
import { GitCredentialsForm } from '../widgets/CredentialsBox';
26-
import { GitPullPushDialog, Operation } from '../widgets/gitPushPull';
2724
import { ActionButton } from './ActionButton';
2825
import { BranchMenu } from './BranchMenu';
29-
30-
/**
31-
* Displays an error dialog when a Git operation fails.
32-
*
33-
* @private
34-
* @param model - Git extension model
35-
* @param operation - Git operation name
36-
* @returns Promise for displaying a dialog
37-
*/
38-
async function showGitOperationDialog(
39-
model: IGitExtension,
40-
operation: Operation
41-
): Promise<void> {
42-
const title = `Git ${operation}`;
43-
let result = await showDialog({
44-
title: title,
45-
body: new GitPullPushDialog(model, operation),
46-
buttons: [Dialog.okButton({ label: 'DISMISS' })]
47-
});
48-
let retry = false;
49-
while (!result.button.accept) {
50-
const credentials = await showDialog({
51-
title: 'Git credentials required',
52-
body: new GitCredentialsForm(
53-
'Enter credentials for remote repository',
54-
retry ? 'Incorrect username or password.' : ''
55-
),
56-
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
57-
});
58-
59-
if (!credentials.button.accept) {
60-
break;
61-
}
62-
63-
result = await showDialog({
64-
title: title,
65-
body: new GitPullPushDialog(model, operation, credentials.value),
66-
buttons: [Dialog.okButton({ label: 'DISMISS' })]
67-
});
68-
retry = true;
69-
}
70-
}
26+
import { CommandIDs } from '../gitMenuCommands';
7127

7228
/**
7329
* Interface describing component properties.
@@ -304,11 +260,10 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
304260
* @param event - event object
305261
*/
306262
private _onPullClick = (): void => {
307-
showGitOperationDialog(this.props.model, Operation.Pull).catch(reason => {
308-
console.error(
309-
`Encountered an error when pulling changes. Error: ${reason}`
310-
);
311-
});
263+
const commands = this.props.model.commands;
264+
if (commands) {
265+
commands.execute(CommandIDs.gitPull);
266+
}
312267
};
313268

314269
/**
@@ -317,11 +272,10 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
317272
* @param event - event object
318273
*/
319274
private _onPushClick = (): void => {
320-
showGitOperationDialog(this.props.model, Operation.Push).catch(reason => {
321-
console.error(
322-
`Encountered an error when pushing changes. Error: ${reason}`
323-
);
324-
});
275+
const commands = this.props.model.commands;
276+
if (commands) {
277+
commands.execute(CommandIDs.gitPush);
278+
}
325279
};
326280

327281
/**

src/gitMenuCommands.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { ISettingRegistry } from '@jupyterlab/settingregistry';
1111
import { ITerminal } from '@jupyterlab/terminal';
1212
import { IGitExtension } from './tokens';
1313
import { doGitClone } from './widgets/gitClone';
14+
import { GitPullPushDialog, Operation } from './widgets/gitPushPull';
15+
import { GitCredentialsForm } from './widgets/CredentialsBox';
1416

1517
/**
1618
* The command IDs used by the git plugin.
@@ -24,6 +26,8 @@ export namespace CommandIDs {
2426
export const gitToggleDoubleClickDiff = 'git:toggle-double-click-diff';
2527
export const gitAddRemote = 'git:add-remote';
2628
export const gitClone = 'git:clone';
29+
export const gitPush = 'git:push';
30+
export const gitPull = 'git:pull';
2731
}
2832

2933
/**
@@ -82,8 +86,8 @@ export function addCommands(
8286

8387
/** Add git init command */
8488
commands.addCommand(CommandIDs.gitInit, {
85-
label: 'Init',
86-
caption: ' Create an empty Git repository or reinitialize an existing one',
89+
label: 'Initialize a Repository',
90+
caption: 'Create an empty Git repository or reinitialize an existing one',
8791
execute: async () => {
8892
const currentPath = fileBrowser.model.path;
8993
const result = await showDialog({
@@ -164,7 +168,7 @@ export function addCommands(
164168

165169
/** Add git clone command */
166170
commands.addCommand(CommandIDs.gitClone, {
167-
label: 'Clone',
171+
label: 'Clone a Repository',
168172
caption: 'Clone a repository from a URL',
169173
isEnabled: () => model.pathRepository === null,
170174
execute: async () => {
@@ -173,4 +177,74 @@ export function addCommands(
173177
},
174178
isVisible: () => model.pathRepository === null
175179
});
180+
181+
/**
182+
* Displays an error dialog when a Git operation fails.
183+
*
184+
* @private
185+
* @param model - Git extension model
186+
* @param operation - Git operation name
187+
* @returns Promise for displaying a dialog
188+
*/
189+
async function showGitOperationDialog(
190+
model: IGitExtension,
191+
operation: Operation
192+
): Promise<void> {
193+
const title = `Git ${operation}`;
194+
let result = await showDialog({
195+
title: title,
196+
body: new GitPullPushDialog(model, operation),
197+
buttons: [Dialog.okButton({ label: 'DISMISS' })]
198+
});
199+
let retry = false;
200+
while (!result.button.accept) {
201+
const credentials = await showDialog({
202+
title: 'Git credentials required',
203+
body: new GitCredentialsForm(
204+
'Enter credentials for remote repository',
205+
retry ? 'Incorrect username or password.' : ''
206+
),
207+
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
208+
});
209+
210+
if (!credentials.button.accept) {
211+
break;
212+
}
213+
214+
result = await showDialog({
215+
title: title,
216+
body: new GitPullPushDialog(model, operation, credentials.value),
217+
buttons: [Dialog.okButton({ label: 'DISMISS' })]
218+
});
219+
retry = true;
220+
}
221+
}
222+
223+
/** Add git push command */
224+
commands.addCommand(CommandIDs.gitPush, {
225+
label: 'Push to Remote',
226+
caption: 'Push code to remote repository',
227+
isVisible: () => model.pathRepository !== null,
228+
execute: async () => {
229+
await showGitOperationDialog(model, Operation.Push).catch(reason => {
230+
console.error(
231+
`Encountered an error when pushing changes. Error: ${reason}`
232+
);
233+
});
234+
}
235+
});
236+
237+
/** Add git pull command */
238+
commands.addCommand(CommandIDs.gitPull, {
239+
label: 'Pull from Remote',
240+
caption: 'Pull latest code from remote repository',
241+
isVisible: () => model.pathRepository !== null,
242+
execute: async () => {
243+
await showGitOperationDialog(model, Operation.Pull).catch(reason => {
244+
console.error(
245+
`Encountered an error when pulling changes. Error: ${reason}`
246+
);
247+
});
248+
}
249+
});
176250
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ function createGitMenu(
145145
[
146146
CommandIDs.gitInit,
147147
CommandIDs.gitClone,
148+
CommandIDs.gitPush,
149+
CommandIDs.gitPull,
148150
CommandIDs.gitAddRemote,
149151
CommandIDs.gitTerminalCommand
150152
].forEach(command => {

src/tokens.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DocumentRegistry } from '@jupyterlab/docregistry';
33
import { Token, JSONObject } from '@lumino/coreutils';
44
import { IDisposable } from '@lumino/disposable';
55
import { ISignal } from '@lumino/signaling';
6+
import { CommandRegistry } from '@lumino/commands';
67

78
export const EXTENSION_ID = 'jupyter.extensions.git_plugin';
89

@@ -57,6 +58,8 @@ export interface IGitExtension extends IDisposable {
5758
*/
5859
readonly statusChanged: ISignal<IGitExtension, Git.IStatusFileResult[]>;
5960

61+
readonly commands: CommandRegistry | null;
62+
6063
/**
6164
* Make request to add one or all files into
6265
* the staging area in repository

0 commit comments

Comments
 (0)