Validate we're working with a git repository.

pull/3/head
Joshua Potter 2022-01-08 21:25:55 -05:00
parent d1d0c8f172
commit 4256a79a87
2 changed files with 19 additions and 19 deletions

View File

@ -1,4 +1,5 @@
use super::{config::PathConfig, path, path::ResPathBuf}; use super::{config::PathConfig, path, path::ResPathBuf};
use git2::Repository;
use simplelog::{info, paris, warn}; use simplelog::{info, paris, warn};
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -8,8 +9,6 @@ use std::{
result, result,
}; };
// TODO(jrpotter): Validate local path is a git repository.
// ======================================== // ========================================
// Error // Error
// ======================================== // ========================================
@ -59,7 +58,7 @@ impl error::Error for Error {}
// ======================================== // ========================================
fn apply_all(pc: &PathConfig) -> Result<()> { 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 repo_files = walk_repo(workdir.as_ref())?;
let package_lookup = get_package_lookup(pc); 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<()> { 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) { if let Some(paths) = pc.config.packages.get(package) {
for path in paths { for path in paths {
@ -136,7 +135,7 @@ pub fn apply(pc: &PathConfig, package: Option<&str>) -> Result<()> {
// ======================================== // ========================================
pub fn stage(pc: &PathConfig) -> 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 repo_files = walk_repo(workdir.as_ref())?;
let package_lookup = get_package_lookup(pc); let package_lookup = get_package_lookup(pc);
@ -178,6 +177,17 @@ pub fn stage(pc: &PathConfig) -> Result<()> {
// Utility // Utility
// ======================================== // ========================================
fn get_workdir(pc: &PathConfig) -> Result<ResPathBuf> {
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<Vec<ResPathBuf>> { fn recursive_walk_repo(root: &Path, path: &Path) -> Result<Vec<ResPathBuf>> {
let mut seen = Vec::new(); let mut seen = Vec::new();
if path.is_dir() { if path.is_dir() {

View File

@ -5,13 +5,7 @@ use git2::{
StashFlags, StashFlags,
}; };
use simplelog::{info, paris, warn}; use simplelog::{info, paris, warn};
use std::{ use std::{collections::HashSet, env::VarError, error, fmt, io, path::Path, result};
collections::HashSet,
env::VarError,
error, fmt, io,
path::{Path, PathBuf},
result,
};
// ======================================== // ========================================
// Error // Error
@ -246,7 +240,7 @@ fn local_rebase_remote(pc: &PathConfig, repo: &Repository) -> Result<()> {
} }
pub fn pull(pc: &PathConfig, repo: &mut 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 // 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 // we can apply stashes. Stow away our changes, rebase on remote, and then
@ -471,8 +465,8 @@ fn get_push_options(pc: &PathConfig) -> Result<PushOptions> {
// Miscellaneous // Miscellaneous
// ======================================== // ========================================
fn check_working_repo(repo: &Repository) -> Result<PathBuf> { fn now_signature(pc: &PathConfig) -> Result<Signature> {
Ok(repo.workdir().ok_or(Error::InvalidBareRepo)?.to_path_buf()) Ok(Signature::now(&pc.config.user.name, &pc.config.user.email)?)
} }
fn get_commit_at_head(repo: &Repository) -> Option<Commit> { fn get_commit_at_head(repo: &Repository) -> Option<Commit> {
@ -487,10 +481,6 @@ fn get_commit_at_head(repo: &Repository) -> Option<Commit> {
peel().ok() peel().ok()
} }
fn now_signature(pc: &PathConfig) -> Result<Signature> {
Ok(Signature::now(&pc.config.user.name, &pc.config.user.email)?)
}
fn temporary_branch_name(pc: &PathConfig, repo: &Repository) -> Result<String> { fn temporary_branch_name(pc: &PathConfig, repo: &Repository) -> Result<String> {
let mut branch_names = HashSet::new(); let mut branch_names = HashSet::new();
for b in repo.branches(Some(BranchType::Local))? { for b in repo.branches(Some(BranchType::Local))? {