Skip to content

Commit

Permalink
settings: cache commit and operation parameters by UserSettings
Browse files Browse the repository at this point in the history
This helps propagate configuration error. RevsetParseContext is also updated
because it was easier to pass &str in to it.
  • Loading branch information
yuja committed Dec 23, 2024
1 parent d91e355 commit 4a69d01
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
16 changes: 8 additions & 8 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ static BUILTIN_FUNCTION_MAP: Lazy<HashMap<&'static str, RevsetFunction>> = Lazy:
// are generally (although not universally) treated as case‐insensitive too, so
// we use a case‐insensitive match here.
Ok(RevsetExpression::filter(RevsetFilterPredicate::Author(
StringPattern::exact_i(&context.user_email),
StringPattern::exact_i(context.user_email),
)))
});
map.insert("committer", |diagnostics, function, _context| {
Expand Down Expand Up @@ -2629,7 +2629,7 @@ impl RevsetExtensions {
#[derive(Clone)]
pub struct RevsetParseContext<'a> {
aliases_map: &'a RevsetAliasesMap,
user_email: String,
user_email: &'a str,
date_pattern_context: DatePatternContext,
extensions: &'a RevsetExtensions,
workspace: Option<RevsetWorkspaceContext<'a>>,
Expand All @@ -2638,7 +2638,7 @@ pub struct RevsetParseContext<'a> {
impl<'a> RevsetParseContext<'a> {
pub fn new(
aliases_map: &'a RevsetAliasesMap,
user_email: String,
user_email: &'a str,
date_pattern_context: DatePatternContext,
extensions: &'a RevsetExtensions,
workspace: Option<RevsetWorkspaceContext<'a>>,
Expand All @@ -2656,8 +2656,8 @@ impl<'a> RevsetParseContext<'a> {
self.aliases_map
}

pub fn user_email(&self) -> &str {
&self.user_email
pub fn user_email(&self) -> &'a str {
self.user_email
}

pub fn date_pattern_context(&self) -> &DatePatternContext {
Expand Down Expand Up @@ -2706,7 +2706,7 @@ mod tests {
let extensions = RevsetExtensions::default();
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
"[email protected]",
chrono::Utc::now().fixed_offset().into(),
&extensions,
None,
Expand Down Expand Up @@ -2735,7 +2735,7 @@ mod tests {
let extensions = RevsetExtensions::default();
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
"[email protected]",
chrono::Utc::now().fixed_offset().into(),
&extensions,
Some(workspace_ctx),
Expand All @@ -2760,7 +2760,7 @@ mod tests {
let extensions = RevsetExtensions::default();
let context = RevsetParseContext::new(
&aliases_map,
"[email protected]".to_string(),
"[email protected]",
chrono::Utc::now().fixed_offset().into(),
&extensions,
None,
Expand Down
40 changes: 27 additions & 13 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ use crate::signing::SignBehavior;
#[derive(Debug, Clone)]
pub struct UserSettings {
config: StackedConfig,
user_name: String,
user_email: String,
commit_timestamp: Option<Timestamp>,
operation_timestamp: Option<Timestamp>,
operation_hostname: String,
operation_username: String,
rng: Arc<JJRng>,
}

Expand Down Expand Up @@ -106,7 +110,7 @@ impl SignSettings {
} else {
SignBehavior::Keep
},
user_email: settings.user_email(),
user_email: settings.user_email().to_owned(),
key: settings.get_string("signing.key").ok(),
}
}
Expand Down Expand Up @@ -142,17 +146,29 @@ fn to_timestamp(value: ConfigValue) -> Result<Timestamp, Box<dyn std::error::Err

impl UserSettings {
pub fn from_config(config: StackedConfig) -> Result<Self, ConfigGetError> {
let user_name = config.get("user.name").unwrap_or_default();
let user_email = config.get("user.email").unwrap_or_default();
let commit_timestamp = config
.get_value_with("debug.commit-timestamp", to_timestamp)
.optional()?;
let operation_timestamp = config
.get_value_with("debug.operation-timestamp", to_timestamp)
.optional()?;
let operation_hostname = config
.get("operation.hostname")
.unwrap_or_else(|_| whoami::fallible::hostname().expect("valid hostname"));
let operation_username = config
.get("operation.username")
.unwrap_or_else(|_| whoami::username());
let rng_seed = config.get::<u64>("debug.randomness-seed").optional()?;
Ok(UserSettings {
config,
user_name,
user_email,
commit_timestamp,
operation_timestamp,
operation_hostname,
operation_username,
rng: Arc::new(JJRng::new(rng_seed)),
})
}
Expand All @@ -168,15 +184,15 @@ impl UserSettings {
self.rng.clone()
}

pub fn user_name(&self) -> String {
self.get_string("user.name").unwrap_or_default()
pub fn user_name(&self) -> &str {
&self.user_name
}

// Must not be changed to avoid git pushing older commits with no set name
pub const USER_NAME_PLACEHOLDER: &'static str = "(no name configured)";

pub fn user_email(&self) -> String {
self.get_string("user.email").unwrap_or_default()
pub fn user_email(&self) -> &str {
&self.user_email
}

pub fn fsmonitor_settings(&self) -> Result<FsmonitorSettings, ConfigGetError> {
Expand All @@ -195,21 +211,19 @@ impl UserSettings {
self.operation_timestamp
}

pub fn operation_hostname(&self) -> String {
self.get_string("operation.hostname")
.unwrap_or_else(|_| whoami::fallible::hostname().expect("valid hostname"))
pub fn operation_hostname(&self) -> &str {
&self.operation_hostname
}

pub fn operation_username(&self) -> String {
self.get_string("operation.username")
.unwrap_or_else(|_| whoami::username())
pub fn operation_username(&self) -> &str {
&self.operation_username
}

pub fn signature(&self) -> Signature {
let timestamp = self.commit_timestamp.unwrap_or_else(Timestamp::now);
Signature {
name: self.user_name(),
email: self.user_email(),
name: self.user_name().to_owned(),
email: self.user_email().to_owned(),
timestamp,
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ pub fn create_op_metadata(
.operation_timestamp()
.unwrap_or_else(Timestamp::now);
let end_time = start_time;
let hostname = user_settings.operation_hostname();
let username = user_settings.operation_username();
let hostname = user_settings.operation_hostname().to_owned();
let username = user_settings.operation_username().to_owned();
OperationMetadata {
start_time,
end_time,
Expand Down
5 changes: 2 additions & 3 deletions lib/tests/test_revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ fn resolve_symbol_with_extensions(
) -> Result<Vec<CommitId>, RevsetResolutionError> {
let aliases_map = RevsetAliasesMap::default();
let now = chrono::Local::now();
let context =
RevsetParseContext::new(&aliases_map, String::new(), now.into(), extensions, None);
let context = RevsetParseContext::new(&aliases_map, "", now.into(), extensions, None);
let expression = parse(&mut RevsetDiagnostics::new(), symbol, &context).unwrap();
assert_matches!(*expression, RevsetExpression::CommitRef(_));
let symbol_resolver = DefaultSymbolResolver::new(repo, extensions.symbol_resolvers());
Expand Down Expand Up @@ -2971,7 +2970,7 @@ fn test_evaluate_expression_mine() {
.set_parents(vec![commit1.id().clone()])
.set_author(Signature {
name: "name2".to_string(),
email: settings.user_email(),
email: settings.user_email().to_owned(),
timestamp,
})
.write()
Expand Down

0 comments on commit 4a69d01

Please sign in to comment.