Skip to content

Commit

Permalink
Add backup command using git
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrib committed Nov 30, 2019
1 parent 58f5758 commit e89379b
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 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,35 @@ 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
gitInit
gitCommit
gitLog | head
}

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 e89379b

Please sign in to comment.