Class: Git::Diff

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git/diff.rb

Overview

object that holds the diff between two commits

Defined Under Namespace

Classes: DiffFile, FullDiffParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, from = nil, to = nil) ⇒ Diff

Returns a new instance of Diff.



11
12
13
14
15
16
17
18
# File 'lib/git/diff.rb', line 11

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 (readonly)

Returns the value of attribute from.



19
20
21
# File 'lib/git/diff.rb', line 19

def from
  @from
end

#to (readonly)

Returns the value of attribute to.



19
20
21
# File 'lib/git/diff.rb', line 19

def to
  @to
end

Instance Method Details

#[](key)



61
62
63
64
# File 'lib/git/diff.rb', line 61

def [](key)
  process_full
  @full_diff_files.assoc(key)[1]
end

#deletions



87
88
89
# File 'lib/git/diff.rb', line 87

def deletions
  stats_provider.deletions
end

#each



66
67
68
69
# File 'lib/git/diff.rb', line 66

def each(&)
  process_full
  @full_diff_files.map { |file| file[1] }.each(&)
end

#insertions



91
92
93
# File 'lib/git/diff.rb', line 91

def insertions
  stats_provider.insertions
end

#lines



83
84
85
# File 'lib/git/diff.rb', line 83

def lines
  stats_provider.lines
end

#name_status

DEPRECATED METHODS



79
80
81
# File 'lib/git/diff.rb', line 79

def name_status
  path_status_provider.to_h
end

#patch Also known as: to_s



56
57
58
# File 'lib/git/diff.rb', line 56

def patch
  @base.lib.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.

Examples:

Limit diff to a single path

git.diff('HEAD~3', 'HEAD').path('lib/')

Limit diff to multiple paths

git.diff('HEAD~3', 'HEAD').path('src/', 'docs/', 'README.md')

Remove path filtering (show all files)

diff.path  # or diff.path(nil)

Parameters:

  • paths (String, Pathname)

    one or more paths to filter the diff. Pass no arguments to remove filtering.

Returns:

  • (self)

    returns self for method chaining

Raises:

  • (ArgumentError)

    if any path is an Array (use splatted arguments instead)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/git/diff.rb', line 40

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



71
72
73
# File 'lib/git/diff.rb', line 71

def size
  stats_provider.total[:files]
end

#stats



95
96
97
98
99
100
# File 'lib/git/diff.rb', line 95

def stats
  {
    files: stats_provider.files,
    total: stats_provider.total
  }
end