repository.rb 8.3 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
  rescue Rugged::OdbError
34
    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
  def add_branch(branch_name, ref)
64
    cache.expire(:branch_names)
65 66 67 68

    gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
  end

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

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

75
  def rm_branch(branch_name)
76
    cache.expire(:branch_names)
77

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

81
  def rm_tag(tag_name)
82
    cache.expire(:tag_names)
83

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
  def branch_names
100
    cache.fetch(:branch_names) { raw_repository.branch_names }
101 102 103
  end

  def tag_names
104
    cache.fetch(:tag_names) { raw_repository.tag_names }
105 106
  end

107
  def commit_count
108
    cache.fetch(:commit_count) do
109
      begin
110
        raw_repository.commit_count(self.root_ref)
111 112 113
      rescue
        0
      end
114
    end
115 116
  end

117 118 119
  # Return repo size in megabytes
  # Cached in redis
  def size
120
    cache.fetch(:size) { raw_repository.size }
121 122 123
  end

  def expire_cache
124
    %i(size branch_names tag_names commit_count graph_log
125
       readme version contribution_guide changelog license).each do |key|
126 127
      cache.expire(key)
    end
D
Dmitriy Zaporozhets 已提交
128 129 130
  end

  def graph_log
131 132
    cache.fetch(:graph_log) do
      commits = raw_repository.log(limit: 6000, skip_merges: true,
133
                                   ref: root_ref)
134

V
Vinnie Okada 已提交
135
      commits.map do |rugged_commit|
136
        commit = Gitlab::Git::Commit.new(rugged_commit)
137

V
Vinnie Okada 已提交
138
        {
139 140
          author_name: commit.author_name,
          author_email: commit.author_email,
V
Vinnie Okada 已提交
141
          additions: commit.stats.additions,
142
          deletions: commit.stats.deletions,
V
Vinnie Okada 已提交
143 144
        }
      end
D
Dmitriy Zaporozhets 已提交
145
    end
146 147
  end

148
  def timestamps_by_user_log(user)
149 150
    author_emails = '(' + user.all_emails.map{ |e| Regexp.escape(e) }.join('|') + ')'
    args = %W(git log -E --author=#{author_emails} --since=#{(Date.today - 1.year).to_s} --branches --pretty=format:%cd --date=short)
151
    dates = Gitlab::Popen.popen(args, path_to_repo).first.split("\n")
152

153 154 155 156 157
    if dates.present?
      dates
    else
      []
    end
158 159
  end

160 161 162 163 164 165 166 167 168 169 170 171 172 173
  def commits_by_user_on_date_log(user, date)
    # format the date string for git
    start_date = date.strftime("%Y-%m-%d 00:00:00")
    end_date = date.strftime("%Y-%m-%d 23:59:59")

    author_emails = '(' + user.all_emails.map{ |e| Regexp.escape(e) }.join('|') + ')'
    args = %W(git log -E --author=#{author_emails} --after=#{start_date.to_s} --until=#{end_date.to_s} --branches --pretty=format:%h)
    commits = Gitlab::Popen.popen(args, path_to_repo).first.split("\n")

    commits.map! do |commit_id|
      commit(commit_id)
    end
  end

174 175
  def commits_per_day_for_user(user)
    timestamps_by_user_log(user).
176 177 178 179 180 181 182
      group_by { |commit_date| commit_date }.
      inject({}) do |hash, (timestamp_date, commits)|
        hash[timestamp_date] = commits.count
        hash
      end
  end

183 184 185 186
  def method_missing(m, *args, &block)
    raw_repository.send(m, *args, &block)
  end

187
  def respond_to?(method)
D
Dmitriy Zaporozhets 已提交
188
    return true if raw_repository.respond_to?(method)
189 190 191

    super
  end
D
Dmitriy Zaporozhets 已提交
192 193 194 195

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

197 198 199 200
  def blob_by_oid(oid)
    Gitlab::Git::Blob.raw(self, oid)
  end

201
  def readme
202
    cache.fetch(:readme) { tree(:head).readme }
203
  end
204

205
  def version
206
    cache.fetch(:version) do
207 208 209 210 211 212
      tree(:head).blobs.find do |file|
        file.name.downcase == 'version'
      end
    end
  end

213
  def contribution_guide
214 215 216 217 218 219
    cache.fetch(:contribution_guide) do
      tree(:head).blobs.find do |file|
        file.contributing?
      end
    end
  end
220 221 222 223 224 225 226

  def changelog
    cache.fetch(:changelog) do
      tree(:head).blobs.find do |file|
        file.name =~ /^(changelog|history)/i
      end
    end
227 228
  end

229 230 231 232 233 234
  def license
    cache.fetch(:license) do
      tree(:head).blobs.find do |file|
        file.name =~ /^license/i
      end
    end
235 236
  end

237 238 239 240 241 242 243 244 245 246 247
  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 已提交
248 249

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

D
Dmitriy Zaporozhets 已提交
252 253 254 255 256
    if last_commit
      blob_at(last_commit.sha, path)
    else
      nil
    end
D
Dmitriy Zaporozhets 已提交
257
  end
D
Dmitriy Zaporozhets 已提交
258 259 260 261 262 263 264 265

  # Returns url for submodule
  #
  # Ex.
  #   @repository.submodule_url_for('master', 'rack')
  #   # => git@localhost:rack.git
  #
  def submodule_url_for(ref, path)
D
Dmitriy Zaporozhets 已提交
266
    if submodules(ref).any?
D
Dmitriy Zaporozhets 已提交
267 268 269 270 271 272 273
      submodule = submodules(ref)[path]

      if submodule
        submodule['url']
      end
    end
  end
274 275

  def last_commit_for_path(sha, path)
D
Dmitriy Zaporozhets 已提交
276
    args = %W(git rev-list --max-count=1 #{sha} -- #{path})
277 278
    sha = Gitlab::Popen.popen(args, path_to_repo).first.strip
    commit(sha)
279
  end
280 281 282

  # Remove archives older than 2 hours
  def clean_old_archives
283 284
    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))
285
  end
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

  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
301 302

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

D
Dmitriy Zaporozhets 已提交
305
    commits.group_by(&:author_email).map do |email, commits|
306 307
      contributor = Gitlab::Contributor.new
      contributor.email = email
308

D
Dmitriy Zaporozhets 已提交
309
      commits.each do |commit|
310
        if contributor.name.blank?
D
Dmitriy Zaporozhets 已提交
311
          contributor.name = commit.author_name
312 313
        end

314
        contributor.commits += 1
315 316
      end

317 318
      contributor
    end
319
  end
D
Dmitriy Zaporozhets 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

  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
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

  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 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

  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
370 371 372 373 374 375

  private

  def cache
    @cache ||= RepositoryCache.new(path_with_namespace)
  end
376
end