Skip to content

Commit c5c0960

Browse files
committed
Add function to return current git version
1 parent 49ca50e commit c5c0960

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

lib/git.ex

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
defmodule GitHooks.Git do
22
@moduledoc false
33

4-
@doc false
5-
@spec resolve_git_path() :: any
6-
def resolve_git_path do
7-
:git_hooks
8-
|> Application.get_env(:git_path)
9-
|> case do
10-
nil ->
11-
{path, 0} = System.cmd("git", ["rev-parse", "--git-path", "hooks"])
12-
String.replace(path, "\n", "")
4+
@spec git_version() :: Version.t()
5+
def git_version() do
6+
{full_git_version, 0} = System.cmd("git", ["--version"])
137

14-
custom_path ->
15-
custom_path
16-
end
17-
end
8+
[version] = Regex.run(~r/\d\.\d+\.\d+/, full_git_version)
189

19-
@spec git_hooks_path_for(path :: String.t()) :: String.t()
20-
def git_hooks_path_for(path) do
21-
__MODULE__.resolve_git_path()
22-
|> Path.join("/#{path}")
10+
Version.parse!(version)
2311
end
2412
end

lib/git/path.ex

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ defmodule GitHooks.Git.Path do
2727

2828
# https://stackoverflow.com/questions/10848191/git-submodule-commit-hooks
2929
#
30-
# Git 2.10+
31-
# `git rev-parse --git-path hooks`
32-
# Pre Git 2.10+
33-
# `git rev-parse --git-dir /hooks`
30+
# For git >= 2.10+ => `git rev-parse --git-path hooks`
31+
# For git < 2.10+ => `git rev-parse --git-dir /hooks`
3432
#
35-
# This will support as well changes on the default /hooks path
36-
# git config core.hooksPath .githooks/
33+
# This will support as well changes on the default /hooks path:
34+
# `git config core.hooksPath .myCustomGithooks/`
3735

3836
@spec resolve_git_hooks_path_based_on_git_version() :: String.t()
3937
defp resolve_git_hooks_path_based_on_git_version() do

0 commit comments

Comments
 (0)