Class: RailVerdict::Git::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rail_verdict/git/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_root:, head:, base:, merge_base:, changed_files:, changed_line_set:, binary_paths:, conflicted_paths:) ⇒ Context

Returns a new instance of Context.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rail_verdict/git/context.rb', line 30

def initialize(repository_root:, head:, base:, merge_base:, changed_files:, changed_line_set:, binary_paths:, conflicted_paths:)
  @repository_root = repository_root.dup.freeze
  @head = head.dup.freeze
  @base = base.dup.freeze
  @merge_base = merge_base.dup.freeze
  @changed_files = changed_files.map(&:freeze).freeze
  @changed_line_set = deep_freeze_line_set(changed_line_set)
  @binary_paths = binary_paths.dup.freeze
  @conflicted_paths = conflicted_paths.dup.freeze
  freeze
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def base
  @base
end

#binary_pathsObject (readonly)

Returns the value of attribute binary_paths.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def binary_paths
  @binary_paths
end

#changed_filesObject (readonly)

Returns the value of attribute changed_files.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def changed_files
  @changed_files
end

#changed_line_setObject (readonly)

Returns the value of attribute changed_line_set.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def changed_line_set
  @changed_line_set
end

#conflicted_pathsObject (readonly)

Returns the value of attribute conflicted_paths.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def conflicted_paths
  @conflicted_paths
end

#headObject (readonly)

Returns the value of attribute head.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def head
  @head
end

#merge_baseObject (readonly)

Returns the value of attribute merge_base.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def merge_base
  @merge_base
end

#repository_rootObject (readonly)

Returns the value of attribute repository_root.



28
29
30
# File 'lib/rail_verdict/git/context.rb', line 28

def repository_root
  @repository_root
end

Class Method Details

.build(repository_root:, base_override:, configuration:, runner: ProcessRunner) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rail_verdict/git/context.rb', line 65

def self.build(repository_root:, base_override:, configuration:, runner: ProcessRunner)
  root = File.realpath(repository_root)
  raise Git::NotARepository, "repository root does not exist: #{repository_root}" unless File.directory?(root)

  git_root = resolve_git_root(root, runner: runner)
  unless Pathname.new(git_root) == Pathname.new(root) || root.start_with?(git_root + File::SEPARATOR) || git_root == root
    git_root = root if File.directory?(File.join(root, ".git"))
  end
  unless Dir.exist?(File.join(git_root, ".git")) || File.file?(File.join(git_root, ".git"))
    toplevel = git_toplevel(root, runner: runner)
    if toplevel && File.directory?(toplevel)
      git_root = toplevel
    end
  end
  repository_root = git_root

  head = resolve_head(repository_root, runner: runner)
  base_raw = resolve_base_raw(base_override, configuration)
  if base_raw.nil? || base_raw.strip.empty?
    raise Git::BaseUnresolvable, "changed scope requires an explicit base: supply --base <revision> or set git.base in .railverdict.yml"
  end
  base = resolve_base_commit(base_raw.strip, repository_root, runner: runner)
  merge_base = resolve_merge_base(base, head, repository_root, runner: runner)
  changed_files, binary_set = resolve_changed_files(merge_base, head, repository_root, runner: runner)
  changed_line_set = resolve_changed_line_set(merge_base, head, changed_files, repository_root, runner: runner)
  conflicted = resolve_conflicts(repository_root, runner: runner)
  binary_paths = (binary_set.to_a + changed_line_set.select { |_, v| v.nil? }.keys).uniq.sort.freeze
  filtered_line_set = changed_line_set.reject { |_, v| v.nil? }

  new(
    repository_root: repository_root,
    head: head,
    base: base,
    merge_base: merge_base,
    changed_files: changed_files,
    changed_line_set: filtered_line_set,
    binary_paths: binary_paths,
    conflicted_paths: conflicted
  )
end

Instance Method Details

#rename_mapObject



55
56
57
58
59
60
61
62
63
# File 'lib/rail_verdict/git/context.rb', line 55

def rename_map
  map = {}
  changed_files.each do |file|
    next unless file.status == :renamed

    map[file.new_path] = file.old_path if file.new_path && file.old_path
  end
  map.freeze
end

#to_hObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rail_verdict/git/context.rb', line 42

def to_h
  {
    "repository_root" => repository_root,
    "head" => head,
    "base" => base,
    "merge_base" => merge_base,
    "changed_files" => changed_files.map(&:to_h),
    "changed_line_set" => changed_line_set,
    "binary_paths" => binary_paths.sort,
    "conflicted_paths" => conflicted_paths.sort
  }
end