Skip to content
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

Run git-hooks more correctly #2483

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
2d90eb5
use /bin/sh for git-hooks
Joshix-1 Jan 15, 2025
6a4b9d6
update CHANGELOG.md
Joshix-1 Jan 15, 2025
8571406
fix escape bugs in hooks
Joshix-1 Jan 15, 2025
cba5142
simplify logic for running hooks on non-windows platforms
Joshix-1 Jan 15, 2025
7f70d52
invert if check
Joshix-1 Jan 15, 2025
bd61ad8
debug_assert
Joshix-1 Jan 15, 2025
c257d02
update CHANGELOG.md
Joshix-1 Jan 15, 2025
cde4bc1
Merge remote-tracking branch 'extrawurst/master' into git-hooks-shell…
Joshix-1 Mar 25, 2025
816e175
simplify build_shell_script_execution_command
Joshix-1 Mar 25, 2025
da71107
create build_shell_script_execution_command
Joshix-1 Mar 25, 2025
2b6d295
small refactorings
Joshix-1 Mar 25, 2025
ce9fc0e
add line
Joshix-1 Mar 25, 2025
04a31eb
execute hoooks directly and fallback to using sh
Joshix-1 Mar 25, 2025
8ba997d
rename function
Joshix-1 Mar 25, 2025
4f65b81
update commend
Joshix-1 Mar 25, 2025
58afb49
sh_command
Joshix-1 Mar 25, 2025
a6e71d3
refactor
Joshix-1 Mar 25, 2025
b79e5ce
name const
Joshix-1 Mar 25, 2025
177d9d8
update CHANGELOG
Joshix-1 Mar 25, 2025
e5e562e
add new test
Joshix-1 Mar 25, 2025
f397799
deindent
Joshix-1 Mar 25, 2025
dbd8fee
fix is_executable
Joshix-1 Mar 25, 2025
7c8b6bc
debug print
Joshix-1 Mar 25, 2025
1370a14
handle ENOEXEC
Joshix-1 Mar 25, 2025
65c5997
add debug print
Joshix-1 Mar 25, 2025
102e7fd
remove debug print
Joshix-1 Mar 25, 2025
26b8871
set different env var
Joshix-1 Mar 25, 2025
5327f4a
Revert "set different env var"
Joshix-1 Mar 25, 2025
11da04a
remove ansi clear from output
Joshix-1 Mar 25, 2025
cfdb196
fix typo
Joshix-1 Mar 25, 2025
08dce84
move ansi escape hack to impl From<git2_hooks::HookResult> for HookRe…
Joshix-1 Mar 25, 2025
b37817d
fix ansi clear
Joshix-1 Mar 25, 2025
a7fe9cb
try strip different
Joshix-1 Mar 25, 2025
a4e2f54
more trimming
Joshix-1 Mar 25, 2025
d25efdc
Revert "more trimming"
Joshix-1 Mar 25, 2025
caf83c8
weird hack only on windows
Joshix-1 Mar 25, 2025
f91aa10
revert stuff
Joshix-1 Mar 25, 2025
adeb50d
trim_ascii_end
Joshix-1 Mar 25, 2025
c5c1fe9
format assert
Joshix-1 Mar 26, 2025
c0a8996
move truncation
Joshix-1 Mar 26, 2025
3e4b3c2
debug
Joshix-1 Mar 26, 2025
4601892
Merge remote-tracking branch 'extrawurst/master' into git-hooks-shell…
Joshix-1 Mar 26, 2025
db60ac3
fix test
Joshix-1 Mar 26, 2025
5a1dfbd
fix windows
Joshix-1 Mar 26, 2025
94b9dc7
add missing argument
Joshix-1 Mar 26, 2025
f9ff366
add argument
Joshix-1 Mar 26, 2025
38a3484
remove hacks
Joshix-1 Mar 26, 2025
7b4903b
correctly escape single-quote if utf8
Joshix-1 Mar 26, 2025
0b3d2c0
less indentation
Joshix-1 Mar 26, 2025
a0b7503
test if string contains single-quote
Joshix-1 Mar 27, 2025
f58f550
replace single-quotes in string
Joshix-1 Mar 27, 2025
9b8a87e
add test with invalid utf8
Joshix-1 Mar 27, 2025
b54711f
Merge remote-tracking branch 'extrawurst/master' into git-hooks-shell…
Joshix-1 Mar 27, 2025
418d016
fix tests with invalid utf8
Joshix-1 Mar 27, 2025
f2d41fd
fix generating invalid utf8 ostring
Joshix-1 Mar 27, 2025
40179d5
remove dead_code
Joshix-1 Mar 27, 2025
16e150e
dereference
Joshix-1 Mar 27, 2025
2796f8f
try different string on windows
Joshix-1 Mar 27, 2025
ee8fcf9
only valid unicode paths are accepted on windows
Joshix-1 Mar 27, 2025
eaad1f3
remove unused import
Joshix-1 Mar 27, 2025
48922ce
revert returning error
Joshix-1 Mar 30, 2025
9649e84
test if string contains single-quote
Joshix-1 Mar 27, 2025
c74e762
make clippy happy
Joshix-1 Mar 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
* execute git-hooks directly if possible (on *nix) else use sh instead of bash (without reading SHELL variable) [[@Joshix](https://github.com/Joshix-1)] ([#2483](https://github.com/extrawurst/gitui/pull/2483))

### Added
* support loading custom syntax highlighting themes from a file [[@acuteenvy](https://github.com/acuteenvy)] ([#2565](https://github.com/gitui-org/gitui/pull/2565))
62 changes: 42 additions & 20 deletions asyncgit/src/sync/hooks.rs
Original file line number Diff line number Diff line change
@@ -74,8 +74,34 @@ pub fn hooks_prepare_commit_msg(

#[cfg(test)]
mod tests {
use std::ffi::OsString;

use git2::Repository;
use tempfile::TempDir;

use super::*;
use crate::sync::tests::repo_init;
use crate::sync::tests::repo_init_with_prefix;

fn repo_init() -> Result<(TempDir, Repository)> {
let mut os_string: OsString = OsString::new();

os_string.push("gitui $# ' ");

#[cfg(target_os = "linux")]
{
use std::os::unix::ffi::OsStrExt;

const INVALID_UTF8: &[u8] = b"\xED\xA0\x80";

os_string.push(std::ffi::OsStr::from_bytes(INVALID_UTF8));

assert!(os_string.to_str().is_none());
}

os_string.push(" ");

repo_init_with_prefix(os_string)
}

#[test]
fn test_post_commit_hook_reject_in_subfolder() {
@@ -85,7 +111,7 @@ mod tests {
let hook = b"#!/bin/sh
echo 'rejected'
exit 1
";
";

git2_hooks::create_hook(
&repo,
@@ -96,9 +122,7 @@ mod tests {
let subfolder = root.join("foo/");
std::fs::create_dir_all(&subfolder).unwrap();

let res =
hooks_post_commit(&subfolder.to_str().unwrap().into())
.unwrap();
let res = hooks_post_commit(&subfolder.into()).unwrap();

assert_eq!(
res,
@@ -114,16 +138,16 @@ mod tests {
fn test_pre_commit_workdir() {
let (_td, repo) = repo_init().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
let repo_path: &RepoPath = &root.to_path_buf().into();
let repository =
crate::sync::repository::repo(repo_path).unwrap();
let workdir =
crate::sync::utils::repo_work_dir(repo_path).unwrap();
crate::sync::utils::work_dir(&repository).unwrap();

let hook = b"#!/bin/sh
echo $(pwd)
echo \"$(pwd)\"
exit 1
";

";
git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_PRE_COMMIT,
@@ -132,8 +156,9 @@ mod tests {
let res = hooks_pre_commit(repo_path).unwrap();
if let HookResult::NotOk(res) = res {
assert_eq!(
std::path::Path::new(res.trim_end()),
std::path::Path::new(&workdir)
res.trim_end().trim_end_matches('/'),
// TODO: fix if output isn't utf8.
workdir.to_string_lossy().trim_end_matches('/'),
);
} else {
assert!(false);
@@ -146,10 +171,10 @@ mod tests {
let root = repo.path().parent().unwrap();

let hook = b"#!/bin/sh
echo 'msg' > $1
echo 'msg' > \"$1\"
echo 'rejected'
exit 1
";
";

git2_hooks::create_hook(
&repo,
@@ -161,11 +186,8 @@ mod tests {
std::fs::create_dir_all(&subfolder).unwrap();

let mut msg = String::from("test");
let res = hooks_commit_msg(
&subfolder.to_str().unwrap().into(),
&mut msg,
)
.unwrap();
let res =
hooks_commit_msg(&subfolder.into(), &mut msg).unwrap();

assert_eq!(
res,
12 changes: 10 additions & 2 deletions asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -123,7 +123,7 @@ pub mod tests {
};
use crate::error::Result;
use git2::Repository;
use std::{path::Path, process::Command};
use std::{ffi::OsStr, path::Path, process::Command};
use tempfile::TempDir;

///
@@ -144,11 +144,19 @@ pub mod tests {

///
pub fn repo_init() -> Result<(TempDir, Repository)> {
repo_init_with_prefix("gitui")
}

///
#[inline]
pub fn repo_init_with_prefix(
prefix: impl AsRef<OsStr>,
) -> Result<(TempDir, Repository)> {
init_log();

sandbox_config_files();

let td = TempDir::new()?;
let td = TempDir::with_prefix(prefix)?;
let repo = Repository::init(td.path())?;
{
let mut config = repo.config()?;
6 changes: 6 additions & 0 deletions asyncgit/src/sync/repository.rs
Original file line number Diff line number Diff line change
@@ -42,6 +42,12 @@ impl RepoPath {
}
}

impl From<PathBuf> for RepoPath {
fn from(value: PathBuf) -> Self {
Self::Path(value)
}
}

impl From<&str> for RepoPath {
fn from(p: &str) -> Self {
Self::Path(PathBuf::from(p))
160 changes: 111 additions & 49 deletions git2-hooks/src/hookspath.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use git2::Repository;
use crate::{error::Result, HookResult, HooksError};

use std::{
env,
ffi::{OsStr, OsString},
path::{Path, PathBuf},
process::Command,
str::FromStr,
@@ -17,6 +17,7 @@ pub struct HookPaths {

const CONFIG_HOOKS_PATH: &str = "core.hooksPath";
const DEFAULT_HOOKS_PATH: &str = "hooks";
const ENOEXEC: i32 = 8;

impl HookPaths {
/// `core.hooksPath` always takes precedence.
@@ -107,30 +108,76 @@ impl HookPaths {
/// this function calls hook scripts based on conventions documented here
/// see <https://git-scm.com/docs/githooks>
pub fn run_hook(&self, args: &[&str]) -> Result<HookResult> {
let hook = self.hook.clone();

let arg_str = format!("{:?} {}", hook, args.join(" "));
// Use -l to avoid "command not found" on Windows.
let bash_args =
vec!["-l".to_string(), "-c".to_string(), arg_str];
self.run_hook_os_str(args)
}

/// this function calls hook scripts based on conventions documented here
/// see <https://git-scm.com/docs/githooks>
pub fn run_hook_os_str<I, S>(&self, args: I) -> Result<HookResult>
where
I: IntoIterator<Item = S> + Copy,
S: AsRef<OsStr>,
{
let hook = self.hook.clone();
log::trace!("run hook '{:?}' in '{:?}'", hook, self.pwd);

let git_shell = find_bash_executable()
.or_else(find_default_unix_shell)
.unwrap_or_else(|| "bash".into());
let output = Command::new(git_shell)
.args(bash_args)
.with_no_window()
.current_dir(&self.pwd)
// This call forces Command to handle the Path environment correctly on windows,
// the specific env set here does not matter
// see https://github.com/rust-lang/rust/issues/37519
.env(
"DUMMY_ENV_TO_FIX_WINDOWS_CMD_RUNS",
"FixPathHandlingOnWindows",
let run_command = |command: &mut Command| {
command
.args(args)
.current_dir(&self.pwd)
.with_no_window()
.output()
};

let output = if cfg!(windows) {
// execute hook in shell
let command = {
// SEE: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_02
// Enclosing characters in single-quotes ( '' ) shall preserve the literal value of each character within the single-quotes.
// A single-quote cannot occur within single-quotes.
const REPLACEMENT: &str = concat!(
"'", // closing single-quote
"\\'", // one escaped single-quote (outside of single-quotes)
"'", // new single-quote
);

let mut os_str = OsString::new();
os_str.push("'");
if let Some(hook) = hook.to_str() {
os_str.push(hook.replace('\'', REPLACEMENT));
} else {
#[cfg(windows)]
{
use std::os::windows::ffi::OsStrExt;
if hook
.as_os_str()
.encode_wide()
.any(|x| x == u16::from(b'\''))
{
// TODO: escape single quotes instead of failing
return Err(HooksError::PathToString);
}
}

os_str.push(hook.as_os_str());
}
os_str.push("'");
os_str.push(" \"$@\"");

os_str
};
run_command(
sh_command().arg("-c").arg(command).arg(&hook),
)
.output()?;
} else {
// execute hook directly
match run_command(&mut Command::new(&hook)) {
Err(err) if err.raw_os_error() == Some(ENOEXEC) => {
run_command(sh_command().arg(&hook))
}
result => result,
}
}?;

if output.status.success() {
Ok(HookResult::Ok { hook })
@@ -150,33 +197,28 @@ impl HookPaths {
}
}

#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
use std::os::unix::fs::PermissionsExt;
fn sh_command() -> Command {
let mut command = Command::new(sh_path());

let metadata = match path.metadata() {
Ok(metadata) => metadata,
Err(e) => {
log::error!("metadata error: {}", e);
return false;
}
};

let permissions = metadata.permissions();

permissions.mode() & 0o111 != 0
}
if cfg!(windows) {
// This call forces Command to handle the Path environment correctly on windows,
// the specific env set here does not matter
// see https://github.com/rust-lang/rust/issues/37519
command.env(
"DUMMY_ENV_TO_FIX_WINDOWS_CMD_RUNS",
"FixPathHandlingOnWindows",
);

// Use -l to avoid "command not found"
command.arg("-l");
}

#[cfg(windows)]
/// windows does not consider bash scripts to be executable so we consider everything
/// to be executable (which is not far from the truth for windows platform.)
const fn is_executable(_: &Path) -> bool {
true
command
}

// Find bash.exe, and avoid finding wsl's bash.exe on Windows.
// None for non-Windows.
fn find_bash_executable() -> Option<PathBuf> {
/// Get the path to the sh executable.
/// On Windows get the sh.exe bundled with Git for Windows
pub fn sh_path() -> PathBuf {
if cfg!(windows) {
Command::new("where.exe")
.arg("git")
@@ -190,16 +232,36 @@ fn find_bash_executable() -> Option<PathBuf> {
.as_deref()
.and_then(Path::parent)
.and_then(Path::parent)
.map(|p| p.join("usr/bin/bash.exe"))
.map(|p| p.join("usr/bin/sh.exe"))
.filter(|p| p.exists())
.unwrap_or_else(|| "sh".into())
} else {
None
"sh".into()
}
}

// Find default shell on Unix-like OS.
fn find_default_unix_shell() -> Option<PathBuf> {
env::var_os("SHELL").map(PathBuf::from)
#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
use std::os::unix::fs::PermissionsExt;

let metadata = match path.metadata() {
Ok(metadata) => metadata,
Err(e) => {
log::error!("metadata error: {}", e);
return false;
}
};

let permissions = metadata.permissions();

permissions.mode() & 0o111 != 0
}

#[cfg(windows)]
/// windows does not consider shell scripts to be executable so we consider everything
/// to be executable (which is not far from the truth for windows platform.)
const fn is_executable(_: &Path) -> bool {
true
}

trait CommandExt {
67 changes: 52 additions & 15 deletions git2-hooks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -132,10 +132,7 @@ pub fn hooks_commit_msg(
let temp_file = hook.git.join(HOOK_COMMIT_MSG_TEMP_FILE);
File::create(&temp_file)?.write_all(msg.as_bytes())?;

let res = hook.run_hook(&[temp_file
.as_os_str()
.to_string_lossy()
.as_ref()])?;
let res = hook.run_hook_os_str([&temp_file])?;

// load possibly altered msg
msg.clear();
@@ -282,7 +279,7 @@ exit 0

let hook = br#"#!/bin/sh
COMMIT_MSG="$(cat "$1")"
printf "$COMMIT_MSG" | sed 's/sth/shell_command/g' >"$1"
printf "$COMMIT_MSG" | sed 's/sth/shell_command/g' > "$1"
exit 0
"#;

@@ -309,6 +306,41 @@ exit 0
assert!(res.is_ok());
}

#[test]
fn test_hook_with_missing_shebang() {
const TEXT: &str = "Hello, world!";

let (_td, repo) = repo_init();

let hook = b"echo \"$@\"\nexit 42";

create_hook(&repo, HOOK_PRE_COMMIT, hook);

let hook =
HookPaths::new(&repo, None, HOOK_PRE_COMMIT).unwrap();

assert!(hook.found());

let result = hook.run_hook(&[TEXT]).unwrap();

let HookResult::RunNotSuccessful {
code,
stdout,
stderr,
hook: h,
} = result
else {
unreachable!("run_hook should've failed");
};

let stdout = stdout.as_str().trim_ascii_end();

assert_eq!(code, Some(42));
assert_eq!(h, hook.hook);
assert_eq!(stdout, TEXT, "{:?} != {TEXT:?}", stdout);
assert!(stderr.is_empty());
}

#[test]
fn test_no_hook_found() {
let (_td, repo) = repo_init();
@@ -388,6 +420,8 @@ exit 1

#[test]
fn test_env_containing_path() {
const PATH_EXPORT: &str = "export PATH";

let (_td, repo) = repo_init();

let hook = b"#!/bin/sh
@@ -402,9 +436,12 @@ exit 1
unreachable!()
};

assert!(stdout
.lines()
.any(|line| line.starts_with("export PATH")));
assert!(
stdout
.lines()
.any(|line| line.starts_with(PATH_EXPORT)),
"Could not find line starting with {PATH_EXPORT:?} in: {stdout:?}"
);
}

#[test]
@@ -470,7 +507,7 @@ sys.exit(0)

create_hook(&repo, HOOK_PRE_COMMIT, hook);
let res = hooks_pre_commit(&repo, None).unwrap();
assert!(res.is_ok());
assert!(res.is_ok(), "{res:?}");
}

#[test]
@@ -499,9 +536,9 @@ sys.exit(1)
let (_td, repo) = repo_init();

let hook = b"#!/bin/sh
echo 'msg' > $1
echo 'rejected'
exit 1
echo 'msg' > \"$1\"
echo 'rejected'
exit 1
";

create_hook(&repo, HOOK_COMMIT_MSG, hook);
@@ -525,7 +562,7 @@ exit 1
let (_td, repo) = repo_init();

let hook = b"#!/bin/sh
echo 'msg' > $1
echo 'msg' > \"$1\"
exit 0
";

@@ -565,7 +602,7 @@ exit 0
let (_td, repo) = repo_init();

let hook = b"#!/bin/sh
echo msg:$2 > $1
echo \"msg:$2\" > \"$1\"
exit 0
";

@@ -589,7 +626,7 @@ exit 0
let (_td, repo) = repo_init();

let hook = b"#!/bin/sh
echo $2,$3 > $1
echo \"$2,$3\" > \"$1\"
echo 'rejected'
exit 2
";