@@ -4,12 +4,15 @@ module MetaCommit::Git
44 # Rugged::Repository wrapper
55 # @attr [Rugged::Repository] repo
66 class Repo
7+ extend Forwardable
8+
79 DIFF_OPTIONS = { :context_lines => 0 , :ignore_whitespace => true }
810 INDEX_DIFF_OPTIONS = { :context_lines => 0 , :ignore_whitespace => true , :reverse => true }
911
1012 FILE_NOT_EXISTS_OID = '0000000000000000000000000000000000000000'
1113
1214 attr_accessor :repo
15+ def_delegators :@repo , :diff , :path
1316
1417 # @param [String] repo_path
1518 def initialize ( repo_path )
@@ -46,43 +49,6 @@ def walk_by_commits
4649 end
4750 end
4851
49- # Proxy to Rugged::Repository#diff
50- # @param [Object] left
51- # @param [Object] right
52- # @param [Hash] options
53- # @return [Rugged::Diff::Delta]
54- def diff ( left , right , options )
55- @repo . diff ( left , right , options )
56- end
57-
58- # Iterates over optimized lines in diff
59- # @param [Object] left
60- # @param [Object] right
61- # @yield [old_file_path, new_file_path, patch, line]
62- # @return [Object]
63- def diff_with_optimized_lines ( left , right )
64- diff = @repo . diff ( left , right , DIFF_OPTIONS )
65- diff . deltas . zip ( diff . patches ) . each do |delta , patch |
66- lines = organize_lines ( delta , patch )
67- lines . each do |line |
68- yield ( delta . old_file [ :path ] , delta . new_file [ :path ] , patch , line )
69- end
70- end
71- end
72-
73- # Iterates over optimized lines in index diff
74- # @yield [old_file_path, new_file_path, patch, line]
75- # @return [Object]
76- def index_diff_with_optimized_lines
77- diff = index_diff ( INDEX_DIFF_OPTIONS )
78- diff . deltas . zip ( diff . patches ) . each do |delta , patch |
79- lines = organize_lines ( delta , patch )
80- lines . each do |line |
81- yield ( delta . old_file [ :path ] , delta . new_file [ :path ] , patch , line )
82- end
83- end
84- end
85-
8652 # Proxy to Rugged::Index#diff
8753 # @param [Hash] options
8854 # @return [Rugged::Diff::Delta]
@@ -112,11 +78,6 @@ def get_content_of(rel_repo_file_path, default = nil)
11278 content
11379 end
11480
115- # @return [String] path to .git folder of repository
116- def path
117- @repo . path
118- end
119-
12081 # @return [String] directory of repository
12182 def dir
12283 @repo . path . reverse . sub ( '/.git' . reverse , '' ) . reverse
@@ -135,52 +96,5 @@ def last_commit_oid
13596 @repo . last_commit . oid
13697 end
13798
138- # @param [Object] delta
139- # @param [Object] patch
140- # @return [Array<MetaCommit::Models::Line>]
141- def organize_lines ( delta , patch )
142- lines_to_walk = [ ]
143- # if whole file was changed examine one time only
144- whole_file_changed = ( delta . old_file [ :oid ] == FILE_NOT_EXISTS_OID ) || ( delta . new_file [ :oid ] == FILE_NOT_EXISTS_OID )
145- skip_walking = false
146- skip_next_line = false
147- patch . hunks . each_with_index do |hunk |
148- break if skip_walking
149- hunk . lines . each_with_index do |line , line_index |
150- break if skip_walking
151-
152- if skip_next_line
153- skip_next_line = false
154- next
155- end
156-
157- next_line = hunk . lines [ line_index + 1 ]
158- is_replace_change = ( line . deletion? ) && ( !next_line . nil? && next_line . addition? ) && ( line . old_lineno && next_line . new_lineno )
159-
160- line_to_walk = MetaCommit ::Models ::Line . new
161- if is_replace_change
162- line_to_walk . line_origin = :replace
163- line_to_walk . old_lineno = line . old_lineno
164- line_to_walk . new_lineno = next_line . new_lineno
165- line_to_walk . content_offset = next_line . content_offset
166- else
167- line_to_walk . line_origin = line . line_origin
168- line_to_walk . old_lineno = line . old_lineno
169- line_to_walk . new_lineno = line . new_lineno
170- line_to_walk . content_offset = line . content_offset
171- end
172- if is_replace_change
173- skip_next_line = true
174- end
175- lines_to_walk . push ( line_to_walk )
176-
177- skip_walking = true if whole_file_changed
178- end
179- end
180- lines_to_walk
181- end
182-
183- private :organize_lines
184-
18599 end
186100end
0 commit comments