Skip to content

Commit bddcc9f

Browse files
Merge branch 'master' into diff_name_status
Conflicts: lib/git/diff.rb
2 parents 72d7a3d + 69bae05 commit bddcc9f

File tree

3 files changed

+184
-172
lines changed

3 files changed

+184
-172
lines changed

Diff for: README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ http://github.com/schacon/ruby-git
1313
You can install Ruby/Git like this:
1414

1515
$ sudo gem install git
16-
16+
1717
## Code Status
1818

1919
* [![Build Status](https://api.travis-ci.org/schacon/ruby-git.png)](https://travis-ci.org/schacon/ruby-git)
@@ -46,7 +46,7 @@ like:
4646

4747
Here are a bunch of examples of how to use the Ruby/Git package.
4848

49-
Ruby < 1.9 will require rubygems to be loaded.
49+
Ruby < 1.9 will require rubygems to be loaded.
5050

5151
```ruby
5252
require 'rubygems'
@@ -127,10 +127,10 @@ Here are the operations that need read permission only.
127127
g.grep('hello') # implies HEAD
128128
g.blob('v2.5:Makefile').grep('hello')
129129
g.tag('v2.5').grep('hello', 'docs/')
130-
g.describe()
130+
g.describe()
131131
g.describe('0djf2aa')
132132
g.describe('HEAD', {:all => true, :tags => true})
133-
133+
134134
g.diff(commit1, commit2).size
135135
g.diff(commit1, commit2).stats
136136
g.gtree('v2.5').diff('v2.6').insertions
@@ -149,7 +149,7 @@ Here are the operations that need read permission only.
149149
g.config # returns whole config hash
150150

151151
g.tags # returns array of Git::Tag objects
152-
152+
153153
g.show()
154154
g.show('HEAD')
155155
g.show('v2.8', 'README.md')
@@ -178,9 +178,11 @@ And here are the operations that will need to write to your git repository.
178178
g.add('file_path') # git add -- "file_path"
179179
g.add(['file_path_1', 'file_path_2']) # git add -- "file_path_1" "file_path_2"
180180

181-
182-
g.remove('file.txt')
183-
g.remove(['file.txt', 'file2.txt'])
181+
g.remove() # git rm -f -- "."
182+
g.remove('file.txt') # git rm -f -- "file.txt"
183+
g.remove(['file.txt', 'file2.txt']) # git rm -f -- "file.txt" "file2.txt"
184+
g.remove('file.txt', :recursive => true) # git rm -f -r -- "file.txt"
185+
g.remove('file.txt', :cached => true) # git rm -f --cached -- "file.txt"
184186

185187
g.commit('message')
186188
g.commit_all('message')

Diff for: lib/git/diff.rb

+28-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module 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,7 +15,7 @@ def initialize(base, from = nil, to = nil)
1515
@stats = nil
1616
end
1717
attr_reader :from, :to
18-
18+
1919
def name_status
2020
cache_name_status
2121
end
@@ -24,55 +24,55 @@ def path(path)
2424
@path = path
2525
return self
2626
end
27-
27+
2828
def size
2929
cache_stats
3030
@stats[:total][:files]
3131
end
32-
32+
3333
def lines
3434
cache_stats
3535
@stats[:total][:lines]
3636
end
37-
37+
3838
def deletions
3939
cache_stats
4040
@stats[:total][:deletions]
4141
end
42-
42+
4343
def insertions
4444
cache_stats
4545
@stats[:total][:insertions]
4646
end
47-
47+
4848
def stats
4949
cache_stats
5050
@stats
5151
end
52-
52+
5353
# if file is provided and is writable, it will write the patch into the file
5454
def patch(file = nil)
5555
cache_full
5656
@full_diff
5757
end
5858
alias_method :to_s, :patch
59-
59+
6060
# enumerable methods
61-
61+
6262
def [](key)
6363
process_full
6464
@full_diff_files.assoc(key)[1]
6565
end
66-
66+
6767
def each(&block) # :yields: each Git::DiffFile in turn
6868
process_full
6969
@full_diff_files.map { |file| file[1] }.each(&block)
7070
end
71-
71+
7272
class DiffFile
7373
attr_accessor :patch, :path, :mode, :src, :dst, :type
7474
@base = nil
75-
75+
7676
def initialize(base, hash)
7777
@base = base
7878
@patch = hash[:patch]
@@ -87,7 +87,7 @@ def initialize(base, hash)
8787
def binary?
8888
!!@binary
8989
end
90-
90+
9191
def blob(type = :dst)
9292
if type == :src
9393
@base.object(@src) if @src != '0000000'
@@ -96,27 +96,27 @@ def blob(type = :dst)
9696
end
9797
end
9898
end
99-
99+
100100
private
101-
101+
102102
def cache_full
103103
@full_diff ||= @base.lib.diff_full(@from, @to, {:path_limiter => @path})
104104
end
105-
105+
106106
def process_full
107107
return if @full_diff_files
108108
cache_full
109109
@full_diff_files = process_full_diff
110110
end
111-
111+
112112
def cache_stats
113113
@stats ||= @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114114
end
115115

116116
def cache_name_status
117117
@name_status ||= @base.lib.diff_name_status(@from, @to, {:path => @path})
118118
end
119-
119+
120120
# break up @diff_full
121121
def process_full_diff
122122
defaults = {
@@ -127,7 +127,11 @@ def process_full_diff
127127
}
128128
final = {}
129129
current_file = nil
130-
@full_diff.split("\n").each do |line|
130+
full_diff_utf8_encoded = @full_diff.encode("UTF-8", "binary", {
131+
:invalid => :replace,
132+
:undef => :replace
133+
})
134+
full_diff_utf8_encoded.split("\n").each do |line|
131135
if m = /^diff --git a\/(.*?) b\/(.*?)/.match(line)
132136
current_file = m[1]
133137
final[current_file] = defaults.merge({:patch => line, :path => current_file})
@@ -144,11 +148,11 @@ def process_full_diff
144148
if m = /^Binary files /.match(line)
145149
final[current_file][:binary] = true
146150
end
147-
final[current_file][:patch] << "\n" + line
151+
final[current_file][:patch] << "\n" + line
148152
end
149153
end
150154
final.map { |e| [e[0], DiffFile.new(@base, e[1])] }
151155
end
152-
156+
153157
end
154158
end

0 commit comments

Comments
 (0)