Skip to content

Commit 88882f2

Browse files
detouchedsds
authored andcommitted
Add Git LFS pre-push hook
Git LFS installs pre-push hook which: - checks that git-lfs utility is on PATH, and if no, warns that Git LFS hook should probably be removed - calls it by executing `git lfs pre-push <remote_name> <remote_url>` The introduced hook is a Ruby port of the same algorithm modulo hook disabling note in the warning message.
1 parent 336c495 commit 88882f2

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ but before any objects have been transferred. If a hook fails, the push is
549549
aborted.
550550

551551
* [Brakeman](lib/overcommit/hook/pre_push/brakeman.rb)
552+
* [GitLfs](lib/overcommit/hook/pre_push/git_lfs.rb)
552553
* [Minitest](lib/overcommit/hook/pre_push/minitest.rb)
553554
* [ProtectedBranches](lib/overcommit/hook/pre_push/protected_branches.rb)
554555
* [Pytest](lib/overcommit/hook/pre_push/pytest.rb)

config/default.yml

+4
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ PrePush:
945945
flags: ['--exit-on-warn', '--quiet', '--summary']
946946
install_command: 'gem install brakeman'
947947

948+
GitLfs:
949+
enabled: false
950+
description: 'Upload files tracked by Git LFS'
951+
948952
# Hooks that run during `git rebase`, before any commits are rebased.
949953
# If a hook fails, the rebase is aborted.
950954
PreRebase:
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Overcommit::Hook::PrePush
2+
# Invokes Git LFS command that uploads files tracked by Git LFS to the LFS storage
3+
#
4+
# @see https://git-lfs.github.com/
5+
class GitLfs < Base
6+
def run
7+
result = execute(['command', '-v', 'git-lfs'])
8+
unless result.success?
9+
return :warn, 'This repository is configured for Git LFS but \'git-lfs\' ' \
10+
'was not found on your path.\nIf you no longer wish to use Git LFS, ' \
11+
'disable this hook by removing or setting \'enabled: false\' for GitLFS ' \
12+
'hook in your .overcommit.yml file'
13+
end
14+
15+
result = execute(['git', 'lfs', 'pre-push', remote_name, remote_url])
16+
return :fail, result.stderr unless result.success?
17+
18+
:pass
19+
end
20+
end
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
3+
describe Overcommit::Hook::PrePush::GitLfs do
4+
let(:config) { Overcommit::ConfigurationLoader.default_configuration }
5+
let(:context) { double('context', remote_name: 'remote_name', remote_url: 'remote_url') }
6+
subject { described_class.new(config, context) }
7+
8+
let(:result) { double('result') }
9+
10+
before do
11+
subject.stub(:execute).and_return(result)
12+
end
13+
14+
context 'when git-lfs is not on path' do
15+
before do
16+
result.stub(success?: false, stderr: '')
17+
end
18+
19+
it { should warn }
20+
end
21+
22+
context 'when git lfs hook exits successfully' do
23+
before do
24+
result.stub(success?: true, stderr: '')
25+
end
26+
27+
it { should pass }
28+
end
29+
30+
context 'when git lfs hook exits unsuccessfully' do
31+
before do
32+
# First for checking that git-lfs is on path, second for calling the hook itself
33+
result.stub(:success?).and_return(true, false)
34+
result.stub(:stderr).and_return('', 'error: failed to push some refs')
35+
end
36+
37+
it { should fail_hook }
38+
end
39+
end

0 commit comments

Comments
 (0)