Add some copyright notices
[gitorious:taladars-gitorious-saltation.git] / app / controllers / commits_controller.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2010 Marko Peltola <marko@markopeltola.com>
4 #   Copyright (C) 2010 Tero Hänninen <tero.j.hanninen@jyu.fi>
5 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
6 #   Copyright (C) 2008 Johan Sørensen <johan@johansorensen.com>
7 #   Copyright (C) 2008 David A. Cuadrado <krawek@gmail.com>
8 #   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
9 #
10 #   This program is free software: you can redistribute it and/or modify
11 #   it under the terms of the GNU Affero General Public License as published by
12 #   the Free Software Foundation, either version 3 of the License, or
13 #   (at your option) any later version.
14 #
15 #   This program is distributed in the hope that it will be useful,
16 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #   GNU Affero General Public License for more details.
19 #
20 #   You should have received a copy of the GNU Affero General Public License
21 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #++
23
24 class CommitsController < ApplicationController
25   before_filter :find_project_and_repository
26   before_filter :require_view_right_to_repository
27   before_filter :check_repository_for_commits
28   renders_in_site_specific_context
29   
30   def index
31     if params[:branch].blank?
32       redirect_to_ref(@repository.head_candidate.name) and return
33     end
34     @git = @repository.git
35     @ref, @path = branch_and_path(params[:branch], @git)
36     if h = @git.get_head(@ref)
37       head = h
38     else
39       if commit = @git.commit(@ref)
40         head = Grit::Head.new(commit.id_abbrev, commit)
41       else
42         flash[:error] = "\"#{CGI.escapeHTML(@ref)}\" was not a valid ref, trying #{CGI.escapeHTML(@git.heads.first.name)} instead"
43         redirect_to_ref(@git.heads.first.name) and return
44       end
45     end
46     if stale_conditional?(head.commit.id, head.commit.committed_date.utc)
47       @root = Breadcrumb::Branch.new(head, @repository)
48       @commits = @repository.cached_paginated_commits(@ref, params[:page])
49       @atom_auto_discovery_url = project_repository_formatted_commits_feed_path(@project, @repository, params[:branch], :atom)
50       respond_to do |format|
51         format.html
52       end
53     end
54   end
55
56   def show
57     @diffmode = params[:diffmode] == "sidebyside" ? "sidebyside" : "inline"
58     @git = @repository.git
59     unless @commit = @git.commit(params[:id])
60       handle_missing_sha and return
61     end
62     @comments = @repository.comments.find_all_by_sha1(@commit.id, :include => :user)
63     last_modified = @comments.size > 0 ? @comments.last.created_at.utc : @commit.committed_date.utc
64     if stale_conditional?([@commit.id, @comments.size], last_modified)
65       @root = Breadcrumb::Commit.new(:repository => @repository, :id => @commit.id_abbrev)
66       @diffs = @commit.parents.empty? ? [] : @commit.diffs
67       @comment_count = @repository.comments.count(:all, :conditions => {:sha1 => @commit.id.to_s})
68       @committer_user = User.find_by_email_with_aliases(@commit.committer.email)
69       @author_user = User.find_by_email_with_aliases(@commit.author.email)
70       respond_to do |format|
71         format.html
72         format.diff  { render :text => @diffs.map{|d| d.diff}.join("\n"), :content_type => "text/plain" }
73         format.patch { render :text => @commit.to_patch, :content_type => "text/plain" }
74       end
75     end
76   end
77   
78   def feed
79     @git = @repository.git
80     @ref = desplat_path(params[:branch])
81     @commits = @repository.git.commits(@ref, 1)
82     return if @commits.empty?
83     expires_in 30.minutes
84     if stale?(:etag => @commits.first.id, :last_modified => @commits.first.committed_date.utc)
85       @commits += @repository.git.commits(@ref, 49, 1)
86       respond_to do |format|
87         format.atom
88       end
89     end
90   end
91   
92   protected
93     def handle_missing_sha
94       flash[:error] = "No such SHA1 was found"
95       redirect_to repo_owner_path(@repository, :project_repository_commits_path, @project, 
96                       @repository)
97     end
98     
99     def redirect_to_ref(ref)
100       redirect_to repo_owner_path(@repository, :project_repository_commits_in_ref_path, 
101                       @project, @repository, ref)
102     end
103 end