Skip to content

Commit

Permalink
Fix error conversion and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Nov 4, 2023
1 parent e71d2db commit 49d3801
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 100 deletions.
91 changes: 91 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { version = "1.0.75", features = ["backtrace"] }
cargo-util = "0.2.6"
git2 = "0.18.1"
thiserror = "1.0.50"
72 changes: 7 additions & 65 deletions src/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::VCSResult;
use cargo_util::paths;
use cargo_util::ProcessBuilder;
use std::path::Path;

Expand Down Expand Up @@ -27,13 +26,13 @@ pub fn check_version_control<P: AsRef<Path>>(path: P, opts: &CheckOptions) -> VC
if opts.allow_no_vcs {
return Ok(());
}
// if !existing_vcs_repo(path, path) {
// bail!(
// "no VCS found for this package and `cargo fix` can potentially \
// perform destructive changes; if you'd like to suppress this \
// error pass `--allow-no-vcs`"
// )
// }
if !existing_vcs_repo(path.as_ref(), path.as_ref()) {
// bail!(
// "no VCS found for this package and `cargo fix` can potentially \
// perform destructive changes; if you'd like to suppress this \
// error pass `--allow-no-vcs`"
// )
}

// if opts.allow_dirty && opts.allow_staged {
// return Ok(());
Expand Down Expand Up @@ -121,29 +120,14 @@ pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {

pub struct HgRepo;
pub struct GitRepo;
pub struct PijulRepo;
pub struct FossilRepo;

impl GitRepo {
pub fn init(path: &Path, _: &Path) -> VCSResult<GitRepo> {
git2::Repository::init(path)?;
Ok(GitRepo)
}
pub fn discover(path: &Path, _: &Path) -> Result<git2::Repository, git2::Error> {
git2::Repository::discover(path)
}
}

impl HgRepo {
pub fn init(path: &Path, cwd: &Path) -> VCSResult<HgRepo> {
ProcessBuilder::new("hg")
.cwd(cwd)
.arg("init")
.arg("--")
.arg(path)
.exec()?;
Ok(HgRepo)
}
pub fn discover(path: &Path, cwd: &Path) -> VCSResult<HgRepo> {
ProcessBuilder::new("hg")
.cwd(cwd)
Expand All @@ -154,45 +138,3 @@ impl HgRepo {
Ok(HgRepo)
}
}

impl PijulRepo {
pub fn init(path: &Path, cwd: &Path) -> VCSResult<PijulRepo> {
ProcessBuilder::new("pijul")
.cwd(cwd)
.arg("init")
.arg("--")
.arg(path)
.exec()?;
Ok(PijulRepo)
}
}

impl FossilRepo {
pub fn init(path: &Path, cwd: &Path) -> VCSResult<FossilRepo> {
// fossil doesn't create the directory so we'll do that first
paths::create_dir_all(path)?;

// set up the paths we'll use
let db_fname = ".fossil";
let mut db_path = path.to_owned();
db_path.push(db_fname);

// then create the fossil DB in that location
ProcessBuilder::new("fossil")
.cwd(cwd)
.arg("init")
.arg("--")
.arg(&db_path)
.exec()?;

// open it in that new directory
ProcessBuilder::new("fossil")
.cwd(&path)
.arg("open")
.arg("--")
.arg(db_fname)
.exec()?;

Ok(FossilRepo)
}
}
60 changes: 26 additions & 34 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
use std::{error::Error, fmt::Display};
use thiserror::Error;

/// Enumerates the possible error values that can be generated by this crate
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum VSCError {
/// Unable to detect a version control system
#[error("no vcs")]
NoVCS,
/// Found a type of file that was not allowed. Includes listing of disallowed files found
#[error("{}", format_disallowed_files(dirty_files, staged_files))]
NotAllowedFilesFound {
/// Dirty files found (note this will be empty if dirty files are allowed)
dirty_files: Vec<String>,
/// Staged files found (note this will be empty if staged files are allowed)
staged_files: Vec<String>,
},
/// Errors produced outside of the ones from this library
#[error(transparent)]
Other(#[from] anyhow::Error), // source and Display delegate to anyhow::Error
}

impl Display for VSCError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VSCError::NoVCS => write!(f, "no VCS found but this not allowed"),
VSCError::NotAllowedFilesFound {
dirty_files,
staged_files,
} => {
let mut files_list = String::new();
for file in dirty_files {
files_list.push_str(" * ");
files_list.push_str(file);
files_list.push_str(" (dirty)\n");
}
for file in staged_files {
files_list.push_str(" * ");
files_list.push_str(file);
files_list.push_str(" (staged)\n");
}

write!(
f,
"disallowed files found:\n\
\n\
{}\n\
",
files_list
)
}
}
fn format_disallowed_files(dirty_files: &[String], staged_files: &[String]) -> String {
let mut files_list = String::new();
for file in dirty_files {
files_list.push_str(" * ");
files_list.push_str(file);
files_list.push_str(" (dirty)\n");
}
for file in staged_files {
files_list.push_str(" * ");
files_list.push_str(file);
files_list.push_str(" (staged)\n");
}
}

impl Error for VSCError {}
format!(
"disallowed files found:\n\
\n\
{}\n\
",
files_list
)
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
mod check;
mod error;

pub type VCSResult<T> = Result<T, error::VSCError>;
/// Type alias for Result<T, [`VSCError`]>
pub type VCSResult<T> = Result<T, VSCError>;
pub use check::{check_version_control, CheckOptions};
pub use error::VSCError;

0 comments on commit 49d3801

Please sign in to comment.