repository.rb 3.42 KB
Newer Older
1
class Repository
2 3
  include Gitlab::ShellAdapter

4
  attr_accessor :raw_repository, :path_with_namespace
5 6

  def initialize(path_with_namespace, default_branch)
7
    @path_with_namespace = path_with_namespace
8
    @raw_repository = Gitlab::Git::Repository.new(path_to_repo) if path_with_namespace
9 10 11 12
  rescue Gitlab::Git::Repository::NoRepository
    nil
  end

13 14 15 16
  def path_to_repo
    @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, path_with_namespace + ".git")
  end

17 18 19 20 21 22
  def exists?
    raw_repository
  end

  def empty?
    raw_repository.empty?
23 24 25
  end

  def commit(id = nil)
26
    return nil unless raw_repository
27
    commit = Gitlab::Git::Commit.find(raw_repository, id)
28 29 30 31 32
    commit = Commit.new(commit) if commit
    commit
  end

  def commits(ref, path = nil, limit = nil, offset = nil)
33 34 35 36 37 38 39
    commits = Gitlab::Git::Commit.where(
      repo: raw_repository,
      ref: ref,
      path: path,
      limit: limit,
      offset: offset,
    )
40
    commits = Commit.decorate(commits) if commits.present?
41 42 43
    commits
  end

44 45
  def commits_between(from, to)
    commits = Gitlab::Git::Commit.between(raw_repository, from, to)
46
    commits = Commit.decorate(commits) if commits.present?
47 48 49
    commits
  end

50 51 52 53 54 55 56 57
  def find_branch(name)
    branches.find { |branch| branch.name == name }
  end

  def find_tag(name)
    tags.find { |tag| tag.name == name }
  end

58 59 60 61 62 63
  def recent_branches(limit = 20)
    branches.sort do |a, b|
      a.commit.committed_date <=> b.commit.committed_date
    end[0..limit]
  end

64 65 66 67 68 69
  def add_branch(branch_name, ref)
    Rails.cache.delete(cache_key(:branch_names))

    gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
  end

70 71 72 73 74 75
  def add_tag(tag_name, ref)
    Rails.cache.delete(cache_key(:tag_names))

    gitlab_shell.add_tag(path_with_namespace, tag_name, ref)
  end

76
  def rm_branch(branch_name)
77 78
    Rails.cache.delete(cache_key(:branch_names))

79 80 81
    gitlab_shell.rm_branch(path_with_namespace, branch_name)
  end

82
  def rm_tag(tag_name)
83 84
    Rails.cache.delete(cache_key(:tag_names))

85 86 87
    gitlab_shell.rm_tag(path_with_namespace, tag_name)
  end

88 89 90 91 92 93 94 95 96 97 98 99
  def round_commit_count
    if commit_count > 10000
      '10000+'
    elsif commit_count > 5000
      '5000+'
    elsif commit_count > 1000
      '1000+'
    else
      commit_count
    end
  end

100 101 102 103 104 105 106 107 108 109 110 111
  def branch_names
    Rails.cache.fetch(cache_key(:branch_names)) do
      raw_repository.branch_names
    end
  end

  def tag_names
    Rails.cache.fetch(cache_key(:tag_names)) do
      raw_repository.tag_names
    end
  end

112 113
  def commit_count
    Rails.cache.fetch(cache_key(:commit_count)) do
114 115 116 117 118
      begin
        raw_repository.raw.commit_count
      rescue
        0
      end
119
    end
120 121
  end

122 123 124 125 126 127 128 129 130 131
  # Return repo size in megabytes
  # Cached in redis
  def size
    Rails.cache.fetch(cache_key(:size)) do
      raw_repository.size
    end
  end

  def expire_cache
    Rails.cache.delete(cache_key(:size))
132 133
    Rails.cache.delete(cache_key(:branch_names))
    Rails.cache.delete(cache_key(:tag_names))
134
    Rails.cache.delete(cache_key(:commit_count))
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
135 136 137 138
    Rails.cache.delete(cache_key(:graph_log))
  end

  def graph_log
139
    Rails.cache.fetch(cache_key(:graph_log)) do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
140 141 142
      stats = Gitlab::Git::GitStats.new(raw, root_ref)
      stats.parsed_log
    end
143 144 145 146 147 148
  end

  def cache_key(type)
    "#{type}:#{path_with_namespace}"
  end

149 150 151 152
  def method_missing(m, *args, &block)
    raw_repository.send(m, *args, &block)
  end

153
  def respond_to?(method)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
154
    return true if raw_repository.respond_to?(method)
155 156 157 158

    super
  end
end