11use anyhow:: { bail, Context , Result } ;
22use arc_swap:: ArcSwap ;
33use gix:: filter:: plumbing:: driver:: apply:: Delay ;
4+ use helix_loader:: find_workspace_in;
5+ use helix_loader:: workspace_trust:: { quick_query_workspace, TrustStatus } ;
46use std:: io:: Read ;
57use std:: path:: Path ;
68use std:: sync:: Arc ;
@@ -27,15 +29,15 @@ fn get_repo_dir(file: &Path) -> Result<&Path> {
2729 file. parent ( ) . context ( "file has no parent directory" )
2830}
2931
30- pub fn get_diff_base ( file : & Path ) -> Result < Vec < u8 > > {
32+ pub fn get_diff_base ( file : & Path , insecure : bool ) -> Result < Vec < u8 > > {
3133 debug_assert ! ( !file. exists( ) || file. is_file( ) ) ;
3234 debug_assert ! ( file. is_absolute( ) ) ;
3335 let file = gix:: path:: realpath ( file) . context ( "resolve symlinks" ) ?;
3436
3537 // TODO cache repository lookup
3638
3739 let repo_dir = get_repo_dir ( & file) ?;
38- let repo = open_repo ( repo_dir)
40+ let repo = open_repo ( repo_dir, insecure )
3941 . context ( "failed to open git repo" ) ?
4042 . to_thread_local ( ) ;
4143 let head = repo. head_commit ( ) ?;
@@ -59,13 +61,13 @@ pub fn get_diff_base(file: &Path) -> Result<Vec<u8>> {
5961 }
6062}
6163
62- pub fn get_current_head_name ( file : & Path ) -> Result < Arc < ArcSwap < Box < str > > > > {
64+ pub fn get_current_head_name ( file : & Path , insecure : bool ) -> Result < Arc < ArcSwap < Box < str > > > > {
6365 debug_assert ! ( !file. exists( ) || file. is_file( ) ) ;
6466 debug_assert ! ( file. is_absolute( ) ) ;
6567 let file = gix:: path:: realpath ( file) . context ( "resolve symlinks" ) ?;
6668
6769 let repo_dir = get_repo_dir ( & file) ?;
68- let repo = open_repo ( repo_dir)
70+ let repo = open_repo ( repo_dir, insecure )
6971 . context ( "failed to open git repo" ) ?
7072 . to_thread_local ( ) ;
7173 let head_ref = repo. head_ref ( ) ?;
@@ -79,13 +81,17 @@ pub fn get_current_head_name(file: &Path) -> Result<Arc<ArcSwap<Box<str>>>> {
7981 Ok ( Arc :: new ( ArcSwap :: from_pointee ( name. into_boxed_str ( ) ) ) )
8082}
8183
82- pub fn for_each_changed_file ( cwd : & Path , f : impl Fn ( Result < FileChange > ) -> bool ) -> Result < ( ) > {
83- status ( & open_repo ( cwd) ?. to_thread_local ( ) , f)
84+ pub fn for_each_changed_file (
85+ cwd : & Path ,
86+ insecure : bool ,
87+ f : impl Fn ( Result < FileChange > ) -> bool ,
88+ ) -> Result < ( ) > {
89+ status ( & open_repo ( cwd, insecure) ?. to_thread_local ( ) , f)
8490}
8591
86- fn open_repo ( path : & Path ) -> Result < ThreadSafeRepository > {
92+ fn open_repo ( path : & Path , insecure : bool ) -> Result < ThreadSafeRepository > {
8793 // custom open options
88- let mut git_open_opts_map = gix:: sec:: trust:: Mapping :: < gix:: open:: Options > :: default ( ) ;
94+ let git_open_opts_map = gix:: sec:: trust:: Mapping :: < gix:: open:: Options > :: default ( ) ;
8995
9096 // On windows various configuration options are bundled as part of the installations
9197 // This path depends on the install location of git and therefore requires some overhead to lookup
@@ -99,30 +105,24 @@ fn open_repo(path: &Path) -> Result<ThreadSafeRepository> {
99105 includes : true ,
100106 git_binary : cfg ! ( windows) ,
101107 } ;
102- // change options for config permissions without touching anything else
103- git_open_opts_map. reduced = git_open_opts_map
104- . reduced
105- . permissions ( gix:: open:: Permissions {
108+
109+ let opts = if let TrustStatus :: Trusted = quick_query_workspace ( insecure) {
110+ git_open_opts_map. full . permissions ( gix:: open:: Permissions {
106111 config,
107- ..gix:: open:: Permissions :: default_for_level ( gix:: sec:: Trust :: Reduced )
108- } ) ;
109- git_open_opts_map. full = git_open_opts_map. full . permissions ( gix:: open:: Permissions {
110- config,
111- ..gix:: open:: Permissions :: default_for_level ( gix:: sec:: Trust :: Full )
112- } ) ;
113-
114- let open_options = gix:: discover:: upwards:: Options {
115- dot_git_only : true ,
116- ..Default :: default ( )
112+ ..gix:: open:: Permissions :: default_for_level ( gix:: sec:: Trust :: Full )
113+ } )
114+ } else {
115+ git_open_opts_map
116+ . reduced
117+ . permissions ( gix:: open:: Permissions {
118+ config,
119+ ..gix:: open:: Permissions :: default_for_level ( gix:: sec:: Trust :: Reduced )
120+ } )
117121 } ;
118122
119- let res = ThreadSafeRepository :: discover_with_environment_overrides_opts (
120- path,
121- open_options,
122- git_open_opts_map,
123- ) ?;
123+ let ( workspace, _) = find_workspace_in ( path) ;
124124
125- Ok ( res )
125+ Ok ( ThreadSafeRepository :: open_opts ( workspace , opts ) ? )
126126}
127127
128128/// Emulates the result of running `git status` from the command line.
0 commit comments