Skip to content

Run git-hooks more correctly #2483

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

Merged
merged 64 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 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
b3cf0ae
Merge remote-tracking branch 'extrawurst/master' into git-hooks-shell…
Joshix-1 Apr 15, 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
Expand Up @@ -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))
Expand Down
72 changes: 45 additions & 27 deletions asyncgit/src/sync/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,40 @@ pub fn hooks_prepare_commit_msg(

#[cfg(test)]
mod tests {
use std::{ffi::OsString, io::Write as _, path::Path};

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

use super::*;
use crate::sync::tests::repo_init;
use std::fs::File;
use std::io::Write;
use std::path::Path;
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)
}

fn create_hook_in_path(path: &Path, hook_script: &[u8]) {
File::create(path).unwrap().write_all(hook_script).unwrap();
std::fs::File::create(path)
.unwrap()
.write_all(hook_script)
.unwrap();

#[cfg(unix)]
{
Expand All @@ -102,7 +128,7 @@ mod tests {
let hook = b"#!/bin/sh
echo 'rejected'
exit 1
";
";

git2_hooks::create_hook(
&repo,
Expand All @@ -113,9 +139,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,
Expand All @@ -131,16 +155,12 @@ mod tests {
fn test_pre_commit_workdir() {
let (_td, repo) = repo_init().unwrap();
let root = repo.workdir().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();
let workdir =
crate::sync::utils::repo_work_dir(repo_path).unwrap();
let repo_path: &RepoPath = &root.to_path_buf().into();

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

";
git2_hooks::create_hook(
&repo,
git2_hooks::HOOK_PRE_COMMIT,
Expand All @@ -149,8 +169,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.
root.to_string_lossy().trim_end_matches('/'),
);
} else {
assert!(false);
Expand All @@ -163,10 +184,10 @@ mod tests {
let root = repo.workdir().unwrap();

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

git2_hooks::create_hook(
&repo,
Expand All @@ -178,11 +199,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,
Expand All @@ -199,7 +217,7 @@ mod tests {
let root = repo.workdir().unwrap();
let mut config = repo.config().unwrap();

const HOOKS_DIR: &'static str = "my_hooks";
const HOOKS_DIR: &str = "my_hooks";
config.set_str("core.hooksPath", HOOKS_DIR).unwrap();

let hook = b"#!/bin/sh
Expand All @@ -213,7 +231,7 @@ mod tests {

let mut msg = String::from("test");
let res = hooks_commit_msg(
&hooks_folder.to_str().unwrap().into(),
&hooks_folder.to_path_buf().into(),
&mut msg,
)
.unwrap();
Expand Down
12 changes: 10 additions & 2 deletions asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

///
Expand All @@ -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()?;
Expand Down
6 changes: 6 additions & 0 deletions asyncgit/src/sync/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading
Loading