mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-05-03 05:54:15 +02:00
Move a bunch of git ops to use the git binary (#302)
This commit is contained in:
@@ -1,29 +1,9 @@
|
||||
use crate::error::Error::{GenericError, NoDefaultRemoteFound};
|
||||
use crate::error::Result;
|
||||
use git2::{Branch, BranchType, Repository};
|
||||
use std::env;
|
||||
use std::path::{Path, PathBuf};
|
||||
use git2::{Branch, BranchType, Remote, Repository};
|
||||
|
||||
const DEFAULT_REMOTE_NAME: &str = "origin";
|
||||
|
||||
pub(crate) fn find_ssh_key() -> Option<PathBuf> {
|
||||
let home_dir = env::var("HOME").ok()?;
|
||||
let key_paths = [
|
||||
format!("{}/.ssh/id_ed25519", home_dir),
|
||||
format!("{}/.ssh/id_rsa", home_dir),
|
||||
format!("{}/.ssh/id_ecdsa", home_dir),
|
||||
format!("{}/.ssh/id_dsa", home_dir),
|
||||
];
|
||||
|
||||
for key_path in key_paths.iter() {
|
||||
let path = Path::new(key_path);
|
||||
if path.exists() {
|
||||
return Some(path.to_path_buf());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn get_current_branch(repo: &Repository) -> Result<Option<Branch<'_>>> {
|
||||
for b in repo.branches(None)? {
|
||||
let branch = b?.0;
|
||||
@@ -34,10 +14,18 @@ pub(crate) fn get_current_branch(repo: &Repository) -> Result<Option<Branch<'_>>
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub(crate) fn get_current_branch_name(repo: &Repository) -> Result<String> {
|
||||
Ok(get_current_branch(&repo)?
|
||||
.ok_or(GenericError("Failed to get current branch".to_string()))?
|
||||
.name()?
|
||||
.ok_or(GenericError("Failed to get current branch name".to_string()))?
|
||||
.to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn local_branch_names(repo: &Repository) -> Result<Vec<String>> {
|
||||
let mut branches = Vec::new();
|
||||
for branch in repo.branches(Some(BranchType::Local))? {
|
||||
let branch = branch?.0;
|
||||
let (branch, _) = branch?;
|
||||
let name = branch.name_bytes()?;
|
||||
let name = bytes_to_string(name)?;
|
||||
branches.push(name);
|
||||
@@ -48,9 +36,12 @@ pub(crate) fn local_branch_names(repo: &Repository) -> Result<Vec<String>> {
|
||||
pub(crate) fn remote_branch_names(repo: &Repository) -> Result<Vec<String>> {
|
||||
let mut branches = Vec::new();
|
||||
for branch in repo.branches(Some(BranchType::Remote))? {
|
||||
let branch = branch?.0;
|
||||
let (branch, _) = branch?;
|
||||
let name = branch.name_bytes()?;
|
||||
let name = bytes_to_string(name)?;
|
||||
if name.ends_with("/HEAD") {
|
||||
continue;
|
||||
}
|
||||
branches.push(name);
|
||||
}
|
||||
Ok(branches)
|
||||
@@ -64,7 +55,13 @@ pub(crate) fn bytes_to_string(bytes: &[u8]) -> Result<String> {
|
||||
Ok(String::from_utf8(bytes.to_vec())?)
|
||||
}
|
||||
|
||||
pub(crate) fn get_default_remote_for_push_in_repo(repo: &Repository) -> Result<String> {
|
||||
pub(crate) fn get_default_remote_for_push_in_repo(repo: &'_ Repository) -> Result<Remote<'_>> {
|
||||
let name = get_default_remote_name_for_push_in_repo(repo)?;
|
||||
let remote = repo.find_remote(&name)?;
|
||||
Ok(remote)
|
||||
}
|
||||
|
||||
pub(crate) fn get_default_remote_name_for_push_in_repo(repo: &Repository) -> Result<String> {
|
||||
let config = repo.config()?;
|
||||
|
||||
let branch = get_current_branch(repo)?;
|
||||
@@ -89,12 +86,22 @@ pub(crate) fn get_default_remote_for_push_in_repo(repo: &Repository) -> Result<S
|
||||
}
|
||||
}
|
||||
|
||||
get_default_remote_in_repo(repo)
|
||||
get_default_remote_name_in_repo(repo)
|
||||
}
|
||||
|
||||
pub(crate) fn get_default_remote_in_repo(repo: &Repository) -> Result<String> {
|
||||
pub(crate) fn get_default_remote_in_repo(repo: &'_ Repository) -> Result<Remote<'_>> {
|
||||
let name = get_default_remote_name_in_repo(repo)?;
|
||||
let remote = repo.find_remote(&name)?;
|
||||
Ok(remote)
|
||||
}
|
||||
|
||||
pub(crate) fn get_default_remote_name_in_repo(repo: &Repository) -> Result<String> {
|
||||
let remotes = repo.remotes()?;
|
||||
|
||||
if remotes.is_empty() {
|
||||
return Err(NoDefaultRemoteFound);
|
||||
}
|
||||
|
||||
// if `origin` exists return that
|
||||
let found_origin = remotes.iter().any(|r| r.is_some_and(|r| r == DEFAULT_REMOTE_NAME));
|
||||
if found_origin {
|
||||
|
||||
Reference in New Issue
Block a user