Automatically push changes with Gitwatch
========================================
My spouse maintains an important spreadsheet with LibreOffice. She
uses an Ubuntu system.

Spreadsheets are fine tools, but unintended changes of cells do
happen, and often stay unnoticed until it is too late. When a
spreadsheet gets bigger, this risk grows too.

I searched for a method to automatically commit any changes into Git
and to push it to a remote. This lowers the risk of accidentally
overwritten cells and of losing the file.

Gitwatch seems a good solution.

Gitwatch
--------
Gitwatch acts on file changes. It commits the changes and then push to
a remote.

It uses inotifywait to trigger it into action.

Depending on the configuration, Gitwatch commits the changes of a
single file, or of all files in an entire directory tree.

When a file on disk changes, Gitwatch first waits two seconds, in case
any other changes follow.

Requirements
------------
The file to watch has to be under Git version control in a Git
repository. It must have a remote repository in its configuration.

It requires a ssh-key without a password and password-less access to
the remote repository to automatically push changes to it.

inotifywait must be on the system (install inotify-tools).

Create a remote repository
--------------------------
To get a test setup, first create a remote repository.

Check that an entry to the remote-system is in your ~/.ssh/config and
allows for password-less ssh-access.

Create the Git repository:

 ssh remote-system
 mkdir ~/myrepo
 cd ~/myrepo
 git init --bare
 exit

Install Gitwatch
----------------
Clone the Git repository to your local system, copy the executable to
/usr/local/bin and copy the file [email protected] to the user's local
systemd directory.

  git clone https://github.com/gitwatch/gitwatch.git
  cd gitwatch
  sudo install -b gitwatch.sh /usr/local/bin/gitwatch
  mkdir -p ~/.config/systemd/user/
  cp [email protected] ~/.config/systemd/user

Clone the remote repository
---------------------------
Next, clone the remote test repository.

  cd
  git clone remote-system:myrepo

Check in a file
---------------
Go into the repository, create a file with some
content, check it in and push it to the remote.

  cd ~/myrepo
  date > testfile
  git add testfile
  git commit -m 'created my testfile'
  git push

Activate Gitwatch
-----------------
Just call Gitwatch with the name of the remote, and the path and name
of the file to watch.

  gitwatch -r remote-system:myrepo ~/myrepo/testfile

Gitwatch keeps running until it is killed or the system halts.

Check that it works
-------------------
Open another terminal, edit the test file and
save to disk.

  date >> ~/myrepo/testfile

Wait a few seconds and check the Git log.

  cd ~/myrepo
  git log

You should see an entry "Scripted auto-commit ..."

Clone the remote repository somewhere else (perhaps in the /tmp
directory) and check that the latest changes are indeed pushed to the
remote.

Set up systemd service
----------------------
Ubuntu uses systemd. To let Gitwatch start after each boot, gitwatch
must be added to the systemd services.

Kill the Gitwatch process (Ctrl-C) that was started from the command
line, and issue the following command to let systemd manage it.

  systemctl --user --now enable gitwatch@$(systemd-escape "'-r remote-system:myrepo' '/home/<username>/myrepo/testfile'").service

Check again that it works. Change the file, wait a few seconds and
issue a git pull in the other repository. The latest change should be
in the file.

When everything is fine, reboot the system and again check that
everything works.

Move from test to production
----------------------------
Now that everything works fine, ditch the test setup and activate
Gitwatch on the "real" file.

Create a remote repository for the "real" file, clone it to the local
system, move the "real" file into the cloned repository. Commit the
file and push to remote.

After this, change the settings in systemd:

  systemctl --user --now disable gitwatch@$(systemd-escape "'-r remote-system:myrepo' '/home/<username>/myrepo/testfile'").service
  systemctl --user --now enable gitwatch@$(systemd-escape "'-r remote-system:realrepo' '/home/<username>/realrepo/realfile'").service

Again, check that changes to the "real" file are committed and pushed,
reboot and retest.

Never lose a change
-------------------
Version control is wonderful for everything that is plain text. It
provides super undo capabilities. Everybody benefits from version
control, not only developers.

Git can also work with binary files, like LibreOffice text documents
and spreadsheets.

Setting up Gitwatch on one or more of those files. Gitwatch automatic
commits all changes to Git and pushes it to a remote repository.

This is great for people who don't know how to use Git. It lowers the
risk of overwriting data or the loss of the file.


Last edited: $Date: 2023/11/27 14:00:17 $