Skip to content

Commit 5a1dfbd

Browse files
committed
fix windows
1 parent db60ac3 commit 5a1dfbd

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

git2-hooks/src/hookspath.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl HookPaths {
110110
let hook = self.hook.clone();
111111
log::trace!("run hook '{:?}' in '{:?}'", hook, self.pwd);
112112

113-
let run_command = |mut command: Command| {
113+
let run_command = |command: &mut Command| {
114114
command
115115
.args(args)
116116
.current_dir(&self.pwd)
@@ -119,13 +119,26 @@ impl HookPaths {
119119
};
120120

121121
let output = if cfg!(windows) {
122-
// execute hook with sh
123-
run_command(sh_command(&hook))
122+
// execute hook in shell
123+
let command = {
124+
let mut os_str = std::ffi::OsString::new();
125+
os_str.push("'");
126+
if let Some(hook) = hook.to_str() {
127+
os_str.push(hook.replace('\'', "\\'"));
128+
} else {
129+
os_str.push(hook.as_os_str()); // TODO: this doesn't work if `hook` contains single-quotes
130+
}
131+
os_str.push("'");
132+
os_str.push(" \"$@\"");
133+
134+
os_str
135+
};
136+
run_command(sh_command().arg(command))
124137
} else {
125138
// execute hook directly
126-
match run_command(Command::new(&hook)) {
139+
match run_command(&mut Command::new(&hook)) {
127140
Err(err) if err.raw_os_error() == Some(ENOEXEC) => {
128-
run_command(sh_command(&hook))
141+
run_command(sh_command().arg(&hook))
129142
}
130143
result => result,
131144
}
@@ -161,7 +174,7 @@ impl HookPaths {
161174
}
162175
}
163176

164-
fn sh_command(script: &Path) -> Command {
177+
fn sh_command() -> Command {
165178
let mut command = Command::new(sh_path());
166179

167180
if cfg!(windows) {
@@ -177,8 +190,6 @@ fn sh_command(script: &Path) -> Command {
177190
command.arg("-l");
178191
}
179192

180-
command.arg(script);
181-
182193
command
183194
}
184195

0 commit comments

Comments
 (0)