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

Commit c8dd8b6

Browse files
committed
Fix branch name retrieval and ensure final snapshot is stored for the last processed commit
1 parent 8e5e03f commit c8dd8b6

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

lib/git/pkgs/commands/init.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def initialize(args)
1717
def run
1818
repo = Repository.new
1919

20+
branch_name = @options[:branch] || repo.default_branch
21+
error "Branch '#{branch_name}' not found" unless repo.branch_exists?(branch_name)
22+
2023
if Database.exists?(repo.git_dir) && !@options[:force]
2124
puts "Database already exists. Use --force to rebuild."
2225
return
@@ -27,9 +30,6 @@ def run
2730
Database.create_schema(with_indexes: false)
2831
Database.optimize_for_bulk_writes
2932

30-
branch_name = @options[:branch] || repo.default_branch
31-
error "Branch '#{branch_name}' not found" unless repo.branch_exists?(branch_name)
32-
3333
branch = Models::Branch.find_or_create(branch_name)
3434
analyzer = Analyzer.new(repo)
3535

@@ -73,6 +73,7 @@ def bulk_process_commits(commits, branch, analyzer, total)
7373
dependency_commit_count = 0
7474
snapshots_stored = 0
7575
processed = 0
76+
last_processed_sha = nil
7677

7778
flush = lambda do
7879
return if pending_commits.empty?
@@ -160,6 +161,8 @@ def bulk_process_commits(commits, branch, analyzer, total)
160161
position: processed
161162
}
162163

164+
last_processed_sha = rugged_commit.oid
165+
163166
if has_changes
164167
dependency_commit_count += 1
165168

@@ -206,13 +209,12 @@ def bulk_process_commits(commits, branch, analyzer, total)
206209
flush.call if pending_commits.size >= BATCH_SIZE
207210
end
208211

209-
# Always store final snapshot for HEAD
210-
if snapshot.any?
211-
last_sha = commits.last&.oid
212-
if last_sha && !pending_snapshots.any? { |s| s[:sha] == last_sha }
212+
# Always store final snapshot for the last processed commit
213+
if snapshot.any? && last_processed_sha
214+
unless pending_snapshots.any? { |s| s[:sha] == last_processed_sha }
213215
snapshot.each do |(manifest_path, name), dep_info|
214216
pending_snapshots << {
215-
sha: last_sha,
217+
sha: last_processed_sha,
216218
manifest_path: manifest_path,
217219
name: name,
218220
ecosystem: dep_info[:ecosystem],

test/git/pkgs/test_cli.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,94 @@ def capture_stdout
745745
end
746746
end
747747

748+
class Git::Pkgs::TestInfoCommand < Minitest::Test
749+
include TestHelpers
750+
751+
def setup
752+
create_test_repo
753+
add_file("Gemfile", "source 'https://rubygems.org'\ngem 'rails'")
754+
commit("Add rails")
755+
@git_dir = File.join(@test_dir, ".git")
756+
Git::Pkgs::Database.connect(@git_dir)
757+
Git::Pkgs::Database.create_schema
758+
end
759+
760+
def teardown
761+
cleanup_test_repo
762+
end
763+
764+
def test_info_with_zero_snapshots_does_not_crash
765+
# Create commits with dependency changes but no snapshots
766+
sha = SecureRandom.hex(20)
767+
Git::Pkgs::Models::Commit.create!(
768+
sha: sha,
769+
message: "Test commit",
770+
author_name: "Test User",
771+
author_email: "[email protected]",
772+
committed_at: Time.now,
773+
has_dependency_changes: true
774+
)
775+
776+
# Should not raise FloatDomainError
777+
output = capture_stdout do
778+
Dir.chdir(@test_dir) do
779+
Git::Pkgs::Commands::Info.new([]).run
780+
end
781+
end
782+
783+
assert_includes output, "Commits with dependency changes: 1"
784+
assert_includes output, "Commits with snapshots: 0"
785+
assert_includes output, "Coverage: 0.0%"
786+
refute_includes output, "1 snapshot per"
787+
end
788+
789+
def test_info_with_snapshots_shows_ratio
790+
sha = SecureRandom.hex(20)
791+
commit = Git::Pkgs::Models::Commit.create!(
792+
sha: sha,
793+
message: "Test commit",
794+
author_name: "Test User",
795+
author_email: "[email protected]",
796+
committed_at: Time.now,
797+
has_dependency_changes: true
798+
)
799+
800+
manifest = Git::Pkgs::Models::Manifest.create!(
801+
path: "Gemfile",
802+
ecosystem: "rubygems",
803+
kind: "manifest"
804+
)
805+
806+
Git::Pkgs::Models::DependencySnapshot.create!(
807+
commit: commit,
808+
manifest: manifest,
809+
name: "rails",
810+
ecosystem: "rubygems",
811+
requirement: ">= 0",
812+
dependency_type: "runtime"
813+
)
814+
815+
output = capture_stdout do
816+
Dir.chdir(@test_dir) do
817+
Git::Pkgs::Commands::Info.new([]).run
818+
end
819+
end
820+
821+
assert_includes output, "Commits with dependency changes: 1"
822+
assert_includes output, "Commits with snapshots: 1"
823+
assert_includes output, "Coverage: 100.0%"
824+
end
825+
826+
def capture_stdout
827+
original = $stdout
828+
$stdout = StringIO.new
829+
yield
830+
$stdout.string
831+
ensure
832+
$stdout = original
833+
end
834+
end
835+
748836
class Git::Pkgs::TestSchemaCommand < Minitest::Test
749837
include TestHelpers
750838

0 commit comments

Comments
 (0)