/ misc / git-hooks / pre-commit
pre-commit
  1  #
  2  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
  3  #
  4  #!/usr/bin/env bash
  5  
  6  set -euo pipefail
  7  
  8  set +e
  9  git diff-files --quiet
 10  is_unclean=$?
 11  set -e
 12  
 13  # Revert `git stash` on exit
 14  function revert_git_stash {
 15    >&2 echo "Unstashing uncommitted changes..."
 16    git stash pop -q
 17  }
 18  
 19  # Stash pending changes and revert them when script ends
 20  if [ -z "${NO_STASH:-}" ] && [ $is_unclean -ne 0 ]; then
 21    >&2 echo "Stashing uncommitted changes..."
 22    GIT_LITERAL_PATHSPECS=0 git stash -q --keep-index
 23    trap revert_git_stash EXIT
 24  fi
 25  
 26  export FLAKEBOX_GIT_LS
 27  if [ -z "${FLAKEBOX_GIT_LS_IGNORE:-}" ]; then
 28    FLAKEBOX_GIT_LS="$(git ls-files | while read -r file; do [ ! -L "$file" ] && echo "$file"; done)"
 29  else
 30    FLAKEBOX_GIT_LS="$(git ls-files | grep -v -E "${FLAKEBOX_GIT_LS_IGNORE}" | while read -r file; do [ ! -L "$file" ] && echo "$file"; done)"
 31  fi
 32  
 33  export FLAKEBOX_GIT_LS_TEXT
 34  if [ -z "${FLAKEBOX_GIT_LS_TEXT_IGNORE:-}" ]; then
 35    FLAKEBOX_GIT_LS_TEXT="$(echo "$FLAKEBOX_GIT_LS" | grep -v -E "\.(png|ods|jpg|jpeg|woff2|keystore|wasm|ttf|jar|ico|gif)\$")"
 36  else
 37    FLAKEBOX_GIT_LS_TEXT="$(echo "$FLAKEBOX_GIT_LS" | grep -v -E "\.(png|ods|jpg|jpeg|woff2|keystore|wasm|ttf|jar|ico|gif)\$" | grep -v -E "${FLAKEBOX_GIT_LS_TEXT_IGNORE}")"
 38  fi
 39  
 40  
 41  function check_nothing() {
 42    true
 43  }
 44  export -f check_nothing
 45  
 46  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
 47  function check_cargo_fmt() {
 48      set -euo pipefail
 49  
 50      flakebox-in-each-cargo-workspace cargo fmt --all --check
 51  }
 52  export -f check_cargo_fmt
 53  
 54  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
 55  function check_cargo_lock() {
 56      set -euo pipefail
 57  
 58      # https://users.rust-lang.org/t/check-if-the-cargo-lock-is-up-to-date-without-building-anything/91048/5
 59      flakebox-in-each-cargo-workspace cargo update --workspace --locked -q
 60  }
 61  export -f check_cargo_lock
 62  
 63  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
 64  function check_leftover_dbg() {
 65      set -euo pipefail
 66  
 67      errors=""
 68      for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep '.*\.rs'); do
 69        if grep 'dbg!(' "$path" > /dev/null; then
 70          >&2 echo "$path contains dbg! macro"
 71          errors="true"
 72        fi
 73      done
 74  
 75      if [ -n "$errors" ]; then
 76        >&2 echo "Fix the problems above or use --no-verify" 1>&2
 77        return 1
 78      fi
 79  }
 80  export -f check_leftover_dbg
 81  
 82  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
 83  function check_nixfmt() {
 84      set -euo pipefail
 85  
 86      # we actually rely on word splitting here
 87      # shellcheck disable=SC2046
 88      nixfmt -c $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep "\.nix$")
 89  }
 90  export -f check_nixfmt
 91  
 92  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
 93  function check_semgrep() {
 94      set -euo pipefail
 95  
 96      # semgrep is not available on MacOS
 97      if ! command -v semgrep > /dev/null ; then
 98        >&2 echo "Skipping semgrep check: not available"
 99        return 0
100      fi
101  
102      if [ ! -f .config/semgrep.yaml ] ; then
103        >&2 echo "Skipping semgrep check: .config/semgrep.yaml doesn't exist"
104        return 0
105      fi
106  
107      if [ ! -s .config/semgrep.yaml ] ; then
108        >&2 echo "Skipping semgrep check: .config/semgrep.yaml empty"
109        return 0
110      fi
111  
112      env SEMGREP_ENABLE_VERSION_CHECK=0 \
113        semgrep -q --error --no-rewrite-rule-ids --config .config/semgrep.yaml
114  }
115  export -f check_semgrep
116  
117  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
118  function check_shellcheck() {
119      set -euo pipefail
120  
121      for path in $(echo "$FLAKEBOX_GIT_LS_TEXT" | grep -E '.*\.sh$'); do
122        shellcheck --severity=warning "$path"
123      done
124  }
125  export -f check_shellcheck
126  
127  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
128  function check_trailing_newline() {
129      set -euo pipefail
130  
131      errors=""
132      for path in $(echo "$FLAKEBOX_GIT_LS_TEXT"); do
133  
134        # extra branches for clarity
135        if [ ! -f "$path" ] || [ ! -s "$path" ]; then
136           # echo "$path is not a file or is empty"
137           true
138        elif [ -z "$(tail -c 1 < "$path")" ]; then
139           # echo "$path ends with a newline or with a null byte"
140           true
141        else
142          >&2 echo "$path doesn't end with a newline" 1>&2
143          errors="true"
144        fi
145      done
146  
147      if [ -n "$errors" ]; then
148        >&2 echo "Fix the problems above or use --no-verify" 1>&2
149        return 1
150      fi
151  }
152  export -f check_trailing_newline
153  
154  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
155  function check_trailing_whitespace() {
156      set -euo pipefail
157  
158      rev="HEAD"
159      if ! git rev-parse -q 1>/dev/null HEAD 2>/dev/null ; then
160        >&2 echo "Warning: no commits yet, checking against --root"
161        rev="--root"
162      fi
163      if ! git diff --check $rev ; then
164        >&2 echo "Trailing whitespace detected. Please remove them before committing."
165        return 1
166      fi
167  }
168  export -f check_trailing_whitespace
169  
170  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
171  function check_typos() {
172      set -euo pipefail
173  
174      if ! echo "$FLAKEBOX_GIT_LS_TEXT" | typos --file-list - --force-exclude ; then
175        >&2 echo "Typos found: Valid new words can be added to '.typos.toml'"
176        return 1
177      fi
178  }
179  export -f check_typos
180  
181  parallel \
182    --nonotice \
183  ::: \
184      check_cargo_fmt \
185      check_cargo_lock \
186      check_leftover_dbg \
187      check_nixfmt \
188      check_semgrep \
189      check_shellcheck \
190      check_trailing_newline \
191      check_trailing_whitespace \
192      check_typos \
193    check_nothing
194  #
195  # NOTE: THIS FILE IS AUTO-GENERATED BY FLAKEBOX
196  #