11#![ allow( clippy:: result_large_err) ]
2+ use gix_config:: file:: Metadata ;
3+ use gix_features:: threading:: OwnShared ;
4+ use gix_object:: bstr:: ByteSlice ;
5+ use gix_path:: RelativePath ;
6+ use std:: path:: Path ;
27use std:: {
38 borrow:: Cow ,
49 collections:: { btree_map:: Entry , BTreeMap } ,
510 ffi:: OsStr ,
611 path:: PathBuf ,
712} ;
813
9- use gix_features:: threading:: OwnShared ;
10- use gix_object:: bstr:: ByteSlice ;
11- use gix_path:: RelativePath ;
12-
1314use super :: { Error , Options } ;
1415use crate :: {
1516 bstr:: BString ,
@@ -257,26 +258,32 @@ impl ThreadSafeRepository {
257258 // core.worktree might be used to overwrite the worktree directory
258259 if !config. is_bare {
259260 let mut key_source = None ;
261+ fn assure_config_is_from_current_repo (
262+ section : & gix_config:: file:: Metadata ,
263+ git_dir : & Path ,
264+ current_dir : & Path ,
265+ filter_config_section : & mut fn ( & Metadata ) -> bool ,
266+ ) -> bool {
267+ if !filter_config_section ( section) {
268+ return false ;
269+ }
270+ // ignore worktree settings that aren't from our repository. This can happen
271+ // with worktrees of submodules for instance.
272+ section
273+ . path
274+ . as_deref ( )
275+ . and_then ( |p| gix_path:: normalize ( p. into ( ) , current_dir) )
276+ . is_some_and ( |config_path| config_path. starts_with ( git_dir) )
277+ }
260278 let worktree_path = config
261279 . resolved
262- . path_filter ( Core :: WORKTREE , {
263- |section| {
264- if !filter_config_section ( section) {
265- return false ;
266- }
267- // ignore worktree settings that aren't from our repository. This can happen
268- // with worktrees of submodules for instance.
269- let is_config_in_our_repo = section
270- . path
271- . as_deref ( )
272- . and_then ( |p| gix_path:: normalize ( p. into ( ) , current_dir) )
273- . is_some_and ( |config_path| config_path. starts_with ( & git_dir) ) ;
274- if !is_config_in_our_repo {
275- return false ;
276- }
280+ . path_filter ( Core :: WORKTREE , |section| {
281+ let res =
282+ assure_config_is_from_current_repo ( section, & git_dir, current_dir, & mut filter_config_section) ;
283+ if res {
277284 key_source = Some ( section. source ) ;
278- true
279285 }
286+ res
280287 } )
281288 . zip ( key_source) ;
282289 if let Some ( ( wt, key_source) ) = worktree_path {
@@ -302,7 +309,9 @@ impl ThreadSafeRepository {
302309 } else if !config. lenient_config
303310 && config
304311 . resolved
305- . boolean_filter ( Core :: WORKTREE , & mut filter_config_section)
312+ . boolean_filter ( Core :: WORKTREE , |section| {
313+ assure_config_is_from_current_repo ( section, & git_dir, current_dir, & mut filter_config_section)
314+ } )
306315 . is_some ( )
307316 {
308317 return Err ( Error :: from ( config:: Error :: ConfigTypedString (
0 commit comments