Class: Greenroom::CLI::Check::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/greenroom/cli/check.rb

Overview

This check gates an automatic merge. The RuboCop check sees one file version, so it cannot compare the new method body with the old body.

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Repository

Returns a new instance of Repository.



57
58
59
# File 'lib/greenroom/cli/check.rb', line 57

def initialize(directory)
  @directory = directory
end

Instance Method Details

#check(commit) ⇒ Object



61
62
63
64
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
# File 'lib/greenroom/cli/check.rb', line 61

def check(commit)
  resolved = git("rev-parse", "--verify", "#{commit}^{commit}").strip
  parents = git("rev-list", "--parents", "-n", "1", resolved).split.drop(1)
  # A merge has more than one source state. A comparison with its first
  # parent would give a different meaning to the acceptance result.
  return reject("The commit is a merge commit.") if parents.length > 1
  return reject("The commit has no parent.") if parents.empty?

  changes = git("diff-tree", "--no-commit-id", "--name-status", "-r", "-M20%", parents.first, resolved).lines
  # A rejected shape goes to human review. Thus, an uncertain result
  # must reject instead of accepting a change for an automatic merge.
  return reject("The commit must modify one Ruby file.") unless changes.one?

  fields = changes.first.chomp.split("\t")
  # Git can report a rename as R or as an addition and a deletion.
  # Requiring one M entry rejects both forms and their identity change.
  return reject("The changed file must have status M.") unless fields.length == 2 && fields.first == "M"

  path = fields.last
  return reject("The changed file must have the .rb extension.") unless path.end_with?(".rb")

  # Complete blobs support a syntax tree comparison across both file
  # versions. Parsing unified diff lines would mix syntax with context.
  before = git("show", "#{parents.first}:#{path}")
  after = git("show", "#{resolved}:#{path}")
  Analyzer.new(before, after).check
rescue GitError, Analyzer::SourceError => error
  reject(error.message)
end