@@ -204,6 +204,11 @@ import {
204204} from '../../ui/lib/diff-mode'
205205import { pathExists } from '../../ui/lib/path-exists'
206206import { updateStore } from '../../ui/lib/update-store'
207+ import {
208+ getPreferredWorktreePath ,
209+ clearPreferredWorktreePath ,
210+ } from '../worktree-preferences'
211+ import { normalizePath } from '../helpers/path'
207212import { resizableComponentClass } from '../../ui/resizable'
208213import { BypassReasonType } from '../../ui/secret-scanning/bypass-push-protection-dialog'
209214import { findContributionTargetDefaultBranch } from '../branch'
@@ -2160,20 +2165,57 @@ export class AppStore extends TypedBaseStore<IAppState> {
21602165
21612166 this . selectedRepository = repository
21622167
2163- this . emitUpdate ( )
21642168 this . stopBackgroundFetching ( )
21652169 this . stopPullRequestUpdater ( )
21662170 this . _clearBanner ( )
21672171 this . stopBackgroundPruner ( )
21682172
21692173 if ( repository == null ) {
2174+ this . emitUpdate ( )
21702175 return Promise . resolve ( null )
21712176 }
21722177
21732178 if ( ! ( repository instanceof Repository ) ) {
2179+ this . emitUpdate ( )
21742180 return Promise . resolve ( null )
21752181 }
21762182
2183+ // When returning to a repository that has worktrees, restore the
2184+ // previously active linked worktree so the user doesn't always land
2185+ // on the main worktree after switching repos.
2186+ if ( ! repository . isLinkedWorktree ) {
2187+ const repoPath = normalizePath ( repository . path )
2188+ const preferredPath = getPreferredWorktreePath ( repoPath )
2189+
2190+ if ( preferredPath && preferredPath !== repoPath ) {
2191+ const linkedRepo = this . repositories . find (
2192+ r =>
2193+ r instanceof Repository && normalizePath ( r . path ) === preferredPath
2194+ )
2195+
2196+ if ( linkedRepo instanceof Repository ) {
2197+ repository = linkedRepo
2198+ this . selectedRepository = repository
2199+ } else {
2200+ const exists = await pathExists ( preferredPath )
2201+ if ( exists ) {
2202+ const addedRepos = await this . _addRepositories (
2203+ [ preferredPath ] ,
2204+ repository . login
2205+ )
2206+ if ( addedRepos . length > 0 ) {
2207+ repository = addedRepos [ 0 ]
2208+ this . selectedRepository = repository
2209+ }
2210+ } else {
2211+ clearPreferredWorktreePath ( repoPath )
2212+ }
2213+ }
2214+ }
2215+ }
2216+
2217+ this . emitUpdate ( )
2218+
21772219 if ( persistSelection ) {
21782220 setNumber ( LastSelectedRepositoryIDKey , repository . id )
21792221 }
@@ -7269,6 +7311,23 @@ export class AppStore extends TypedBaseStore<IAppState> {
72697311 return
72707312 }
72717313
7314+ if ( repository instanceof Repository ) {
7315+ if ( repository . isLinkedWorktree ) {
7316+ const repoPath = normalizePath ( repository . path )
7317+ const mainRepo = this . repositories . find (
7318+ r =>
7319+ r instanceof Repository &&
7320+ ! r . isLinkedWorktree &&
7321+ getPreferredWorktreePath ( normalizePath ( r . path ) ) === repoPath
7322+ )
7323+ if ( mainRepo instanceof Repository ) {
7324+ clearPreferredWorktreePath ( normalizePath ( mainRepo . path ) )
7325+ }
7326+ } else {
7327+ clearPreferredWorktreePath ( normalizePath ( repository . path ) )
7328+ }
7329+ }
7330+
72727331 const allRepositories = await this . repositoriesStore . getAll ( )
72737332 if ( allRepositories . length === 0 ) {
72747333 this . _closeFoldout ( FoldoutType . Repository )
0 commit comments