#![allow(deprecated)]
use std::fs;
use std::path::PathBuf;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use crate::config::ConfigSet;
use crate::error::Result;
use crate::hooks::{
resolve_hooks_dir, run_commit_hook, run_commit_hook_checked, run_hook, run_hook_in_git_dir,
run_post_checkout, run_post_merge, run_post_rewrite, run_pre_push_hook, run_pre_rebase,
CommitHookEnv, HookOutput, HookResult, PrePushRef, RewriteKind,
};
use crate::list_limit::Limited;
use crate::objects::ObjectId;
use crate::repo::Repository;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HookSource {
HooksDir,
CoreHooksPath,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HookInfo {
pub name: String,
pub path: PathBuf,
pub executable: bool,
pub source: HookSource,
}
#[derive(Debug, Copy, Clone)]
pub struct RepositoryHooks<'repo> {
repo: &'repo Repository,
}
impl<'repo> RepositoryHooks<'repo> {
pub(crate) fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
#[must_use]
pub fn hooks_dir(&self) -> PathBuf {
resolve_hooks_dir(self.repo)
}
pub fn list(&self, limit: Option<usize>) -> Result<Limited<HookInfo>> {
let config = self.repo.config().snapshot()?;
let hooks = list_hooks_in_dir(self.repo, config.as_ref())?;
Ok(Limited::from_vec(hooks, limit))
}
pub fn run(
&self,
hook_name: &str,
args: &[&str],
stdin_data: Option<&[u8]>,
) -> Result<HookResult> {
run_hook(self.repo, hook_name, args, stdin_data)
}
pub fn run_commit(
&self,
hook_name: &str,
args: &[&str],
stdin_data: Option<&[u8]>,
commit_env: &CommitHookEnv<'_>,
) -> Result<HookResult> {
run_commit_hook(self.repo, hook_name, args, stdin_data, commit_env)
}
pub fn run_commit_checked(
&self,
hook_name: &str,
args: &[&str],
stdin_data: Option<&[u8]>,
commit_env: &CommitHookEnv<'_>,
) -> Result<()> {
run_commit_hook_checked(self.repo, hook_name, args, stdin_data, commit_env)
}
#[must_use]
pub fn run_in_git_dir(
&self,
hook_name: &str,
args: &[&str],
stdin_data: Option<&[u8]>,
env_vars: &[(&str, &str)],
) -> HookResult {
run_hook_in_git_dir(self.repo, hook_name, args, stdin_data, env_vars)
}
#[must_use]
pub fn run_post_checkout(
&self,
old: Option<ObjectId>,
new: ObjectId,
branch_checkout: bool,
) -> bool {
run_post_checkout(self.repo, old, new, branch_checkout)
}
#[must_use]
pub fn run_post_merge(&self, squash: bool) -> bool {
run_post_merge(self.repo, squash)
}
#[must_use]
pub fn run_post_rewrite(&self, kind: RewriteKind, pairs: &[(ObjectId, ObjectId)]) -> bool {
run_post_rewrite(self.repo, kind, pairs)
}
pub fn run_pre_rebase(&self, upstream: Option<&str>) -> Result<bool> {
run_pre_rebase(self.repo, upstream)
}
pub fn run_pre_push(&self, remote: &str, url: &str, refs: &[PrePushRef]) -> Result<HookOutput> {
run_pre_push_hook(self.repo, remote, url, refs)
}
}
fn list_hooks_in_dir(repo: &Repository, config: &ConfigSet) -> Result<Vec<HookInfo>> {
let hooks_dir = resolve_hooks_dir(repo);
let source = if config.value("core.hooksPath").is_some() {
HookSource::CoreHooksPath
} else {
HookSource::HooksDir
};
let read_dir = match fs::read_dir(&hooks_dir) {
Ok(rd) => rd,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
Err(e) => return Err(e.into()),
};
let mut hooks = Vec::new();
for entry in read_dir {
let entry = entry?;
let file_type = entry.file_type()?;
if !file_type.is_file() {
continue;
}
let path = entry.path();
let name = entry.file_name().to_string_lossy().into_owned();
let executable = hook_is_executable(&path);
hooks.push(HookInfo {
name,
path,
executable,
source,
});
}
hooks.sort_by(|a, b| a.name.cmp(&b.name));
Ok(hooks)
}
fn hook_is_executable(path: &std::path::Path) -> bool {
#[cfg(unix)]
{
fs::metadata(path)
.map(|m| m.permissions().mode() & 0o111 != 0)
.unwrap_or(false)
}
#[cfg(not(unix))]
{
let _ = path;
true
}
}