Skip to content

Commit c52101f

Browse files
committed
Closes #4542 adds restore previous changes command
1 parent 1d2472f commit c52101f

File tree

6 files changed

+87
-40
lines changed

6 files changed

+87
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
3131
- Updates `gitlens.graph.multiselect` setting to default to `topological`; set to `true` to allow selecting multiple commits without restriction
3232
- Adds ability to switch between open repositories on the _Visual History_
3333
- Adds _Visualize Repository History_ commands to the SCM menus
34+
- Adds _Restore Previous Changes_ command to files in the views ([#4542](https://github.com/gitkraken/vscode-gitlens/issues/4542))
3435

3536
### Changed
3637

contributions.json

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,6 +3941,40 @@
39413941
"label": "Reset Views Layout",
39423942
"commandPalette": true
39433943
},
3944+
"gitlens.restore.file:views": {
3945+
"label": "Restore Changes (Checkout)",
3946+
"menus": {
3947+
"view/item/context": [
3948+
{
3949+
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+stashed\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
3950+
"group": "1_gitlens_actions",
3951+
"order": 3
3952+
},
3953+
{
3954+
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
3955+
"group": "8_gitlens_actions",
3956+
"order": 3
3957+
}
3958+
]
3959+
}
3960+
},
3961+
"gitlens.restorePrevious.file:views": {
3962+
"label": "Restore Previous Changes",
3963+
"menus": {
3964+
"view/item/context": [
3965+
{
3966+
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+stashed\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
3967+
"group": "1_gitlens_actions",
3968+
"order": 4
3969+
},
3970+
{
3971+
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
3972+
"group": "8_gitlens_actions",
3973+
"order": 4
3974+
}
3975+
]
3976+
}
3977+
},
39443978
"gitlens.revealCommitInView": {
39453979
"label": "Reveal Commit in Side Bar",
39463980
"commandPalette": "resource in gitlens:tabs:blameable",
@@ -12077,24 +12111,6 @@
1207712111
]
1207812112
}
1207912113
},
12080-
"gitlens.views.restore": {
12081-
"label": "Restore (Checkout)",
12082-
"icon": "$(gitlens-switch)",
12083-
"menus": {
12084-
"view/item/context": [
12085-
{
12086-
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+stashed\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
12087-
"group": "1_gitlens_actions",
12088-
"order": 3
12089-
},
12090-
{
12091-
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
12092-
"group": "8_gitlens_actions",
12093-
"order": 3
12094-
}
12095-
]
12096-
}
12097-
},
1209812114
"gitlens.views.revealRepositoryInExplorer": {
1209912115
"label": "Reveal in File Explorer",
1210012116
"menus": {

package.json

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7725,6 +7725,14 @@
77257725
"title": "Reset Views Layout",
77267726
"category": "GitLens"
77277727
},
7728+
{
7729+
"command": "gitlens.restore.file:views",
7730+
"title": "Restore Changes (Checkout)"
7731+
},
7732+
{
7733+
"command": "gitlens.restorePrevious.file:views",
7734+
"title": "Restore Previous Changes"
7735+
},
77287736
{
77297737
"command": "gitlens.revealCommitInView",
77307738
"title": "Reveal Commit in Side Bar",
@@ -9629,11 +9637,6 @@
96299637
"title": "Reset Current Branch to Tip...",
96309638
"enablement": "!operationInProgress"
96319639
},
9632-
{
9633-
"command": "gitlens.views.restore",
9634-
"title": "Restore (Checkout)",
9635-
"icon": "$(gitlens-switch)"
9636-
},
96379640
{
96389641
"command": "gitlens.views.revealRepositoryInExplorer",
96399642
"title": "Reveal in File Explorer"
@@ -12171,6 +12174,14 @@
1217112174
"command": "gitlens.regenerateMarkdownDocument",
1217212175
"when": "false"
1217312176
},
12177+
{
12178+
"command": "gitlens.restore.file:views",
12179+
"when": "false"
12180+
},
12181+
{
12182+
"command": "gitlens.restorePrevious.file:views",
12183+
"when": "false"
12184+
},
1217412185
{
1217512186
"command": "gitlens.revealCommitInView",
1217612187
"when": "resource in gitlens:tabs:blameable"
@@ -13687,10 +13698,6 @@
1368713698
"command": "gitlens.views.resetToTip",
1368813699
"when": "false"
1368913700
},
13690-
{
13691-
"command": "gitlens.views.restore",
13692-
"when": "false"
13693-
},
1369413701
{
1369513702
"command": "gitlens.views.revealRepositoryInExplorer",
1369613703
"when": "false"
@@ -18093,10 +18100,15 @@
1809318100
"group": "1_gitlens_actions@2"
1809418101
},
1809518102
{
18096-
"command": "gitlens.views.restore",
18103+
"command": "gitlens.restore.file:views",
1809718104
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+stashed\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
1809818105
"group": "1_gitlens_actions@3"
1809918106
},
18107+
{
18108+
"command": "gitlens.restorePrevious.file:views",
18109+
"when": "viewItem =~ /gitlens:file\\b(?=.*?\\b\\+stashed\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
18110+
"group": "1_gitlens_actions@4"
18111+
},
1810018112
{
1810118113
"command": "gitlens.views.openChanges",
1810218114
"when": "viewItem =~ /gitlens:file\\b/ && !listMultiSelection",
@@ -18189,10 +18201,15 @@
1818918201
"group": "8_gitlens_actions@2"
1819018202
},
1819118203
{
18192-
"command": "gitlens.views.restore",
18204+
"command": "gitlens.restore.file:views",
1819318205
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
1819418206
"group": "8_gitlens_actions@3"
1819518207
},
18208+
{
18209+
"command": "gitlens.restorePrevious.file:views",
18210+
"when": "viewItem =~ /gitlens:file\\b((?=.*?\\b\\+committed\\b)|:results\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",
18211+
"group": "8_gitlens_actions@4"
18212+
},
1819618213
{
1819718214
"command": "gitlens.views.stageDirectory",
1819818215
"when": "viewItem =~ /gitlens:folder\\b(?=.*?\\b\\+working\\b)/ && !listMultiSelection && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders",

src/constants.commands.generated.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export type ContributedCommands =
177177
| 'gitlens.openWorkingFile:editor/title'
178178
| 'gitlens.plus.cloudIntegrations.connect'
179179
| 'gitlens.regenerateMarkdownDocument'
180+
| 'gitlens.restore.file:views'
181+
| 'gitlens.restorePrevious.file:views'
180182
| 'gitlens.setUpstream:graph'
181183
| 'gitlens.setUpstream:views'
182184
| 'gitlens.showInCommitGraph'
@@ -503,7 +505,6 @@ export type ContributedCommands =
503505
| 'gitlens.views.resetCommit'
504506
| 'gitlens.views.resetToCommit'
505507
| 'gitlens.views.resetToTip'
506-
| 'gitlens.views.restore'
507508
| 'gitlens.views.revealRepositoryInExplorer'
508509
| 'gitlens.views.revealWorktreeInExplorer'
509510
| 'gitlens.views.revert'

src/git/actions/commit.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,31 +663,35 @@ export async function openFilesAtRevision(
663663
);
664664
}
665665

666-
export async function restoreFile(file: string | GitFile, revision: GitRevisionReference): Promise<void> {
666+
export async function restoreFile(
667+
file: string | GitFile,
668+
revision: GitRevisionReference,
669+
previous?: boolean,
670+
): Promise<void> {
667671
let path;
668-
let ref;
672+
let rev;
669673
if (typeof file === 'string') {
670674
path = file;
671-
ref = revision.ref;
675+
rev = previous ? `${revision.ref}^` : revision.ref;
672676
} else {
673677
path = file.path;
674678
if (file.status === 'D') {
675679
// If the file is deleted, check to see if it exists, if so, restore it from the previous commit, otherwise restore it from the current commit
676680
const uri = GitUri.fromFile(file, revision.repoPath);
677681
try {
678682
await workspace.fs.stat(uri);
679-
ref = `${revision.ref}^`;
683+
rev = `${revision.ref}^`;
680684
} catch {
681-
ref = revision.ref;
685+
rev = previous ? `${revision.ref}^` : revision.ref;
682686
}
683687
} else if (file.status === '?') {
684-
ref = `${revision.ref}^3`;
688+
rev = `${revision.ref}^3`;
685689
} else {
686-
ref = revision.ref;
690+
rev = previous ? `${revision.ref}^` : revision.ref;
687691
}
688692
}
689693

690-
await Container.instance.git.getRepositoryService(revision.repoPath).checkout(ref, { path: path });
694+
await Container.instance.git.getRepositoryService(revision.repoPath).checkout(rev, { path: path });
691695
}
692696

693697
export function revealCommit(commit: GitRevisionReference, options?: RevealOptions): Promise<ViewNode | undefined> {

src/views/viewCommands.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,14 +993,22 @@ export class ViewCommands implements Disposable {
993993
);
994994
}
995995

996-
@command('gitlens.views.restore')
996+
@command('gitlens.restore.file:views')
997997
@log()
998-
private restore(node: ViewRefFileNode) {
998+
private restoreFile(node: ViewRefFileNode) {
999999
if (!(node instanceof ViewRefFileNode)) return Promise.resolve();
10001000

10011001
return CommitActions.restoreFile(node.file, node.ref);
10021002
}
10031003

1004+
@command('gitlens.restorePrevious.file:views')
1005+
@log()
1006+
private restorePreviousFile(node: ViewRefFileNode) {
1007+
if (!(node instanceof ViewRefFileNode)) return Promise.resolve();
1008+
1009+
return CommitActions.restoreFile(node.file, node.ref, true);
1010+
}
1011+
10041012
@command('gitlens.views.revealRepositoryInExplorer')
10051013
@log()
10061014
private revealRepositoryInExplorer(node: RepositoryNode) {

0 commit comments

Comments
 (0)