@@ -10,7 +10,7 @@ use jj_lib::{
1010 workspace:: { WorkingCopyFactories , Workspace } ,
1111} ;
1212use std:: {
13- collections:: { HashMap , VecDeque } ,
13+ collections:: { HashMap , HashSet , VecDeque } ,
1414 fs:: { self } ,
1515 path:: { Path , PathBuf } ,
1616 process:: { self , Stdio } ,
@@ -240,22 +240,20 @@ impl RepoProvider {
240240 . collect ( ) ) ,
241241
242242 RepoProvider :: Jujutsu ( workspace) => {
243+ let repo = workspace
244+ . repo_loader ( )
245+ . load_at_head ( )
246+ . change_context ( TmsError :: GitError ) ?;
247+ let mut workspaces: HashSet < & str > = HashSet :: from_iter (
248+ repo. view ( )
249+ . wc_commit_ids ( )
250+ . keys ( )
251+ . filter ( |name| name. as_str ( ) != workspace. workspace_name ( ) . as_str ( ) )
252+ . map ( |buf| buf. as_str ( ) ) ,
253+ ) ;
243254 let mut repos: Vec < RepoProvider > = Vec :: new ( ) ;
244255
245- search_dirs ( config, |_, repo| {
246- if !repo. is_worktree ( ) {
247- return Ok ( ( ) ) ;
248- }
249- let Some ( path) = repo. main_repo ( ) else {
250- return Ok ( ( ) ) ;
251- } ;
252- if workspace. repo_path ( ) == path {
253- repos. push ( repo) ;
254- }
255- Ok ( ( ) )
256- } ) ?;
257-
258- if self . is_bare ( ) {
256+ if self . is_bare ( ) && !workspaces. is_empty ( ) {
259257 if let Ok ( read_dir) = fs:: read_dir ( self . path ( ) ) {
260258 let mut sub = read_dir
261259 . filter_map ( |entry| entry. ok ( ) )
@@ -268,10 +266,30 @@ impl RepoProvider {
268266 . is_some_and ( |main| main == self . path ( ) . join ( ".jj/repo" ) )
269267 } )
270268 . collect :: < Vec < _ > > ( ) ;
269+ for repo in & sub {
270+ if let RepoProvider :: Jujutsu ( ws) = repo {
271+ workspaces. remove ( ws. workspace_name ( ) . as_str ( ) ) ;
272+ }
273+ }
271274 repos. append ( & mut sub) ;
272275 }
273276 }
274277
278+ if !workspaces. is_empty ( ) {
279+ search_dirs ( config, |_, repo| {
280+ if !repo. is_worktree ( ) {
281+ return Ok ( ( ) ) ;
282+ }
283+ let Some ( path) = repo. main_repo ( ) else {
284+ return Ok ( ( ) ) ;
285+ } ;
286+ if workspace. repo_path ( ) == path {
287+ repos. push ( repo) ;
288+ }
289+ Ok ( ( ) )
290+ } ) ?;
291+ }
292+
275293 let repos = repos
276294 . into_iter ( )
277295 . filter_map ( |repo| match repo {
@@ -302,6 +320,26 @@ pub fn find_repos(config: &Config) -> Result<HashMap<String, Vec<Session>>> {
302320 } ) ?
303321 . to_string ( ) ?;
304322
323+ if let Some ( true ) = config. list_worktrees {
324+ for worktree in repo. worktrees ( config) ?. iter ( ) {
325+ let Ok ( sub) = worktree
326+ . path ( )
327+ . and_then ( |path| RepoProvider :: open ( & path, config) )
328+ else {
329+ continue ;
330+ } ;
331+ let session = Session :: new (
332+ format ! ( "{}#{}" , session_name, worktree. name( ) ) ,
333+ SessionType :: Git ( sub) ,
334+ ) ;
335+ if let Some ( list) = repos. get_mut ( & session. name ) {
336+ list. push ( session) ;
337+ } else {
338+ repos. insert ( session. name . clone ( ) , vec ! [ session] ) ;
339+ }
340+ }
341+ }
342+
305343 let session = Session :: new ( session_name, SessionType :: Git ( repo) ) ;
306344 if let Some ( list) = repos. get_mut ( & session. name ) {
307345 list. push ( session) ;
0 commit comments