Skip to content

Instantly share code, notes, and snippets.

@queuebit
Forked from mileszs/pre-commit
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save queuebit/eb84b5f9bfdbdf14ec6b to your computer and use it in GitHub Desktop.

Select an option

Save queuebit/eb84b5f9bfdbdf14ec6b to your computer and use it in GitHub Desktop.

Revisions

  1. queuebit revised this gist Jul 14, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@ hits = []

    checks = {
    #'_spec\.rb$' => ['focus:[:space:]*true'],
    '\.rb$' => ['binding\.pry', 'debugger']
    '\.(rb|erb|haml)$' => ['binding\.pry', 'debugger'],
    '\.(js|coffee)$' => ['debugger']
    }

    # Find the names of all the filenames that have been (A)dded (C)opied or (M)odified
  2. @mileszs mileszs created this gist Jun 25, 2013.
    34 changes: 34 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env ruby
    hits = []

    checks = {
    #'_spec\.rb$' => ['focus:[:space:]*true'],
    '\.rb$' => ['binding\.pry', 'debugger']
    }

    # Find the names of all the filenames that have been (A)dded (C)opied or (M)odified
    filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")

    filenames.each do |filename|
    # Perform special checks for _spec filenames (rspec tests)
    checks.each do |filename_pattern, patterns|
    if filename.match filename_pattern
    patterns.each do |contents_pattern|
    results = `git diff --cached #{filename} | grep "^\+[^+]" | grep "#{contents_pattern}"`.split("\n").map { |r| r.sub(/^\+[\s\t]*/, '') }
    if $? == 0
    # Add the relevant change with line number to the hits array
    results.each{ |r|
    hits.push "#{filename}:" + `grep -n '#{r}' #{filename}`.sub(/:\s+/, ' ').chomp
    }
    end
    end
    end
    end
    end

    if hits.any?
    puts "\e[33m>>> Please remove the following problems from these files before committing\e[0m"
    puts hits.join("\n")
    end

    exit 1 if hits.any?