some cleanup

This commit is contained in:
Stephan Dilly 2020-06-14 11:27:23 +02:00
parent 6be72c7983
commit dfe284a45b
5 changed files with 56 additions and 76 deletions

View file

@ -2,17 +2,6 @@ use super::{utils::repo, CommitId};
use crate::error::Result; use crate::error::Result;
use scopetime::scope_time; use scopetime::scope_time;
///
pub fn get_head(repo_path: &str) -> Result<CommitId> {
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( pub fn amend(
repo_path: &str, repo_path: &str,
@ -46,10 +35,9 @@ mod tests {
use crate::error::Result; use crate::error::Result;
use crate::sync::{ use crate::sync::{
commit, get_commit_details, get_commit_files, stage_add_file, commit, get_commit_details, get_commit_files, stage_add_file,
tests::{repo_init, repo_init_empty}, tests::repo_init_empty, utils::get_head, CommitId, LogWalker,
CommitId, LogWalker,
}; };
use commit::{amend, get_head}; use commit::amend;
use git2::Repository; use git2::Repository;
use std::{fs::File, io::Write, path::Path}; use std::{fs::File, io::Write, path::Path};
@ -96,26 +84,4 @@ mod tests {
Ok(()) 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(())
}
} }

View file

@ -8,7 +8,7 @@ use git2::{
}; };
use scopetime::scope_time; use scopetime::scope_time;
use std::{fs, path::Path}; use std::{fs, path::Path};
use utils::work_dir; use utils::{get_head_repo, work_dir};
/// type of diff of a single line /// type of diff of a single line
#[derive(Copy, Clone, PartialEq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Hash, Debug)]
@ -89,16 +89,8 @@ pub(crate) fn get_diff_raw<'a>(
let diff = if stage { let diff = if stage {
// diff against head // diff against head
if let Ok(ref_head) = repo.head() { if let Ok(id) = get_head_repo(&repo) {
let parent = repo.find_commit( let parent = repo.find_commit(id.into())?;
//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)
)
})?,
)?;
let tree = parent.tree()?; let tree = parent.tree()?;
repo.diff_tree_to_index( repo.diff_tree_to_index(

View file

@ -17,7 +17,7 @@ mod tags;
pub mod utils; pub mod utils;
pub use branch::get_branch_name; 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_details::{get_commit_details, CommitDetails};
pub use commit_files::get_commit_files; pub use commit_files::get_commit_files;
pub use commits_info::{get_commits_info, CommitId, CommitInfo}; 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 stash::{get_stashes, stash_apply, stash_drop, stash_save};
pub use tags::{get_tags, Tags}; pub use tags::{get_tags, Tags};
pub use utils::{ pub use utils::{
commit, commit_new, is_bare_repo, is_repo, stage_add_all, commit, commit_new, get_head, is_bare_repo, is_repo,
stage_add_file, stage_addremoved, stage_add_all, stage_add_file, stage_addremoved,
}; };
#[cfg(test)] #[cfg(test)]

View file

@ -1,5 +1,5 @@
use super::utils::repo; use super::utils::{get_head_repo, repo};
use crate::error::{Error, Result}; use crate::error::Result;
use git2::{build::CheckoutBuilder, ObjectType}; use git2::{build::CheckoutBuilder, ObjectType};
use scopetime::scope_time; use scopetime::scope_time;
@ -9,19 +9,9 @@ pub fn reset_stage(repo_path: &str, path: &str) -> Result<()> {
let repo = repo(repo_path)?; let repo = repo(repo_path)?;
let head = repo.head(); if let Ok(id) = get_head_repo(&repo) {
let obj =
if let Ok(reference) = head { repo.find_object(id.into(), Some(ObjectType::Commit))?;
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),
)?;
repo.reset_default(Some(&obj), &[path])?; repo.reset_default(Some(&obj), &[path])?;
} else { } else {

View file

@ -47,6 +47,25 @@ pub fn work_dir(repo: &Repository) -> &Path {
repo.workdir().expect("unable to query workdir") repo.workdir().expect("unable to query workdir")
} }
///
pub fn get_head(repo_path: &str) -> Result<CommitId> {
let repo = repo(repo_path)?;
get_head_repo(&repo)
}
///
pub fn get_head_repo(repo: &Repository) -> Result<CommitId> {
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 /// ditto
pub fn commit_new(repo_path: &str, msg: &str) -> Result<CommitId> { pub fn commit_new(repo_path: &str, msg: &str) -> Result<CommitId> {
commit(repo_path, msg).map(CommitId::new) commit(repo_path, msg).map(CommitId::new)
@ -63,17 +82,8 @@ pub fn commit(repo_path: &str, msg: &str) -> Result<Oid> {
let tree_id = index.write_tree()?; let tree_id = index.write_tree()?;
let tree = repo.find_tree(tree_id)?; let tree = repo.find_tree(tree_id)?;
//TODO: use NoHead error let parents = if let Ok(id) = get_head(repo_path) {
let parents = if let Ok(reference) = repo.head() { vec![repo.find_commit(id.into())?]
let parent = repo.find_commit(
reference.target().ok_or_else(|| {
Error::Generic(
"failed to get the target for reference"
.to_string(),
)
})?,
)?;
vec![parent]
} else { } else {
Vec::new() Vec::new()
}; };
@ -323,4 +333,26 @@ mod tests {
Ok(()) 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(())
}
} }