Skip to content

Commit

Permalink
git: use discover instead of find_repository
Browse files Browse the repository at this point in the history
Alwats better to use underlying lib
  • Loading branch information
Pilou97 committed Feb 23, 2024
1 parent 2be9918 commit 698ae25
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 deletions src/git/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::config::GitConfig;
use auth_git2::GitAuthenticator;
use git2::{Branch, BranchType, Oid, Repository, Signature};
use serde::{de::DeserializeOwned, Serialize};
use std::{path::Path, process::Command};
use std::process::Command;

pub struct Git {
repository: Repository,
Expand All @@ -20,25 +20,12 @@ pub struct EnhancedCommit<N> {
}

impl Git {
/// Try to find a repository in the given path
/// Otherwise, it tries to open the parent directory
fn find_repository(path: &Path) -> Repository {
let repository = Repository::open(path);
match repository {
Ok(repository) => repository,
Err(_) => {
let path = path.parent().expect("repository not found");
Self::find_repository(path)
}
}
}

/// Open a repository at the given path
/// Also load the signature from the .gitconfig
pub fn open(path: &str) -> Self {
let current_dir = std::env::current_dir().expect("cannot open current directory");
let path = current_dir.join(path);
let repository = Self::find_repository(path.as_path());
let repository = Repository::discover(path).expect("repository not found");
let gitconfig = GitConfig::open().expect("git config not found");

let signature = Signature::now(&gitconfig.user.name, &gitconfig.user.email)
Expand Down

0 comments on commit 698ae25

Please sign in to comment.