Use these scripts in a cron job to automatically commit daily to a git repo and create a weekly Git branch.
The following script will commit, pull and push changes when needed. It is used daily, or if wanted hourly.
filename: commit-daily.sh
#!/bin/sh
set -e
echo "------------------------------------------------------------------------"
PREFIX="[commit-daily]"
echo $PREFIX $1
cd $1
[ "$(ps -o comm= $PPID)" = "branch-weekly.s" ] && PREFIX="[branch-weekly]"
git fetch --all
if [ "$(git status --porcelain)" ]; then
echo $PREFIX commit open changes
git add -A
git commit -am $(date +%Y%m%d%H%M)
else
echo $PREFIX no open changes
fi
if [ $(git rev-list @..@{u} --count) -eq 0 ]; then
echo $PREFIX remote has NO new commits
else
echo $PREFIX rebase on new commits in remote
git pull --rebase
fi
if [ $(git rev-list @{u}..@ --count) -eq 0 ]; then
echo $PREFIX local has NO new commits
else
echo $PREFIX push new commits to remote
git push
fi
Use the following line in cron to call the script. This will run it each day at 1 in the morning
0 1 * * * /home/<user>/.local/bin/commit-daily.sh <directory>
The following script assumes the local repo already has two branches, a weekly branch and master.
It also assumes the commit-daily.sh
daily script (above) is available.
filename: branch-weekly.sh
#!/bin/sh
set -e
PREFIX="[branch-weekly]"
echo "------------------------------------------------------------------------"
echo $PREFIX $1
cd $1
current_branch=$(git rev-parse --abbrev-ref HEAD)
new_branch=$(date +y%Yw%W)
if [ "$current_branch" = "$new_branch" ]; then
echo $PREFIX nothing to do
exit 0
fi
echo $PREFIX calling commit-daily
/home/akkerman/.local/bin/commit-daily.sh $1
echo $PREFIX merge current branch to main and delete
git switch main
git merge $current_branch --squash
git commit -am $current_branch
git branch -D $current_branch
echo $PREFIX create new branch
git switch -c $new_branch
echo $PREFIX update origin
git push origin +main:main
git push --delete origin $current_branch
git push --set-upstream origin +$new_branch:$new_branch
Use the following line in cron to call the script. This will run it each monday at 1 in the morning
0 1 * * 1 /home/<user>/.local/bin/branch-weekly.sh <directory>