Skip to content

Commit 26ae766

Browse files
committed
fix!: allow querying Repository::submodules() in an unborn repository.
It's a breaking change merely because the error type changed.
1 parent 376ed0c commit 26ae766

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

gix/src/repository/submodule.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Repository {
2626
)?))
2727
}
2828

29-
/// Return a shared [`.gitmodules` file](crate::submodule::File) which is updated automatically if the in-memory snapshot
29+
/// Return a shared [`.gitmodules` file](submodule::File) which is updated automatically if the in-memory snapshot
3030
/// has become stale as the underlying file on disk has changed. The snapshot based on the file on disk is shared across all
3131
/// clones of this repository.
3232
///
@@ -54,12 +54,20 @@ impl Repository {
5454
}) {
5555
Some(id) => id,
5656
None => match self
57-
.head_commit()?
58-
.tree()?
59-
.find_entry(submodule::MODULES_FILE)
60-
.map(|entry| entry.inner.oid)
57+
.head()?
58+
.try_peel_to_id_in_place()?
59+
.map(|id| -> Result<Option<_>, submodule::modules::Error> {
60+
Ok(id
61+
.object()?
62+
.peel_to_commit()?
63+
.tree()?
64+
.find_entry(submodule::MODULES_FILE)
65+
.map(|entry| entry.inner.oid.to_owned()))
66+
})
67+
.transpose()?
68+
.flatten()
6169
{
62-
Some(id) => id.to_owned(),
70+
Some(id) => id,
6371
None => return Ok(None),
6472
},
6573
};

gix/src/status/index_worktree.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,6 @@ mod submodule_status {
226226
v
227227
}
228228
Ok(None) => Vec::new(),
229-
Err(crate::submodule::modules::Error::FindHeadCommit(
230-
crate::reference::head_commit::Error::PeelToCommit(
231-
crate::head::peel::to_commit::Error::PeelToObject(
232-
crate::head::peel::to_object::Error::Unborn { .. },
233-
),
234-
),
235-
)) => Vec::new(),
236229
Err(err) => return Err(err),
237230
};
238231
Ok(Self {

gix/src/submodule/errors.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ pub mod modules {
2323
OpenIndex(#[from] crate::worktree::open_index::Error),
2424
#[error("Could not find the .gitmodules file by id in the object database")]
2525
FindExistingBlob(#[from] crate::object::find::existing::Error),
26-
#[error("Did not find commit in current HEAD to access its tree")]
27-
FindHeadCommit(#[from] crate::reference::head_commit::Error),
26+
#[error(transparent)]
27+
FindHeadRef(#[from] crate::reference::find::existing::Error),
28+
#[error(transparent)]
29+
PeelHeadRef(#[from] crate::head::peel::Error),
30+
#[error(transparent)]
31+
PeelObjectToCommit(#[from] crate::object::peel::to_kind::Error),
2832
#[error(transparent)]
2933
TreeFromCommit(#[from] crate::object::commit::Error),
3034
}
Binary file not shown.

gix/tests/fixtures/make_submodules.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,5 @@ git clone with-submodules not-a-submodule
144144
mv modules.bak .gitmodules
145145
git add m1 && git commit -m "no submodule in index and commit, but in configuration"
146146
)
147+
148+
git init unborn

gix/tests/gix/submodule.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,17 @@ mod open {
340340
Ok(())
341341
}
342342

343+
#[test]
344+
fn in_unborn() -> crate::Result {
345+
let repo = repo("unborn")?;
346+
assert_eq!(
347+
repo.submodules()?.into_iter().flatten().count(),
348+
0,
349+
"there is nothing, and that is fine"
350+
);
351+
Ok(())
352+
}
353+
343354
#[test]
344355
#[cfg(feature = "revision")]
345356
fn submodule_worktrees() -> crate::Result {

0 commit comments

Comments
 (0)