summary refs log tree commit diff
path: root/git-fetch-email.sh
diff options
context:
space:
mode:
Diffstat (limited to 'git-fetch-email.sh')
-rw-r--r--git-fetch-email.sh94
1 files changed, 64 insertions, 30 deletions
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}"