Skip to content

Commit 082b2c1

Browse files
committed
Fixes #3050 handles renamed file in working tree
1 parent 8c184cc commit 082b2c1

File tree

3 files changed

+34
-46
lines changed

3 files changed

+34
-46
lines changed

CHANGELOG.md

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

3333
### Fixed
3434

35+
- Fixes [#3050](https://github.com/gitkraken/vscode-gitlens/issues/3050) - Opening revision of a renamed file is broken
3536
- Fixes [#3019](https://github.com/gitkraken/vscode-gitlens/issues/3019) - Commits Views not working
3637
- Fixes [#3026](https://github.com/gitkraken/vscode-gitlens/issues/3026) - Gitlens stopped working in sub-repositories
3738
- Fixes [#2746](https://github.com/gitkraken/vscode-gitlens/issues/2746) - Remove 'undo commit' command from gitlens inspect

src/env/node/git/git.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ export class Git {
20912091
async status(
20922092
repoPath: string,
20932093
porcelainVersion: number = 1,
2094-
{ similarityThreshold }: { similarityThreshold?: number | null } = {},
2094+
options?: { similarityThreshold?: number },
20952095
): Promise<string> {
20962096
const params = [
20972097
'status',
@@ -2100,7 +2100,9 @@ export class Git {
21002100
'-u',
21012101
];
21022102
if (await this.isAtLeastVersion('2.18')) {
2103-
params.push(`--find-renames${similarityThreshold == null ? '' : `=${similarityThreshold}%`}`);
2103+
params.push(
2104+
`--find-renames${options?.similarityThreshold == null ? '' : `=${options.similarityThreshold}%`}`,
2105+
);
21042106
}
21052107

21062108
return this.git<string>(
@@ -2110,27 +2112,6 @@ export class Git {
21102112
);
21112113
}
21122114

2113-
async status__file(
2114-
repoPath: string,
2115-
fileName: string,
2116-
porcelainVersion: number = 1,
2117-
{ similarityThreshold }: { similarityThreshold?: number | null } = {},
2118-
): Promise<string> {
2119-
const [file, root] = splitPath(fileName, repoPath, true);
2120-
2121-
const params = ['status', porcelainVersion >= 2 ? `--porcelain=v${porcelainVersion}` : '--porcelain'];
2122-
if (await this.isAtLeastVersion('2.18')) {
2123-
params.push(`--find-renames${similarityThreshold == null ? '' : `=${similarityThreshold}%`}`);
2124-
}
2125-
2126-
return this.git<string>(
2127-
{ cwd: root, configs: gitStatusDefaultConfigs, env: { GIT_OPTIONAL_LOCKS: '0' } },
2128-
...params,
2129-
'--',
2130-
file,
2131-
);
2132-
}
2133-
21342115
symbolic_ref(repoPath: string, ref: string) {
21352116
return this.git<string>({ cwd: repoPath }, 'symbolic-ref', '--short', ref);
21362117
}

src/env/node/git/localGitProvider.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import type {
4949
RevisionUriData,
5050
ScmRepository,
5151
} from '../../../git/gitProvider';
52-
import { encodeGitLensRevisionUriAuthority, GitUri } from '../../../git/gitUri';
52+
import { encodeGitLensRevisionUriAuthority, GitUri, isGitUri } from '../../../git/gitUri';
5353
import type { GitBlame, GitBlameAuthor, GitBlameLine, GitBlameLines } from '../../../git/models/blame';
5454
import type { BranchSortOptions } from '../../../git/models/branch';
5555
import {
@@ -4744,31 +4744,28 @@ export class LocalGitProvider implements GitProvider, Disposable {
47444744
}
47454745

47464746
@log()
4747-
async getStatusForFile(repoPath: string, uri: Uri): Promise<GitStatusFile | undefined> {
4748-
const porcelainVersion = (await this.git.isAtLeastVersion('2.11')) ? 2 : 1;
4749-
4750-
const [relativePath, root] = splitPath(uri, repoPath);
4751-
4752-
const data = await this.git.status__file(root, relativePath, porcelainVersion, {
4753-
similarityThreshold: configuration.get('advanced.similarityThreshold'),
4754-
});
4747+
async getStatusForFile(repoPath: string, pathOrUri: string | Uri): Promise<GitStatusFile | undefined> {
4748+
const status = await this.getStatusForRepo(repoPath);
4749+
if (!status?.files.length) return undefined;
47554750

4756-
const status = parseGitStatus(data, root, porcelainVersion);
4757-
return status?.files?.[0];
4751+
const [relativePath] = splitPath(pathOrUri, repoPath);
4752+
const file = status.files.find(f => f.path === relativePath);
4753+
return file;
47584754
}
47594755

47604756
@log()
47614757
async getStatusForFiles(repoPath: string, pathOrGlob: Uri): Promise<GitStatusFile[] | undefined> {
4762-
const porcelainVersion = (await this.git.isAtLeastVersion('2.11')) ? 2 : 1;
4763-
4764-
const [relativePath, root] = splitPath(pathOrGlob, repoPath);
4758+
let [relativePath] = splitPath(pathOrGlob, repoPath);
4759+
if (!relativePath.endsWith('/*')) {
4760+
return this.getStatusForFile(repoPath, pathOrGlob).then(f => (f != null ? [f] : undefined));
4761+
}
47654762

4766-
const data = await this.git.status__file(root, relativePath, porcelainVersion, {
4767-
similarityThreshold: configuration.get('advanced.similarityThreshold'),
4768-
});
4763+
relativePath = relativePath.substring(0, relativePath.length - 1);
4764+
const status = await this.getStatusForRepo(repoPath);
4765+
if (!status?.files.length) return undefined;
47694766

4770-
const status = parseGitStatus(data, root, porcelainVersion);
4771-
return status?.files ?? [];
4767+
const files = status.files.filter(f => f.path.startsWith(relativePath));
4768+
return files;
47724769
}
47734770

47744771
@log()
@@ -4778,7 +4775,7 @@ export class LocalGitProvider implements GitProvider, Disposable {
47784775
const porcelainVersion = (await this.git.isAtLeastVersion('2.11')) ? 2 : 1;
47794776

47804777
const data = await this.git.status(repoPath, porcelainVersion, {
4781-
similarityThreshold: configuration.get('advanced.similarityThreshold'),
4778+
similarityThreshold: configuration.get('advanced.similarityThreshold') ?? undefined,
47824779
});
47834780
const status = parseGitStatus(data, repoPath, porcelainVersion);
47844781

@@ -4935,13 +4932,13 @@ export class LocalGitProvider implements GitProvider, Disposable {
49354932
if (ref === deletedOrMissing) return undefined;
49364933

49374934
repository = this.container.git.getRepository(Uri.file(pathOrUri));
4938-
repoPath = repoPath || repository?.path;
4935+
repoPath ||= repository?.path;
49394936

49404937
[relativePath, repoPath] = splitPath(pathOrUri, repoPath);
49414938
} else {
49424939
if (!this.isTrackable(pathOrUri)) return undefined;
49434940

4944-
if (pathOrUri instanceof GitUri) {
4941+
if (isGitUri(pathOrUri)) {
49454942
// Always use the ref of the GitUri
49464943
ref = pathOrUri.sha;
49474944
if (ref === deletedOrMissing) return undefined;
@@ -5033,9 +5030,18 @@ export class LocalGitProvider implements GitProvider, Disposable {
50335030

50345031
continue;
50355032
}
5033+
5034+
return undefined;
50365035
}
50375036

5038-
return undefined;
5037+
if (ref != null) return undefined;
5038+
5039+
// If we still didn't find anything then check if we've been renamed first
5040+
const status = await this.getStatusForFile(repoPath, relativePath);
5041+
if (status?.originalPath != null) {
5042+
tracked = Boolean(await this.git.ls_files(repoPath, status.originalPath, { ref: 'HEAD' }));
5043+
if (!tracked) return undefined;
5044+
}
50395045
}
50405046

50415047
return [relativePath, repoPath];

0 commit comments

Comments
 (0)