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 pathupdate.rb
More file actions
148 lines (119 loc) · 4.59 KB
/
update.rb
File metadata and controls
148 lines (119 loc) · 4.59 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
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Update
include Output
def initialize(args)
@args = args
@options = parse_options
end
def run
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
branch_name = @options[:branch] || repo.default_branch
branch = Models::Branch.first(name: branch_name)
error "Branch '#{branch_name}' not in database. Run 'git pkgs init --branch=#{branch_name}' first." unless branch
since_sha = branch.last_analyzed_sha
current_sha = repo.branch_target(branch_name)
if since_sha == current_sha
info "Already up to date."
return
end
analyzer = Analyzer.new(repo)
# Get current snapshot from last analyzed commit
last_commit = Models::Commit.first(sha: since_sha)
snapshot = {}
if last_commit
last_commit.dependency_snapshots.each do |s|
key = [s.manifest.path, s.name]
snapshot[key] = {
ecosystem: s.ecosystem,
purl: s.purl,
requirement: s.requirement,
dependency_type: s.dependency_type,
integrity: s.integrity
}
end
end
walker = repo.walk(branch_name, since_sha)
commits = walker.to_a
total = commits.size
repo.prefetch_blob_paths(commits)
processed = 0
dependency_commits = 0
last_position = Models::BranchCommit.where(branch: branch).max(:position) || 0
print "Updating #{branch_name}..." unless Git::Pkgs.quiet
Database.db.transaction do
commits.each do |rugged_commit|
processed += 1
result = analyzer.analyze_commit(rugged_commit, snapshot)
commit = Models::Commit.find_or_create_from_rugged(rugged_commit)
Models::BranchCommit.find_or_create(
branch: branch,
commit: commit
) do |bc|
bc.position = last_position + processed
end
if result && result[:changes].any?
dependency_commits += 1
commit.update(has_dependency_changes: true)
result[:changes].each do |change|
manifest = Models::Manifest.find_or_create(
path: change[:manifest_path],
ecosystem: change[:ecosystem],
kind: change[:kind]
)
Models::DependencyChange.create(
commit: commit,
manifest: manifest,
name: change[:name],
ecosystem: change[:ecosystem],
purl: change[:purl],
change_type: change[:change_type],
requirement: change[:requirement],
previous_requirement: change[:previous_requirement],
dependency_type: change[:dependency_type]
)
end
snapshot = result[:snapshot]
snapshot.each do |(manifest_path, name), dep_info|
manifest = Models::Manifest.first(path: manifest_path)
Models::DependencySnapshot.find_or_create(
commit: commit,
manifest: manifest,
name: name
) do |s|
s.ecosystem = dep_info[:ecosystem]
s.purl = dep_info[:purl]
s.requirement = dep_info[:requirement]
s.dependency_type = dep_info[:dependency_type]
s.integrity = dep_info[:integrity]
end
end
end
end
branch.update(last_analyzed_sha: current_sha)
end
info "\r#{' ' * 40}\rUpdated #{branch_name}: #{total} commits (#{dependency_commits} with dependency changes)"
end
def parse_options
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: git pkgs update [options]"
opts.on("-b", "--branch=NAME", "Branch to update (default: default branch)") do |v|
options[:branch] = v
end
opts.on("-h", "--help", "Show this help") do
puts opts
exit
end
end
parser.parse!(@args)
options
end
end
end
end
end