about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile15
-rw-r--r--git-notemap.sh90
-rw-r--r--notemap.143
4 files changed, 144 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 7dbdb7e..9e9cfb2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.o
 config.mk
+git-notemap
 notemap
diff --git a/Makefile b/Makefile
index 1fd5a03..5475b97 100644
--- a/Makefile
+++ b/Makefile
@@ -6,22 +6,29 @@ LDLIBS = -ltls
 
 -include config.mk
 
+BINS = notemap git-notemap
+
 OBJS += getservinfo.o
 OBJS += imap.o
 OBJS += notemap.o
 
+all: ${BINS}
+
 notemap: ${OBJS}
 	${CC} ${LDFLAGS} ${OBJS} ${LDLIBS} -o $@
 
 ${OBJS}: imap.h
 
 clean:
-	rm -f notemap ${OBJS}
+	rm -f ${BINS} ${OBJS}
 
-install: notemap notemap.1
+install: ${BINS} notemap.1
 	install -d ${DESTDIR}${PREFIX}/bin ${DESTDIR}${MANDIR}/man1
-	install notemap ${DESTDIR}${PREFIX}/bin
+	install ${BINS} ${DESTDIR}${PREFIX}/bin
 	install -m 644 notemap.1 ${DESTDIR}${MANDIR}/man1
+	ln -fs notemap.1 ${DESTDIR}${MANDIR}/man1/git-notemap.1
 
 uninstall:
-	rm -f ${DESTDIR}${PREFIX}/bin/notemap ${DESTDIR}${MANDIR}/man1/notemap.1
+	rm -f ${BINS:%=${DESTDIR}${PREFIX}/bin/%}
+	rm -f ${DESTDIR}${MANDIR}/man1/notemap.1
+	rm -f ${DESTDIR}${MANDIR}/man1/git-notemap.1
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
diff --git a/notemap.1 b/notemap.1
index 0855524..0c8a9b4 100644
--- a/notemap.1
+++ b/notemap.1
@@ -3,7 +3,8 @@
 .Os
 .
 .Sh NAME
-.Nm notemap
+.Nm notemap ,
+.Nm git-notemap
 .Nd mirror notes to IMAP
 .
 .Sh SYNOPSIS
@@ -16,6 +17,16 @@
 .Ar user
 .Op Ar
 .
+.Nm git
+.Cm notemap
+.Op Fl afv
+.Op Fl M Ar mailbox
+.Op Fl h Ar host
+.Op Fl m Ar file
+.Op Fl p Ar port
+.Op Fl u Ar user
+.Op Ar
+.
 .Sh DESCRIPTION
 The
 .Nm
@@ -49,6 +60,15 @@ or standard input if
 is used.
 .
 .Pp
+The
+.Nm git-notemap
+wrapper uses
+.Xr git-config 1
+and
+.Xr gitcredentials 7
+for defaults and authentication.
+.
+.Pp
 The arguments are as follows:
 .Bl -tag -width Ds
 .It Fl M Ar mailbox
@@ -76,9 +96,30 @@ Log IMAP protocol to standard error.
 Read the password from standard input.
 .El
 .
+.Pp
+The
+.Nm git-notemap
+wrapper loads defaults
+from the following
+.Xr git-config 1
+options:
+.Cm notemap.imapServer ,
+.Cm notemap.imapServerPort ,
+.Cm notemap.imapUser ,
+.Cm notemap.imapPass ,
+.Cm notemap.imapMailbox ,
+.Cm notemap.mapFile .
+If
+.Cm notemap.imapPass
+is unset,
+the password is obtained through
+.Xr gitcredentials 7 .
+.
 .Sh EXAMPLES
 .Bd -literal
 notemap -a june@causal.agency note.txt
+git config notemap.imapUser june@causal.agency
+git notemap note.txt
 .Ed
 .
 .Sh STANDARDS