Skip to content

Runs on contents which aren't staged for commit #60

Closed
@asottile

Description

Here's a minimal example:

FROM ubuntu:bionic
RUN : \
    && apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        git \
    && apt-get clean
RUN : \
    && curl --location --silent --output /tmp/lefthook.gz https://github.com/Arkweid/lefthook/releases/download/v0.6.3/lefthook_0.6.3_Linux_x86_64.gz \
    && echo '0876ae7a862cb26aa5bd50173502dfd836cd16317f7af18be0797deeb3e1cfcd  /tmp/lefthook.gz' | sha256sum --check \
    && gunzip /tmp/lefthook.gz \
    && mv /tmp/lefthook /usr/local/bin \
    && chmod +x /usr/local/bin/lefthook \
    && which lefthook

WORKDIR /src

COPY lefthook.yml ./

ENV \
    GIT_AUTHOR_EMAIL=a@example.com \
    GIT_AUTHOR_NAME='A A' \
    GIT_COMMITTER_EMAIL=a@example.com \
    GIT_COMMITTER_NAME='A A'

RUN : \
    && git init . \
    && echo -n "staged contents" > t \
    && git add . \
    && : unstaged trailing whitespace, commit should succeed \
    && echo ' ' >> t \
    && lefthook install

CMD ["git", "commit", "-m", "test"]
pre-commit:
  commands:
    no-trailing-whitespace:
      files: git diff --staged --name-only
      run: if grep ' $' {files}; then exit 1; fi
docker build -t test .
docker run --rm -ti test
$ docker run --rm -ti test
Lefthook v0.6.3
RUNNING HOOKS GROUP: pre-commit

  EXECUTE > no-trailing-whitespace
t:staged contents 

SUMMARY: (done in 0.00 seconds)
🥊  no-trailing-whitespace

the commit should have succeeded as the trailing whitespace was only in the unstaged contents

A similar case exists but in the worse direction (commit allowed but the staged contents are incorrect)

adjusting the part of the dockerfile above:

$ diff -u Dockerfileold Dockerfile
--- Dockerfileold	2019-08-07 09:07:14.589180456 -0700
+++ Dockerfile	2019-08-07 09:09:58.543863227 -0700
@@ -26,10 +26,11 @@
 
 RUN : \
     && git init . \
-    && echo -n "staged contents" > t \
+    && : staged trailing whitespace should not be allowed \
+    && echo "staged contents " > t \
     && git add . \
-    && : unstaged trailing whitespace, commit should succeed \
-    && echo ' ' >> t \
+    && : remove the trailing whitespace but without staging \
+    && sed -i 's/ *$//g' t \
     && lefthook install
 
 CMD ["git", "commit", "-m", "test"]
$ docker run --rm -ti test
Lefthook v0.6.3
RUNNING HOOKS GROUP: pre-commit

  EXECUTE > no-trailing-whitespace

SUMMARY: (done in 0.00 seconds)
✔️  no-trailing-whitespace
[master (root-commit) 2e9d396] test
 2 files changed, 6 insertions(+)
 create mode 100644 lefthook.yml
 create mode 100644 t

this should have failed, but instead allowed trailing whitespace to enter the repo

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions