Skip to content

Add timeouts for commit hooks #2547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* new command-line option to override the default log file path (`--logfile`) [[@acuteenvy](https://github.com/acuteenvy)] ([#2539](https://github.com/gitui-org/gitui/pull/2539))
* dx: `make check` checks Cargo.toml dependency ordering using `cargo sort` [[@naseschwarz](https://github.com/naseschwarz)]
* add `use_selection_fg` to theme file to allow customizing selection foreground color [[@Upsylonbare](https://github.com/Upsylonbare)] ([#2515](https://github.com/gitui-org/gitui/pull/2515))
* add timeouts for hooks [[@DaRacci](https://github.com/DaRacci)] ([#2547](https://github.com/gitui-org/gitui/pull/2547))

### Changed
* improve syntax highlighting file detection [[@acuteenvy](https://github.com/acuteenvy)] ([#2524](https://github.com/extrawurst/gitui/pull/2524))
180 changes: 177 additions & 3 deletions asyncgit/src/sync/hooks.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use super::{repository::repo, RepoPath};
use crate::error::Result;
pub use git2_hooks::PrepareCommitMsgSource;
use scopetime::scope_time;
use std::time::Duration;

///
#[derive(Debug, PartialEq, Eq)]
@@ -10,6 +11,8 @@ pub enum HookResult {
Ok,
/// Hook returned error
NotOk(String),
/// Hook timed out
TimedOut,
}

impl From<git2_hooks::HookResult> for HookResult {
@@ -22,6 +25,7 @@ impl From<git2_hooks::HookResult> for HookResult {
stderr,
..
} => Self::NotOk(format!("{stdout}{stderr}")),
git2_hooks::HookResult::TimedOut { .. } => Self::TimedOut,
}
}
}
@@ -38,6 +42,22 @@ pub fn hooks_commit_msg(
Ok(git2_hooks::hooks_commit_msg(&repo, None, msg)?.into())
}

/// see `git2_hooks::hooks_prepare_commit_msg`
#[allow(unused)]
pub fn hooks_commit_msg_with_timeout(
repo_path: &RepoPath,
msg: &mut String,
timeout: Duration,
) -> Result<HookResult> {
scope_time!("hooks_prepare_commit_msg");

let repo = repo(repo_path)?;
Ok(git2_hooks::hooks_commit_msg_with_timeout(
&repo, None, msg, timeout,
)?
.into())
}

/// see `git2_hooks::hooks_pre_commit`
pub fn hooks_pre_commit(repo_path: &RepoPath) -> Result<HookResult> {
scope_time!("hooks_pre_commit");
@@ -47,6 +67,22 @@ pub fn hooks_pre_commit(repo_path: &RepoPath) -> Result<HookResult> {
Ok(git2_hooks::hooks_pre_commit(&repo, None)?.into())
}

/// see `git2_hooks::hooks_pre_commit`
#[allow(unused)]
pub fn hooks_pre_commit_with_timeout(
repo_path: &RepoPath,
timeout: Duration,
) -> Result<HookResult> {
scope_time!("hooks_pre_commit");

let repo = repo(repo_path)?;

Ok(git2_hooks::hooks_pre_commit_with_timeout(
&repo, None, timeout,
)?
.into())
}

/// see `git2_hooks::hooks_post_commit`
pub fn hooks_post_commit(repo_path: &RepoPath) -> Result<HookResult> {
scope_time!("hooks_post_commit");
@@ -56,6 +92,22 @@ pub fn hooks_post_commit(repo_path: &RepoPath) -> Result<HookResult> {
Ok(git2_hooks::hooks_post_commit(&repo, None)?.into())
}

/// see `git2_hooks::hooks_post_commit`
#[allow(unused)]
pub fn hooks_post_commit_with_timeout(
repo_path: &RepoPath,
timeout: Duration,
) -> Result<HookResult> {
scope_time!("hooks_post_commit");

let repo = repo(repo_path)?;

Ok(git2_hooks::hooks_post_commit_with_timeout(
&repo, None, timeout,
)?
.into())
}

/// see `git2_hooks::hooks_prepare_commit_msg`
pub fn hooks_prepare_commit_msg(
repo_path: &RepoPath,
@@ -72,8 +124,28 @@ pub fn hooks_prepare_commit_msg(
.into())
}

/// see `git2_hooks::hooks_prepare_commit_msg`
#[allow(unused)]
pub fn hooks_prepare_commit_msg_with_timeout(
repo_path: &RepoPath,
source: PrepareCommitMsgSource,
msg: &mut String,
timeout: Duration,
) -> Result<HookResult> {
scope_time!("hooks_prepare_commit_msg");

let repo = repo(repo_path)?;

Ok(git2_hooks::hooks_prepare_commit_msg_with_timeout(
&repo, None, source, msg, timeout,
)?
.into())
}

#[cfg(test)]
mod tests {
use tempfile::tempdir;

use super::*;
use crate::sync::tests::repo_init;
use std::fs::File;
@@ -99,7 +171,7 @@ mod tests {
let (_td, repo) = repo_init().unwrap();
let root = repo.workdir().unwrap();

let hook = b"#!/bin/sh
let hook = b"#!/usr/bin/env sh
echo 'rejected'
exit 1
";
@@ -136,7 +208,7 @@ mod tests {
let workdir =
crate::sync::utils::repo_work_dir(repo_path).unwrap();

let hook = b"#!/bin/sh
let hook = b"#!/usr/bin/env sh
echo $(pwd)
exit 1
";
@@ -162,7 +234,7 @@ mod tests {
let (_td, repo) = repo_init().unwrap();
let root = repo.workdir().unwrap();

let hook = b"#!/bin/sh
let hook = b"#!/usr/bin/env sh
echo 'msg' > $1
echo 'rejected'
exit 1
@@ -224,4 +296,106 @@ mod tests {

assert_eq!(msg, String::from("msg\n"));
}

#[test]
fn test_hooks_respect_timeout() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();

let hook = b"#!/usr/bin/env sh
sleep 0.250
";

git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_PRE_COMMIT,
hook,
);

let res = hooks_pre_commit_with_timeout(
&root.to_str().unwrap().into(),
Duration::from_millis(200),
)
.unwrap();

assert_eq!(res, HookResult::TimedOut);
}

#[test]
fn test_hooks_faster_than_timeout() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();

let hook = b"#!/usr/bin/env sh
sleep 0.1
";

git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_PRE_COMMIT,
hook,
);

let res = hooks_pre_commit_with_timeout(
&root.to_str().unwrap().into(),
Duration::from_millis(150),
)
.unwrap();

assert_eq!(res, HookResult::Ok);
}

#[test]
fn test_hooks_timeout_zero() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();

let hook = b"#!/usr/bin/env sh
sleep 1
";

git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_POST_COMMIT,
hook,
);

let res = hooks_post_commit_with_timeout(
&root.to_str().unwrap().into(),
Duration::ZERO,
)
.unwrap();

assert_eq!(res, HookResult::Ok);
}

#[test]
fn test_run_with_timeout_kills() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();

let temp_dir = tempdir().expect("temp dir");
let file = temp_dir.path().join("test");
let hook = format!(
"#!/usr/bin/env sh
sleep 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say a test stalling for 1 s due to some I/O is common enough to warrant setting this to a longer time. If everything is alright, this should not affect the test.

Suggested change
sleep 1
sleep 60

echo 'after sleep' > {}
",
file.as_path().to_str().unwrap()
);

git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_PRE_COMMIT,
hook.as_bytes(),
);

let res = hooks_pre_commit_with_timeout(
&root.to_str().unwrap().into(),
Duration::from_millis(100),
);

assert!(res.is_ok());
assert!(!file.exists());
}
}
7 changes: 5 additions & 2 deletions asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -66,8 +66,11 @@ pub use config::{
pub use diff::get_diff_commit;
pub use git2::BranchType;
pub use hooks::{
hooks_commit_msg, hooks_post_commit, hooks_pre_commit,
hooks_prepare_commit_msg, HookResult, PrepareCommitMsgSource,
hooks_commit_msg, hooks_commit_msg_with_timeout,
hooks_post_commit, hooks_post_commit_with_timeout,
hooks_pre_commit, hooks_pre_commit_with_timeout,
hooks_prepare_commit_msg, hooks_prepare_commit_msg_with_timeout,
HookResult, PrepareCommitMsgSource,
};
pub use hunks::{reset_hunk, stage_hunk, unstage_hunk};
pub use ignore::add_to_ignore;
Loading
Loading