1
1
module Git
2
-
2
+
3
3
# object that holds the last X commits on given branch
4
4
class Diff
5
5
include Enumerable
6
-
6
+
7
7
def initialize ( base , from = nil , to = nil )
8
8
@base = base
9
- @from = from && from . to_s
9
+ @from = from && from . to_s
10
10
@to = to && to . to_s
11
11
12
12
@path = nil
@@ -15,60 +15,60 @@ def initialize(base, from = nil, to = nil)
15
15
@stats = nil
16
16
end
17
17
attr_reader :from , :to
18
-
18
+
19
19
def path ( path )
20
20
@path = path
21
21
return self
22
22
end
23
-
23
+
24
24
def size
25
25
cache_stats
26
26
@stats [ :total ] [ :files ]
27
27
end
28
-
28
+
29
29
def lines
30
30
cache_stats
31
31
@stats [ :total ] [ :lines ]
32
32
end
33
-
33
+
34
34
def deletions
35
35
cache_stats
36
36
@stats [ :total ] [ :deletions ]
37
37
end
38
-
38
+
39
39
def insertions
40
40
cache_stats
41
41
@stats [ :total ] [ :insertions ]
42
42
end
43
-
43
+
44
44
def stats
45
45
cache_stats
46
46
@stats
47
47
end
48
-
48
+
49
49
# if file is provided and is writable, it will write the patch into the file
50
50
def patch ( file = nil )
51
51
cache_full
52
52
@full_diff
53
53
end
54
54
alias_method :to_s , :patch
55
-
55
+
56
56
# enumerable methods
57
-
57
+
58
58
def []( key )
59
59
process_full
60
60
@full_diff_files . assoc ( key ) [ 1 ]
61
61
end
62
-
62
+
63
63
def each ( &block ) # :yields: each Git::DiffFile in turn
64
64
process_full
65
65
@full_diff_files . map { |file | file [ 1 ] } . each ( &block )
66
66
end
67
-
67
+
68
68
class DiffFile
69
69
attr_accessor :patch , :path , :mode , :src , :dst , :type
70
70
@base = nil
71
-
71
+
72
72
def initialize ( base , hash )
73
73
@base = base
74
74
@patch = hash [ :patch ]
@@ -83,7 +83,7 @@ def initialize(base, hash)
83
83
def binary?
84
84
!!@binary
85
85
end
86
-
86
+
87
87
def blob ( type = :dst )
88
88
if type == :src
89
89
@base . object ( @src ) if @src != '0000000'
@@ -92,28 +92,28 @@ def blob(type = :dst)
92
92
end
93
93
end
94
94
end
95
-
95
+
96
96
private
97
-
97
+
98
98
def cache_full
99
99
unless @full_diff
100
100
@full_diff = @base . lib . diff_full ( @from , @to , { :path_limiter => @path } )
101
101
end
102
102
end
103
-
103
+
104
104
def process_full
105
105
unless @full_diff_files
106
106
cache_full
107
107
@full_diff_files = process_full_diff
108
108
end
109
109
end
110
-
110
+
111
111
def cache_stats
112
112
unless @stats
113
113
@stats = @base . lib . diff_stats ( @from , @to , { :path_limiter => @path } )
114
114
end
115
115
end
116
-
116
+
117
117
# break up @diff_full
118
118
def process_full_diff
119
119
defaults = {
@@ -124,7 +124,10 @@ def process_full_diff
124
124
}
125
125
final = { }
126
126
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
+ } )
128
131
full_diff_utf8_encoded . split ( "\n " ) . each do |line |
129
132
if m = /^diff --git a\/ (.*?) b\/ (.*?)/ . match ( line )
130
133
current_file = m [ 1 ]
@@ -142,11 +145,11 @@ def process_full_diff
142
145
if m = /^Binary files / . match ( line )
143
146
final [ current_file ] [ :binary ] = true
144
147
end
145
- final [ current_file ] [ :patch ] << "\n " + line
148
+ final [ current_file ] [ :patch ] << "\n " + line
146
149
end
147
150
end
148
151
final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
149
152
end
150
-
153
+
151
154
end
152
155
end
0 commit comments