Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions gix-config/src/file/access/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,16 @@ impl<'event> File<'event> {
/// ```
pub fn set_raw_value<'b>(
&mut self,
key: &'event impl AsKey,
key: impl AsKey,
new_value: impl Into<&'b BStr>,
) -> Result<Option<Cow<'event, BStr>>, crate::file::set_raw_value::Error> {
let key = key.as_key();
self.set_raw_value_by(key.section_name, key.subsection_name, key.value_name, new_value)
self.set_raw_value_by(
key.section_name,
key.subsection_name,
key.value_name.to_owned(),
new_value,
)
}

/// Sets a value in a given `section_name`, optional `subsection_name`, and `value_name`.
Expand Down Expand Up @@ -699,12 +704,18 @@ impl<'event> File<'event> {
/// `filter`, creating a new section otherwise.
pub fn set_raw_value_filter<'b>(
&mut self,
key: &'event impl AsKey,
key: impl AsKey,
new_value: impl Into<&'b BStr>,
filter: impl FnMut(&Metadata) -> bool,
) -> Result<Option<Cow<'event, BStr>>, crate::file::set_raw_value::Error> {
let key = key.as_key();
self.set_raw_value_filter_by(key.section_name, key.subsection_name, key.value_name, new_value, filter)
self.set_raw_value_filter_by(
key.section_name,
key.subsection_name,
key.value_name.to_owned(),
new_value,
filter,
)
}

/// Similar to [`set_raw_value_by()`](Self::set_raw_value_by()), but only sets existing values in sections matching
Expand Down
11 changes: 11 additions & 0 deletions gix-config/tests/config/file/access/raw/set_raw_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ fn non_existing_values_cannot_be_set() -> crate::Result {
);
Ok(())
}

#[test]
fn accepts_short_lived_keys() -> crate::Result {
let mut file = gix_config::File::default();
let key = String::from("new.key");

file.set_raw_value(key.as_str(), "value")?;

assert_eq!(file.string("new.key").expect("present").as_ref(), "value");
Ok(())
}
4 changes: 2 additions & 2 deletions gix/examples/init-repo-and-commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn main() -> anyhow::Result<()> {
let empty_tree_id = repo.write_object(&tree)?.detach();

let mut config = repo.config_snapshot_mut();
config.set_raw_value(&Author::NAME, "Maria Sanchez")?;
config.set_raw_value(&Author::EMAIL, "maria@example.com")?;
config.set_raw_value(Author::NAME, "Maria Sanchez")?;
config.set_raw_value(Author::EMAIL, "maria@example.com")?;
{
let repo = config.commit_auto_rollback()?;
let initial_commit_id = repo.commit("HEAD", "initial commit", empty_tree_id, gix::commit::NO_PARENT_IDS)?;
Expand Down
4 changes: 2 additions & 2 deletions gix/src/repository/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl crate::Repository {
if self.committer().is_none() {
let mut config = gix_config::File::new(gix_config::file::Metadata::api());
config
.set_raw_value(&gitoxide::Committer::NAME_FALLBACK, "no name configured")
.set_raw_value(gitoxide::Committer::NAME_FALLBACK, "no name configured")
.expect("works - statically known");
config
.set_raw_value(&gitoxide::Committer::EMAIL_FALLBACK, "noEmailAvailable@example.com")
.set_raw_value(gitoxide::Committer::EMAIL_FALLBACK, "noEmailAvailable@example.com")
.expect("works - statically known");
let mut repo_config = self.config_snapshot_mut();
repo_config.append(config);
Expand Down
2 changes: 1 addition & 1 deletion gix/tests/gix/remote/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ mod blocking_and_async_io {
let (mut repo, _tmp) = repo_rw("two-origins");
if let Some(version) = version {
repo.config_snapshot_mut()
.set_raw_value(&Protocol::VERSION, (version as u8).to_string().as_str())?;
.set_raw_value(Protocol::VERSION, (version as u8).to_string().as_str())?;
}

// No updates
Expand Down
2 changes: 1 addition & 1 deletion gix/tests/gix/remote/ref_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mod blocking_and_async_io {
let mut repo = remote::repo("clone");
if let Some(version) = version {
repo.config_snapshot_mut()
.set_raw_value(&Protocol::VERSION, (version as u8).to_string().as_str())?;
.set_raw_value(Protocol::VERSION, (version as u8).to_string().as_str())?;
}

let remote = into_daemon_remote_if_async(
Expand Down
14 changes: 7 additions & 7 deletions gix/tests/gix/repository/config/config_snapshot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn commit_auto_rollback() -> crate::Result {

{
let mut config = repo.config_snapshot_mut();
config.set_raw_value(&Core::ABBREV, "4")?;
config.set_raw_value(Core::ABBREV, "4")?;
let repo = config.commit_auto_rollback()?;
assert_eq!(repo.head_id()?.shorten()?.to_string(), "3189");
}
Expand All @@ -21,7 +21,7 @@ fn commit_auto_rollback() -> crate::Result {

let repo = {
let mut config = repo.config_snapshot_mut();
config.set_raw_value(&Core::ABBREV, "4")?;
config.set_raw_value(Core::ABBREV, "4")?;
let mut repo = config.commit_auto_rollback()?;
assert_eq!(repo.head_id()?.shorten()?.to_string(), "3189");
// access to the mutable repo underneath
Expand All @@ -39,7 +39,7 @@ mod trusted_path {
#[test]
fn optional_is_respected() -> crate::Result {
let mut repo: gix::Repository = named_repo("make_basic_repo.sh")?;
repo.config_snapshot_mut().set_raw_value(&"my.path", "does-not-exist")?;
repo.config_snapshot_mut().set_raw_value("my.path", "does-not-exist")?;

let actual = repo
.config_snapshot()
Expand All @@ -53,7 +53,7 @@ mod trusted_path {
);

repo.config_snapshot_mut()
.set_raw_value(&"my.path", ":(optional)does-not-exist")?;
.set_raw_value("my.path", ":(optional)does-not-exist")?;
let actual = repo.config_snapshot().trusted_path("my.path").transpose()?;
assert_eq!(actual, None, "non-existing paths aren't returned to the caller");
Ok(())
Expand All @@ -71,7 +71,7 @@ fn snapshot_mut_commit_and_forget() -> crate::Result {
assert_eq!(repo.config_snapshot().integer("core.abbrev").expect("set"), 4);
{
let mut repo = repo.config_snapshot_mut();
repo.set_raw_value(&Core::ABBREV, "8")?;
repo.set_raw_value(Core::ABBREV, "8")?;
repo.forget();
}
assert_eq!(repo.config_snapshot().integer("core.abbrev"), Some(4));
Expand All @@ -89,7 +89,7 @@ fn values_are_set_in_memory_only() {

{
let mut config = repo.config_snapshot_mut();
config.set_raw_value(&"hallo.welt", "true").unwrap();
config.set_raw_value("hallo.welt", "true").unwrap();
config
.set_subsection_value(&Branch::MERGE, "main", "refs/heads/foo")
.unwrap();
Expand Down Expand Up @@ -192,7 +192,7 @@ fn reload_reloads_on_disk_changes() -> crate::Result {
fn reload_discards_in_memory_only_changes() -> crate::Result {
let mut repo = named_repo("make_config_repo.sh")?;

repo.config_snapshot_mut().set_raw_value(&Core::ABBREV, "4")?;
repo.config_snapshot_mut().set_raw_value(Core::ABBREV, "4")?;
assert_eq!(repo.config_snapshot().integer("core.abbrev"), Some(4));

repo.reload()?;
Expand Down
2 changes: 1 addition & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn main() -> Result<()> {
.boolean(gix::config::tree::Gitoxide::PARSE_PRECIOUS)
.is_none()
{
config_mut.set_raw_value(&gix::config::tree::Gitoxide::PARSE_PRECIOUS, "true")?;
config_mut.set_raw_value(gix::config::tree::Gitoxide::PARSE_PRECIOUS, "true")?;
}
}
Ok(repo)
Expand Down
Loading