Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Last active January 18, 2023 13:56
Show Gist options
  • Select an option

  • Save tomas-stefano/fa46e26b5778ec1f7030 to your computer and use it in GitHub Desktop.

Select an option

Save tomas-stefano/fa46e26b5778ec1f7030 to your computer and use it in GitHub Desktop.

Revisions

  1. tomas-stefano revised this gist Jun 27, 2017. No changes.
  2. tomas-stefano revised this gist Jun 27, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ class Check
    def run
    if files.empty?
    puts "Diff does not contain relevant files. \033[0;33m [SKIPPED]\e[0m"
    true
    else
    run_command
    end
  3. tomas-stefano renamed this gist Jun 16, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Readme → Readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ## Pre-commit hook

    This is a pre-commit script that will see the git diff and run the appropriate checks like JSHint and Rubocop

    You can add to your `.git/hooks` of any repo that you like.
  4. tomas-stefano revised this gist Jun 16, 2017. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions Readme
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,12 @@ This is a pre-commit script that will see the git diff and run the appropriate c

    You can add to your `.git/hooks` of any repo that you like.

    Don't forget to add the executable permission to this file and have the following installed:
    ## Notes

    1) Don't forget to add the executable permission to this file and have the following installed:

    * Git
    * Rubocop
    * JShint
    * JShint

    2) This script will only run on "Changes to be committed" because it uses the --cached option for the git diff.
  5. tomas-stefano revised this gist Jun 16, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Readme
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    This is a pre-commit script
    This is a pre-commit script that will see the git diff and run the appropriate checks like JSHint and Rubocop

    You can add to your `.git/hooks` of any repo that you like.

  6. tomas-stefano revised this gist Jun 16, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions Readme
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    This is a pre-commit script

    You can add to your `.git/hooks` of any repo that you like.

    Don't forget to add the executable permission to this file and have the following installed:

    * Git
    * Rubocop
    * JShint
  7. tomas-stefano created this gist Jan 13, 2015.
    124 changes: 124 additions & 0 deletions pre-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,124 @@
    #!/usr/bin/env ruby

    class GitDiff
    attr_reader :filenames

    def initialize
    @filenames = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
    end
    end

    class Check
    attr_reader :diff, :exit_status, :files

    def initialize(diff)
    @diff = diff
    end

    def check
    before_run
    puts "Running \033[1;34m#{self.class}#check\e[0m: #{command}"
    report_error unless run
    after_run
    end

    def before_run
    end

    def after_run
    end

    def files
    @files ||= Array(diff.filenames)
    end

    def run
    if files.empty?
    puts "Diff does not contain relevant files. \033[0;33m [SKIPPED]\e[0m"
    else
    run_command
    end
    end

    def run_command
    system(command)
    end

    def command
    raise NotImplementedError
    end

    def report_error
    raise NotImplementedError
    end

    private

    def select_files(extension:)
    @files = diff.filenames.select { |filename| File.extname(filename) == ".#{extension}" }
    end
    end

    class Keywords < Check
    BAD = %w(
    binding.pry
    throw
    console.log
    debugger
    )

    def run_command
    result = `#{command}`
    puts result

    report_error unless result.empty?

    result.empty?
    end

    def command
    %(git diff --cached -G"#{BAD.join('|')}" #{files.join(' ')})
    end

    def report_error
    puts "There are files that contain this keywords: #{BAD.join(', ')}.\n"
    exit 1
    end
    end

    class Rubocop < Check
    def before_run
    @files = select_files(extension: :rb)
    end

    def command
    "rubocop #{files.join(' ')}"
    end

    def report_error
    puts "Rubocop reported some offenses. Aborting commit"
    exit 1
    end
    end

    class JSHint < Check
    def before_run
    @files = select_files(extension: :js)
    end

    def command
    "jshint #{files.join(' ')}"
    end

    def report_error
    end
    end

    diff = GitDiff.new
    [
    Keywords,
    Rubocop,
    JSHint
    ].each do |klass|
    klass.new(diff).check
    end