repository.rb 8.0 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 = nil)
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
  # Return absolute path to repository
14
  def path_to_repo
15 16 17
    @path_to_repo ||= File.expand_path(
      File.join(Gitlab.config.gitlab_shell.repos_path, path_with_namespace + ".git")
    )
18 19
  end

20 21 22 23 24 25
  def exists?
    raw_repository
  end

  def empty?
    raw_repository.empty?
26 27
  end

28
  def commit(id = 'HEAD')
29
    return nil unless raw_repository
30
    commit = Gitlab::Git::Commit.find(raw_repository, id)
31 32
    commit = Commit.new(commit) if commit
    commit
33 34
  rescue Rugged::OdbError => ex
    nil
35 36
  end

D
Dmitriy Zaporozhets 已提交
37
  def commits(ref, path = nil, limit = nil, offset = nil, skip_merges = false)
38 39 40 41 42 43 44
    commits = Gitlab::Git::Commit.where(
      repo: raw_repository,
      ref: ref,
      path: path,
      limit: limit,
      offset: offset,
    )
45
    commits = Commit.decorate(commits) if commits.present?
46 47 48
    commits
  end

49 50
  def commits_between(from, to)
    commits = Gitlab::Git::Commit.between(raw_repository, from, to)
51
    commits = Commit.decorate(commits) if commits.present?
52 53 54
    commits
  end

55 56 57 58 59 60 61 62
  def find_branch(name)
    branches.find { |branch| branch.name == name }
  end

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

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

    gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
  end

69
  def add_tag(tag_name, ref, message = nil)
70 71
    Rails.cache.delete(cache_key(:tag_names))

72
    gitlab_shell.add_tag(path_with_namespace, tag_name, ref, message)
73 74
  end

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

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

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

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

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

99 100 101 102 103 104 105 106 107 108 109 110
  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

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

121 122 123 124 125 126 127 128 129 130
  # 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))
131 132
    Rails.cache.delete(cache_key(:branch_names))
    Rails.cache.delete(cache_key(:tag_names))
133
    Rails.cache.delete(cache_key(:commit_count))
D
Dmitriy Zaporozhets 已提交
134
    Rails.cache.delete(cache_key(:graph_log))
135
    Rails.cache.delete(cache_key(:readme))
136
    Rails.cache.delete(cache_key(:version))
137
    Rails.cache.delete(cache_key(:contribution_guide))
D
Dmitriy Zaporozhets 已提交
138 139 140
  end

  def graph_log
141
    Rails.cache.fetch(cache_key(:graph_log)) do
142 143 144 145 146 147

      # handle empty repos that don't have a root_ref set yet
      unless raw_repository.root_ref.present?
        raw_repository.root_ref = 'refs/heads/master'
      end

V
Vinnie Okada 已提交
148
      commits = raw_repository.log(limit: 6000, skip_merges: true,
149 150
                                   ref: raw_repository.root_ref)

V
Vinnie Okada 已提交
151 152
      commits.map do |rugged_commit|

153
        commit = Gitlab::Git::Commit.new(rugged_commit)
V
Vinnie Okada 已提交
154 155 156 157
        {
          author_name: commit.author_name.force_encoding('UTF-8'),
          author_email: commit.author_email.force_encoding('UTF-8'),
          additions: commit.stats.additions,
158 159
          deletions: commit.stats.deletions,
          date: commit.committed_date
V
Vinnie Okada 已提交
160 161
        }
      end
D
Dmitriy Zaporozhets 已提交
162
    end
163 164
  end

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  def graph_logs_by_user_email(user)
    graph_log.select { |u_email| u_email[:author_email] == user.email }
  end

  def timestamps_by_user_from_graph_log(user)
    graph_logs_by_user_email(user).map { |graph_log| graph_log[:date].to_time.to_i }
  end

  def commits_log_of_user_by_date(user)
    timestamps_by_user_from_graph_log(user).
      group_by { |commit_date| commit_date }.
      inject({}) do |hash, (timestamp_date, commits)|
        hash[timestamp_date] = commits.count
        hash
      end
  end

182 183 184 185
  def cache_key(type)
    "#{type}:#{path_with_namespace}"
  end

186 187 188 189
  def method_missing(m, *args, &block)
    raw_repository.send(m, *args, &block)
  end

190
  def respond_to?(method)
D
Dmitriy Zaporozhets 已提交
191
    return true if raw_repository.respond_to?(method)
192 193 194

    super
  end
D
Dmitriy Zaporozhets 已提交
195 196 197 198

  def blob_at(sha, path)
    Gitlab::Git::Blob.find(self, sha, path)
  end
199

200 201 202 203
  def blob_by_oid(oid)
    Gitlab::Git::Blob.raw(self, oid)
  end

204 205
  def readme
    Rails.cache.fetch(cache_key(:readme)) do
206
      tree(:head).readme
207 208
    end
  end
209

210 211 212 213 214 215 216 217
  def version
    Rails.cache.fetch(cache_key(:version)) do
      tree(:head).blobs.find do |file|
        file.name.downcase == 'version'
      end
    end
  end

218 219 220 221 222 223
  def contribution_guide
    Rails.cache.fetch(cache_key(:contribution_guide)) do
      tree(:head).contribution_guide
    end
  end

224 225 226 227 228 229 230 231 232 233 234
  def head_commit
    commit(self.root_ref)
  end

  def tree(sha = :head, path = nil)
    if sha == :head
      sha = head_commit.sha
    end

    Tree.new(self, sha, path)
  end
D
Dmitriy Zaporozhets 已提交
235 236

  def blob_at_branch(branch_name, path)
D
Dmitriy Zaporozhets 已提交
237
    last_commit = commit(branch_name)
D
Dmitriy Zaporozhets 已提交
238

D
Dmitriy Zaporozhets 已提交
239 240 241 242 243
    if last_commit
      blob_at(last_commit.sha, path)
    else
      nil
    end
D
Dmitriy Zaporozhets 已提交
244
  end
D
Dmitriy Zaporozhets 已提交
245 246 247 248 249 250 251 252

  # Returns url for submodule
  #
  # Ex.
  #   @repository.submodule_url_for('master', 'rack')
  #   # => git@localhost:rack.git
  #
  def submodule_url_for(ref, path)
D
Dmitriy Zaporozhets 已提交
253
    if submodules(ref).any?
D
Dmitriy Zaporozhets 已提交
254 255 256 257 258 259 260
      submodule = submodules(ref)[path]

      if submodule
        submodule['url']
      end
    end
  end
261 262

  def last_commit_for_path(sha, path)
263 264 265
    args = %W(git rev-list --max-count 1 #{sha} -- #{path})
    sha = Gitlab::Popen.popen(args, path_to_repo).first.strip
    commit(sha)
266
  end
267 268 269

  # Remove archives older than 2 hours
  def clean_old_archives
270 271
    repository_downloads_path = Gitlab.config.gitlab.repository_downloads_path
    Gitlab::Popen.popen(%W(find #{repository_downloads_path} -not -path #{repository_downloads_path} -mmin +120 -delete))
272
  end
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

  def branches_sorted_by(value)
    case value
    when 'recently_updated'
      branches.sort do |a, b|
        commit(b.target).committed_date <=> commit(a.target).committed_date
      end
    when 'last_updated'
      branches.sort do |a, b|
        commit(a.target).committed_date <=> commit(b.target).committed_date
      end
    else
      branches
    end
  end
288 289

  def contributors
D
Dmitriy Zaporozhets 已提交
290
    commits = self.commits(nil, nil, 2000, 0, true)
291

D
Dmitriy Zaporozhets 已提交
292
    commits.group_by(&:author_email).map do |email, commits|
293 294
      contributor = Gitlab::Contributor.new
      contributor.email = email
295

D
Dmitriy Zaporozhets 已提交
296
      commits.each do |commit|
297
        if contributor.name.blank?
D
Dmitriy Zaporozhets 已提交
298
          contributor.name = commit.author_name
299 300
        end

301
        contributor.commits += 1
302 303
      end

304 305
      contributor
    end
306
  end
D
Dmitriy Zaporozhets 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

  def blob_for_diff(commit, diff)
    file = blob_at(commit.id, diff.new_path)

    unless file
      file = prev_blob_for_diff(commit, diff)
    end

    file
  end

  def prev_blob_for_diff(commit, diff)
    if commit.parent_id
      blob_at(commit.parent_id, diff.old_path)
    end
  end
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

  def branch_names_contains(sha)
    args = %W(git branch --contains #{sha})
    names = Gitlab::Popen.popen(args, path_to_repo).first

    if names.respond_to?(:split)
      names = names.split("\n").map(&:strip)

      names.each do |name|
        name.slice! '* '
      end

      names
    else
      []
    end
  end
H
Hannes Rosenögger 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

  def tag_names_contains(sha)
    args = %W(git tag --contains #{sha})
    names = Gitlab::Popen.popen(args, path_to_repo).first

    if names.respond_to?(:split)
      names = names.split("\n").map(&:strip)

      names.each do |name|
        name.slice! '* '
      end

      names
    else
      []
    end
  end
357
end