Repository: google/comprehensive-rust
File: xtask/src/main.rs (Rust)

Code before:
    // These special slides are not checked against the style guide
    let exclude_paths = [
        "exercise.html",
        "solution.html",
        "toc.html",
        "print.html",
        "404.html",
        "glossary.html",
        "index.html",
        "course-structure.html",
    ]
    .map(PathBuf::from);

    // Collect the files relevant for evaluation.
    // - CI environment variable is set: all modified markdown files in the src/
    //   directory
    // - all html files in the provided directory otherwise
    let candidate_slides: Vec<PathBuf> = if env::var("CI").is_ok() {
        println!("CI environment detected, checking only modified slides.");
        // GITHUB_BASE_REF is available in PRs. Default to 'main' for other CI
        // contexts.
        let base_ref = env::var("GITHUB_BASE_REF").unwrap_or("main".to_string());
        let mut cmd = Command::new("git");
        cmd.arg("diff")
            .arg("--name-only")
            .arg(format!("{}...", base_ref))
            .arg("--")
            // Retrieve all modified files in the src directory.
            // Pathspec syntax: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-pathspec
            // * can match path separators, thus matches also files in subdirectories
            .arg("src/*.md");
        println!("> {cmd:?}");
        let output = cmd.output().context("Failed to run git diff")?;
        String::from_utf8(output.stdout)?
            .lines()
            .map(|line| {
                let path = Path::new(line);
                // We know the path starts with "src/" because of the pathspec in the
                // git diff command. and we need it relative to the html base
                // directory
                let stripped_path = path.strip_prefix("src").unwrap();
                let mut html_path = stripped_path.to_path_buf();
                // replace the .md extension with .html
                html_path.set_extension("html");
                html_path
            })
            .collect()
    } else {
        println!("Local environment, checking all slides.");
        WalkDir::new(&html_directory)
            .into_iter()

Review comment (suggestion):
this is one of the things I'm not 100% sure if this would create issues. This git diff should provide the list of filenames (`--name-only`) that was changed by this PR (over all commits). But I'm not sure about GITHUB_BASE_REF in various situations. I might need to use a more reliable way to do this

Code after:
    // These special slides are not checked against the style guide
    let exclude_paths = [
        "exercise.html",
        "solution.html",
        "toc.html",
        "print.html",
        "404.html",
        "glossary.html",
        "index.html",
        "course-structure.html",
    ]
    .map(PathBuf::from);

    // Collect the files relevant for evaluation.
    // - CI environment variable is set: all modified markdown files in the src/
    //   directory
    // - all html files in the provided directory otherwise
    let candidate_slides: Vec<PathBuf> = if env::var("CI").is_ok() {
        println!("CI environment detected, checking only modified slides.");
        // GITHUB_BASE_REF is available in PRs. Default to 'main' for other CI
        // contexts.
        let base_ref = env::var("GITHUB_BASE_REF").unwrap_or("main".to_string());
        let mut cmd = Command::new("git");
        cmd.arg("diff")
            .arg("--name-only")
            .arg(format!("{}...", base_ref))
            .arg("--")
            // Retrieve all modified files in the src directory.
            // Pathspec syntax: https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-pathspec
            // `*` can match path separators, thus matches also files in
            // subdirectories
            .arg("src/*.md");
        println!("> {cmd:?}");
        let output = cmd.output().context("Failed to run git diff")?;
        String::from_utf8(output.stdout)?
            .lines()
            .map(|line| {
                let path = Path::new(line);
                // We know the path starts with "src/" because of the pathspec in the
                // `git diff` command, and we need it relative to the html base
                // directory
                let stripped_path = path.strip_prefix("src").unwrap();
                let mut html_path = stripped_path.to_path_buf();
                // replace the .md extension with .html
                html_path.set_extension("html");
                html_path
            })
            .collect()
    } else {
        println!("Local environment, checking all slides.");
        WalkDir::new(&html_directory)