From 1c1cfa41941ed6f33338fe3b04f4464568b5a98d Mon Sep 17 00:00:00 2001 From: Isaac Corbrey Date: Fri, 27 Sep 2024 15:00:24 -0400 Subject: [PATCH 1/5] fix: Set `CREATE_NO_WINDOW` flag when executing Git hooks --- git2-hooks/src/hookspath.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/git2-hooks/src/hookspath.rs b/git2-hooks/src/hookspath.rs index fe01386b4e..c41c22b39d 100644 --- a/git2-hooks/src/hookspath.rs +++ b/git2-hooks/src/hookspath.rs @@ -3,7 +3,10 @@ use git2::Repository; use crate::{error::Result, HookResult, HooksError}; use std::{ - env, path::Path, path::PathBuf, process::Command, str::FromStr, + env, + path::{Path, PathBuf}, + process::Command, + str::FromStr, }; pub struct HookPaths { @@ -118,6 +121,7 @@ impl HookPaths { .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 @@ -197,3 +201,20 @@ fn find_bash_executable() -> Option { fn find_default_unix_shell() -> Option { env::var_os("SHELL").map(PathBuf::from) } + +trait CommandExt { + fn with_no_window(&mut self) -> &mut Self; +} + +impl CommandExt for Command { + /// On Windows, CLI applications that aren't the window's subsystem will + /// create and show a console window that pops up next to the main + /// application window when run. We disable this behavior by setting the + /// `CREATE_NO_WINDOW` flag. + #[inline] + fn with_no_window(&mut self) -> &mut Self { + use std::os::windows::process::CommandExt; + #[cfg(windows)] + self.creation_flags(0x0800_0000) + } +} From 022351d94decb30fe5610b0dddcd8ebf164678da Mon Sep 17 00:00:00 2001 From: Isaac Corbrey Date: Fri, 27 Sep 2024 15:16:28 -0400 Subject: [PATCH 2/5] docs: Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56167573ce..e89e1af6cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixes * respect env vars like `GIT_CONFIG_GLOBAL` ([#2298](https://github.com/extrawurst/gitui/issues/2298)) +* Set `CREATE_NO_WINDOW` flag when executing Git hooks on Windows ([#2371](https://github.com/extrawurst/gitui/pull/2371)) ### Added * add popups for viewing, adding, updating and removing remotes [[@robin-thoene](https://github.com/robin-thoene)] ([#2172](https://github.com/extrawurst/gitui/issues/2172)) From 9894c895d957f8e1515b3edb0e3ae4ab90289e6c Mon Sep 17 00:00:00 2001 From: Isaac Corbrey Date: Sat, 28 Sep 2024 06:31:10 -0400 Subject: [PATCH 3/5] fix(git2-hooks): `CommandExt` has invalid syntax in non-Windows environments --- git2-hooks/src/hookspath.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/git2-hooks/src/hookspath.rs b/git2-hooks/src/hookspath.rs index c41c22b39d..cd120c6846 100644 --- a/git2-hooks/src/hookspath.rs +++ b/git2-hooks/src/hookspath.rs @@ -213,8 +213,12 @@ impl CommandExt for Command { /// `CREATE_NO_WINDOW` flag. #[inline] fn with_no_window(&mut self) -> &mut Self { - use std::os::windows::process::CommandExt; #[cfg(windows)] - self.creation_flags(0x0800_0000) + { + use std::os::windows::process::CommandExt; + self.creation_flags(0x0800_0000); + } + + self } } From 47ed1c3fa6fecad0f0717cf66892c92ed41f984c Mon Sep 17 00:00:00 2001 From: Isaac Corbrey Date: Sun, 29 Sep 2024 10:37:24 -0400 Subject: [PATCH 4/5] docs: Clarify CREATE_NO_WINDOW usage --- git2-hooks/src/hookspath.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/git2-hooks/src/hookspath.rs b/git2-hooks/src/hookspath.rs index cd120c6846..30ee9e8819 100644 --- a/git2-hooks/src/hookspath.rs +++ b/git2-hooks/src/hookspath.rs @@ -203,6 +203,16 @@ fn find_default_unix_shell() -> Option { } trait CommandExt { + /// The process is a console application that is being run without a + /// console window. Therefore, the console handle for the application is + /// not set. + /// + /// This flag is ignored if the application is not a console application, + /// or if it used with either `CREATE_NEW_CONSOLE` or `DETACHED_PROCESS`. + /// + /// See: https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags + const CREATE_NO_WINDOW: u32 = 0x0800_0000; + fn with_no_window(&mut self) -> &mut Self; } @@ -216,7 +226,7 @@ impl CommandExt for Command { #[cfg(windows)] { use std::os::windows::process::CommandExt; - self.creation_flags(0x0800_0000); + self.creation_flags(Self::CREATE_NO_WINDOW); } self From c1be0dae12b085f9d9ebe59a1e88352195ffa8d0 Mon Sep 17 00:00:00 2001 From: Isaac Corbrey Date: Sun, 29 Sep 2024 19:20:17 -0400 Subject: [PATCH 5/5] fix: Clippy is pedantic --- git2-hooks/src/hookspath.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git2-hooks/src/hookspath.rs b/git2-hooks/src/hookspath.rs index 30ee9e8819..3648676ee2 100644 --- a/git2-hooks/src/hookspath.rs +++ b/git2-hooks/src/hookspath.rs @@ -210,7 +210,7 @@ trait CommandExt { /// This flag is ignored if the application is not a console application, /// or if it used with either `CREATE_NEW_CONSOLE` or `DETACHED_PROCESS`. /// - /// See: https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags + /// See: const CREATE_NO_WINDOW: u32 = 0x0800_0000; fn with_no_window(&mut self) -> &mut Self;