Skip to content

Commit 72d7a3d

Browse files
Adding Git::Diff.name_status support
1 parent 14738bb commit 72d7a3d

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/git/diff.rb

+14-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ def initialize(base, from = nil, to = nil)
1515
@stats = nil
1616
end
1717
attr_reader :from, :to
18-
18+
19+
def name_status
20+
cache_name_status
21+
end
22+
1923
def path(path)
2024
@path = path
2125
return self
@@ -96,22 +100,21 @@ def blob(type = :dst)
96100
private
97101

98102
def cache_full
99-
unless @full_diff
100-
@full_diff = @base.lib.diff_full(@from, @to, {:path_limiter => @path})
101-
end
103+
@full_diff ||= @base.lib.diff_full(@from, @to, {:path_limiter => @path})
102104
end
103105

104106
def process_full
105-
unless @full_diff_files
106-
cache_full
107-
@full_diff_files = process_full_diff
108-
end
107+
return if @full_diff_files
108+
cache_full
109+
@full_diff_files = process_full_diff
109110
end
110111

111112
def cache_stats
112-
unless @stats
113-
@stats = @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114-
end
113+
@stats ||= @base.lib.diff_stats(@from, @to, {:path_limiter => @path})
114+
end
115+
116+
def cache_name_status
117+
@name_status ||= @base.lib.diff_name_status(@from, @to, {:path => @path})
115118
end
116119

117120
# break up @diff_full

lib/git/lib.rb

+14
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
371371
hsh
372372
end
373373

374+
def diff_name_status(reference1 = nil, reference2 = nil, opts = {})
375+
opts_arr = ['--name-status']
376+
opts_arr << reference1 if reference1
377+
opts_arr << reference2 if reference2
378+
379+
opts_arr << '--' << opts[:path] if opts[:path]
380+
381+
command_lines('diff', opts_arr).inject({}) do |memo, line|
382+
status, path = line.split("\t")
383+
memo[path] = status
384+
memo
385+
end
386+
end
387+
374388
# compares the index and the working directory
375389
def diff_files
376390
diff_as_hash('diff-files')

0 commit comments

Comments
 (0)