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 pathshow.rb
More file actions
265 lines (222 loc) · 8.56 KB
/
Copy pathshow.rb
File metadata and controls
265 lines (222 loc) · 8.56 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Show
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
ref = @args.shift || "HEAD"
repo = Repository.new
use_stateless = @options[:stateless] || !Database.exists?(repo.git_dir)
sha = repo.rev_parse(ref)
error "Could not resolve '#{ref}'. Check that the ref exists with 'git rev-parse #{ref}'." unless sha
if use_stateless
run_stateless(repo, sha)
else
run_with_database(repo, sha)
end
end
def run_stateless(repo, sha)
rugged_commit = repo.lookup(sha)
analyzer = Analyzer.new(repo)
if rugged_commit.parents.empty?
# First commit - all deps are "added"
deps = analyzer.dependencies_at_commit(rugged_commit)
changes = deps.map do |dep|
{
name: dep[:name],
change_type: "added",
requirement: dep[:requirement],
ecosystem: dep[:ecosystem],
manifest_path: dep[:manifest_path]
}
end
else
diff = analyzer.diff_commits(rugged_commit.parents[0], rugged_commit)
changes = []
diff[:added].each { |d| changes << d.merge(change_type: "added") }
diff[:modified].each { |d| changes << d.merge(change_type: "modified") }
diff[:removed].each { |d| changes << d.merge(change_type: "removed") }
end
if @options[:ecosystem]
changes = changes.select { |c| c[:ecosystem] == @options[:ecosystem] }
end
commit_info = {
sha: sha,
short_sha: sha[0..7],
message: rugged_commit.message,
author_name: rugged_commit.author[:name],
author_email: rugged_commit.author[:email],
committed_at: rugged_commit.time
}
if changes.empty?
empty_result "No dependency changes in #{commit_info[:short_sha]}"
return
end
if @options[:format] == "json"
output_json_stateless(commit_info, changes)
else
paginate { output_text_stateless(commit_info, changes) }
end
end
def run_with_database(repo, sha)
Database.connect(repo.git_dir)
commit = Models::Commit.find_or_create_from_repo(repo, sha)
error "Commit '#{sha[0..7]}' not in database. Run 'git pkgs update' to index new commits." unless commit
changes = Models::DependencyChange
.eager(:commit, :manifest)
.where(commit_id: commit.id)
if @options[:ecosystem]
changes = changes.where(ecosystem: @options[:ecosystem])
end
changes = changes.all
if changes.empty?
empty_result "No dependency changes in #{commit.short_sha}"
return
end
if @options[:format] == "json"
output_json(commit, changes)
else
paginate { output_text(commit, changes) }
end
end
def output_text(commit, changes)
puts "Commit: #{commit.short_sha} #{commit.message&.lines&.first&.strip}"
puts "Author: #{commit.author_name} <#{commit.author_email}>"
puts "Date: #{commit.committed_at.strftime("%Y-%m-%d")}"
puts
added = changes.select { |c| c.change_type == "added" }
modified = changes.select { |c| c.change_type == "modified" }
removed = changes.select { |c| c.change_type == "removed" }
if added.any?
puts Color.green("Added:")
added.each do |change|
puts Color.green(" + #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
end
puts
end
if modified.any?
puts Color.yellow("Modified:")
modified.each do |change|
puts Color.yellow(" ~ #{change.name} #{change.previous_requirement} -> #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
end
puts
end
if removed.any?
puts Color.red("Removed:")
removed.each do |change|
puts Color.red(" - #{change.name} #{change.requirement} (#{change.ecosystem}, #{change.manifest.path})")
end
puts
end
end
def output_json(commit, changes)
require "json"
data = {
commit: {
sha: commit.sha,
short_sha: commit.short_sha,
message: commit.message&.lines&.first&.strip,
author_name: commit.author_name,
author_email: commit.author_email,
date: commit.committed_at.iso8601
},
changes: changes.map do |change|
{
name: change.name,
change_type: change.change_type,
requirement: change.requirement,
previous_requirement: change.previous_requirement,
ecosystem: change.ecosystem,
manifest: change.manifest.path
}
end
}
puts JSON.pretty_generate(data)
end
def output_text_stateless(commit_info, changes)
puts "Commit: #{commit_info[:short_sha]} #{commit_info[:message]&.lines&.first&.strip}"
puts "Author: #{commit_info[:author_name]} <#{commit_info[:author_email]}>"
puts "Date: #{commit_info[:committed_at].strftime("%Y-%m-%d")}"
puts
added = changes.select { |c| c[:change_type] == "added" }
modified = changes.select { |c| c[:change_type] == "modified" }
removed = changes.select { |c| c[:change_type] == "removed" }
if added.any?
puts Color.green("Added:")
added.each do |change|
puts Color.green(" + #{change[:name]} #{change[:requirement]} (#{change[:ecosystem]}, #{change[:manifest_path]})")
end
puts
end
if modified.any?
puts Color.yellow("Modified:")
modified.each do |change|
puts Color.yellow(" ~ #{change[:name]} #{change[:previous_requirement]} -> #{change[:requirement]} (#{change[:ecosystem]}, #{change[:manifest_path]})")
end
puts
end
if removed.any?
puts Color.red("Removed:")
removed.each do |change|
puts Color.red(" - #{change[:name]} #{change[:requirement]} (#{change[:ecosystem]}, #{change[:manifest_path]})")
end
puts
end
end
def output_json_stateless(commit_info, changes)
require "json"
data = {
commit: {
sha: commit_info[:sha],
short_sha: commit_info[:short_sha],
message: commit_info[:message]&.lines&.first&.strip,
author_name: commit_info[:author_name],
author_email: commit_info[:author_email],
date: commit_info[:committed_at].iso8601
},
changes: changes.map do |change|
{
name: change[:name],
change_type: change[:change_type],
requirement: change[:requirement],
previous_requirement: change[:previous_requirement],
ecosystem: change[:ecosystem],
manifest: change[:manifest_path]
}
end
}
puts JSON.pretty_generate(data)
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs show [commit] [options]"
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v|
options[:ecosystem] = 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