git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jacob Keller <jacob.e.keller@intel.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"SZEDER Gábor" <szeder@ira.uka.de>,
	"Shawn O . Pearce" <spearce@spearce.org>,
	"Felipe Contreras" <felipe.contreras@gmail.com>,
	"Lee Marlow" <lee.marlow@gmail.com>,
	"Jacob Keller" <jacob.keller@gmail.com>
Subject: [PATCH RFC] completion: add support for completing email aliases
Date: Fri, 13 Nov 2015 10:17:40 -0800	[thread overview]
Message-ID: <1447438660-6115-1-git-send-email-jacob.e.keller@intel.com> (raw)

From: Jacob Keller <jacob.keller@gmail.com>

Extract email aliases from the sendemail.aliasesfile according to the
known types. Implementation only extracts the alias name and does not
attempt to complete email addresses.

Add a few tests for simple layouts of the currently supported alias
filetypes.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---

Labeled this RFC because I have only been able to test the mutt format
as this is what I use locally. I have a few (probably brittle) test
cases for the files, but they are not "real" configuration files as per
the upstream tools, so they are essentially made to work with the simple
extractors that I have now. I'd like some review on this to see if it's
valuable, but it definitely helps me type out aliases and see what is
available by just using TAB.

 contrib/completion/git-completion.bash | 69 ++++++++++++++++++++++++++
 t/t9902-completion.sh                  | 90 ++++++++++++++++++++++++++++++++++
 2 files changed, 159 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 482ca84b451b..9b786bb390ba 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -10,6 +10,7 @@
 #    *) local and remote tag names
 #    *) .git/remotes file names
 #    *) git 'subcommands'
+#    *) git email aliases for git-send-email
 #    *) tree paths within 'ref:path/to/file' expressions
 #    *) file paths within current working directory and index
 #    *) common --long-options
@@ -785,6 +786,56 @@ __git_aliased_command ()
 	done
 }
 
+# Print aliases for email addresses from sendemail.aliasesfile
+__git_email_aliases ()
+{
+	local file="$(git --git-dir="$(__gitdir)" config --path sendemail.aliasesfile)"
+	local filetype="$(git --git-dir="$(__gitdir)" config sendemail.aliasfiletype)"
+
+	# Only run awk if we find an actual file
+	if ! [ -f $file ]; then
+		return
+	fi
+
+	case "$filetype" in
+		# Each file type needs to be parsed differently.
+		mutt|mailrc)
+			# Mutt and mailrc are simple and just put the alias in
+			# the 2nd field of the file.
+			awk '{print $2}' $file
+			return
+			;;
+		sendmail)
+			# Skip new lines, lines without fields, and lines
+			# ending in '\' then print the name minus the final :
+			awk 'NF && $1!~/^#/ && !/\\$/ {sub(/:$/, "", $1); print $1 }' $file
+			return
+			;;
+		pine)
+			# According to spec, line continuations are any line
+			# which starts with whitespace, otherwise we can just
+			# use the normal separator and print the first field.
+			awk '/^\S/ {print $1}' "$file"
+			return
+			;;
+		elm)
+			# Elm doesn't appear to allow newlines, and
+			# git-send-email only accepts one alias per line, so
+			# just print the first field.
+			awk '{print $1}' "$file"
+			return
+			;;
+		gnus)
+			# The gnus format has the alias quoted, so we just use
+			# gsub to extract the alias from the quotes
+			awk '/define-mail-alias/ {gsub(/"/, "", $2); print $2}' $file
+			return
+			;;
+		*)
+			return;;
+	esac
+}
+
 # __git_find_on_cmdline requires 1 argument
 __git_find_on_cmdline ()
 {
@@ -1735,6 +1786,24 @@ _git_send_email ()
 			" "" "${cur##--thread=}"
 		return
 		;;
+	--to=*)
+		__gitcomp "
+		$(__git_email_aliases)
+		" "" "${cur##--to=}"
+		return
+		;;
+	--cc=*)
+		__gitcomp "
+		$(__git_email_aliases)
+		" "" "${cur##--cc=}"
+		return
+		;;
+	--bcc=*)
+		__gitcomp "
+		$(__git_email_aliases)
+		" "" "${cur##--bcc=}"
+		return
+		;;
 	--*)
 		__gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
 			--compose --confirm= --dry-run --envelope-sender
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 2ba62fbc178e..0549f75e6e7c 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -404,6 +404,96 @@ test_expect_success '__git_aliases' '
 	test_cmp expect actual
 '
 
+test_expect_success '__git_email_aliases (mutt)' '
+	cat >aliases <<-EOF &&
+	alias user1 Some User <user1@example.org>
+	alias user2 random-user-foo@foo.garbage
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype mutt &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '__git_email_aliases (mailrc)' '
+	cat >aliases <<-EOF &&
+	alias user1 Some User <user1@example.org>
+	alias user2 random-user-foo@foo.garbage
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype mailrc &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '__git_email_aliases (sendmail)' '
+	cat >aliases <<-EOF &&
+	user1: Some User <user1@example.org>
+	user2: random-user-foo@foo.garbage
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype sendmail &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '__git_email_aliases (pine)' '
+	cat >aliases <<-EOF &&
+	user1	Some User	user1@example.org>
+	user2	random-user-foo@foo.garbage
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype pine &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '__git_email_aliases (elm)' '
+	cat >aliases <<-EOF &&
+	user1 = User; Someone = user1@example.org
+	user2 = = user2@garbage.foo
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype elm &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '__git_email_aliases (gnus)' '
+	cat >aliases <<-EOF &&
+	define-mail-alias "user1" "user1@example.org"
+	define-mail-alias "user2" "user2@arbitrary.foo"
+	EOF
+	cat >expect <<-EOF &&
+	user1
+	user2
+	EOF
+	test_config sendemail.aliasesfile aliases &&
+	test_config sendemail.aliasfiletype gnus &&
+	__git_email_aliases >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'basic' '
 	run_completion "git " &&
 	# built-in
-- 
2.6.1.264.gbab76a9

             reply	other threads:[~2015-11-13 18:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-13 18:17 Jacob Keller [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-11-14  0:55 [PATCH RFC] completion: add support for completing email aliases SZEDER Gábor
2015-11-14  8:36 ` Jacob Keller
2015-11-14  9:44   ` SZEDER Gábor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447438660-6115-1-git-send-email-jacob.e.keller@intel.com \
    --to=jacob.e.keller@intel.com \
    --cc=felipe.contreras@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jacob.keller@gmail.com \
    --cc=lee.marlow@gmail.com \
    --cc=spearce@spearce.org \
    --cc=szeder@ira.uka.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).