diff options
author | MichaĆ Zdunek <mzdunek93@users.noreply.github.com> | 2020-05-09 12:22:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-09 06:22:27 -0400 |
commit | 9790399bcea36091da85799c4ed86ead6c9af92a (patch) | |
tree | e3ee9bf993eeaf4d93f0e0f148de6a2611d9880d /tools/util.py | |
parent | 4e5e6da348fb6c6d289426f313e564d6fd6fe242 (diff) |
add option to lint and format only staged files (#5172)
Diffstat (limited to 'tools/util.py')
-rw-r--r-- | tools/util.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tools/util.py b/tools/util.py index c4aaa1e5b..e5dcaccce 100644 --- a/tools/util.py +++ b/tools/util.py @@ -193,6 +193,23 @@ def git_ls_files(base_dir, patterns=None): return files +# list all files staged for commit +def git_staged(base_dir, patterns=None): + base_dir = os.path.abspath(base_dir) + args = [ + "git", "-C", base_dir, "diff", "--staged", "--diff-filter=ACMR", + "--name-only", "-z" + ] + 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 != "" + ] + return files + + # The Python equivalent of `rm -rf`. def rmtree(directory): # On Windows, shutil.rmtree() won't delete files that have a readonly bit. @@ -406,3 +423,8 @@ def tty_capture(cmd, bytes_input, timeout=5): os.close(fd) # can't do it sooner: it leads to errno.EIO error p.wait() return p.returncode, res['stdout'], res['stderr'] + + +def print_command(cmd, files): + noun = "file" if len(files) == 1 else "files" + print "%s (%d %s)" % (cmd, len(files), noun) |