Skip to content

Commit

Permalink
#2 Reset current branch to commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutchie committed Feb 13, 2019
1 parent 9542bce commit 47fef00
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ View a Git Graph of your repository, and easily perform Git actions from the gra
* Perform Git Actions (available by right clicking on a commit / branch / tag):
* Create, Checkout, Rename & Delete Branches
* Add & Delete Tags
* Reset to commit
* Copy Commit Hash to Clipboard
* Configurable settings (e.g. graph style, branch colours, and more...)
* "Git Graph: View Git Graph" launch command in the Command Palette
Expand Down
2 changes: 1 addition & 1 deletion media/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ body.notGitRepository h1, body.notGitRepository p{
margin-top:10px;
display:inline-block;
}
#dialog input[type=text]{
#dialog input[type=text], #dialog select{
margin-top:10px;
width:100%;
padding:4px;
Expand Down
6 changes: 5 additions & 1 deletion src/dataSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cp from 'child_process';
import { Config } from './config';
import { GitCommandStatus, GitCommit, GitCommitNode, GitRef, GitUnsavedChanges } from './types';
import { GitCommandStatus, GitCommit, GitCommitNode, GitRef, GitResetMode, GitUnsavedChanges } from './types';

const eolRegex = /\r\n|\r|\n/g;
const gitLogSeparator = '4Rvn5rwg14BTwO3msm0ftBCk';
Expand Down Expand Up @@ -122,6 +122,10 @@ export class DataSource {
return this.runGitCommand('git branch -m ' + escapeRefName(oldName) + ' ' + escapeRefName(newName));
}

public resetToCommit(commitHash: string, resetMode: GitResetMode): GitCommandStatus {
return this.runGitCommand('git reset --' + resetMode + ' ' + commitHash);
}

private runGitCommand(command: string): GitCommandStatus {
try {
cp.execSync(command, { cwd: this.workspaceDir });
Expand Down
6 changes: 6 additions & 0 deletions src/gitGraphView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export class GitGraphView {
data: this.dataSource.renameBranch(message.data.oldName, message.data.newName)
});
return;
case 'resetToCommit':
this.sendMessage({
command: 'resetToCommit',
data: this.dataSource.resetToCommit(message.data.commitHash, message.data.resetMode)
});
return;
}
}, null, this.disposables);
}
Expand Down
36 changes: 21 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export interface RequestLoadBranchesMessage {
showRemoteBranches: boolean
};
}
export interface ResponseLoadBranchesMessage {
command: 'loadBranches';
data: string[];
}

export interface RequestLoadCommitsMessage {
command: 'loadCommits';
Expand All @@ -58,12 +62,6 @@ export interface RequestLoadCommitsMessage {
currentBranch: string | null
};
}

export interface ResponseLoadBranchesMessage {
command: 'loadBranches';
data: string[];
}

export interface ResponseLoadCommitsMessage {
command: 'loadCommits';
data: {
Expand All @@ -79,7 +77,6 @@ export interface RequestAddTag {
tagName: string
};
}

export interface ResponseAddTag {
command: 'addTag';
data: GitCommandStatus;
Expand All @@ -89,7 +86,6 @@ export interface RequestDeleteTag {
command: 'deleteTag';
data: string;
}

export interface ResponseDeleteTag {
command: 'deleteTag';
data: GitCommandStatus;
Expand All @@ -99,7 +95,6 @@ export interface RequestCopyCommitHashToClipboard {
command: 'copyCommitHashToClipboard';
data: string;
}

export interface ResponseCopyCommitHashToClipboard {
command: 'copyCommitHashToClipboard';
data: boolean;
Expand All @@ -112,7 +107,6 @@ export interface RequestCreateBranch {
branchName: string
};
}

export interface ResponseCreateBranch {
command: 'createBranch';
data: GitCommandStatus;
Expand All @@ -125,7 +119,6 @@ export interface RequestCheckoutBranch {
remoteBranch: string | null
};
}

export interface ResponseCheckoutBranch {
command: 'checkoutBranch';
data: GitCommandStatus;
Expand All @@ -138,7 +131,6 @@ export interface RequestDeleteBranch {
forceDelete: boolean
};
}

export interface ResponseDeleteBranch {
command: 'deleteBranch';
data: GitCommandStatus;
Expand All @@ -151,12 +143,23 @@ export interface RequestRenameBranch {
newName: string
};
}

export interface ResponseRenameBranch {
command: 'renameBranch';
data: GitCommandStatus;
}

export interface RequestResetToCommit {
command: 'resetToCommit';
data: {
commitHash: string,
resetMode: GitResetMode
};
}
export interface ResponseResetToCommit {
command: 'resetToCommit';
data: GitCommandStatus;
}

/* Types */

export type RequestMessage = RequestLoadBranchesMessage
Expand All @@ -167,7 +170,8 @@ export type RequestMessage = RequestLoadBranchesMessage
| RequestCreateBranch
| RequestCheckoutBranch
| RequestDeleteBranch
| RequestRenameBranch;
| RequestRenameBranch
| RequestResetToCommit;
export type ResponseMessage = ResponseLoadBranchesMessage
| ResponseLoadCommitsMessage
| ResponseAddTag
Expand All @@ -176,7 +180,9 @@ export type ResponseMessage = ResponseLoadBranchesMessage
| ResponseCreateBranch
| ResponseCheckoutBranch
| ResponseDeleteBranch
| ResponseRenameBranch;
| ResponseRenameBranch
| ResponseResetToCommit;
export type DateFormat = 'Date & Time' | 'Date Only' | 'Relative';
export type GraphStyle = 'rounded' | 'angular';
export type GitCommandStatus = string | null;
export type GitResetMode = 'soft' | 'mixed' | 'hard';
2 changes: 2 additions & 0 deletions web/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
GitCommandStatus as GitCommandStatusX,
GitCommitNode as GitCommitNodeX,
GitGraphViewSettings as GitGraphViewSettingsX,
GitResetMode as GitResetModeX,
RequestMessage as RequestMessageX,
ResponseMessage as ResponseMessageX
} from "../out/types";
Expand All @@ -10,6 +11,7 @@ declare global {
type GitCommandStatus = GitCommandStatusX;
type GitCommitNode = GitCommitNodeX;
type GitGraphViewSettings = GitGraphViewSettingsX;
type GitResetMode = GitResetModeX;
type RequestMessage = RequestMessageX;
type ResponseMessage = ResponseMessageX;
}
33 changes: 31 additions & 2 deletions web/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,28 @@ declare interface ContextMenuItem {
}
},
{
title: 'Create Branch from Commit',
title: 'Create Branch from this Commit',
onClick: () => {
showInputDialog('Enter the name of the branch you would like to create from commit <b><i>' + hash.substring(0, 8) + '</i></b>:', '', 'Create Branch', (name: string) => {
sendMessage({ command: 'createBranch', data: { branchName: name, commitHash: hash } });
});
}
},
{
title: 'Reset current branch to this Commit',
onClick: () => {
showSelectDialog('Are you sure you want to reset the <b>current branch</b> to commit <b><i>' + hash.substring(0, 8) + '</i></b>:', 'mixed', [
{ name: 'Soft - Keep all changes, but reset head', value: 'soft' },
{ name: 'Mixed - Keep working tree, but reset index', value: 'mixed' },
{ name: 'Hard - Discard all changes', value: 'hard' }
], 'Yes, reset', (mode: string) => {
sendMessage({ command: 'resetToCommit', data: { commitHash: hash, resetMode: <GitResetMode>mode } });
});
}
},
{
title: 'Copy Commit Hash to Clipboard',
onClick: () => {
hideContextMenu();
sendMessage({ command: 'copyCommitHashToClipboard', data: hash });
}
}
Expand Down Expand Up @@ -602,6 +613,9 @@ declare interface ContextMenuItem {
case 'renameBranch':
refreshGraphOrDisplayError(msg.data, 'Unable to Rename Branch');
break;
case 'resetToCommit':
refreshGraphOrDisplayError(msg.data, 'Unable to Reset to Commit');
break;
}
});
function refreshGraphOrDisplayError(status: GitCommandStatus, errorMessage: string) {
Expand Down Expand Up @@ -766,6 +780,21 @@ declare interface ContextMenuItem {
});
document.getElementById('dialogCancel')!.addEventListener('click', hideDialog);
}
function showSelectDialog(message: string, defaultValue: string, options: { name: string, value: string }[], action: string, actioned: (value: string) => void) {
dialogBacking.className = 'active';
dialog.className = 'active';
let selectOptions = '', i;
for (i = 0; i < options.length; i++) {
selectOptions += '<option value="' + options[i].value + '"' + (options[i].value === defaultValue ? ' selected' : '') + '>' + options[i].name + '</option>';
}
dialog.innerHTML = message + '<br><select id="dialogInput">' + selectOptions + '</select><br><div id="dialogAction" class="roundedBtn">' + action + '</div><div id="dialogCancel" class="roundedBtn">Cancel</div>';
document.getElementById('dialogAction')!.addEventListener('click', () => {
let value = (<HTMLSelectElement>document.getElementById('dialogInput')).value;
hideDialog();
actioned(value);
});
document.getElementById('dialogCancel')!.addEventListener('click', hideDialog);
}
function showErrorDialog(message: string, reason: string | null) {
dialogBacking.className = 'active';
dialog.className = 'active';
Expand Down

0 comments on commit 47fef00

Please sign in to comment.