git文件比较功能

This commit is contained in:
guange 2016-11-26 17:47:39 +08:00
parent c2629ebdc0
commit a2439f0a14
7 changed files with 2542 additions and 2473 deletions

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20161117060138) do
ActiveRecord::Schema.define(:version => 20161125024643) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -996,19 +996,6 @@ ActiveRecord::Schema.define(:version => 20161117060138) do
t.datetime "updated_at", :null => false
end
create_table "ii", :force => true do |t|
t.integer "project_id"
t.string "author_login"
t.string "rep_identifier"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "sonar_version", :default => 0
t.string "path"
t.string "branch"
t.string "language"
t.string "sonar_name"
end
create_table "innodb_monitor", :id => false, :force => true do |t|
t.integer "a"
end
@ -1634,6 +1621,19 @@ ActiveRecord::Schema.define(:version => 20161117060138) do
t.datetime "updated_at", :null => false
end
create_table "quality_analyses", :force => true do |t|
t.integer "project_id"
t.string "author_login"
t.string "rep_identifier"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "sonar_version", :default => 0
t.string "path"
t.string "branch"
t.string "language"
t.string "sonar_name"
end
create_table "queries", :force => true do |t|
t.integer "project_id"
t.string "name", :default => "", :null => false

View File

@ -1,3 +1,4 @@
module Trustie
module Gitlab
module Diff
class File
@ -20,7 +21,7 @@ module Gitlab
end
def parser
Gitlab::Diff::Parser.new
Parser.new
end
def raw_diff
@ -55,3 +56,4 @@ module Gitlab
end
end
end
end

View File

@ -1,3 +1,4 @@
module Trustie
module Gitlab
module Diff
class Line
@ -18,3 +19,4 @@ module Gitlab
end
end
end
end

View File

@ -1,3 +1,4 @@
module Trustie
module Gitlab
module Diff
class LineCode
@ -7,3 +8,5 @@ module Gitlab
end
end
end
end

View File

@ -1,3 +1,4 @@
module Trustie
module Gitlab
module Diff
class Parser
@ -11,13 +12,13 @@ module Gitlab
line_new = 1
type = nil
lines_arr = ::Gitlab::InlineDiff.processing lines
lines_arr = InlineDiff.processing lines
lines_arr.each do |line|
next if filename?(line)
full_line = html_escape(line.gsub(/\n/, ''))
full_line = ::Gitlab::InlineDiff.replace_markers full_line
full_line = InlineDiff.replace_markers full_line
if line.match(/^@@ -/)
type = "match"
@ -26,12 +27,12 @@ module Gitlab
line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
next if line_old <= 1 && line_new <= 1 #top of file
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
lines_obj << Line.new(full_line, type, line_obj_index, line_old, line_new)
line_obj_index += 1
next
else
type = identification_type(line)
lines_obj << Gitlab::Diff::Line.new(full_line, type, line_obj_index, line_old, line_new)
lines_obj << Line.new(full_line, type, line_obj_index, line_old, line_new)
line_obj_index += 1
end
@ -77,3 +78,4 @@ module Gitlab
end
end
end
end

View File

@ -1,3 +1,4 @@
module Trustie
module Gitlab
class InlineDiff
class << self
@ -102,3 +103,4 @@ module Gitlab
end
end
end
end

View File

@ -0,0 +1,58 @@
require 'spec_helper'
require 'rails_helper'
require 'ostruct'
describe "Git diff" do
before(:each) do
@content = '''
[{"diff":"--- a/readme\n+++ b/readme\n@@ -1,7 +1,8 @@\n 邀请界面\n 1.1 界面设计 (已处理)\n- 1.2 改写成html功能 (已处理)\n+sdkfsjkdf\n 1.3 集成到游戏邀请功能 (已处理)\n+adfjsdajlfk\n \n 微信登录功能\n 2.1 集成sdk (已处理)\n","new_path":"readme","old_path":"readme","a_mode":"100644","b_mode":"100644","new_file":false,"renamed_file":false,"deleted_file":false}]
'''
@output = %Q{
| 1 | 1 |
| 2 | 2 | 1.1
| 3 | |-<span class='idiff'> 1.2 html功能 </span>
| | 3 |+<span class='idiff'>sdkfsjkdf</span>
| 4 | 4 | 1.3
| | 5 |+adfjsdajlfk
| 5 | 6 | } +
%Q{
| 6 | 7 |
| 7 | 8 | 2.1 sdk
}
end
it "正确解析diff文件" do
diff = ActiveSupport::JSON.decode(@content).first
diff = OpenStruct.new(diff)
diff_file = Trustie::Gitlab::Diff::File.new(diff)
output = ''
diff_file.diff_lines.each_with_index do |line, index|
type = line.type
last_line = line.new_pos
line_code = Trustie::Gitlab::Diff::LineCode.generate('diff.file_path', line.new_pos, line.old_pos)
line_old = line.old_pos.to_s
#puts "type: #{type} last_line: #{last_line} last_code: #{line_code} line_old: #{line_old} text: #{line.text}"
if type == 'match'
## 表示没有修改,两个都要显示行号
output += "|#{line_old.center(4)}|#{line.new_pos.to_s.center(4)}|#{line.text}\n"
else
old_line = type == 'new' ? ' '*4: line_old
new_line = type == 'old' ? ' '*4: line.new_pos
output += "|#{old_line.to_s.center(4)}|#{new_line.to_s.center(4)}|#{line.text}\n"
end
end
puts output
expect(output.strip).to eq(@output.strip)
end
end