Last active
November 14, 2020 12:43
-
-
Save the-codinator/60042e4e01bdf76cb1f33f20449ceff8 to your computer and use it in GitHub Desktop.
A script that automatically commits changes to stuff in a directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script to auto commit all changes in a specific repository | |
# Run this with a scheduler like launchd or systemd | |
REMOTE=origin | |
BRANCH=master | |
DIR="$1" | |
[[ -z "$DIR" ]] && DIR="." | |
[[ ! -d "$DIR" ]] && echo "[LOG] Invalid directory" && exit 2 | |
cd "$DIR" | |
[[ $? -ne 0 ]] && echo "[LOG] Access to directory is restricted" && exit 3 | |
[[ ! -d ".git" ]] && echo "[LOG] Not a git repository" && exit 4 | |
MSG="Auto commit for [$(pwd)] at [$(date)]" | |
echo "[LOG] Running backup: $MSG" | |
# Note: We expect the repo to have been cloned... if starting fresh, make the first commit manually | |
git log --oneline $REMOTE/$BRANCH..$BRANCH >/dev/null | |
[[ $? -ne 0 ]] && echo "[LOG] Error comparing changelog, probably repo not setup correctly?" && exit 5 | |
PENDING=$(git log --oneline $REMOTE/$BRANCH..$BRANCH | wc -l | xargs) | |
[[ "$PENDING" -ne 0 ]] && echo "[LOG] Found $PENDING pending commits" | |
[[ "$PENDING" -gt 3 ]] && echo "[LOG] Warning: auto-commit has multiple consecutive failures" | |
git add . | |
git commit -m "$MSG" | |
[[ $? -ne 0 ]] && echo "[LOG] No new changes detected" && [[ "$PENDING" -eq 0 ]] && exit 0 | |
git fetch $REMOTE $BRANCH >/dev/null | |
[[ $? -ne 0 ]] && echo "[LOG] Error accessing remote server" && exit 6 | |
git push $REMOTE $BRANCH | |
if [[ $? -eq 0 ]] | |
then | |
echo "[LOG] Successfully pushed backup" | |
else | |
echo "[LOG] Failed to push" | |
echo "[LOG] All pending commits will be synced in the next successful push" | |
exit 7 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Data backup launchd agent for work directory --> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" | |
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>org.codi.backup.work</string> | |
<key>WorkingDirectory</key> | |
<string>/Users/ppanchal/Documents/work</string> | |
<key>Program</key> | |
<string>/Users/ppanchal/Documents/work/.auto-commit.sh</string> | |
<key>ExitTimeOut</key> | |
<integer>60</integer> | |
<!-- Every weekday at 2 pm IST (0830 UTC) --> | |
<key>StartCalendarInterval</key> | |
<array> | |
<dict> | |
<key>Hour</key> | |
<integer>8</integer> | |
<key>Minute</key> | |
<integer>30</integer> | |
<key>Weekday</key> | |
<integer>1</integer> | |
</dict> | |
<dict> | |
<key>Hour</key> | |
<integer>8</integer> | |
<key>Minute</key> | |
<integer>30</integer> | |
<key>Weekday</key> | |
<integer>2</integer> | |
</dict> | |
<dict> | |
<key>Hour</key> | |
<integer>8</integer> | |
<key>Minute</key> | |
<integer>30</integer> | |
<key>Weekday</key> | |
<integer>3</integer> | |
</dict> | |
<dict> | |
<key>Hour</key> | |
<integer>8</integer> | |
<key>Minute</key> | |
<integer>30</integer> | |
<key>Weekday</key> | |
<integer>4</integer> | |
</dict> | |
<dict> | |
<key>Hour</key> | |
<integer>8</integer> | |
<key>Minute</key> | |
<integer>30</integer> | |
<key>Weekday</key> | |
<integer>5</integer> | |
</dict> | |
</array> | |
</dict> | |
</plist> | |
<!-- | |
Docs: | |
https://medium.com/swlh/how-to-use-launchd-to-run-services-in-macos-b972ed1e352 | |
https://www.unix.com/man-page/osx/5/launchd.plist/ | |
https://en.wikipedia.org/wiki/Property_list#Format | |
Debug: | |
launchctl list | grep org.codi.backup.work | |
launchctl debug gui/$UID/org.codi.backup.work --stdout --stderr | |
launchctl kickstart gui/$UID/org.codi.backup.work | |
launchctl kill gui/$UID/org.codi.backup.work | |
Registration: | |
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/org.codi.backup.work.plist | |
launchctl bootout gui/$UID/org.codi.backup.work | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example uses launchd as its scheduler (native macos tool)
You can use systemctl as well which is available on linux