1
1
# frozen_string_literal: true
2
2
3
- module Git
3
+ require_relative 'diff_path_status'
4
+ require_relative 'diff_stats'
4
5
5
- # object that holds the last X commits on given branch
6
+ module Git
7
+ # object that holds the diff between two commits
6
8
class Diff
7
9
include Enumerable
8
10
@@ -12,63 +14,68 @@ def initialize(base, from = nil, to = nil)
12
14
@to = to && to . to_s
13
15
14
16
@path = nil
15
- @full_diff = nil
16
17
@full_diff_files = nil
17
- @stats = nil
18
18
end
19
19
attr_reader :from , :to
20
20
21
- def name_status
22
- cache_name_status
23
- end
24
-
25
21
def path ( path )
26
22
@path = path
27
23
self
28
24
end
29
25
30
- def size
31
- cache_stats
32
- @stats [ :total ] [ :files ]
26
+ def patch
27
+ @base . lib . diff_full ( @from , @to , { path_limiter : @path } )
33
28
end
29
+ alias_method :to_s , :patch
34
30
35
- def lines
36
- cache_stats
37
- @stats [ :total ] [ :lines ]
31
+ def [] ( key )
32
+ process_full
33
+ @full_diff_files . assoc ( key ) [ 1 ]
38
34
end
39
35
40
- def deletions
41
- cache_stats
42
- @stats [ :total ] [ :deletions ]
36
+ def each ( & block )
37
+ process_full
38
+ @full_diff_files . map { | file | file [ 1 ] } . each ( & block )
43
39
end
44
40
45
- def insertions
46
- cache_stats
47
- @stats [ :total ] [ :insertions ]
41
+ #
42
+ # DEPRECATED METHODS
43
+ #
44
+
45
+ def name_status
46
+ Git ::Deprecation . warn ( "Git::Diff#name_status is deprecated. Use Git::Base#diff_path_status instead." )
47
+ path_status_provider . to_h
48
48
end
49
49
50
- def stats
51
- cache_stats
52
- @stats
50
+ def size
51
+ Git :: Deprecation . warn ( "Git::Diff#size is deprecated. Use Git::Base#diff_stats(...).total[:files] instead." )
52
+ stats_provider . total [ :files ]
53
53
end
54
54
55
- # if file is provided and is writable, it will write the patch into the file
56
- def patch ( file = nil )
57
- cache_full
58
- @full_diff
55
+
56
+
57
+ def lines
58
+ Git ::Deprecation . warn ( "Git::Diff#lines is deprecated. Use Git::Base#diff_stats(...).lines instead." )
59
+ stats_provider . lines
59
60
end
60
- alias_method :to_s , :patch
61
61
62
- # enumerable methods
62
+ def deletions
63
+ Git ::Deprecation . warn ( "Git::Diff#deletions is deprecated. Use Git::Base#diff_stats(...).deletions instead." )
64
+ stats_provider . deletions
65
+ end
63
66
64
- def [] ( key )
65
- process_full
66
- @full_diff_files . assoc ( key ) [ 1 ]
67
+ def insertions
68
+ Git :: Deprecation . warn ( "Git::Diff#insertions is deprecated. Use Git::Base#diff_stats(...).insertions instead." )
69
+ stats_provider . insertions
67
70
end
68
71
69
- def each ( &block ) # :yields: each Git::DiffFile in turn
70
- process_full
71
- @full_diff_files . map { |file | file [ 1 ] } . each ( &block )
72
+ def stats
73
+ Git ::Deprecation . warn ( "Git::Diff#stats is deprecated. Use Git::Base#diff_stats instead." )
74
+ # CORRECTED: Re-create the original hash structure for backward compatibility
75
+ {
76
+ files : stats_provider . files ,
77
+ total : stats_provider . total
78
+ }
72
79
end
73
80
74
81
class DiffFile
@@ -102,56 +109,48 @@ def blob(type = :dst)
102
109
103
110
private
104
111
105
- def cache_full
106
- @full_diff ||= @base . lib . diff_full ( @from , @to , { :path_limiter => @path } )
107
- end
108
-
109
- def process_full
110
- return if @full_diff_files
111
- cache_full
112
- @full_diff_files = process_full_diff
113
- end
112
+ def process_full
113
+ return if @full_diff_files
114
+ @full_diff_files = process_full_diff
115
+ end
114
116
115
- def cache_stats
116
- @stats ||= @base . lib . diff_stats ( @from , @to , { :path_limiter => @path } )
117
- end
117
+ # CORRECTED: Pass the @path variable to the new objects
118
+ def path_status_provider
119
+ @path_status_provider ||= Git ::DiffPathStatus . new ( @base , @from , @to , @path )
120
+ end
118
121
119
- def cache_name_status
120
- @name_status ||= @base . lib . diff_name_status ( @from , @to , { :path => @path } )
121
- end
122
+ # CORRECTED: Pass the @path variable to the new objects
123
+ def stats_provider
124
+ @stats_provider ||= Git ::DiffStats . new ( @base , @from , @to , @path )
125
+ end
122
126
123
- # break up @diff_full
124
- def process_full_diff
125
- defaults = {
126
- :mode => '' ,
127
- :src => '' ,
128
- :dst => '' ,
129
- :type => 'modified'
130
- }
131
- final = { }
132
- current_file = nil
133
- @full_diff . split ( "\n " ) . each do |line |
134
- if m = %r{\A diff --git ("?)a/(.+?)\1 ("?)b/(.+?)\3 \z } . match ( line )
135
- current_file = Git ::EscapedPath . new ( m [ 2 ] ) . unescape
136
- final [ current_file ] = defaults . merge ( { :patch => line , :path => current_file } )
137
- else
138
- if m = /^index ([0-9a-f]{4,40})\. \. ([0-9a-f]{4,40})( ......)*/ . match ( line )
139
- final [ current_file ] [ :src ] = m [ 1 ]
140
- final [ current_file ] [ :dst ] = m [ 2 ]
141
- final [ current_file ] [ :mode ] = m [ 3 ] . strip if m [ 3 ]
142
- end
143
- if m = /^([[:alpha:]]*?) file mode (......)/ . match ( line )
144
- final [ current_file ] [ :type ] = m [ 1 ]
145
- final [ current_file ] [ :mode ] = m [ 2 ]
146
- end
147
- if m = /^Binary files / . match ( line )
148
- final [ current_file ] [ :binary ] = true
149
- end
150
- final [ current_file ] [ :patch ] << "\n " + line
127
+ def process_full_diff
128
+ defaults = {
129
+ mode : '' , src : '' , dst : '' , type : 'modified'
130
+ }
131
+ final = { }
132
+ current_file = nil
133
+ patch . split ( "\n " ) . each do |line |
134
+ if m = %r{\A diff --git ("?)a/(.+?)\1 ("?)b/(.+?)\3 \z } . match ( line )
135
+ current_file = Git ::EscapedPath . new ( m [ 2 ] ) . unescape
136
+ final [ current_file ] = defaults . merge ( { patch : line , path : current_file } )
137
+ else
138
+ if m = /^index ([0-9a-f]{4,40})\. \. ([0-9a-f]{4,40})( ......)*/ . match ( line )
139
+ final [ current_file ] [ :src ] = m [ 1 ]
140
+ final [ current_file ] [ :dst ] = m [ 2 ]
141
+ final [ current_file ] [ :mode ] = m [ 3 ] . strip if m [ 3 ]
142
+ end
143
+ if m = /^([[:alpha:]]*?) file mode (......)/ . match ( line )
144
+ final [ current_file ] [ :type ] = m [ 1 ]
145
+ final [ current_file ] [ :mode ] = m [ 2 ]
146
+ end
147
+ if m = /^Binary files / . match ( line )
148
+ final [ current_file ] [ :binary ] = true
151
149
end
150
+ final [ current_file ] [ :patch ] << "\n " + line
152
151
end
153
- final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
154
152
end
155
-
153
+ final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
154
+ end
156
155
end
157
156
end
0 commit comments