mirror of
https://github.com/openai/codex.git
synced 2026-12-05 01:00:54 +00:00
git-utils: accept implicit status booleans
This commit is contained in:
@@ -154,6 +154,21 @@ impl TryFrom<GitConfigValueEntry> for GitConfigEntry {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_implicit_boolean_entry(
|
||||
mut entry: GitConfigValueEntry,
|
||||
implicit_boolean_keys: &[&str],
|
||||
) -> io::Result<GitConfigEntry> {
|
||||
if entry.value == GitConfigValue::Implicit
|
||||
&& implicit_boolean_keys.contains(&entry.key.as_str())
|
||||
{
|
||||
// Git's config grammar defines a key without `=` as Boolean true.
|
||||
// Normalize only keys whose consumer accepts Boolean values; all
|
||||
// other implicit values retain the ordinary strict rejection.
|
||||
entry.value = GitConfigValue::Explicit("true".to_string());
|
||||
}
|
||||
GitConfigEntry::try_from(entry)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum GitConfigOrigin {
|
||||
CommandLine,
|
||||
@@ -475,6 +490,30 @@ async fn read_config_entries_with_fallback_async(
|
||||
follow_includes: bool,
|
||||
config_file: Option<&Path>,
|
||||
) -> io::Result<Vec<GitConfigEntry>> {
|
||||
read_config_value_entries_with_fallback_async(
|
||||
git,
|
||||
cwd,
|
||||
git_config_args,
|
||||
pattern,
|
||||
probe,
|
||||
follow_includes,
|
||||
config_file,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(GitConfigEntry::try_from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn read_config_value_entries_with_fallback_async(
|
||||
git: &GitRunner,
|
||||
cwd: &Path,
|
||||
git_config_args: &[String],
|
||||
pattern: &str,
|
||||
probe: &str,
|
||||
follow_includes: bool,
|
||||
config_file: Option<&Path>,
|
||||
) -> io::Result<Vec<GitConfigValueEntry>> {
|
||||
let scoped = run_effective_config_query_async(
|
||||
git,
|
||||
cwd,
|
||||
@@ -490,7 +529,7 @@ async fn read_config_entries_with_fallback_async(
|
||||
.code()
|
||||
.is_some_and(|code| code == 0 || code == 1)
|
||||
{
|
||||
return parse_config_entries(&scoped.stdout);
|
||||
return parse_config_value_entries(&scoped.stdout);
|
||||
}
|
||||
|
||||
let legacy = run_effective_config_query_async(
|
||||
@@ -514,7 +553,7 @@ async fn read_config_entries_with_fallback_async(
|
||||
String::from_utf8_lossy(&legacy.stderr).trim()
|
||||
)));
|
||||
}
|
||||
parse_config_entries_with_origins(&legacy.stdout)
|
||||
parse_config_value_entries_with_origins(&legacy.stdout)
|
||||
}
|
||||
|
||||
pub(crate) fn read_effective_config_with_fallback(
|
||||
@@ -554,6 +593,35 @@ pub(crate) async fn read_effective_config_with_fallback_async(
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Read effective config while accepting implicit values only for the named
|
||||
/// Boolean keys. Git defines an implicit Boolean as true; path-, enum-, and
|
||||
/// integer-valued consumers remain strict.
|
||||
pub(crate) async fn read_effective_config_with_implicit_booleans_async(
|
||||
git: &GitRunner,
|
||||
cwd: &Path,
|
||||
git_config_args: &[String],
|
||||
pattern: &str,
|
||||
probe: &str,
|
||||
implicit_boolean_keys: &[&str],
|
||||
) -> io::Result<BTreeMap<String, GitConfigEntry>> {
|
||||
read_config_value_entries_with_fallback_async(
|
||||
git,
|
||||
cwd,
|
||||
git_config_args,
|
||||
pattern,
|
||||
probe,
|
||||
/*follow_includes*/ true,
|
||||
/*config_file*/ None,
|
||||
)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|entry| {
|
||||
let entry = normalize_implicit_boolean_entry(entry, implicit_boolean_keys)?;
|
||||
Ok((entry.key.clone(), entry))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn run_effective_config_query(
|
||||
git: &GitRunner,
|
||||
cwd: &Path,
|
||||
|
||||
@@ -11,7 +11,26 @@ use super::command_failure;
|
||||
use crate::git_command::IsolatedGitReadContext;
|
||||
use crate::git_command::MAX_INTERNAL_GIT_OUTPUT_BYTES;
|
||||
use crate::git_config::parse_git_boolean;
|
||||
use crate::git_config::read_effective_config_with_implicit_booleans_async;
|
||||
const STATUS_SAFE_CONFIG_PATTERN: &str = r"^(attr\.tree|core\.(filemode|symlinks|ignorecase|precomposeunicode|protecthfs|protectntfs|trustctime|checkstat|longpaths|fscache|splitindex|sparsecheckout|sparsecheckoutcone|autocrlf|eol|safecrlf|checkroundtripencoding|bigfilethreshold|quotepath|abbrev)|index\.(sparse|version))$";
|
||||
const STATUS_IMPLICIT_BOOLEAN_KEYS: &[&str] = &[
|
||||
"core.filemode",
|
||||
"core.symlinks",
|
||||
"core.ignorecase",
|
||||
"core.precomposeunicode",
|
||||
"core.protecthfs",
|
||||
"core.protectntfs",
|
||||
"core.trustctime",
|
||||
"core.longpaths",
|
||||
"core.fscache",
|
||||
"core.splitindex",
|
||||
"core.sparsecheckout",
|
||||
"core.sparsecheckoutcone",
|
||||
"core.autocrlf",
|
||||
"core.safecrlf",
|
||||
"core.quotepath",
|
||||
"index.sparse",
|
||||
];
|
||||
const REPOSITORY_FORMAT_CONFIG_PATTERN: &str =
|
||||
r"^(core\.repositoryformatversion|extensions\.(objectformat|compatobjectformat))$";
|
||||
const MAX_STATUS_ATTRIBUTE_SOURCE_BYTES: usize = 16 * 1024 * 1024;
|
||||
@@ -108,10 +127,15 @@ impl GuardedGitConfig<'_> {
|
||||
core_attributes_file: Option<&OsStr>,
|
||||
configured_core_symlinks: Option<bool>,
|
||||
) -> io::Result<()> {
|
||||
let entries = self
|
||||
.sources
|
||||
.read_effective_async(STATUS_SAFE_CONFIG_PATTERN, "status allowlist")
|
||||
.await?;
|
||||
let entries = read_effective_config_with_implicit_booleans_async(
|
||||
self.sources.git,
|
||||
&self.sources.canonical_root,
|
||||
&self.sources.base_config_args,
|
||||
STATUS_SAFE_CONFIG_PATTERN,
|
||||
"status allowlist",
|
||||
STATUS_IMPLICIT_BOOLEAN_KEYS,
|
||||
)
|
||||
.await?;
|
||||
let repository_format = self
|
||||
.sources
|
||||
.read_direct_common_config_async(REPOSITORY_FORMAT_CONFIG_PATTERN, "repository format")
|
||||
@@ -351,3 +375,7 @@ fn parse_status_config_path(output: &[u8]) -> io::Result<OsString> {
|
||||
}
|
||||
crate::safe_git::git_path_argument(value)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "status_context_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
use std::io::Write;
|
||||
|
||||
use super::*;
|
||||
use crate::git_command::GitRunner;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn init_repo() -> tempfile::TempDir {
|
||||
let repo = tempfile::tempdir().expect("repository");
|
||||
let mut command = std::process::Command::new("git");
|
||||
crate::safe_git::isolate_git_command_environment(&mut command);
|
||||
let output = command
|
||||
.args(["init", "-q"])
|
||||
.current_dir(repo.path())
|
||||
.output()
|
||||
.expect("initialize repository");
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"git init failed: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
repo
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn status_config_normalizes_only_implicit_boolean_keys() {
|
||||
let repo = init_repo();
|
||||
let config_path = repo.path().join(".git/config");
|
||||
let mut config = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&config_path)
|
||||
.expect("open repository config");
|
||||
writeln!(config, "[core]").expect("start core section");
|
||||
for key in STATUS_IMPLICIT_BOOLEAN_KEYS {
|
||||
if let Some(name) = key.strip_prefix("core.") {
|
||||
writeln!(config, "\t{name}").expect("append implicit core Boolean");
|
||||
}
|
||||
}
|
||||
writeln!(config, "[index]\n\tsparse").expect("append implicit index Boolean");
|
||||
drop(config);
|
||||
|
||||
let git = GitRunner::for_cwd_io(repo.path()).expect("Git runner");
|
||||
let entries = read_effective_config_with_implicit_booleans_async(
|
||||
&git,
|
||||
repo.path(),
|
||||
&[],
|
||||
STATUS_SAFE_CONFIG_PATTERN,
|
||||
"status allowlist test",
|
||||
STATUS_IMPLICIT_BOOLEAN_KEYS,
|
||||
)
|
||||
.await
|
||||
.expect("read implicit Status Booleans");
|
||||
for key in STATUS_IMPLICIT_BOOLEAN_KEYS {
|
||||
assert_eq!(
|
||||
entries.get(*key).map(|entry| entry.value.as_str()),
|
||||
Some("true"),
|
||||
"implicit {key}"
|
||||
);
|
||||
}
|
||||
|
||||
let mut config = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(&config_path)
|
||||
.expect("reopen repository config");
|
||||
write!(
|
||||
config,
|
||||
"[core]\n\tfilemode =\n\tcheckstat\n\teol\n\tcheckroundtripencoding\n\tbigfilethreshold\n\tabbrev\n\
|
||||
[attr]\n\ttree\n\
|
||||
[index]\n\tversion\n"
|
||||
)
|
||||
.expect("append explicit empty Boolean and strict implicit values");
|
||||
drop(config);
|
||||
|
||||
let explicit_empty = read_effective_config_with_implicit_booleans_async(
|
||||
&git,
|
||||
repo.path(),
|
||||
&[],
|
||||
r"^core\.filemode$",
|
||||
"explicit empty Boolean test",
|
||||
STATUS_IMPLICIT_BOOLEAN_KEYS,
|
||||
)
|
||||
.await
|
||||
.expect("read explicit empty Boolean");
|
||||
assert_eq!(
|
||||
explicit_empty
|
||||
.get("core.filemode")
|
||||
.map(|entry| entry.value.as_str()),
|
||||
Some("")
|
||||
);
|
||||
|
||||
for key in [
|
||||
"attr.tree",
|
||||
"core.checkstat",
|
||||
"core.eol",
|
||||
"core.checkroundtripencoding",
|
||||
"core.bigfilethreshold",
|
||||
"core.abbrev",
|
||||
"index.version",
|
||||
] {
|
||||
let pattern = format!(r"^{}$", key.replace('.', r"\."));
|
||||
let error = read_effective_config_with_implicit_booleans_async(
|
||||
&git,
|
||||
repo.path(),
|
||||
&[],
|
||||
&pattern,
|
||||
"strict implicit Status value test",
|
||||
STATUS_IMPLICIT_BOOLEAN_KEYS,
|
||||
)
|
||||
.await
|
||||
.expect_err("non-Boolean implicit Status value must remain invalid");
|
||||
assert_eq!(error.kind(), io::ErrorKind::InvalidData, "implicit {key}");
|
||||
}
|
||||
}
|
||||
@@ -823,6 +823,31 @@ async fn get_has_changes_pins_normal_untracked_files_over_repository_config() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_has_changes_accepts_implicit_boolean_status_config() {
|
||||
use std::io::Write;
|
||||
|
||||
let temp_dir = tempfile::tempdir().expect("create temp dir");
|
||||
let repo_path = create_test_git_repo(&temp_dir).await;
|
||||
let mut config = std::fs::OpenOptions::new()
|
||||
.append(true)
|
||||
.open(repo_path.join(".git/config"))
|
||||
.expect("open repository config");
|
||||
writeln!(config, "[core]\n\tfilemode").expect("append implicit core.filemode");
|
||||
drop(config);
|
||||
|
||||
let stock = run_git(&repo_path, &["status", "--porcelain"]);
|
||||
assert!(
|
||||
stock.status.success(),
|
||||
"stock Git rejected implicit Boolean"
|
||||
);
|
||||
assert!(stock.stdout.is_empty(), "fixture must remain clean");
|
||||
assert_eq!(
|
||||
try_get_has_changes_with_test_timeout(&repo_path).await,
|
||||
Ok(false)
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_has_changes_distinguishes_filter_sentinels_from_literal_driver_names() {
|
||||
for (driver, sentinel_attribute) in
|
||||
|
||||
Reference in New Issue
Block a user