Skip to content

Commit b5f9076

Browse files
committed
Use .git path relative to parent project path
If the `project_path` configuration is present, the `.git` path will be relative to the absolute path given in the configuration.
1 parent 2974370 commit b5f9076

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/git/path.ex

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ defmodule GitHooks.Git.Path do
33

44
alias GitHooks.Git
55

6-
@doc false
6+
@doc """
7+
Returns the absolute `.git` path directory for the parent project.
8+
"""
79
@spec resolve_git_hooks_path() :: any
810
def resolve_git_hooks_path do
9-
resolve_git_path_based_on_git_version("hooks")
11+
path_for =
12+
"hooks"
13+
|> resolve_git_path_based_on_git_version()
14+
|> Path.relative_to(resolve_app_path())
15+
16+
resolve_app_path()
17+
|> Path.join(path_for)
18+
|> String.replace(~r/\/+/, "/")
1019
end
1120

1221
@doc false
@@ -20,6 +29,10 @@ defmodule GitHooks.Git.Path do
2029
Path.relative_to(project_path, repo_path)
2130
end
2231

32+
@doc """
33+
Returns the absolute `.git` path directory for the parent project, appending
34+
the given path.
35+
"""
2336
@spec git_hooks_path_for(path :: String.t()) :: String.t()
2437
def git_hooks_path_for(path) do
2538
__MODULE__.resolve_git_hooks_path()

test/git/path_test.exs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@ defmodule GitHooks.Git.PathTest do
44
alias GitHooks.Git.Path
55

66
describe "git_hooks_path_for/1" do
7-
test "appends the path to the hooks folder" do
8-
assert Path.git_hooks_path_for("/testing") == ".git/hooks/testing"
7+
setup [:project_path]
8+
9+
test "appends the path to the `.git/hooks` folder", %{project_path: project_path} do
10+
assert Path.git_hooks_path_for("/testing") == "#{project_path}/.git/hooks/testing"
911
end
1012
end
1113

1214
describe "resolve_git_hooks_path/0" do
13-
test "returns the git path of the project" do
14-
assert Path.resolve_git_hooks_path() == ".git/hooks"
15+
setup [:project_path]
16+
17+
test "returns the git path of the project", %{project_path: project_path} do
18+
assert Path.resolve_git_hooks_path() == "#{project_path}/.git/hooks"
1519
end
1620
end
21+
22+
#
23+
# Private functions
24+
#
25+
26+
defp project_path(_) do
27+
{project_path, 0} = System.cmd("pwd", [])
28+
project_path = String.replace(project_path, ~r/\n/, "")
29+
30+
{:ok, %{project_path: project_path}}
31+
end
1732
end

0 commit comments

Comments
 (0)