Skip to content

Commit 68a9f21

Browse files
committed
Fix cmds chained with && are not treated separated
The problem is caused by the fact that the code uses a simple `String.split(original_command, " ")` and treats the first token as the executable (base_command) and the rest as arguments. To allow using shell operators like `&&` or `||`, we can run everything in a shell. So instead of splitting the command, we hand off the entire string to the shell.
1 parent d1b8993 commit 68a9f21

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/tasks/cmd.ex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,22 @@ defmodule GitHooks.Tasks.Cmd do
5959
) ::
6060
__MODULE__.t()
6161
def new({:cmd, original_command, opts}, git_hook_type, git_hook_args) when is_list(opts) do
62-
[base_command | args] = String.split(original_command, " ")
62+
env_vars = Keyword.get(opts, :env, [])
63+
include_hook_args = Keyword.get(opts, :include_hook_args, false)
6364

64-
command_args =
65-
if Keyword.get(opts, :include_hook_args, false) do
66-
Enum.concat(args, git_hook_args)
65+
cmd_string =
66+
if include_hook_args do
67+
original_command <> " " <> Enum.join(git_hook_args, " ")
6768
else
68-
args
69+
original_command
6970
end
7071

7172
%__MODULE__{
7273
original_command: original_command,
73-
command: base_command,
74-
args: command_args,
75-
env: Keyword.get(opts, :env, []),
74+
command: "sh",
75+
# ["-c", "full shell command"]
76+
args: ["-c", cmd_string],
77+
env: env_vars,
7678
git_hook_type: git_hook_type
7779
}
7880
end

0 commit comments

Comments
 (0)