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
118 119 120 121 |
# File 'lib/git/diff.rb', line 118 def [](key) process_full @full_diff_files.assoc(key)[1] end |
#deletions ⇒ Integer
Returns the total number of deleted lines in the diff
193 194 195 |
# File 'lib/git/diff.rb', line 193 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
144 145 146 147 |
# File 'lib/git/diff.rb', line 144 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
204 205 206 |
# File 'lib/git/diff.rb', line 204 def insertions stats_provider.insertions end |
#lines ⇒ Integer
Returns the total number of changed lines in the diff
182 183 184 |
# File 'lib/git/diff.rb', line 182 def lines stats_provider.lines end |
#name_status ⇒ Hash<String, String>
Returns the path-to-status hash for all changed files in the diff
171 172 173 |
# File 'lib/git/diff.rb', line 171 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 |
# File 'lib/git/diff.rb', line 104 def patch @base.diff_full(@from, @to, path_limiter: @path) 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
156 157 158 |
# File 'lib/git/diff.rb', line 156 def size stats_provider.total[:files] end |
#stats ⇒ Hash
Returns a statistics hash for the diff
219 220 221 222 223 224 |
# File 'lib/git/diff.rb', line 219 def stats { files: stats_provider.files, total: stats_provider.total } end |