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 pathhistory.rb
More file actions
165 lines (133 loc) · 4.83 KB
/
Copy pathhistory.rb
File metadata and controls
165 lines (133 loc) · 4.83 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class History
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
package_name = @args.shift
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
changes = Models::DependencyChange
.eager_graph(:commit, :manifest)
.order(Sequel[:commit][:committed_at])
changes = changes.for_package(package_name) if package_name
if @options[:ecosystem]
changes = changes.for_platform(@options[:ecosystem])
end
if @options[:author]
author = @options[:author]
changes = changes.where(
Sequel.like(Sequel[:commit][:author_name], "%#{author}%") |
Sequel.like(Sequel[:commit][:author_email], "%#{author}%")
)
end
if @options[:since]
since_time = parse_time(@options[:since])
changes = changes.where { Sequel[:commit][:committed_at] >= since_time }
end
if @options[:until]
until_time = parse_time(@options[:until])
changes = changes.where { Sequel[:commit][:committed_at] <= until_time }
end
changes_list = changes.all
if changes_list.empty?
msg = package_name ? "No history found for '#{package_name}'" : "No dependency changes found"
empty_result msg
return
end
if @options[:format] == "json"
output_json(changes_list)
else
paginate { output_text(changes_list, package_name) }
end
end
def output_text(changes, package_name)
if package_name
puts "History for #{package_name}:"
else
puts "Dependency history:"
end
puts
changes.each do |change|
commit = change.commit
date = commit.committed_at.strftime("%Y-%m-%d")
case change.change_type
when "added"
action = Color.green("Added")
version_info = change.requirement
when "modified"
action = Color.yellow("Updated")
version_info = "#{change.previous_requirement} -> #{change.requirement}"
when "removed"
action = Color.red("Removed")
version_info = change.requirement
end
name_prefix = package_name ? "" : "#{change.name} "
puts "#{date} #{action} #{name_prefix}#{version_info}"
puts " Commit: #{commit.short_sha} #{commit.message&.lines&.first&.strip}"
puts " Author: #{commit.author_name} <#{commit.author_email}>"
puts " Manifest: #{change.manifest.path}"
puts
end
end
def output_json(changes)
require "json"
data = changes.map do |change|
{
name: change.name,
date: change.commit.committed_at.iso8601,
change_type: change.change_type,
requirement: change.requirement,
previous_requirement: change.previous_requirement,
manifest: change.manifest.path,
ecosystem: change.ecosystem,
commit: {
sha: change.commit.sha,
message: change.commit.message&.lines&.first&.strip,
author_name: change.commit.author_name,
author_email: change.commit.author_email
}
}
end
puts JSON.pretty_generate(data)
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs history [package] [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("--author=NAME", "Filter by author name or email") do |v|
options[:author] = v
end
opts.on("--since=DATE", "Show changes after date (YYYY-MM-DD)") do |v|
options[:since] = v
end
opts.on("--until=DATE", "Show changes before date (YYYY-MM-DD)") do |v|
options[:until] = 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