repository.rb 2.8 KB
Newer Older
1
module Repository
R
randx 已提交
2 3
  include GitHost

4 5 6 7 8 9 10
  def valid_repo?
    repo
  rescue
    errors.add(:path, "Invalid repository path")
    false
  end

11 12 13 14
  def empty_repo?
    !repo_exists? || !has_commits?
  end

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  def commit(commit_id = nil)
    Commit.find_or_first(repo, commit_id, root_ref)
  end

  def fresh_commits(n = 10)
    Commit.fresh_commits(repo, n)
  end

  def commits_with_refs(n = 20)
    Commit.commits_with_refs(repo, n)
  end

  def commits_since(date)
    Commit.commits_since(repo, date)
  end

  def commits(ref, path = nil, limit = nil, offset = nil)
    Commit.commits(repo, ref, path, limit, offset)
  end

  def commits_between(from, to)
    Commit.commits_between(repo, from, to)
  end

  def satellite
    @satellite ||= Gitlab::Satellite.new(self)
  end

  def has_post_receive_file?
    hook_file = File.join(path_to_repo, 'hooks', 'post-receive')
45
    File.exists?(hook_file)
46 47 48 49 50 51 52 53 54 55 56
  end

  def tags
    repo.tags.map(&:name).sort.reverse
  end

  def repo
    @repo ||= Grit::Repo.new(path_to_repo)
  end

  def url_to_repo
R
randx 已提交
57
    git_host.url_to_repo(path)
58 59 60
  end

  def path_to_repo
61
    File.join(Gitlab.config.git_base_path, "#{path}.git")
62 63 64
  end

  def update_repository
R
randx 已提交
65
    git_host.update_repository(self)
66 67 68
  end

  def destroy_repository
R
randx 已提交
69
    git_host.remove_repository(self)
70 71 72 73
  end

  def repo_exists?
    @repo_exists ||= (repo && !repo.branches.empty?)
74
  rescue
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    @repo_exists = false
  end

  def heads
    @heads ||= repo.heads
  end

  def tree(fcommit, path = nil)
    fcommit = commit if fcommit == :head
    tree = fcommit.tree
    path ? (tree / path) : tree
  end

  def open_branches
    if protected_branches.empty?
      self.repo.heads
    else
      pnames = protected_branches.map(&:name)
      self.repo.heads.reject { |h| pnames.include?(h.name) }
    end.sort_by(&:name)
  end

  def has_commits?
    !!commit
  end

101
  def root_ref
102 103 104 105 106 107
    default_branch || "master"
  end

  def root_ref? branch
    root_ref == branch
  end
108 109 110

  # Archive Project to .tar.gz
  #
111
  # Already packed repo archives stored at
112 113 114 115 116 117 118 119 120 121 122 123
  # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
  #
  def archive_repo ref
    ref = ref || self.root_ref
    commit = self.commit(ref)
    return nil unless commit

    # Build file path
    file_name = self.code + "-" + commit.id.to_s + ".tar.gz"
    storage_path = File.join(Rails.root, "tmp", "repositories", self.code)
    file_path = File.join(storage_path, file_name)

D
Dmitry Kalinkin 已提交
124 125 126
    # Put files into a directory before archiving
    prefix = self.code + "/"

127 128 129
    # Create file if not exists
    unless File.exists?(file_path)
      FileUtils.mkdir_p storage_path
D
Dmitry Kalinkin 已提交
130
      file = self.repo.archive_to_file(ref, prefix,  file_path)
131 132 133 134
    end

    file_path
  end
135 136 137 138 139 140 141 142

  def ssh_url_to_repo
    url_to_repo
  end

  def http_url_to_repo
    http_url = [Gitlab.config.url, "/", path, ".git"].join()
  end
143
end