Skip to content

Commit

Permalink
#12 Revert, cherry-pick & merge git commands available from context m…
Browse files Browse the repository at this point in the history
…enus
  • Loading branch information
mhutchie committed Feb 25, 2019
1 parent 50d3ef8 commit 2430e50
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ export class DataSource {
return this.runGitCommand('git branch -m ' + escapeRefName(oldName) + ' ' + escapeRefName(newName));
}

public mergeBranch(branchName: string){
return this.runGitCommand('git merge ' + escapeRefName(branchName));
}

public cherrypickCommit(commitHash: string, parentIndex: number) {
return this.runGitCommand('git cherry-pick ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
}

public revertCommit(commitHash: string, parentIndex: number) {
return this.runGitCommand('git revert --no-edit ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
}

public resetToCommit(commitHash: string, resetMode: GitResetMode) {
return this.runGitCommand('git reset --' + resetMode + ' ' + commitHash);
}
Expand Down
18 changes: 18 additions & 0 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class GitGraphView {
status: await this.dataSource.checkoutBranch(msg.branchName, msg.remoteBranch)
});
return;
case 'cherrypickCommit':
this.sendMessage({
command: 'cherrypickCommit',
status: await this.dataSource.cherrypickCommit(msg.commitHash, msg.parentIndex)
});
return;
case 'commitDetails':
this.sendMessage({
command: 'commitDetails',
Expand Down Expand Up @@ -99,6 +105,12 @@ export class GitGraphView {
... await this.dataSource.getCommits(msg.branchName, msg.maxCommits, msg.showRemoteBranches, msg.currentBranch)
});
return;
case 'mergeBranch':
this.sendMessage({
command: 'mergeBranch',
status: await this.dataSource.mergeBranch(msg.branchName)
});
return;
case 'renameBranch':
this.sendMessage({
command: 'renameBranch',
Expand All @@ -111,6 +123,12 @@ export class GitGraphView {
status: await this.dataSource.resetToCommit(msg.commitHash, msg.resetMode)
});
return;
case 'revertCommit':
this.sendMessage({
command: 'revertCommit',
status: await this.dataSource.revertCommit(msg.commitHash, msg.parentIndex)
});
return;
case 'viewDiff':
this.viewDiff(msg.commitHash, msg.oldFilePath, msg.newFilePath, msg.type);
return;
Expand Down
35 changes: 35 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ export interface ResponseCheckoutBranch {
status: GitCommandStatus;
}

export interface RequestCherrypickCommit {
command: 'cherrypickCommit';
commitHash: string;
parentIndex: number;
}
export interface ResponseCherrypickCommit {
command: 'cherrypickCommit';
status: GitCommandStatus;
}

export interface RequestCommitDetails {
command: 'commitDetails';
commitHash: string;
Expand Down Expand Up @@ -158,6 +168,15 @@ export interface ResponseLoadCommits {
moreCommitsAvailable: boolean;
}

export interface RequestMergeBranch {
command: 'mergeBranch';
branchName: string;
}
export interface ResponseMergeBranch {
command: 'mergeBranch';
status: GitCommandStatus;
}

export interface RequestRenameBranch {
command: 'renameBranch';
oldName: string;
Expand All @@ -178,6 +197,16 @@ export interface ResponseResetToCommit {
status: GitCommandStatus;
}

export interface RequestRevertCommit {
command: 'revertCommit';
commitHash: string;
parentIndex: number;
}
export interface ResponseRevertCommit {
command: 'revertCommit';
status: GitCommandStatus;
}

export interface RequestViewDiff {
command: 'viewDiff';
commitHash: string;
Expand All @@ -193,27 +222,33 @@ export interface ResponseViewDiff {
export type RequestMessage =
RequestAddTag
| RequestCheckoutBranch
| RequestCherrypickCommit
| RequestCommitDetails
| RequestCopyCommitHashToClipboard
| RequestCreateBranch
| RequestDeleteBranch
| RequestDeleteTag
| RequestLoadBranches
| RequestLoadCommits
| RequestMergeBranch
| RequestRenameBranch
| RequestResetToCommit
| RequestRevertCommit
| RequestViewDiff;

export type ResponseMessage =
ResponseAddTag
| ResponseCheckoutBranch
| ResponseCherrypickCommit
| ResponseCommitDetails
| ResponseCopyCommitHashToClipboard
| ResponseCreateBranch
| ResponseDeleteBranch
| ResponseDeleteTag
| ResponseLoadBranches
| ResponseLoadCommits
| ResponseMergeBranch
| ResponseRenameBranch
| ResponseResetToCommit
| ResponseRevertCommit
| ResponseViewDiff;
86 changes: 71 additions & 15 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,42 @@
}, sourceElem);
}
},
{
title: 'Cherry Pick this Commit',
onClick: () => {
if (this.commits[this.commitLookup[hash]].parentHashes.length === 1) {
showConfirmationDialog('Are you sure you want to cherry pick commit <b><i>' + abbrevCommit(hash) + '</i></b>?', () => {
sendMessage({ command: 'cherrypickCommit', commitHash: hash, parentIndex: 0 });
}, sourceElem);
} else {
let options = this.commits[this.commitLookup[hash]].parentHashes.map((hash, index) => ({
name: abbrevCommit(hash) + (typeof this.commitLookup[hash] === 'number' ? ': ' + this.commits[this.commitLookup[hash]].message : ''),
value: (index + 1).toString()
}));
showSelectDialog('Are you sure you want to cherry pick merge commit <b><i>' + abbrevCommit(hash) + '</i></b>? Choose the parent hash on the main branch, to cherry pick the commit relative to:', '1', options, 'Yes, cherry pick commit', (parentIndex) => {
sendMessage({ command: 'cherrypickCommit', commitHash: hash, parentIndex: parseInt(parentIndex) });
}, sourceElem);
}
}
},
{
title: 'Reverse this Commit',
onClick: () => {
if (this.commits[this.commitLookup[hash]].parentHashes.length === 1) {
showConfirmationDialog('Are you sure you want to reverse commit <b><i>' + abbrevCommit(hash) + '</i></b>?', () => {
sendMessage({ command: 'revertCommit', commitHash: hash, parentIndex: 0 });
}, sourceElem);
} else {
let options = this.commits[this.commitLookup[hash]].parentHashes.map((hash, index) => ({
name: abbrevCommit(hash) + (typeof this.commitLookup[hash] === 'number' ? ': ' + this.commits[this.commitLookup[hash]].message : ''),
value: (index + 1).toString()
}));
showSelectDialog('Are you sure you want to reverse merge commit <b><i>' + abbrevCommit(hash) + '</i></b>? Choose the parent hash on the main branch, to reverse the commit relative to:', '1', options, 'Yes, reverse commit', (parentIndex) => {
sendMessage({ command: 'revertCommit', commitHash: hash, parentIndex: parseInt(parentIndex) });
}, sourceElem);
}
}
},
{
title: 'Reset current branch to this Commit',
onClick: () => {
Expand Down Expand Up @@ -570,22 +606,33 @@
}
}];
if (sourceElem.className === 'gitRef head') {
menu.push({
title: 'Rename Branch',
onClick: () => {
showInputDialog('Enter the new name for branch <b><i>' + escapeHtml(refName) + '</i></b>:', refName, 'Rename Branch', (newName) => {
sendMessage({ command: 'renameBranch', oldName: refName, newName: newName });
}, null);
}
});
menu.push({
title: 'Delete Branch',
onClick: () => {
showCheckboxDialog('Are you sure you want to delete the branch <b><i>' + escapeHtml(refName) + '</i></b>?', 'Force Delete', 'Delete Branch', (forceDelete) => {
sendMessage({ command: 'deleteBranch', branchName: refName, forceDelete: forceDelete });
}, null);
menu.push(
{
title: 'Rename Branch',
onClick: () => {
showInputDialog('Enter the new name for branch <b><i>' + escapeHtml(refName) + '</i></b>:', refName, 'Rename Branch', (newName) => {
sendMessage({ command: 'renameBranch', oldName: refName, newName: newName });
}, null);
}
}, {
title: 'Delete Branch',
onClick: () => {
showCheckboxDialog('Are you sure you want to delete the branch <b><i>' + escapeHtml(refName) + '</i></b>?', 'Force Delete', 'Delete Branch', (forceDelete) => {
sendMessage({ command: 'deleteBranch', branchName: refName, forceDelete: forceDelete });
}, null);
}
}
});
);
if (this.branchOptions.length > 0 && this.branchOptions[0] !== refName) {
menu.push({
title: 'Merge into current branch',
onClick: () => {
showConfirmationDialog('Are you sure you want to merge branch <b><i>' + escapeHtml(refName) + '</i></b> into the current branch?', () => {
sendMessage({ command: 'mergeBranch', branchName: refName });
}, null);
}
});
}
}
}
showContextMenu(<MouseEvent>e, menu, sourceElem);
Expand Down Expand Up @@ -698,6 +745,9 @@
case 'checkoutBranch':
refreshGraphOrDisplayError(msg.status, 'Unable to Checkout Branch');
break;
case 'cherrypickCommit':
refreshGraphOrDisplayError(msg.status, 'Unable to Cherry Pick Commit');
break;
case 'commitDetails':
if (msg.commitDetails === null) {
gitGraph.hideCommitDetails();
Expand All @@ -724,12 +774,18 @@
case 'loadCommits':
gitGraph.loadCommits(msg.commits, msg.moreCommitsAvailable);
break;
case 'mergeBranch':
refreshGraphOrDisplayError(msg.status, 'Unable to Merge Branch');
break;
case 'renameBranch':
refreshGraphOrDisplayError(msg.status, 'Unable to Rename Branch');
break;
case 'resetToCommit':
refreshGraphOrDisplayError(msg.status, 'Unable to Reset to Commit');
break;
case 'revertCommit':
refreshGraphOrDisplayError(msg.status, 'Unable to Reverse Commit');
break;
case 'viewDiff':
if (msg.success === false) showErrorDialog('Unable to view diff of file', null, null);
break;
Expand Down

0 comments on commit 2430e50

Please sign in to comment.