Skip to content

Commit 5c327bb

Browse files
authored
Merge pull request #1834 from GitoxideLabs/improvements
improvements
2 parents c042813 + 27e62d7 commit 5c327bb

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

gix/src/filter.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::borrow::Cow;
44
pub use gix_filter as plumbing;
55
use gix_object::Find;
66

7+
use crate::prelude::ObjectIdExt;
78
use crate::{
89
bstr::BStr,
910
config::{
@@ -207,6 +208,9 @@ impl Pipeline<'_> {
207208
}
208209

209210
/// Add the worktree file at `rela_path` to the object database and return its `(id, entry, symlink_metadata)` for use in a tree or in the index, for instance.
211+
/// If `rela_path` is a directory *and* is a repository with its `HEAD` pointing to a commit, it will also be provided with the appropriate kind.
212+
/// Note that this can easily lead to embedded repositories as no submodule-crosscheck is performed. Otherwise, unreadable repositories or directories
213+
/// are ignored with `None` as return value.
210214
///
211215
/// `index` is used in particularly rare cases where the CRLF filter in auto-mode tries to determine whether to apply itself,
212216
/// and it should match the state used when [instantiating this instance](Self::new()).
@@ -257,6 +261,14 @@ impl Pipeline<'_> {
257261
gix_object::tree::EntryKind::Blob
258262
};
259263
(id, kind)
264+
} else if md.is_dir() {
265+
let Some(submodule_repo) = crate::open_opts(&path, repo.open_options().clone()).ok() else {
266+
return Ok(None);
267+
};
268+
let Some(id) = submodule_repo.head_id().ok() else {
269+
return Ok(None);
270+
};
271+
(id.detach().attach(repo), gix_object::tree::EntryKind::Commit)
260272
} else {
261273
// This is probably a type-change to something we can't track.
262274
return Ok(None);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
#!/usr/bin/env bash
22
set -eu -o pipefail
33

4+
git init embedded-repository
5+
(cd embedded-repository
6+
echo content >file && git add file && git commit -m "init"
7+
)
8+
49
git init -q
510
echo content >file
611
ln -s file link
712

813
echo binary >exe && chmod +x exe
914
mkfifo fifo
15+
16+
git submodule add ./embedded-repository submodule
17+
18+
mkdir empty-dir
19+
git init uninitialized-embedded-repository

gix/tests/gix/repository/filter.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,32 @@ fn pipeline_in_repo_without_special_options() -> crate::Result {
3535
#[cfg(unix)]
3636
fn pipeline_worktree_file_to_object() -> crate::Result {
3737
let repo = named_repo("repo_with_untracked_files.sh")?;
38+
let work_dir = repo.work_dir().expect("non-bare");
3839
let (mut pipe, index) = repo.filter_pipeline(None)?;
3940
fn take_two<A, B, C>(t: Option<(A, B, C)>) -> Option<(A, B)> {
4041
t.map(|t| (t.0, t.1))
4142
}
4243

44+
let submodule_id = hex_to_id("a047f8183ba2bb7eb00ef89e60050c5fde740483");
45+
assert_eq!(
46+
take_two(pipe.worktree_file_to_object("embedded-repository".into(), &index)?),
47+
Some((submodule_id, gix::object::tree::EntryKind::Commit))
48+
);
49+
assert_eq!(
50+
take_two(pipe.worktree_file_to_object("submodule".into(), &index)?),
51+
Some((submodule_id, gix::object::tree::EntryKind::Commit))
52+
);
53+
assert_eq!(
54+
take_two(pipe.worktree_file_to_object("uninitialized-embedded-repository".into(), &index)?),
55+
None,
56+
"repositories that don't have HEAD pointing to an ID yet are ignored"
57+
);
58+
assert!(work_dir.join("empty-dir").is_dir());
59+
assert_eq!(
60+
take_two(pipe.worktree_file_to_object("empty-dir".into(), &index)?),
61+
None,
62+
"directories that aren't even repos are also ignored"
63+
);
4364
assert_eq!(
4465
take_two(pipe.worktree_file_to_object("file".into(), &index)?),
4566
Some((
@@ -66,10 +87,7 @@ fn pipeline_worktree_file_to_object() -> crate::Result {
6687
None,
6788
"Missing files are specifically typed and no error"
6889
);
69-
assert!(
70-
repo.work_dir().expect("non-bare").join("fifo").exists(),
71-
"there is a fifo"
72-
);
90+
assert!(work_dir.join("fifo").exists(), "there is a fifo");
7391
assert_eq!(
7492
take_two(pipe.worktree_file_to_object("fifo".into(), &index)?),
7593
None,

0 commit comments

Comments
 (0)