Skip to content

Instantly share code, notes, and snippets.

@NICHOLAS85
Last active March 14, 2020 20:52
Show Gist options
  • Save NICHOLAS85/a31f0d31fed7fb06dfe19b4c7a152609 to your computer and use it in GitHub Desktop.
Save NICHOLAS85/a31f0d31fed7fb06dfe19b4c7a152609 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Script that can be run daily to autocommit /bedrock/* changes
bedotcmd(){ git --git-dir=/bedrock/.git --work-tree=/bedrock "${@}"; }
sedotcmd(){ /bedrock/bin/strat -r "$1" git --git-dir=/etc/.git --work-tree=/etc "${@:2}"; }
mapfile -t bedotmod < <(bedotcmd ls-files --modified --exclude-standard)
bedotdel="$(bedotcmd ls-files --deleted --exclude-standard)"
strata=( debian arch )
for stratum in "${strata[@]:1}"; do
/bedrock/bin/strat -r $stratum /etc/etckeeper/daily
done
if ! [ -z "$bedotmod" ] || ! [ -z "$bedotdel" ]; then
AVOID_SPECIAL_FILE_WARNING=1
export AVOID_SPECIAL_FILE_WARNING
hostname=$(hostname)
hostname="${hostname%%.*}"
dnsdomainname=$(dnsdomainname 2>/dev/null || true)
if [ -n "$dnsdomainname" ]; then
hostname="$hostname.$dnsdomainname"
fi
USER=
if [ -n "$SUDO_USER" ]; then
USER="$SUDO_USER"
else
# try to check tty ownership, in case user su'd to root
TTY="$(tty 2>/dev/null || true)"
if [ -n "$TTY" ] && [ -c "$TTY" ]; then
USER="$(find "$TTY" -printf "%u")"
fi
fi
if [ -n "$USER" ]; then
export GIT_AUTHOR_NAME="$USER"
export GIT_AUTHOR_EMAIL="$USER@$hostname"
export GIT_COMMITTER_EMAIL=$(whoami)"@$hostname"
fi
bedotcmd add --update
checkrecent(){
if [[ $((( $(date +%s) - $(stat -c '%Y' "/bedrock/$1" ) ))) -gt 3600 ]]; then
return 1
else
return 0
fi
}
for file in "${bedotmod[@]}"; do
checkrecent "$file" && bedotcmd restore --staged -- "$file"
done
bedotcmd commit -m "daily autocommit" >/dev/null
fi
if ! [ -z "$(bedotcmd cherry -v)" ]; then
bedotcmd push
fi
for stratum in "${strata[@]}"; do
if ! [ -z "$(sedotcmd "$stratum" cherry -v)" ]; then
sedotcmd "$stratum" push
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment