Skip to content

Commit

Permalink
#2 feature: backup using git
Browse files Browse the repository at this point in the history
Git backup
  • Loading branch information
johngrib authored Nov 30, 2019
2 parents 58f5758 + e4937fb commit 44202cf
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion bin/droller
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ HEAD="$DIRECTORY/head"
## [edit | e] # Edit the index file # Example: droller edit
## [<uri>] # Add a record to the index file # Example: droller http://www...
## [delete | d] # Delete a record from the index file # Example: droller d
## [help | h] # Help # Example: droller h
## [backup | b] # Back up your index file. # Example: droller backup
## [log | l] # Show backup logs. # Example: droller l
## [help | h] # Help # Example: droller h

main() {
createFiles
Expand Down Expand Up @@ -49,6 +51,10 @@ main() {
selectRandomRecord
checkStatus
;;
"backup"|"b")
backup;;
"log"|"l")
gitLog;;
"help"|"h")
showHelp;;
*)
Expand Down Expand Up @@ -142,5 +148,44 @@ showHelp() {
egrep '## \[' $BASH_SOURCE | column -ts'#'
}

backup() {
git --version 2> /dev/null
if [ "$?" -ne "0" ]; then
printf "Backup requires git. Please install git.\nBackup has been canceled.\n"
return 1
fi
if [ `isNeedBackup` == "0" ]; then
echo "There are no changes. You do not need to back up."
gitLog
return 0
fi
gitInit
gitCommit
gitLog
}

isNeedBackup() {
git -C $DIRECTORY status --short | grep -v '??' | wc -l | tr -d ' '
}

gitInit() {
if [ ! -d "$DIRECTORY/.git" ]; then
mkdir -p $DIRECTORY
git init $DIRECTORY
fi
}

gitCommit() {
linkCount=`wc -l $INDEX | sed 's/^ *\([0-9]*\).*/\1/'`
totalPoint=`awk '{ total+= $2 } END { print total }' $INDEX`
git -C $DIRECTORY add $INDEX
git -C $DIRECTORY commit --allow-empty \
-m "`printf "links: %s / total point: %s" $linkCount $totalPoint`"
}

gitLog() {
git -C $DIRECTORY log --color --graph --pretty=format:'%Cred%h%Creset - %s %Cgreen(%ci)' --abbrev-commit
}

main $@

0 comments on commit 44202cf

Please sign in to comment.