Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New context menu action to restore a commit #703

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@
"type": "boolean",
"title": "Create Branch..."
},
"restore": {
"type": "boolean",
"title": "Restore..."
},
"checkout": {
"type": "boolean",
"title": "Checkout..."
Expand Down Expand Up @@ -1537,7 +1541,7 @@
"eslint": "7.15.0",
"jest": "26.6.3",
"ts-jest": "26.4.4",
"typescript": "4.0.2",
"typescript": "4.1.5",
"uglify-js": "3.10.0"
}
}
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Config {
const userConfig = this.config.get('contextMenuActionsVisibility', {});
const config: ContextMenuActionsVisibility = {
branch: { checkout: true, rename: true, delete: true, merge: true, rebase: true, push: true, viewIssue: true, createPullRequest: true, createArchive: true, selectInBranchesDropdown: true, unselectInBranchesDropdown: true, copyName: true },
commit: { addTag: true, createBranch: true, checkout: true, cherrypick: true, revert: true, drop: true, merge: true, rebase: true, reset: true, copyHash: true, copySubject: true },
commit: { addTag: true, createBranch: true, restore: true, checkout: true, cherrypick: true, revert: true, drop: true, merge: true, rebase: true, reset: true, copyHash: true, copySubject: true },
commitDetailsViewFile: { viewDiff: true, viewFileAtThisRevision: true, viewDiffWithWorkingFile: true, openFile: true, markAsReviewed: true, markAsNotReviewed: true, resetFileToThisRevision: true, copyAbsoluteFilePath: true, copyRelativeFilePath: true },
remoteBranch: { checkout: true, delete: true, fetch: true, merge: true, pull: true, viewIssue: true, createPullRequest: true, createArchive: true, selectInBranchesDropdown: true, unselectInBranchesDropdown: true, copyName: true },
stash: { apply: true, createBranch: true, pop: true, drop: true, copyName: true, copyHash: true },
Expand Down
10 changes: 10 additions & 0 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,16 @@ export class DataSource extends Disposable {

/* Git Action Methods - Commits */

/**
* Restore a commit in a repository.
* @param repo The path of the repository.
* @param commitHash The hash of the commit to check out.
* @returns The ErrorInfo from the executed command.
*/
public restoreCommit(repo: string, commitHash: string) {
return this.runGitCommand(['restore', '--source=' + commitHash, ':/'], repo);
}

/**
* Checkout a commit in a repository.
* @param repo The path of the repository.
Expand Down
1 change: 1 addition & 0 deletions src/extensionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const DEFAULT_REPO_STATE: GitRepoState = {

const DEFAULT_GIT_GRAPH_VIEW_GLOBAL_STATE: GitGraphViewGlobalState = {
alwaysAcceptCheckoutCommit: false,
alwaysAcceptRestoreCommit: false,
issueLinkingConfig: null,
pushTagSkipRemoteCheck: false
};
Expand Down
6 changes: 6 additions & 0 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ export class GitGraphView extends Disposable {
errors: errorInfos
});
break;
case 'restoreCommit':
this.sendMessage({
command: 'restoreCommit',
error: await this.dataSource.restoreCommit(msg.repo, msg.commitHash)
});
break;
case 'checkoutCommit':
this.sendMessage({
command: 'checkoutCommit',
Expand Down
2 changes: 1 addition & 1 deletion src/life-cycle/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function getLifeCycleStateInDirectory(directory: string) {
* @param state The state to save.
*/
export function saveLifeCycleStateInDirectory(directory: string, state: LifeCycleState) {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
fs.mkdir(directory, (err) => {
if (!err || err.code === 'EEXIST') {
fs.writeFile(getLifeCycleFilePathInDirectory(directory), JSON.stringify(state), (err) => {
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export interface GitGraphViewConfig {

export interface GitGraphViewGlobalState {
alwaysAcceptCheckoutCommit: boolean;
alwaysAcceptRestoreCommit: boolean;
issueLinkingConfig: IssueLinkingConfig | null;
pushTagSkipRemoteCheck: boolean;
}
Expand Down Expand Up @@ -359,6 +360,7 @@ export interface ContextMenuActionsVisibility {
readonly commit: {
readonly addTag: boolean;
readonly createBranch: boolean;
readonly restore: boolean;
readonly checkout: boolean;
readonly cherrypick: boolean;
readonly revert: boolean;
Expand Down Expand Up @@ -1137,6 +1139,14 @@ export interface ResponseResetToCommit extends ResponseWithErrorInfo {
readonly command: 'resetToCommit';
}

export interface RequestRestoreCommit extends RepoRequest {
readonly command: 'restoreCommit';
readonly commitHash: string;
}
export interface ResponseRestoreCommit extends ResponseWithErrorInfo {
readonly command: 'restoreCommit';
}

export interface RequestRevertCommit extends RepoRequest {
readonly command: 'revertCommit';
readonly commitHash: string;
Expand Down Expand Up @@ -1298,6 +1308,7 @@ export type RequestMessage =
| RequestRescanForRepos
| RequestResetFileToRevision
| RequestResetToCommit
| RequestRestoreCommit
| RequestRevertCommit
| RequestSetGlobalViewState
| RequestSetRepoState
Expand Down Expand Up @@ -1362,6 +1373,7 @@ export type ResponseMessage =
| ResponseResetFileToRevision
| ResponseResetToCommit
| ResponseRevertCommit
| ResponseRestoreCommit
| ResponseSetGlobalViewState
| ResponseSetWorkspaceViewState
| ResponseStartCodeReview
Expand Down
19 changes: 19 additions & 0 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,22 @@ class GitGraphView {
}
], [
{
title: 'Restore' + (globalState.alwaysAcceptRestoreCommit ? '' : ELLIPSIS),
visible: visibility.restore,
onClick: () => {
const restoreCommit = () => runAction({ command: 'restoreCommit', repo: this.currentRepo, commitHash: hash }, 'Restore Commit');
if (globalState.alwaysAcceptRestoreCommit) {
restoreCommit();
} else {
dialog.showCheckbox('Are you sure you want to restore commit <b><i>' + abbrevCommit(hash) + '</i></b>?', 'Always Accept', false, 'Yes, restore', (alwaysAccept) => {
if (alwaysAccept) {
updateGlobalViewState('alwaysAcceptRestoreCommit', true);
}
restoreCommit();
}, target);
}
}
}, {
title: 'Checkout' + (globalState.alwaysAcceptCheckoutCommit ? '' : ELLIPSIS),
visible: visibility.checkout,
onClick: () => {
Expand Down Expand Up @@ -3372,6 +3388,9 @@ window.addEventListener('load', () => {
case 'resetToCommit':
refreshOrDisplayError(msg.error, 'Unable to Reset to Commit');
break;
case 'restoreCommit':
refreshOrDisplayError(msg.error, 'Unable to Restore Commit');
break;
case 'revertCommit':
refreshOrDisplayError(msg.error, 'Unable to Revert Commit');
break;
Expand Down