From e89379b44595489a3220b498466de60f4b3e4f26 Mon Sep 17 00:00:00 2001 From: John Grib Date: Sat, 30 Nov 2019 09:42:16 +0900 Subject: [PATCH 1/2] Add backup command using git --- bin/droller | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/bin/droller b/bin/droller index 9434c10..37ed139 100755 --- a/bin/droller +++ b/bin/droller @@ -14,7 +14,9 @@ HEAD="$DIRECTORY/head" ## [edit | e] # Edit the index file # Example: droller edit ## [] # 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 @@ -49,6 +51,10 @@ main() { selectRandomRecord checkStatus ;; + "backup"|"b") + backup;; + "log"|"l") + gitLog;; "help"|"h") showHelp;; *) @@ -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 $@ From e4937fbc99e66c760b67fdfe48a5c21e3b807a31 Mon Sep 17 00:00:00 2001 From: John Grib Date: Sat, 30 Nov 2019 09:51:53 +0900 Subject: [PATCH 2/2] Check if the index file should be backup --- bin/droller | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/droller b/bin/droller index 37ed139..538d02e 100755 --- a/bin/droller +++ b/bin/droller @@ -154,9 +154,18 @@ backup() { 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 | head + gitLog +} + +isNeedBackup() { + git -C $DIRECTORY status --short | grep -v '??' | wc -l | tr -d ' ' } gitInit() {