about summary refs log tree commit diff
path: root/git-notemap.sh
diff options
context:
space:
mode:
Diffstat (limited to 'git-notemap.sh')
-rw-r--r--git-notemap.sh90
1 files changed, 90 insertions, 0 deletions
diff --git a/git-notemap.sh b/git-notemap.sh
new file mode 100644
index 0000000..601987b
--- /dev/null
+++ b/git-notemap.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+# Copyright (C) 2020  C. McEnroe <june@causal.agency>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+set -u
+
+add=
+force=
+verbose=
+host=$(git config notemap.imapServer)
+port=$(git config notemap.imapServerPort)
+user=$(git config notemap.imapUser)
+pass=$(git config notemap.imapPass)
+mailbox=$(git config notemap.imapMailbox)
+path=$(git config notemap.mapFile)
+
+OPTS_SPEC="\
+git notemap [<options>] [<args>...]
+--
+M,mailbox=! mirror notes to mailbox
+a,add! add new notes to map file
+f,force! overwite modified notes in mailbox
+h,host=! connect to IMAP on host
+m,map-file=! set path to map file
+p,port=! connect to IMAP on port
+u,user=! log in to IMAP as user
+v,verbose! log IMAP protocol to standard error
+"
+eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
+
+while [ $# -gt 0 ]; do
+	opt=$1
+	shift
+	case "${opt}" in
+		(-M) mailbox=$1; shift;;
+		(-a) add=yes;;
+		(-f) force=yes;;
+		(-h) host=$1; shift;;
+		(-m) path=$1; shift;;
+		(-p) port=$1; shift;;
+		(-u) user=$1; shift;;
+		(-v) verbose=yes;;
+		(--) break;;
+	esac
+done
+if [ -z "${user}" ]; then
+	echo "${0}: username required" >&2
+	exit 1
+fi
+
+description() {
+	cat <<-EOF
+		protocol=imaps
+		host=${host:-${user#*@}}
+		username=${user%@*}
+		${pass:+password=${pass}}
+	EOF
+}
+
+if [ -z "${pass}" ]; then
+	pass=$(description | git credential fill | grep '^password=')
+	pass=${pass#*=}
+fi
+
+printf '%s' "${pass}" | notemap -w \
+	${add:+-a} ${force:+-f} ${verbose:+-v} \
+	${path:+-m "${path}"} \
+	${host:+-h "${host}"} \
+	${port:+-p "${port}"} \
+	${mailbox:+-M "${mailbox}"} \
+	"${user}" "$@"
+status=$?
+if [ $status -ne 78 ]; then
+	description | git credential approve
+else
+	description | git credential reject
+fi
+exit $status