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 pathtree.rb
More file actions
172 lines (137 loc) · 5.08 KB
/
Copy pathtree.rb
File metadata and controls
172 lines (137 loc) · 5.08 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Tree
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
# Get the commit to analyze
commit_sha = @options[:commit] || repo.head_sha
commit = find_commit_with_snapshot(commit_sha, repo)
error "No dependency data for commit #{commit_sha[0, 7]}. Run 'git pkgs update' to index new commits." unless commit
# Get current snapshots
snapshots = commit.dependency_snapshots_dataset.eager(:manifest)
if @options[:ecosystem]
snapshots = snapshots.where(ecosystem: @options[:ecosystem])
end
snapshots_list = snapshots.all
if snapshots_list.empty?
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate({ manifests: [], total: 0 })
else
empty_result "No dependencies found"
end
return
end
# Group by manifest and build tree
grouped = snapshots_list.group_by { |s| s.manifest }
if @options[:format] == "json"
output_json(grouped, snapshots_list)
else
paginate { output_text(grouped, snapshots_list) }
end
end
def output_text(grouped, snapshots)
grouped.each do |manifest, deps|
puts "#{manifest.path} (#{manifest.ecosystem})"
puts
# Separate by dependency type
by_type = deps.group_by { |d| d.dependency_type || "runtime" }
by_type.each do |type, type_deps|
puts " [#{type}]"
type_deps.sort_by(&:name).each do |dep|
print_dependency(dep, 2)
end
puts
end
end
# Show summary
puts "Total: #{snapshots.count} dependencies across #{grouped.keys.count} manifest(s)"
end
def output_json(grouped, snapshots)
require "json"
manifests = grouped.map do |manifest, deps|
by_type = deps.group_by { |d| d.dependency_type || "runtime" }
{
path: manifest.path,
ecosystem: manifest.ecosystem,
dependencies: by_type.transform_values do |type_deps|
type_deps.sort_by(&:name).map do |dep|
{
name: dep.name,
requirement: dep.requirement || "*"
}
end
end
}
end
data = {
manifests: manifests,
total: snapshots.count,
manifest_count: grouped.keys.count
}
puts JSON.pretty_generate(data)
end
def print_dependency(dep, indent)
prefix = " " * indent
version = dep.requirement || "*"
puts "#{prefix}#{dep.name} #{version}"
# If this manifest has lockfile data, we could show transitive deps
# For now, we just show the direct dependencies
# Future enhancement: parse lockfiles to show full tree
end
def find_commit_with_snapshot(sha, repo)
commit = Models::Commit.first(sha: sha) ||
Models::Commit.where(Sequel.like(:sha, "#{sha}%")).first
return commit if commit&.dependency_snapshots&.any?
# Find most recent commit with a snapshot
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.first(name: branch_name)
return nil unless branch
target_time = commit&.committed_at || Time.now
branch.commits_dataset
.join(:dependency_snapshots, commit_id: :id)
.where { Sequel[:commits][:committed_at] <= target_time }
.order(Sequel.desc(Sequel[:commits][:committed_at]))
.distinct
.first
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs tree [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") do |v|
options[:ecosystem] = 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