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.



22
23
24
25
# File 'lib/git_jump/config.rb', line 22

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.



8
9
10
# File 'lib/git_jump/config.rb', line 8

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/git_jump/config.rb', line 8

def path
  @path
end

Class Method Details

.default_config_contentObject



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

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



30
31
32
33
# File 'lib/git_jump/config.rb', line 30

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)


72
73
74
# File 'lib/git_jump/config.rb', line 72

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

#database_pathObject



64
65
66
# File 'lib/git_jump/config.rb', line 64

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

#exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/git_jump/config.rb', line 60

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

#find_project(project_path) ⇒ Object



87
88
89
90
# File 'lib/git_jump/config.rb', line 87

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

#keep_patterns(project_path = nil) ⇒ Object



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

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



68
69
70
# File 'lib/git_jump/config.rb', line 68

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