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

Commit e5f8b17

Browse files
committed
Refactor commit retrieval logic to use Models::Commit.find_or_create_from_repo
1 parent c511f07 commit e5f8b17

8 files changed

Lines changed: 35 additions & 76 deletions

File tree

lib/git/pkgs/commands/diff.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def run
2929
error "Could not resolve '#{from_ref}'" unless from_sha
3030
error "Could not resolve '#{to_ref}'" unless to_sha
3131

32-
from_commit = find_or_create_commit(repo, from_sha)
33-
to_commit = find_or_create_commit(repo, to_sha)
32+
from_commit = Models::Commit.find_or_create_from_repo(repo, from_sha)
33+
to_commit = Models::Commit.find_or_create_from_repo(repo, to_sha)
3434

3535
error "Commit '#{from_sha[0..7]}' not found" unless from_commit
3636
error "Commit '#{to_sha[0..7]}' not found" unless to_commit
@@ -98,27 +98,6 @@ def output_text(from_commit, to_commit, changes)
9898
puts "Summary: #{added_count} #{removed_count} #{modified_count}"
9999
end
100100

101-
def find_or_create_commit(repo, sha)
102-
commit = Models::Commit.find_by(sha: sha) ||
103-
Models::Commit.where("sha LIKE ?", "#{sha}%").first
104-
return commit if commit
105-
106-
# Lazily insert commit if it exists in git but not in database
107-
rugged_commit = repo.lookup(sha)
108-
return nil unless rugged_commit
109-
110-
Models::Commit.create!(
111-
sha: rugged_commit.oid,
112-
message: rugged_commit.message,
113-
author_name: rugged_commit.author[:name],
114-
author_email: rugged_commit.author[:email],
115-
committed_at: rugged_commit.time,
116-
has_dependency_changes: false
117-
)
118-
rescue Rugged::OdbError
119-
nil
120-
end
121-
122101
def parse_options
123102
options = {}
124103

lib/git/pkgs/commands/history.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "time"
4-
53
module Git
64
module Pkgs
75
module Commands
@@ -159,11 +157,6 @@ def parse_options
159157
options
160158
end
161159

162-
def parse_time(str)
163-
Time.parse(str)
164-
rescue ArgumentError
165-
error "Invalid date format: #{str}"
166-
end
167160
end
168161
end
169162
end

lib/git/pkgs/commands/log.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "time"
4-
53
module Git
64
module Pkgs
75
module Commands
@@ -102,12 +100,6 @@ def output_json(commits)
102100
puts JSON.pretty_generate(data)
103101
end
104102

105-
def parse_time(str)
106-
Time.parse(str)
107-
rescue ArgumentError
108-
error "Invalid date format: #{str}"
109-
end
110-
111103
def parse_options
112104
options = {}
113105

lib/git/pkgs/commands/show.rb

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def run
2222
sha = repo.rev_parse(ref)
2323
error "Could not resolve '#{ref}'" unless sha
2424

25-
commit = find_or_create_commit(repo, sha)
25+
commit = Models::Commit.find_or_create_from_repo(repo, sha)
2626
error "Commit '#{sha[0..7]}' not found" unless commit
2727

2828
changes = Models::DependencyChange
@@ -107,26 +107,6 @@ def output_json(commit, changes)
107107
puts JSON.pretty_generate(data)
108108
end
109109

110-
def find_or_create_commit(repo, sha)
111-
commit = Models::Commit.find_by(sha: sha) ||
112-
Models::Commit.where("sha LIKE ?", "#{sha}%").first
113-
return commit if commit
114-
115-
rugged_commit = repo.lookup(sha)
116-
return nil unless rugged_commit
117-
118-
Models::Commit.create!(
119-
sha: rugged_commit.oid,
120-
message: rugged_commit.message,
121-
author_name: rugged_commit.author[:name],
122-
author_email: rugged_commit.author[:email],
123-
committed_at: rugged_commit.time,
124-
has_dependency_changes: false
125-
)
126-
rescue Rugged::OdbError
127-
nil
128-
end
129-
130110
def parse_options
131111
options = {}
132112

lib/git/pkgs/commands/stats.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "time"
4-
53
module Git
64
module Pkgs
75
module Commands
@@ -199,12 +197,6 @@ def output_by_author_text(counts)
199197
end
200198
end
201199

202-
def parse_time(str)
203-
Time.parse(str)
204-
rescue ArgumentError
205-
error "Invalid date format: #{str}"
206-
end
207-
208200
def parse_options
209201
options = {}
210202

lib/git/pkgs/models/commit.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ def self.find_or_create_from_rugged(rugged_commit)
2020
end
2121
end
2222

23+
def self.find_or_create_from_repo(repo, sha)
24+
commit = find_by(sha: sha) || where("sha LIKE ?", "#{sha}%").first
25+
return commit if commit
26+
27+
rugged_commit = repo.lookup(sha)
28+
return nil unless rugged_commit
29+
30+
create!(
31+
sha: rugged_commit.oid,
32+
message: rugged_commit.message,
33+
author_name: rugged_commit.author[:name],
34+
author_email: rugged_commit.author[:email],
35+
committed_at: rugged_commit.time,
36+
has_dependency_changes: false
37+
)
38+
rescue Rugged::OdbError
39+
nil
40+
end
41+
2342
def short_sha
2443
sha[0, 7]
2544
end

lib/git/pkgs/output.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
# frozen_string_literal: true
22

33
require "json"
4+
require "time"
45
require_relative "pager"
56

67
module Git
78
module Pkgs
89
module Output
910
include Pager
11+
12+
def parse_time(str)
13+
Time.parse(str)
14+
rescue ArgumentError
15+
error "Invalid date format: #{str}"
16+
end
1017
# Print error message and exit with code 1.
1118
# Use for user errors (bad input, invalid refs) and system errors (db missing).
1219
# When format is :json, outputs JSON to stdout; otherwise outputs text to stderr.

test/git/pkgs/test_cli.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def teardown
7070
cleanup_test_repo
7171
end
7272

73-
def test_find_or_create_commit_finds_existing_commit
73+
def test_find_or_create_from_repo_finds_existing_commit
7474
repo = Git::Pkgs::Repository.new(@test_dir)
7575
sha = repo.head_sha
7676

@@ -83,34 +83,31 @@ def test_find_or_create_commit_finds_existing_commit
8383
committed_at: Time.now
8484
)
8585

86-
diff = Git::Pkgs::Commands::Diff.new([])
87-
result = diff.send(:find_or_create_commit, repo, sha)
86+
result = Git::Pkgs::Models::Commit.find_or_create_from_repo(repo, sha)
8887

8988
assert result
9089
assert_equal sha, result.sha
9190
end
9291

93-
def test_find_or_create_commit_creates_missing_commit
92+
def test_find_or_create_from_repo_creates_missing_commit
9493
repo = Git::Pkgs::Repository.new(@test_dir)
9594
sha = repo.head_sha
9695

9796
# Commit doesn't exist in database yet
9897
assert_nil Git::Pkgs::Models::Commit.find_by(sha: sha)
9998

100-
diff = Git::Pkgs::Commands::Diff.new([])
101-
result = diff.send(:find_or_create_commit, repo, sha)
99+
result = Git::Pkgs::Models::Commit.find_or_create_from_repo(repo, sha)
102100

103101
assert result
104102
assert_equal sha, result.sha
105103
# Verify it was persisted
106104
assert Git::Pkgs::Models::Commit.find_by(sha: sha)
107105
end
108106

109-
def test_find_or_create_commit_returns_nil_for_invalid_sha
107+
def test_find_or_create_from_repo_returns_nil_for_invalid_sha
110108
repo = Git::Pkgs::Repository.new(@test_dir)
111109

112-
diff = Git::Pkgs::Commands::Diff.new([])
113-
result = diff.send(:find_or_create_commit, repo, "0000000000000000000000000000000000000000")
110+
result = Git::Pkgs::Models::Commit.find_or_create_from_repo(repo, "0000000000000000000000000000000000000000")
114111

115112
assert_nil result
116113
end

0 commit comments

Comments
 (0)