11module Git
2-
2+
33 # object that holds the last X commits on given branch
44 class Diff
55 include Enumerable
6-
6+
77 def initialize ( base , from = nil , to = nil )
88 @base = base
9- @from = from && from . to_s
9+ @from = from && from . to_s
1010 @to = to && to . to_s
1111
1212 @path = nil
@@ -15,60 +15,60 @@ def initialize(base, from = nil, to = nil)
1515 @stats = nil
1616 end
1717 attr_reader :from , :to
18-
18+
1919 def path ( path )
2020 @path = path
2121 return self
2222 end
23-
23+
2424 def size
2525 cache_stats
2626 @stats [ :total ] [ :files ]
2727 end
28-
28+
2929 def lines
3030 cache_stats
3131 @stats [ :total ] [ :lines ]
3232 end
33-
33+
3434 def deletions
3535 cache_stats
3636 @stats [ :total ] [ :deletions ]
3737 end
38-
38+
3939 def insertions
4040 cache_stats
4141 @stats [ :total ] [ :insertions ]
4242 end
43-
43+
4444 def stats
4545 cache_stats
4646 @stats
4747 end
48-
48+
4949 # if file is provided and is writable, it will write the patch into the file
5050 def patch ( file = nil )
5151 cache_full
5252 @full_diff
5353 end
5454 alias_method :to_s , :patch
55-
55+
5656 # enumerable methods
57-
57+
5858 def []( key )
5959 process_full
6060 @full_diff_files . assoc ( key ) [ 1 ]
6161 end
62-
62+
6363 def each ( &block ) # :yields: each Git::DiffFile in turn
6464 process_full
6565 @full_diff_files . map { |file | file [ 1 ] } . each ( &block )
6666 end
67-
67+
6868 class DiffFile
6969 attr_accessor :patch , :path , :mode , :src , :dst , :type
7070 @base = nil
71-
71+
7272 def initialize ( base , hash )
7373 @base = base
7474 @patch = hash [ :patch ]
@@ -83,7 +83,7 @@ def initialize(base, hash)
8383 def binary?
8484 !!@binary
8585 end
86-
86+
8787 def blob ( type = :dst )
8888 if type == :src
8989 @base . object ( @src ) if @src != '0000000'
@@ -92,28 +92,28 @@ def blob(type = :dst)
9292 end
9393 end
9494 end
95-
95+
9696 private
97-
97+
9898 def cache_full
9999 unless @full_diff
100100 @full_diff = @base . lib . diff_full ( @from , @to , { :path_limiter => @path } )
101101 end
102102 end
103-
103+
104104 def process_full
105105 unless @full_diff_files
106106 cache_full
107107 @full_diff_files = process_full_diff
108108 end
109109 end
110-
110+
111111 def cache_stats
112112 unless @stats
113113 @stats = @base . lib . diff_stats ( @from , @to , { :path_limiter => @path } )
114114 end
115115 end
116-
116+
117117 # break up @diff_full
118118 def process_full_diff
119119 defaults = {
@@ -124,7 +124,10 @@ def process_full_diff
124124 }
125125 final = { }
126126 current_file = nil
127- full_diff_utf8_encoded = @full_diff . encode ( "UTF-8" , "binary" , :invalid => "replace" , :undef => "replace" )
127+ full_diff_utf8_encoded = @full_diff . encode ( "UTF-8" , "binary" , {
128+ :invalid => :replace ,
129+ :undef => :replace
130+ } )
128131 full_diff_utf8_encoded . split ( "\n " ) . each do |line |
129132 if m = /^diff --git a\/ (.*?) b\/ (.*?)/ . match ( line )
130133 current_file = m [ 1 ]
@@ -142,11 +145,11 @@ def process_full_diff
142145 if m = /^Binary files / . match ( line )
143146 final [ current_file ] [ :binary ] = true
144147 end
145- final [ current_file ] [ :patch ] << "\n " + line
148+ final [ current_file ] [ :patch ] << "\n " + line
146149 end
147150 end
148151 final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
149152 end
150-
153+
151154 end
152155end
0 commit comments