Skip to content

Makefile.python: add check-staged rule for pre-commit hook

When executing the pre-commit hook, we want to run it on the staged files, not on the working directory, or else the buildfarm might reject something that the precommit hook accepted because the staged version was incorrect, but the working directory was correct. This also allows to push valid changes while having a dirty working directory.

Test Plan

Case where there's an error in the working directory, but no errors in the staged changes:

seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % git diff
diff --git a/test.py b/test.py
index 982cc50..a493714 100644
--- a/test.py
+++ b/test.py
@@ -1,3 +1,7 @@
 def respescting_pep8(arg):
     a = 42 + arg
     return a
+
+
+def   not_respecting_pep8 ( arg ) :
+    return(a+42)#return a+42
seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % git diff --staged
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..982cc50
--- /dev/null
+++ b/test.py
@@ -0,0 +1,3 @@
+def respescting_pep8(arg):
+    a = 42 + arg
+    return a
seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % make check-staged
git diff -z --staged --name-only -- '*.py' \
        | xargs -0 -L1 -i'{}' /bin/sh -c \
        "git show ':{}' | python3 -m flake8  --stdin-display-name '{}' -"

Case where there's an error in the staged changes, but no error in the unstaged changes:

seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % git diff --staged
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..965cd23
--- /dev/null
+++ b/test.py
@@ -0,0 +1,2 @@
+def   not_respecting_pep8 ( arg ) :
+    return(a+42)#return a+42
seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % git diff
diff --git a/test.py b/test.py
index 965cd23..a493714 100644
--- a/test.py
+++ b/test.py
@@ -1,2 +1,7 @@
+def respescting_pep8(arg):
+    a = 42 + arg
+    return a
+
+
 def   not_respecting_pep8 ( arg ) :
     return(a+42)#return a+42
seirl@grand-palais ~/swh-environment/swh-vault (git)-[master] % make check-staged
git diff -z --staged --name-only -- '*.py' \
        | xargs -0 -L1 -i'{}' /bin/sh -c \
        "git show ':{}' | python3 -m flake8  --stdin-display-name '{}' -"
test.py:1:4: E271 multiple spaces after keyword
test.py:1:26: E211 whitespace before '('
test.py:1:28: E201 whitespace after '('
test.py:1:32: E202 whitespace before ')'
test.py:1:34: E203 whitespace before ':'
test.py:2:12: F821 undefined name 'a'
test.py:2:17: E261 at least two spaces before inline comment
test.py:2:17: E262 inline comment should start with '# '
../Makefile.python:32: recipe for target 'check-staged' failed
make: *** [check-staged] Error 123

Migrated from D305 (view on Phabricator)

Merge request reports

Loading