Skip to content

Instantly share code, notes, and snippets.

@parndt
Forked from ideasasylum/pre-commit
Last active December 16, 2015 11:59

Revisions

  1. parndt revised this gist May 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pre-commit
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,4 @@ if spec_hits.any?
    puts spec_hits.join("\n")
    end

    exit 1 if spec_hits.any?
    exit 1 if spec_hits.any?
  2. parndt revised this gist Apr 21, 2013. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -14,20 +14,21 @@ filenames.each do |filename|
    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]*/, '') }
    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 spec_hits array
    results.each{ |r|
    spec_hits.push "#{filename}:" + `grep -n '#{r}' #{filename}`.sub(/:\s+/, ' ').chomp
    }
    results.each do |result|
    line = `grep -n '#{result}' #{filename}`.sub(/:\s+/, ' ').chomp
    spec_hits.push "#{filename}:" + line
    end
    end
    end
    end
    end
    end

    if spec_hits.any?
    puts "\e[33m>>> Please remove the following problems from these files before committing\e[0m"
    puts "\e[33m>>> Oops! You forgot something:\e[0m"
    puts spec_hits.join("\n")
    end

  3. @ideasasylum ideasasylum created this gist Mar 7, 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
    spec_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 spec_hits array
    results.each{ |r|
    spec_hits.push "#{filename}:" + `grep -n '#{r}' #{filename}`.sub(/:\s+/, ' ').chomp
    }
    end
    end
    end
    end
    end

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

    exit 1 if spec_hits.any?