Module: VagrantGitHooks::Util::Git

Defined in:
lib/vagrant-git-hooks/util/git.rb

Class Method Summary collapse

Class Method Details

.absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/vagrant-git-hooks/util/git.rb', line 60

def absolute?(path)
  File.absolute_path?(path)
end

.available?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/vagrant-git-hooks/util/git.rb', line 11

def available?
  Verbose.log("git", "--version")
  _out, _err, st = Open3.capture3("git", "--version")
  st.success?
rescue StandardError
  false
end

.capture(root, *args) ⇒ Object



55
56
57
58
# File 'lib/vagrant-git-hooks/util/git.rb', line 55

def capture(root, *args)
  Verbose.log("git", "-C", root.to_s, *args)
  Open3.capture3("git", "-C", root.to_s, *args)
end

.hooks_dir(root) ⇒ Object



29
30
31
# File 'lib/vagrant-git-hooks/util/git.rb', line 29

def hooks_dir(root)
  resolve(root, "hooks") || File.join(toplevel(root) || root.to_s, ".git", "hooks")
end

.manifest_path(root) ⇒ Object



33
34
35
36
# File 'lib/vagrant-git-hooks/util/git.rb', line 33

def manifest_path(root)
  resolve(root, "vagrant-git-hooks.json") ||
    File.join(toplevel(root) || root.to_s, ".git", "vagrant-git-hooks.json")
end

.repo?(root) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/vagrant-git-hooks/util/git.rb', line 19

def repo?(root)
  out, _err, st = capture(root, "rev-parse", "--is-inside-work-tree")
  st.success? && out.strip == "true"
end

.resolve(root, name) ⇒ String?

Resolves a path under the repository's real hooks directory via git rev-parse --git-path, rather than assuming .git/<name>, so core.hooksPath and worktrees are honored.

Parameters:

  • root (String, Pathname)

    Git repository root.

  • name (String)

    Path fragment relative to the git directory.

Returns:

  • (String, nil)

    Absolute path, or nil when git can't resolve it.



45
46
47
48
49
50
51
52
53
# File 'lib/vagrant-git-hooks/util/git.rb', line 45

def resolve(root, name)
  out, _err, st = capture(root, "rev-parse", "--git-path", name)
  return nil unless st.success?

  path = out.strip
  return nil if path.empty?

  absolute?(path) ? path : File.expand_path(path, root.to_s)
end

.toplevel(root) ⇒ Object



24
25
26
27
# File 'lib/vagrant-git-hooks/util/git.rb', line 24

def toplevel(root)
  out, _err, st = capture(root, "rev-parse", "--show-toplevel")
  st.success? && !out.strip.empty? ? out.strip : nil
end