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 pathlist.rb
More file actions
217 lines (179 loc) · 7.06 KB
/
list.rb
File metadata and controls
217 lines (179 loc) · 7.06 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class List
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
use_stateless = @options[:stateless] || !Database.exists?(repo.git_dir)
if use_stateless
deps = run_stateless(repo)
else
deps = run_with_database(repo)
end
# Apply filters
if @options[:manifest]
deps = deps.select { |d| d[:manifest_path] == @options[:manifest] }
end
if @options[:ecosystem]
deps = deps.select { |d| d[:ecosystem] == @options[:ecosystem] }
end
if @options[:type]
deps = deps.select { |d| d[:dependency_type] == @options[:type] }
end
if deps.empty?
empty_result "No dependencies found"
return
end
locked_versions = build_locked_versions(deps)
if @options[:format] == "json"
require "json"
deps_with_locked = deps.map do |dep|
if dep[:kind] == "manifest"
locked = locked_versions[[dep[:ecosystem], dep[:name]]]
locked ? dep.merge(locked_version: locked) : dep
else
dep
end
end
puts JSON.pretty_generate(deps_with_locked)
else
paginate { output_text(deps, locked_versions) }
end
end
def run_stateless(repo)
commit_sha = @options[:commit] || repo.head_sha
rugged_commit = repo.lookup(repo.rev_parse(commit_sha))
error "Could not resolve '#{commit_sha}'. Check that the ref exists." unless rugged_commit
analyzer = Analyzer.new(repo)
analyzer.dependencies_at_commit(rugged_commit)
end
def run_with_database(repo)
Database.connect(repo.git_dir)
commit_sha = @options[:commit] || repo.head_sha
target_commit = Models::Commit.first(sha: commit_sha)
error "Commit #{commit_sha[0, 7]} not in database. Run 'git pkgs update' to index new commits." unless target_commit
compute_dependencies_at_commit(target_commit, repo)
end
def build_locked_versions(deps)
locked_versions = {}
deps.each do |d|
next unless d[:kind] == "lockfile"
locked_versions[[d[:ecosystem], d[:name]]] = d[:requirement]
end
locked_versions
end
def output_text(deps, locked_versions)
grouped = deps.group_by { |d| [d[:manifest_path], d[:ecosystem]] }
grouped.each do |(path, platform), manifest_deps|
puts "#{path} (#{platform}):"
manifest_deps.sort_by { |d| d[:name] }.each do |dep|
type_suffix = dep[:dependency_type] && dep[:dependency_type] != "runtime" ? " [#{dep[:dependency_type]}]" : ""
locked = locked_versions[[dep[:ecosystem], dep[:name]]] if dep[:kind] == "manifest"
locked_suffix = locked ? " [#{locked}]" : ""
puts " #{dep[:name]} #{dep[:requirement]}#{locked_suffix}#{type_suffix}"
end
puts
end
end
def compute_dependencies_at_commit(target_commit, repo)
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.first(name: branch_name)
return [] unless branch
# Find the nearest snapshot commit before or at target
snapshot_commit = branch.commits_dataset
.join(:dependency_snapshots, commit_id: :id)
.where { Sequel[:commits][:committed_at] <= target_commit.committed_at }
.order(Sequel.desc(Sequel[:commits][:committed_at]))
.distinct
.first
# Build initial state from snapshot
deps = {}
if snapshot_commit
snapshot_commit.dependency_snapshots.each do |s|
key = [s.manifest.path, s.name]
deps[key] = {
manifest_path: s.manifest.path,
name: s.name,
ecosystem: s.ecosystem,
kind: s.manifest.kind,
requirement: s.requirement,
dependency_type: s.dependency_type
}
end
end
# Replay changes from snapshot to target
if snapshot_commit && snapshot_commit.id != target_commit.id
commit_ids = branch.commits_dataset.select_map(Sequel[:commits][:id])
changes = Models::DependencyChange
.join(:commits, id: :commit_id)
.where(Sequel[:commits][:id] => commit_ids)
.where { Sequel[:commits][:committed_at] > snapshot_commit.committed_at }
.where { Sequel[:commits][:committed_at] <= target_commit.committed_at }
.order(Sequel[:commits][:committed_at])
.eager(:manifest)
.all
changes.each do |change|
key = [change.manifest.path, change.name]
case change.change_type
when "added", "modified"
deps[key] = {
manifest_path: change.manifest.path,
name: change.name,
ecosystem: change.ecosystem,
kind: change.manifest.kind,
requirement: change.requirement,
dependency_type: change.dependency_type
}
when "removed"
deps.delete(key)
end
end
end
deps.values
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs list [options]"
opts.on("-c", "--commit=SHA", "Show dependencies at specific commit") do |v|
options[:commit] = v
end
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem (npm, rubygems, etc.)") do |v|
options[:ecosystem] = v
end
opts.on("-m", "--manifest=PATH", "Filter by manifest path") do |v|
options[:manifest] = v
end
opts.on("-t", "--type=TYPE", "Filter by dependency type") do |v|
options[:type] = v
end
opts.on("-b", "--branch=NAME", "Branch context for finding snapshots") do |v|
options[:branch] = v
end
opts.on("-f", "--format=FORMAT", "Output format (text, json)") do |v|
options[:format] = v
end
opts.on("--no-pager", "Do not pipe output into a pager") do
options[:no_pager] = true
end
opts.on("--stateless", "Parse manifests directly without database") do
options[:stateless] = true
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end