Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ The `make dev` and `make release` commands do three things:
4. Compile the OCaml bytecode to JavaScript
(`_build/default/src/hazelweb/www/hazel.js`) using `js_of_ocaml`.

## Git Hooks

`make deps` installs a `pre-commit` hook (under `scripts/git-hooks/`) by setting `core.hooksPath`. On every commit the hook runs `dune fmt --auto-promote` and re-stages any of the *staged* files the formatter touched, so commits never contain unformatted code.

- Files you didn't stage are left alone, even if the formatter modifies them — your unrelated unstaged WIP won't be swept into the commit.
- If you ever need to bypass the hook (e.g. to share a WIP refactor that you don't want autoformatted yet), pass `--no-verify` to `git commit`.
- If your clone predates this and the hook isn't running, run `make deps` (or `make install-hooks`) once to install it.

## Live Building
For a smoother dev experience, use `make watch` rather than `make dev` to automatically watch
for file changes and rebuild immediately. You can also run `make watch-release` to continuously build the release
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
HTML_DIR="$(shell pwd)/_build/default/src/web/www"
SERVER="http://0.0.0.0:8000/"

.PHONY: all deps change-deps setup-instructor setup-student dev dev-helper dev-student fmt watch watch-release release release-student echo-html-dir serve serve2 repl test clean setup-zarith
.PHONY: all deps change-deps setup-instructor setup-student dev dev-helper dev-student fmt watch watch-release release release-student echo-html-dir serve serve2 repl test clean setup-zarith install-hooks

all: dev

install-hooks:
@if [ "$$(git config --get core.hooksPath)" != "scripts/git-hooks" ]; then \
git config core.hooksPath scripts/git-hooks; \
echo "Installed git hooks (core.hooksPath -> scripts/git-hooks)."; \
fi

# Install native BigInt runtime for zarith_stubs_js to fix WebWorker postMessage serialization.
# The vendored runtime.js uses native JS BigInt (from zarith_stubs_js v0.17.0) which survives
# structured clone, unlike the BigInteger.js library used in older versions.
setup-zarith:
@echo "Installing native BigInt zarith runtime..."
@cp vendor/zarith_native_bigint_runtime.js "$$(opam var lib)/zarith_stubs_js/runtime.js"

deps:
deps: install-hooks
opam repo add archive git+https://github.com/ocaml/opam-repository-archive
opam update
opam install ./hazel.opam.locked --deps-only --with-test --with-doc
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ If you already have `ocaml` version 5.2.0, at least version 2.0 of `opam`, and a
- `make deps`
- `make dev`

`make deps` also installs a pre-commit git hook that auto-formats your staged code on every commit. See [CONTRIBUTING.md](CONTRIBUTING.md#git-hooks) for details.

### Long Version

If the above pre-requisites don't apply, please follow the step-by-step installation instructions contained in [INSTALL.md](INSTALL.md).
Expand Down
16 changes: 16 additions & 0 deletions scripts/git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
set -u

# Capture which files are staged for this commit. After running the formatter,
# we'll re-add these (and only these) so the commit picks up any formatting
# fixes — without sweeping in unrelated unstaged changes the developer may have.
staged=$(git diff --cached --name-only --diff-filter=ACMR)

echo "[pre-commit] dune fmt --auto-promote"
dune fmt --auto-promote || true

if [ -n "$staged" ]; then
while IFS= read -r f; do
[ -e "$f" ] && git add -- "$f"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to behave interestingly in the case where someone partially stages a file. Resulting in the entire file being committed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having thought about this more my personal preference is probably to just abort the commit if there's reformatting. So I would probably stash the unstaged components run formatting (abort if there's changes) then commit then unstash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we can have github reformat on the server

done <<<"$staged"
fi
Loading