Teach check-large-files-added about git-lfs. Reslves #82. · lircs/pre-commit-hooks@3f6f23d · GitHub
Skip to content

Commit 3f6f23d

Browse files
committed
Teach check-large-files-added about git-lfs. Reslves pre-commit#82.
1 parent 2c62e4a commit 3f6f23d

6 files changed

Lines changed: 109 additions & 2 deletions

File tree

.coveragerc

Lines changed: 1 addition & 0 deletions

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ script: tox
1212
before_install:
1313
- git config --global user.name "Travis CI"
1414
- git config --global user.email "user@example.com"
15+
# Install git-lfs for a test
16+
- './get-git-lfs.py && export PATH="/tmp/git-lfs:$PATH"'
1517
after_success:
1618
- coveralls
1719
sudo: false
1820
cache:
1921
directories:
2022
- $HOME/.cache/pip
2123
- $HOME/.pre-commit
24+
- /tmp/git-lfs

get-git-lfs.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3.4
2+
"""This is a script to install git-lfs to a tempdir for use in tests"""
3+
import io
4+
import os.path
5+
import shutil
6+
import tarfile
7+
from urllib.request import urlopen
8+
9+
DOWNLOAD_PATH = (
10+
'https://github.com/github/git-lfs/releases/download/'
11+
'v1.1.0/git-lfs-linux-amd64-1.1.0.tar.gz'
12+
)
13+
PATH_IN_TAR = 'git-lfs-1.1.0/git-lfs'
14+
DEST_PATH = '/tmp/git-lfs/git-lfs'
15+
DEST_DIR = os.path.dirname(DEST_PATH)
16+
17+
18+
def main():
19+
if (
20+
os.path.exists(DEST_PATH) and
21+
os.path.isfile(DEST_PATH) and
22+
os.access(DEST_PATH, os.X_OK)
23+
):
24+
print('Already installed!')
25+
return 0
26+
27+
shutil.rmtree(DEST_DIR, ignore_errors=True)
28+
os.makedirs(DEST_DIR, exist_ok=True)
29+
30+
contents = io.BytesIO(urlopen(DOWNLOAD_PATH).read())
31+
with tarfile.open(fileobj=contents) as tar:
32+
with tar.extractfile(PATH_IN_TAR) as src_file:
33+
with open(DEST_PATH, 'wb') as dest_file:
34+
shutil.copyfileobj(src_file, dest_file)
35+
os.chmod(DEST_PATH, 0o755)
36+
37+
if __name__ == '__main__':
38+
exit(main())

pre_commit_hooks/check_added_large_files.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,34 @@
99
import sys
1010

1111
from pre_commit_hooks.util import added_files
12+
from pre_commit_hooks.util import CalledProcessError
13+
from pre_commit_hooks.util import cmd_output
14+
15+
16+
def lfs_files():
17+
try: # pragma: no cover (no git-lfs)
18+
lines = cmd_output('git', 'lfs', 'status', '--porcelain').splitlines()
19+
except CalledProcessError:
20+
lines = []
21+
22+
modes_and_fileparts = [
23+
(line[:3].strip(), line[3:].rpartition(' ')[0]) for line in lines
24+
]
25+
26+
def to_file_part(mode, filepart):
27+
assert mode in ('A', 'R')
28+
return filepart if mode == 'A' else filepart.split(' -> ')[1]
29+
30+
return set(
31+
to_file_part(mode, filepart) for mode, filepart in modes_and_fileparts
32+
if mode in ('A', 'R')
33+
)
1234

1335

1436
def find_large_added_files(filenames, maxkb):
1537
# Find all added files that are also in the list of files pre-commit tells
1638
# us about
17-
filenames = added_files() & set(filenames)
39+
filenames = (added_files() & set(filenames)) - lfs_files()
1840

1941
retv = 0
2042
for filename in filenames:

pre_commit_hooks/util.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ def cmd_output(*cmd, **kwargs):
2121
popen_kwargs.update(kwargs)
2222
proc = subprocess.Popen(cmd, **popen_kwargs)
2323
stdout, stderr = proc.communicate()
24-
stdout, stderr = stdout.decode('UTF-8'), stderr.decode('UTF-8')
24+
stdout = stdout.decode('UTF-8')
25+
if stderr is not None:
26+
stderr = stderr.decode('UTF-8')
2527
if retcode is not None and proc.returncode != retcode:
2628
raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
2729
return stdout

tests/check_added_large_files_test.py

Lines changed: 41 additions & 0 deletions

0 commit comments

Comments
 (0)