Class: Gitlab::Git::Repository
- Inherits:
-
Object
- Object
- Gitlab::Git::Repository
- Includes:
- Popen
- Defined in:
- lib/gitlab_git/repository.rb
Defined Under Namespace
Classes: NoRepository
Instance Attribute Summary collapse
-
#grit ⇒ Object
readonly
Grit repo object.
-
#name ⇒ Object
readonly
Directory name of repo.
-
#path ⇒ Object
readonly
Full path to repo.
-
#root_ref ⇒ Object
Default branch in the repository.
-
#rugged ⇒ Object
readonly
Rugged repo object.
Instance Method Summary collapse
-
#archive_repo(ref, storage_path, format = "tar.gz") ⇒ Object
Archive Project to .tar.gz.
-
#branch_names ⇒ Object
Returns an Array of branch names sorted by name ASC.
-
#branch_names_contains(commit) ⇒ Object
Returns branch names collection that contains the special commit(SHA1 or name).
-
#branches ⇒ Object
Returns an Array of Branches.
-
#commit_count(ref) ⇒ Object
Return total commits count accessible from passed ref.
-
#commits_between(from, to) ⇒ Object
Delegate commits_between to Grit method.
- #diff(from, to, *paths) ⇒ Object
-
#discover_default_branch ⇒ Object
Discovers the default branch based on the repository’s available branches.
- #empty? ⇒ Boolean
-
#find_commits(options = {}) ⇒ Object
Returns commits collection.
- #has_commits? ⇒ Boolean
-
#heads ⇒ Object
Deprecated.
-
#initialize(path) ⇒ Repository
constructor
A new instance of Repository.
-
#log(options) ⇒ Object
Delegate log to Grit method.
-
#lookup(oid) ⇒ Object
Lookup for rugged object by oid.
- #merge_base_commit(from, to) ⇒ Object
-
#raw ⇒ Object
Alias to old method for compatibility.
-
#ref_names ⇒ Object
Returns an Array of branch and tag names.
-
#refs_hash ⇒ Object
Get refs hash which key is SHA1 and value is ref object(Grit::Head or Grit::Remote or Grit::Tag).
- #repo_exists? ⇒ Boolean
- #rugged_head ⇒ Object
- #search_files(query, ref = nil) ⇒ Object
-
#size ⇒ Object
Return repo size in megabytes.
-
#submodules(ref) ⇒ Object
Return hash with submodules info for this repository.
-
#tag_names ⇒ Object
Returns an Array of tag names.
-
#tags ⇒ Object
Returns an Array of Tags.
Methods included from Popen
Constructor Details
#initialize(path) ⇒ Repository
Returns a new instance of Repository.
29 30 31 32 33 |
# File 'lib/gitlab_git/repository.rb', line 29 def initialize(path) @path = path @name = path.split("/").last @root_ref = discover_default_branch end |
Instance Attribute Details
#grit ⇒ Object (readonly)
Grit repo object
24 25 26 |
# File 'lib/gitlab_git/repository.rb', line 24 def grit @grit end |
#name ⇒ Object (readonly)
Directory name of repo
21 22 23 |
# File 'lib/gitlab_git/repository.rb', line 21 def name @name end |
#path ⇒ Object (readonly)
Full path to repo
18 19 20 |
# File 'lib/gitlab_git/repository.rb', line 18 def path @path end |
#root_ref ⇒ Object
Default branch in the repository
15 16 17 |
# File 'lib/gitlab_git/repository.rb', line 15 def root_ref @root_ref end |
#rugged ⇒ Object (readonly)
Rugged repo object
27 28 29 |
# File 'lib/gitlab_git/repository.rb', line 27 def rugged @rugged end |
Instance Method Details
#archive_repo(ref, storage_path, format = "tar.gz") ⇒ Object
Archive Project to .tar.gz
Already packed repo archives stored at app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
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 |
# File 'lib/gitlab_git/repository.rb', line 132 def archive_repo(ref, storage_path, format = "tar.gz") ref = ref || self.root_ref commit = Gitlab::Git::Commit.find(self, ref) return nil unless commit extension = nil git_archive_format = nil pipe_cmd = nil case format when "tar.bz2", "tbz", "tbz2", "tb2", "bz2" extension = ".tar.bz2" pipe_cmd = %W(bzip2) when "tar" extension = ".tar" pipe_cmd = %W(cat) when "zip" extension = ".zip" git_archive_format = "zip" pipe_cmd = %W(cat) else # everything else should fall back to tar.gz extension = ".tar.gz" git_archive_format = nil pipe_cmd = %W(gzip -n) end # Build file path file_name = self.name.gsub("\.git", "") + "-" + commit.id.to_s + extension file_path = File.join(storage_path, self.name, file_name) # Put files into a directory before archiving prefix = File.basename(self.name) + "/" # Create file if not exists unless File.exists?(file_path) # create archive in temp file tmp_file = Tempfile.new('gitlab-archive-repo') self.grit.archive_to_file(ref, prefix, tmp_file.path, git_archive_format, pipe_cmd) # move temp file to persisted location FileUtils.mkdir_p File.dirname(file_path) FileUtils.move(tmp_file.path, file_path) # delte temp file tmp_file.close tmp_file.unlink end file_path end |
#branch_names ⇒ Object
Returns an Array of branch names sorted by name ASC
54 55 56 |
# File 'lib/gitlab_git/repository.rb', line 54 def branch_names branches.map(&:name) end |
#branch_names_contains(commit) ⇒ Object
Returns branch names collection that contains the special commit(SHA1 or name)
Ex.
repo.branch_names_contains('master')
313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/gitlab_git/repository.rb', line 313 def branch_names_contains(commit) output = grit.git.native(:branch, {contains: true}, commit) # Fix encoding issue output = EncodingHelper::encode!(output) # The output is expected as follow # fix-aaa # fix-bbb # * master output.scan(/[^* \n]+/) end |
#branches ⇒ Object
Returns an Array of Branches
59 60 61 62 63 |
# File 'lib/gitlab_git/repository.rb', line 59 def branches rugged.branches.map do |rugged_ref| Branch.new(rugged_ref.name, rugged_ref.target) end.sort_by(&:name) end |
#commit_count(ref) ⇒ Object
Return total commits count accessible from passed ref
364 365 366 367 368 369 |
# File 'lib/gitlab_git/repository.rb', line 364 def commit_count(ref) walker = Rugged::Walker.new(rugged) walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE) walker.push(ref) walker.count end |
#commits_between(from, to) ⇒ Object
Delegate commits_between to Grit method
234 235 236 |
# File 'lib/gitlab_git/repository.rb', line 234 def commits_between(from, to) grit.commits_between(from, to) end |
#diff(from, to, *paths) ⇒ Object
242 243 244 |
# File 'lib/gitlab_git/repository.rb', line 242 def diff(from, to, *paths) grit.diff(from, to, *paths) end |
#discover_default_branch ⇒ Object
Discovers the default branch based on the repository’s available branches
-
If no branches are present, returns nil
-
If one branch is present, returns its name
-
If two or more branches are present, returns current HEAD or master or first branch
107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gitlab_git/repository.rb', line 107 def discover_default_branch if branch_names.length == 0 nil elsif branch_names.length == 1 branch_names.first elsif rugged_head && branch_names.include?(Ref.extract_branch_name(rugged_head.name)) Ref.extract_branch_name(rugged_head.name) elsif branch_names.include?("master") "master" else branch_names.first end end |
#empty? ⇒ Boolean
94 95 96 |
# File 'lib/gitlab_git/repository.rb', line 94 def empty? rugged.empty? end |
#find_commits(options = {}) ⇒ Object
Returns commits collection
Ex.
repo.find_commits(
ref: 'master',
max_count: 10,
skip: 5,
order: :date
)
+options+ is a Hash of optional arguments to git
:ref is the ref from which to begin (SHA1 or name)
:contains is the commit contained by the refs from which to begin (SHA1 or name)
:max_count is the maximum number of commits to fetch
:skip is the number of commits to skip
:order is the commits order and allowed value is :date(default) or :topo
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 |
# File 'lib/gitlab_git/repository.rb', line 263 def find_commits( = {}) = .dup = [:ref, :max_count, :skip, :contains, :order] .keep_if do |key, value| .include?(key) end = {pretty: 'raw', order: :date} = .merge() order = .delete(:order) case order when :date [:date_order] = true when :topo [:topo_order] = true end ref = .delete(:ref) containing_commit = .delete(:contains) args = [] if ref args.push(ref) elsif containing_commit args.push(*branch_names_contains(containing_commit)) else [:all] = true end output = grit.git.native(:rev_list, , *args) Grit::Commit.list_from_string(grit, output).map do |commit| Gitlab::Git::Commit.decorate(commit) end rescue Grit::GitRuby::Repository::NoSuchShaFound [] end |
#has_commits? ⇒ Boolean
90 91 92 |
# File 'lib/gitlab_git/repository.rb', line 90 def has_commits? !empty? end |
#heads ⇒ Object
Deprecated. Will be removed in 5.2
86 87 88 |
# File 'lib/gitlab_git/repository.rb', line 86 def heads @heads ||= grit.heads.sort_by(&:name) end |
#log(options) ⇒ Object
Delegate log to Grit method
Usage.
repo.log(
ref: 'master',
path: 'app/models',
limit: 10,
offset: 5,
)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/gitlab_git/repository.rb', line 212 def log() = { limit: 10, offset: 0, path: nil, ref: root_ref, follow: false } = .merge() grit.log( [:ref] || root_ref, [:path], max_count: [:limit].to_i, skip: [:offset].to_i, follow: [:follow] ) end |
#lookup(oid) ⇒ Object
Lookup for rugged object by oid
341 342 343 |
# File 'lib/gitlab_git/repository.rb', line 341 def lookup(oid) rugged.lookup(oid) end |
#merge_base_commit(from, to) ⇒ Object
238 239 240 |
# File 'lib/gitlab_git/repository.rb', line 238 def merge_base_commit(from, to) grit.git.native(:merge_base, {}, [to, from]).strip end |
#raw ⇒ Object
Alias to old method for compatibility
42 43 44 |
# File 'lib/gitlab_git/repository.rb', line 42 def raw grit end |
#ref_names ⇒ Object
Returns an Array of branch and tag names
81 82 83 |
# File 'lib/gitlab_git/repository.rb', line 81 def ref_names branch_names + tag_names end |
#refs_hash ⇒ Object
Get refs hash which key is SHA1 and value is ref object(Grit::Head or Grit::Remote or Grit::Tag)
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/gitlab_git/repository.rb', line 328 def refs_hash # Initialize only when first call if @refs_hash.nil? @refs_hash = Hash.new { |h, k| h[k] = [] } grit.refs.each do |r| @refs_hash[r.commit.id] << r end end @refs_hash end |
#repo_exists? ⇒ Boolean
98 99 100 |
# File 'lib/gitlab_git/repository.rb', line 98 def repo_exists? !!rugged end |
#rugged_head ⇒ Object
121 122 123 124 125 |
# File 'lib/gitlab_git/repository.rb', line 121 def rugged_head rugged.head rescue Rugged::ReferenceError nil end |
#search_files(query, ref = nil) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/gitlab_git/repository.rb', line 190 def search_files(query, ref = nil) if ref.nil? || ref == "" ref = root_ref end greps = grit.grep(query, 3, ref) greps.map do |grep| Gitlab::Git::BlobSnippet.new(ref, grep.content, grep.startline, grep.filename) end end |
#size ⇒ Object
Return repo size in megabytes
185 186 187 188 |
# File 'lib/gitlab_git/repository.rb', line 185 def size size = popen(%W(du -s), path).first.strip.to_i (size.to_f / 1024).round(2) end |
#submodules(ref) ⇒ Object
Return hash with submodules info for this repository
Ex.
{
"rack" => {
"id" => "c67be4624545b4263184c4a0e8f887efd0a66320",
"path" => "rack",
"url" => "git://github.com/chneukirchen/rack.git"
},
"encoding" => {
"id" => ....
}
}
359 360 361 |
# File 'lib/gitlab_git/repository.rb', line 359 def submodules(ref) Grit::Submodule.config(grit, ref) end |
#tag_names ⇒ Object
Returns an Array of tag names
66 67 68 69 |
# File 'lib/gitlab_git/repository.rb', line 66 def tag_names # rugged.tags returns array of names rugged..to_a end |