diff --git a/src/copy.rs b/src/copy.rs index 52b4f6f..1e5e022 100644 --- a/src/copy.rs +++ b/src/copy.rs @@ -1,4 +1,5 @@ use super::{config::PathConfig, path, path::ResPathBuf}; +use git2::Repository; use simplelog::{info, paris, warn}; use std::{ collections::HashMap, @@ -8,8 +9,6 @@ use std::{ result, }; -// TODO(jrpotter): Validate local path is a git repository. - // ======================================== // Error // ======================================== @@ -59,7 +58,7 @@ impl error::Error for Error {} // ======================================== fn apply_all(pc: &PathConfig) -> Result<()> { - let workdir = path::resolve(&pc.config.repos.local)?; + let workdir = get_workdir(pc)?; let repo_files = walk_repo(workdir.as_ref())?; let package_lookup = get_package_lookup(pc); @@ -94,7 +93,7 @@ fn apply_all(pc: &PathConfig) -> Result<()> { } fn apply_one(pc: &PathConfig, package: &str) -> Result<()> { - let workdir = path::resolve(&pc.config.repos.local)?; + let workdir = get_workdir(pc)?; if let Some(paths) = pc.config.packages.get(package) { for path in paths { @@ -136,7 +135,7 @@ pub fn apply(pc: &PathConfig, package: Option<&str>) -> Result<()> { // ======================================== pub fn stage(pc: &PathConfig) -> Result<()> { - let workdir = path::resolve(&pc.config.repos.local)?; + let workdir = get_workdir(pc)?; let repo_files = walk_repo(workdir.as_ref())?; let package_lookup = get_package_lookup(pc); @@ -178,6 +177,17 @@ pub fn stage(pc: &PathConfig) -> Result<()> { // Utility // ======================================== +fn get_workdir(pc: &PathConfig) -> Result { + let workdir = path::resolve(&pc.config.repos.local)?; + match Repository::open(workdir.resolved()) { + Ok(_) => Ok(workdir), + Err(_) => Err(io::Error::new( + io::ErrorKind::NotFound, + "Local repository not found.", + ))?, + } +} + fn recursive_walk_repo(root: &Path, path: &Path) -> Result> { let mut seen = Vec::new(); if path.is_dir() { diff --git a/src/git.rs b/src/git.rs index ee8b96e..f7f59c0 100644 --- a/src/git.rs +++ b/src/git.rs @@ -5,13 +5,7 @@ use git2::{ StashFlags, }; use simplelog::{info, paris, warn}; -use std::{ - collections::HashSet, - env::VarError, - error, fmt, io, - path::{Path, PathBuf}, - result, -}; +use std::{collections::HashSet, env::VarError, error, fmt, io, path::Path, result}; // ======================================== // Error @@ -246,7 +240,7 @@ fn local_rebase_remote(pc: &PathConfig, repo: &Repository) -> Result<()> { } pub fn pull(pc: &PathConfig, repo: &mut Repository) -> Result<()> { - check_working_repo(repo)?; + repo.workdir().ok_or(Error::InvalidBareRepo)?; // If our local branch exists, it must also have a commit on it. Therefore // we can apply stashes. Stow away our changes, rebase on remote, and then @@ -471,8 +465,8 @@ fn get_push_options(pc: &PathConfig) -> Result { // Miscellaneous // ======================================== -fn check_working_repo(repo: &Repository) -> Result { - Ok(repo.workdir().ok_or(Error::InvalidBareRepo)?.to_path_buf()) +fn now_signature(pc: &PathConfig) -> Result { + Ok(Signature::now(&pc.config.user.name, &pc.config.user.email)?) } fn get_commit_at_head(repo: &Repository) -> Option { @@ -487,10 +481,6 @@ fn get_commit_at_head(repo: &Repository) -> Option { peel().ok() } -fn now_signature(pc: &PathConfig) -> Result { - Ok(Signature::now(&pc.config.user.name, &pc.config.user.email)?) -} - fn temporary_branch_name(pc: &PathConfig, repo: &Repository) -> Result { let mut branch_names = HashSet::new(); for b in repo.branches(Some(BranchType::Local))? {