Skip to content

Commit 615ff19

Browse files
committed
feat: add Repository::committer_or_set_generic_fallback().
That way one can always obtain a committer, even though it might not represent the entity actually committing.
1 parent 62e4bab commit 615ff19

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

gix/src/clone/fetch/mod.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,11 @@ impl PrepareFetch {
8181
.as_mut()
8282
.expect("user error: multiple calls are allowed only until it succeeds");
8383

84-
if repo.committer().is_none() {
85-
let mut config = gix_config::File::new(gix_config::file::Metadata::api());
86-
config
87-
.set_raw_value(&gitoxide::Committer::NAME_FALLBACK, "no name configured during fetch")
88-
.expect("works - statically known");
89-
config
90-
.set_raw_value(&gitoxide::Committer::EMAIL_FALLBACK, "[email protected]")
91-
.expect("works - statically known");
92-
let mut repo_config = repo.config_snapshot_mut();
93-
repo_config.append(config);
94-
repo_config.commit().expect("configuration is still valid");
95-
}
84+
repo.committer_or_set_generic_fallback()?;
9685

9786
if !self.config_overrides.is_empty() {
9887
let mut snapshot = repo.config_snapshot_mut();
9988
snapshot.append_config(&self.config_overrides, gix_config::Source::Api)?;
100-
snapshot.commit()?;
10189
}
10290

10391
let remote_name = match self.remote_name.as_ref() {

gix/src/clone/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(clippy::result_large_err)]
2-
use crate::{bstr::BString, config::tree::gitoxide, remote};
2+
use crate::{bstr::BString, remote};
33

44
type ConfigureRemoteFn =
55
Box<dyn FnMut(crate::Remote<'_>) -> Result<crate::Remote<'_>, Box<dyn std::error::Error + Send + Sync>>>;
@@ -46,6 +46,8 @@ pub enum Error {
4646
#[error(transparent)]
4747
Init(#[from] crate::init::Error),
4848
#[error(transparent)]
49+
CommitterOrFallback(#[from] crate::config::time::Error),
50+
#[error(transparent)]
4951
UrlParse(#[from] gix_url::parse::Error),
5052
#[error("Failed to turn a the relative file url \"{}\" into an absolute one", url.to_bstring())]
5153
CanonicalizeUrl {
@@ -102,18 +104,7 @@ impl PrepareFetch {
102104
url: url.clone(),
103105
source: err,
104106
})?;
105-
if repo.committer().is_none() {
106-
let mut config = gix_config::File::new(gix_config::file::Metadata::api());
107-
config
108-
.set_raw_value(&gitoxide::Committer::NAME_FALLBACK, "no name configured during clone")
109-
.expect("works - statically known");
110-
config
111-
.set_raw_value(&gitoxide::Committer::EMAIL_FALLBACK, "[email protected]")
112-
.expect("works - statically known");
113-
let mut repo_config = repo.config_snapshot_mut();
114-
repo_config.append(config);
115-
repo_config.commit().expect("configuration is still valid");
116-
}
107+
repo.committer_or_set_generic_fallback()?;
117108
Ok(PrepareFetch {
118109
url,
119110
#[cfg(any(feature = "async-network-client", feature = "blocking-network-client"))]

gix/src/repository/identity.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ impl crate::Repository {
4343
.into()
4444
}
4545

46+
/// Return the committer or its fallback just like [`committer()`](Self::committer()), but if *not* set generate a
47+
/// possibly arbitrary fallback and configure it in memory on this instance. That fallback is then returned and future
48+
/// calls to [`committer()`](Self::committer()) will return it as well.
49+
pub fn committer_or_set_generic_fallback(&mut self) -> Result<gix_actor::SignatureRef<'_>, config::time::Error> {
50+
if self.committer().is_none() {
51+
let mut config = gix_config::File::new(gix_config::file::Metadata::api());
52+
config
53+
.set_raw_value(&gitoxide::Committer::NAME_FALLBACK, "no name configured during fetch")
54+
.expect("works - statically known");
55+
config
56+
.set_raw_value(&gitoxide::Committer::EMAIL_FALLBACK, "[email protected]")
57+
.expect("works - statically known");
58+
let mut repo_config = self.config_snapshot_mut();
59+
repo_config.append(config);
60+
}
61+
self.committer().expect("committer was just set")
62+
}
63+
4664
/// Return the author as configured by this repository, which is determined by…
4765
///
4866
/// * …the git configuration `author.name|email`…

gix/tests/gix-init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ mod with_overrides {
268268
Ok(())
269269
}
270270

271-
fn cow_bstr(s: &str) -> Cow<BStr> {
271+
fn cow_bstr(s: &str) -> Cow<'_, BStr> {
272272
Cow::Borrowed(s.into())
273273
}
274274
}

gix/tests/gix/object/tree/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ mod track_rewrites {
879879
}
880880
}
881881

882-
fn tree_named(repo: &gix::Repository, rev_spec: impl AsRef<str>) -> gix::Tree {
882+
fn tree_named(repo: &gix::Repository, rev_spec: impl AsRef<str>) -> gix::Tree<'_> {
883883
repo.rev_parse_single(rev_spec.as_ref())
884884
.unwrap()
885885
.object()

0 commit comments

Comments
 (0)