diff --git a/README.md b/README.md index e94a5406..69e2e1c8 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,7 @@ but before any objects have been transferred. If a hook fails, the push is aborted. * [Brakeman](lib/overcommit/hook/pre_push/brakeman.rb) +* [GitLfs](lib/overcommit/hook/pre_push/git_lfs.rb) * [Minitest](lib/overcommit/hook/pre_push/minitest.rb) * [ProtectedBranches](lib/overcommit/hook/pre_push/protected_branches.rb) * [Pytest](lib/overcommit/hook/pre_push/pytest.rb) diff --git a/config/default.yml b/config/default.yml index 0ef15b95..1c5086ec 100644 --- a/config/default.yml +++ b/config/default.yml @@ -945,6 +945,10 @@ PrePush: flags: ['--exit-on-warn', '--quiet', '--summary'] install_command: 'gem install brakeman' + GitLfs: + enabled: false + description: 'Upload files tracked by Git LFS' + # Hooks that run during `git rebase`, before any commits are rebased. # If a hook fails, the rebase is aborted. PreRebase: diff --git a/lib/overcommit/hook/pre_push/git_lfs.rb b/lib/overcommit/hook/pre_push/git_lfs.rb new file mode 100644 index 00000000..f29c3437 --- /dev/null +++ b/lib/overcommit/hook/pre_push/git_lfs.rb @@ -0,0 +1,20 @@ +module Overcommit::Hook::PrePush + # Invokes Git LFS command that uploads files tracked by Git LFS to the LFS storage + # + # @see https://git-lfs.github.com/ + class GitLfs < Base + def run + unless in_path?('git-lfs') + return :warn, 'This repository is configured for Git LFS but \'git-lfs\' ' \ + "was not found on your path.\nIf you no longer wish to use Git LFS, " \ + 'disable this hook by removing or setting \'enabled: false\' for GitLFS ' \ + 'hook in your .overcommit.yml file' + end + + result = execute(['git', 'lfs', 'pre-push', remote_name, remote_url]) + return :fail, result.stderr unless result.success? + + :pass + end + end +end diff --git a/spec/overcommit/hook/pre_push/git_lfs_spec.rb b/spec/overcommit/hook/pre_push/git_lfs_spec.rb new file mode 100644 index 00000000..1d85fc3f --- /dev/null +++ b/spec/overcommit/hook/pre_push/git_lfs_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe Overcommit::Hook::PrePush::GitLfs do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context', remote_name: 'remote_name', remote_url: 'remote_url') } + subject { described_class.new(config, context) } + + let(:result) { double('result') } + + before do + subject.stub(in_path?: true) + subject.stub(:execute).and_return(result) + end + + context 'when git-lfs is not on path' do + before do + subject.stub(in_path?: false) + end + + it { should warn } + end + + context 'when git lfs hook exits successfully' do + before do + result.stub(success?: true, stderr: '') + end + + it { should pass } + end + + context 'when git lfs hook exits unsuccessfully' do + before do + result.stub(success?: false) + result.stub(stderr: 'error: failed to push some refs') + end + + it { should fail_hook } + end +end