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 pathdiff.rb
More file actions
335 lines (279 loc) · 11.8 KB
/
diff.rb
File metadata and controls
335 lines (279 loc) · 11.8 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Diff
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)
from_ref, to_ref = parse_range_argument
from_ref ||= @options[:from]
to_ref ||= @options[:to] || "HEAD"
error "Usage: git pkgs diff <commit>..<commit> or git pkgs diff --from=REF [--to=REF]" unless from_ref
# Resolve git refs (like HEAD~10) to SHAs
from_sha = repo.rev_parse(from_ref)
to_sha = repo.rev_parse(to_ref)
error "Could not resolve '#{from_ref}'. Check that the ref exists." unless from_sha
error "Could not resolve '#{to_ref}'. Check that the ref exists." unless to_sha
if use_stateless
run_stateless(repo, from_sha, to_sha)
else
run_with_database(repo, from_sha, to_sha)
end
end
def run_stateless(repo, from_sha, to_sha)
from_commit = repo.lookup(from_sha)
to_commit = repo.lookup(to_sha)
analyzer = Analyzer.new(repo)
diff = analyzer.diff_commits(from_commit, to_commit)
if @options[:ecosystem]
diff[:added] = diff[:added].select { |d| d[:ecosystem] == @options[:ecosystem] }
diff[:modified] = diff[:modified].select { |d| d[:ecosystem] == @options[:ecosystem] }
diff[:removed] = diff[:removed].select { |d| d[:ecosystem] == @options[:ecosystem] }
end
if diff[:added].empty? && diff[:modified].empty? && diff[:removed].empty?
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate({ from: from_sha[0..7], to: to_sha[0..7], added: [], modified: [], removed: [] })
else
empty_result "No dependency changes between #{from_sha[0..7]} and #{to_sha[0..7]}"
end
return
end
if @options[:format] == "json"
output_json_stateless(from_sha, to_sha, diff)
else
paginate { output_text_stateless(from_sha, to_sha, diff) }
end
end
def run_with_database(repo, from_sha, to_sha)
Database.connect(repo.git_dir)
from_commit = Models::Commit.find_or_create_from_repo(repo, from_sha)
to_commit = Models::Commit.find_or_create_from_repo(repo, to_sha)
error "Commit '#{from_sha[0..7]}' not in database. Run 'git pkgs update' to index new commits." unless from_commit
error "Commit '#{to_sha[0..7]}' not in database. Run 'git pkgs update' to index new commits." unless to_commit
# Get all changes between the two commits
changes = Models::DependencyChange
.eager(:commit, :manifest)
.join(:commits, id: :commit_id)
.where { Sequel[:commits][:committed_at] > from_commit.committed_at }
.where { Sequel[:commits][:committed_at] <= to_commit.committed_at }
.order(Sequel[:commits][:committed_at])
if @options[:ecosystem]
changes = changes.where(Sequel[:dependency_changes][:ecosystem] => @options[:ecosystem])
end
changes_list = changes.all
if changes_list.empty?
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate({ from: from_commit.short_sha, to: to_commit.short_sha, added: [], modified: [], removed: [] })
else
empty_result "No dependency changes between #{from_commit.short_sha} and #{to_commit.short_sha}"
end
return
end
if @options[:format] == "json"
output_json(from_commit, to_commit, changes_list)
else
paginate { output_text(from_commit, to_commit, changes_list) }
end
end
def output_text(from_commit, to_commit, changes)
puts "Dependency changes from #{from_commit.short_sha} to #{to_commit.short_sha}:"
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.group_by(&:name).each do |name, pkg_changes|
latest = pkg_changes.last
puts Color.green(" + #{name} #{latest.requirement} (#{latest.manifest.path})")
end
puts
end
if modified.any?
puts Color.yellow("Modified:")
modified.group_by(&:name).each do |name, pkg_changes|
first = pkg_changes.first
latest = pkg_changes.last
puts Color.yellow(" ~ #{name} #{first.previous_requirement} -> #{latest.requirement}")
end
puts
end
if removed.any?
puts Color.red("Removed:")
removed.group_by(&:name).each do |name, pkg_changes|
latest = pkg_changes.last
puts Color.red(" - #{name} (was #{latest.requirement})")
end
puts
end
# Summary
added_count = Color.green("+#{added.map(&:name).uniq.count}")
removed_count = Color.red("-#{removed.map(&:name).uniq.count}")
modified_count = Color.yellow("~#{modified.map(&:name).uniq.count}")
puts "Summary: #{added_count} #{removed_count} #{modified_count}"
end
def output_json(from_commit, to_commit, changes)
require "json"
added = changes.select { |c| c.change_type == "added" }
modified = changes.select { |c| c.change_type == "modified" }
removed = changes.select { |c| c.change_type == "removed" }
format_change = lambda do |change|
{
name: change.name,
ecosystem: change.ecosystem,
requirement: change.requirement,
manifest: change.manifest.path,
commit: change.commit.short_sha,
date: change.commit.committed_at.iso8601
}
end
format_modified = lambda do |first, latest|
{
name: first.name,
ecosystem: first.ecosystem,
previous_requirement: first.previous_requirement,
requirement: latest.requirement,
manifest: latest.manifest.path
}
end
data = {
from: from_commit.short_sha,
to: to_commit.short_sha,
added: added.group_by(&:name).map { |_name, pkg_changes| format_change.call(pkg_changes.last) },
modified: modified.group_by(&:name).map { |_name, pkg_changes| format_modified.call(pkg_changes.first, pkg_changes.last) },
removed: removed.group_by(&:name).map { |_name, pkg_changes| format_change.call(pkg_changes.last) },
summary: {
added: added.map(&:name).uniq.count,
modified: modified.map(&:name).uniq.count,
removed: removed.map(&:name).uniq.count
}
}
puts JSON.pretty_generate(data)
end
def output_text_stateless(from_sha, to_sha, diff)
puts "Dependency changes from #{from_sha[0..7]} to #{to_sha[0..7]}:"
puts
if diff[:added].any?
puts Color.green("Added:")
diff[:added].group_by { |d| d[:name] }.each do |name, pkg_changes|
latest = pkg_changes.last
puts Color.green(" + #{name} #{latest[:requirement]} (#{latest[:manifest_path]})")
end
puts
end
if diff[:modified].any?
puts Color.yellow("Modified:")
diff[:modified].group_by { |d| d[:name] }.each do |name, pkg_changes|
latest = pkg_changes.last
puts Color.yellow(" ~ #{name} #{latest[:previous_requirement]} -> #{latest[:requirement]}")
end
puts
end
if diff[:removed].any?
puts Color.red("Removed:")
diff[:removed].group_by { |d| d[:name] }.each do |name, pkg_changes|
latest = pkg_changes.last
puts Color.red(" - #{name} (was #{latest[:requirement]})")
end
puts
end
added_count = Color.green("+#{diff[:added].map { |d| d[:name] }.uniq.count}")
removed_count = Color.red("-#{diff[:removed].map { |d| d[:name] }.uniq.count}")
modified_count = Color.yellow("~#{diff[:modified].map { |d| d[:name] }.uniq.count}")
puts "Summary: #{added_count} #{removed_count} #{modified_count}"
end
def output_json_stateless(from_sha, to_sha, diff)
require "json"
format_change = lambda do |change|
{
name: change[:name],
ecosystem: change[:ecosystem],
requirement: change[:requirement],
manifest: change[:manifest_path]
}
end
format_modified = lambda do |change|
{
name: change[:name],
ecosystem: change[:ecosystem],
previous_requirement: change[:previous_requirement],
requirement: change[:requirement],
manifest: change[:manifest_path]
}
end
data = {
from: from_sha[0..7],
to: to_sha[0..7],
added: diff[:added].map { |c| format_change.call(c) },
modified: diff[:modified].map { |c| format_modified.call(c) },
removed: diff[:removed].map { |c| format_change.call(c) },
summary: {
added: diff[:added].map { |d| d[:name] }.uniq.count,
modified: diff[:modified].map { |d| d[:name] }.uniq.count,
removed: diff[:removed].map { |d| d[:name] }.uniq.count
}
}
puts JSON.pretty_generate(data)
end
def parse_range_argument
return [nil, nil] if @args.empty?
arg = @args.first
return [nil, nil] if arg.start_with?("-")
if arg.include?("..")
@args.shift
parts = arg.split("..", 2)
[parts[0], parts[1].empty? ? "HEAD" : parts[1]]
else
# Single ref means "from that ref to HEAD"
@args.shift
[arg, "HEAD"]
end
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs diff [<from>..<to>] [options]"
opts.separator ""
opts.separator "Examples:"
opts.separator " git pkgs diff main..feature"
opts.separator " git pkgs diff HEAD~10"
opts.separator " git pkgs diff --from=v1.0 --to=v2.0"
opts.separator ""
opts.on("--from=REF", "Start commit") do |v|
options[:from] = v
end
opts.on("--to=REF", "End commit (default: HEAD)") do |v|
options[:to] = v
end
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