diff options
author | Bert Belder <bertbelder@gmail.com> | 2019-09-14 16:25:49 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2019-09-15 17:47:51 +0200 |
commit | fbfd895bc7439c16d8cdbf209ed9ef28d3985703 (patch) | |
tree | 69310c81012441cd3b99d17a4ff5d91a833a8544 /tools/util.py | |
parent | 456b75cbf9e6934a25fc77cce2afa3d33466b809 (diff) |
tools: refactor lint.py and format.py (#2950)
Diffstat (limited to 'tools/util.py')
-rw-r--r-- | tools/util.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tools/util.py b/tools/util.py index ce4c83573..7f5b95ea2 100644 --- a/tools/util.py +++ b/tools/util.py @@ -166,6 +166,32 @@ def touch(fname): open(fname, 'a').close() +# Recursively list all files in (a subdirectory of) a git worktree. +# * Optionally, glob patterns may be specified to e.g. only list files with a +# certain extension. +# * Untracked files are included, unless they're listed in .gitignore. +# * Directory names themselves are not listed (but the files inside are). +# * Submodules and their contents are ignored entirely. +# * This function fails if the query matches no files. +def git_ls_files(base_dir, patterns=None): + base_dir = os.path.abspath(base_dir) + args = [ + "git", "-C", base_dir, "ls-files", "-z", "--exclude-standard", + "--cached", "--modified", "--others" + ] + if patterns: + args += ["--"] + patterns + output = subprocess.check_output(args) + files = [ + os.path.normpath(os.path.join(base_dir, f)) for f in output.split("\0") + if f != "" + ] + if not files: + raise RuntimeError("git_ls_files: no files in '%s'" % base_dir + + (" matching %s" % patterns if patterns else "")) + return files + + # Recursive search for files of certain extensions. # * Recursive glob doesn't exist in python 2.7. # * On windows, `os.walk()` unconditionally follows symlinks. |