Skip to content

Commit

Permalink
Ensure git path resolution for different versions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
qgadrian committed Nov 29, 2024
1 parent 91e6afa commit 38cb573
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
46 changes: 40 additions & 6 deletions lib/git/git_path.ex
Original file line number Diff line number Diff line change
@@ -1,35 +1,69 @@
defmodule GitHooks.Git.GitPath do
@moduledoc false

alias GitHooks.Git

@doc """
Returns the absolute path to the project's root directory.
"""
def resolve_app_path do
# Attempt to get the project path from the config,
# otherwise find the git root by traversing upwards.
Application.get_env(:git_hooks, :project_path) ||
project_path = Application.get_env(:git_hooks, :project_path)

if project_path do
Path.expand(project_path)
else
# Find the git root by traversing upwards
find_git_root(File.cwd!()) ||
raise "Could not find .git directory from #{File.cwd!()}"
raise "Could not find .git directory from #{File.cwd!()}"
end
end

@doc """
Returns the absolute `.git/hooks` path directory for the parent project.
"""
def resolve_git_hooks_path do
Path.join(resolve_git_dir(), "hooks")
"hooks"
|> resolve_git_path()
|> Path.expand(resolve_app_path())
end

@doc """
Returns the path to a specific hook file within the `.git/hooks` directory.
"""
def git_hooks_path_for(hook_name) do
Path.join(resolve_git_hooks_path(), hook_name)
resolve_git_hooks_path()
|> Path.join(hook_name)
|> Path.expand()
end

#
# Private helper functions
#

# Resolves the absolute path to a directory within the `.git` directory
defp resolve_git_path(dir) when is_binary(dir) and dir != "" do
git_path =
Git.git_version()
|> Version.compare(Version.parse!("2.10.0"))
|> case do
:lt ->
git_dir = resolve_git_dir()
Path.join(git_dir, dir)

_ ->
{git_path, 0} =
System.cmd("git", ["rev-parse", "--git-path", dir], cd: resolve_app_path())

String.trim(git_path)
end

Path.expand(git_path, resolve_app_path())
end

defp resolve_git_path(_dir) do
raise ArgumentError, "resolve_git_path/1 requires a non-empty directory argument"
end

# Returns the absolute `.git` directory path for the parent project.
defp resolve_git_dir do
{git_dir, 0} =
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule GitHooks.MixProject do
use Mix.Project

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

def project do
[
Expand Down

0 comments on commit 38cb573

Please sign in to comment.