-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[codex] preserve fsmonitor for worktree Git reads #26880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dbe7f3
[codex] preserve fsmonitor for worktree Git reads
tamird 80893ee
[codex] Gate built-in fsmonitor override
tamird 0294cf2
[codex] Make fsmonitor tests hermetic
tamird f0dba2a
[codex] Preserve fsmonitor in TUI diffs
tamird 89096fe
[codex] Centralize fsmonitor probe policy
tamird 6d893fd
[codex] Share fsmonitor timeout budget
tamird d2b82c0
[codex] Restore per-command Git timeouts
tamird 8d74c88
Merge branch 'main' into fix-fsmonitor-usage
bookholt-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| //! Policy for preserving Git's built-in filesystem monitor. | ||
| //! | ||
| //! Codex overrides `core.fsmonitor` so repository configuration cannot select | ||
| //! an executable helper. Preserve the built-in daemon only when the effective | ||
| //! value is boolean true and Git advertises daemon support. | ||
| //! | ||
| //! The daemon avoids scanning every tracked file and untracked directory: | ||
| //! https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/Documentation/git-fsmonitor--daemon.adoc#L49-L57 | ||
| //! https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/Documentation/git-update-index.adoc#L545-L550 | ||
|
|
||
| use std::future::Future; | ||
|
|
||
| /// The safe `core.fsmonitor` override for an internal Git command. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum FsmonitorOverride { | ||
| /// Disable repository-selected filesystem monitor helpers. | ||
| Disabled, | ||
| /// Preserve Git's built-in filesystem monitor daemon. | ||
| BuiltIn, | ||
| } | ||
|
|
||
| impl FsmonitorOverride { | ||
| /// Returns the complete Git configuration override. | ||
| pub const fn git_config_arg(self) -> &'static str { | ||
| match self { | ||
| Self::Disabled => "core.fsmonitor=false", | ||
| Self::BuiltIn => "core.fsmonitor=true", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Executes the Git commands required by [`detect_fsmonitor_override`]. | ||
| /// | ||
| /// Implementations must return stdout only when Git exits successfully. | ||
| /// Timeouts, spawn or transport failures, signal termination, and nonzero exit | ||
| /// statuses must return `None`. | ||
| pub trait FsmonitorProbeRunner: Send { | ||
| /// Runs one bounded probe in the target repository. | ||
| fn run_probe(&mut self, args: &[&str]) -> impl Future<Output = Option<Vec<u8>>> + Send; | ||
| } | ||
|
|
||
| /// Returns the safe filesystem monitor override for the target repository. | ||
| /// | ||
| /// This intentionally probes every time. Effective Git configuration is | ||
| /// layered, may use conditional includes, and can change while Codex is | ||
| /// running: | ||
| /// https://git-scm.com/docs/git-config#SCOPES | ||
| /// https://git-scm.com/docs/git-config#_conditional_includes | ||
| pub async fn detect_fsmonitor_override( | ||
| runner: &mut impl FsmonitorProbeRunner, | ||
| ) -> FsmonitorOverride { | ||
| // A typed query converts every matching value before `--get` selects the | ||
| // effective one. A shadowed helper path can therefore make a repository- | ||
| // local true fail conversion. Query the raw effective value first. | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/builtin/config.c#L482-L514 | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/builtin/config.c#L611-L614 | ||
| let Some(config) = runner | ||
| .run_probe(&["config", "--null", "--get", "core.fsmonitor"]) | ||
| .await | ||
| else { | ||
| return FsmonitorOverride::Disabled; | ||
| }; | ||
| let Some(config) = config.strip_suffix(b"\0") else { | ||
| return FsmonitorOverride::Disabled; | ||
| }; | ||
| if config.contains(&0) { | ||
| return FsmonitorOverride::Disabled; | ||
| } | ||
| let Ok(config) = str::from_utf8(config) else { | ||
| return FsmonitorOverride::Disabled; | ||
| }; | ||
|
|
||
| // Git accepts these case-insensitive spellings directly, as well as | ||
| // valueless keys and nonzero integers. Ask Git to normalize uncommon | ||
| // spellings, filtering by the raw effective value before conversion so a | ||
| // shadowed helper pathname cannot make the query fail. | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/parse.c#L158-L181 | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/builtin/config.c#L264-L279 | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/builtin/config.c#L496-L507 | ||
| let configured = if ["true", "yes", "on"] | ||
| .iter() | ||
| .any(|value| config.eq_ignore_ascii_case(value)) | ||
| { | ||
| true | ||
| } else if ["false", "no", "off"] | ||
| .iter() | ||
| .any(|value| config.eq_ignore_ascii_case(value)) | ||
| { | ||
| false | ||
| } else { | ||
| let typed_args = [ | ||
| "config", | ||
| "--null", | ||
| "--type=bool", | ||
| "--fixed-value", | ||
| "--get", | ||
| "core.fsmonitor", | ||
| config, | ||
| ]; | ||
| matches!( | ||
| runner.run_probe(&typed_args).await.as_deref(), | ||
| Some(b"true\0") | ||
| ) | ||
| }; | ||
| if !configured { | ||
| return FsmonitorOverride::Disabled; | ||
| } | ||
|
|
||
| // Git 2.35.1 and older interpret "true" as a hook pathname. Before Git | ||
| // 2.26, a successful empty hook response can hide tracked changes. Require | ||
| // the feature line Git added specifically for capability checks. | ||
| // https://github.com/git/git/blob/94f057755b7941b321fd11fec1b2e3ca5313a4e0/Documentation/config/core.adoc#L90-L99 | ||
| // https://github.com/git/git/commit/dd77cf61a1a2fbf52c94d0cd986d555ad2ba8a4b | ||
| let Some(build_options) = runner.run_probe(&["version", "--build-options"]).await else { | ||
| return FsmonitorOverride::Disabled; | ||
| }; | ||
| if build_options | ||
| .split(|byte| *byte == b'\n') | ||
| .any(|line| line.trim_ascii() == b"feature: fsmonitor--daemon") | ||
| { | ||
| FsmonitorOverride::BuiltIn | ||
| } else { | ||
| FsmonitorOverride::Disabled | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "fsmonitor_tests.rs"] | ||
| mod tests; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| use std::collections::VecDeque; | ||
| use std::future::Future; | ||
|
|
||
| use pretty_assertions::assert_eq; | ||
|
|
||
| use super::FsmonitorOverride; | ||
| use super::FsmonitorProbeRunner; | ||
| use super::detect_fsmonitor_override; | ||
|
|
||
| struct ProbeResponse { | ||
| args: Vec<&'static str>, | ||
| output: Option<Vec<u8>>, | ||
| } | ||
|
|
||
| struct FakeRunner { | ||
| responses: VecDeque<ProbeResponse>, | ||
| } | ||
|
|
||
| impl FsmonitorProbeRunner for FakeRunner { | ||
| fn run_probe(&mut self, args: &[&str]) -> impl Future<Output = Option<Vec<u8>>> + Send { | ||
| let response = self.responses.pop_front().expect("missing probe response"); | ||
| assert_eq!(args, response.args); | ||
| std::future::ready(response.output) | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn detects_supported_builtin_fsmonitor_values() { | ||
| let cases = [ | ||
| ( | ||
| "missing config", | ||
| vec![response(config_args(), /*output*/ None)], | ||
| FsmonitorOverride::Disabled, | ||
| ), | ||
| ( | ||
| "helper path", | ||
| vec![ | ||
| response(config_args(), Some(b"/tmp/fsmonitor-helper\0")), | ||
| response( | ||
| typed_config_args("/tmp/fsmonitor-helper"), | ||
| /*output*/ None, | ||
| ), | ||
| ], | ||
| FsmonitorOverride::Disabled, | ||
| ), | ||
| ( | ||
| "false spelling", | ||
| vec![response(config_args(), Some(b"OFF\0"))], | ||
| FsmonitorOverride::Disabled, | ||
| ), | ||
| ( | ||
| "unsupported Git", | ||
| vec![ | ||
| response(config_args(), Some(b"yes\0")), | ||
| response(capability_args(), Some(b"")), | ||
| ], | ||
| FsmonitorOverride::Disabled, | ||
| ), | ||
| ( | ||
| "common true spelling", | ||
| vec![ | ||
| response(config_args(), Some(b"On\0")), | ||
| response(capability_args(), Some(fsmonitor_capability())), | ||
| ], | ||
| FsmonitorOverride::BuiltIn, | ||
| ), | ||
| ( | ||
| "numeric true", | ||
| vec![ | ||
| response(config_args(), Some(b"2k\0")), | ||
| response(typed_config_args("2k"), Some(b"true\0")), | ||
| response(capability_args(), Some(fsmonitor_capability())), | ||
| ], | ||
| FsmonitorOverride::BuiltIn, | ||
| ), | ||
| ( | ||
| "valueless true", | ||
| vec![ | ||
| response(config_args(), Some(b"\0")), | ||
| response(typed_config_args(""), Some(b"true\0")), | ||
| response(capability_args(), Some(fsmonitor_capability())), | ||
| ], | ||
| FsmonitorOverride::BuiltIn, | ||
| ), | ||
| ( | ||
| "explicit empty false", | ||
| vec![ | ||
| response(config_args(), Some(b"\0")), | ||
| response(typed_config_args(""), Some(b"false\0")), | ||
| ], | ||
| FsmonitorOverride::Disabled, | ||
| ), | ||
| ]; | ||
|
|
||
| for (name, responses, expected) in cases { | ||
| let mut runner = FakeRunner { | ||
| responses: responses.into(), | ||
| }; | ||
|
|
||
| let actual = detect_fsmonitor_override(&mut runner).await; | ||
|
|
||
| assert_eq!( | ||
| (actual, runner.responses.len()), | ||
| (expected, 0), | ||
| "case: {name}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn response(args: Vec<&'static str>, output: Option<&[u8]>) -> ProbeResponse { | ||
| ProbeResponse { | ||
| args, | ||
| output: output.map(<[u8]>::to_vec), | ||
| } | ||
| } | ||
|
|
||
| fn config_args() -> Vec<&'static str> { | ||
| vec!["config", "--null", "--get", "core.fsmonitor"] | ||
| } | ||
|
|
||
| fn typed_config_args(value: &'static str) -> Vec<&'static str> { | ||
| vec![ | ||
| "config", | ||
| "--null", | ||
| "--type=bool", | ||
| "--fixed-value", | ||
| "--get", | ||
| "core.fsmonitor", | ||
| value, | ||
| ] | ||
| } | ||
|
|
||
| fn capability_args() -> Vec<&'static str> { | ||
| vec!["version", "--build-options"] | ||
| } | ||
|
|
||
| fn fsmonitor_capability() -> &'static [u8] { | ||
| b"feature: fsmonitor--daemon\n" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.