Skip to content

Commit 3ab708c

Browse files
committed
implemented toolchain_channel again
1 parent 0ff37ab commit 3ab708c

File tree

1 file changed

+32
-38
lines changed

1 file changed

+32
-38
lines changed

dylint/src/package_options/revs.rs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use super::common::clippy_repository;
22
use anyhow::{Context, Result, anyhow};
3-
use dylint_internal::git2::{Commit, Oid, Repository, Sort, Tree};
3+
use dylint_internal::{
4+
clippy_utils::toolchain_channel,
5+
git2::{Commit, Oid, Repository, Sort},
6+
};
47
use if_chain::if_chain;
58
use semver::Version;
69
use std::{path::Path, rc::Rc, time::Instant};
@@ -19,39 +22,6 @@ pub struct Revs {
1922
quiet: bool,
2023
}
2124

22-
fn toolchain_channel(repo: &Repository, tree: &Tree) -> Result<Option<String>> {
23-
// Try both filenames
24-
let entry = match tree
25-
.get_path(Path::new("rust-toolchain"))
26-
.or_else(|_| tree.get_path(Path::new("rust-toolchain.toml")))
27-
{
28-
Ok(entry) => entry,
29-
Err(_) => return Ok(None),
30-
};
31-
32-
// Load its blob
33-
let blob = repo
34-
.find_blob(entry.id())
35-
.context("failed to load rust-toolchain blob")?;
36-
37-
// Read as UTF-8 and trim
38-
let raw = std::str::from_utf8(blob.content())
39-
.context("rust-toolchain is not valid UTF-8")?
40-
.trim();
41-
42-
// If it's valid TOML, extract [toolchain].channel; otherwise use raw
43-
let channel = toml::from_str::<Value>(raw)
44-
.ok()
45-
.and_then(|doc| {
46-
doc.get("toolchain")
47-
.and_then(|t| t.get("channel"))
48-
.and_then(|c| c.as_str().map(ToString::to_string))
49-
})
50-
.unwrap_or_else(|| raw.to_string());
51-
52-
Ok(Some(channel))
53-
}
54-
5525
impl Revs {
5626
pub fn new(quiet: bool) -> Result<Self> {
5727
let start = Instant::now();
@@ -88,10 +58,34 @@ impl Revs {
8858
return Ok(None);
8959
};
9060

91-
// Try to get channel from rust-toolchain
92-
let channel = match toolchain_channel(&self.repo, &tree)? {
93-
Some(chan) => chan,
94-
None => return Ok(None),
61+
// Create a temporary directory to extract files for toolchain detection
62+
let temp_dir = tempfile::tempdir().context("Failed to create temporary directory")?;
63+
let temp_path = temp_dir.path();
64+
65+
// Extract rust-toolchain files to the temporary directory
66+
let mut found_toolchain_file = false;
67+
for filename in &["rust-toolchain", "rust-toolchain.toml"] {
68+
if_chain! {
69+
if let Ok(entry) = tree.get_path(Path::new(filename));
70+
if let Ok(blob) = self.repo.find_blob(entry.id());
71+
if let Ok(content) = std::str::from_utf8(blob.content());
72+
then {
73+
let file_path = temp_path.join(filename);
74+
std::fs::write(&file_path, content)
75+
.with_context(|| format!("Failed to write {} to temp dir", filename))?;
76+
found_toolchain_file = true;
77+
}
78+
}
79+
}
80+
81+
// If no toolchain files were found, return None
82+
if !found_toolchain_file {
83+
return Ok(None);
84+
}
85+
86+
let channel = match toolchain_channel(temp_path) {
87+
Ok(chan) => chan,
88+
Err(_) => return Ok(None),
9589
};
9690

9791
Ok(Some(Rev {

0 commit comments

Comments
 (0)