Yet another commit transformer-- but this one will finally help you truly forget about formatting!
yact is a tool that applies code formatters / prettifiers to your staged
changes, seamlessly updating what you commit. Additionally, it works well when
staging only some changes within a file-- it merges the formatting changes back
into your worktree (accepting the worktree version in case of a conflict). It's
like another programmer cleaning up your commits behind the scenes!
yact is designed to avoid the following issues that plague other popular
auto-formatters / auto-formatting strategies:
- Putting formatting changes in your worktree, aborting the commit, and forcing you to add back the formatting updates to whatever you staged. This is just annoying.
- Not playing nice in cases when some changes are staged and some are unstaged
and still in progress. Some tools result in borkage; others make you
git add -ptwice, which is again, just annoying. - Format on write can sometimes be jarring. For example, when writing a new
function and then saving, the function will be reduced to a minimal form (ex.
int main(){}).
For more information on how yact works, see this page.
- Install Rust
cargo install yact- Set up desired code formatters:
yactdoes not manage formatting tools. This entails that any desired external formatters must be installed and on the system path (or configured to be run from the workspace such as in the case of a virtual environment).
- Create a config file named
yactrc.tomlin the workspace root. Thedocs/config_samplesfolder contains example config files that will apply to most people. Note that the configuration file is dependent on what languages and formatters your team prefers, so these may require modification.
For a full list of configuration options, see this page.
- Update your pre-commit git hook to run yact. In the simplest case (where no pre-commit hooks have been configured yet), run
yact initThis simply symlinks or hardlinks the yact executable into
[repository root]/.git/hooks/pre-commit. If you already have pre-commit
scripts set up that you'd like to keep, add yact to your script manually. Due to
fallability of performing such an update automatically, these cases are left up
to the user.
The configuration is now complete. Committed changes will be formatted transparently.
yact defines a transformer as any process that applies some sort of formatting
to a file. yact includes builtin transformers (written in native Rust) and
transformers that invoke another process.
The standard interface for transformers that are a separate process are a command that reads a source file in from stdin and writes the formatter version to stdout, returning a nonzero exit code if the operation failed.
yact has the following builtin transformers:
TrailingWhitespace: trims trailing whitespace and ensures the source file ends in a newline.
Additionally, yact has options for the following popular transformers (simply
providing the correct command-line arguments to them):
RustfmtClangFormatDenoFmtPrettierRuffFormatGofmt
Finally, yact provides a catch-all System transformer where command, env,
and args can be configured. Example below.
[[items]]
glob = "**/*.rs"
transformers = [ { External = { System = { command = "rustfmt", env = {}, args = ["--emit", "stdout"] }}}]For a full list of configuration options, see this page.
yact will never bork your git history. However, yact will take liberty in
modifying your working tree as it sees fit. This is done in a fairly safe
manner, merging formatting changes back into your worktree but keeping the
worktree's version in case of conflicts.