|Felix 99ef9786f0||3 years ago|
|.gitignore||8 years ago|
|LICENSE||8 years ago|
|README.md||3 years ago|
|gitwatch.service||4 years ago|
|gitwatch.sh||4 years ago|
A bash script to watch a file or folder and commit changes to a git repo
That's really up to you, but here are some examples:
To run this script, you must have installed and globally available:
git( git/git | http://www.git-scm.com )
inotifywait(part of inotify-tools: rvoicilas/inotify-tools )
When you start the script, it prepares some variables and checks if the file [a] or directory [b] 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:
inotifywaitwill block until something happens)
cdinto the directory [b] / the directory containing the file [a] (because
gitlikes to operate locally)
git add <file>[a] /
git add .[b]
git commit -m "Scripted auto-commit on change (<date>)"[a] /
git commit -a -m"Scripted auto-commit on change (<date>)"[b]
-r) do a push after the commit (a specific branch can be selected with
gitwatch.sh <file or directory to watch> [-p <remote> [-b <branch>]]
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 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.
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
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> &
<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).
Note that this method assumes you have Gitwatch installed in
systemctl --user enable --now gitwatch.service@/path/to/folder/to/monitor(use
realpath <dir>to get an absolute path)