diff --git a/Cargo.lock b/Cargo.lock index adc9d44e..826679e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -899,9 +899,9 @@ dependencies = [ [[package]] name = "git-bot-feedback" -version = "0.12.3" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b59004ceb4f7e055f4e05681a181c6c9c58b393633ed5665c90f905b9cd2fde" +checksum = "1173b3c651a9669c34fbbb9e16c21ba82a0bc29466751008745046995962e885" dependencies = [ "async-trait", "chrono", @@ -2504,7 +2504,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix", "windows-sys 0.61.2", diff --git a/cpp-linter/Cargo.toml b/cpp-linter/Cargo.toml index e4d0a81c..f9cbb0cc 100644 --- a/cpp-linter/Cargo.toml +++ b/cpp-linter/Cargo.toml @@ -35,7 +35,7 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } [dependencies.git-bot-feedback] -version = "0.12.3" +version = "0.12.4" features = ["file-changes"] # path = "../../git-bot-feedback" # git = "https://github.com/2bndy5/git-bot-feedback" diff --git a/cpp-linter/src/rest_client/feedback.rs b/cpp-linter/src/rest_client/feedback.rs index 02726fc7..2ebebb2d 100644 --- a/cpp-linter/src/rest_client/feedback.rs +++ b/cpp-linter/src/rest_client/feedback.rs @@ -52,7 +52,17 @@ impl RestClient { for annotation in &mut annotations { annotation.path = prefix_feedback_path(&annotation.path, &self.repo_root_prefix); } - self.client.write_file_annotations(&annotations)?; + if self.client.client_kind() == "local" && feedback_inputs.report_to_stdout() { + // The local client prints annotations to stdout, + // which must hold only the report. + let mut err = stderr().lock(); + for annotation in &annotations { + writeln!(err, "{}", annotation.fmt_local()) + .map_err(ClientError::StdoutWriteFailed)?; + } + } else { + self.client.write_file_annotations(&annotations)?; + } if self.client.client_kind() == "github" && let Some(msg) = annotation_cap_message(&annotations, &feedback_inputs) { diff --git a/cpp-linter/src/rest_client/mod.rs b/cpp-linter/src/rest_client/mod.rs index 7968165e..9a18ad1f 100644 --- a/cpp-linter/src/rest_client/mod.rs +++ b/cpp-linter/src/rest_client/mod.rs @@ -91,9 +91,15 @@ impl RestClient { ignore_index: bool, ) -> Result, ClientError> { let mut changed_files_filter = file_filter.clone(); - changed_files_filter.ignored.clear(); - changed_files_filter.not_ignored.clear(); - changed_files_filter.not_ignored.insert(String::new()); + if !self.repo_root_prefix.as_os_str().is_empty() { + // The patterns are relative to `--repo-root`, but the client lists files + // relative to the git root, so they are applied after the prefix is removed. + // Without a prefix, the client applies them itself and doesn't read + // ignored untracked files at all. + changed_files_filter.ignored.clear(); + changed_files_filter.not_ignored.clear(); + changed_files_filter.not_ignored.insert(String::new()); + } let files = self .client .get_list_of_changed_files( diff --git a/cpp-linter/src/rest_client/test.rs b/cpp-linter/src/rest_client/test.rs index 6d39ec8c..47863898 100644 --- a/cpp-linter/src/rest_client/test.rs +++ b/cpp-linter/src/rest_client/test.rs @@ -897,3 +897,70 @@ fn annotations_over_step_limit() { "{msg}" ); } + +// ************************* tests for listing changed files + +/// An ignored untracked file is not read, so one that can't be read doesn't stop the run. +#[cfg(unix)] +#[tokio::test] +async fn ignored_untracked_file_is_not_read() { + use std::{fs, os::unix::fs::PermissionsExt, process::Command}; + + let tmp = tempdir().unwrap(); + let root = tmp.path().canonicalize().unwrap(); + let git = |args: &[&str]| { + let status = Command::new("git") + .current_dir(&root) + .args(args) + .status() + .unwrap(); + assert!(status.success(), "git {args:?}"); + }; + git(&["init", "-q"]); + git(&[ + "-c", + "user.name=test", + "-c", + "user.email=test@example.com", + "commit", + "-q", + "--allow-empty", + "-m", + "init", + ]); + fs::create_dir_all(root.join("src")).unwrap(); + fs::write(root.join("src/ok.cpp"), "int main() {}\n").unwrap(); + fs::create_dir_all(root.join("vendor")).unwrap(); + let unreadable = root.join("vendor/broken.cpp"); + fs::write(&unreadable, "int x;\n").unwrap(); + fs::set_permissions(&unreadable, fs::Permissions::from_mode(0o000)).unwrap(); + + // a local run, not a CI run + unsafe { + for var in [ + "GITHUB_ACTIONS", + "GITEA_ACTIONS", + "FORGEJO_ACTIONS", + "GITLAB_CI", + ] { + env::remove_var(var); + } + } + let cur_dir = env::current_dir().unwrap(); + env::set_current_dir(&root).unwrap(); + let client = RestClient::new(&root).unwrap(); + assert_eq!(client.client_kind(), "local"); + let file_filter = FileFilter::new(&["vendor"], &["cpp"], None); + let files = client + .get_list_of_changed_files( + &file_filter, + &git_bot_feedback::LinesChangedOnly::Off, + &None, + false, + ) + .await; + env::set_current_dir(cur_dir).unwrap(); + + let names: Vec = files.unwrap().into_iter().map(|f| f.name).collect(); + assert_eq!(names, vec![PathBuf::from("src/ok.cpp")]); +}