Class: RighteousGitHooks::LinkedFilesChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/hooks/linked-files-not-in-repo.rb

Instance Method Summary collapse

Constructor Details

#initialize(git_root, project_dir, csproj_filename) ⇒ LinkedFilesChecker



8
9
10
11
12
# File 'lib/hooks/linked-files-not-in-repo.rb', line 8

def initialize(git_root, project_dir, csproj_filename)
  @git_root = git_root
  @project_dir = project_dir
  @csproj_filename = csproj_filename
end

Instance Method Details

#adjudge!Object



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
# File 'lib/hooks/linked-files-not-in-repo.rb', line 14

def adjudge!()
  
  puts 'Checking linked files in your csproj are not in the repository...'
  csproj_path = File.join(@git_root, @project_dir, @csproj_filename)

  return Result.error("Cannot find #{csproj_path}") unless File.exists? csproj_path

  puts "Checking csproj: '#{csproj_path}'"
  csproj = Nokogiri::XML(File.read(csproj_path))
  sins = []
  
  Dir.chdir(@git_root) do
    # CSS Selectors for maximum coolness (also XPath sucks)

    csproj.css('ItemGroup > Content > Link').each do |link|
      # Get the repo-relative path for each file with unix-style path separators

      path = File.join(@project_dir, link.text.gsub('\\', '/'))
      # Check the file is not staged in Git. An empty response means it's not staged

      puts link.text
      result = `git ls-files --stage #{path}`
      sins.push(result) unless result.empty?
    end
  end

  return Result.success("Congratulations, this is a righteous commit!") if sins.empty?

  message = "\nYou have sinned! The following files are csproj links, but exist in your repo..."
  sins.each do |sin|
    message = message + "\n#{sin}"
  end
  message = message + "\nPlease delete them before committing. You may need to add them to .gitignore as well."
  
  return Result.error(message)
end