Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions driver/build/extra_symbols.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use anyhow::{Context, Result, anyhow, bail};
use anyhow::{Context, Result, anyhow};
use dylint_internal::{
clippy_utils::{Rev, Revs, clippy_repository, parse_as_nightly},
clippy_utils::{Rev, Revs, clippy_repository, file_at_revision, parse_as_nightly},
env,
rustup::parse_toolchain,
};
use git2::Oid;
use proc_macro2::TokenTree;
use std::{
fs::{OpenOptions, read_to_string, write},
fs::{OpenOptions, write},
io::Write,
path::PathBuf,
process::Command,
path::{Path, PathBuf},
};
use syn::{Item, ItemMacro, Macro, parse_file};

Expand Down Expand Up @@ -87,22 +86,7 @@ fn rev_from_channel(channel: &str) -> Result<Rev> {

fn clippy_utils_sym_rs(rev: Oid) -> Result<String> {
let repository = clippy_repository(false)?;
let Some(workdir) = repository.workdir() else {
bail!("Repository has no working directory");
};

let mut command = Command::new("git");
command
.current_dir(workdir)
.args(["checkout", &rev.to_string()]);
let status = command
.status()
.with_context(|| format!("command failed: {command:?}"))?;
assert!(status.success());

let sym_path = workdir.join("clippy_utils/src/sym.rs");
read_to_string(&sym_path)
.with_context(|| format!("`read_to_string` failed for `{}`", sym_path.display()))
file_at_revision(&repository, rev, Path::new("clippy_utils/src/sym.rs"))
}

fn quoted_symbols(sym_rs: &str) -> Result<Vec<String>> {
Expand Down
14 changes: 11 additions & 3 deletions internal/src/clippy_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use toml_edit::{DocumentMut, Item, Value};

mod repository;
pub use repository::{clippy_repository, parse_as_nightly};
pub use repository::{clippy_repository, file_at_revision, parse_as_nightly};

mod revs_no_preinstall;
pub use revs_no_preinstall::{Rev, Revs};
Expand All @@ -28,7 +28,11 @@ pub fn clippy_utils_package_version(path: &Path) -> Result<Version> {
cargo_toml.to_string_lossy(),
)
})?;
let table = toml::from_str::<toml::Table>(&contents)?;
package_version_from_contents(&contents)
}

fn package_version_from_contents(contents: &str) -> Result<Version> {
let table = toml::from_str::<toml::Table>(contents)?;
let version = table
.get("package")
.and_then(toml::Value::as_table)
Expand Down Expand Up @@ -76,7 +80,11 @@ pub fn toolchain_channel(path: &Path) -> Result<String> {
let Some((_, contents)) = read_rust_toolchain_file(path)? else {
bail!("Could not find rust-toolchain file at `{}`", path.display());
};
let table = toml::from_str::<toml::Table>(&contents)?;
toolchain_channel_from_contents(&contents)
}

fn toolchain_channel_from_contents(contents: &str) -> Result<String> {
let table = toml::from_str::<toml::Table>(contents)?;
table
.get("toolchain")
.and_then(toml::Value::as_table)
Expand Down
19 changes: 17 additions & 2 deletions internal/src/clippy_utils/repository.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{clone, git2::Repository};
use crate::{
clone,
git2::{Oid, Repository},
};
use anyhow::{Context, Result};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, path::Path, rc::Rc};
use tempfile::{TempDir, tempdir};

const RUST_CLIPPY_URL: &str = "https://github.com/rust-lang/rust-clippy";
Expand All @@ -26,6 +29,18 @@ pub fn clippy_repository(quiet: bool) -> Result<Rc<Repository>> {
})
}

/// Reads a UTF-8 file from a commit without changing HEAD or the working tree.
pub fn file_at_revision(repository: &Repository, rev: Oid, path: &Path) -> Result<String> {
(|| -> Result<String> {
let commit = repository.find_commit(rev)?;
let tree = commit.tree()?;
let entry = tree.get_path(path)?;
let blob = repository.find_blob(entry.id())?;
Ok(std::str::from_utf8(blob.content())?.to_owned())
})()
.with_context(|| format!("Could not read `{}` at revision `{rev}`", path.display()))
}

pub fn parse_as_nightly(channel: &str) -> Option<[u32; 3]> {
channel.strip_prefix("nightly-").and_then(parse_date)
}
Expand Down
Loading
Loading