diff --git a/app/controllers/git/base_controller.rb b/app/controllers/git/base_controller.rb new file mode 100644 index 000000000..027d3bf60 --- /dev/null +++ b/app/controllers/git/base_controller.rb @@ -0,0 +1,20 @@ +class Git::BaseController < ApplicationController + before_filter :authenticate_user! + + before_filter :find_platfrom + before_filter :find_project + before_filter :find_repository + + protected + def find_platform + @platform = Platform.find_by_name!(params[:platform_name]) + end + + def find_project + @project = Project.find_by_name!(params[:project_name]) + end + + def find_repository + @repository = @project.git_repository + end +end \ No newline at end of file diff --git a/app/controllers/git/blobs_controller.rb b/app/controllers/git/blobs_controller.rb new file mode 100644 index 000000000..ac5885f22 --- /dev/null +++ b/app/controllers/git/blobs_controller.rb @@ -0,0 +1,61 @@ +class Git::BlobsController < Git::BaseController + before_filter :set_path + before_filter :set_treeish + before_filter :set_commit_hash + + def show + if @commit_hash + @tree = @repository.tree(@commit_hash) + else + @tree = @repository.tree(@treeish) + @commit_hash = @repository.repo.log(@treeish, @path).first.id + end + + @blob = @tree / @path + end + + def blame + if @commit_hash + @tree = @repository.tree(@commit_hash) + @commit = @repository.commits(@commit_hash).first + else + @tree = @repository.tree(@treeish) + @commit = @repository.repo.log(@treeish, @path).first + end + + @blob = @tree / @path + + @blame = Grit::Blob.blame(@repository.repo, @commit.id, @path) + end + + def raw + if @commit_hash + @tree = @repository.tree(@commit_hash) + else + @tree = @repository.tree(@treeish) + @commit_hash = @repository.repo.log(@treeish, @path).first.id + end + + @blob = @tree / @path + + headers["Content-Disposition"] = %[attachment;filename="#{@blob.name}"] + render :text => @blob.data, :content_type => @blob.mime_type + end + + protected + def find_repository +# @repository = @project.repositories.find_by_name!(params[:repository_name]) + end + + def set_path + @path = params[:path].join("/") + end + + def set_treeish + @treeish = params[:treeish] ? params[:treeish] : "master" + end + + def set_commit_hash + @commit_hash = params[:commit_hash].present? ? params[:commit_hash] : nil + end +end \ No newline at end of file diff --git a/app/controllers/git/commits_controller.rb b/app/controllers/git/commits_controller.rb new file mode 100644 index 000000000..13f548a8b --- /dev/null +++ b/app/controllers/git/commits_controller.rb @@ -0,0 +1,20 @@ +class Git::CommitsController < Git::BaseController + + def index + @branch_name = (params[:branch] ? params[:branch] : "master") + @path = params[:path] + + @commits = @path.present? ? @repository.repo.log(@branch_name, @path) : @repository.commits(@branch_name) + end + + def show + @commit = @repository.commits(params[:id]).first + + respond_to do |format| + format.html + format.diff { render :text => @commit.diffs.map{|d| d.diff}.join("\n"), :content_type => "text/plain" } + format.patch { render :text => @commit.to_patch, :content_type => "text/plain" } + end + end + +end \ No newline at end of file diff --git a/app/controllers/git/repositories_controller.rb b/app/controllers/git/repositories_controller.rb new file mode 100644 index 000000000..39cace0d9 --- /dev/null +++ b/app/controllers/git/repositories_controller.rb @@ -0,0 +1,13 @@ +class Git::RepositoriesController < Git::BaseController + + def show + @commit = @repository.master + @tree = @commit.tree + end + +# def commits +# branch_name = (params[:branch] ? params[:branch] : "master") +# @commits = @repository.commits(branch_name) +# end + +end \ No newline at end of file diff --git a/app/controllers/git/trees_controller.rb b/app/controllers/git/trees_controller.rb new file mode 100644 index 000000000..af9ed8a09 --- /dev/null +++ b/app/controllers/git/trees_controller.rb @@ -0,0 +1,12 @@ +class Git::TreesController < Git::BaseController + + def show + @treeish = params[:treeish] ? params[:treeish] : "master" + @path = params[:path] + + @tree = @repository.tree(@treeish) + @tree = @tree / @path if @path + + render :template => "git/repositories/show" + end +end \ No newline at end of file diff --git a/app/controllers/git_controller.rb b/app/controllers/git_controller.rb deleted file mode 100644 index 75bae3d9d..000000000 --- a/app/controllers/git_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class GitController < ApplicationController -end diff --git a/app/models/git/repository.rb b/app/models/git/repository.rb new file mode 100644 index 000000000..b07145539 --- /dev/null +++ b/app/models/git/repository.rb @@ -0,0 +1,28 @@ +class Git::Repository + delegate :commits, :tree, :tags, :heads, :to => :repo + + attr_accessor :path, :name + + def initialize(path, name) + @path = path + @name = name + end + + def master + commits.first + end + + def to_s + name + end + + def repo + @repo ||= Grit::Repo.new(repo_path) + end + + protected + def repo_path + @repo_path ||= File.join(path, name) + end + +end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index e01184770..43a42232f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,10 +1,13 @@ class Project < ActiveRecord::Base + belongs_to :platform + validate :name, :uniqueness => true, :presence => true validate :unixname, :uniqueness => true, :presence => true - before_validation :generate_unixname validate :validate_unixname - belongs_to :platform + before_validation :generate_unixname + + include Project::HasRepository protected diff --git a/app/models/project/has_repository.rb b/app/models/project/has_repository.rb new file mode 100644 index 000000000..a109297a2 --- /dev/null +++ b/app/models/project/has_repository.rb @@ -0,0 +1,14 @@ +module Project::HasRepository + + def self.included(model) + end + + def git_repository + @repository ||= Git::Repository(git_repo_path, name) + end + + protected + def git_repo_path + @git_repo_path ||= "xxx" + end +end \ No newline at end of file diff --git a/app/views/git/repositories/show.html.haml b/app/views/git/repositories/show.html.haml new file mode 100644 index 000000000..5568b5761 --- /dev/null +++ b/app/views/git/repositories/show.html.haml @@ -0,0 +1,34 @@ +.row= link_to "Commits", commits_path(@platform.name, @project.name, @treeish) +.row.tags + %h3 Tags: + %ul + - @repository.tags.each do |tag| + %li= link_to tag.name, tree_path(@platform.name, @project.name, tag.name) + +.row.heads + %h3 Heads: + %ul + - @repository.heads.each do |head| + %li= link_to head.name, tree_path(@platform.name, @project.name, head.name) + +%table + %thead + %tr + %th name + %th age + %th message + + %tbody + - if @path.present? + %tr + %td + = link_to "..", tree_path(@platform.name, @project.name, @treeish, File.join([@path, ".."].compact)) + - @tree.contents.each do |entry| + %tr + %td + - if entry.is_a?(Grit::Blob) + = link_to entry.name, blob_path(@platform.name, @project.name, @treeish, File.join([@path, entry.name].compact)) + - else + = link_to "#{entry.name}/", tree_path(@platform.name, @project.name, @repository.name, @treeish, File.join([@path, entry.name].compact)) + %td==   + %td==   \ No newline at end of file diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index 72aca7e44..e9307179b 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -3,3 +3,6 @@ # Add new mime types for use in respond_to blocks: # Mime::Type.register "text/richtext", :rtf # Mime::Type.register_alias "text/html", :iphone + +Mime::Type.register "text/plain", :diff +Mime::Type.register "text/plain", :patch \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2d64738ef..3626cb851 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,12 +2,29 @@ Rosa::Application.routes.draw do devise_for :users resources :platforms do - resources :projects do - resource :git - end + resources :projects end resources :users + # Tree + match 'platforms/:platform_name/projects/:project_name/git/tree/:treeish(/*path)', :controller => "git/trees", :action => :show, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :tree + + # Commits + match 'platforms/:platform_name//projects/:project_name/git/commits/:treeish(/*path)', :controller => "git/commits", :action => :index, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :commits + match 'platforms/:platform_name//projects/:project_name/git/commit/:id(.:format)', :controller => "git/comnits", :action => :show, :defaults => { :format => :html }, :as => :commit + + # Blobs + match 'platforms/:platform_name/projects/:project_name/git/blob/:treeish/*path', :controller => "git/blobs", :action => :show, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :blob + match 'platforms/:platform_name/projects/:project_name/git/commit/blob/:commit_hash/*path', :controller => "git/blobs", :action => :show, :as => :blob_commit + + # Blame + match 'platforms/:platform_name/projects/:project_name/git/blame/:treeish/*path', :controller => "git/blobs", :action => :blame, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :blame + match 'platforms/:platform_name/projects/:project_name/git/commit/blame/:commit_hash/*path', :controller => "git/blobs", :action => :blame, :as => :blame_commit + + # Raw + match 'platforms/:platform_name/projects/:project_name/git/raw/:treeish/*path', :controller => "git/blobs", :action => :raw, :treeish => /[0-9a-zA-Z_.\-]*/, :defaults => { :treeish => :master }, :as => :raw + match 'platforms/:platform_name/projects/:project_name/git/commit/raw/:commit_hash/*path', :controller => "git/blobs", :action => :raw, :as => :raw_commit + root :to => "platforms#index" end