Mercurial > hg-stable
comparison rust/rhg/src/main.rs @ 47410:ebdef6283798
rhg: read [paths] for `--repository` value
hg parses `-R` and `--repository` CLI arguments "early" in order to know which
local repository to load config from. (Config can then affect whether or how to
fall back.)
The value of of those arguments can be not only a filesystem path, but also an
alias configured in the `[paths]` section. This part was missing in rhg and
this patch implements that.
The current patch still lacks functionality to read config of current repository
if we are not at root of repo. That will be fixed in upcoming patches.
A new crate `home` is added to get path of home directory.
Differential Revision: https://phab.mercurial-scm.org/D10296
author | Pulkit Goyal <7895pulkit@gmail.com> |
---|---|
date | Sun, 11 Apr 2021 00:50:10 +0530 |
parents | c71e8d9e7f2a |
children | 88119fffecc8 |
comparison
equal
deleted
inserted
replaced
47409:8a6e6b62b9a3 | 47410:ebdef6283798 |
---|---|
3 use clap::App; | 3 use clap::App; |
4 use clap::AppSettings; | 4 use clap::AppSettings; |
5 use clap::Arg; | 5 use clap::Arg; |
6 use clap::ArgMatches; | 6 use clap::ArgMatches; |
7 use format_bytes::{format_bytes, join}; | 7 use format_bytes::{format_bytes, join}; |
8 use hg::config::Config; | 8 use hg::config::{Config, ConfigSource}; |
9 use hg::repo::{Repo, RepoError}; | 9 use hg::repo::{Repo, RepoError}; |
10 use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes}; | 10 use hg::utils::files::{get_bytes_from_os_str, get_path_from_bytes}; |
11 use hg::utils::SliceExt; | 11 use hg::utils::SliceExt; |
12 use std::ffi::OsString; | 12 use std::ffi::OsString; |
13 use std::path::PathBuf; | 13 use std::path::PathBuf; |
165 .get_bool(b"ui", b"detailed-exit-code") | 165 .get_bool(b"ui", b"detailed-exit-code") |
166 .unwrap_or(false), | 166 .unwrap_or(false), |
167 ) | 167 ) |
168 } | 168 } |
169 } | 169 } |
170 let repo_path = early_args.repo.as_deref().map(get_path_from_bytes); | 170 let repo_arg = early_args.repo.unwrap_or(Vec::new()); |
171 let repo_result = match Repo::find(&non_repo_config, repo_path) { | 171 let repo_path: Option<PathBuf> = { |
172 if repo_arg.is_empty() { | |
173 None | |
174 } else { | |
175 let local_config = { | |
176 if std::env::var_os("HGRCSKIPREPO").is_none() { | |
177 let current_dir = hg::utils::current_dir(); | |
178 // TODO: handle errors from current_dir | |
179 if let Ok(current_dir_path) = current_dir { | |
180 let config_files = vec![ | |
181 ConfigSource::AbsPath( | |
182 current_dir_path.join(".hg/hgrc"), | |
183 ), | |
184 ConfigSource::AbsPath( | |
185 current_dir_path.join(".hg/hgrc-not-shared"), | |
186 ), | |
187 ]; | |
188 // TODO: handle errors from | |
189 // `load_from_explicit_sources` | |
190 Config::load_from_explicit_sources(config_files).ok() | |
191 } else { | |
192 None | |
193 } | |
194 } else { | |
195 None | |
196 } | |
197 }; | |
198 | |
199 let non_repo_config_val = { | |
200 let non_repo_val = non_repo_config.get(b"paths", &repo_arg); | |
201 match &non_repo_val { | |
202 Some(val) if val.len() > 0 => home::home_dir() | |
203 .unwrap_or_else(|| PathBuf::from("~")) | |
204 .join(get_path_from_bytes(val)) | |
205 .canonicalize() | |
206 // TODO: handle error and make it similar to python | |
207 // implementation maybe? | |
208 .ok(), | |
209 _ => None, | |
210 } | |
211 }; | |
212 | |
213 let config_val = match &local_config { | |
214 None => non_repo_config_val, | |
215 Some(val) => { | |
216 let local_config_val = val.get(b"paths", &repo_arg); | |
217 match &local_config_val { | |
218 Some(val) if val.len() > 0 => { | |
219 // presence of a local_config assures that | |
220 // current_dir | |
221 // wont result in an Error | |
222 let canpath = hg::utils::current_dir() | |
223 .unwrap() | |
224 .join(get_path_from_bytes(val)) | |
225 .canonicalize(); | |
226 canpath.ok().or(non_repo_config_val) | |
227 } | |
228 _ => non_repo_config_val, | |
229 } | |
230 } | |
231 }; | |
232 config_val.or(Some(get_path_from_bytes(&repo_arg).to_path_buf())) | |
233 } | |
234 }; | |
235 | |
236 let repo_result = match Repo::find(&non_repo_config, repo_path.to_owned()) | |
237 { | |
172 Ok(repo) => Ok(repo), | 238 Ok(repo) => Ok(repo), |
173 Err(RepoError::NotFound { at }) if repo_path.is_none() => { | 239 Err(RepoError::NotFound { at }) if repo_path.is_none() => { |
174 // Not finding a repo is not fatal yet, if `-R` was not given | 240 // Not finding a repo is not fatal yet, if `-R` was not given |
175 Err(NoRepoInCwdError { cwd: at }) | 241 Err(NoRepoInCwdError { cwd: at }) |
176 } | 242 } |