Skip to content

Commit

Permalink
abort when repository is not clean
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Jun 24, 2023
1 parent 148df2e commit aab343b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use chrono::{DateTime, TimeZone, Utc};
use git2::{Commit, Repository};
use git2::{Commit, Repository, RepositoryState};
use serde::Serialize;
use std::collections::HashMap;
use std::path::PathBuf;
Expand All @@ -19,13 +19,18 @@ pub struct GitProject {
impl GitProject {
pub fn open(working_dir: &PathBuf) -> Result<Self> {
let repository = Repository::open(working_dir)?;
println!("state {:?}", repository.state());

Ok(GitProject {
repository,
working_dir: working_dir.clone(),
})
}

pub fn is_clean(&self) -> bool {
self.repository.state() == RepositoryState::Clean
}

/// Get the remote url
pub fn remote_url(&self) -> Result<String> {
let remote = &self.repository.find_remote("origin")?;
Expand Down Expand Up @@ -94,13 +99,13 @@ impl GitProject {
/// Checkout a branch (main), or a tag (v0.1.1) or a commit (8e8128)
pub fn checkout(&self, ref_name: &String) -> Result<()> {
log::info!("Checking out: {}", ref_name);
if ref_name.to_owned() == self.branch()? {
if *ref_name == self.branch()? {
log::info!("Branch {} already checked out", ref_name);
return Ok(());
}

let repo = &self.repository;
let (object, reference) = repo.revparse_ext(&ref_name).expect("Object not found");
let (object, reference) = repo.revparse_ext(ref_name).expect("Object not found");

repo.checkout_tree(&object, None)
.expect("Failed to checkout");
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub async fn run(config: &Config) -> Result<()> {
}

let project = GitProject::open(&config.working_dir)?;
if !project.is_clean() {
panic!("Repository is not clean");
}

log::info!("Current branch: {}", project.branch()?);

let conn = db::create_connection(&config.output_dir)?;
Expand Down

0 comments on commit aab343b

Please sign in to comment.