From dfe284a45ba1501a411d2cf80fa200f09dcb8d5a Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 14 Jun 2020 11:27:23 +0200 Subject: [PATCH] some cleanup --- asyncgit/src/sync/commit.rs | 38 ++------------------------ asyncgit/src/sync/diff.rs | 14 +++------- asyncgit/src/sync/mod.rs | 6 ++--- asyncgit/src/sync/reset.rs | 20 ++++---------- asyncgit/src/sync/utils.rs | 54 +++++++++++++++++++++++++++++-------- 5 files changed, 56 insertions(+), 76 deletions(-) diff --git a/asyncgit/src/sync/commit.rs b/asyncgit/src/sync/commit.rs index d39e6edf..d5cf49f4 100644 --- a/asyncgit/src/sync/commit.rs +++ b/asyncgit/src/sync/commit.rs @@ -2,17 +2,6 @@ use super::{utils::repo, CommitId}; use crate::error::Result; use scopetime::scope_time; -/// -pub fn get_head(repo_path: &str) -> Result { - scope_time!("get_head"); - - let repo = repo(repo_path)?; - - let head_id = repo.head()?.target().expect("head target error"); - - Ok(CommitId::new(head_id)) -} - /// pub fn amend( repo_path: &str, @@ -46,10 +35,9 @@ mod tests { use crate::error::Result; use crate::sync::{ commit, get_commit_details, get_commit_files, stage_add_file, - tests::{repo_init, repo_init_empty}, - CommitId, LogWalker, + tests::repo_init_empty, utils::get_head, CommitId, LogWalker, }; - use commit::{amend, get_head}; + use commit::amend; use git2::Repository; use std::{fs::File, io::Write, path::Path}; @@ -96,26 +84,4 @@ mod tests { Ok(()) } - - #[test] - fn test_head_empty() -> Result<()> { - let (_td, repo) = repo_init_empty()?; - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); - - assert_eq!(get_head(repo_path).is_ok(), false); - - Ok(()) - } - - #[test] - fn test_head() -> Result<()> { - let (_td, repo) = repo_init()?; - let root = repo.path().parent().unwrap(); - let repo_path = root.as_os_str().to_str().unwrap(); - - assert_eq!(get_head(repo_path).is_ok(), true); - - Ok(()) - } } diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 2b23731c..b535e73d 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -8,7 +8,7 @@ use git2::{ }; use scopetime::scope_time; use std::{fs, path::Path}; -use utils::work_dir; +use utils::{get_head_repo, work_dir}; /// type of diff of a single line #[derive(Copy, Clone, PartialEq, Hash, Debug)] @@ -89,16 +89,8 @@ pub(crate) fn get_diff_raw<'a>( let diff = if stage { // diff against head - if let Ok(ref_head) = repo.head() { - let parent = repo.find_commit( - //TODO: use new NoHead Error - ref_head.target().ok_or_else(|| { - let name = ref_head.name().unwrap_or("??"); - Error::Generic( - format!("can not find the target of symbolic references: {}", name) - ) - })?, - )?; + if let Ok(id) = get_head_repo(&repo) { + let parent = repo.find_commit(id.into())?; let tree = parent.tree()?; repo.diff_tree_to_index( diff --git a/asyncgit/src/sync/mod.rs b/asyncgit/src/sync/mod.rs index fb4d4e42..55147459 100644 --- a/asyncgit/src/sync/mod.rs +++ b/asyncgit/src/sync/mod.rs @@ -17,7 +17,7 @@ mod tags; pub mod utils; pub use branch::get_branch_name; -pub use commit::{amend, get_head}; +pub use commit::amend; pub use commit_details::{get_commit_details, CommitDetails}; pub use commit_files::get_commit_files; pub use commits_info::{get_commits_info, CommitId, CommitInfo}; @@ -30,8 +30,8 @@ pub use reset::{reset_stage, reset_workdir}; pub use stash::{get_stashes, stash_apply, stash_drop, stash_save}; pub use tags::{get_tags, Tags}; pub use utils::{ - commit, commit_new, is_bare_repo, is_repo, stage_add_all, - stage_add_file, stage_addremoved, + commit, commit_new, get_head, is_bare_repo, is_repo, + stage_add_all, stage_add_file, stage_addremoved, }; #[cfg(test)] diff --git a/asyncgit/src/sync/reset.rs b/asyncgit/src/sync/reset.rs index 4140cb31..bfdd7980 100644 --- a/asyncgit/src/sync/reset.rs +++ b/asyncgit/src/sync/reset.rs @@ -1,5 +1,5 @@ -use super::utils::repo; -use crate::error::{Error, Result}; +use super::utils::{get_head_repo, repo}; +use crate::error::Result; use git2::{build::CheckoutBuilder, ObjectType}; use scopetime::scope_time; @@ -9,19 +9,9 @@ pub fn reset_stage(repo_path: &str, path: &str) -> Result<()> { let repo = repo(repo_path)?; - let head = repo.head(); - - if let Ok(reference) = head { - let obj = repo.find_object( - //TODO: use NoHead error type - reference.target().ok_or_else(|| { - Error::Generic( - "can't get reference to symbolic reference," - .to_string(), - ) - })?, - Some(ObjectType::Commit), - )?; + if let Ok(id) = get_head_repo(&repo) { + let obj = + repo.find_object(id.into(), Some(ObjectType::Commit))?; repo.reset_default(Some(&obj), &[path])?; } else { diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 813bc7d9..e68636f9 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -47,6 +47,25 @@ pub fn work_dir(repo: &Repository) -> &Path { repo.workdir().expect("unable to query workdir") } +/// +pub fn get_head(repo_path: &str) -> Result { + let repo = repo(repo_path)?; + get_head_repo(&repo) +} + +/// +pub fn get_head_repo(repo: &Repository) -> Result { + scope_time!("get_head_repo"); + + let head = repo.head()?.target(); + + if let Some(head_id) = head { + Ok(CommitId::new(head_id)) + } else { + Err(Error::NoHead) + } +} + /// ditto pub fn commit_new(repo_path: &str, msg: &str) -> Result { commit(repo_path, msg).map(CommitId::new) @@ -63,17 +82,8 @@ pub fn commit(repo_path: &str, msg: &str) -> Result { let tree_id = index.write_tree()?; let tree = repo.find_tree(tree_id)?; - //TODO: use NoHead error - let parents = if let Ok(reference) = repo.head() { - let parent = repo.find_commit( - reference.target().ok_or_else(|| { - Error::Generic( - "failed to get the target for reference" - .to_string(), - ) - })?, - )?; - vec![parent] + let parents = if let Ok(id) = get_head(repo_path) { + vec![repo.find_commit(id.into())?] } else { Vec::new() }; @@ -323,4 +333,26 @@ mod tests { Ok(()) } + + #[test] + fn test_head_empty() -> Result<()> { + let (_td, repo) = repo_init_empty()?; + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + assert_eq!(get_head(repo_path).is_ok(), false); + + Ok(()) + } + + #[test] + fn test_head() -> Result<()> { + let (_td, repo) = repo_init()?; + let root = repo.path().parent().unwrap(); + let repo_path = root.as_os_str().to_str().unwrap(); + + assert_eq!(get_head(repo_path).is_ok(), true); + + Ok(()) + } }