Module: SimpleCov::CLI::Affected::ChangedFiles

Extended by:
ChangedFiles
Included in:
ChangedFiles
Defined in:
lib/simplecov/cli/affected/changed_files.rb

Overview

The change as git sees it, as {root:, changed:}: the files that differ between the merge base of the given ref and the working tree, so commits that landed on the base after the branch point stay out while uncommitted work stays in, unioned with untracked files, since a brand-new spec exists in no diff. Everything runs against the repository toplevel rather than the cwd, so a subdirectory run selects over the whole change. NUL separation keeps exotic filenames literal instead of quoted.

Instance Method Summary collapse

Instance Method Details

#call(base, stderr) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/simplecov/cli/affected/changed_files.rb', line 18

def call(base, stderr)
  return report(stderr, "invalid base ref #{base.inspect}") if Git.option_like_ref?(base)

  root = repository_toplevel(stderr)
  return nil unless root

  diff = list("diff", ["-C", root, "diff", "--name-only", "-z", "--merge-base", base, "--"], stderr)
  return nil unless diff

  untracked = list("ls-files", ["-C", root, "ls-files", "--others", "--exclude-standard", "-z"], stderr)
  untracked && {root: root, changed: (diff + untracked).uniq}
end

#list(command, argv, stderr) ⇒ Object

git's own first stderr line is relayed on failure, so a bad ref or an old git names itself.



43
44
45
46
47
48
49
# File 'lib/simplecov/cli/affected/changed_files.rb', line 43

def list(command, argv, stderr)
  stdout, detail, success = Git.capture(*argv)
  return (_ = stdout).split("\0") if success
  return report(stderr, "cannot run git (#{detail})") if stdout.nil?

  report(stderr, "`git #{command}` failed: #{detail}")
end

#report(stderr, message) ⇒ Object



51
52
53
# File 'lib/simplecov/cli/affected/changed_files.rb', line 51

def report(stderr, message)
  stderr.puts("simplecov affected: #{message}")
end

#repository_toplevel(stderr) ⇒ Object

nil stdout from Git.capture means git never ran; a run that failed means the cwd is outside a working tree.



33
34
35
36
37
38
39
# File 'lib/simplecov/cli/affected/changed_files.rb', line 33

def repository_toplevel(stderr)
  stdout, detail, success = Git.capture("rev-parse", "--show-toplevel")
  return (_ = stdout).chomp if success
  return report(stderr, "cannot run git (#{detail})") if stdout.nil?

  report(stderr, "not inside a git working tree")
end