2
0
mirror of https://github.com/gitwatch/gitwatch synced 2024-11-15 06:12:52 +00:00
Go to file
Yury Shvedov e17d9cf02b
[nix] Add wantedBy to systemd service (#126)
This field needed to properly enable service by configuration.
2024-05-20 07:26:02 -05:00
.github tests back in 2022-12-04 10:44:27 -06:00
tests bats test for new rebasing 2024-03-13 16:54:47 -05:00
.editorconfig straight conversion to bats-core, added super-linter checking 2021-03-07 10:52:50 -07:00
.gitignore Forgot to add *~ for a backup file 2012-11-28 14:43:23 -05:00
.markdownlint.yml ignored linter error SC2294 2022-05-03 13:02:47 -05:00
com.github.gitwatch.plist Example launchd script 2020-12-08 22:48:52 +01:00
Dockerfile fixed hadolint issues 2021-03-09 06:43:07 -07:00
flake.lock Update nix flake to read gitwatch.nix instead of gitwatch.sh (#124) 2024-04-08 07:08:11 -05:00
flake.nix Update nix flake to read gitwatch.nix instead of gitwatch.sh (#124) 2024-04-08 07:08:11 -05:00
gitwatch.nix Add support for NixOS (#117) 2023-12-26 07:21:22 -06:00
gitwatch.sh Add new -R option triggering a git pull --rebase 2024-01-19 10:00:43 +01:00
gitwatch@.service fix sending all script arguments as arg 1 (#125) 2024-04-12 07:00:03 -05:00
LICENSE Add full GPLv3 license text 2012-10-22 09:07:26 +02:00
module.nix [nix] Add wantedBy to systemd service (#126) 2024-05-20 07:26:02 -05:00
package.json make bpkg friendly 2017-08-22 08:29:36 -04:00
README.md Update nix flake to read gitwatch.nix instead of gitwatch.sh (#124) 2024-04-08 07:08:11 -05:00

gitwatch

A bash script to watch a file or folder and commit changes to a git repo

What to use it for?

That's really up to you, but here are some examples:

  • config files: some programs auto-write their config files, without waiting for you to click an 'Apply' button; or even if there is such a button, most programs offer you no way of going back to an earlier version of your settings. If you commit your config file(s) to a git repo, you can track changes and go back to older versions. This script makes it convenient, to have all changes recorded automatically.
  • document files: if you use an editor that does not have built-in git support (or maybe if you don't like the git support it has), you can use gitwatch to automatically commit your files when you save them, or combine it with the editor's auto-save feature to fully automatically and regularly track your changes
  • more stuff! If you have any other uses, or can think of ones, please let us know, and we can add them to this list!

Installation

gitwatch can be installed in various ways.

From Source

gitwatch can be installed from source by simply cloning the repository and putting the shell script into your $PATH. The commands below will do that for you if /usr/local/bin is in your $PATH. You may need to invoke install with sudo.

git clone https://github.com/gitwatch/gitwatch.git
cd gitwatch
[sudo] install -b gitwatch.sh /usr/local/bin/gitwatch

Update

If you installed gitwatch from source, you can update it by following the exact same steps (or git pull rather than clone if you kept the repository around).

bpkg

gitwatch can be installed with bpkg. Make sure you have bpkg installed before running the command below. You may need to invoke bpkg with sudo when using the -g flag.

[sudo] bpkg install -g gitwatch/gitwatch

Archlinux

There is an AUR package for Archlinux. Install it with you favorite aur helper.

NixOs

As Module

If you add gitwatch to your flake, and append field gitwatch.modules to your nixosSystem modules, then you can enable services.gitwatch.*:

services.gitwatch.<service name> = {
    enable = true;
    path = "/home/me/my-repo";
    remote = "git@github.com:me/my-repo.git";
    user = "me";
};

This will make NixOS to create systemd service named gitwatch-<service name>. Note that the service does not start before reboot, or alternatively if it is started manually: systemctl start gitwatch-<service name>.

As Package

You can to play around with nix package inside this repository. Call nix run in this repository to run gitwatch script or nix shell to enter shell with gitwatch command available.

Requirements

To run this script, you must have installed and globally available:

Notes for Mac

If running on OS X, you'll need to install the following Homebrew tools:

brew install fswatch
brew install coreutils

What it does

When you start the script, it prepares some variables and checks if the file or directory given as input really exists.

Then it goes into the main loop (which will run forever, until the script is forcefully stopped/killed), which will:

  • watch for changes to the file/directory using inotifywait (inotifywait will block until something happens)
  • wait 2 seconds
  • case file:
    • cd into the directory containing the file (because git likes to operate locally)
    • git add <file>
    • git commit -m "Scripted auto-commit on change (<date>)"
  • case directory:
    • cd into the directory (because git likes to operate locally)
    • git add --all .
    • git commit -m "Scripted auto-commit on change (<date>)"
  • if a remote is defined (with -r) do a push after the commit (a specific branch can be selected with -b)

Notes:

  • the waiting period of 2 sec is added to allow for several changes to be written out completely before committing; depending on how fast the script is executed, this might otherwise cause race conditions when watching a folder
  • currently, folders are always watched recursively

Usage

gitwatch.sh [-r <remote> [-b <branch>]] <file or directory to watch>

It is expected that the watched file/directory are already in a git repository (the script will not create a repository). If a folder is being watched, this will be watched fully recursively; this also means that all files and sub-folders added and removed from the directory will always be added and removed in the next commit. The .git folder will be excluded from the inotifywait call so changes to it will not cause unnecessary triggering of the script.

If you have any large files in your repository that are changing frequently, you might wish to ignore them with a .gitignore file.

Starting on Boot

If you want to have the script auto-started upon boot, the method to do this depends on your operating system and distribution. If you have a GUI dialog to set up startup launches, you might want to use that, so you can more easily find and change the startup script calls later on.

Please also note that if either of the paths involved (script or target) contains spaces or special characters, you need to escape them accordingly; if you don't know how to do that, the internet will help you, or feel free to ask here or contact me directly.

SysVInit

A central place to put startup scripts on Linux is generally /etc/rc.local (to my knowledge; only tested and confirmed on Ubuntu). This file, if it has the +x bit, will be executed upon startup, by the root user account. If you want to start gitwatch from rc.local, the recommended way to call it is:

su -c "/absolute/path/to/script/gitwatch.sh /absolute/path/to/watched/file/or/folder" -l <username> &

The <username> bit should be replaced with your username or that of any other (non-root) user account; it only needs write-access to the git repository of the file/folder you want to watch. The ampersand (&) at the end sends the launched process into the background (this is important if you have other calls in rc.local after the mentioned line, because the gitwatch call does not usually return).

systemd

  • If installed to a path other than /usr/local/bin/gitwatch, modify gitwatch@.service to suit
  • Create dir if it does not exist and copy systemd service file with mkdir -p "$HOME/.config/systemd/user" && cp gitwatch@.service $HOME/.config/systemd/user
  • Start and enable the service for a given path by running systemctl --user --now enable gitwatch@$(systemd-escape "'-r url/to/repository' /path/to/folder").service

On the Gitwatch Wiki

Community Articles