Keeping a Repository Tidy
A layout that tests enforce, one-off scripts in a folder Git ignores, hooks that catch mistakes, and docs that cannot go stale.
Why tidiness is an operations concern
A messy repository is not just ugly. Stray files get copied into images, picked up by tests, and confuse the next person, which on a solo project is you in three months. Tidiness is cheapest when a machine enforces it.
A layout that a test enforces
This site keeps every section in the same named folder across layers: content/<section>, routes/<section>, views/<section>, lib/<section>. A layout test fails when:
- a file sits at the top of
routes/,views/orlib/instead of in a section folder; - code builds a
content/...ordata/...path by hand instead of asking the one module that knows where things live; - a script in
scripts/is not listed in the scripts README.
The first time each rule fired, it felt fussy. Each one prevents a real class of drift: half the code using the new layout and half the old.
One-off scripts
Every project accumulates scripts that ran once: a migration, a bulk fix, an import. They are dangerous to keep in the main scripts folder, because someone may run them again. The rule here: throwaway scripts go in scripts/_local/, which Git ignores. Scripts that stay are listed, with a line saying what they do, and they follow a safe pattern:
- dry run by default: show what would change, change nothing;
--applyto actually change anything;- refuse to run on
main; - never touch secrets or files outside the repository.
Hooks
A Git hook is a script Git runs at a moment such as before a commit. This repository keeps its hooks in a tracked .githooks folder, so every clone can use the same ones:
git config core.hooksPath .githooks
The pre-commit hook here runs a small cleanup that removes *.bak files and makes sure .gitignore covers common noise. Keep hooks fast and forgiving: a hook that takes a minute or blocks commits for trivial reasons gets switched off, and then protects nothing.
Docs that cannot go stale
Documentation rots when it holds status: "done", "in progress", "next week". This site's docs rule:
- one topic, one file; edit it instead of adding
notes-v2.md; - no dated file names, no status or hand-off files: status belongs in the pull request description;
- an index of every doc, rebuilt by a command, and a check that fails if the index and the folder disagree.
Local clutter
Even with all of that, a working folder collects untracked files: downloads, picture choices, test reports. git status shows them as ??. Once a week:
git status --short
git clean -n # list untracked files, delete nothing
git clean -n is a dry run. Read the list, move anything worth keeping to its proper place, and only then remove the rest.
Try it yourself
Pick one rule your project keeps "by convention", for example where new files go, and turn it into a test that fails when the rule is broken. Conventions are forgotten; failing tests are not.