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 pathlog.rb
More file actions
150 lines (119 loc) · 4.52 KB
/
log.rb
File metadata and controls
150 lines (119 loc) · 4.52 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Log
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
commits = Models::Commit
.includes(:dependency_changes)
.where(has_dependency_changes: true)
.order(committed_at: :desc)
commits = commits.where("author_name LIKE ? OR author_email LIKE ?",
"%#{@options[:author]}%", "%#{@options[:author]}%") if @options[:author]
commits = commits.where("committed_at >= ?", parse_time(@options[:since])) if @options[:since]
commits = commits.where("committed_at <= ?", parse_time(@options[:until])) if @options[:until]
commits = commits.limit(@options[:limit] || 20)
if commits.empty?
empty_result "No commits with dependency changes found"
return
end
if @options[:format] == "json"
output_json(commits)
else
paginate { output_text(commits) }
end
end
def output_text(commits)
commits.each do |commit|
changes = commit.dependency_changes.to_a
changes = changes.select { |c| c.ecosystem == @options[:ecosystem] } if @options[:ecosystem]
next if changes.empty?
puts "#{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" }
added.each do |change|
puts " + #{change.name} #{change.requirement}"
end
modified.each do |change|
puts " ~ #{change.name} #{change.previous_requirement} -> #{change.requirement}"
end
removed.each do |change|
puts " - #{change.name}"
end
puts
end
end
def output_json(commits)
require "json"
data = commits.map do |commit|
changes = commit.dependency_changes.to_a
changes = changes.select { |c| c.ecosystem == @options[:ecosystem] } if @options[:ecosystem]
{
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
}
end
}
end
puts JSON.pretty_generate(data)
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs log [options]"
opts.on("-e", "--ecosystem=NAME", "Filter by ecosystem") do |v|
options[:ecosystem] = v
end
opts.on("--author=NAME", "Filter by author name or email") do |v|
options[:author] = v
end
opts.on("--since=DATE", "Show commits after date") do |v|
options[:since] = v
end
opts.on("--until=DATE", "Show commits before date") do |v|
options[:until] = v
end
opts.on("-n", "--limit=N", Integer, "Limit commits (default: 20)") do |v|
options[:limit] = 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