[PATCH 1/2] Use git status -z
David Shea
dshea at redhat.com
Tue Nov 25 19:50:43 UTC 2014
This format is "recommended for machine parsing," and this script is a
machine, beep boop, so let's use it.
The changes to the output are as follows:
- nul instead of newline, so you can use newlines in filenames I guess
(please do not do this)
- filenames are never ever quoted (a comforting guarantee)
- renames are reordered (target first instead of source first) and
separated by nul, which makes parsing them simpler for us.
---
scripts/githooks/pre-commit | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/scripts/githooks/pre-commit b/scripts/githooks/pre-commit
index c8832ef..0f62d3c 100755
--- a/scripts/githooks/pre-commit
+++ b/scripts/githooks/pre-commit
@@ -30,25 +30,30 @@ if "NOPYLINT" in os.environ:
# run pylint on all the python files changed by this commit
try:
- git_files = check_output("git status --porcelain", shell=True)
+ git_files = check_output("git status --porcelain -z", shell=True)
except CalledProcessError:
sys.exit(1)
+git_files = git_files.strip('\0').split('\0')
+
pylint_files = []
# Lines look like: MM tests/pylint/runpylint.sh
# The first character is the status in the index (or our side of a merge),
# the second character is the status in the tree (or their side of a merge).
-for gf in git_files.splitlines():
+# Use an iterator so we can skip lines in the case of renames
+git_iter = iter(git_files)
+for gf in git_iter:
# If the file is being removed, or is not in git, or is not part of this
# commit, ignore it
if gf[0] in ('D', '?', ' '):
continue
- # If the file is being renamed, grab the target filename
+
+ # If the file is being renamed, the target filename (which we want) is on
+ # the current line, and the next line is the source, which we want to skip
if gf[0] == 'R':
- target = gf[3:].split(' -> ')[1]
- if is_python(target):
- pylint_files.append(target)
- elif is_python(gf[3:]):
+ git_iter.next()
+
+ if is_python(gf[3:]):
# If the file is unmerged or changed locally, raise an error
if gf[0] == 'U' or gf[1] in ('U', 'M'):
print("ERROR: %s in commit does not match tree" % gf[3:])
--
2.1.0
More information about the anaconda-patches
mailing list