Skip to content

Commit

Permalink
feat: add clone::PrepareFetch::with_in_memory_config_overrides().
Browse files Browse the repository at this point in the history
With it one can affect the repository configuration right before fetching.
  • Loading branch information
Byron committed Dec 8, 2023
1 parent 9833b45 commit b5c36b8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
13 changes: 7 additions & 6 deletions gix/src/clone/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl PrepareFetch {
/// _all changes done in `f()` will be persisted_.
///
/// It can also be used to configure additional options, like those for fetching tags. Note that
/// [`with_fetch_tags()`][crate::Remote::with_fetch_tags()] should be called here to configure the clone as desired.
/// [`with_fetch_tags()`](crate::Remote::with_fetch_tags()) should be called here to configure the clone as desired.
/// Otherwise a clone is configured to be complete and fetches all tags, not only those reachable from all branches.
pub fn configure_remote(
mut self,
Expand All @@ -21,7 +21,7 @@ impl PrepareFetch {
}

/// Set the remote's name to the given value after it was configured using the function provided via
/// [`configure_remote()`][Self::configure_remote()].
/// [`configure_remote()`](Self::configure_remote()).
///
/// If not set here, it defaults to `origin` or the value of `clone.defaultRemoteName`.
pub fn with_remote_name(mut self, name: impl Into<BString>) -> Result<Self, crate::remote::name::Error> {
Expand All @@ -35,10 +35,11 @@ impl PrepareFetch {
self
}

/// Apply the given configuration `values` early to allow affecting the repository instantiation phase.
/// The configuration is marked with [source API][gix_config::Source::Api].
pub fn config_overrides(mut self, values: impl IntoIterator<Item = impl Into<BString>>) -> Self {
self.api_config_overrides = values.into_iter().map(Into::into).collect();
/// Apply the given configuration `values` right before readying the actual fetch from the remote.
/// The configuration is marked with [source API](gix_config::Source::Api), and will not be written back, it's
/// retained only in memory.
pub fn with_in_memory_config_overrides(mut self, values: impl IntoIterator<Item = impl Into<BString>>) -> Self {
self.config_overrides = values.into_iter().map(Into::into).collect();
self
}
}
Expand Down
8 changes: 5 additions & 3 deletions gix/src/clone/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ impl PrepareFetch {
.as_mut()
.expect("user error: multiple calls are allowed only until it succeeds");

let mut snapshot = repo.config_snapshot_mut();
snapshot.append_config(self.api_config_overrides.as_slice(), gix_config::Source::Api)?;
snapshot.commit()?;
if !self.config_overrides.is_empty() {
let mut snapshot = repo.config_snapshot_mut();
snapshot.append_config(&self.config_overrides, gix_config::Source::Api)?;
snapshot.commit()?;
}

let remote_name = match self.remote_name.as_ref() {
Some(name) => name.to_owned(),
Expand Down
8 changes: 4 additions & 4 deletions gix/src/clone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct PrepareFetch {
/// The name of the remote, which defaults to `origin` if not overridden.
remote_name: Option<BString>,
/// Additional config `values` that are applied in-memory before starting the fetch process.
api_config_overrides: Vec<BString>,
config_overrides: Vec<BString>,
/// A function to configure a remote prior to fetching a pack.
configure_remote: Option<ConfigureRemoteFn>,
/// A function to configure a connection before using it.
Expand Down Expand Up @@ -128,7 +128,7 @@ impl PrepareFetch {
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
fetch_options: Default::default(),
repo: Some(repo),
api_config_overrides: Vec::new(),
config_overrides: Vec::new(),
remote_name: None,
configure_remote: None,
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
Expand All @@ -147,8 +147,6 @@ pub struct PrepareCheckout {
pub(self) repo: Option<crate::Repository>,
}

mod access;

// This module encapsulates functionality that works with both feature toggles. Can be combined with `fetch`
// once async and clone are a thing.
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]
Expand Down Expand Up @@ -184,6 +182,8 @@ mod access_feat {
#[cfg(any(feature = "async-network-client-async-std", feature = "blocking-network-client"))]
pub mod fetch;

mod access;

///
#[cfg(feature = "worktree-mutation")]
pub mod checkout;
13 changes: 13 additions & 0 deletions gix/tests/clone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod blocking_io {
fn from_shallow_allowed_by_default() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let (repo, _change) = gix::prepare_clone_bare(remote::repo("base.shallow").path(), tmp.path())?
.with_in_memory_config_overrides(Some("my.marker=1"))
.fetch_only(gix::progress::Discard, &std::sync::atomic::AtomicBool::default())?;
assert_eq!(
repo.shallow_commits()?.expect("present").as_slice(),
Expand All @@ -118,6 +119,18 @@ mod blocking_io {
hex_to_id("dfd0954dabef3b64f458321ef15571cc1a46d552"),
]
);
assert_eq!(
repo.config_snapshot().boolean("my.marker"),
Some(true),
"configuration overrides are set in time"
);
assert_eq!(
gix::open_opts(repo.git_dir(), gix::open::Options::isolated())?
.config_snapshot()
.boolean("my.marker"),
None,
"these options are not persisted"
);
Ok(())
}

Expand Down

0 comments on commit b5c36b8

Please sign in to comment.