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
162 lines (133 loc) · 4.94 KB
/
list.rb
File metadata and controls
162 lines (133 loc) · 4.94 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
# 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
require_database(repo)
Database.connect(repo.git_dir)
commit_sha = @options[:commit] || repo.head_sha
target_commit = Models::Commit.find_by(sha: commit_sha)
error "Commit #{commit_sha[0, 7]} not found in database" unless target_commit
deps = compute_dependencies_at_commit(target_commit, repo)
if deps.empty?
empty_result "No dependencies found"
return
end
# Apply filters
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 @options[:format] == "json"
require "json"
puts JSON.pretty_generate(deps)
else
paginate { output_text(deps) }
end
end
def output_text(deps)
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]}]" : ""
puts " #{dep[:name]} #{dep[:requirement]}#{type_suffix}"
end
puts
end
end
def compute_dependencies_at_commit(target_commit, repo)
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.find_by(name: branch_name)
return [] unless branch
# Find the nearest snapshot commit before or at target
snapshot_commit = branch.commits
.joins(:dependency_snapshots)
.where("commits.committed_at <= ?", target_commit.committed_at)
.order(committed_at: :desc)
.distinct
.first
# Build initial state from snapshot
deps = {}
if snapshot_commit
snapshot_commit.dependency_snapshots.includes(:manifest).each do |s|
key = [s.manifest.path, s.name]
deps[key] = {
manifest_path: s.manifest.path,
name: s.name,
ecosystem: s.ecosystem,
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
changes = Models::DependencyChange
.joins(:commit)
.where(commits: { id: branch.commit_ids })
.where("commits.committed_at > ? AND commits.committed_at <= ?",
snapshot_commit.committed_at, target_commit.committed_at)
.order("commits.committed_at ASC")
.includes(:manifest)
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,
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("-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("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end