Module: Git::Rails::Changes

Defined in:
lib/git/rails/changes.rb,
lib/git/rails/changes/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.mainObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
# File 'lib/git/rails/changes.rb', line 12

def main
  opts = Optimist.options do
    opt "all-files", "Don't exclude any files"
    opt "include-deleted", "Don't filter out deleted files"
    opt "code-only", "Filter out .yml, .json, and .md files"
    opt "spec-only", "Convert all paths to spec and remove non-spec files"
    opt "cached", "Only compare staged files (useful for pre-commit hooks)"
    opt "unstaged-only", "Only compare unstaged/uncommitted files"
    opt "branch",
      "Compare all changes with given branch (default main)",
      type: :string,
      default: "main"
    opt "replace-blank",
      "Prevent blank output by sending alternate output",
      type: :string,
      default: "--version"
    opt "rspec-pre-commit", "Preset option for getting cached specs safely"
    opt "run-rspec", "Run rspec on the files found by the given options"
    opt "run-rspec-fail-fast", "Run rspec with the --fail-fast option"
    opt "run-rubocop", "Run rubocop on the files found by the given options"
    opt "run-rubocop-autocorrect", "Run rubocop with the --safe-auto-correct flag"
    opt "debug", "Print all command line options"
  end

  if opts["rspec-pre-commit"]
    opts["cached"] = true
    opts["spec-only"] = true
    opts["replace-blank"] = "--version"
  end

  puts opts if opts["debug"]

  raise "Can't use both 'cached' (-c) and 'unstaged-only' (-u) flags" if opts["cached"] && opts["unstaged-only"]

  # gather all changes
  file_list = if opts["unstaged-only"]
    `git ls-files --modified --others #{"--exclude-standard" unless opts["all-files"]}`
  elsif opts["cached"]
    `git diff --name-status --cached #{"| grep -v '\''^D'\''" unless opts["include-deleted"]} | awk '{print $2}'`
  else
    `git diff --name-status #{opts["branch"]} #{"| grep -v '\''^D'\''" unless opts["include-deleted"]} | awk '{print $2}'`
  end
  file_list = file_list.split("\n")

  if opts["spec-only"]
    spec_array = file_list.map { |file| spec?(file) ? file : spec_from_code(file) }
    spec_array = spec_array.select { |file| file.match?(/_spec.rb$/) }
    spec_array = spec_array.map { |spec| File.exist?(File.expand_path(spec, `pwd`.strip)) ? spec : nil }
    file_list = spec_array.compact.uniq
  end

  file_list = file_list.reject { |file| /\.(yml|json|md|enc)\Z/.match(file) } if opts["code-only"]

  command = []
  if opts["run-rspec"] || opts["run-rspec-fail-fast"]
    command << `which spring`.strip
    command << "rspec"
    command << "--fail-fast" if opts["run-rspec-fail-fast"]
  elsif opts["run-rubocop"] || opts["run-rubocop-autocorrect"]
    command << `which rubocop`.strip
    command << "--safe-auto-correct" if opts["run-rubocop-autocorrect"]
  else
    command << `which echo`.strip
    file_list = [file_list.join("\n")]
  end

  if file_list.empty? && opts["replace-blank"]
    command << opts["replace-blank"]
  else
    command.concat(file_list)
  end

  command.compact!

  exec(*command)
end