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
9
@from = from . to_s
@@ -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,36 +92,36 @@ 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
final = { }
120
120
current_file = nil
121
121
@full_diff . split ( "\n " ) . each do |line |
122
122
if m = /diff --git a\/ (.*?) b\/ (.*?)/ . match ( line )
123
123
current_file = m [ 1 ]
124
- final [ current_file ] = { :patch => line , :path => current_file ,
124
+ final [ current_file ] = { :patch => line , :path => current_file ,
125
125
:mode => '' , :src => '' , :dst => '' , :type => 'modified' }
126
126
else
127
127
if m = /index (.......)\. \. (.......)( ......)*/ . match ( line )
@@ -136,11 +136,11 @@ def process_full_diff
136
136
if m = /^Binary files / . match ( line )
137
137
final [ current_file ] [ :binary ] = true
138
138
end
139
- final [ current_file ] [ :patch ] << "\n " + line
139
+ final [ current_file ] [ :patch ] << "\n " + line
140
140
end
141
141
end
142
142
final . map { |e | [ e [ 0 ] , DiffFile . new ( @base , e [ 1 ] ) ] }
143
143
end
144
-
144
+
145
145
end
146
146
end
0 commit comments