Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 16f33dc

Browse files
committed
Add environment configuration support for Git directory and work tree
- Introduced methods to configure Git directory, work tree, and database path from environment variables. - Updated CLI to parse new options for `--git-dir` and `--work-tree`. - Enhanced repository initialization to respect configured paths. - Added tests to verify environment configuration behavior.
1 parent 079008c commit 16f33dc

5 files changed

Lines changed: 62 additions & 22 deletions

File tree

lib/git/pkgs.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,24 @@ class NotInitializedError < Error; end
4444
class NotInGitRepoError < Error; end
4545

4646
class << self
47-
attr_accessor :quiet
47+
attr_accessor :quiet, :git_dir, :work_tree, :db_path
48+
49+
def configure_from_env
50+
@git_dir ||= presence(ENV["GIT_DIR"])
51+
@work_tree ||= presence(ENV["GIT_WORK_TREE"])
52+
@db_path ||= presence(ENV["GIT_PKGS_DB"])
53+
end
54+
55+
def reset_config!
56+
@quiet = false
57+
@git_dir = nil
58+
@work_tree = nil
59+
@db_path = nil
60+
end
61+
62+
def presence(value)
63+
value && !value.empty? ? value : nil
64+
end
4865
end
4966
self.quiet = false
5067
end

lib/git/pkgs/cli.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def initialize(args)
5050
end
5151

5252
def run
53+
Git::Pkgs.configure_from_env
5354
parse_global_options
5455

5556
command = @args.shift
@@ -70,10 +71,23 @@ def run
7071

7172
def parse_global_options
7273
while @args.first&.start_with?("-")
73-
case @args.first
74+
arg = @args.first
75+
case arg
7476
when "-q", "--quiet"
7577
Git::Pkgs.quiet = true
7678
@args.shift
79+
when /^--git-dir=(.+)$/
80+
Git::Pkgs.git_dir = $1
81+
@args.shift
82+
when "--git-dir"
83+
@args.shift
84+
Git::Pkgs.git_dir = @args.shift
85+
when /^--work-tree=(.+)$/
86+
Git::Pkgs.work_tree = $1
87+
@args.shift
88+
when "--work-tree"
89+
@args.shift
90+
Git::Pkgs.work_tree = @args.shift
7791
else
7892
break
7993
end
@@ -86,7 +100,9 @@ def run_command(command)
86100
class_name = command.split(/[-_]/).map(&:capitalize).join
87101
command_class = Commands.const_get(class_name)
88102
command_class.new(@args).run
89-
rescue NameError
103+
rescue NameError => e
104+
# Only catch NameError for missing command class, not NoMethodError
105+
raise unless e.is_a?(NameError) && !e.is_a?(NoMethodError)
90106
$stderr.puts "Command '#{command}' not yet implemented"
91107
exit 1
92108
end
@@ -106,9 +122,11 @@ def print_help
106122
end
107123

108124
puts "Options:"
109-
puts " -h, --help Show this help message"
110-
puts " -v, --version Show version"
111-
puts " -q, --quiet Suppress informational messages"
125+
puts " -h, --help Show this help message"
126+
puts " -v, --version Show version"
127+
puts " -q, --quiet Suppress informational messages"
128+
puts " --git-dir=<path> Path to the git directory"
129+
puts " --work-tree=<path> Path to the working tree"
112130
puts
113131
puts "Run 'git pkgs <command> -h' for command-specific options."
114132
end

lib/git/pkgs/database.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ class Database
1010
SCHEMA_VERSION = 1
1111

1212
def self.path(git_dir = nil)
13-
return ENV["GIT_PKGS_DB"] if ENV["GIT_PKGS_DB"] && !ENV["GIT_PKGS_DB"].empty?
13+
return Git::Pkgs.db_path if Git::Pkgs.db_path
1414

1515
git_dir ||= find_git_dir
1616
File.join(git_dir, DB_FILE)
1717
end
1818

1919
def self.find_git_dir
20-
# Respect GIT_DIR environment variable
21-
if ENV["GIT_DIR"] && !ENV["GIT_DIR"].empty?
22-
git_dir = ENV["GIT_DIR"]
23-
return git_dir if File.directory?(git_dir)
20+
if Git::Pkgs.git_dir
21+
return Git::Pkgs.git_dir if File.directory?(Git::Pkgs.git_dir)
2422

25-
raise NotInGitRepoError, "GIT_DIR '#{git_dir}' does not exist"
23+
raise NotInGitRepoError, "GIT_DIR '#{Git::Pkgs.git_dir}' does not exist"
2624
end
2725

28-
dir = Dir.pwd
26+
# Start from work_tree if set, otherwise current directory
27+
dir = Git::Pkgs.work_tree || Dir.pwd
2928
loop do
3029
git_dir = File.join(dir, ".git")
3130
return git_dir if File.directory?(git_dir)

lib/git/pkgs/repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Repository
88
attr_reader :path
99

1010
def initialize(path = nil)
11-
@path = path || ENV["GIT_DIR"] || Dir.pwd
11+
@path = path || Git::Pkgs.git_dir || Git::Pkgs.work_tree || Dir.pwd
1212
@rugged = Rugged::Repository.new(@path)
1313
end
1414

test/git/pkgs/test_repository.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ class Git::Pkgs::TestRepository < Minitest::Test
66
include TestHelpers
77

88
def setup
9-
@original_git_dir = ENV["GIT_DIR"]
10-
ENV.delete("GIT_DIR")
9+
Git::Pkgs.reset_config!
1110
create_test_repo
1211
add_file("README.md", "# Test")
1312
commit("Initial commit")
1413
end
1514

1615
def teardown
1716
cleanup_test_repo
18-
if @original_git_dir
19-
ENV["GIT_DIR"] = @original_git_dir
20-
else
21-
ENV.delete("GIT_DIR")
22-
end
17+
Git::Pkgs.reset_config!
2318
end
2419

2520
def test_initializes_with_path
@@ -103,11 +98,22 @@ def test_rev_parse_returns_nil_for_invalid_ref
10398
assert_nil sha
10499
end
105100

106-
def test_respects_git_dir_env
101+
def test_respects_git_dir_config
102+
git_dir = File.join(@test_dir, ".git")
103+
Git::Pkgs.git_dir = git_dir
104+
105+
repo = Git::Pkgs::Repository.new
106+
assert_equal git_dir, repo.path
107+
end
108+
109+
def test_configure_from_env_reads_git_dir
107110
git_dir = File.join(@test_dir, ".git")
108111
ENV["GIT_DIR"] = git_dir
112+
Git::Pkgs.configure_from_env
109113

110114
repo = Git::Pkgs::Repository.new
111115
assert_equal git_dir, repo.path
116+
ensure
117+
ENV.delete("GIT_DIR")
112118
end
113119
end

0 commit comments

Comments
 (0)