class Gitlab::Git::Repository

Attributes

repos_path[RW]
path_with_namespace[RW]

Repository directory name with namespace direcotry Examples:

gitlab/gitolite
diaspora
raw[R]

Grit repo object

repo[R]

Grit repo object

root_ref[RW]

Default branch in the repository

Public Class Methods

new(path_with_namespace, root_ref) click to toggle source
# File lib/gitlab_git/repository.rb, line 31
def initialize(path_with_namespace, root_ref)
  @path_with_namespace = path_with_namespace
  @root_ref = root_ref || raw.head.name

  # Init grit repo object
  raw
end

Public Instance Methods

archive_repo(ref, storage_path) click to toggle source

Archive Project to .tar.gz

Already packed repo archives stored at app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz

# File lib/gitlab_git/repository.rb, line 120
def archive_repo(ref, storage_path)
  ref = ref || self.root_ref
  commit = Gitlab::Git::Commit.find(self, ref)
  return nil unless commit

  # Build file path
  file_name = self.path_with_namespace.gsub("/","_") + "-" + commit.id.to_s + ".tar.gz"
  file_path = File.join(storage_path, self.path_with_namespace, file_name)

  # Put files into a directory before archiving
  prefix = File.basename(self.path_with_namespace) + "/"

  # Create file if not exists
  unless File.exists?(file_path)
    FileUtils.mkdir_p File.dirname(file_path)
    file = self.raw.archive_to_file(ref, prefix,  file_path)
  end

  file_path
end
branch_names() click to toggle source

Returns an Array of branch names sorted by name ASC

# File lib/gitlab_git/repository.rb, line 55
def branch_names
  branches.map(&:name)
end
branch_names_contains(commit) click to toggle source

Returns branch names collection that contains the special commit(SHA1 or name)

Ex.

repo.branch_names_contains('master')
# File lib/gitlab_git/repository.rb, line 270
def branch_names_contains(commit)
  output = raw.git.native(:branch, {contains: true}, commit)
  # The output is expected as follow
  #   fix-aaa
  #   fix-bbb
  # * master
  output.scan(/[^* \n]+/)
end
branches() click to toggle source

Returns an Array of Branches

# File lib/gitlab_git/repository.rb, line 60
def branches
  raw.branches.sort_by(&:name)
end
commits_between(from, to) click to toggle source

Delegate #commits_between to Grit method

# File lib/gitlab_git/repository.rb, line 191
def commits_between(from, to)
  raw.commits_between(from, to)
end
diff(from, to) click to toggle source
# File lib/gitlab_git/repository.rb, line 199
def diff(from, to)
  raw.diff(from, to)
end
discover_default_branch() click to toggle source

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 the one that has a name matching #root_ref (default_branch or 'master' if default_branch is nil)

# File lib/gitlab_git/repository.rb, line 105
def discover_default_branch
  if branch_names.length == 0
    nil
  elsif branch_names.length == 1
    branch_names.first
  else
    branch_names.select { |v| v == root_ref }.first
  end
end
empty?() click to toggle source
# File lib/gitlab_git/repository.rb, line 95
def empty?
  !has_commits?
end
find_commits(options = {}) click to toggle source

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
# File lib/gitlab_git/repository.rb, line 220
def find_commits(options = {})
  actual_options = options.dup

  allowed_options = [:ref, :max_count, :skip, :contains, :order]

  actual_options.keep_if do |key, value|
    allowed_options.include?(key)
  end

  default_options = {pretty: 'raw', order: :date}

  actual_options = default_options.merge(actual_options)

  order = actual_options.delete(:order)

  case order
  when :date
    actual_options[:date_order] = true
  when :topo
    actual_options[:topo_order] = true
  end

  ref = actual_options.delete(:ref)

  containing_commit = actual_options.delete(:contains)

  args = []

  if ref
    args.push(ref)
  elsif containing_commit
    args.push(*branch_names_contains(containing_commit))
  else
    actual_options[:all] = true
  end

  output = raw.git.native(:rev_list, actual_options, *args)

  Grit::Commit.list_from_string(raw, output).map do |commit|
    Gitlab::Git::Commit.decorate(commit)
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
  []
end
has_commits?() click to toggle source
# File lib/gitlab_git/repository.rb, line 89
def has_commits?
  !!Gitlab::Git::Commit.last(self)
rescue Grit::NoSuchPathError
  false
end
heads() click to toggle source
# File lib/gitlab_git/repository.rb, line 79
def heads
  @heads ||= raw.heads.sort_by(&:name)
end
log(options) click to toggle source

Delegate log to Grit method

Usage.

repo.log(
  ref: 'master',
  path: 'app/models',
  limit: 10,
  offset: 5,
)
# File lib/gitlab_git/repository.rb, line 169
def log(options)
  default_options = {
    limit: 10,
    offset: 0,
    path: nil,
    ref: root_ref,
    follow: false
  }

  options = default_options.merge(options)

  raw.log(
    options[:ref] || root_ref,
    options[:path],
    max_count: options[:limit].to_i,
    skip: options[:offset].to_i,
    follow: options[:follow]
  )
end
merge_base_commit(from, to) click to toggle source
# File lib/gitlab_git/repository.rb, line 195
def merge_base_commit(from, to)
  raw.git.native(:merge_base, {}, [to, from]).strip
end
path_to_repo() click to toggle source
# File lib/gitlab_git/repository.rb, line 39
def path_to_repo
  @path_to_repo ||= File.join(repos_path, "#{path_with_namespace}.git")
end
ref_names() click to toggle source

Returns an Array of branch and tag names

# File lib/gitlab_git/repository.rb, line 75
def ref_names
  branch_names + tag_names
end
refs_hash() click to toggle source

Get refs hash which key is SHA1 and value is ref object(Grit::Head or Grit::Remote or Grit::Tag)

# File lib/gitlab_git/repository.rb, line 280
def refs_hash
  # Initialize only when first call
  if @refs_hash.nil?
    @refs_hash = Hash.new { |h, k| h[k] = [] }

    @raw.refs.each do |r|
      @refs_hash[r.commit.id] << r
    end
  end

  @refs_hash
end
repos_path() click to toggle source
# File lib/gitlab_git/repository.rb, line 43
def repos_path
  self.class.repos_path
end
search_files(query, ref = nil) click to toggle source
# File lib/gitlab_git/repository.rb, line 147
def search_files(query, ref = nil)
  if ref.nil? || ref == ""
    ref = root_ref
  end

  greps = raw.grep(query, 3, ref)

  greps.map do |grep|
    Gitlab::Git::BlobSnippet.new(ref, grep.content, grep.startline, grep.filename)
  end
end
size() click to toggle source

Return repo size in megabytes

# File lib/gitlab_git/repository.rb, line 142
def size
  size = popen('du -s', path_to_repo).first.strip.to_i
  (size.to_f / 1024).round(2)
end
tag_names() click to toggle source

Returns an Array of tag names

# File lib/gitlab_git/repository.rb, line 65
def tag_names
  tags.map(&:name)
end
tags() click to toggle source

Returns an Array of Tags

# File lib/gitlab_git/repository.rb, line 70
def tags
  raw.tags.sort_by(&:name).reverse
end
tree(fcommit, path = nil) click to toggle source
# File lib/gitlab_git/repository.rb, line 83
def tree(fcommit, path = nil)
  fcommit = commit if fcommit == :head
  tree = fcommit.tree
  path ? (tree / path) : tree
end