configparser: support "%include builtin:git.rc"

Summary:
The `%include builtin:git.rc` will be written to repo `hgrc` when creating
repos backed by git. The main benefit is that we can update the builtin
git.rc without migrating existing hgrc files in the future.

Reviewed By: DurhamG

Differential Revision: D33352777

fbshipit-source-id: 9b6904e4bc0eff53bf623ce9ae382d723da1213e
This commit is contained in:
Jun Wu 2022-01-19 17:37:36 -08:00 committed by Facebook GitHub Bot
parent fe6348cc0e
commit cf612fcd26
3 changed files with 73 additions and 7 deletions

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
//! Builtin hgrc templates
static GIT_RC: &str = r#"
[extensions]
remotenames=
treemanifest=
autopullhoisthotfix=!
[commitcloud]
automigrate=false
[remotenames]
autopullhoistpattern=
autopullpattern=
disallowedto=^origin/
disallowhint=please don't specify 'origin/' prefix in remote bookmark's name
hoist=origin
[smartlog]
names=main,master
repos=origin/
[experimental]
copytrace=off
%include /etc/mercurial/git_overrides.rc
%include %PROGRAMDATA%/Facebook/Mercurial/git_overrides.rc
"#;
pub(crate) fn get(name: &str) -> Option<&'static str> {
if name == "builtin:git.rc" {
Some(GIT_RC)
} else {
None
}
}

View File

@ -372,9 +372,15 @@ impl ConfigSet {
if let Rule::line = pair.as_rule() {
if !skip_include {
let include_path = pair.as_str();
let full_include_path =
path.parent().unwrap().join(expand_path(include_path));
this.load_file(&full_include_path, opts, visited, errors);
if let Some(content) = crate::builtin::get(include_path) {
let text = Text::from(content);
let path = Path::new(include_path);
this.load_file_content(path, text, opts, visited, errors);
} else {
let full_include_path =
path.parent().unwrap().join(expand_path(include_path));
this.load_file(&full_include_path, opts, visited, errors);
}
}
}
}
@ -520,10 +526,11 @@ impl ConfigSet {
// If only certain locations are allowed, and this isn't one of them, remove
// it. If location is None, it came from inmemory, so don't filter it.
if let Some(location) = location {
if allowed_locations
.as_ref()
.map(|a| a.contains(location.as_str()))
== Some(false)
if crate::builtin::get(location.as_str()).is_none()
&& allowed_locations
.as_ref()
.map(|a| a.contains(location.as_str()))
== Some(false)
&& allowed_configs
.as_ref()
.map(|a| a.contains(&(sname, kname)))
@ -1109,6 +1116,21 @@ pub(crate) mod tests {
assert_eq!(cfg.get("y", "b"), Some(Text::from("1")));
}
#[test]
fn test_parse_include_builtin() {
let dir = TempDir::new("test_parse_include").unwrap();
write_file(dir.path().join("rootrc"), "%include builtin:git.rc\n");
let mut cfg = ConfigSet::new();
let errors = cfg.load_path(
dir.path().join("rootrc"),
&"test_parse_include_builtin".into(),
);
assert!(errors.is_empty());
assert_eq!(cfg.get("remotenames", "hoist"), Some(Text::from("origin")));
}
#[test]
fn test_parse_include_expand() {
use std::env;

View File

@ -66,6 +66,7 @@
//! line3
//! ```
pub(crate) mod builtin;
pub mod c_api;
pub mod config;
pub mod hg;