A Rust implementation of the original gitwatch script - with a few additional features.
- Watch a local Git repository and automatically commit changes
- Optionally push to a remote
- Use a custom commit message or generate one via a script
- Configure a debounce time to limit commit frequency
Basic usage:
gitwatch watch /path/to/repo --commit-message "Auto commit"Example use case
I use gitwatch to watch my local notes repository and generate commit messages using aichat.
The example folder contains a small repository demonstrating this use case:
❯ gitwatch watch --help
Watch a repository and commit changes
Usage: gitwatch watch [OPTIONS] [REPOSITORY]
Arguments:
[REPOSITORY] Path to the Git repository to monitor for changes [default: .]
Options:
-m, --commit-message <MESSAGE>
Static commit message to use for all commits
--commit-message-script <SCRIPT>
Path to executable script that generates commit messages.
The path can be absolute or relative to the repository.
The script is executed with the repository as working directory
and must output the message to stdout.
--commit-on-start <COMMIT_ON_START>
Automatically commit any existing changes on start [default: true] [possible values: true, false]
--debounce-seconds <DEBOUNCE_SECONDS>
Number of seconds to wait before processing multiple changes to the same file.
Higher values reduce commit frequency but group more changes together. [default: 1]
--dry-run
Run without performing actual Git operations (staging, committing, etc.)
-i, --ignore-regex <IGNORE_REGEX>
Regular expression pattern for files to exclude from watching.
Matching is performed against repository-relative file paths.
Note: the .git folder & gitignored files are ignored by default.
Example: "\.tmp$" to ignore temporary files.
--log-level <LOG_LEVEL>
Set the log level [default: info] [possible values: trace, debug, info, warn, error]
-r, --remote <REMOTE>
Name of the remote to push to (if specified).
Example: "origin".
--retries <RETRIES>
Number of retry attempts when errors occur.
Use -1 for infinite retries. [default: 3]
-w, --watch <WATCH>
Enable continuous monitoring of filesystem changes.
Set to false for one-time commit of current changes. [default: true] [possible values: true, false]
-h, --help
Print helpMost options can also be configured in a gitwatch.yml file located at the root of the watched repository.
See docs/gitwatch.example.yaml for reference.
Disable GPG commit signing for your watched repo
If you've enabled gpgsign globally, you might want to disable it for the watched repositories, since gitwatch uses your regular git user to create the commits.
- Add an include to a custom
.gitconfigfile to your local gitconfig:# cd /path/to/repo git config --local include.path ../.gitconfig - Create a .gitconfig file (which can be committed):
[commit] gpgsign = false
Run as systemd service
Create a systemd user service file gitwatch@.service:
[Unit]
Description=Watch a Git repository and automatically commit changes
[Service]
ExecStart=/usr/local/bin/gitwatch watch %I
ExecStop=/bin/true
[Install]
WantedBy=default.targetI recommend you use it in combination with a config file, then you only need to pass the repo dir as argument:
systemctl --user start gitwatch@$(systemd-escape /path/to/repo/).serviceThere's also a Nix module, which provides a home-manager service:
inputs = {
gitwatch-rs.url = "github:croissong/gitwatch-rs";
};
...
imports = [ inputs.gitwatch-rs.modules.gitwatch ];
services.gitwatch = {
notes = {
repo_path = "${config.home.homeDirectory}/notes/";
args = [ "--log-level=debug" ];
extraPackages = with pkgs; [
bash
coreutils
git
aichat
];
};
}Nix
A flake.nix is available for Nix:
inputs = {
gitwatch-rs.url = "github:croissong/gitwatch-rs";
};
# Reference the package as `inputs.gitwatch-rs.packages.<system>.default`Ubuntu
# TODO: Add Ubuntu packageDocker
ghcr.io/croissong/gitwatch-rs:
docker run -v /path/to/repo:/repo ghcr.io/croissong/gitwatch-rs:latest /repoBinaries
Precompiled binaries are available for Linux and macOS from releases.
Shell completion scripts for bash, zsh, fish & more can be generated via:
gitwatch completion <SHELL>This is a Rust implementation of the original gitwatch bash script.
Contributions are welcome! Feel free to submit a Pull Request.
See CONTRIBUTING.md for development hints.