Skip to content

Commit

Permalink
Backport PR #699 on branch 0.11.x (#702)
Browse files Browse the repository at this point in the history
* Merge pull request #699
  • Loading branch information
fcollonval authored Jul 24, 2020
1 parent 1175534 commit cd2f97f
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 91 deletions.
64 changes: 9 additions & 55 deletions src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import { classes } from 'typestyle';
import { Dialog, showDialog } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';

import {
Expand All @@ -23,52 +22,9 @@ import {
toolbarMenuWrapperClass,
toolbarNavClass
} from '../style/Toolbar';
import { GitCredentialsForm } from '../widgets/CredentialsBox';
import { GitPullPushDialog, Operation } from '../widgets/gitPushPull';
import { IGitExtension } from '../tokens';
import { BranchMenu } from './BranchMenu';

/**
* Displays an error dialog when a Git operation fails.
*
* @private
* @param model - Git extension model
* @param operation - Git operation name
* @returns Promise for displaying a dialog
*/
async function showGitOperationDialog(
model: IGitExtension,
operation: Operation
): Promise<void> {
const title = `Git ${operation}`;
let result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
let retry = false;
while (!result.button.accept) {
const credentials = await showDialog({
title: 'Git credentials required',
body: new GitCredentialsForm(
'Enter credentials for remote repository',
retry ? 'Incorrect username or password.' : ''
),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
});

if (!credentials.button.accept) {
break;
}

result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation, credentials.value),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
retry = true;
}
}
import { CommandIDs } from '../gitMenuCommands';

/**
* Interface describing component properties.
Expand Down Expand Up @@ -326,11 +282,10 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
* @param event - event object
*/
private _onPullClick = (): void => {
showGitOperationDialog(this.props.model, Operation.Pull).catch(reason => {
console.error(
`Encountered an error when pulling changes. Error: ${reason}`
);
});
const commands = this.props.model.commands;
if (commands) {
commands.execute(CommandIDs.gitPull);
}
};

/**
Expand All @@ -339,11 +294,10 @@ export class Toolbar extends React.Component<IToolbarProps, IToolbarState> {
* @param event - event object
*/
private _onPushClick = (): void => {
showGitOperationDialog(this.props.model, Operation.Push).catch(reason => {
console.error(
`Encountered an error when pushing changes. Error: ${reason}`
);
});
const commands = this.props.model.commands;
if (commands) {
commands.execute(CommandIDs.gitPush);
}
};

/**
Expand Down
96 changes: 90 additions & 6 deletions src/gitMenuCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { FileBrowser } from '@jupyterlab/filebrowser';
import { ITerminal } from '@jupyterlab/terminal';
import { IGitExtension } from './tokens';
import { doGitClone } from './widgets/gitClone';
import { GitPullPushDialog, Operation } from './widgets/gitPushPull';
import { GitCredentialsForm } from './widgets/CredentialsBox';

/**
* The command IDs used by the git plugin.
Expand All @@ -24,6 +26,8 @@ export namespace CommandIDs {
export const gitToggleDoubleClickDiff = 'git:toggle-double-click-diff';
export const gitAddRemote = 'git:add-remote';
export const gitClone = 'git:clone';
export const gitPush = 'git:push';
export const gitPull = 'git:pull';
}

/**
Expand Down Expand Up @@ -63,7 +67,8 @@ export function addCommands(
console.error(e);
main.dispose();
}
}
},
isEnabled: () => model.pathRepository !== null
});

/** Add open/go to git interface command */
Expand All @@ -81,8 +86,8 @@ export function addCommands(

/** Add git init command */
commands.addCommand(CommandIDs.gitInit, {
label: 'Init',
caption: ' Create an empty Git repository or reinitialize an existing one',
label: 'Initialize a Repository',
caption: 'Create an empty Git repository or reinitialize an existing one',
execute: async () => {
const currentPath = fileBrowser.model.path;
const result = await showDialog({
Expand All @@ -95,7 +100,8 @@ export function addCommands(
await model.init(currentPath);
model.pathRepository = currentPath;
}
}
},
isEnabled: () => model.pathRepository === null
});

/** Open URL externally */
Expand Down Expand Up @@ -127,7 +133,7 @@ export function addCommands(

/** Command to add a remote Git repository */
commands.addCommand(CommandIDs.gitAddRemote, {
label: 'Add remote repository',
label: 'Add Remote Repository',
caption: 'Add a Git remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async args => {
Expand Down Expand Up @@ -162,12 +168,90 @@ export function addCommands(

/** Add git clone command */
commands.addCommand(CommandIDs.gitClone, {
label: 'Clone',
label: 'Clone a Repository',
caption: 'Clone a repository from a URL',
isEnabled: () => model.pathRepository === null,
execute: async () => {
await doGitClone(model, fileBrowser.model.path);
fileBrowser.model.refresh();
}
});

/** Add git push command */
commands.addCommand(CommandIDs.gitPush, {
label: 'Push to Remote',
caption: 'Push code to remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async () => {
await Private.showGitOperationDialog(model, Operation.Push).catch(
reason => {
console.error(
`Encountered an error when pushing changes. Error: ${reason}`
);
}
);
}
});

/** Add git pull command */
commands.addCommand(CommandIDs.gitPull, {
label: 'Pull from Remote',
caption: 'Pull latest code from remote repository',
isEnabled: () => model.pathRepository !== null,
execute: async () => {
await Private.showGitOperationDialog(model, Operation.Pull).catch(
reason => {
console.error(
`Encountered an error when pulling changes. Error: ${reason}`
);
}
);
}
});
}

/* eslint-disable no-inner-declarations */
namespace Private {
/**
* Displays an error dialog when a Git operation fails.
*
* @private
* @param model - Git extension model
* @param operation - Git operation name
* @returns Promise for displaying a dialog
*/
export async function showGitOperationDialog(
model: IGitExtension,
operation: Operation
): Promise<void> {
const title = `Git ${operation}`;
let result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
let retry = false;
while (!result.button.accept) {
const credentials = await showDialog({
title: 'Git credentials required',
body: new GitCredentialsForm(
'Enter credentials for remote repository',
retry ? 'Incorrect username or password.' : ''
),
buttons: [Dialog.cancelButton(), Dialog.okButton({ label: 'OK' })]
});

if (!credentials.button.accept) {
break;
}

result = await showDialog({
title: title,
body: new GitPullPushDialog(model, operation, credentials.value),
buttons: [Dialog.okButton({ label: 'DISMISS' })]
});
retry = true;
}
}
}
/* eslint-enable no-inner-declarations */
24 changes: 14 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,34 @@ function createGitMenu(
const menu = new Menu({ commands });
menu.title.label = 'Git';
[
CommandIDs.gitUI,
CommandIDs.gitTerminalCommand,
CommandIDs.gitInit,
CommandIDs.gitClone,
CommandIDs.gitAddRemote
CommandIDs.gitPush,
CommandIDs.gitPull,
CommandIDs.gitAddRemote,
CommandIDs.gitTerminalCommand
].forEach(command => {
menu.addItem({ command });
});

menu.addItem({ type: 'separator' });

menu.addItem({ command: CommandIDs.gitToggleSimpleStaging });

menu.addItem({ command: CommandIDs.gitToggleDoubleClickDiff });

menu.addItem({ type: 'separator' });

const tutorial = new Menu({ commands });
tutorial.title.label = ' Tutorial ';
tutorial.title.label = ' Help ';
RESOURCES.map(args => {
tutorial.addItem({
args,
command: CommandIDs.gitOpenUrl
});
});
menu.addItem({ type: 'submenu', submenu: tutorial });

menu.addItem({ type: 'separator' });

menu.addItem({ command: CommandIDs.gitToggleSimpleStaging });

menu.addItem({ command: CommandIDs.gitToggleDoubleClickDiff });
menu.addItem({ type: 'submenu', submenu: tutorial });

return menu;
}
3 changes: 3 additions & 0 deletions src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IChangedArgs } from '@jupyterlab/coreutils';
import { Token, JSONObject } from '@phosphor/coreutils';
import { IDisposable } from '@phosphor/disposable';
import { ISignal } from '@phosphor/signaling';
import { CommandRegistry } from '@phosphor/commands';

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

Expand Down Expand Up @@ -56,6 +57,8 @@ export interface IGitExtension extends IDisposable {
*/
readonly statusChanged: ISignal<IGitExtension, Git.IStatusFileResult[]>;

readonly commands: CommandRegistry | null;

/**
* Make request to add one or all files into
* the staging area in repository
Expand Down
Loading

0 comments on commit cd2f97f

Please sign in to comment.