This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.rb
More file actions
280 lines (229 loc) · 9.48 KB
/
init.rb
File metadata and controls
280 lines (229 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Init
include Output
DEFAULT_BATCH_SIZE = 500
DEFAULT_SNAPSHOT_INTERVAL = 50
def batch_size
Git::Pkgs.batch_size || DEFAULT_BATCH_SIZE
end
def snapshot_interval
Git::Pkgs.snapshot_interval || DEFAULT_SNAPSHOT_INTERVAL
end
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
branch_name = @options[:branch] || repo.default_branch
error "Branch '#{branch_name}' not found. Check 'git branch -a' for available branches." unless repo.branch_exists?(branch_name)
if Database.exists?(repo.git_dir) && !@options[:force]
info "Database already exists. Use --force to rebuild."
return
end
Database.drop if @options[:force]
Database.connect(repo.git_dir, check_version: false)
Database.create_schema(with_indexes: false)
Database.optimize_for_bulk_writes
branch = Models::Branch.find_or_create(branch_name)
analyzer = Analyzer.new(repo)
print "Analyzing #{branch_name}..." unless Git::Pkgs.quiet
walker = repo.walk(branch_name, @options[:since])
commits = walker.to_a
total = commits.size
repo.prefetch_blob_paths(commits)
stats = bulk_process_commits(commits, branch, analyzer, total)
branch.update(last_analyzed_sha: repo.branch_target(branch_name))
Database.create_bulk_indexes
Database.optimize_for_reads
info "\r#{' ' * 40}\rAnalyzed #{branch_name}: #{total} commits (#{stats[:dependency_commits]} with dependency changes)"
unless @options[:no_hooks]
Commands::Hooks.new([]).install_hooks(repo, quiet: true)
end
end
def bulk_process_commits(commits, branch, analyzer, total)
now = Time.now
snapshot = {}
manifests_cache = {}
pending_commits = []
pending_branch_commits = []
pending_changes = []
pending_snapshots = []
dependency_commit_count = 0
snapshots_stored = 0
processed = 0
last_processed_sha = nil
flush = lambda do
return if pending_commits.empty?
Database.db.transaction do
Models::Commit.dataset.multi_insert(pending_commits) if pending_commits.any?
commit_ids = Models::Commit
.where(sha: pending_commits.map { |c| c[:sha] })
.select_hash(:sha, :id)
if pending_branch_commits.any?
branch_commit_records = pending_branch_commits.map do |bc|
{ branch_id: bc[:branch_id], commit_id: commit_ids[bc[:sha]], position: bc[:position] }
end
Models::BranchCommit.dataset.multi_insert(branch_commit_records)
end
if pending_changes.any?
manifest_ids = Models::Manifest.select_hash(:path, :id)
change_records = pending_changes.map do |c|
{
commit_id: commit_ids[c[:sha]],
manifest_id: manifest_ids[c[:manifest_path]],
name: c[:name],
ecosystem: c[:ecosystem],
purl: c[:purl],
change_type: c[:change_type],
requirement: c[:requirement],
previous_requirement: c[:previous_requirement],
dependency_type: c[:dependency_type],
created_at: now,
updated_at: now
}
end
Models::DependencyChange.dataset.multi_insert(change_records)
end
if pending_snapshots.any?
manifest_ids ||= Models::Manifest.select_hash(:path, :id)
snapshot_records = pending_snapshots.map do |s|
{
commit_id: commit_ids[s[:sha]],
manifest_id: manifest_ids[s[:manifest_path]],
name: s[:name],
ecosystem: s[:ecosystem],
purl: s[:purl],
requirement: s[:requirement],
dependency_type: s[:dependency_type],
integrity: s[:integrity],
created_at: now,
updated_at: now
}
end
Models::DependencySnapshot.dataset.multi_insert(snapshot_records)
end
end
pending_commits.clear
pending_branch_commits.clear
pending_changes.clear
pending_snapshots.clear
end
progress_interval = [total / 100, 10].max
commits.each do |rugged_commit|
processed += 1
print "\rProcessing commit #{processed}/#{total}..." if !Git::Pkgs.quiet && (processed % progress_interval == 0 || processed == total)
next if rugged_commit.parents.length > 1 # skip merge commits
result = analyzer.analyze_commit(rugged_commit, snapshot)
has_changes = result && result[:changes].any?
pending_commits << {
sha: rugged_commit.oid,
message: rugged_commit.message,
author_name: rugged_commit.author[:name],
author_email: rugged_commit.author[:email],
committed_at: rugged_commit.time,
has_dependency_changes: has_changes,
created_at: now,
updated_at: now
}
pending_branch_commits << {
branch_id: branch.id,
sha: rugged_commit.oid,
position: processed
}
last_processed_sha = rugged_commit.oid
if has_changes
dependency_commit_count += 1
result[:changes].each do |change|
manifest_key = change[:manifest_path]
unless manifests_cache[manifest_key]
manifests_cache[manifest_key] = Models::Manifest.find_or_create(
path: change[:manifest_path],
ecosystem: change[:ecosystem],
kind: change[:kind]
)
end
pending_changes << {
sha: rugged_commit.oid,
manifest_path: manifest_key,
name: change[:name],
ecosystem: change[:ecosystem],
purl: change[:purl],
change_type: change[:change_type],
requirement: change[:requirement],
previous_requirement: change[:previous_requirement],
dependency_type: change[:dependency_type]
}
end
snapshot = result[:snapshot]
# Store snapshot at intervals
if dependency_commit_count % snapshot_interval == 0
snapshot.each do |(manifest_path, name), dep_info|
pending_snapshots << {
sha: rugged_commit.oid,
manifest_path: manifest_path,
name: name,
ecosystem: dep_info[:ecosystem],
purl: dep_info[:purl],
requirement: dep_info[:requirement],
dependency_type: dep_info[:dependency_type],
integrity: dep_info[:integrity]
}
end
snapshots_stored += snapshot.size
end
end
flush.call if pending_commits.size >= batch_size
end
# Always store final snapshot for the last processed commit
if snapshot.any? && last_processed_sha
unless pending_snapshots.any? { |s| s[:sha] == last_processed_sha }
snapshot.each do |(manifest_path, name), dep_info|
pending_snapshots << {
sha: last_processed_sha,
manifest_path: manifest_path,
name: name,
ecosystem: dep_info[:ecosystem],
purl: dep_info[:purl],
requirement: dep_info[:requirement],
dependency_type: dep_info[:dependency_type],
integrity: dep_info[:integrity]
}
end
snapshots_stored += snapshot.size
end
end
flush.call
{ dependency_commits: dependency_commit_count, snapshots_stored: snapshots_stored }
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs init [options]"
opts.on("-b", "--branch=NAME", "Branch to analyze (default: default branch)") do |v|
options[:branch] = v
end
opts.on("-s", "--since=SHA", "Start from specific commit") do |v|
options[:since] = v
end
opts.on("-f", "--force", "Rebuild database from scratch") do
options[:force] = true
end
opts.on("--no-hooks", "Skip installing git hooks") do
options[:no_hooks] = true
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end