diff --git a/app/controllers/git/blobs_controller.rb b/app/controllers/git/blobs_controller.rb index 75663689e..0bfe6b4b4 100644 --- a/app/controllers/git/blobs_controller.rb +++ b/app/controllers/git/blobs_controller.rb @@ -1,12 +1,10 @@ # -*- encoding : utf-8 -*- class Git::BlobsController < Git::BaseController - before_filter :set_path - before_filter :set_commit_hash before_filter :find_tree + before_filter :set_path_blob + before_filter :set_commit_hash def show - @blob = @tree / @path - if params[:raw] image_url = Rails.root.to_s + "/" + @path @@ -19,21 +17,18 @@ class Git::BlobsController < Git::BaseController end def blame - @blob = @tree / @path - @blame = Grit::Blob.blame(@git_repository.repo, @commit.try(:id), @path) end def raw - @blob = @tree / @path - headers["Content-Disposition"] = %[attachment;filename="#{@blob.name}"] render :text => @blob.data, :content_type => @blob.mime_type end protected - def set_path + def set_path_blob @path = params[:path] + @blob = @tree / @path.encode_to_default end def set_commit_hash diff --git a/app/controllers/git/commits_controller.rb b/app/controllers/git/commits_controller.rb index 45e4c50d5..dbf9becaa 100644 --- a/app/controllers/git/commits_controller.rb +++ b/app/controllers/git/commits_controller.rb @@ -2,7 +2,7 @@ class Git::CommitsController < Git::BaseController def index - @branch_name = (params[:branch] ? params[:branch] : "master") + @branch_name = params[:treeish] || "master" @path = params[:path] if @path.present? diff --git a/app/helpers/commit_helper.rb b/app/helpers/commit_helper.rb index 8093043b3..180374610 100644 --- a/app/helpers/commit_helper.rb +++ b/app/helpers/commit_helper.rb @@ -13,7 +13,7 @@ module CommitHelper end res << "" - res.join("\n").html_safe + res.join("\n").encode_to_default.html_safe end # def format_commit_message(message) @@ -34,7 +34,7 @@ module CommitHelper def short_commit_message(message) # Why 42? Because it is the Answer! - truncate(message, :length => 42, :omission => "...") + truncate(message, :length => 42, :omission => "...").encode_to_default end end diff --git a/app/helpers/git_helper.rb b/app/helpers/git_helper.rb index 619376dd2..f302b5aed 100644 --- a/app/helpers/git_helper.rb +++ b/app/helpers/git_helper.rb @@ -26,7 +26,7 @@ module GitHelper res = "#{link_to @project.name, tree_path(@project)} /" end - res.html_safe + res.encode_to_default.html_safe end def render_line_numbers(n) @@ -38,7 +38,9 @@ module GitHelper def render_blob(blob) res = "" - blob.data.split("\n").collect{|line| "
#{line.present? ? h(line) : "
"}
"}.join + blob.data.encode_to_default.split("\n").collect do |line| + "
#{line.present? ? h(line) : "
"}
" + end.join end def choose_render_way(blob) @@ -46,4 +48,9 @@ module GitHelper return :text if blob.mime_type.match(/text|xml|json/) :binary end + + def force_encoding_to_site(string) + string.dup.encode_to_default + end + end diff --git a/app/models/git_hook.rb b/app/models/git_hook.rb new file mode 100644 index 000000000..1a2ef2b50 --- /dev/null +++ b/app/models/git_hook.rb @@ -0,0 +1,56 @@ +class GitHook + attr_reader :repo, :newrev, :oldrev, :newrev_type, :oldrev_type, :refname, + :change_type, :rev, :rev_type, :refname_type, :owner, :project + + def initialize(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type) + @repo, @newrev, @oldrev, @refname, @newrev_type, @oldrev_type = repo, newrev, oldrev, ref, newrev_type, oldrev_type + @owner = User.find_by_uname owner_uname + @project = @owner.projects.where(:name => repo).first + @change_type = git_change_type + git_revision_types + commit_type + end + + def git_change_type + if @oldrev =~ /0+$/ + return 'create' + elsif @newrev =~ /0+$/ + return 'delete' + else + return 'update' + end + end + + def git_revision_types + case @change_type + when 'create', 'update' + @rev = @newrev + @rev_type = @newrev_type + when 'delete' + @rev = @oldrev + @rev_type = @oldrev_type + end + end + + def commit_type + if @refname =~ /refs\/tags\/*/ && @rev_type == 'commit' + # un-annotated tag + @refname_type= 'tag' + #~ short_refname=refname + '##refs/tags/' + elsif @refname =~ /refs\/tags\/*/ && @rev_type == 'tag' + # annotated tag + @refname_type="annotated tag" + #~ short_refname= refname + '##refs/tags/' + elsif @refname =~ /refs\/heads\/*/ && @rev_type == 'commit' + # branch + @refname_type= 'branch' + elsif @refname =~ /refs\/remotes\/*'/ && @rev_type == 'commit' + # tracking branch + @refname_type="tracking branch" + @short_refname= @refname + '##refs/remotes/' + else + # Anything else (is there anything else?) + @refname_type= "*** Unknown type of update to $refname (#{rev_type})" + end + end +end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index ecbf136ee..c15332d5e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -162,6 +162,11 @@ class Project < ActiveRecord::Base owner == user end + def self.process_hook(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type) + rec = GitHook.new(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type) + #ActivityFeedObserver.instance.after_create rec # for example + end + protected def build_path(dir) @@ -178,6 +183,7 @@ class Project < ActiveRecord::Base def create_git_repo is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path) + write_hook.delay end def destroy_git_repo @@ -205,4 +211,10 @@ class Project < ActiveRecord::Base FileUtils.rm_rf wiki_path end + def write_hook + hook_file = File.join(path, 'hooks', 'post-receive') + FileUtils.cp(File.join(::Rails.root.to_s, 'lib', 'post-receive-hook'), hook_file) + #File.chmod(0775, hook_file) # need? + rescue Exception # FIXME + end end diff --git a/app/views/git/blobs/blame.html.haml b/app/views/git/blobs/blame.html.haml index c39582dd1..aaf184b15 100644 --- a/app/views/git/blobs/blame.html.haml +++ b/app/views/git/blobs/blame.html.haml @@ -29,7 +29,9 @@ - @blame.each do |elem| %tr %td.message{ :rowspan => elem[1].length } - .commit #{link_to shortest_hash_id(elem[0].id), commit_path(@project, elem[0].id)} by #{elem[0].author} #{elem[0].author != elem[0].committer ? "(#{elem[0].committer})" : "" } + .commit + #{link_to shortest_hash_id(elem[0].id), commit_path(@project, elem[0].id)} by + #{elem[0].author.to_s.encode_to_default} #{elem[0].author != elem[0].committer ? "(#{elem[0].committer.to_s.encode_to_default})" : "" } .message %span.date= commit_date(elem[0].committed_date) %span.message= short_commit_message(elem[0].message) @@ -40,7 +42,7 @@ %td.code %pre - %div= elem[1].first + %div= elem[1].first.encode_to_default - elem[1][1..-1].each do |line| %tr @@ -49,6 +51,6 @@ - index += 1 %td.code %pre - %div= line + %div= line.encode_to_default -- content_for :sidebar, render(:partial => 'git/shared/sidebar') \ No newline at end of file +- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/blobs/show.html.haml b/app/views/git/blobs/show.html.haml index 04b3a19b3..43127ac7e 100644 --- a/app/views/git/blobs/show.html.haml +++ b/app/views/git/blobs/show.html.haml @@ -49,7 +49,7 @@ %td.blob :plain
-
#{ link_to @blob.basename, raw_path(@project, @treeish, @path) }
+
#{ link_to @blob.basename.encode_to_default, raw_path(@project, @treeish, @path) }

- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/app/views/git/commits/_commit_diff.html.haml b/app/views/git/commits/_commit_diff.html.haml index 5d387b06a..05f68c621 100644 --- a/app/views/git/commits/_commit_diff.html.haml +++ b/app/views/git/commits/_commit_diff.html.haml @@ -2,13 +2,13 @@ .content .inner - %a{ :name => h(commit_diff.a_path) } + %a{ :name => h(commit_diff.a_path.encode_to_default) } .blob_header - .size= h(commit_diff.a_path) + .size= h(commit_diff.a_path.encode_to_default) - if commit_diff.b_path.present? .buttons - = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path)) + = link_to("view file @ #{short_hash_id(@commit.id)}", blob_commit_path(@project, @commit.id, commit_diff.b_path.encode_to_default)) .clear .diff_data diff --git a/app/views/git/commits/_commits.html.haml b/app/views/git/commits/_commits.html.haml index b248234eb..9e4d9ae50 100644 --- a/app/views/git/commits/_commits.html.haml +++ b/app/views/git/commits/_commits.html.haml @@ -4,12 +4,12 @@ %table %tr %td.committers - .author #{commit.author}, #{commit_date(commit.authored_date)} + .author #{commit.author.to_s.encode_to_default}, #{commit_date(commit.authored_date)} - if commit.committer != commit.author .committer - (committed by: #{commit.committer}, #{commit_date(commit.committed_date)}) + (committed by: #{commit.committer.to_s.encode_to_default}, #{commit_date(commit.committed_date)}) %td.message - %p= link_to commit.message, commit_path(@project, commit.id) + %p= link_to commit.message.encode_to_default, commit_path(@project, commit.id) %td.trees .commit Commit: @@ -23,4 +23,4 @@ .parent Parent: %span{ :style => "float: right;"} - #{link_to short_hash_id(parent.id), tree_path(@project, :treeish => parent.id)} \ No newline at end of file + #{link_to short_hash_id(parent.id), tree_path(@project, :treeish => parent.id)} diff --git a/app/views/git/repositories/show.html.haml b/app/views/git/repositories/show.html.haml index a0e547531..41c205ebd 100644 --- a/app/views/git/repositories/show.html.haml +++ b/app/views/git/repositories/show.html.haml @@ -37,10 +37,10 @@ = image_tag("git/icons/folder_16.png") %td.tree_element - if entry.is_a?(Grit::Blob) - = link_to entry.name, blob_path(@project, @treeish, File.join([@path, entry.name].compact)) + = link_to entry.name.encode_to_default, blob_path(@project, @treeish, File.join([@path, entry.name.encode_to_default].compact)) - else - = link_to "#{entry.name}/", tree_path(@project, @treeish, File.join([@path, entry.name].compact)) + = link_to "#{entry.name.encode_to_default}/", tree_path(@project, @treeish, File.join([@path, entry.name.encode_to_default].compact)) %td==   %td.last==   -- content_for :sidebar, render(:partial => 'git/shared/sidebar') \ No newline at end of file +- content_for :sidebar, render(:partial => 'git/shared/sidebar') diff --git a/config/initializers/core.rb b/config/initializers/core.rb new file mode 100644 index 000000000..fe7ee4f48 --- /dev/null +++ b/config/initializers/core.rb @@ -0,0 +1 @@ +require './lib/ext/core/string' diff --git a/db/migrate/20120131141651_write_git_hook_to_projects.rb b/db/migrate/20120131141651_write_git_hook_to_projects.rb new file mode 100644 index 000000000..d8dded706 --- /dev/null +++ b/db/migrate/20120131141651_write_git_hook_to_projects.rb @@ -0,0 +1,13 @@ +class WriteGitHookToProjects < ActiveRecord::Migration + def self.up + origin_hook = File.join(::Rails.root.to_s, 'lib', 'post-receive-hook') + Project.all.each do |project| + hook_file = File.join(project.path, 'hooks', 'post-receive') + FileUtils.cp(origin_hook, hook_file) + end + end + + def self.down + Project.all.each { |project| FileUtils.rm_rf File.join(project.path, 'hooks', 'post-receive')} + end +end diff --git a/lib/ext/core/string.rb b/lib/ext/core/string.rb new file mode 100644 index 000000000..1810ec2f7 --- /dev/null +++ b/lib/ext/core/string.rb @@ -0,0 +1,5 @@ +class String + def encode_to_default + force_encoding(Encoding.default_internal || Encoding::UTF_8) + end +end diff --git a/lib/post-receive-hook b/lib/post-receive-hook new file mode 100755 index 000000000..b577daa5b --- /dev/null +++ b/lib/post-receive-hook @@ -0,0 +1,15 @@ +#!/bin/bash + +# This file was placed here by rosa-team. It makes sure that your pushed commits will be processed properly. + +pwd=`pwd` +reponame=`basename $pwd .git` +owner=`basename \`dirname $pwd\`` + +while read oldrev newrev ref +do + newrev_type=$(git cat-file -t $newrev 2> /dev/null) + oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null) + + /bin/bash -l -c "cd /srv/rosa_build/current && bundle exec rails runner 'Project.delay.process_hook(\"$owner\", \"$reponame\", \"$newrev\", \"$oldrev\", \"$ref\", \"$newrev_type\", \"$oldrev_type\")'" +done \ No newline at end of file