Module: VagrantGitHooks::Service

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

Class Method Summary collapse

Class Method Details

.adopt_source(cfg, root, entries) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-git-hooks/service.rb', line 28

def adopt_source(cfg, root, entries)
  # Explicit Vagrantfile hooks win over hooks_source files with the same name.
  src = cfg.hooks_source.to_s.strip
  return if src.empty?

  dir = File.absolute_path?(src) ? src : File.expand_path(src, root.to_s)
  return unless File.directory?(dir)

  Dir.children(dir).sort.each do |fname|
    next unless Config::KNOWN_HOOKS.include?(fname)

    path = File.join(dir, fname)
    next unless File.file?(path)
    next if entries.key?(fname)

    entries[fname] = { name: fname, content: File.read(path) }
  end
end

.entries_from_config(cfg, root) ⇒ Array<Hash>

Builds installable hook entries from direct config and an optional source directory.

Parameters:

Returns:

  • (Array<Hash>)

    Hook entries consumed by the installer.



17
18
19
20
21
22
23
24
25
26
# File 'lib/vagrant-git-hooks/service.rb', line 17

def entries_from_config(cfg, root)
  entries = {}

  (cfg.hooks || {}).each do |name, cmd|
    entries[name.to_s] = { name: name.to_s, commands: cmd }
  end

  adopt_source(cfg, root, entries)
  entries.values
end

.install!(root:, cfg:, ui: nil, dry_run: false) ⇒ Hash

Installs or updates hooks for a Git repository.

Parameters:

  • root (String, Pathname)

    Git repository root.

  • cfg (VagrantGitHooks::Config)

    Plugin configuration.

  • ui (Object, nil) (defaults to: nil)

    Optional Vagrant UI object.

  • dry_run (Boolean) (defaults to: false)

    Whether to report planned changes without writing files.

Returns:

  • (Hash)

    Installation result.



62
63
64
65
66
67
68
# File 'lib/vagrant-git-hooks/service.rb', line 62

def install!(root:, cfg:, ui: nil, dry_run: false)
  installer_for(root, ui: ui).install(
    entries_from_config(cfg, root),
    fail_on_error: cfg.fail_on_error,
    dry_run: dry_run
  )
end

.installer_for(root, ui: nil) ⇒ Object



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

def installer_for(root, ui: nil)
  Util::Installer.new(
    hooks_dir: Util::Git.hooks_dir(root),
    manifest: Util::Manifest.new(Util::Git.manifest_path(root)),
    ui: ui
  )
end

.status(root:, cfg:) ⇒ Array<Hash>

Reports hook installation status for configured and previously managed hooks.

Parameters:

Returns:

  • (Array<Hash>)

    Status rows keyed by hook name.



85
86
87
88
# File 'lib/vagrant-git-hooks/service.rb', line 85

def status(root:, cfg:)
  configured = entries_from_config(cfg, root).map { |e| e[:name].to_s }
  installer_for(root).status(configured)
end

.uninstall!(root:, ui: nil, dry_run: false) ⇒ Hash

Removes managed hooks and restores backed-up foreign hooks.

Parameters:

  • root (String, Pathname)

    Git repository root.

  • ui (Object, nil) (defaults to: nil)

    Optional Vagrant UI object.

  • dry_run (Boolean) (defaults to: false)

    Whether to report planned changes without writing files.

Returns:

  • (Hash)

    Uninstall result.



76
77
78
# File 'lib/vagrant-git-hooks/service.rb', line 76

def uninstall!(root:, ui: nil, dry_run: false)
  installer_for(root, ui: ui).uninstall(dry_run: dry_run)
end