summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/repository/shallow.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/gix/src/repository/shallow.rs
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix/src/repository/shallow.rs')
-rw-r--r--vendor/gix/src/repository/shallow.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/gix/src/repository/shallow.rs b/vendor/gix/src/repository/shallow.rs
new file mode 100644
index 000000000..7fac83a55
--- /dev/null
+++ b/vendor/gix/src/repository/shallow.rs
@@ -0,0 +1,65 @@
+use std::{borrow::Cow, path::PathBuf};
+
+use crate::{
+ bstr::ByteSlice,
+ config::tree::{gitoxide, Key},
+ Repository,
+};
+
+impl Repository {
+ /// Return `true` if the repository is a shallow clone, i.e. contains history only up to a certain depth.
+ pub fn is_shallow(&self) -> bool {
+ self.shallow_file()
+ .metadata()
+ .map_or(false, |m| m.is_file() && m.len() > 0)
+ }
+
+ /// Return a shared list of shallow commits which is updated automatically if the in-memory snapshot has become stale
+ /// as the underlying file on disk has changed.
+ ///
+ /// The list of shallow commits represents the shallow boundary, beyond which we are lacking all (parent) commits.
+ /// Note that the list is never empty, as `Ok(None)` is returned in that case indicating the repository
+ /// isn't a shallow clone.
+ ///
+ /// The shared list is shared across all clones of this repository.
+ pub fn shallow_commits(&self) -> Result<Option<crate::shallow::Commits>, crate::shallow::open::Error> {
+ self.shallow_commits.recent_snapshot(
+ || self.shallow_file().metadata().ok().and_then(|m| m.modified().ok()),
+ || {
+ let buf = match std::fs::read(self.shallow_file()) {
+ Ok(buf) => buf,
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(err) => return Err(err.into()),
+ };
+
+ let mut commits = buf
+ .lines()
+ .map(gix_hash::ObjectId::from_hex)
+ .collect::<Result<Vec<_>, _>>()?;
+
+ commits.sort();
+ if commits.is_empty() {
+ Ok(None)
+ } else {
+ Ok(Some(commits))
+ }
+ },
+ )
+ }
+
+ /// Return the path to the `shallow` file which contains hashes, one per line, that describe commits that don't have their
+ /// parents within this repository.
+ ///
+ /// Note that it may not exist if the repository isn't actually shallow.
+ pub fn shallow_file(&self) -> PathBuf {
+ let shallow_name = self
+ .config
+ .resolved
+ .string_filter_by_key(
+ gitoxide::Core::SHALLOW_FILE.logical_name().as_str(),
+ &mut self.filter_config_section(),
+ )
+ .unwrap_or_else(|| Cow::Borrowed("shallow".into()));
+ self.common_dir().join(gix_path::from_bstr(shallow_name))
+ }
+}