Skip to content

Commit

Permalink
allow to re-read the logallrefupdates config after overrides (#450)
Browse files Browse the repository at this point in the history
It's still not enough though as it won't be able to write ref-logs
when receiving packs, so that kind of override isn't possible
and I wonder if it's worth the effort.

Also, it's not yet possible to enforce reflogs when writing received
refs, only the HEAD, and even that isn't really working yet.
  • Loading branch information
Byron committed Oct 19, 2022
1 parent 372e9d4 commit ff06de4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
3 changes: 1 addition & 2 deletions git-repository/src/clone/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl PrepareFetch {
repo_config.append(config);
crate::config::overrides::apply(repo_config, &repo.options.config_overrides, git_config::Source::Api)
.expect("applied once and can be applied again");
repo.config
.reread_values_and_clear_caches()
repo.reread_values_and_clear_caches()
.expect("values could be read once and can be read again");
}

Expand Down
15 changes: 10 additions & 5 deletions git-repository/src/config/cache/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,30 @@ impl Cache {
/// Note that we unconditionally re-read all values.
pub fn reread_values_and_clear_caches_replacing_config(&mut self, config: crate::Config) -> Result<(), Error> {
let prev = std::mem::replace(&mut self.resolved, config);
if let Err(err) = self.reread_values_and_clear_caches() {
drop(std::mem::replace(&mut self.resolved, prev));
Err(err)
} else {
Ok(())
match self.reread_values_and_clear_caches() {
Err(err) => {
drop(std::mem::replace(&mut self.resolved, prev));
Err(err)
}
Ok(()) => Ok(()),
}
}

/// Similar to `reread_values_and_clear_caches_replacing_config()`, but works on the existing configuration instead of a passed
/// in one that it them makes the default.
pub fn reread_values_and_clear_caches(&mut self) -> Result<(), Error> {
let config = &self.resolved;
let hex_len = util::check_lenient(util::parse_core_abbrev(&config, self.object_hash), self.lenient_config)?;

use util::config_bool;
let ignore_case = config_bool(&config, "core.ignoreCase", false, self.lenient_config)?;
let object_kind_hint = util::disambiguate_hint(&config);
let reflog = util::query_refupdates(config);

self.hex_len = hex_len;
self.ignore_case = ignore_case;
self.object_kind_hint = object_kind_hint;
self.reflog = reflog;

self.personas = Default::default();
self.url_rewrite = Default::default();
Expand Down
38 changes: 29 additions & 9 deletions git-repository/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use git_features::threading::OwnShared;

use crate::bstr::BString;
use crate::{config, config::cache::interpolate_context, permission, Permissions, ThreadSafeRepository};
use crate::{config, config::cache::interpolate_context, permission, Permissions, Repository, ThreadSafeRepository};

/// A way to configure the usage of replacement objects, see `git replace`.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -416,14 +416,7 @@ impl ThreadSafeRepository {
None => {}
}

refs.write_reflog = config.reflog.unwrap_or_else(|| {
if worktree_dir.is_none() {
git_ref::store::WriteReflog::Disable
} else {
git_ref::store::WriteReflog::Normal
}
});

refs.write_reflog = reflog_or_default(config.reflog, worktree_dir.is_some());
let replacements = replacement_objects
.clone()
.refs_prefix()
Expand Down Expand Up @@ -465,6 +458,33 @@ impl ThreadSafeRepository {
}
}

impl Repository {
/// Causes our configuration to re-read cached values which will also be applied to the repository in-memory state if applicable.
///
/// Similar to `reread_values_and_clear_caches_replacing_config()`, but works on the existing instance instead of a passed
/// in one that it them makes the default.
pub fn reread_values_and_clear_caches(&mut self) -> Result<(), config::Error> {
self.config.reread_values_and_clear_caches()?;
self.apply_changed_values();
Ok(())
}

fn apply_changed_values(&mut self) {
self.refs.write_reflog = reflog_or_default(self.config.reflog, self.work_dir().is_some());
}
}

fn reflog_or_default(
config_reflog: Option<git_ref::store::WriteReflog>,
has_worktree: bool,
) -> git_ref::store::WriteReflog {
config_reflog.unwrap_or_else(|| {
has_worktree
.then(|| git_ref::store::WriteReflog::Normal)
.unwrap_or(git_ref::store::WriteReflog::Disable)
})
}

fn check_safe_directories(
git_dir: &std::path::Path,
git_install_dir: Option<&std::path::Path>,
Expand Down
2 changes: 0 additions & 2 deletions git-repository/tests/clone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod blocking_io {
use crate::remote;

#[test]
#[ignore]
fn fetch_only_with_configuration() -> crate::Result {
let tmp = git_testtools::tempfile::TempDir::new()?;
let called_configure_remote = std::sync::Arc::new(std::sync::atomic::AtomicBool::default());
Expand Down Expand Up @@ -37,7 +36,6 @@ mod blocking_io {
});
let (repo, out) = prepare.fetch_only(git::progress::Discard, &std::sync::atomic::AtomicBool::default())?;
drop(prepare);
dbg!(repo.config_snapshot());

assert!(
called_configure_remote.load(std::sync::atomic::Ordering::Relaxed),
Expand Down

0 comments on commit ff06de4

Please sign in to comment.