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 pathanalyzer.rb
More file actions
434 lines (365 loc) · 15.5 KB
/
analyzer.rb
File metadata and controls
434 lines (365 loc) · 15.5 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# frozen_string_literal: true
require "bibliothecary"
module Git
module Pkgs
class Analyzer
attr_reader :repository
# Common manifest file patterns for quick pre-filtering
# This avoids calling Bibliothecary.identify_manifests for commits that clearly don't touch manifests
QUICK_MANIFEST_PATTERNS = %w[
Gemfile Gemfile.lock gems.rb gems.locked *.gemspec
package.json package-lock.json yarn.lock npm-shrinkwrap.json pnpm-lock.yaml bun.lock npm-ls.json
setup.py req*.txt req*.pip requirements/*.txt requirements/*.pip requirements*.in requirements.frozen
Pipfile Pipfile.lock pyproject.toml poetry.lock uv.lock pylock.toml pdm.lock
pip-resolved-dependencies.txt pip-dependency-graph.json
pom.xml ivy.xml build.gradle build.gradle.kts gradle-dependencies-q.txt gradle.lockfile verification-metadata.xml
maven-resolved-dependencies.txt sbt-update-full.txt maven-dependency-tree.txt maven-dependency-tree.dot
Cargo.toml Cargo.lock
go.mod go.sum glide.yaml glide.lock Godeps Godeps/Godeps.json
vendor/manifest vendor/vendor.json Gopkg.toml Gopkg.lock go-resolved-dependencies.json
composer.json composer.lock
Podfile Podfile.lock *.podspec *.podspec.json
packages.config packages.lock.json Project.json Project.lock.json
*.nuspec paket.lock *.csproj project.assets.json *.deps.json
bower.json bentofile.yaml
META.json META.yml cpanfile cpanfile.snapshot Makefile.PL Build.PL
environment.yml environment.yaml
cog.yaml versions.json MLmodel DESCRIPTION renv.lock
pubspec.yaml pubspec.lock
dub.json dub.sdl
REQUIRE Project.toml Manifest.toml
shard.yml shard.lock
elm-package.json elm_dependencies.json elm-stuff/exact-dependencies.json
haxelib.json stack.yaml stack.yaml.lock
action.yml action.yaml .github/workflows/*.yml .github/workflows/*.yaml
Dockerfile docker-compose*.yml docker-compose*.yaml
dvc.yaml vcpkg.json _generated-vcpkg-list.json
Brewfile Brewfile.lock.json
Modelfile
deno.json deno.jsonc deno.lock
conanfile.py conanfile.txt conan.lock
*.rockspec
*.nimble
*.cabal *cabal.config stack.yaml.lock cabal.project.freeze
Cartfile Cartfile.private Cartfile.resolved
project.clj
Package.swift Package.resolved
mix.exs mix.lock gleam.toml manifest.toml rebar.lock
flake.nix flake.lock nix/sources.json npins/sources.json
].freeze
QUICK_MANIFEST_REGEX = Regexp.union(
QUICK_MANIFEST_PATTERNS.map do |pattern|
if pattern.include?('*')
Regexp.new(Regexp.escape(pattern).gsub('\\*', '.*'))
else
/(?:^|\/)#{Regexp.escape(pattern)}$/
end
end
).freeze
def initialize(repository)
@repository = repository
@blob_cache = {}
@manifest_path_cache = {}
Config.configure_bibliothecary
end
def generate_purl(ecosystem, name)
Ecosystems.generate_purl(ecosystem, name)
end
# Quick check if any paths might be manifests (fast regex check)
def might_have_manifests?(paths)
paths.any? { |p| p.match?(QUICK_MANIFEST_REGEX) }
end
# Cached version of Bibliothecary.identify_manifests
def identify_manifests_cached(paths)
uncached = paths.reject { |p| @manifest_path_cache.key?(p) }
if uncached.any?
# Call Bibliothecary only for uncached paths
manifests = Bibliothecary.identify_manifests(uncached)
manifest_set = manifests.to_set
uncached.each do |path|
@manifest_path_cache[path] = manifest_set.include?(path)
end
end
paths.select { |p| @manifest_path_cache[p] }
end
# Quick check if a commit touches any manifest files
def has_manifest_changes?(rugged_commit)
return false if repository.merge_commit?(rugged_commit)
blob_paths = repository.blob_paths(rugged_commit)
all_paths = blob_paths.map { |p| p[:path] }
return false unless might_have_manifests?(all_paths)
identify_manifests_cached(all_paths).any?
end
def analyze_commit(rugged_commit, previous_snapshot = {})
return nil if repository.merge_commit?(rugged_commit)
blob_paths = repository.blob_paths(rugged_commit)
added_paths = blob_paths.select { |p| p[:status] == :added }.map { |p| p[:path] }
modified_paths = blob_paths.select { |p| p[:status] == :modified }.map { |p| p[:path] }
removed_paths = blob_paths.select { |p| p[:status] == :deleted }.map { |p| p[:path] }
all_paths = added_paths + modified_paths + removed_paths
return nil unless might_have_manifests?(all_paths)
added_manifests = identify_manifests_cached(added_paths)
modified_manifests = identify_manifests_cached(modified_paths)
removed_manifests = identify_manifests_cached(removed_paths)
return nil if added_manifests.empty? && modified_manifests.empty? && removed_manifests.empty?
changes = []
new_snapshot = previous_snapshot.dup
# Process added manifest files
added_manifests.each do |manifest_path|
result = parse_manifest_at_commit(rugged_commit, manifest_path)
next unless result && result[:dependencies]
result[:dependencies].each do |dep|
changes << {
manifest_path: manifest_path,
ecosystem: result[:platform],
kind: result[:kind],
name: dep[:name],
purl: generate_purl(result[:platform], dep[:name]),
change_type: "added",
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
key = [manifest_path, dep[:name]]
new_snapshot[key] = {
ecosystem: result[:platform],
kind: result[:kind],
purl: generate_purl(result[:platform], dep[:name]),
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
end
end
# Process modified manifest files
modified_manifests.each do |manifest_path|
before_result = parse_manifest_before_commit(rugged_commit, manifest_path)
after_result = parse_manifest_at_commit(rugged_commit, manifest_path)
next unless after_result
before_deps = (before_result&.dig(:dependencies) || []).map { |d| [d[:name], d] }.to_h
after_deps = (after_result[:dependencies] || []).map { |d| [d[:name], d] }.to_h
added_names = after_deps.keys - before_deps.keys
removed_names = before_deps.keys - after_deps.keys
common_names = after_deps.keys & before_deps.keys
added_names.each do |name|
dep = after_deps[name]
changes << {
manifest_path: manifest_path,
ecosystem: after_result[:platform],
kind: after_result[:kind],
name: name,
purl: generate_purl(after_result[:platform], name),
change_type: "added",
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
key = [manifest_path, name]
new_snapshot[key] = {
ecosystem: after_result[:platform],
kind: after_result[:kind],
purl: generate_purl(after_result[:platform], name),
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
end
removed_names.each do |name|
dep = before_deps[name]
changes << {
manifest_path: manifest_path,
ecosystem: before_result[:platform],
kind: before_result[:kind],
name: name,
purl: generate_purl(before_result[:platform], name),
change_type: "removed",
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
key = [manifest_path, name]
new_snapshot.delete(key)
end
common_names.each do |name|
before_dep = before_deps[name]
after_dep = after_deps[name]
if before_dep[:requirement] != after_dep[:requirement] || before_dep[:type] != after_dep[:type]
changes << {
manifest_path: manifest_path,
ecosystem: after_result[:platform],
kind: after_result[:kind],
name: name,
purl: generate_purl(after_result[:platform], name),
change_type: "modified",
requirement: after_dep[:requirement],
previous_requirement: before_dep[:requirement],
dependency_type: after_dep[:type],
integrity: after_dep[:integrity]
}
key = [manifest_path, name]
new_snapshot[key] = {
ecosystem: after_result[:platform],
kind: after_result[:kind],
purl: generate_purl(after_result[:platform], name),
requirement: after_dep[:requirement],
dependency_type: after_dep[:type],
integrity: after_dep[:integrity]
}
end
end
end
# Process removed manifest files
removed_manifests.each do |manifest_path|
result = parse_manifest_before_commit(rugged_commit, manifest_path)
next unless result && result[:dependencies]
result[:dependencies].each do |dep|
changes << {
manifest_path: manifest_path,
ecosystem: result[:platform],
kind: result[:kind],
name: dep[:name],
purl: generate_purl(result[:platform], dep[:name]),
change_type: "removed",
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
key = [manifest_path, dep[:name]]
new_snapshot.delete(key)
end
end
{
changes: changes,
snapshot: new_snapshot
}
end
# Cache stats for debugging
def cache_stats
blob_hits = @blob_cache.values.count { |v| v[:hits] > 0 }
blob_total = @blob_cache.size
manifest_paths = @manifest_path_cache.size
{
cached_blobs: blob_total,
blobs_with_hits: blob_hits,
cached_paths: manifest_paths
}
end
def parse_manifest_at_commit(rugged_commit, manifest_path)
blob_oid = repository.blob_oid_at_commit(rugged_commit, manifest_path)
return nil unless blob_oid
parse_manifest_by_oid(blob_oid, manifest_path)
end
def parse_manifest_before_commit(rugged_commit, manifest_path)
return nil if rugged_commit.parents.empty?
blob_oid = repository.blob_oid_at_commit(rugged_commit.parents[0], manifest_path)
return nil unless blob_oid
parse_manifest_by_oid(blob_oid, manifest_path)
end
def parse_manifest_by_oid(blob_oid, manifest_path)
cache_key = [blob_oid, manifest_path]
if @blob_cache.key?(cache_key)
@blob_cache[cache_key][:hits] += 1
return @blob_cache[cache_key][:result]
end
content = repository.blob_content(blob_oid)
return nil unless content
result = Bibliothecary.analyse_file(manifest_path, content).first
result = nil if result && Config.filter_ecosystem?(result[:platform])
@blob_cache[cache_key] = { result: result, hits: 0 }
result
end
# Parse all manifest files at a given commit (stateless)
def dependencies_at_commit(rugged_commit)
deps = []
manifest_paths = find_manifest_paths_in_tree(rugged_commit.tree)
manifest_paths.each do |path|
result = parse_manifest_at_commit(rugged_commit, path)
next unless result && result[:dependencies]
result[:dependencies].each do |dep|
deps << {
manifest_path: path,
manifest_kind: result[:kind],
name: dep[:name],
ecosystem: result[:platform],
kind: result[:kind],
purl: generate_purl(result[:platform], dep[:name]),
requirement: dep[:requirement],
dependency_type: dep[:type],
integrity: dep[:integrity]
}
end
end
deps
end
# Compute changes between two commits (stateless)
def diff_commits(from_commit, to_commit)
from_deps = dependencies_at_commit(from_commit).group_by { |d| [d[:manifest_path], d[:name]] }
to_deps = dependencies_at_commit(to_commit).group_by { |d| [d[:manifest_path], d[:name]] }
added = []
modified = []
removed = []
# Find added and modified
to_deps.each do |key, to_list|
to_dep = to_list.first
if from_deps[key]
from_dep = from_deps[key].first
if from_dep[:requirement] != to_dep[:requirement]
modified << to_dep.merge(previous_requirement: from_dep[:requirement])
end
else
added << to_dep
end
end
# Find removed
from_deps.each do |key, from_list|
removed << from_list.first unless to_deps[key]
end
{ added: added, modified: modified, removed: removed }
end
def find_manifest_paths_in_tree(tree, prefix = "")
paths = []
tree.each do |entry|
full_path = prefix.empty? ? entry[:name] : "#{prefix}/#{entry[:name]}"
if entry[:type] == :tree
subtree = repository.lookup(entry[:oid])
paths.concat(find_manifest_paths_in_tree(subtree, full_path))
elsif entry[:type] == :blob && full_path.match?(QUICK_MANIFEST_REGEX)
paths << full_path
end
end
identify_manifests_cached(paths)
end
def lookup(oid)
repository.lookup(oid)
end
# Pair manifest dependencies with their corresponding lockfile versions.
# Groups by directory + ecosystem + name, preferring lockfile over manifest.
# Can be called as instance method or class method.
def pair_manifests_with_lockfiles(deps)
self.class.pair_manifests_with_lockfiles(deps)
end
def self.pair_manifests_with_lockfiles(deps)
# Group by (directory, ecosystem, name)
groups = {}
deps.each do |dep|
dir = File.dirname(dep[:manifest_path])
dir = "" if dir == "."
key = [dir, dep[:ecosystem], dep[:name]]
groups[key] ||= []
groups[key] << dep
end
# For each group, pick the best entry (lockfile preferred)
groups.values.map do |group_deps|
lockfile_dep = group_deps.find { |d| d[:manifest_kind] == "lockfile" }
manifest_dep = group_deps.find { |d| d[:manifest_kind] == "manifest" }
# Prefer lockfile version, fall back to manifest
lockfile_dep || manifest_dep || group_deps.first
end.compact
end
# Filter to only lockfile dependencies
def self.lockfile_dependencies(deps)
deps.select { |d| d[:manifest_kind] == "lockfile" }
end
end
end
end