Module: GitReflow::Config

Defined in:
lib/git_reflow/config.rb

Overview

This is a utility module for getting and setting git-config variables.

Constant Summary collapse

CONFIG_FILE_PATH =
"#{ENV['HOME']}/.gitconfig.reflow"

Class Method Summary collapse

Class Method Details

.add(key, value, local: false, global: false, **_other_options) ⇒ Object

Adds a new git configuration variable.

Parameters:

  • key (String)

    The new key to set the value for

  • value (String)

    The value to set it to

  • options (Hash)

    a customizable set of options

Returns:

  • the result of running the git command



66
67
68
69
70
71
72
73
74
# File 'lib/git_reflow/config.rb', line 66

def add(key, value, local: false, global: false, **_other_options)
  if global
    GitReflow::Sandbox.run "git config --global --add #{key} \"#{value}\"", loud: false, blocking: false
  elsif local
    GitReflow::Sandbox.run "git config --add #{key} \"#{value}\"", loud: false, blocking: false
  else
    GitReflow::Sandbox.run "git config -f #{CONFIG_FILE_PATH} --add #{key} \"#{value}\"", loud: false, blocking: false
  end
end

.cache_git_config_key(key, value) ⇒ Object



80
81
82
# File 'lib/git_reflow/config.rb', line 80

def cache_git_config_key(key, value)
  instance_variable_set(:"@#{key.tr('.-', '_')}", value.to_s.strip)
end

.cached_git_config_value(key) ⇒ Object



76
77
78
# File 'lib/git_reflow/config.rb', line 76

def cached_git_config_value(key)
  instance_variable_get(:"@#{key.tr('.-', '_')}").to_s
end

.get(key, reload: false, all: false, local: false, **_other_options) ⇒ Object

Gets the reqested git configuration variable.

Parameters:

  • key (String)

    The key to get the value(s) for

  • options (Hash)

    a customizable set of options

Returns:

  • the value of the git configuration



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git_reflow/config.rb', line 17

def get(key, reload: false, all: false, local: false, **_other_options)
  return cached_git_config_value(key) unless reload || cached_git_config_value(key).empty?

  local = local ? '--local ' : ''
  if all
    new_value = GitReflow::Sandbox.run("git config #{local}--get-all #{key}", loud: false, blocking: false)
  else
    new_value = GitReflow::Sandbox.run("git config #{local}--get #{key}", loud: false, blocking: false)
  end
  cache_git_config_key(key, new_value)
end

.set(key, value, local: false, **_other_options) ⇒ Object

Sets the reqested git configuration variable.

Parameters:

  • key (String)

    The key to set the value for

  • value (String)

    The value to set it to

  • options (Hash)

    a customizable set of options

Returns:

  • the value of the git configuration



35
36
37
38
39
40
41
42
# File 'lib/git_reflow/config.rb', line 35

def set(key, value, local: false, **_other_options)
  value = value.to_s.strip
  if local
    GitReflow::Sandbox.run "git config --replace-all #{key} \"#{value}\"", loud: false, blocking: false
  else
    GitReflow::Sandbox.run "git config -f #{CONFIG_FILE_PATH} --replace-all #{key} \"#{value}\"", loud: false, blocking: false
  end
end

.unset(key, value: nil, local: false, **_other_options) ⇒ Object

Remove values of the reqested git configuration variable.

Parameters:

  • key (String)

    The key to remove

  • options (Hash)

    a customizable set of options

Returns:

  • the result of running the git command



50
51
52
53
54
55
56
57
# File 'lib/git_reflow/config.rb', line 50

def unset(key, value: nil, local: false, **_other_options)
  value = value.nil? ? '' : "\"#{value}\""
  if local
    GitReflow::Sandbox.run "git config --unset-all #{key} #{value}", loud: false, blocking: false
  else
    GitReflow::Sandbox.run "git config -f #{CONFIG_FILE_PATH} --unset-all #{key} #{value}", loud: false, blocking: false
  end
end