summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--git-fetch-email.153
-rw-r--r--git-fetch-email.sh94
2 files changed, 101 insertions, 46 deletions
diff --git a/git-fetch-email.1 b/git-fetch-email.1
index 8ee6508..97a1194 100644
--- a/git-fetch-email.1
+++ b/git-fetch-email.1
@@ -9,6 +9,7 @@
 .Sh SYNOPSIS
 .Nm git
 .Cm fetch-email
+.Op Fl av
 .Op Fl C Ar cc
 .Op Fl F Ar from
 .Op Fl S Ar subject
@@ -16,7 +17,11 @@
 .Op Fl h Ar host
 .Op Fl m Ar mailbox
 .Op Fl p Ar port
-.Op Ar user
+.Op Fl u Ar user
+.Oo
+.Fl Fl
+.Ar args ...
+.Oc
 .
 .Sh DESCRIPTION
 The
@@ -27,27 +32,27 @@ using
 .Xr git-config 1
 and
 .Xr git-credential 1 .
-It fetches patches to be piped into
+It fetches patches to be applied with
 .Xr git-am 1 .
 .
 .Pp
 The arguments are as follows:
 .Bl -tag -width Ds
-.It Fl C Ar cc
+.It Fl C Ar cc , Fl Fl cc Ns = Ns Ar cc , Fl Fl no-cc
 Fetch patches with matching
 .Cm Cc
 headers.
 The default is the value of
 .Cm fetchemail.cc .
 .
-.It Fl F Ar from
+.It Fl F Ar from , Fl Fl from Ns = Ns Ar from , Fl Fl no-from
 Fetch patches with matching
 .Cm From
 headers.
 The default is the value of
 .Cm fetchemail.from .
 .
-.It Fl S Ar subject
+.It Fl S Ar subject, Fl Fl subject Ns = Ns Ar subject , Fl Fl no-subject
 Fetch patches with matching
 .Cm Subject
 headers.
@@ -56,14 +61,26 @@ The default is the value of
 or
 .Ql [PATCH .
 .
-.It Fl T Ar to
+.It Fl T Ar to , Fl Fl to Ns = Ns Ar to , Fl Fl no-to
 Fetch patches with matching
 .Cm To
 headers.
 The default is the value of
 .Cm fetchemail.to .
 .
-.It Fl h Ar host
+.It Fl a , Fl Fl Oo no- Oc Ns apply
+Apply patches with
+.Xr git-am 1 .
+The arguments
+.Fl Fl patch-format Ns = Ns mboxrd
+are passed to
+.Xr git-am 1
+along with any arguments following
+.Ql Fl Fl
+passed to
+.Nm .
+.
+.It Fl h Ar host , Fl Fl host Ns = Ns Ar host
 Connect to IMAP on
 .Ar host .
 The default is the value of
@@ -71,30 +88,33 @@ The default is the value of
 or as inferred by
 .Xr imbox 1 .
 .
-.It Fl m Ar mailbox
+.It Fl m Ar mailbox , Fl Fl mailbox Ns = Ns Ar mailbox
 Fetch patches from
 .Ar mailbox .
 The default is the value of
 .Cm fetchemail.imapMailbox ,
 or INBOX.
 .
-.It Fl p Ar port
+.It Fl p Ar port , Fl Fl port Ns = Ns Ar port
 Connect to IMAP on
 .Ar port .
 The default is the value of
 .Cm fetchemail.imapServerPort ,
 or as inferred by
 .Xr imbox 1 .
+.
+.It Fl u Ar user , Fl Fl user Ns = Ns Ar user
+Log in to IMAP as
+.Ar user .
+The default is the value of
+.Cm fetchemail.imapUser .
+.
+.It Fl v , Fl Fl Oo no- Oc Ns verbose
+Log IMAP protocl to standard error.
 .El
 .
 .Pp
 If
-.Ar user
-is not provided,
-the value of
-.Cm fetchemail.imapUser
-is used.
-If
 .Cm fetchemail.imapPass
 is set,
 it is used as the IMAP login password,
@@ -103,8 +123,9 @@ otherwise the password is obtained using
 .
 .Sh EXAMPLES
 .Bd -literal
+git config fetchemail.imapUser june@causal.agency
 git config fetchemail.to list+imbox@causal.agency
-git fetch-email june@causal.agency | git am
+git fetch-email --apply
 .Ed
 .
 .Sh SEE ALSO
diff --git a/git-fetch-email.sh b/git-fetch-email.sh
index 92455d5..8fcb66d 100644
--- a/git-fetch-email.sh
+++ b/git-fetch-email.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright (C) 2019  C. McEnroe <june@causal.agency>
+# Copyright (C) 2019, 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
@@ -26,22 +26,46 @@ from=$(git config fetchemail.from)
 to=$(git config fetchemail.to)
 cc=$(git config fetchemail.cc)
 
-while getopts 'C:F:S:T:h:m:p:' opt; do
-	case "$opt" in
-		(C) cc=$OPTARG;;
-		(F) from=$OPTARG;;
-		(S) subject=$OPTARG;;
-		(T) to=$OPTARG;;
-		(h) host=$OPTARG;;
-		(m) mailbox=$OPTARG;;
-		(p) port=$OPTARG;;
-		(?) exit 1;;
+OPTS_SPEC="\
+git fetch-email [<options>]
+--
+C,cc= fetch patches with matching Cc headers
+F,from= fetch patches with matching From headers
+S,subject= fetch patches with matching Subject headers
+T,to= fetch patches with matching To headers
+a,apply apply patches with git-am
+h,host=! connect to IMAP on host
+m,mailbox=! fetch patches from mailbox
+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 $?)"
+
+for opt; do
+	case "${opt}" in
+		(-C) cc=$2; shift;;
+		(-F) from=$2; shift;;
+		(-S) subject=$2; shift;;
+		(-T) to=$2; shift;;
+		(-a) apply=1;;
+		(-h) host=$2; shift;;
+		(-m) mailbox=$2; shift;;
+		(-p) port=$2; shift;;
+		(-u) user=$2; shift;;
+		(-v) verbose=1;;
+		(--no-apply) apply=;;
+		(--no-cc) cc=;;
+		(--no-from) from=;;
+		(--no-subject) subject=;;
+		(--no-to) to=;;
+		(--no-verbose) verbose=;;
+		(--) break;;
 	esac
+	shift
 done
-shift $((OPTIND - 1))
-[ $# -ne 0 ] && user=$1
 if [ -z "${user:-}" ]; then
-	echo "$0: username required"
+	echo "${0}: username required" >&2
 	exit 1
 fi
 
@@ -54,23 +78,33 @@ description() {
 	EOF
 }
 
-pass=$(description | git credential fill | grep '^password=')
-pass=${pass#*=}
+if [ -z "${pass:-}" ]; then
+	pass=$(description | git credential fill | grep '^password=')
+	pass=${pass#*=}
+fi
 
-echo "${pass}" | imbox -w \
-	${host:+-h "${host}"} \
-	${port:+-p "${port}"} \
-	${mailbox:+-m "${mailbox}"} \
-	${subject:+-S "${subject}"} \
-	${from:+-F "${from}"} \
-	${to:+-T "${to}"} \
-	${cc:+-C "${cc}"} \
-	${user}
-status=$?
+fetch() {
+	echo "${pass}" | imbox -w \
+		${verbose:+-v} \
+		${host:+-h "${host}"} \
+		${port:+-p "${port}"} \
+		${mailbox:+-m "${mailbox}"} \
+		${subject:+-S "${subject}"} \
+		${from:+-F "${from}"} \
+		${to:+-T "${to}"} \
+		${cc:+-C "${cc}"} \
+		${user}
+	local status=$?
+	if [ $status -ne 78 ]; then
+		description | git credential approve
+	else
+		description | git credential reject
+	fi
+	return $status
+}
 
-if [ "${status}" -ne 78 ]; then
-	description | git credential approve
+if [ -n "${apply:-}" ]; then
+	fetch | git am --patch-format=mboxrd "$@"
 else
-	description | git credential reject
+	fetch
 fi
-exit "${status}"