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 pathbranch.rb
More file actions
327 lines (268 loc) · 10.9 KB
/
Copy pathbranch.rb
File metadata and controls
327 lines (268 loc) · 10.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# frozen_string_literal: true
module Git
module Pkgs
module Commands
class Branch
include Output
DEFAULT_BATCH_SIZE = 500
DEFAULT_SNAPSHOT_INTERVAL = 50
def batch_size
Git::Pkgs.batch_size || DEFAULT_BATCH_SIZE
end
def snapshot_interval
Git::Pkgs.snapshot_interval || DEFAULT_SNAPSHOT_INTERVAL
end
def initialize(args)
@args = args
@options = parse_options
end
def run
subcommand = @args.shift
case subcommand
when "add"
add_branch
when "list"
list_branches
when "remove", "rm"
remove_branch
when nil, "-h", "--help"
print_help
else
error "Unknown subcommand: #{subcommand}. Run 'git pkgs branch --help' for usage"
end
end
def add_branch
branch_name = @args.shift
error "Usage: git pkgs branch add <name>" unless branch_name
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
error "Branch '#{branch_name}' not found. Check 'git branch -a' for available branches." unless repo.branch_exists?(branch_name)
existing = Models::Branch.first(name: branch_name)
if existing
info "Branch '#{branch_name}' already tracked (#{existing.commits.count} commits)"
info "Use 'git pkgs update' to refresh"
return
end
Database.optimize_for_bulk_writes
branch = Models::Branch.create(name: branch_name)
analyzer = Analyzer.new(repo)
info "Analyzing branch: #{branch_name}"
print "Loading commits..." unless Git::Pkgs.quiet
walker = repo.walk(branch_name)
commits = walker.to_a
total = commits.size
print "\rPrefetching diffs..." unless Git::Pkgs.quiet
repo.prefetch_blob_paths(commits)
print "\r#{' ' * 20}\r" unless Git::Pkgs.quiet
stats = bulk_process_commits(commits, branch, analyzer, total, repo)
branch.update(last_analyzed_sha: repo.branch_target(branch_name))
Database.optimize_for_reads
info "\rDone!#{' ' * 20}"
info "Analyzed #{total} commits"
info "Found #{stats[:dependency_commits]} commits with dependency changes"
info "Stored #{stats[:snapshots_stored]} snapshots"
end
def list_branches
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
branches = Models::Branch.all
if branches.empty?
empty_result "No branches tracked"
return
end
puts "Tracked branches:"
branches.each do |branch|
commit_count = branch.commits.count
dep_commits = branch.commits_dataset.where(has_dependency_changes: true).count
last_sha = branch.last_analyzed_sha&.slice(0, 7) || "none"
puts " #{branch.name}: #{commit_count} commits (#{dep_commits} with deps), last: #{last_sha}"
end
end
def remove_branch
branch_name = @args.shift
error "Usage: git pkgs branch remove <name>" unless branch_name
repo = Repository.new
require_database(repo)
Database.connect(repo.git_dir)
branch = Models::Branch.first(name: branch_name)
error "Branch '#{branch_name}' not tracked. Run 'git pkgs branch list' to see tracked branches." unless branch
# Only delete branch_commits, keep shared commits
count = branch.branch_commits.count
branch.branch_commits_dataset.delete
branch.delete
info "Removed branch '#{branch_name}' (#{count} branch-commit links)"
end
def bulk_process_commits(commits, branch, analyzer, total, repo)
now = Time.now
snapshot = {}
manifests_cache = {}
pending_commits = []
pending_branch_commits = []
pending_changes = []
pending_snapshots = []
dependency_commit_count = 0
snapshots_stored = 0
processed = 0
flush = lambda do
return if pending_commits.empty?
Database.db.transaction do
# Use insert with on_conflict for commits since they may already exist from other branches
if pending_commits.any?
Models::Commit.dataset
.insert_conflict(target: :sha, update: { has_dependency_changes: Sequel[:excluded][:has_dependency_changes] })
.multi_insert(pending_commits)
end
commit_ids = Models::Commit
.where(sha: pending_commits.map { |c| c[:sha] })
.select_hash(:sha, :id)
if pending_branch_commits.any?
branch_commit_records = pending_branch_commits.map do |bc|
{ branch_id: bc[:branch_id], commit_id: commit_ids[bc[:sha]], position: bc[:position] }
end
Models::BranchCommit.dataset.multi_insert(branch_commit_records)
end
if pending_changes.any?
manifest_ids = Models::Manifest.select_hash(:path, :id)
change_records = pending_changes.map do |c|
{
commit_id: commit_ids[c[:sha]],
manifest_id: manifest_ids[c[:manifest_path]],
name: c[:name],
ecosystem: c[:ecosystem],
change_type: c[:change_type],
requirement: c[:requirement],
previous_requirement: c[:previous_requirement],
dependency_type: c[:dependency_type],
created_at: now,
updated_at: now
}
end
Models::DependencyChange.dataset.multi_insert(change_records)
end
if pending_snapshots.any?
manifest_ids ||= Models::Manifest.select_hash(:path, :id)
snapshot_records = pending_snapshots.map do |s|
{
commit_id: commit_ids[s[:sha]],
manifest_id: manifest_ids[s[:manifest_path]],
name: s[:name],
ecosystem: s[:ecosystem],
requirement: s[:requirement],
dependency_type: s[:dependency_type],
integrity: s[:integrity],
created_at: now,
updated_at: now
}
end
Models::DependencySnapshot.dataset.multi_insert(snapshot_records)
end
end
pending_commits.clear
pending_branch_commits.clear
pending_changes.clear
pending_snapshots.clear
end
commits.each do |rugged_commit|
processed += 1
print "\rProcessing commit #{processed}/#{total}..." if !Git::Pkgs.quiet && (processed % 50 == 0 || processed == total)
next if rugged_commit.parents.length > 1
result = analyzer.analyze_commit(rugged_commit, snapshot)
has_changes = result && result[:changes].any?
pending_commits << {
sha: rugged_commit.oid,
message: rugged_commit.message,
author_name: rugged_commit.author[:name],
author_email: rugged_commit.author[:email],
committed_at: rugged_commit.time,
has_dependency_changes: has_changes,
created_at: now,
updated_at: now
}
pending_branch_commits << {
branch_id: branch.id,
sha: rugged_commit.oid,
position: processed
}
if has_changes
dependency_commit_count += 1
result[:changes].each do |change|
manifest_key = change[:manifest_path]
unless manifests_cache[manifest_key]
manifests_cache[manifest_key] = Models::Manifest.find_or_create(
path: change[:manifest_path],
ecosystem: change[:ecosystem],
kind: change[:kind]
)
end
pending_changes << {
sha: rugged_commit.oid,
manifest_path: manifest_key,
name: change[:name],
ecosystem: change[:ecosystem],
change_type: change[:change_type],
requirement: change[:requirement],
previous_requirement: change[:previous_requirement],
dependency_type: change[:dependency_type]
}
end
snapshot = result[:snapshot]
if dependency_commit_count % snapshot_interval == 0
snapshot.each do |(manifest_path, name), dep_info|
pending_snapshots << {
sha: rugged_commit.oid,
manifest_path: manifest_path,
name: name,
ecosystem: dep_info[:ecosystem],
requirement: dep_info[:requirement],
dependency_type: dep_info[:dependency_type],
integrity: dep_info[:integrity]
}
end
snapshots_stored += snapshot.size
end
end
flush.call if pending_commits.size >= batch_size
end
if snapshot.any?
last_sha = commits.last&.oid
if last_sha && !pending_snapshots.any? { |s| s[:sha] == last_sha }
snapshot.each do |(manifest_path, name), dep_info|
pending_snapshots << {
sha: last_sha,
manifest_path: manifest_path,
name: name,
ecosystem: dep_info[:ecosystem],
requirement: dep_info[:requirement],
dependency_type: dep_info[:dependency_type],
integrity: dep_info[:integrity]
}
end
snapshots_stored += snapshot.size
end
end
flush.call
{ dependency_commits: dependency_commit_count, snapshots_stored: snapshots_stored }
end
def print_help
puts <<~HELP
Usage: git pkgs branch <subcommand> [options]
Subcommands:
add <name> Analyze and track a branch
list List tracked branches
remove <name> Stop tracking a branch
Examples:
git pkgs branch add feature-x
git pkgs branch list
git pkgs branch remove feature-x
HELP
end
def parse_options
options = {}
options
end
end
end
end
end