11# frozen_string_literal: true
22
3- module Git
3+ require_relative 'diff_path_status'
4+ require_relative 'diff_stats'
45
5- # object that holds the last X commits on given branch
6+ module Git
7+ # object that holds the diff between two commits
68 class Diff
79 include Enumerable
810
@@ -12,63 +14,68 @@ def initialize(base, from = nil, to = nil)
1214 @to = to && to . to_s
1315
1416 @path = nil
15- @full_diff = nil
1617 @full_diff_files = nil
17- @stats = nil
1818 end
1919 attr_reader :from , :to
2020
21- def name_status
22- cache_name_status
23- end
24-
2521 def path ( path )
2622 @path = path
2723 self
2824 end
2925
30- def size
31- cache_stats
32- @stats [ :total ] [ :files ]
26+ def patch
27+ @base . lib . diff_full ( @from , @to , { path_limiter : @path } )
3328 end
29+ alias_method :to_s , :patch
3430
35- def lines
36- cache_stats
37- @stats [ :total ] [ :lines ]
31+ def [] ( key )
32+ process_full
33+ @full_diff_files . assoc ( key ) [ 1 ]
3834 end
3935
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 )
4339 end
4440
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
4848 end
4949
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 ]
5353 end
5454
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
5960 end
60- alias_method :to_s , :patch
6161
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
6366
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
6770 end
6871
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+ }
7279 end
7380
7481 class DiffFile
@@ -102,56 +109,48 @@ def blob(type = :dst)
102109
103110 private
104111
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
114116
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
118121
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
122126
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
151149 end
150+ final [ current_file ] [ :patch ] << "\n " + line
152151 end
153- final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
154152 end
155-
153+ final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
154+ end
156155 end
157156end
0 commit comments