Skip to content

Commit

Permalink
Add config to set a path where to run the hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjc authored and qgadrian committed Feb 7, 2022
1 parent 224bf77 commit d5c9622
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/git/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ defmodule GitHooks.Git.Path do
end
end

@doc false
def resolve_app_path do
git_dir = Application.get_env(:git_hooks, :git_path, &resolve_git_hooks_path/0)
repo_dir = Path.dirname(git_dir)
Path.relative_to(File.cwd!(), repo_dir)
end

@spec git_hooks_path_for(path :: String.t()) :: String.t()
def git_hooks_path_for(path) do
__MODULE__.resolve_git_hooks_path()
Expand Down
2 changes: 2 additions & 0 deletions lib/mix/tasks/git_hooks/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule Mix.Tasks.GitHooks.Install do
Printer.info("Installing git hooks...")

mix_path = Config.mix_path()
project_path = GitPath.resolve_app_path()

ensure_hooks_folder_exists()
clean_missing_hooks()
Expand All @@ -61,6 +62,7 @@ defmodule Mix.Tasks.GitHooks.Install do
body
|> String.replace("$git_hook", git_hook_atom_as_string)
|> String.replace("$mix_path", mix_path)
|> String.replace("$project_path", project_path)

unless opts[:quiet] || !Config.verbose?() do
Printer.info(
Expand Down
2 changes: 2 additions & 0 deletions priv/hook_template
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

[ "$project_path" != "" ] && cd $project_path

$mix_path git_hooks.run $git_hook "$@"
[ $? -ne 0 ] && exit 1
exit 0

0 comments on commit d5c9622

Please sign in to comment.