Class: Git::Diff
Overview
Diff between two commits or between a commit and the working tree
Defined Under Namespace
Classes: DiffFile, FullDiffParser
Instance Attribute Summary collapse
-
#from ⇒ String?
readonly
The starting commit ref.
-
#to ⇒ String?
readonly
The ending commit ref.
Instance Method Summary collapse
-
#[](key) ⇒ Git::Diff::DiffFile
Returns the diff file info for the given path.
-
#deletions ⇒ Integer
Returns the total number of deleted lines in the diff.
-
#each
Iterates over each changed file in the diff.
-
#initialize(base, from = nil, to = nil)
constructor
Creates a new Diff.
-
#insertions ⇒ Integer
Returns the total number of inserted lines in the diff.
-
#lines ⇒ Integer
Returns the total number of changed lines in the diff.
-
#name_status ⇒ Hash<String, String>
Returns the path-to-status hash for all changed files in the diff.
-
#patch ⇒ String
(also: #to_s)
Returns the full diff output as a string.
-
#path(*paths) ⇒ self
Limits the diff to the specified path(s).
-
#size ⇒ Integer
Returns the number of changed files in the diff.
-
#stats ⇒ Hash
Returns a statistics hash for the diff.
Constructor Details
#initialize(base, from = nil, to = nil)
Creates a new Diff
38 39 40 41 42 43 44 45 |
# File 'lib/git/diff.rb', line 38 def initialize(base, from = nil, to = nil) @base = base @from = from&.to_s @to = to&.to_s @path = nil @full_diff_files = nil end |
Instance Attribute Details
#from ⇒ String? (readonly)
The starting commit ref
51 52 53 |
# File 'lib/git/diff.rb', line 51 def from @from end |
#to ⇒ String? (readonly)
The ending commit ref
57 58 59 |
# File 'lib/git/diff.rb', line 57 def to @to end |
Instance Method Details
#[](key) ⇒ Git::Diff::DiffFile
Returns the diff file info for the given path
122 123 124 125 |
# File 'lib/git/diff.rb', line 122 def [](key) process_full @full_diff_files.assoc(key)[1] end |
#deletions ⇒ Integer
Returns the total number of deleted lines in the diff
197 198 199 |
# File 'lib/git/diff.rb', line 197 def deletions stats_provider.deletions end |
#each ⇒ Enumerator<Git::Diff::DiffFile> #each {|file| ... } ⇒ Array<Git::Diff::DiffFile>
Iterates over each changed file in the diff
148 149 150 151 |
# File 'lib/git/diff.rb', line 148 def each(&) process_full @full_diff_files.map { |file| file[1] }.each(&) end |
#insertions ⇒ Integer
Returns the total number of inserted lines in the diff
208 209 210 |
# File 'lib/git/diff.rb', line 208 def insertions stats_provider.insertions end |
#lines ⇒ Integer
Returns the total number of changed lines in the diff
186 187 188 |
# File 'lib/git/diff.rb', line 186 def lines stats_provider.lines end |
#name_status ⇒ Hash<String, String>
Returns the path-to-status hash for all changed files in the diff
175 176 177 |
# File 'lib/git/diff.rb', line 175 def name_status path_status_provider.to_h end |
#patch ⇒ String Also known as: to_s
Returns the full diff output as a string
104 105 106 107 108 109 110 |
# File 'lib/git/diff.rb', line 104 def patch if @base.respond_to?(:diff_full) @base.diff_full(@from, @to, path_limiter: @path) else @base.lib.diff_full(@from, @to, { path_limiter: @path }) end end |
#path(*paths) ⇒ self
Limits the diff to the specified path(s)
When called with no arguments (or only nil arguments), removes any existing path filter, showing all files in the diff. Internally stores a single path as a String and multiple paths as an Array for efficiency.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/git/diff.rb', line 81 def path(*paths) validate_paths_not_arrays(paths) cleaned_paths = paths.compact @path = if cleaned_paths.empty? nil elsif cleaned_paths.length == 1 cleaned_paths.first else cleaned_paths end self end |
#size ⇒ Integer
Returns the number of changed files in the diff
160 161 162 |
# File 'lib/git/diff.rb', line 160 def size stats_provider.total[:files] end |
#stats ⇒ Hash
Returns a statistics hash for the diff
223 224 225 226 227 228 |
# File 'lib/git/diff.rb', line 223 def stats { files: stats_provider.files, total: stats_provider.total } end |