BigW Consortium Gitlab

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

4
  attr_accessor :raw_repository, :path_with_namespace, :project
5

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

14
  # Return absolute path to repository
15
  def path_to_repo
16 17 18
    @path_to_repo ||= File.expand_path(
      File.join(Gitlab.config.gitlab_shell.repos_path, path_with_namespace + ".git")
    )
19 20
  end

21 22 23 24 25 26
  def exists?
    raw_repository
  end

  def empty?
    raw_repository.empty?
27 28
  end

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

38
  def commits(ref, path = nil, limit = nil, offset = nil, skip_merges = false)
39 40 41 42 43 44 45
    commits = Gitlab::Git::Commit.where(
      repo: raw_repository,
      ref: ref,
      path: path,
      limit: limit,
      offset: offset,
    )
46
    commits = Commit.decorate(commits, @project) if commits.present?
47 48 49
    commits
  end

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

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

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

64
  def add_branch(branch_name, ref)
65
    cache.expire(:branch_names)
66
    @branches = nil
67 68 69 70

    gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
  end

71
  def add_tag(tag_name, ref, message = nil)
72
    cache.expire(:tag_names)
73
    @tags = nil
74

75
    gitlab_shell.add_tag(path_with_namespace, tag_name, ref, message)
76 77
  end

78
  def rm_branch(branch_name)
79
    cache.expire(:branch_names)
80
    @branches = nil
81

82 83 84
    gitlab_shell.rm_branch(path_with_namespace, branch_name)
  end

85
  def rm_tag(tag_name)
86
    cache.expire(:tag_names)
87
    @tags = nil
88

89 90 91
    gitlab_shell.rm_tag(path_with_namespace, tag_name)
  end

92 93 94 95 96 97 98 99 100 101 102 103
  def round_commit_count
    if commit_count > 10000
      '10000+'
    elsif commit_count > 5000
      '5000+'
    elsif commit_count > 1000
      '1000+'
    else
      commit_count
    end
  end

104
  def branch_names
105
    cache.fetch(:branch_names) { raw_repository.branch_names }
106 107 108
  end

  def tag_names
109
    cache.fetch(:tag_names) { raw_repository.tag_names }
110 111
  end

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

122 123 124
  # Return repo size in megabytes
  # Cached in redis
  def size
125
    cache.fetch(:size) { raw_repository.size }
126 127 128
  end

  def expire_cache
129
    %i(size branch_names tag_names commit_count graph_log
130
       readme version contribution_guide changelog license).each do |key|
131 132
      cache.expire(key)
    end
Dmitriy Zaporozhets committed
133 134 135
  end

  def graph_log
136 137
    cache.fetch(:graph_log) do
      commits = raw_repository.log(limit: 6000, skip_merges: true,
138
                                   ref: root_ref)
139

140
      commits.map do |rugged_commit|
141
        commit = Gitlab::Git::Commit.new(rugged_commit)
142

143
        {
144 145
          author_name: commit.author_name,
          author_email: commit.author_email,
146
          additions: commit.stats.additions,
147
          deletions: commit.stats.deletions,
148 149
        }
      end
Dmitriy Zaporozhets committed
150
    end
151 152
  end

153 154 155 156
  def lookup_cache
    @lookup_cache ||= {}
  end

157
  def method_missing(m, *args, &block)
158 159 160 161 162 163
    if m == :lookup && !block_given?
      lookup_cache[m] ||= {}
      lookup_cache[m][args.join(":")] ||= raw_repository.send(m, *args, &block)
    else
      raw_repository.send(m, *args, &block)
    end
164 165
  end

166
  def respond_to?(method)
Dmitriy Zaporozhets committed
167
    return true if raw_repository.respond_to?(method)
168 169 170

    super
  end
171 172 173 174

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

176 177 178 179
  def blob_by_oid(oid)
    Gitlab::Git::Blob.raw(self, oid)
  end

180
  def readme
181
    cache.fetch(:readme) { tree(:head).readme }
182
  end
183

184
  def version
185
    cache.fetch(:version) do
186 187 188 189 190 191
      tree(:head).blobs.find do |file|
        file.name.downcase == 'version'
      end
    end
  end

192
  def contribution_guide
193 194 195 196 197 198
    cache.fetch(:contribution_guide) do
      tree(:head).blobs.find do |file|
        file.contributing?
      end
    end
  end
199 200 201 202

  def changelog
    cache.fetch(:changelog) do
      tree(:head).blobs.find do |file|
203
        file.name =~ /\A(changelog|history)/i
204 205
      end
    end
206 207
  end

208 209 210
  def license
    cache.fetch(:license) do
      tree(:head).blobs.find do |file|
211
        file.name =~ /\Alicense/i
212 213
      end
    end
214 215
  end

216
  def head_commit
217 218 219 220 221
    @head_commit ||= commit(self.root_ref)
  end

  def head_tree
    @head_tree ||= Tree.new(self, head_commit.sha, nil)
222 223 224 225
  end

  def tree(sha = :head, path = nil)
    if sha == :head
226 227 228 229 230
      if path.nil?
        return head_tree
      else
        sha = head_commit.sha
      end
231 232 233 234
    end

    Tree.new(self, sha, path)
  end
235 236

  def blob_at_branch(branch_name, path)
237
    last_commit = commit(branch_name)
238

239 240 241 242 243
    if last_commit
      blob_at(last_commit.sha, path)
    else
      nil
    end
244
  end
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)
253
    if submodules(ref).any?
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
    args = %W(git rev-list --max-count=1 #{sha} -- #{path})
264 265
    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
    repository_downloads_path = Gitlab.config.gitlab.repository_downloads_path
271 272 273

    return unless File.directory?(repository_downloads_path)

274
    Gitlab::Popen.popen(%W(find #{repository_downloads_path} -not -path #{repository_downloads_path} -mmin +120 -delete))
275
  end
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

  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
291 292

  def contributors
293
    commits = self.commits(nil, nil, 2000, 0, true)
294

295
    commits.group_by(&:author_email).map do |email, commits|
296 297
      contributor = Gitlab::Contributor.new
      contributor.email = email
298

299
      commits.each do |commit|
300
        if contributor.name.blank?
301
          contributor.name = commit.author_name
302 303
        end

304
        contributor.commits += 1
305 306
      end

307 308
      contributor
    end
309
  end
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

  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
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

  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
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

  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
360

361 362 363 364 365 366 367 368 369 370 371 372
  def branches
    @branches ||= raw_repository.branches
  end

  def tags
    @tags ||= raw_repository.tags
  end

  def root_ref
    @root_ref ||= raw_repository.root_ref
  end

373 374 375
  def commit_file(user, path, content, message, ref)
    path[0] = '' if path[0] == '/'

376
    committer = user_to_comitter(user)
377
    options = {}
378 379
    options[:committer] = committer
    options[:author] = committer
380 381 382 383 384 385 386 387 388 389 390 391 392
    options[:commit] = {
      message: message,
      branch: ref
    }

    options[:file] = {
      content: content,
      path: path
    }

    Gitlab::Git::Blob.commit(raw_repository, options)
  end

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  def remove_file(user, path, message, ref)
    path[0] = '' if path[0] == '/'

    committer = user_to_comitter(user)
    options = {}
    options[:committer] = committer
    options[:author] = committer
    options[:commit] = {
      message: message,
      branch: ref
    }

    options[:file] = {
      path: path
    }

    Gitlab::Git::Blob.remove(raw_repository, options)
  end

412 413
  private

414 415 416 417 418 419 420 421
  def user_to_comitter(user)
    {
      email: user.email,
      name: user.name,
      time: Time.now
    }
  end

422 423 424
  def cache
    @cache ||= RepositoryCache.new(path_with_namespace)
  end
425
end