2016-03-22 20:00:23 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2016-03-22 20:50:20 +01:00
|
|
|
from collections import defaultdict
|
2016-03-22 20:00:23 +01:00
|
|
|
|
2016-03-22 20:50:20 +01:00
|
|
|
def get_ftype(fpath):
|
|
|
|
ext = os.path.splitext(fpath)[1]
|
|
|
|
if ext:
|
|
|
|
return ext[1:]
|
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def list_files(targets=[], ftypes=[], modified_only=False, exclude=[], group_by_ftype=False):
|
2016-03-22 20:00:23 +01:00
|
|
|
"""
|
|
|
|
List files tracked by git.
|
|
|
|
Returns a list of files which are either in targets or in directories in targets.
|
|
|
|
If targets is [], list of all tracked files in current directory is returned.
|
2016-03-22 20:19:43 +01:00
|
|
|
|
|
|
|
Other arguments:
|
2016-03-22 20:50:20 +01:00
|
|
|
ftypes - List of file types on which to filter the search.
|
|
|
|
If ftypes is [], all files are included.
|
2016-03-22 20:19:43 +01:00
|
|
|
modified_only - Only include files which have been modified.
|
2016-03-22 20:34:17 +01:00
|
|
|
exclude - List of paths to be excluded.
|
2016-03-22 20:50:20 +01:00
|
|
|
group_by_ftype - If True, returns a dict of lists keyed by file type.
|
|
|
|
If False, returns a flat list of files.
|
2016-03-22 20:00:23 +01:00
|
|
|
"""
|
2016-03-22 20:50:20 +01:00
|
|
|
ftypes = [x.strip('.') for x in ftypes]
|
|
|
|
ftypes_set = set(ftypes)
|
|
|
|
|
2016-03-22 20:00:23 +01:00
|
|
|
cmdline = ['git', 'ls-files'] + targets
|
2016-03-22 20:19:43 +01:00
|
|
|
if modified_only:
|
|
|
|
cmdline.append('-m')
|
2016-03-22 20:00:23 +01:00
|
|
|
|
|
|
|
files_gen = (x.strip() for x in subprocess.check_output(cmdline, universal_newlines=True).split('\n'))
|
|
|
|
# throw away empty lines and non-files (like symlinks)
|
|
|
|
files = list(filter(os.path.isfile, files_gen))
|
|
|
|
|
2016-03-22 20:50:20 +01:00
|
|
|
result = defaultdict(list) if group_by_ftype else []
|
2016-03-22 20:34:17 +01:00
|
|
|
|
|
|
|
for fpath in files:
|
|
|
|
# this will take a long time if exclude is very large
|
|
|
|
in_exclude = False
|
|
|
|
for expath in exclude:
|
|
|
|
expath = expath.rstrip('/')
|
|
|
|
if fpath == expath or fpath.startswith(expath + '/'):
|
|
|
|
in_exclude = True
|
|
|
|
if in_exclude:
|
|
|
|
continue
|
|
|
|
|
2016-03-22 20:50:20 +01:00
|
|
|
if ftypes or group_by_ftype:
|
|
|
|
filetype = get_ftype(fpath)
|
|
|
|
if ftypes and filetype not in ftypes_set:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if group_by_ftype:
|
|
|
|
result['.' + filetype].append(fpath)
|
|
|
|
else:
|
|
|
|
result.append(fpath)
|
2016-03-22 20:34:17 +01:00
|
|
|
|
|
|
|
return result
|