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

Set CREATE_NO_WINDOW flag when executing Git hooks #2371

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -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))
Expand Down
37 changes: 36 additions & 1 deletion git2-hooks/src/hookspath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -197,3 +201,34 @@ fn find_bash_executable() -> Option<PathBuf> {
fn find_default_unix_shell() -> Option<PathBuf> {
env::var_os("SHELL").map(PathBuf::from)
}

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;
}

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.
extrawurst marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
fn with_no_window(&mut self) -> &mut Self {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
self.creation_flags(Self::CREATE_NO_WINDOW);
}

self
}
}