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 pathwhy.rb
More file actions
116 lines (94 loc) · 3.23 KB
/
Copy pathwhy.rb
File metadata and controls
116 lines (94 loc) · 3.23 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Why
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
package_name = @args.shift
error "Usage: git pkgs why <package>" unless package_name
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
# Find the first time this package was added
added_change = Models::DependencyChange
.eager(:commit, :manifest)
.for_package(package_name)
.added
.order("commits.committed_at ASC")
if @options[:ecosystem]
added_change = added_change.for_platform(@options[:ecosystem])
end
added_change = added_change.first
unless added_change
if @options[:format] == "json"
require "json"
puts JSON.pretty_generate({ found: false, package: package_name })
else
empty_result "Package '#{package_name}' not found in dependency history"
end
return
end
commit = added_change.commit
if @options[:format] == "json"
output_json(package_name, added_change, commit)
else
output_text(package_name, added_change, commit)
end
end
def output_text(package_name, added_change, commit)
puts "#{package_name} was added in commit #{commit.short_sha}"
puts
puts "Date: #{commit.committed_at.strftime("%Y-%m-%d %H:%M")}"
puts "Author: #{commit.author_name} <#{commit.author_email}>"
puts "Manifest: #{added_change.manifest.path}"
puts "Version: #{added_change.requirement}"
puts
puts "Commit message:"
puts commit.message.to_s.lines.map { |l| " #{l}" }.join
end
def output_json(package_name, added_change, commit)
require "json"
data = {
found: true,
package: package_name,
ecosystem: added_change.ecosystem,
requirement: added_change.requirement,
manifest: added_change.manifest.path,
commit: {
sha: commit.sha,
short_sha: commit.short_sha,
message: commit.message,
author_name: commit.author_name,
author_email: commit.author_email,
date: commit.committed_at.iso8601
}
}
puts JSON.pretty_generate(data)
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs why <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("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end