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 pathrepository.rb
More file actions
165 lines (133 loc) · 4.17 KB
/
repository.rb
File metadata and controls
165 lines (133 loc) · 4.17 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
require "rugged"
module Git
module Pkgs
class Repository
attr_reader :path
def initialize(path = nil)
@path = path || Git::Pkgs.git_dir || Git::Pkgs.work_tree || Dir.pwd
@rugged = Rugged::Repository.new(@path)
@blob_paths_cache = {}
end
def git_dir
@rugged.path.chomp("/")
end
def default_branch
# Try origin/HEAD first (what GitHub/GitLab set as default)
if @rugged.references["refs/remotes/origin/HEAD"]
ref = @rugged.references["refs/remotes/origin/HEAD"].resolve
return ref.name.sub("refs/remotes/origin/", "")
end
# Fall back to current HEAD
head = @rugged.head
head.name.sub("refs/heads/", "")
rescue Rugged::ReferenceError
# Last resort: common default names
%w[main master].find { |name| branch_exists?(name) } || "main"
end
def branch_exists?(name)
@rugged.branches[name] != nil
end
def branch_target(name)
@rugged.branches[name]&.target_id
end
def walk(branch_name, since_sha = nil)
walker = Rugged::Walker.new(@rugged)
walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
branch = @rugged.branches[branch_name]
raise Error, "Branch '#{branch_name}' not found" unless branch
if since_sha
begin
walker.hide(@rugged.lookup(since_sha).oid)
rescue Rugged::OdbError
# Commit not found, walk from beginning
end
end
walker.push(branch.target_id)
walker
end
def lookup(sha)
@rugged.lookup(sha)
end
DEFAULT_THREADS = 4
def prefetch_blob_paths(commits)
thread_count = Git::Pkgs.threads || DEFAULT_THREADS
# Serial is faster for small repos due to thread overhead
if commits.size < 1500 || thread_count <= 1
commits.each { |c| @blob_paths_cache[c.oid] = compute_blob_paths(c) }
return
end
queue = Queue.new
mutex = Mutex.new
commits.each { |c| queue << c }
thread_count.times { queue << nil }
thread_pool = thread_count.times.map do
Thread.new do
while (commit = queue.pop)
paths = compute_blob_paths(commit)
mutex.synchronize { @blob_paths_cache[commit.oid] = paths }
end
end
end
thread_pool.each(&:join)
end
def blob_paths(rugged_commit)
@blob_paths_cache[rugged_commit.oid] || compute_blob_paths(rugged_commit)
end
def compute_blob_paths(rugged_commit)
paths = []
if rugged_commit.parents.empty?
rugged_commit.tree.walk_blobs(:postorder) do |root, entry|
paths << {
status: :added,
path: "#{root}#{entry[:name]}"
}
end
else
diffs = rugged_commit.parents[0].diff(rugged_commit)
diffs.each_delta do |delta|
paths << {
status: delta.status,
path: delta.new_file[:path]
}
end
end
paths
end
def content_at_commit(rugged_commit, file_path)
entry = rugged_commit.tree.path(file_path)
blob = @rugged.lookup(entry[:oid])
blob.content
rescue Rugged::TreeError
nil
end
def content_before_commit(rugged_commit, file_path)
return nil if rugged_commit.parents.empty?
content_at_commit(rugged_commit.parents[0], file_path)
end
def blob_oid_at_commit(rugged_commit, file_path)
entry = rugged_commit.tree.path(file_path)
entry[:oid]
rescue Rugged::TreeError
nil
end
def blob_content(oid)
blob = @rugged.lookup(oid)
blob.content
rescue Rugged::OdbError
nil
end
def merge_commit?(rugged_commit)
rugged_commit.parents.length > 1
end
def head_sha
@rugged.head.target_id
end
def rev_parse(ref)
@rugged.rev_parse(ref).oid
rescue Rugged::ReferenceError, Rugged::InvalidError
nil
end
end
end
end