|
| 1 | + # Freight |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +Freight is a Go-based CLI tool that streamlines Git workflows by rewiring every Git hook in your repository to call a |
| 6 | +single binary named `conductor` (placed at the repo root).`conductor` reads a JSON configuration file (`railcar.json`) |
| 7 | +that lists the shell commands you want to run for each |
| 8 | +hook. All logic therefore lives in one easy-to-version file instead of a dozen ad-hoc hook scripts. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +* **One-step hook bootstrap** – `freight init` installs `conductor`, generates `railcar.json`, and rewrites every Git |
| 15 | + hook in one go. |
| 16 | +* **Declarative configuration** – add, remove, or reorder hook commands by editing JSON. |
| 17 | +* **Cross-platform binaries** – pre-built for Linux, macOS, and Windows. |
| 18 | +* **Positional-argument support** – hooks such as `commit-msg` or `pre-push` automatically pass their arguments to your |
| 19 | + commands (via `${HOOK_INPUT}`). |
| 20 | +* **Zero vendor lock-in** – all generated files live inside your repo; deleting them restores the default Git behaviour. |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +### From Source |
| 27 | + |
| 28 | +``` |
| 29 | +git clone https://github.com/devbytes-cloud/freight.git |
| 30 | +cd freight |
| 31 | +go mod tidy |
| 32 | +make build-all |
| 33 | +``` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Quick Start |
| 38 | + |
| 39 | +``` |
| 40 | +# Inside any existing Git repository |
| 41 | +freight init # installs conductor + railcar.json + rewired hooks |
| 42 | +git add . && git commit -m "test" # your new hooks will now fire |
| 43 | +``` |
| 44 | + |
| 45 | +Need to overwrite an existing `railcar.json`? |
| 46 | +`freight init --config-force` |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## Command-line Reference |
| 51 | + |
| 52 | +| Command | Description | |
| 53 | +|----------------|---------------------------------------------| |
| 54 | +| `freight init` | Bootstrap Freight in the current repository | |
| 55 | +| `freight help` | Show global or command-specific help | |
| 56 | + |
| 57 | +Global flags: |
| 58 | + |
| 59 | +* `-c, --config-force` – overwrite an existing `railcar.json` |
| 60 | +* `-h, --help` – display help |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## How It Works (under the hood) |
| 65 | + |
| 66 | +1. **Bootstrap (`freight init`)** |
| 67 | + * Places a self-contained `conductor` binary at your repo root. |
| 68 | + * Generates a starter `railcar.json`. |
| 69 | + * Replaces every file in `.git/hooks/` with a tiny wrapper script that simply executes `conductor` with the hook |
| 70 | + name and original arguments. |
| 71 | + |
| 72 | +2. **Hook trigger** – Git fires `pre-commit`, `commit-msg`, etc. |
| 73 | + * The wrapper calls `conductor`. |
| 74 | + * `conductor` loads `railcar.json`, finds the matching section, and runs each configured action. |
| 75 | + * Any non-zero exit in a pre-hook aborts the Git operation. |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## `railcar.json` Syntax |
| 80 | + |
| 81 | +Hierarchical structure |
| 82 | + |
| 83 | +* **config** – top level |
| 84 | +* **\<operation-family\>** – e.g. `commit-operations`, `checkout-operations` |
| 85 | +* **\<git-hook\>** – e.g. `pre-commit`, `commit-msg`, `post-checkout` |
| 86 | +* **actions array** – each item needs: |
| 87 | + * `name` – label for readability |
| 88 | + * `command` – shell snippet to run |
| 89 | + |
| 90 | +Example starter file: |
| 91 | + |
| 92 | +``` |
| 93 | +{ |
| 94 | + "config": { |
| 95 | + "commit-operations": { |
| 96 | + "pre-commit": [ |
| 97 | + { "name": "echo", "command": "echo conductor is running!" } |
| 98 | + ], |
| 99 | + "prepare-commit-msg": [], |
| 100 | + "commit-msg": [], |
| 101 | + "post-commit": [] |
| 102 | + }, |
| 103 | + "checkout-operations": { |
| 104 | + "post-checkout": [] |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Referencing Hook Arguments |
| 111 | + |
| 112 | +Hooks that receive parameters expose them in two interchangeable ways: |
| 113 | + |
| 114 | +| Placeholder | Meaning (example: `commit-msg`) | |
| 115 | +|-----------------|---------------------------------| |
| 116 | +| `${HOOK_INPUT}` | Alias for the parameter (`$1`) | |
| 117 | + |
| 118 | +Use whichever style you prefer: |
| 119 | + |
| 120 | +``` |
| 121 | +{ |
| 122 | + "commit-msg": [ |
| 123 | + { "name": "validate", "command": "grep -E '^(feat|fix): ' ${HOOK_INPUT}" }, |
| 124 | + { "name": "print", "command": "echo 'MSG file → ${HOOK_INPUT}'" } |
| 125 | + ] |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### Real-world Examples |
| 130 | + |
| 131 | +* Run tests before committing |
| 132 | + |
| 133 | +``` |
| 134 | +{ |
| 135 | + "pre-commit": [ |
| 136 | + { "name": "tests", "command": "go test ./..." } |
| 137 | + ] |
| 138 | + } |
| 139 | +``` |
| 140 | + |
| 141 | +* Enforce Conventional Commits format |
| 142 | + |
| 143 | +``` |
| 144 | +{ |
| 145 | + "commit-msg": [ |
| 146 | + { "name": "conventional", "command": "npx commitlint --edit $1" } |
| 147 | + ] |
| 148 | + } |
| 149 | +``` |
| 150 | + |
| 151 | +* Verify tags before pushing |
| 152 | + |
| 153 | +``` |
| 154 | +{ |
| 155 | + "pre-push": [ |
| 156 | + { "name": "verify-tags", "command": "./scripts/check_tags.sh $@" } |
| 157 | + ] |
| 158 | + } |
| 159 | +``` |
| 160 | + |
| 161 | +Notes |
| 162 | + |
| 163 | +* **Order** – actions execute sequentially in array order. |
| 164 | +* **Shell chaining** – combine commands (`go vet ./... && go test ./...`). |
| 165 | +* **Environment vars** – standard shell expansion works (`FOO=bar ./script.sh`). |
| 166 | +* **Idempotency** – `freight init` never overwrites `railcar.json` unless `--config-force` is supplied. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Supported Git Hooks & Execution Order |
| 171 | + |
| 172 | +``` |
| 173 | +Commit : pre-commit → prepare-commit-msg → commit-msg → post-commit |
| 174 | +Merge : pre-merge-commit → post-merge |
| 175 | +Rebase : pre-rebase → post-rewrite |
| 176 | +Push : pre-push → update → post-update → post-receive |
| 177 | +Checkout : pre-checkout → post-checkout |
| 178 | +ApplyPatch : applypatch-msg → pre-applypatch → post-applypatch |
| 179 | +``` |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## Troubleshooting |
| 184 | + |
| 185 | +| Issue | Fix | |
| 186 | +|------------------------------|-----------------------------------------------------------------------------------| |
| 187 | +| Permission denied on hooks | `chmod +x ./conductor` (and ensure hooks are executable) | |
| 188 | +| Hook seems to do nothing | Check `.git/hooks/<hook>` – it should contain the wrapper that calls `conductor`. | |
| 189 | +| Command not found | Ensure the command exists in `$PATH` or use an absolute path in `railcar.json`. | |
| 190 | +| Need to debug a failing hook | Run the failing hook script manually or add `set -x` inside your action command. | |
| 191 | + |
| 192 | +--- |
| 193 | + |
| 194 | +## Contributing |
| 195 | + |
| 196 | +``` |
| 197 | +git clone https://github.com/yourusername/freight.git |
| 198 | +git checkout -b my-feature |
| 199 | +# make changes |
| 200 | +git commit -s -m "feat: awesome contribution" |
| 201 | +git push origin my-feature |
| 202 | +``` |
| 203 | + |
| 204 | +Open a Pull Request—thank you! |
| 205 | + |
| 206 | +### Build & Release |
| 207 | + |
| 208 | +### 1. Release & Distribution (GoReleaser + ) `go:embed` |
| 209 | + |
| 210 | +- The repository contains a that builds **platform-specific `conductor` binaries** for Linux, macOS and Windows (amd64, |
| 211 | + arm, arm64). `.goreleaser.yaml` |
| 212 | +- These binaries are dropped into `assets/dist/` and then **embedded directly into the main `freight` executable** via |
| 213 | + Go’s mechanism (). `//go:embed``assets/embed.go` |
| 214 | +- At runtime, `freight init` extracts the correct pre-built `conductor` for the user’s OS/CPU. |
| 215 | +- CGO is disabled () so binaries are fully statically linked and portable. `CGO_ENABLED=0` |
| 216 | + |
| 217 | +### 2. Hook-Generation Template |
| 218 | + |
| 219 | +- Git hooks are produced from a **single script template** (`internal/githooks/gitHookTemplate`) so every generated hook |
| 220 | + is tiny, consistent, and easy to audit. |
| 221 | +- Unit tests in verify that every hook file is generated with the expected path and template. `githooks_test.go` |
| 222 | + |
| 223 | +A short note in the README can highlight the attention to reliability and test coverage. |
| 224 | + |
| 225 | +### 3. Testing |
| 226 | + |
| 227 | +- The project ships with a Go test suite () that covers hook generation, path handling, and validation helpers. |
| 228 | + `go test ./...` |
| 229 | +- Mentioning this encourages contributors to run tests before submitting pull requests. |
| 230 | + |
| 231 | +### 4. Make Targets |
| 232 | + |
| 233 | +- If you already have a , consider listing other useful targets (`make test`, `make lint`, etc.) so newcomers can find |
| 234 | + them quickly. `make build-all` |
| 235 | + |
| 236 | +--- |
| 237 | + |
| 238 | +## License |
| 239 | + |
| 240 | +BSD-style. See `LICENSE` for full text. |
| 241 | +railcar.json |
| 242 | + |
| 243 | +# Docusaurus |
| 244 | +website/node_modules |
| 245 | +website/.docusaurus |
| 246 | +website/build |
| 247 | +website/.cache |
0 commit comments