Skip to content

Commit

Permalink
Use git cli instead of git2
Browse files Browse the repository at this point in the history
This significantly reduces the amount of dependencies and most users of
cargo-minify likely have git installed anyway.
  • Loading branch information
bjorn3 committed Oct 11, 2024
1 parent 1ed1060 commit b4686f6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 216 deletions.
189 changes: 0 additions & 189 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
61 changes: 35 additions & 26 deletions src/vcs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io;
use std::path::Path;

mod check_vcs;
Expand All @@ -13,39 +14,47 @@ pub enum Status {
staged: Vec<String>,
},
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("??") {
dirty.push(line[2..].to_owned());
} else {
return Status::Error(io::Error::new(
io::ErrorKind::Other,
format!("git status returned invalid data: {line:?}"),
));
}
}

Expand Down

0 comments on commit b4686f6

Please sign in to comment.