Skip to content

Commit 7b7b788

Browse files
committed
Closes #684 - adds checkout to files
1 parent 8febb67 commit 7b7b788

File tree

6 files changed

+52
-32
lines changed

6 files changed

+52
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
88

99
### Added
1010

11+
- Adds a _Checkout_ command to file nodes in the views to replace the local file with the specified revision — closes [#684](https://github.com/eamodio/vscode-gitlens/issues/684)
1112
- Adds a prompt to enable the view to the _Show \* View_ commands when the specified view is disabled — closes [#710](https://github.com/eamodio/vscode-gitlens/issues/710) & [#711](https://github.com/eamodio/vscode-gitlens/issues/711)
1213

1314
### Removed

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,20 +4319,30 @@
43194319
"when": "viewItem =~ /gitlens:file:(commit|results)\\b/",
43204320
"group": "5_gitlens_1@1"
43214321
},
4322+
{
4323+
"command": "gitlens.views.checkout",
4324+
"when": "viewItem =~ /gitlens:file:(commit|results)\\b/",
4325+
"group": "5_gitlens_1@2"
4326+
},
43224327
{
43234328
"command": "gitlens.views.applyChanges",
43244329
"when": "!gitlens:readonly && viewItem == gitlens:file:stash",
43254330
"group": "1_gitlens@1"
43264331
},
4332+
{
4333+
"command": "gitlens.views.checkout",
4334+
"when": "!gitlens:readonly && viewItem == gitlens:file:stash",
4335+
"group": "1_gitlens@2"
4336+
},
43274337
{
43284338
"command": "gitlens.showQuickCommitDetails",
43294339
"when": "viewItem =~ /gitlens:file\\b(?!(:stash|:status))/",
4330-
"group": "5_gitlens_2@2"
4340+
"group": "5_gitlens_2@1"
43314341
},
43324342
{
43334343
"command": "gitlens.showCommitInView",
43344344
"when": "viewItem =~ /gitlens:file\\b(?!(:stash|:status))/",
4335-
"group": "5_gitlens_2@3"
4345+
"group": "5_gitlens_2@2"
43364346
},
43374347
{
43384348
"command": "gitlens.showQuickFileHistory",

src/views/nodes/commitFileNode.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { GlyphChars } from '../../constants';
66
import { Container } from '../../container';
77
import { CommitFormatter, GitFile, GitLogCommit, GitUri, StatusFileFormatter } from '../../git/gitService';
88
import { View } from '../viewBase';
9-
import { ResourceType, ViewNode, ViewRefNode } from './viewNode';
9+
import { ResourceType, ViewNode, ViewRefFileNode } from './viewNode';
1010

1111
export enum CommitFileNodeDisplayAs {
1212
CommitLabel = 1 << 0,
@@ -19,7 +19,7 @@ export enum CommitFileNodeDisplayAs {
1919
File = FileLabel | StatusIcon
2020
}
2121

22-
export class CommitFileNode extends ViewRefNode {
22+
export class CommitFileNode extends ViewRefFileNode {
2323
constructor(
2424
view: View,
2525
parent: ViewNode,
@@ -31,6 +31,10 @@ export class CommitFileNode extends ViewRefNode {
3131
super(GitUri.fromFile(file, commit.repoPath, commit.sha), view, parent);
3232
}
3333

34+
get fileName(): string {
35+
return this.file.fileName;
36+
}
37+
3438
get priority(): number {
3539
return 0;
3640
}

src/views/nodes/resultsFileNode.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Commands, DiffWithCommandArgs } from '../../commands';
55
import { Container } from '../../container';
66
import { GitFile, GitUri, StatusFileFormatter } from '../../git/gitService';
77
import { View } from '../viewBase';
8-
import { ResourceType, ViewNode } from './viewNode';
8+
import { ResourceType, ViewNode, ViewRefFileNode } from './viewNode';
99

10-
export class ResultsFileNode extends ViewNode {
10+
export class ResultsFileNode extends ViewRefFileNode {
1111
constructor(
1212
view: View,
1313
parent: ViewNode,
@@ -16,11 +16,15 @@ export class ResultsFileNode extends ViewNode {
1616
public readonly ref1: string,
1717
public readonly ref2: string
1818
) {
19-
super(GitUri.fromFile(file, repoPath, ref1 ? ref1 : ref2 ? ref2 : undefined), view, parent);
19+
super(GitUri.fromFile(file, repoPath, ref1 || ref2), view, parent);
20+
}
21+
22+
get fileName(): string {
23+
return this.file.fileName;
2024
}
2125

2226
get ref() {
23-
return this.ref1 ? this.ref1 : this.ref2 ? this.ref2 : undefined;
27+
return this.ref1 || this.ref2;
2428
}
2529

2630
getChildren(): ViewNode[] {

src/views/nodes/viewNode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export abstract class ViewRefNode<TView extends View = View> extends ViewNode<TV
113113
}
114114
}
115115

116+
export abstract class ViewRefFileNode<TView extends View = View> extends ViewRefNode<TView> {
117+
abstract get fileName(): string;
118+
}
119+
116120
export interface PageableViewNode {
117121
readonly supportsPaging: boolean;
118122
maxCount: number | undefined;

src/views/viewCommands.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
StatusFileNode,
3535
TagNode,
3636
ViewNode,
37+
ViewRefFileNode,
3738
ViewRefNode,
3839
viewSupportsNodeDismissal
3940
} from './nodes';
@@ -210,10 +211,8 @@ export class ViewCommands implements Disposable {
210211
return node.push({ force: force });
211212
}
212213

213-
private async applyChanges(node: CommitFileNode | StashFileNode | ResultsFileNode) {
214-
if (!(node instanceof CommitFileNode) && !(node instanceof ResultsFileNode)) {
215-
return;
216-
}
214+
private async applyChanges(node: ViewRefFileNode) {
215+
if (!(node instanceof ViewRefFileNode)) return;
217216

218217
void (await this.openFile(node));
219218

@@ -228,9 +227,13 @@ export class ViewCommands implements Disposable {
228227
}
229228
}
230229

231-
private checkout(node: ViewRefNode) {
230+
private checkout(node: ViewRefNode | ViewRefFileNode) {
232231
if (!(node instanceof ViewRefNode)) return undefined;
233232

233+
if (node instanceof ViewRefFileNode) {
234+
return Container.git.checkout(node.repoPath, node.ref, node.fileName);
235+
}
236+
234237
return Container.git.checkout(node.repoPath, node.ref);
235238
}
236239

@@ -287,14 +290,11 @@ export class ViewCommands implements Disposable {
287290
Container.compareView.selectForCompare(node.repoPath, node.ref);
288291
}
289292

290-
private compareFileWithSelected(node: CommitFileNode | ResultsFileNode | StashFileNode) {
291-
if (
292-
this._selectedFile === undefined ||
293-
(!(node instanceof CommitFileNode) && !(node instanceof ResultsFileNode)) ||
294-
node.ref === undefined
295-
) {
293+
private compareFileWithSelected(node: ViewRefFileNode) {
294+
if (this._selectedFile === undefined || !(node instanceof ViewRefFileNode) || node.ref === undefined) {
296295
return undefined;
297296
}
297+
298298
if (this._selectedFile.repoPath !== node.repoPath) {
299299
this.selectFileForCompare(node);
300300
return undefined;
@@ -321,8 +321,8 @@ export class ViewCommands implements Disposable {
321321

322322
private _selectedFile: CompareSelectedInfo | undefined;
323323

324-
private selectFileForCompare(node: CommitFileNode | ResultsFileNode | StashFileNode) {
325-
if ((!(node instanceof CommitFileNode) && !(node instanceof ResultsFileNode)) || node.ref === undefined) return;
324+
private selectFileForCompare(node: ViewRefFileNode) {
325+
if (!(node instanceof ViewRefFileNode) || node.ref === undefined) return;
326326

327327
this._selectedFile = {
328328
ref: node.ref,
@@ -343,8 +343,8 @@ export class ViewCommands implements Disposable {
343343
void commands.executeCommand(BuiltInCommands.FocusFilesExplorer);
344344
}
345345

346-
private openChanges(node: CommitFileNode | ResultsFileNode | StashFileNode) {
347-
if (!(node instanceof CommitFileNode) && !(node instanceof ResultsFileNode)) return undefined;
346+
private openChanges(node: ViewRefFileNode) {
347+
if (!(node instanceof ViewRefFileNode)) return undefined;
348348

349349
const command = node.getCommand();
350350
if (command === undefined || command.arguments === undefined) return undefined;
@@ -354,8 +354,8 @@ export class ViewCommands implements Disposable {
354354
return commands.executeCommand(command.command, uri, args);
355355
}
356356

357-
private async openChangesWithWorking(node: CommitFileNode | ResultsFileNode | StashFileNode) {
358-
if (!(node instanceof CommitFileNode) && !(node instanceof ResultsFileNode)) return undefined;
357+
private async openChangesWithWorking(node: ViewRefFileNode) {
358+
if (!(node instanceof ViewRefFileNode)) return undefined;
359359

360360
const args: DiffWithWorkingCommandArgs = {
361361
showOptions: {
@@ -375,15 +375,12 @@ export class ViewCommands implements Disposable {
375375
return commands.executeCommand(Commands.DiffWithWorking, node.uri, args);
376376
}
377377

378-
private openFile(
379-
node: CommitFileNode | FileHistoryNode | LineHistoryNode | ResultsFileNode | StashFileNode | StatusFileNode
380-
) {
378+
private openFile(node: ViewRefFileNode | StatusFileNode | FileHistoryNode | LineHistoryNode) {
381379
if (
382-
!(node instanceof CommitFileNode) &&
380+
!(node instanceof ViewRefFileNode) &&
381+
!(node instanceof StatusFileNode) &&
383382
!(node instanceof FileHistoryNode) &&
384-
!(node instanceof LineHistoryNode) &&
385-
!(node instanceof ResultsFileNode) &&
386-
!(node instanceof StatusFileNode)
383+
!(node instanceof LineHistoryNode)
387384
) {
388385
return undefined;
389386
}

0 commit comments

Comments
 (0)