just playing with tangled
at tmp-tutorial 233 lines 7.0 kB view raw
1// Copyright 2020 The Jujutsu Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#![allow(missing_docs)] 16 17use std::path::Path; 18use std::sync::{Arc, Mutex}; 19 20use chrono::DateTime; 21use rand::prelude::*; 22use rand_chacha::ChaCha20Rng; 23 24use crate::backend::{ChangeId, ObjectId, Signature, Timestamp}; 25use crate::fsmonitor::FsmonitorKind; 26 27#[derive(Debug, Clone)] 28pub struct UserSettings { 29 config: config::Config, 30 timestamp: Option<Timestamp>, 31 rng: Arc<JJRng>, 32} 33 34#[derive(Debug, Clone)] 35pub struct RepoSettings { 36 _config: config::Config, 37} 38 39#[derive(Debug, Clone)] 40pub struct GitSettings { 41 pub auto_local_branch: bool, 42} 43 44impl GitSettings { 45 pub fn from_config(config: &config::Config) -> Self { 46 GitSettings { 47 auto_local_branch: config.get_bool("git.auto-local-branch").unwrap_or(true), 48 } 49 } 50} 51 52impl Default for GitSettings { 53 fn default() -> Self { 54 GitSettings { 55 auto_local_branch: true, 56 } 57 } 58} 59 60fn get_timestamp_config(config: &config::Config, key: &str) -> Option<Timestamp> { 61 match config.get_string(key) { 62 Ok(timestamp_str) => match DateTime::parse_from_rfc3339(&timestamp_str) { 63 Ok(datetime) => Some(Timestamp::from_datetime(datetime)), 64 Err(_) => None, 65 }, 66 Err(_) => None, 67 } 68} 69 70fn get_rng_seed_config(config: &config::Config) -> Option<u64> { 71 config 72 .get_string("debug.randomness-seed") 73 .ok() 74 .and_then(|str| str.parse().ok()) 75} 76 77impl UserSettings { 78 pub fn from_config(config: config::Config) -> Self { 79 let timestamp = get_timestamp_config(&config, "debug.commit-timestamp"); 80 let rng_seed = get_rng_seed_config(&config); 81 UserSettings { 82 config, 83 timestamp, 84 rng: Arc::new(JJRng::new(rng_seed)), 85 } 86 } 87 88 // TODO: Reconsider UserSettings/RepoSettings abstraction. See 89 // https://github.com/martinvonz/jj/issues/616#issuecomment-1345170699 90 pub fn with_repo(&self, _repo_path: &Path) -> Result<RepoSettings, config::ConfigError> { 91 let config = self.config.clone(); 92 Ok(RepoSettings { _config: config }) 93 } 94 95 pub fn get_rng(&self) -> Arc<JJRng> { 96 self.rng.clone() 97 } 98 99 pub fn user_name(&self) -> String { 100 self.config 101 .get_string("user.name") 102 .unwrap_or_else(|_| Self::user_name_placeholder().to_string()) 103 } 104 105 pub fn user_name_placeholder() -> &'static str { 106 "(no name configured)" 107 } 108 109 pub fn user_email(&self) -> String { 110 self.config 111 .get_string("user.email") 112 .unwrap_or_else(|_| Self::user_email_placeholder().to_string()) 113 } 114 115 pub fn fsmonitor_kind(&self) -> Result<Option<FsmonitorKind>, config::ConfigError> { 116 match self.config.get_string("core.fsmonitor") { 117 Ok(fsmonitor_kind) => Ok(Some(fsmonitor_kind.parse()?)), 118 Err(config::ConfigError::NotFound(_)) => Ok(None), 119 Err(err) => Err(err), 120 } 121 } 122 123 pub fn user_email_placeholder() -> &'static str { 124 "(no email configured)" 125 } 126 127 pub fn operation_timestamp(&self) -> Option<Timestamp> { 128 get_timestamp_config(&self.config, "debug.operation-timestamp") 129 } 130 131 pub fn operation_hostname(&self) -> String { 132 self.config 133 .get_string("operation.hostname") 134 .unwrap_or_else(|_| whoami::hostname()) 135 } 136 137 pub fn operation_username(&self) -> String { 138 self.config 139 .get_string("operation.username") 140 .unwrap_or_else(|_| whoami::username()) 141 } 142 143 pub fn push_branch_prefix(&self) -> String { 144 self.config 145 .get_string("push.branch-prefix") 146 .unwrap_or_else(|_| "push-".to_string()) 147 } 148 149 pub fn default_revset(&self) -> String { 150 self.config.get_string("revsets.log").unwrap_or_else(|_| { 151 // For compatibility with old config files (<0.8.0) 152 self.config 153 .get_string("ui.default-revset") 154 .unwrap_or_else(|_| { 155 "@ | (remote_branches() | tags()).. | ((remote_branches() | tags())..)-" 156 .to_string() 157 }) 158 }) 159 } 160 161 pub fn signature(&self) -> Signature { 162 let timestamp = self.timestamp.clone().unwrap_or_else(Timestamp::now); 163 Signature { 164 name: self.user_name(), 165 email: self.user_email(), 166 timestamp, 167 } 168 } 169 170 pub fn allow_native_backend(&self) -> bool { 171 self.config 172 .get_bool("ui.allow-init-native") 173 .unwrap_or(false) 174 } 175 176 pub fn diff_instructions(&self) -> bool { 177 self.config.get_bool("ui.diff-instructions").unwrap_or(true) 178 } 179 180 pub fn config(&self) -> &config::Config { 181 &self.config 182 } 183 184 pub fn git_settings(&self) -> GitSettings { 185 GitSettings::from_config(&self.config) 186 } 187 188 pub fn graph_style(&self) -> String { 189 self.config 190 .get_string("ui.graph.style") 191 .unwrap_or_else(|_| "curved".to_string()) 192 } 193} 194 195/// This Rng uses interior mutability to allow generating random values using an 196/// immutable reference. It also fixes a specific seedable RNG for 197/// reproducibility. 198#[derive(Debug)] 199pub struct JJRng(Mutex<ChaCha20Rng>); 200impl JJRng { 201 pub fn new_change_id(&self, length: usize) -> ChangeId { 202 let mut rng = self.0.lock().unwrap(); 203 let random_bytes = (0..length).map(|_| rng.gen::<u8>()).collect(); 204 ChangeId::new(random_bytes) 205 } 206 207 /// Creates a new RNGs. Could be made public, but we'd like to encourage all 208 /// RNGs references to point to the same RNG. 209 fn new(seed: Option<u64>) -> Self { 210 Self(Mutex::new(JJRng::internal_rng_from_seed(seed))) 211 } 212 213 fn internal_rng_from_seed(seed: Option<u64>) -> ChaCha20Rng { 214 match seed { 215 Some(seed) => ChaCha20Rng::seed_from_u64(seed), 216 None => ChaCha20Rng::from_entropy(), 217 } 218 } 219} 220 221pub trait ConfigResultExt<T> { 222 fn optional(self) -> Result<Option<T>, config::ConfigError>; 223} 224 225impl<T> ConfigResultExt<T> for Result<T, config::ConfigError> { 226 fn optional(self) -> Result<Option<T>, config::ConfigError> { 227 match self { 228 Ok(value) => Ok(Some(value)), 229 Err(config::ConfigError::NotFound(_)) => Ok(None), 230 Err(err) => Err(err), 231 } 232 } 233}