From 8df327ee53951d1b928426ed2215c29f6842f28d Mon Sep 17 00:00:00 2001 From: stormdragon2976 Date: Mon, 3 Apr 2023 19:26:26 -0400 Subject: [PATCH] Initial work on adding git support for backups. --- notestorm | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/notestorm b/notestorm index dc9f78a..1a9c449 100755 --- a/notestorm +++ b/notestorm @@ -108,6 +108,11 @@ add_note() { $editor "${xdgPath}/notestorm/notes/${noteName}.md" if [ -f "${xdgPath}/notestorm/notes/${noteName}.md" ]; then infobox "Note added." + if [[ -d "$xdgPath/notestorm/notes/.git" ]]; then + { git -C "$xdgPath/notestorm/notes" add -A + git -C "$xdgPath/notestorm/notes" commit -m "$(*gettext "Automatic push from notestorm, note added.")" + git -C "$xdgPath/notestorm/notes" push -q ; } | dialog --progressbox "$(gettext "Pushing to git...")" -1 -1 + fi else infobox "Note canceled." fi @@ -133,6 +138,11 @@ delete_note() { if [ "$answer" == "Yes" ]; then rm -f "$1" infobox "$(gettext "Note deleted.")" + if [[ -d "$xdgPath/notestorm/notes/.git" ]]; then + { git -C "$xdgPath/notestorm/notes" rm -f "$1" + git -C "$xdgPath/notestorm/notes" commit -m "$(*gettext "Automatic push from notestorm, note deleted.")" + git -C "$xdgPath/notestorm/notes" push -q ; } | dialog --progressbox "$(gettext "Pushing to git...")" -1 -1 + fi else infobox "$(gettext "Action canceled.")" fi @@ -162,6 +172,11 @@ edit_note() { local newMd5="$(md5sum "$1")" if [ "$oldMd5" != "$newMd5" ]; then infobox "$(gettext "Changes saved.")" + if [[ -d "$xdgPath/notestorm/notes/.git" ]]; then + { git -C "$xdgPath/notestorm/notes" add -A + git -C "$xdgPath/notestorm/notes" commit -m "$(*gettext "Automatic push from notestorm, note updated.")" + git -C "$xdgPath/notestorm/notes" push -q ii; } | dialog --progressbox "$(gettext "Pushing to git...")" -1 -1 + fi else infobox "$(gettext "Changes discarded.")" fi @@ -182,6 +197,12 @@ encrypt_note() { if gpg -o "${1%/*}/${noteName%.md*}.md.gpg" -c "$1" ; then rm -f "$1" infobox "$(gettext "Note encrypted.")" + if [[ -d "$xdgPath/notestorm/notes/.git" ]]; then + { git -C "$xdgPath/notestorm/notes" add -A + git rm -f "$1" + git -C "$xdgPath/notestorm/notes" commit -m "$(*gettext "Automatic push from notestorm, note encrypted.")" + git -C "$xdgPath/notestorm/notes" push -q ; } | dialog --progressbox "$(gettext "Pushing to git...")" -1 -1 + fi return fi infobox "$(gettext "Encryption failed.")" @@ -195,6 +216,28 @@ list_notes() { done } +initialize_git() { + local message + if [[ -d "$xdgPath/notestorm/notes/.git" ]]; then + message="$(gettext "A git configuration already exists.")" + infobox "$message" + exit 1 + fi + message="$(gettext "Please enter the url to your git repository for notes.")" + local gitURL="$(inputbox "$message")" + if [[ ${#gitURL} -lt 3 ]]; then + message="$(gettext "Invalid URL detected, exiting.")" + infobox "$message" + exit 1 + fi + { git -C "$xdgPath/notestorm/notes" init + git -C "$xdgPath/notestorm/notes" remote add origin "$gitURL" + git -C "$xdgPath/notestorm/notes" add -A + git -C "$xdgPath/notestorm/notes" commit -m "Initial commit added by notestorm" + git -C "$xdgPath/notestorm/notes" push -u origin master ; } | dialog --progressbox "$(gettext "Setting up git...")" -1 -1 + exit 0 +} + original_note() { # If no notes are present this will try to copy the README.md file into the notes directory so the menu won't fail. mapfile -t notes < <(find "$xdgPath/notestorm/notes" -type f -iname '*.md' -o -iname '*.gpg') @@ -210,9 +253,11 @@ original_note() { fi } + # Configuration section # Available arguments in both long and short versions stored in associative array. declare -A argList=( + [g]="git" [l]="list" [n]="new") # Make the args a continuous string. @@ -262,6 +307,7 @@ fi # Parse non-numeric command line args. if ! options=$(getopt -o "$short" -l "$long" -n "notestorm" -- "$@"); then gettext -e "Usage: notestorm launch interactive session.\n" + gettext -e -- "-g or --git set up backups to git. Requires existing git repository with branch name \"master\".\n" gettext -e -- "-n or --new add a new note without opening an interactive session.\n" echo gettext -e "You can use markdown syntax in notes.\n" @@ -277,8 +323,9 @@ eval set -- "$options" while [ $# -gt 0 ]; do case "$1" in - "-n"|"--new") add_note;; + "-g"|"--git") initialize_git;; "-l"|"--list") list_notes;; + "-n"|"--new") add_note;; esac shift done