Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git backup #2

Merged
merged 2 commits into from
Nov 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 $@