Skip to content

Commit

Permalink
Remove git2 to avoid openssl which could fail the published app.
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed May 7, 2024
1 parent 32f0ec9 commit 03dd135
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion devclean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ color-eyre = "0.6.3"
colored = "2.1.0"
dialoguer = "0.11.0"
fs_extra = "1.3.0"
git2 = "0.18.3"
# git2 = "0.18.3"
human_bytes = "0.4.3"
humantime = "2.1.0"
indicatif = "0.17.8"
Expand Down
32 changes: 28 additions & 4 deletions devclean/src/predicates/general.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
use color_eyre::eyre::Result;
use git2::Repository;
// use git2::Repository;
use std::path::Path;

// Git
pub fn is_git_repo(path: &Path) -> bool {
path.join(".git").is_dir()
}

/// Check if the git repo has any uncommitted changes with "git status --porcelain"
pub fn is_git_repo_clean(path: &Path) -> Result<bool> {
let repo = Repository::open(path)?;
let statuses = repo.statuses(None)?;
Ok(statuses.is_empty())
let output = std::process::Command::new("git")
.current_dir(path)
.args(&["status", "--porcelain"])
.output()
.expect("failed to execute process");
let output_str =
std::str::from_utf8(&output.stdout).expect("Failed to convert bytes to string");
let line_count = output_str
.split("\n")
.filter(|x| !x.trim().is_empty())
.count();
Ok(line_count == 0)
}

pub fn is_dir_empty(path: &Path) -> bool {
path.read_dir().map_or(true, |mut dir| dir.next().is_none())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_git_repo_clean() {
let x = is_git_repo_clean(Path::new("/Users/hacker/Dev/projects/tauri-demo"));
// let x = is_git_repo_clean(Path::new("/Users/hacker/Dev/projects/Nowtu"));
println!("{:?}", x);
// assert!(is_git_repo_clean(Path::new("tests/fixtures/git")));
// assert!(!is_git_repo_clean(Path::new("tests/fixtures/git_dirty")));
}
}
35 changes: 26 additions & 9 deletions devclean/src/predicates/languages/git.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use super::LanguagePredicate;
use crate::predicates::Reportable;
use crate::predicates::{
general::{is_git_repo, is_git_repo_clean},
Reportable,
};
use color_eyre::eyre::Result;
use git2::{Repository, StatusOptions};
// use git2::{Repository, StatusOptions};
use std::path::Path;

pub struct GitDirtyRepoPredicate;
impl GitDirtyRepoPredicate {
fn is_git_repo(&self, path: &Path) -> bool {
Repository::open(path).is_ok()
// Repository::open(path).is_ok()
is_git_repo(path)
}

/// no files to be commited
fn is_git_repo_clean(&self, path: &Path) -> Result<bool> {
let repo = Repository::open(path)?;
let mut status_opts = StatusOptions::new();
status_opts.include_untracked(true);
let statuses = repo.statuses(Some(&mut status_opts))?;
Ok(statuses.is_empty())
is_git_repo_clean(path)
// let repo = Repository::open(path)?;
// let mut status_opts = StatusOptions::new();
// status_opts.include_untracked(true);
// let statuses = repo.statuses(Some(&mut status_opts))?;
// Ok(statuses.is_empty())
}
}

Expand All @@ -29,6 +34,18 @@ impl Reportable for GitDirtyRepoPredicate {

impl LanguagePredicate for GitDirtyRepoPredicate {
fn is_in_project(&self, path: &std::path::Path) -> bool {
Repository::open(path).is_ok()
// iterate over the parent directories to find the .git directory
let mut current_path = path.to_path_buf();
loop {
if current_path.join(".git").is_dir() {
return true;
}
if !current_path.pop() {
break;
}
}
false

// Repository::open(path).is_ok()
}
}

0 comments on commit 03dd135

Please sign in to comment.