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 pathsearch.rb
More file actions
155 lines (123 loc) · 4.5 KB
/
Copy pathsearch.rb
File metadata and controls
155 lines (123 loc) · 4.5 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Search
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
pattern = @args.first
error "Usage: git pkgs search <pattern>" unless pattern
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
# Search for dependencies matching the pattern
query = Models::DependencyChange
.join(:manifests, id: :manifest_id)
.where(Sequel.like(Sequel[:dependency_changes][:name], "%#{pattern}%"))
if @options[:ecosystem]
query = query.where(Sequel[:dependency_changes][:ecosystem] => @options[:ecosystem])
end
if @options[:direct]
query = query.where(Sequel[:manifests][:kind] => "manifest")
end
# Get unique dependency names
matches = query.distinct.select_map([Sequel[:dependency_changes][:name], Sequel[:dependency_changes][:ecosystem]])
if matches.empty?
empty_result "No dependencies found matching '#{pattern}'"
return
end
if @options[:format] == "json"
output_json(matches, pattern)
else
paginate { output_text(matches, pattern) }
end
end
def output_text(matches, pattern)
puts "Dependencies matching '#{pattern}':"
puts
matches.group_by { |_, platform| platform }.each do |platform, deps|
puts "#{platform}:"
deps.each do |name, _|
summary = dependency_summary(name, platform)
puts " #{name}"
puts " #{summary}"
end
puts
end
end
def output_json(matches, pattern)
require "json"
results = matches.map do |name, platform|
changes = Models::DependencyChange
.where(name: name, ecosystem: platform)
.eager(:commit)
.association_join(:commit)
.order(Sequel[:commit][:committed_at])
.all
first = changes.first
last = changes.last
current = changes.select { |c| %w[added modified].include?(c.change_type) }.last
{
name: name,
ecosystem: platform,
first_seen: first&.commit&.committed_at&.iso8601,
last_changed: last&.commit&.committed_at&.iso8601,
current_version: current&.requirement,
removed: changes.last&.change_type == "removed",
total_changes: changes.count
}
end
puts JSON.pretty_generate(results)
end
def dependency_summary(name, platform)
changes = Models::DependencyChange
.where(name: name, ecosystem: platform)
.eager(:commit)
.association_join(:commit)
.order(Sequel[:commit][:committed_at])
.all
first = changes.first
last = changes.last
parts = []
parts << "added #{first.commit.committed_at.strftime('%Y-%m-%d')}"
if last.change_type == "removed"
parts << "removed #{last.commit.committed_at.strftime('%Y-%m-%d')}"
else
current = changes.select { |c| %w[added modified].include?(c.change_type) }.last
parts << "current: #{current&.requirement || 'unknown'}"
end
parts << "#{changes.count} changes"
parts.join(", ")
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs search <pattern> [options]"
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v|
options[:ecosystem] = v
end
opts.on("-d", "--direct", "Only show direct dependencies (not from lockfiles)") do
options[:direct] = true
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