Skip to content

Commit 31df505

Browse files
committed
Avoids requiring an open repo
- Using `getRepository` requires that the repo to be open, but many commands can work against non-opened repos
1 parent 9bcc02f commit 31df505

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/commands/browseRepoAtRevision.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ export class BrowseRepoAtRevisionCommand extends ActiveEditorCommand {
5656
}
5757

5858
let gitUri = await GitUri.fromUri(uri);
59-
if (gitUri.sha == null) throw new Error('No SHA for Uri');
59+
if (gitUri.repoPath == null || gitUri.sha == null) throw new Error('No repo or SHA for Uri');
6060

61-
const repo = this.container.git.getRepository(gitUri.repoPath!);
62-
if (repo == null) throw new Error('No repository for Uri');
61+
const { git } = this.container;
6362

64-
const sha = args?.before ? (await repo.git.revision().resolveRevision(`${gitUri.sha}^`)).sha : gitUri.sha;
65-
uri = repo.git.getRevisionUri(sha, gitUri.repoPath!);
63+
const sha = args?.before
64+
? (await git.revision(gitUri.repoPath).resolveRevision(`${gitUri.sha}^`)).sha
65+
: gitUri.sha;
66+
uri = git.getRevisionUri(gitUri.repoPath, sha, gitUri.repoPath);
6667
gitUri = GitUri.fromRevisionUri(uri);
6768

6869
openWorkspace(uri, {

src/commands/diffWith.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ export class DiffWithCommand extends GlCommandBase {
7474

7575
async execute(args?: DiffWithCommandArgs): Promise<any> {
7676
if (args?.lhs == null || args?.rhs == null) return;
77+
if (args.repoPath == null) {
78+
debugger;
79+
return;
80+
}
7781

78-
const repo = args.repoPath ? this.container.git.getRepository(args.repoPath) : undefined;
79-
if (repo == null) return;
82+
const { git } = this.container;
8083

8184
try {
8285
let {
@@ -86,8 +89,8 @@ export class DiffWithCommand extends GlCommandBase {
8689
const showOptions = { viewColumn: ViewColumn.Active, ...args.showOptions };
8790

8891
let [lhsResolvedResult, rhsResolvedResult] = await Promise.allSettled([
89-
repo.git.revision().resolveRevision(lhsSha, lhsUri),
90-
repo.git.revision().resolveRevision(rhsSha, rhsUri),
92+
git.revision(args.repoPath).resolveRevision(lhsSha, lhsUri),
93+
git.revision(args.repoPath).resolveRevision(rhsSha, rhsUri),
9194
]);
9295

9396
let lhsResolved = getSettledValue(lhsResolvedResult)!;
@@ -96,8 +99,8 @@ export class DiffWithCommand extends GlCommandBase {
9699
// If both are missing, check for renames by swapping the paths
97100
if (lhsResolved.sha === deletedOrMissing && rhsResolved.sha === deletedOrMissing) {
98101
[lhsResolvedResult, rhsResolvedResult] = await Promise.allSettled([
99-
repo?.git.revision().resolveRevision(lhsSha, rhsUri),
100-
repo?.git.revision().resolveRevision(rhsSha, lhsUri),
102+
git.revision(args.repoPath).resolveRevision(lhsSha, rhsUri),
103+
git.revision(args.repoPath).resolveRevision(rhsSha, lhsUri),
101104
]);
102105

103106
lhsResolved = getSettledValue(lhsResolvedResult)!;
@@ -118,8 +121,8 @@ export class DiffWithCommand extends GlCommandBase {
118121
}
119122

120123
const [lhsResult, rhsResult] = await Promise.allSettled([
121-
repo.git.getBestRevisionUri(lhsUri.fsPath, lhsResolved.sha),
122-
repo.git.getBestRevisionUri(rhsUri.fsPath, rhsResolved.sha),
124+
git.getBestRevisionUri(args.repoPath, lhsUri.fsPath, lhsResolved.sha),
125+
git.getBestRevisionUri(args.repoPath, rhsUri.fsPath, rhsResolved.sha),
123126
]);
124127

125128
const lhs = getSettledValue(lhsResult);
@@ -168,8 +171,8 @@ export class DiffWithCommand extends GlCommandBase {
168171
}
169172

170173
await openDiffEditor(
171-
lhs ?? repo.git.getRevisionUri(deletedOrMissing, args.lhs.uri.fsPath),
172-
rhs ?? repo.git.getRevisionUri(deletedOrMissing, args.rhs.uri.fsPath),
174+
lhs ?? git.getRevisionUri(args.repoPath, deletedOrMissing, args.lhs.uri.fsPath),
175+
rhs ?? git.getRevisionUri(args.repoPath, deletedOrMissing, args.rhs.uri.fsPath),
173176
title,
174177
showOptions,
175178
);

src/git/models/commit.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,11 @@ export class GitCommit implements GitRevisionReference {
291291
}): Promise<void> {
292292
if (this.hasFullDetails(options)) return;
293293

294-
const repo = this.container.git.getRepository(this.repoPath);
294+
const { git } = this.container;
295295

296296
// If the commit is "uncommitted", then have the files list be all uncommitted files
297297
if (this.isUncommitted) {
298+
const repo = this.container.git.getRepository(this.repoPath);
298299
this._etagFileSystem = repo?.etagFileSystem;
299300

300301
if (this._etagFileSystem != null || options?.include?.uncommittedFiles) {
@@ -331,7 +332,7 @@ export class GitCommit implements GitRevisionReference {
331332

332333
if (this.refType === 'stash') {
333334
const [stashFilesResult] = await Promise.allSettled([
334-
repo?.git.stash()?.getStashCommitFiles(this.sha),
335+
git.stash(this.repoPath)?.getStashCommitFiles(this.sha),
335336
this.getPreviousSha(),
336337
]);
337338

@@ -342,7 +343,7 @@ export class GitCommit implements GitRevisionReference {
342343
this._stashUntrackedFilesLoaded = true;
343344
} else {
344345
const [commitResult] = await Promise.allSettled([
345-
repo?.git.commits().getCommit(this.sha),
346+
git.commits(this.repoPath).getCommit(this.sha),
346347
this.getPreviousSha(),
347348
]);
348349

src/quickpicks/comparisonPicker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ export async function showComparisonPicker(
7777

7878
if (isBranchReference(head)) {
7979
// get the merge target for the branch
80-
const repo = container.git.getRepository(repoPath);
81-
const branch = await repo?.git.branches().getBranch(head.name);
80+
const branch = await container.git.branches(repoPath).getBranch(head.name);
8281
if (branch != null) {
8382
const info = await getBranchMergeTargetInfo(container, branch);
8483
let target;

0 commit comments

Comments
 (0)