repository.rb 4.46 KB
Newer Older
1
module Repository
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
  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

35 36 37 38
  def last_commit_for(ref, path = nil)
    commits(ref, path, 1).first
  end

39 40 41 42 43
  def commits_between(from, to)
    Commit.commits_between(repo, from, to)
  end

  def satellite
44
    @satellite ||= Gitlab::Satellite::Satellite.new(self)
45 46 47
  end

  def has_post_receive_file?
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    !!hook_file
  end

  def valid_post_receive_file?
    valid_hook_file == hook_file
  end

  def valid_hook_file
    @valid_hook_file ||= File.read(Rails.root.join('lib', 'hooks', 'post-receive'))
  end

  def hook_file
    @hook_file ||= begin
                     hook_path = File.join(path_to_repo, 'hooks', 'post-receive')
                     File.read(hook_path) if File.exists?(hook_path)
                   end
64 65
  end

Robert Speicher's avatar
Robert Speicher committed
66
  # Returns an Array of branch names
67
  def branch_names
68 69 70
    repo.branches.collect(&:name).sort
  end

71 72 73 74 75
  # Returns an Array of Branches
  def branches
    repo.branches.sort_by(&:name)
  end

Robert Speicher's avatar
Robert Speicher committed
76
  # Returns an Array of tag names
77
  def tag_names
78 79 80
    repo.tags.collect(&:name).sort.reverse
  end

81 82 83 84 85
  # Returns an Array of Tags
  def tags
    repo.tags.sort_by(&:name).reverse
  end

Robert Speicher's avatar
Robert Speicher committed
86
  # Returns an Array of branch and tag names
87
  def ref_names
88
    [branch_names + tag_names].flatten
89 90 91 92 93 94 95
  end

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

  def url_to_repo
96
    git_host.url_to_repo(path_with_namespace)
97 98 99
  end

  def path_to_repo
100
    File.join(Gitlab.config.git_base_path, "#{path_with_namespace}.git")
101 102 103
  end

  def namespace_dir
104
    namespace.try(:path) || ''
105 106 107
  end

  def update_repository
108
    git_host.update_repository(self)
109 110 111
  end

  def destroy_repository
112
    git_host.remove_repository(self)
113 114 115 116
  end

  def repo_exists?
    @repo_exists ||= (repo && !repo.branches.empty?)
117
  rescue
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    @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

140 141 142 143 144 145 146
  # Discovers the default branch based on the repository's available branches
  #
  # - If no branches are present, returns nil
  # - If one branch is present, returns its name
  # - If two or more branches are present, returns the one that has a name
  #   matching root_ref (default_branch or 'master' if default_branch is nil)
  def discover_default_branch
147
    if branch_names.length == 0
148
      nil
149 150
    elsif branch_names.length == 1
      branch_names.first
151
    else
152
      branch_names.select { |v| v == root_ref }.first
153 154 155
    end
  end

156 157
  def has_commits?
    !!commit
158 159
  rescue Grit::NoSuchPathError
    false
160 161
  end

162
  def root_ref
163 164 165
    default_branch || "master"
  end

166
  def root_ref?(branch)
167 168
    root_ref == branch
  end
169 170 171

  # Archive Project to .tar.gz
  #
172
  # Already packed repo archives stored at
173 174
  # app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz
  #
175
  def archive_repo(ref)
176 177 178 179 180
    ref = ref || self.root_ref
    commit = self.commit(ref)
    return nil unless commit

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

Dmitry Kalinkin's avatar
Dmitry Kalinkin committed
185
    # Put files into a directory before archiving
186
    prefix = self.path + "/"
Dmitry Kalinkin's avatar
Dmitry Kalinkin committed
187

188 189 190
    # Create file if not exists
    unless File.exists?(file_path)
      FileUtils.mkdir_p storage_path
Dmitry Kalinkin's avatar
Dmitry Kalinkin committed
191
      file = self.repo.archive_to_file(ref, prefix,  file_path)
192 193 194 195
    end

    file_path
  end
196 197 198 199 200 201

  def ssh_url_to_repo
    url_to_repo
  end

  def http_url_to_repo
202
    http_url = [Gitlab.config.url, "/", path_with_namespace, ".git"].join('')
203
  end
204 205 206 207 208

  # Check if current branch name is marked as protected in the system
  def protected_branch? branch_name
    protected_branches.map(&:name).include?(branch_name)
  end
209
end