Skip to content

Instantly share code, notes, and snippets.

@alexagranov
Created April 19, 2016 02:59
Show Gist options
  • Save alexagranov/5492bbd8bf5d6f5869c232b1127cd4a4 to your computer and use it in GitHub Desktop.
Save alexagranov/5492bbd8bf5d6f5869c232b1127cd4a4 to your computer and use it in GitHub Desktop.
Custom Overcommit hook to replace the hook installed by git-lfs (which Overcommit removes)
# .git_hooks/pre_push/git_lfs.rb
# Be sure to enable this hook in .overcommit.yml like this:
#
# PrePush:
# GitLfs:
# enabled: true
module Overcommit::Hook::PrePush
class GitLfs < Base
# Overcommit expects you to override this method which will be called
# everytime your pre-commit hook is run.
def run
# Create two arrays to hold our error and warning messages.
error_lines = []
warning_lines = []
# Check if git-lfs executable exists
path_to_git_lfs = `command -v git-lfs`.strip
error_lines << "This repository is configured for Git LFS but 'git-lfs' was not found "\
"on your path. If you no longer wish to use Git LFS, disable GitLfs in "\
".overcommit.yml." unless File.exist?(path_to_git_lfs)
# Run the `git lfs pre-push` with supplied remote_name and remote_url accessors
`git lfs pre-push #{remote_name} #{remote_url}`
# Overcommit expects 1 of the 3 as return values, `:fail`, `:warn` or `:pass`.
# If the hook returns `:fail`, the commit will be aborted with our message
# containing the errors.
return :fail, error_lines.join("\n") if error_lines.any?
:pass
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment