Skip to content

Fixes #708 - Prompt for file path when missing from revision #2825

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

Merged
merged 5 commits into from
Dec 6, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Added

- Adds support for opening renamed/deleted files using the _Open File at Revision..._ & _Open File at Revision from..._ commands by showing a quick pick menu if the requested file doesn't exist in the selected revision — closes [#708](https://github.com/gitkraken/vscode-gitlens/issues/708) thanks to [PR #2825](https://github.com/gitkraken/vscode-gitlens/pull/2825) by Victor Hallberg ([@mogelbrod](https://github.com/mogelbrod))

### Changed

- Changes blame to show the last modified time of the file for uncommitted changes
Expand Down
32 changes: 17 additions & 15 deletions src/commands/diffWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
'Choose a commit to compare with',
{
picked: gitUri.sha,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
void (await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: item.item.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args!.line,
showOptions: args!.showOptions,
}));
keyboard: {
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
await executeCommand<DiffWithCommandArgs>(Commands.DiffWith, {
repoPath: gitUri.repoPath,
lhs: {
sha: item.item.ref,
uri: gitUri,
},
rhs: {
sha: '',
uri: gitUri,
},
line: args!.line,
showOptions: args!.showOptions,
});
},
},
showOtherReferences: [
CommandQuickPickItem.fromCommand('Choose a Branch or Tag...', Commands.DiffWithRevisionFrom),
Expand Down
14 changes: 10 additions & 4 deletions src/commands/gitCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ export class GitCommandsCommand extends Command {

const scope = this.container.keyboard.createScope(mapping);
void scope.start();
if (step.value != null) {
void scope.pause(['left', 'ctrl+left', 'right', 'ctrl+right']);
}

disposables.push(
scope,
Expand Down Expand Up @@ -404,9 +407,9 @@ export class GitCommandsCommand extends Command {
if (scope != null) {
// Pause the left/right keyboard commands if there is a value, otherwise the left/right arrows won't work in the input properly
if (e.length !== 0) {
await scope.pause(['left', 'right']);
void scope.pause(['left', 'ctrl+left', 'right', 'ctrl+right']);
} else {
await scope.resume();
void scope.resume();
}
}

Expand Down Expand Up @@ -521,6 +524,9 @@ export class GitCommandsCommand extends Command {

const scope = this.container.keyboard.createScope(mapping);
void scope.start();
if (step.value != null) {
void scope.pause(['left', 'ctrl+left', 'right', 'ctrl+right']);
}

let overrideItems = false;

Expand Down Expand Up @@ -592,9 +598,9 @@ export class GitCommandsCommand extends Command {
if (scope != null) {
// Pause the left/right keyboard commands if there is a value, otherwise the left/right arrows won't work in the input properly
if (e.length !== 0) {
await scope.pause(['left', 'right']);
void scope.pause(['left', 'ctrl+left', 'right', 'ctrl+right']);
} else {
await scope.resume();
void scope.resume();
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
`Choose a commit to ${args.annotationType === 'blame' ? 'blame' : 'open'} the file revision from`,
{
picked: gitUri.sha,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
await openFileAtRevision(item.item.file!, item.item, {
annotationType: args!.annotationType,
line: args!.line,
preserveFocus: true,
preview: false,
});
keyboard: {
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
await openFileAtRevision(item.item.file!, item.item, {
annotationType: args!.annotationType,
line: args!.line,
preserveFocus: true,
preview: true,
});
},
},
showOtherReferences: [
CommandQuickPickItem.fromCommand(
Expand Down
11 changes: 5 additions & 6 deletions src/commands/openFileAtRevisionFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,19 @@ export class OpenFileAtRevisionFromCommand extends ActiveEditorCommand {
'Choose a branch or tag to open the file revision from',
{
allowEnteringRefs: true,
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, quickpick) => {
const [item] = quickpick.activeItems;
if (item != null) {
keyboard: {
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, item) => {
await openFileAtRevision(
this.container.git.getRevisionUri(item.ref, gitUri.fsPath, gitUri.repoPath!),
{
annotationType: args!.annotationType,
line: args!.line,
preserveFocus: true,
preview: false,
preview: true,
},
);
}
},
},
},
);
Expand Down
8 changes: 4 additions & 4 deletions src/env/node/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ export class Git {
);
}

async cat_file__size(repoPath: string, object: string): Promise<number> {
const data = await this.git<string>({ cwd: repoPath }, 'cat-file', '-s', object);
async cat_file__size(repoPath: string, oid: string): Promise<number> {
const data = await this.git<string>({ cwd: repoPath }, 'cat-file', '-s', oid);
return data.length ? parseInt(data.trim(), 10) : 0;
}

Expand Down Expand Up @@ -1343,13 +1343,13 @@ export class Git {

async log__find_object(
repoPath: string,
objectId: string,
oid: string,
ref: string,
ordering: 'date' | 'author-date' | 'topo' | null,
file?: string,
cancellation?: CancellationToken,
) {
const params = ['log', '-n1', '--no-renames', '--format=%H', `--find-object=${objectId}`, ref];
const params = ['log', '-n1', '--no-renames', '--format=%H', `--find-object=${oid}`, ref];

if (ordering) {
params.push(`--${ordering}-order`);
Expand Down
9 changes: 5 additions & 4 deletions src/env/node/git/localGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4844,25 +4844,26 @@ export class LocalGitProvider implements GitProvider, Disposable {
const [result] = parseGitLsFiles(data);
if (result == null) return undefined;

const size = await this.git.cat_file__size(repoPath, result.object);
const size = await this.git.cat_file__size(repoPath, result.oid);
return {
commitSha: ref,
ref: ref,
oid: result.oid,
path: relativePath,
size: size,
type: 'blob',
};
}

const data = await this.git.ls_tree(root, ref, relativePath);
return parseGitTree(data)[0];
return parseGitTree(data, ref)[0];
}

@log()
async getTreeForRevision(repoPath: string, ref: string): Promise<GitTreeEntry[]> {
if (repoPath == null) return [];

const data = await this.git.ls_tree(repoPath, ref);
return parseGitTree(data);
return parseGitTree(data, ref);
}

@log({ args: { 1: false } })
Expand Down
33 changes: 30 additions & 3 deletions src/git/actions/commit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TextDocumentShowOptions } from 'vscode';
import type { TextDocumentShowOptions, TextEditor } from 'vscode';
import { env, Range, Uri, window, workspace } from 'vscode';
import type { DiffWithCommandArgs } from '../../commands/diffWith';
import type { DiffWithPreviousCommandArgs } from '../../commands/diffWithPrevious';
Expand All @@ -12,6 +12,7 @@ import type { FileAnnotationType } from '../../config';
import { Commands, GlyphChars } from '../../constants';
import { Container } from '../../container';
import type { ShowInCommitGraphCommandArgs } from '../../plus/webviews/graph/protocol';
import { showRevisionPicker } from '../../quickpicks/revisionPicker';
import { executeCommand, executeEditorCommand } from '../../system/command';
import { configuration } from '../../system/configuration';
import { findOrOpenEditor, findOrOpenEditors, openChangesEditor } from '../../system/utils';
Expand Down Expand Up @@ -463,7 +464,7 @@ export async function openFileAtRevision(
commitOrOptions?: GitCommit | TextDocumentShowOptions,
options?: TextDocumentShowOptions & { annotationType?: FileAnnotationType; line?: number },
): Promise<void> {
let uri;
let uri: Uri;
if (fileOrRevisionUri instanceof Uri) {
if (isCommit(commitOrOptions)) throw new Error('Invalid arguments');

Expand Down Expand Up @@ -501,7 +502,33 @@ export async function openFileAtRevision(
opts.selection = new Range(line, 0, line, 0);
}

const editor = await findOrOpenEditor(uri, opts);
const gitUri = await GitUri.fromUri(uri);

let editor: TextEditor | undefined;
try {
editor = await findOrOpenEditor(uri, { throwOnError: true, ...opts });
} catch (ex) {
if (!ex?.message?.includes('Unable to resolve nonexistent file')) {
void window.showErrorMessage(`Unable to open '${gitUri.relativePath}' in revision '${gitUri.sha}'`);
return;
}

const pickedUri = await showRevisionPicker(Container.instance, gitUri, {
ignoreFocusOut: true,
title: `Open File at Revision \u2022 Unable to open '${gitUri.relativePath}'`,
placeholder: 'Choose a file revision to open',
keyboard: {
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: async (key, uri) => {
await findOrOpenEditor(uri, { ...opts, preserveFocus: true, preview: true });
},
},
});
if (pickedUri == null) return;

editor = await findOrOpenEditor(pickedUri, opts);
}

if (annotationType != null && editor != null) {
void (await Container.instance.fileAnnotations.show(editor, annotationType, {
selection: { line: line },
Expand Down
2 changes: 1 addition & 1 deletion src/git/fsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class GitFileSystemProvider implements FileSystemProvider, Disposable {
const trees = await this.container.git.getTreeForRevision(repoPath, ref);

// Add a fake root folder so that searches will work
searchTree.set('~', { commitSha: '', path: '~', size: 0, type: 'tree' });
searchTree.set('~', { ref: '', oid: '', path: '~', size: 0, type: 'tree' });
for (const item of trees) {
searchTree.set(`~/${item.path}`, item);
}
Expand Down
5 changes: 3 additions & 2 deletions src/git/models/tree.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
export interface GitTreeEntry {
commitSha: string;
ref: string;
oid: string;
path: string;
size: number;
type: 'blob' | 'tree';
}

export interface GitLsFilesEntry {
mode: string;
oid: string;
path: string;
object: string;
stage: number;
}
19 changes: 10 additions & 9 deletions src/git/parsers/treeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type { GitLsFilesEntry, GitTreeEntry } from '../models/tree';
const treeRegex = /(?:.+?)\s+(.+?)\s+(.+?)\s+(.+?)\s+(.+)/gm;
const filesRegex = /^(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/gm;

export function parseGitTree(data: string | undefined): GitTreeEntry[] {
export function parseGitTree(data: string | undefined, ref: string): GitTreeEntry[] {
using sw = maybeStopWatch(`Git.parseTree`, { log: false, logLevel: 'debug' });

const trees: GitTreeEntry[] = [];
if (!data) return trees;

let type;
let sha;
let oid;
let size;
let filePath;

Expand All @@ -20,11 +20,12 @@ export function parseGitTree(data: string | undefined): GitTreeEntry[] {
match = treeRegex.exec(data);
if (match == null) break;

[, type, sha, size, filePath] = match;
[, type, oid, size, filePath] = match;

trees.push({
ref: ref,
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
commitSha: sha == null || sha.length === 0 ? '' : ` ${sha}`.substr(1),
oid: oid == null || oid.length === 0 ? '' : ` ${oid}`.substr(1),
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
path: filePath == null || filePath.length === 0 ? '' : ` ${filePath}`.substr(1),
size: Number(size) || 0,
Expand All @@ -46,23 +47,23 @@ export function parseGitLsFiles(data: string | undefined): GitLsFilesEntry[] {

let filePath;
let mode;
let object;
let oid;
let stage;

let match;
do {
match = filesRegex.exec(data);
if (match == null) break;

[, mode, object, stage, filePath] = match;
[, mode, oid, stage, filePath] = match;

files.push({
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
path: filePath == null || filePath.length === 0 ? '' : ` ${filePath}`.substr(1),
mode: mode == null || mode.length === 0 ? '' : ` ${mode}`.substr(1),
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
object: object == null || object.length === 0 ? '' : ` ${object}`.substr(1),
oid: oid == null || oid.length === 0 ? '' : ` ${oid}`.substr(1),
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
mode: mode == null || mode.length === 0 ? '' : ` ${mode}`.substr(1),
path: filePath == null || filePath.length === 0 ? '' : ` ${filePath}`.substr(1),
stage: parseInt(stage, 10),
});
} while (true);
Expand Down
6 changes: 4 additions & 2 deletions src/plus/integrations/providers/github/githubGitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2740,8 +2740,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
if (stats == null) return undefined;

return {
ref: ref,
oid: '',
path: this.getRelativePath(uri, repoPath),
commitSha: ref,
size: stats.size,
type: (stats.type & FileType.Directory) === FileType.Directory ? 'tree' : 'blob',
};
Expand Down Expand Up @@ -2772,8 +2773,9 @@ export class GitHubGitProvider implements GitProvider, Disposable {
// const stats = await workspace.fs.stat(uri);

result.push({
ref: ref,
oid: '',
path: this.getRelativePath(path, uri),
commitSha: ref,
size: 0, // stats?.size,
type: (type & FileType.Directory) === FileType.Directory ? 'tree' : 'blob',
});
Expand Down
Loading