Class: GitJump::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/git_jump/config.rb

Overview

Manages configuration from TOML file

Constant Summary collapse

DEFAULT_CONFIG =
{
  "database" => {
    "path" => "$XDG_DATA_HOME/git-jump/branches.db"
  },
  "tracking" => {
    "max_branches" => 20,
    "auto_track" => true,
    "keep_patterns" => ["^main$", "^master$", "^develop$", "^staging$"]
  },
  "projects" => []
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Config

Returns a new instance of Config.



20
21
22
23
# File 'lib/git_jump/config.rb', line 20

def initialize(path = nil)
  @path = Utils::XDG.config_path(path)
  @data = load_config
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/git_jump/config.rb', line 6

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/git_jump/config.rb', line 6

def path
  @path
end

Class Method Details

.default_config_contentObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git_jump/config.rb', line 33

def self.default_config_content
  <<~TOML
    [database]
    # SQLite database location (defaults to XDG_DATA_HOME/git-jump/branches.db)
    # You can use environment variables like $XDG_DATA_HOME or $HOME
    path = "$XDG_DATA_HOME/git-jump/branches.db"

    [tracking]
    # Maximum number of branches to track per project
    max_branches = 20

    # Automatically track branches on checkout (via git hook)
    auto_track = true

    # Global branch patterns to always keep when clearing (regex patterns)
    keep_patterns = ["^main$", "^master$", "^develop$", "^staging$"]

    # Example project-specific configuration
    # [[projects]]
    # name = "my-project"
    # path = "/path/to/my-project"
    # keep_patterns = ["^main$", "^feature/.*$"]
  TOML
end

.instance(path = nil) ⇒ Object



28
29
30
31
# File 'lib/git_jump/config.rb', line 28

def self.instance(path = nil)
  normalized_path = Utils::XDG.config_path(path)
  @instances[normalized_path] ||= new(path)
end

Instance Method Details

#auto_track?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/git_jump/config.rb', line 70

def auto_track?
  data.dig("tracking", "auto_track") != false
end

#database_pathObject



62
63
64
# File 'lib/git_jump/config.rb', line 62

def database_path
  expand_env_vars(data.dig("database", "path") || Utils::XDG.database_path)
end

#exists?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/git_jump/config.rb', line 58

def exists?
  File.exist?(@path)
end

#find_project(project_path) ⇒ Object



85
86
87
88
# File 'lib/git_jump/config.rb', line 85

def find_project(project_path)
  projects = data["projects"] || []
  projects.find { |p| p["path"] == project_path }
end

#keep_patterns(project_path = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/git_jump/config.rb', line 74

def keep_patterns(project_path = nil)
  # Check for project-specific patterns first
  if project_path
    project = find_project(project_path)
    return project["keep_patterns"] if project && project["keep_patterns"]
  end

  # Fall back to global patterns
  data.dig("tracking", "keep_patterns") || []
end

#max_branchesObject



66
67
68
# File 'lib/git_jump/config.rb', line 66

def max_branches
  data.dig("tracking", "max_branches") || 20
end