Merge pull request #747 from warpc/674-diff_between_commits

[refs #674] Diff between commits
This commit is contained in:
Vladimir Sharshov 2012-12-03 12:25:34 -08:00
commit c025bec679
17 changed files with 207 additions and 107 deletions

View File

@ -1,7 +1,7 @@
# -*- encoding : utf-8 -*-
class Projects::Git::BaseController < Projects::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show, :index, :blame, :raw, :archive] if APP_CONFIG['anonymous_access']
skip_before_filter :authenticate_user!, :only => [:show, :index, :blame, :raw, :archive, :diff] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :project
before_filter :set_treeish_and_path

View File

@ -18,4 +18,32 @@ class Projects::Git::CommitsController < Projects::Git::BaseController
format.patch { render :text => (@commit.to_patch rescue ''), :content_type => "text/plain" }
end
end
def diff
if params[:commit2].present?
params1 = params[:commit1]
params2 = params[:commit2] == 'HEAD' ? @project.default_branch : params[:commit2]
else # get only one parameter
params1 = @project.default_branch
params2 = params[:commit1]
end
params1.sub! 'HEAD', @project.default_branch
params2.sub! 'HEAD', @project.default_branch
ref1 = if @project.repo.branches_and_tags.include? params1
@project.repo.commits(params1).first
else
params1 # possible commit hash
end
@commit1 = @project.repo.commit(ref1) || raise(ActiveRecord::RecordNotFound)
ref = if @project.repo.branches_and_tags.include? params2
@project.repo.commits(params2).first
else
params2 # possible commit hash
end
@commit = @project.repo.commit(ref) || raise(ActiveRecord::RecordNotFound)
@common_ancestor = @project.repo.commit(@project.repo.git.merge_base({}, @commit1, @commit)) || @commit1
@stats = @project.repo.diff_stats @commit1.id, @commit.id
end
end

View File

@ -36,9 +36,15 @@ module BuildListsHelper
end
def build_list_version_link(bl, str_version = false)
hash_size=5
if bl.commit_hash.present?
link_to str_version ? "#{shortest_hash_id bl.commit_hash} ( #{bl.project_version} )" : shortest_hash_id(bl.commit_hash),
commit_path(bl.project.owner, bl.project, bl.commit_hash)
if bl.last_published_commit_hash.present?
link_to "#{shortest_hash_id bl.last_published_commit_hash, hash_size}...#{shortest_hash_id bl.commit_hash, hash_size}",
diff_path(bl.project.owner, bl.project, bl.last_published_commit_hash) + "...#{bl.commit_hash}"
else
link_to str_version ? "#{shortest_hash_id bl.commit_hash, hash_size}" : shortest_hash_id(bl.commit_hash, hash_size),
commit_path(bl.project.owner, bl.project, bl.commit_hash)
end
else
bl.project_version
end

View File

@ -34,8 +34,8 @@ module CommitHelper
id[0..19]
end
def shortest_hash_id(id)
id[0..9]
def shortest_hash_id(id, size=10)
id[0..size-1]
end
def short_commit_message(message)

View File

@ -1,7 +1,7 @@
# -*- encoding : utf-8 -*-
module DiffHelper
def render_diff_stats(stats)
path = @pull.id ? polymorphic_path([@project, @pull]) : ''
path = @pull.try(:id) ? polymorphic_path([@project, @pull]) : ''
res = ["<table class='commit_stats'>"]
stats.each_with_index do |stat, ind|
res << "<tr>"
@ -51,12 +51,14 @@ module DiffHelper
########################################################
def prepare(args)
@url, @diff_counter, @in_discussion = args[:url], args[:diff_counter], args[:in_discussion]
@filepath, @line_comments, @in_wiki = args[:filepath], args[:comments], args[:in_wiki]
@filepath, @line_comments = args[:filepath], args[:comments]
@add_reply_id, @num_line = if @in_discussion
[@line_comments[0].id, @line_comments[0].data[:line].to_i - @line_comments[0].data[:strings].lines.count.to_i-1]
else
[nil, -1]
end
@no_commit_comment = true if params[:controller] == 'projects/wiki' || (params[:action] == 'diff')
end
def headerline(line)
@ -225,12 +227,12 @@ module DiffHelper
end
def line_comment
return if @in_wiki || (@in_discussion && @add_reply_id && @line_comments[0].data[:line].to_i != @num_line)
return if @no_commit_comment || (@in_discussion && @add_reply_id && @line_comments[0].data[:line].to_i != @num_line)
link_to image_tag('line_comment.png', :alt => t('layout.comments.new_header')), new_comment_path, :class => 'add_line-comment'
end
def render_line_comments
unless @in_wiki || @in_discussion
unless @no_commit_comment || @in_discussion
comments = @line_comments.select do |c|
c.data.try('[]', :line) == @num_line.to_s && c.actual_inline_comment?
end
@ -243,7 +245,7 @@ module DiffHelper
end
def tr_line_comments comments
return if @in_wiki
return if @no_commit_comment
res="<tr class='inline-comments'>
<td class='line_numbers' colspan='2'>#{comments.count}</td>
<td>"

View File

@ -218,14 +218,9 @@ class BuildList < ActiveRecord::Base
def actualize_packages
ActiveRecord::Base.transaction do
old_pkgs = self.class.where(:project_id => self.project_id)
.where(:save_to_repository_id => self.save_to_repository_id)
.for_platform(self.build_for_platform_id)
.scoped_to_arch(self.arch_id)
.for_status(BUILD_PUBLISHED)
.recent.limit(2).last.packages # packages from previous build_list
old_pkgs.update_all(:actual => false)
self.packages.update_all(:actual => true)
# packages from previous build_list
self.last_published.packages.update_all :actual => false
self.packages.update_all :actual => true
end
end
@ -437,4 +432,13 @@ class BuildList < ActiveRecord::Base
yield p
end
end
def last_published
BuildList.where(:project_id => self.project_id,
:save_to_repository_id => self.save_to_repository_id)
.for_platform(self.build_for_platform_id)
.scoped_to_arch(self.arch_id)
.for_status(BUILD_PUBLISHED)
.recent.limit(2).last
end
end

View File

@ -104,33 +104,12 @@ class PullRequest < ActiveRecord::Base
alias_method :to_commit, :common_ancestor
def diff_stats
stats = []
Dir.chdir(path) do
lines = repo.git.native(:diff, {:numstat => true, :M => true}, "#{to_commit.id}...#{from_commit.id}").split("\n")
while !lines.empty?
files = []
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/
additions, deletions, filename = lines.shift.gsub(' => ', '=>').split
additions, deletions = additions.to_i, deletions.to_i
stat = Grit::DiffStat.new filename, additions, deletions
stats << stat
end
end
stats
end
@diff_stats ||= repo.diff_stats(to_commit.id, from_commit.id)
end
# FIXME maybe move to warpc/grit?
def diff
return @diff if @diff.present?
diff = repo.git.native('diff', {:M => true}, "#{to_commit.id}...#{from_commit.id}")
if diff =~ /diff --git a/
diff = diff.sub(/.*?(diff --git a)/m, '\1')
else
diff = ''
end
@diff = Grit::Diff.list_from_string(repo, diff)
@diff ||= repo.diff(to_commit.id, from_commit.id)
end
def set_user_and_time user

View File

@ -47,7 +47,7 @@
.leftlist= t("activerecord.attributes.build_list.auto_publish")
.rightlist= t("layout.#{@build_list.auto_publish}_")
.both
.leftlist= t("activerecord.attributes.build_list.project_version")
.leftlist= t("diff")
.rightlist= build_list_version_link(@build_list, true)
.both
.leftlist= t("activerecord.attributes.build_list.arch")

View File

@ -6,5 +6,6 @@
- if commit_diff.b_path.present?
.r= link_to "view file @ #{short_hash_id(commit_id)}", blob_path(@project, commit_id, commit_diff.b_path)
.clear
-unless (@project.repo.tree(commit_id) / commit_diff.b_path).binary?
-if commit_diff.diff.present? && !(@project.repo.tree(commit_id) / commit_diff.a_path).binary?
="a_path=#{commit_diff.a_path}; b_path=#{commit_diff.b_path}" if (@project.repo.tree(commit_id) / commit_diff.b_path).nil?
.diff_data=render_diff(commit_diff, :diff_counter => commit_diff_counter, :comments => @comments)

View File

@ -0,0 +1,30 @@
-title = "#{t('diff')} #{shortest_hash_id @commit1.id}...#{shortest_hash_id @commit.id}"
-set_meta_tags :title => [title_object(@project), title]
=render 'submenu'
%h3=title
.both
#repo-wrapper
.leftside
-total_additions = @stats.inject(0) {|sum, n| sum + n.additions}
-total_deletions = @stats.inject(0) {|sum, n| sum + n.deletions}
%h5= t("layout.projects.diff_show_header",
:files => t("layout.projects.commit_files_count", :count => @stats.count),
:additions => t("layout.projects.commit_additions_count", :count => total_additions),
:deletions => t("layout.projects.commit_deletions_count", :count => total_deletions))
.both
-begin
=render_diff_stats @stats
-@project.repo.diff(@commit1.id, @commit.id).each_with_index do |commit_diff, diff_counter|
- commit_id = commit_diff.deleted_file ? @common_ancestor.id : @commit.id
.file
%a{:name => "diff-#{diff_counter}"}
.top
.l= h(commit_diff.a_path.rtruncate 120)
- if commit_diff.b_path.present?
.r= link_to "view file @ #{short_hash_id(commit_id)}", blob_path(@project, commit_id, commit_diff.b_path)
.clear
-if commit_diff.diff.present? && !(@project.repo.tree(commit_id) / commit_diff.b_path).binary?
.diff_data=render_diff(commit_diff, :diff_counter => diff_counter)
- rescue Grit::Git::GitTimeout
%p= t 'layout.git.repositories.commit_diff_too_big'

View File

@ -200,3 +200,4 @@ en:
into: into
from: from
by: by
diff: Diff

View File

@ -201,3 +201,4 @@ ru:
into: в
from: из
by: от
diff: Изменения

View File

@ -307,6 +307,8 @@ Rosa::Application.routes.draw do
get '/raw/:treeish/*path' => "git/blobs#raw", :as => :raw, :format => false
# Archive
get '/archive/:treeish.:format' => "git/trees#archive", :as => :archive, :format => /zip|tar\.gz/
# Git diff
get '/diff/:commit1(...:commit2)' => "git/commits#diff", :as => :diff
end
end
end

View File

@ -0,0 +1,5 @@
class AddLastPublishedCommitHashToBuildList < ActiveRecord::Migration
def change
add_column :build_lists, :last_published_commit_hash, :string
end
end

View File

@ -11,14 +11,14 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121127122032) do
ActiveRecord::Schema.define(:version => 20121203142727) do
create_table "activity_feeds", :force => true do |t|
t.integer "user_id", :null => false
t.string "kind"
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "advisories", :force => true do |t|
@ -53,8 +53,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "arches", ["name"], :name => "index_arches_on_name", :unique => true
@ -63,8 +63,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "authentications", ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true
@ -75,8 +75,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.integer "level"
t.integer "status"
t.integer "build_list_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "version"
end
@ -110,21 +110,21 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.integer "project_id"
t.integer "arch_id"
t.datetime "notified_at"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_circle", :default => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "is_circle", :default => false
t.text "additional_repos"
t.string "name"
t.boolean "build_requires", :default => false
t.boolean "build_requires", :default => false
t.string "update_type"
t.integer "build_for_platform_id"
t.integer "save_to_platform_id"
t.text "include_repos"
t.integer "user_id"
t.boolean "auto_publish", :default => true
t.boolean "auto_publish", :default => true
t.string "package_version"
t.string "commit_hash"
t.integer "priority", :default => 0, :null => false
t.integer "priority", :default => 0, :null => false
t.datetime "started_at"
t.integer "duration"
t.integer "advisory_id"
@ -132,6 +132,7 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.integer "save_to_repository_id"
t.text "results"
t.boolean "new_core"
t.string "last_published_commit_hash"
end
add_index "build_lists", ["advisory_id"], :name => "index_build_lists_on_advisory_id"
@ -144,8 +145,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "commentable_type"
t.integer "user_id"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.decimal "commentable_id", :precision => 50, :scale => 0
t.integer "project_id"
t.text "data"
@ -163,8 +164,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "controller"
t.string "action"
t.text "message"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "flash_notifies", :force => true do |t|
@ -178,8 +179,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "groups", :force => true do |t|
t.integer "owner_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "uname"
t.integer "own_projects_count", :default => 0, :null => false
t.text "description"
@ -196,8 +197,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "title"
t.text "body"
t.string "status", :default => "open"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.datetime "closed_at"
t.integer "closed_by"
@ -257,14 +258,14 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "description"
t.string "name", :null => false
t.integer "parent_platform_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "released", :default => false, :null => false
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open", :null => false
t.string "platform_type", :default => "main", :null => false
t.string "distrib_type", :null => false
t.string "distrib_type"
end
add_index "platforms", ["name"], :name => "index_platforms_on_name", :unique => true, :case_sensitive => false
@ -273,16 +274,16 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.integer "platform_id"
t.string "login"
t.string "password"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end
create_table "product_build_lists", :force => true do |t|
t.integer "product_id"
t.integer "status", :default => 2, :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "status", :default => 3, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "project_id"
t.string "project_version"
t.string "commit_hash"
@ -298,8 +299,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "products", :force => true do |t|
t.string "name", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "description"
t.integer "project_id"
t.string "params"
@ -312,8 +313,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "name"
t.string "version"
t.datetime "file_mtime"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "platform_id"
end
@ -322,25 +323,25 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "project_to_repositories", :force => true do |t|
t.integer "project_id"
t.integer "repository_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open"
t.text "description"
t.string "ancestry"
t.boolean "has_issues", :default => true
t.boolean "has_wiki", :default => false
t.string "srpm_file_name"
t.string "srpm_content_type"
t.integer "srpm_file_size"
t.datetime "srpm_updated_at"
t.string "srpm_content_type"
t.boolean "has_wiki", :default => false
t.string "default_branch", :default => "master"
t.boolean "is_package", :default => true, :null => false
t.integer "average_build_time", :default => 0, :null => false
@ -370,8 +371,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "token"
t.boolean "approved", :default => false
t.boolean "rejected", :default => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "interest"
t.text "more"
t.string "language"
@ -385,16 +386,16 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "actor_type"
t.integer "target_id"
t.string "target_type"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "role"
end
create_table "repositories", :force => true do |t|
t.string "description", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name", :null => false
t.boolean "publish_without_qa", :default => true
end
@ -406,8 +407,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.boolean "new_comment_reply", :default => true
t.boolean "new_issue", :default => true
t.boolean "issue_assign", :default => true
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "new_comment_commit_owner", :default => true
t.boolean "new_comment_commit_repo_owner", :default => true
t.boolean "new_comment_commit_commentor", :default => true
@ -418,8 +419,8 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "subscribes", :force => true do |t|
t.string "subscribeable_type"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "status", :default => true
t.integer "project_id"
t.decimal "subscribeable_id", :precision => 50, :scale => 0
@ -427,18 +428,21 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "ssh_key"
t.string "uname"
t.string "role"
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.text "professional_experience"
t.string "site"
t.string "company"
@ -447,14 +451,11 @@ ActiveRecord::Schema.define(:version => 20121127122032) do
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "failed_attempts", :default => 0
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "authentication_token"
t.integer "build_priority", :default => 50
t.integer "build_priority", :default => 50
end
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token"

View File

@ -91,6 +91,41 @@ module Grit
def branches_and_tags
branches + tags # @branches_and_tags ||= # ???
end
def diff(a, b, *paths)
diff = self.git.native('diff', {:M => true}, "#{a}...#{b}", '--', *paths)
if diff =~ /diff --git a/
diff = diff.sub(/.*?(diff --git a)/m, '\1')
else
diff = ''
end
Diff.list_from_string(self, diff)
end
# The diff stats for the given treeish
# git diff --numstat -M a...b
#
# +a+ is the base treeish
# +b+ is the head treeish
#
# Returns Grit::DiffStat[]
def diff_stats(a,b)
stats = []
Dir.chdir(path) do
lines = self.git.native(:diff, {:numstat => true, :M => true}, "#{a}...#{b}").split("\n")
while !lines.empty?
files = []
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/
additions, deletions, filename = lines.shift.gsub(' => ', '=>').split
additions, deletions = additions.to_i, deletions.to_i
stat = DiffStat.new filename, additions, deletions
stats << stat
end
end
stats
end
end
end
end

View File

@ -5,7 +5,7 @@ module Modules
extend ActiveSupport::Concern
included do
validate lambda {
if project && (commit_hash.blank? || project.repo.commit(commit_hash).blank?)
errors.add :commit_hash, I18n.t('flash.build_list.wrong_commit_hash', :commit_hash => commit_hash)
@ -13,6 +13,7 @@ module Modules
}
before_validation :set_commit_and_version
before_create :set_last_published_commit
end
protected
@ -25,6 +26,10 @@ module Modules
self.project_version = commit_hash
end
end
def set_last_published_commit
self.last_published_commit_hash = self.last_published.try :commit_hash
end
end
end
end
end