| 1 | class CommitsController < ApplicationController |
| 2 | before_action :load_owner |
| 3 | before_action :load_repository |
| 4 | before_action :ensure_can_read! |
| 5 | |
| 6 | def index |
| 7 | @branch = params[:branch] || @repository.default_branch |
| 8 | @branches = GitRepositoryService.branches(@repository.disk_path) rescue [] |
| 9 | @page = (params[:page] || 1).to_i |
| 10 | @per_page = 20 |
| 11 | @offset = (@page - 1) * @per_page |
| 12 | |
| 13 | unless @repository.initialized? && GitRepositoryService.branch_exists?(@repository.disk_path, @branch) |
| 14 | redirect_to repository_path(@owner.username, @repository.name) |
| 15 | return |
| 16 | end |
| 17 | |
| 18 | @commits = GitRepositoryService.commits( |
| 19 | @repository.disk_path, @branch, |
| 20 | limit: @per_page, offset: @offset |
| 21 | ) |
| 22 | @total = GitRepositoryService.commit_count(@repository.disk_path, @branch) |
| 23 | @commit_count = @total |
| 24 | @total_pages = (@total.to_f / @per_page).ceil |
| 25 | end |
| 26 | |
| 27 | def show |
| 28 | @sha = params[:sha] |
| 29 | |
| 30 | unless @repository.initialized? |
| 31 | redirect_to repository_path(@owner.username, @repository.name) |
| 32 | return |
| 33 | end |
| 34 | |
| 35 | @commit = GitRepositoryService.commit(@repository.disk_path, @sha) |
| 36 | if @commit.nil? |
| 37 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 38 | end |
| 39 | end |
| 40 | |
| 41 | private |
| 42 | |
| 43 | def load_owner |
| 44 | @owner = User.find_by!(username: params[:username]) |
| 45 | rescue ActiveRecord::RecordNotFound |
| 46 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 47 | end |
| 48 | |
| 49 | def load_repository |
| 50 | @repository = @owner.repositories.find_by!(name: params[:repository]) |
| 51 | rescue ActiveRecord::RecordNotFound |
| 52 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 53 | end |
| 54 | |
| 55 | def ensure_can_read! |
| 56 | unless !@repository.is_private || (signed_in? && current_user == @owner) |
| 57 | render file: Rails.public_path.join("404.html"), status: :not_found, layout: false |
| 58 | end |
| 59 | end |
| 60 | end |