Skip to content

Commit 189d1a0

Browse files
authored
Merge pull request #1987 from GitoxideLabs/fix-1985
fix 1985
2 parents f965540 + 3f85bf5 commit 189d1a0

File tree

2 files changed

+64
-31
lines changed

2 files changed

+64
-31
lines changed

gix/src/open/repository.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
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;
27
use 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-
1314
use super::{Error, Options};
1415
use 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(

gix/tests/gix-init.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
#![allow(clippy::result_large_err)]
2+
3+
use gix::open::Permissions;
4+
use gix::{Repository, ThreadSafeRepository};
5+
use gix_sec::Permission;
6+
use serial_test::serial;
7+
8+
pub fn named_subrepo_opts(
9+
fixture: &str,
10+
name: &str,
11+
opts: gix::open::Options,
12+
) -> std::result::Result<Repository, gix::open::Error> {
13+
let repo_path = gix_testtools::scripted_fixture_read_only(fixture).unwrap().join(name);
14+
Ok(ThreadSafeRepository::open_opts(repo_path, opts)?.to_thread_local())
15+
}
16+
217
mod with_overrides {
318
use std::borrow::Cow;
419

5-
use gix::{Repository, ThreadSafeRepository};
20+
use crate::named_subrepo_opts;
621
use gix_object::bstr::BStr;
722
use gix_sec::Permission;
823
use gix_testtools::Env;
924
use serial_test::serial;
1025

11-
pub fn named_subrepo_opts(
12-
fixture: &str,
13-
name: &str,
14-
opts: gix::open::Options,
15-
) -> std::result::Result<Repository, gix::open::Error> {
16-
let repo_path = gix_testtools::scripted_fixture_read_only(fixture).unwrap().join(name);
17-
Ok(ThreadSafeRepository::open_opts(repo_path, opts)?.to_thread_local())
18-
}
19-
2026
#[test]
2127
#[serial]
2228
fn order_from_api_and_cli_and_environment() -> gix_testtools::Result {
@@ -264,3 +270,21 @@ mod with_overrides {
264270
Cow::Borrowed(s.into())
265271
}
266272
}
273+
274+
#[test]
275+
#[serial]
276+
fn git_worktree_and_strict_config() -> gix_testtools::Result {
277+
let _restore_env_on_drop = gix_testtools::Env::new().set("GIT_WORK_TREE", ".");
278+
let _repo = named_subrepo_opts(
279+
"make_empty_repo.sh",
280+
"",
281+
gix::open::Options::isolated()
282+
.permissions({
283+
let mut perm = Permissions::isolated();
284+
perm.env.git_prefix = Permission::Allow;
285+
perm
286+
})
287+
.strict_config(true),
288+
)?;
289+
Ok(())
290+
}

0 commit comments

Comments
 (0)