Skip to content

Commit fc8c70d

Browse files
committed
Ensure git path resolution for different versions
Updated `resolve_git_path/1` to handle both legacy and modern git versions correctly and ensured absolute paths are returned for all git-related path resolution functions.
1 parent 1b49b5f commit fc8c70d

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/git/git_path.ex

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,69 @@
11
defmodule GitHooks.Git.GitPath do
22
@moduledoc false
33

4+
alias GitHooks.Git
5+
46
@doc """
57
Returns the absolute path to the project's root directory.
68
"""
79
def resolve_app_path do
8-
# Attempt to get the project path from the config,
9-
# otherwise find the git root by traversing upwards.
10-
Application.get_env(:git_hooks, :project_path) ||
10+
project_path = Application.get_env(:git_hooks, :project_path)
11+
12+
if project_path do
13+
Path.expand(project_path)
14+
else
15+
# Find the git root by traversing upwards
1116
find_git_root(File.cwd!()) ||
12-
raise "Could not find .git directory from #{File.cwd!()}"
17+
raise "Could not find .git directory from #{File.cwd!()}"
18+
end
1319
end
1420

1521
@doc """
1622
Returns the absolute `.git/hooks` path directory for the parent project.
1723
"""
1824
def resolve_git_hooks_path do
19-
Path.join(resolve_git_dir(), "hooks")
25+
"hooks"
26+
|> resolve_git_path()
27+
|> Path.expand(resolve_app_path())
2028
end
2129

2230
@doc """
2331
Returns the path to a specific hook file within the `.git/hooks` directory.
2432
"""
2533
def git_hooks_path_for(hook_name) do
26-
Path.join(resolve_git_hooks_path(), hook_name)
34+
resolve_git_hooks_path()
35+
|> Path.join(hook_name)
36+
|> Path.expand()
2737
end
2838

2939
#
3040
# Private helper functions
3141
#
3242

43+
# Resolves the absolute path to a directory within the `.git` directory
44+
defp resolve_git_path(dir) when is_binary(dir) and dir != "" do
45+
git_path =
46+
Git.git_version()
47+
|> Version.compare(Version.parse!("2.10.0"))
48+
|> case do
49+
:lt ->
50+
git_dir = resolve_git_dir()
51+
Path.join(git_dir, dir)
52+
53+
_ ->
54+
{git_path, 0} =
55+
System.cmd("git", ["rev-parse", "--git-path", dir], cd: resolve_app_path())
56+
57+
String.trim(git_path)
58+
end
59+
60+
Path.expand(git_path, resolve_app_path())
61+
end
62+
63+
defp resolve_git_path(_dir) do
64+
raise ArgumentError, "resolve_git_path/1 requires a non-empty directory argument"
65+
end
66+
3367
# Returns the absolute `.git` directory path for the parent project.
3468
defp resolve_git_dir do
3569
{git_dir, 0} =

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule GitHooks.MixProject do
44
use Mix.Project
55

66
@source_url "https://github.com/qgadrian/elixir_git_hooks"
7-
@version "0.8.0-pre1"
7+
@version "0.8.0-pre2"
88

99
def project do
1010
[

0 commit comments

Comments
 (0)