From 0d69c51ff1c6e8ad3ddc4db16abd39adc0dd25a0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:36:31 +0200 Subject: [PATCH 1/5] Remove dependency on nu-ansi-term --- Cargo.lock | 76 ---------------------------------------------- Cargo.toml | 1 - src/diff_format.rs | 13 ++++---- 3 files changed, 6 insertions(+), 84 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d2dbd9..326545e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,7 +27,6 @@ dependencies = [ "glob-match", "gumdrop", "multimap", - "nu-ansi-term", "proc-macro2", "syn 2.0.39", "thiserror", @@ -208,15 +207,6 @@ dependencies = [ "serde", ] -[[package]] -name = "nu-ansi-term" -version = "0.49.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c073d3c1930d0751774acf49e66653acecb416c3a54c6ec095a9b11caddb5a68" -dependencies = [ - "windows-sys", -] - [[package]] name = "openssl-probe" version = "0.1.5" @@ -405,69 +395,3 @@ name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index c54545d..dc73cfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ git2 = "0.17" glob-match = "0.2.1" gumdrop = "0.8" multimap = "0.9" -nu-ansi-term = "0.49.0" proc-macro2 = { version = "1.0.66", features = ["span-locations"] } syn = { version = "2.0.28", features = ["full"] } thiserror = "1.0.44" diff --git a/src/diff_format.rs b/src/diff_format.rs index 0c40076..eec7e64 100644 --- a/src/diff_format.rs +++ b/src/diff_format.rs @@ -1,6 +1,5 @@ use std::str::FromStr; -use nu_ansi_term::Color; use thiserror::Error; use crate::cauterize::Change; @@ -11,7 +10,7 @@ const AFTER_CONTEXT: isize = 3; pub fn println(change: &Change, color_mode: ColorMode) { let text = format!("#\n#\tshowing diff for {:?}:\n#", change.file_name()); if color_mode.enabled() { - println!("{}", Color::DarkGray.paint(text)); + println!("\x1b[90m{text}\x1b[0m"); } else { println!("{text}") } @@ -47,17 +46,17 @@ pub fn println(change: &Change, color_mode: ColorMode) { for line in included { let (symbol, color, line) = match line { - DiffLine::Diff(diff::Result::Left(line)) => ('-', Color::LightRed, line), - DiffLine::Diff(diff::Result::Right(line)) => ('+', Color::LightGreen, line), + DiffLine::Diff(diff::Result::Left(line)) => ('-', "91", line), + DiffLine::Diff(diff::Result::Right(line)) => ('+', "92", line), DiffLine::Diff(diff::Result::Both(_, _)) => unreachable!(), - DiffLine::Context(line) => (' ', Color::Default, line), - DiffLine::Ellipsis => ('#', Color::DarkGray, "..."), + DiffLine::Context(line) => (' ', "39", line), + DiffLine::Ellipsis => ('#', "90", "..."), }; let format = format!("{symbol}\t{line}"); if color_mode.enabled() { - println!("{}", color.paint(format)); + println!("\x1b[{color}m{format}\x1b[0m"); } else { println!("{format}"); } From 8571c2943f4515a262171b40c89a38dab83e0c68 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:41:35 +0200 Subject: [PATCH 2/5] Remove usage of multimap in favor of HashMap<_, Vec<_>> This is what multimap effectively is under the hoods anyway. --- Cargo.lock | 10 ---------- Cargo.toml | 1 - src/cauterize.rs | 25 +++++++++++++++---------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 326545e..2844946 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,7 +26,6 @@ dependencies = [ "git2", "glob-match", "gumdrop", - "multimap", "proc-macro2", "syn 2.0.39", "thiserror", @@ -198,15 +197,6 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" -[[package]] -name = "multimap" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1a5d38b9b352dbd913288736af36af41c48d61b1a8cd34bcecd727561b7d511" -dependencies = [ - "serde", -] - [[package]] name = "openssl-probe" version = "0.1.5" diff --git a/Cargo.toml b/Cargo.toml index dc73cfc..fdfb7d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ diff = "0.1.13" git2 = "0.17" glob-match = "0.2.1" gumdrop = "0.8" -multimap = "0.9" proc-macro2 = { version = "1.0.66", features = ["span-locations"] } syn = { version = "2.0.28", features = ["full"] } thiserror = "1.0.44" diff --git a/src/cauterize.rs b/src/cauterize.rs index 436343c..9259498 100644 --- a/src/cauterize.rs +++ b/src/cauterize.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashMap, ops::Range, path::{Path, PathBuf}, }; @@ -132,7 +133,11 @@ fn diagnostics_to_ranges<'a>( } /// Handles (inline) module content -fn handle_mod_diagnostic(block: &syn::ItemMod, kind: &UnusedDiagnosticKind, ident: &str) -> Option { +fn handle_mod_diagnostic( + block: &syn::ItemMod, + kind: &UnusedDiagnosticKind, + ident: &str, +) -> Option { use syn::Item; use UnusedDiagnosticKind::*; @@ -224,15 +229,15 @@ fn process_files>( pub fn process_diagnostics( diagnostics: impl IntoIterator, ) -> impl Iterator { - process_files( - diagnostics - .into_iter() - .map(|diagnostic| { - let path = PathBuf::from(&diagnostic.span.file_name); - (path, diagnostic) - }) - .collect::>(), - ) + let mut diagnostics_by_path: HashMap> = HashMap::new(); + for diagnostic in diagnostics { + let path = PathBuf::from(&diagnostic.span.file_name); + diagnostics_by_path + .entry(path) + .or_default() + .push(diagnostic); + } + process_files(diagnostics_by_path) } /// Create a table of byte locations of newline symbols, From 1640c64722962a599b796d24169b2c389a3ef6aa Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:47:05 +0200 Subject: [PATCH 3/5] Remove unnecessary duplicate param of existing_vcs_repo The only caller passes the same value for both params. --- src/vcs/check_vcs.rs | 8 ++++---- src/vcs/mod.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/vcs/check_vcs.rs b/src/vcs/check_vcs.rs index bb24960..0bf8371 100644 --- a/src/vcs/check_vcs.rs +++ b/src/vcs/check_vcs.rs @@ -8,8 +8,8 @@ use std::path::Path; // 1. We are in a git repo and the path to the new package is not an ignored // path in that repo. // 2. We are in an HG repo. -pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool { - in_git_repo(path) || hgrepo_discover(path, cwd).is_ok() +pub fn existing_vcs_repo(path: &Path) -> bool { + in_git_repo(path) || hgrepo_discover(path).is_ok() } fn in_git_repo(path: &Path) -> bool { @@ -25,9 +25,9 @@ fn in_git_repo(path: &Path) -> bool { } } -fn hgrepo_discover(path: &Path, cwd: &Path) -> std::io::Result<()> { +fn hgrepo_discover(path: &Path) -> std::io::Result<()> { std::process::Command::new("hg") - .current_dir(cwd) + .current_dir(path) .arg("--cwd") .arg(path) .arg("root") diff --git a/src/vcs/mod.rs b/src/vcs/mod.rs index d2fae21..76b5103 100644 --- a/src/vcs/mod.rs +++ b/src/vcs/mod.rs @@ -20,7 +20,7 @@ pub enum Status { // Copyright (c) 2016-2021 The Cargo Developers fn check_version_control(path: &Path) -> Status { - if !check_vcs::existing_vcs_repo(path, path) { + if !check_vcs::existing_vcs_repo(path) { return Status::NoVCS; } From 1ed1060c0dc9f2b941c9f481cc4c412a3211c14b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:58:21 +0200 Subject: [PATCH 4/5] Use git check-ignore for the in_git_repo check --- src/vcs/check_vcs.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/vcs/check_vcs.rs b/src/vcs/check_vcs.rs index 0bf8371..c369142 100644 --- a/src/vcs/check_vcs.rs +++ b/src/vcs/check_vcs.rs @@ -13,16 +13,15 @@ pub fn existing_vcs_repo(path: &Path) -> bool { } fn in_git_repo(path: &Path) -> bool { - if let Ok(repo) = git2::Repository::discover(path) { - // Don't check if the working directory itself is ignored. - if repo.workdir().map_or(false, |workdir| workdir == path) { - true - } else { - !repo.is_path_ignored(path).unwrap_or(false) - } - } else { - false - } + std::process::Command::new("git") + .current_dir(path) + .arg("check-ignore") + .arg("-q") + .arg(".") + .status() + .map_or(false, |status| { + status.code() == Some(1) // status is 0 when ignored, 1 when not ignored and 128 when not a git repo + }) } fn hgrepo_discover(path: &Path) -> std::io::Result<()> { From 489a300a31b63862d0e69b7d97cc9b9f75c08d39 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 11 Oct 2024 17:38:23 +0200 Subject: [PATCH 5/5] Use git cli instead of git2 This significantly reduces the amount of dependencies and most users of cargo-minify likely have git installed anyway. --- Cargo.lock | 189 ------------------------------------------------- Cargo.toml | 1 - src/vcs/mod.rs | 61 +++++++++------- 3 files changed, 35 insertions(+), 216 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2844946..151f9a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "camino" version = "1.1.6" @@ -23,7 +17,6 @@ version = "0.5.0" dependencies = [ "cargo_metadata", "diff", - "git2", "glob-match", "gumdrop", "proc-macro2", @@ -54,46 +47,12 @@ dependencies = [ "thiserror", ] -[[package]] -name = "cc" -version = "1.0.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" -dependencies = [ - "jobserver", - "libc", -] - [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" -dependencies = [ - "percent-encoding", -] - -[[package]] -name = "git2" -version = "0.17.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b989d6a7ca95a362cf2cfc5ad688b3a467be1f87e480b8dad07fee8c79b0044" -dependencies = [ - "bitflags", - "libc", - "libgit2-sys", - "log", - "openssl-probe", - "openssl-sys", - "url", -] - [[package]] name = "glob-match" version = "0.2.1" @@ -120,113 +79,12 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "idna" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "itoa" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" -[[package]] -name = "jobserver" -version = "0.1.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" -dependencies = [ - "libc", -] - -[[package]] -name = "libc" -version = "0.2.150" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" - -[[package]] -name = "libgit2-sys" -version = "0.15.2+1.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a80df2e11fb4a61f4ba2ab42dbe7f74468da143f1a75c74e11dee7c813f694fa" -dependencies = [ - "cc", - "libc", - "libssh2-sys", - "libz-sys", - "openssl-sys", - "pkg-config", -] - -[[package]] -name = "libssh2-sys" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" -dependencies = [ - "cc", - "libc", - "libz-sys", - "openssl-sys", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "libz-sys" -version = "1.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "log" -version = "0.4.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" - -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.96" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3812c071ba60da8b5677cc12bcb1d42989a65553772897a7e0355545a819838f" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - -[[package]] -name = "pkg-config" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" - [[package]] name = "proc-macro2" version = "1.0.70" @@ -333,55 +191,8 @@ dependencies = [ "syn 2.0.39", ] -[[package]] -name = "tinyvec" -version = "1.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - -[[package]] -name = "unicode-bidi" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" - [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" - -[[package]] -name = "unicode-normalization" -version = "0.1.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "url" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/Cargo.toml b/Cargo.toml index fdfb7d8..61cd71b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ name = "useless" [dependencies] cargo_metadata = "0.17" diff = "0.1.13" -git2 = "0.17" glob-match = "0.2.1" gumdrop = "0.8" proc-macro2 = { version = "1.0.66", features = ["span-locations"] } diff --git a/src/vcs/mod.rs b/src/vcs/mod.rs index 76b5103..e2c33dc 100644 --- a/src/vcs/mod.rs +++ b/src/vcs/mod.rs @@ -1,3 +1,4 @@ +use std::io; use std::path::Path; mod check_vcs; @@ -13,39 +14,47 @@ pub enum Status { staged: Vec, }, NoVCS, - Error(git2::Error), + Error(io::Error), } -// Portions of the below code are inspired by/taken from Cargo, https://github.com/rust-lang/cargo/ -// Copyright (c) 2016-2021 The Cargo Developers - fn check_version_control(path: &Path) -> Status { if !check_vcs::existing_vcs_repo(path) { return Status::NoVCS; } - let mut dirty = Vec::new(); - let mut staged = Vec::new(); - if let Ok(repo) = git2::Repository::discover(path) { - let mut repo_opts = git2::StatusOptions::new(); - repo_opts.include_ignored(false); - repo_opts.include_untracked(true); - let statuses = match repo.statuses(Some(&mut repo_opts)) { - Ok(value) => value, - Err(error) => return Status::Error(error), - }; - for status in statuses.iter() { - if let Some(path) = status.path() { - match status.status() { - git2::Status::CURRENT => (), - git2::Status::INDEX_NEW - | git2::Status::INDEX_MODIFIED - | git2::Status::INDEX_DELETED - | git2::Status::INDEX_RENAMED - | git2::Status::INDEX_TYPECHANGE => staged.push(path.to_string()), - _ => dirty.push(path.to_string()), - }; - } + let output = match std::process::Command::new("git") + .current_dir(path) + .arg("status") + .arg("--porcelain=v1") + .output() + { + Ok(output) if output.status.success() => output, + Ok(output) => { + return Status::Error(io::Error::new( + io::ErrorKind::Other, + format!( + "git status failed with exit code {}:\nstdout:\n{}\n\nstderr:\n{}", + output.status, + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr), + ), + )) + } + Err(err) => return Status::Error(err), + }; + let stdout = output.stdout; + let mut dirty = vec![]; + let mut staged = vec![]; + for line in String::from_utf8_lossy(&stdout).lines() { + if line.starts_with("M ") || line.starts_with("A ") { + staged.push(line[2..].to_owned()); + } else if line.starts_with(" M") || line.starts_with("MM") || line.starts_with("??") { + dirty.push(line[2..].to_owned()); + } else { + return Status::Error(io::Error::new( + io::ErrorKind::Other, + format!("git status returned invalid data: {line:?}"), + )); } }