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

Commit daa300a

Browse files
committed
Enhance color and database support; add environment variable handling for GIT_DIR and GIT_PKGS_DB
1 parent 72b1d43 commit daa300a

File tree

7 files changed

+134
-35
lines changed

7 files changed

+134
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
- Pager support for long output (respects `GIT_PAGER`, `core.pager`, `PAGER`)
44
- `--no-pager` option for commands with long output
5+
- Colored output (respects `NO_COLOR`, `color.ui`, `color.pkgs`)
6+
- `GIT_DIR` and `GIT_PKGS_DB` environment variable support
57
- `git pkgs stats` now supports `--since` and `--until` date filters
68
- Consistent error handling across all commands (JSON errors when `--format=json`)
79
- `git pkgs update` now uses a transaction for atomicity and better performance
8-
- `GIT_PKGS_DB` environment variable to customize database location
9-
- Colored output for diff, show, and history commands (respects `NO_COLOR`)
1010
- Renamed `git pkgs outdated` to `git pkgs stale` (outdated remains as alias)
1111
- `git pkgs log` command to list commits with dependency changes
1212
- `git pkgs schema` command to output database schema in text, SQL, JSON, or markdown

README.md

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ git pkgs diff --from=main --to=feature # compare branches
3737
git pkgs init
3838
```
3939

40-
Walks through git history and builds a SQLite database of dependency changes, stored in `.git/pkgs.sqlite3`. Set `GIT_PKGS_DB` environment variable to use a custom location.
40+
Walks through git history and builds a SQLite database of dependency changes, stored in `.git/pkgs.sqlite3`.
4141

4242
Options:
4343
- `--branch=NAME` - analyze a specific branch (default: default branch)
@@ -328,38 +328,18 @@ jobs:
328328
- run: git pkgs diff --from=origin/${{ github.base_ref }} --to=HEAD
329329
```
330330
331-
## Colors
331+
## Configuration
332332
333-
Output is colored when writing to a terminal. Colors are automatically disabled when piping to another command or redirecting to a file.
333+
git-pkgs respects [standard git configuration](https://git-scm.com/docs/git-config).
334334
335-
To disable colors explicitly:
335+
**Colors** are enabled when writing to a terminal. Disable with `NO_COLOR=1`, `git config color.ui never`, or `git config color.pkgs never` for git-pkgs only.
336336

337-
```bash
338-
NO_COLOR=1 git pkgs diff --from=HEAD~10
339-
```
340-
341-
Or set `NO_COLOR` in your shell profile to disable colors permanently. See [no-color.org](https://no-color.org/) for details.
342-
343-
## Pager
344-
345-
Long output is piped through a pager, following git's precedence:
346-
347-
1. `GIT_PAGER` environment variable
348-
2. `core.pager` git config
349-
3. `PAGER` environment variable
350-
4. `less -FRSX` as fallback
337+
**Pager** follows git's precedence: `GIT_PAGER` env, `core.pager` config, `PAGER` env, then `less -FRSX`. Use `--no-pager` flag or `git config core.pager cat` to disable.
351338

352-
To disable paging for a single command:
339+
**Environment variables:**
353340

354-
```bash
355-
git pkgs history --no-pager
356-
```
357-
358-
To disable paging entirely, set your pager to `cat` or empty string:
359-
360-
```bash
361-
git config core.pager cat
362-
```
341+
- `GIT_DIR` - git directory location (standard git variable)
342+
- `GIT_PKGS_DB` - database path (default: `.git/pkgs.sqlite3`)
363343

364344
## Performance
365345

lib/git/pkgs/color.rb

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,43 @@ def self.enabled=(value)
2424
@enabled = value
2525
end
2626

27+
def self.reset!
28+
remove_instance_variable(:@enabled) if defined?(@enabled)
29+
end
30+
2731
def self.determine_color_support
28-
return false unless $stdout.respond_to?(:tty?) && $stdout.tty?
32+
# NO_COLOR takes precedence (https://no-color.org/)
2933
return false if ENV["NO_COLOR"] && !ENV["NO_COLOR"].empty?
3034
return false if ENV["TERM"] == "dumb"
3135

32-
true
36+
# Check git config: color.pkgs takes precedence over color.ui
37+
git_color = git_color_config
38+
case git_color
39+
when "always" then return true
40+
when "never" then return false
41+
# "auto" falls through to TTY check
42+
end
43+
44+
$stdout.respond_to?(:tty?) && $stdout.tty?
45+
end
46+
47+
def self.git_color_config
48+
# color.pkgs overrides color.ui for git-pkgs specific control
49+
pkgs_color = `git config --get color.pkgs 2>/dev/null`.chomp
50+
return normalize_color_value(pkgs_color) unless pkgs_color.empty?
51+
52+
ui_color = `git config --get color.ui 2>/dev/null`.chomp
53+
return normalize_color_value(ui_color) unless ui_color.empty?
54+
55+
"auto"
56+
end
57+
58+
def self.normalize_color_value(value)
59+
case value.downcase
60+
when "true", "always" then "always"
61+
when "false", "never" then "never"
62+
else "auto"
63+
end
3364
end
3465

3566
def self.colorize(text, *codes)

lib/git/pkgs/database.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ def self.path(git_dir = nil)
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)
24+
25+
raise NotInGitRepoError, "GIT_DIR '#{git_dir}' does not exist"
26+
end
27+
2028
dir = Dir.pwd
2129
loop do
2230
git_dir = File.join(dir, ".git")

lib/git/pkgs/repository.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ module Pkgs
77
class Repository
88
attr_reader :path
99

10-
def initialize(path = Dir.pwd)
11-
@path = path
12-
@rugged = Rugged::Repository.new(path)
10+
def initialize(path = nil)
11+
@path = path || ENV["GIT_DIR"] || Dir.pwd
12+
@rugged = Rugged::Repository.new(@path)
1313
end
1414

1515
def git_dir

test/git/pkgs/test_color.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class Git::Pkgs::TestColor < Minitest::Test
6+
def setup
7+
@original_env = {
8+
"NO_COLOR" => ENV["NO_COLOR"],
9+
"TERM" => ENV["TERM"]
10+
}
11+
Git::Pkgs::Color.reset!
12+
end
13+
14+
def teardown
15+
@original_env.each do |key, value|
16+
if value
17+
ENV[key] = value
18+
else
19+
ENV.delete(key)
20+
end
21+
end
22+
Git::Pkgs::Color.reset!
23+
end
24+
25+
def test_no_color_env_disables_color
26+
ENV["NO_COLOR"] = "1"
27+
Git::Pkgs::Color.reset!
28+
29+
refute Git::Pkgs::Color.enabled?
30+
end
31+
32+
def test_term_dumb_disables_color
33+
ENV.delete("NO_COLOR")
34+
ENV["TERM"] = "dumb"
35+
Git::Pkgs::Color.reset!
36+
37+
refute Git::Pkgs::Color.enabled?
38+
end
39+
40+
def test_colorize_returns_plain_text_when_disabled
41+
Git::Pkgs::Color.enabled = false
42+
43+
assert_equal "hello", Git::Pkgs::Color.red("hello")
44+
assert_equal "world", Git::Pkgs::Color.green("world")
45+
end
46+
47+
def test_colorize_returns_ansi_codes_when_enabled
48+
Git::Pkgs::Color.enabled = true
49+
50+
assert_equal "\e[31mhello\e[0m", Git::Pkgs::Color.red("hello")
51+
assert_equal "\e[32mworld\e[0m", Git::Pkgs::Color.green("world")
52+
assert_equal "\e[33mtest\e[0m", Git::Pkgs::Color.yellow("test")
53+
end
54+
55+
def test_normalize_color_value
56+
assert_equal "always", Git::Pkgs::Color.normalize_color_value("true")
57+
assert_equal "always", Git::Pkgs::Color.normalize_color_value("always")
58+
assert_equal "always", Git::Pkgs::Color.normalize_color_value("TRUE")
59+
assert_equal "never", Git::Pkgs::Color.normalize_color_value("false")
60+
assert_equal "never", Git::Pkgs::Color.normalize_color_value("never")
61+
assert_equal "never", Git::Pkgs::Color.normalize_color_value("FALSE")
62+
assert_equal "auto", Git::Pkgs::Color.normalize_color_value("auto")
63+
assert_equal "auto", Git::Pkgs::Color.normalize_color_value("anything")
64+
end
65+
end

test/git/pkgs/test_repository.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ 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")
911
create_test_repo
1012
add_file("README.md", "# Test")
1113
commit("Initial commit")
1214
end
1315

1416
def teardown
1517
cleanup_test_repo
18+
if @original_git_dir
19+
ENV["GIT_DIR"] = @original_git_dir
20+
else
21+
ENV.delete("GIT_DIR")
22+
end
1623
end
1724

1825
def test_initializes_with_path
@@ -95,4 +102,12 @@ def test_rev_parse_returns_nil_for_invalid_ref
95102

96103
assert_nil sha
97104
end
105+
106+
def test_respects_git_dir_env
107+
git_dir = File.join(@test_dir, ".git")
108+
ENV["GIT_DIR"] = git_dir
109+
110+
repo = Git::Pkgs::Repository.new
111+
assert_equal git_dir, repo.path
112+
end
98113
end

0 commit comments

Comments
 (0)