@@ -4,12 +4,15 @@ module MetaCommit::Git
4
4
# Rugged::Repository wrapper
5
5
# @attr [Rugged::Repository] repo
6
6
class Repo
7
+ extend Forwardable
8
+
7
9
DIFF_OPTIONS = { :context_lines => 0 , :ignore_whitespace => true }
8
10
INDEX_DIFF_OPTIONS = { :context_lines => 0 , :ignore_whitespace => true , :reverse => true }
9
11
10
12
FILE_NOT_EXISTS_OID = '0000000000000000000000000000000000000000'
11
13
12
14
attr_accessor :repo
15
+ def_delegators :@repo , :diff , :path
13
16
14
17
# @param [String] repo_path
15
18
def initialize ( repo_path )
@@ -46,43 +49,6 @@ def walk_by_commits
46
49
end
47
50
end
48
51
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
-
86
52
# Proxy to Rugged::Index#diff
87
53
# @param [Hash] options
88
54
# @return [Rugged::Diff::Delta]
@@ -112,11 +78,6 @@ def get_content_of(rel_repo_file_path, default = nil)
112
78
content
113
79
end
114
80
115
- # @return [String] path to .git folder of repository
116
- def path
117
- @repo . path
118
- end
119
-
120
81
# @return [String] directory of repository
121
82
def dir
122
83
@repo . path . reverse . sub ( '/.git' . reverse , '' ) . reverse
@@ -135,52 +96,5 @@ def last_commit_oid
135
96
@repo . last_commit . oid
136
97
end
137
98
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
-
185
99
end
186
100
end
0 commit comments