Module: Git::Repository::Diffing
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/diffing.rb
Overview
Facade methods for comparing commits and trees using git diff
Included by Git::Repository.
Instance Method Summary collapse
-
#diff(obj1 = 'HEAD', obj2 = nil) ⇒ Git::Diff
Returns a lazy Diff object for the comparison between two trees.
-
#diff_files ⇒ Hash{String => Hash}
Compares the index and the working directory.
-
#diff_full(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ String
Returns the full unified diff patch text between two trees.
-
#diff_index(treeish) ⇒ Hash{String => Hash}
Compares the working tree against the given tree object.
-
#diff_numstat(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ Hash
Returns per-file insertion/deletion counts and totals between two trees.
-
#diff_path_status(from = 'HEAD', to = nil, opts = {}) ⇒ Git::DiffPathStatus
(also: #diff_name_status)
Returns the file path status between two trees.
-
#diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ Git::DiffStats
Returns the stats between two trees as a DiffStats object.
Instance Method Details
#diff(obj1 = 'HEAD', obj2 = nil) ⇒ Git::Diff
Returns a lazy Diff object for the comparison between two trees
Compares (1) two commits, (2) a commit against the working tree, or (3) the index against the working tree. The returned Diff is lazy — it does not run any git commands until an accessor method (e.g., Diff#patch, Diff#each) is called.
Use Diff#path to limit the diff to a sub-path after construction.
291 292 293 |
# File 'lib/git/repository/diffing.rb', line 291 def diff(obj1 = 'HEAD', obj2 = nil) Git::Diff.new(self, obj1, obj2) end |
#diff_files ⇒ Hash{String => Hash}
The field names in the returned hash are legacy names inherited
from Git::Lib#diff_files and appear counterintuitive: :mode_repo
and :sha_repo hold index (staging area) values, while
:mode_index and :sha_index hold working tree values.
Compares the index and the working directory
Runs git diff-files to list files that differ between the index
(staging area) and the working directory. These are changes that have
been made to tracked files but not yet staged.
423 424 425 426 427 428 |
# File 'lib/git/repository/diffing.rb', line 423 def diff_files Git::Commands::Status.new(@execution_context).call Private.parse_diff_files_output( Git::Commands::DiffFiles.new(@execution_context).call.stdout ) end |
#diff_full(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ String
Returns the full unified diff patch text between two trees
Compares (1) two commits, (2) a commit against the working tree, or (3) the
index against the working tree using git diff -p, and returns the raw
unified diff patch output.
Comparing two commits
When both obj1 and obj2 are provided, the comparison is between those two refs (commits, tags, branches, etc.).
Comparing a commit against the working tree
When only obj1 is provided (and isn't nil), the comparison is between obj1 and the working tree; the patch reflects all changes since obj1.
Comparing the index against the working tree
When obj1 is explicitly nil then obj2 must be omitted or nil. In this case,
the comparison is between the index and the working tree; the patch reflects
unstaged changes.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/git/repository/diffing.rb', line 86 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {}) SharedPrivate.assert_valid_opts!(DIFF_FULL_ALLOWED_OPTS, **opts) raise ArgumentError, 'Invalid arguments: obj1 is nil but obj2 is not' if obj1.nil? && !obj2.nil? pathspecs = Private.normalize_pathspecs(opts[:path_limiter], 'path limiter') result = Git::Commands::Diff.new(@execution_context).call( *[obj1, obj2].compact, patch: true, numstat: true, shortstat: true, src_prefix: 'a/', dst_prefix: 'b/', path: pathspecs ) Private.extract_patch_text(result.stdout) end |
#diff_index(treeish) ⇒ Hash{String => Hash}
git diff-index without --cached uses the index as a stat cache:
any file whose index entry differs from the tree is reported as changed,
even when the on-disk working-tree content is byte-for-byte identical to
the tree. A staged change that has been reverted in the working tree will
therefore still appear in the result (because the index still differs from
the tree).
The field names in the returned hash are legacy names inherited
from Git::Lib#diff_index and appear counterintuitive: :mode_repo
and :sha_repo hold tree (treeish) values, while :mode_index and
:sha_index hold working tree values.
Compares the working tree against the given tree object
Runs git diff-index <treeish> (without --cached) to list files that
differ between the given tree object (e.g. a commit or "HEAD") and the
working tree. The index is refreshed via git status first so that cached
stat information is up to date.
This is equivalent to the 4.x Git::Lib#diff_index behavior, which also
ran git diff-index without --cached.
483 484 485 486 487 488 |
# File 'lib/git/repository/diffing.rb', line 483 def diff_index(treeish) Git::Commands::Status.new(@execution_context).call Private.parse_diff_files_output( Git::Commands::DiffIndex.new(@execution_context).call(treeish).stdout ) end |
#diff_numstat(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ Hash
Returns per-file insertion/deletion counts and totals between two trees
Compares (1) two commits, (2) a commit against the working tree, or (3) the
index against the working tree using git diff --numstat, and returns a
structured hash of per-file insertion and deletion line counts together with
aggregate totals.
Comparing two commits
When both obj1 and obj2 are provided, the comparison is between those two refs (commits, tags, branches, etc.).
Comparing a commit against the working tree
When only obj1 is provided (and isn't nil), the comparison is between obj1 and the working tree; the stats reflect all changes since obj1.
Comparing the index against the working tree
When obj1 is explicitly nil then obj2 must be omitted or nil. In this case,
the comparison is between the index and the working tree; the stats reflect
unstaged changes.
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/git/repository/diffing.rb', line 174 def diff_numstat(obj1 = 'HEAD', obj2 = nil, opts = {}) SharedPrivate.assert_valid_opts!(DIFF_NUMSTAT_ALLOWED_OPTS, **opts) raise ArgumentError, 'Invalid arguments: obj1 is nil but obj2 is not' if obj1.nil? && !obj2.nil? pathspecs = Private.normalize_pathspecs(opts[:path_limiter], 'path limiter') result = Git::Commands::Diff.new(@execution_context).call( *[obj1, obj2].compact, numstat: true, shortstat: true, src_prefix: 'a/', dst_prefix: 'b/', path: pathspecs ) Private.parse_numstat_output(result.stdout) end |
#diff_path_status(from = 'HEAD', to = nil, opts = {}) ⇒ Git::DiffPathStatus Also known as: diff_name_status
Returns the file path status between two trees
Compares (1) two commits, (2) a commit against the working tree, or (3) the
index against the working tree and returns a DiffPathStatus enumerating
each changed file together with its status code (e.g. "M" for modified,
"A" for added, "D" for deleted, "R100" for a rename with 100%
similarity, etc.).
Comparing two commits
When both from and to are provided, the comparison is between those two refs (commits, tags, branches, etc.).
Comparing a commit against the working tree
When only from is provided (and isn't nil), the comparison is between from and the working tree; the status reflects all changes since from.
Comparing the index against the working tree
When from is explicitly nil then to must be omitted or nil. In this case,
the comparison is between the index and the working tree; the status reflects
unstaged changes.
364 365 366 367 368 369 370 371 372 373 |
# File 'lib/git/repository/diffing.rb', line 364 def diff_path_status(from = 'HEAD', to = nil, opts = {}) SharedPrivate.assert_valid_opts!(DIFF_PATH_STATUS_ALLOWED_OPTS, **opts) raise ArgumentError, 'Invalid arguments: `from` is nil but `to` is not' if from.nil? && !to.nil? path_limiter = Private.resolve_path_limiter(opts) pathspecs = Private.normalize_pathspecs(path_limiter, 'path limiter') result = Private.call_diff_command(@execution_context, from, to, pathspecs) Git::DiffPathStatus.new(Private.extract_name_status_from_raw(result.stdout)) end |
#diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {}) ⇒ Git::DiffStats
Returns the stats between two trees as a DiffStats object
Compares (1) two commits, (2) a commit against the working tree, or (3) the index against the working tree and constructs a lazy DiffStats that computes per-file insertion and deletion counts on demand when its accessor methods are called.
Comparing two commits
When both obj1 and obj2 are provided, the comparison is between those two refs (commits, tags, branches, etc.).
Comparing a commit against the working tree
When only obj1 is provided (and isn't nil), the comparison is between obj1 and the working tree; the stats reflect all changes since obj1.
Comparing the index against the working tree
When obj1 is explicitly nil then obj2 must be omitted or nil. In this case,
the comparison is between the index and the working tree; the stats reflect
unstaged changes.
253 254 255 256 257 258 |
# File 'lib/git/repository/diffing.rb', line 253 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {}) SharedPrivate.assert_valid_opts!(DIFF_STATS_ALLOWED_OPTS, **opts) raise ArgumentError, 'Invalid arguments: obj1 is nil but obj2 is not' if obj1.nil? && !obj2.nil? Git::DiffStats.new(self, obj1, obj2, opts[:path_limiter]) end |