Skip to content

Commit c7619f5

Browse files
committed
Improves branch worktree checks
Refs #4201
1 parent 2452be2 commit c7619f5

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

src/env/node/git/sub-providers/branches.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
133133
if (resultsPromise == null) {
134134
async function load(this: BranchesGitSubProvider): Promise<PagedResult<GitBranch>> {
135135
try {
136-
const parser = getBranchParser(await this.git.supports('git:for-each-ref:worktreePath'));
136+
const supportsWorktreePath = await this.git.supports('git:for-each-ref:worktreePath');
137+
const parser = getBranchParser(supportsWorktreePath);
137138

138139
const data = await this.git.exec(
139140
{ cwd: repoPath },
@@ -178,11 +179,10 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
178179
entry.date ? new Date(entry.date) : undefined,
179180
entry.sha,
180181
upstream,
181-
worktreePath
182-
? {
183-
path: worktreePath,
184-
isDefault: worktreePath === defaultWorktreePath,
185-
}
182+
supportsWorktreePath
183+
? worktreePath
184+
? { path: worktreePath, isDefault: worktreePath === defaultWorktreePath }
185+
: false
186186
: undefined,
187187
),
188188
);
@@ -488,7 +488,9 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
488488

489489
return { merged: false };
490490
} catch (ex) {
491-
Logger.error(ex, scope);
491+
if (Logger.enabled('debug')) {
492+
Logger.error(ex, scope);
493+
}
492494
return { merged: false };
493495
}
494496
}

src/git/models/branch.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class GitBranch implements GitBranchReference {
4848
public readonly date: Date | undefined,
4949
public readonly sha?: string,
5050
public readonly upstream?: GitTrackingUpstream,
51-
public readonly worktree?: { path: string; isDefault: boolean },
51+
public readonly worktree?: { path: string; isDefault: boolean } | false,
5252
detached: boolean = false,
5353
public readonly rebasing: boolean = false,
5454
) {
@@ -178,10 +178,14 @@ export class GitBranch implements GitBranchReference {
178178

179179
@debug()
180180
async getWorktree(): Promise<GitWorktree | undefined> {
181-
if (this.worktree == null) return undefined;
181+
if (this.worktree === false) return undefined;
182+
if (this.worktree == null) {
183+
const { id } = this;
184+
return this.container.git.worktrees(this.repoPath)?.getWorktree(wt => wt.branch?.id === id);
185+
}
182186

183-
const worktreePath = this.worktree.path;
184-
return this.container.git.worktrees(this.repoPath)?.getWorktree(wt => wt.path === worktreePath);
187+
const { path } = this.worktree;
188+
return this.container.git.worktrees(this.repoPath)?.getWorktree(wt => wt.path === path);
185189
}
186190

187191
get starred(): boolean {

src/plus/integrations/providers/github/sub-providers/branches.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
6262
undefined,
6363
revision.revision,
6464
undefined,
65-
undefined,
65+
false,
6666
true,
6767
);
6868
}
@@ -112,12 +112,30 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
112112
const ref = branch.target.oid;
113113

114114
branches.push(
115-
new GitBranch(container, repoPath!, `refs/heads/${branch.name}`, current, date, ref, {
116-
name: `origin/${branch.name}`,
117-
missing: false,
118-
state: { ahead: 0, behind: 0 },
119-
}),
120-
new GitBranch(container, repoPath!, `refs/remotes/origin/${branch.name}`, false, date, ref),
115+
new GitBranch(
116+
container,
117+
repoPath!,
118+
`refs/heads/${branch.name}`,
119+
current,
120+
date,
121+
ref,
122+
{
123+
name: `origin/${branch.name}`,
124+
missing: false,
125+
state: { ahead: 0, behind: 0 },
126+
},
127+
false,
128+
),
129+
new GitBranch(
130+
container,
131+
repoPath!,
132+
`refs/remotes/origin/${branch.name}`,
133+
false,
134+
date,
135+
ref,
136+
undefined,
137+
false,
138+
),
121139
);
122140
}
123141

src/webviews/home/homeWebview.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,9 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
12011201
const { repo, branch } = await this.getRepoInfoFromRef(ref);
12021202
if (branch == null) return;
12031203

1204-
if (branch.current && mergeTarget != null && (!branch.worktree || branch.worktree.isDefault)) {
1204+
const worktree = branch.worktree === false ? undefined : branch.worktree ?? (await branch.getWorktree());
1205+
1206+
if (branch.current && mergeTarget != null && (!worktree || worktree.isDefault)) {
12051207
const mergeTargetLocalBranchName = getBranchNameWithoutRemote(mergeTarget.branchName);
12061208
const confirm = await window.showWarningMessage(
12071209
`Before deleting the current branch '${branch.name}', you will be switched to '${mergeTargetLocalBranchName}'.`,
@@ -1221,7 +1223,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
12211223
references: branch,
12221224
},
12231225
});
1224-
} else if (repo != null && branch?.worktree != null && !branch.worktree.isDefault) {
1226+
} else if (repo != null && worktree != null && !worktree.isDefault) {
12251227
const commonRepo = await repo.getCommonRepository();
12261228
const defaultWorktree = await repo.git.worktrees?.()?.getWorktree(w => w.isDefault);
12271229
if (defaultWorktree == null || commonRepo == null) return;

0 commit comments

Comments
 (0)