git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael J Gruber <michaeljgruber+gmane@fastmail.fm>
To: git@vger.kernel.org
Cc: Jeff King <peff@peff.net>, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2] allow user aliases for the --author parameter
Date: Tue, 26 Aug 2008 10:02:56 +0200	[thread overview]
Message-ID: <48B3B8B0.4020609@fastmail.fm> (raw)
In-Reply-To: <20080822211902.GA31884@coredump.intra.peff.net>

This allows the use of author abbreviations when specifying commit
authors via the --author option to git commit. "--author=$key" is
resolved by looking up "user.$key.name" and "user.$key.email" in the
config.

In an ideal word, all my collaborators would exchange changes as git
patches (or even via pull/push). In the real world, they send new
versions which I integrate (after dealing with their whitespace and
encoding changes...). Therefore, being able to say "git commit
--author=mickey" and having git translate "mickey" into "Mickey Mouse
<mickey@ducktown.us>" is a real time saver. The patch accomplishes
this by reading config keys "user.mickey.name" and "user.mickey.email"
when encountering an --author argument without "<>".

Signed-off-by: Michael J Gruber <michaeljgruber+gmane@fastmail.fm>
---

I tried to apply everything I've learned from this thread:
- Justification in commit message rather than cover
- minor style adjustments
- xstrdup two more strings to spare future leakage cleanup-a-thons a few
  unpleasant surprises
- comes with documentation patch now

I think the relation to and distinction from "git-svn -A" and ".mailmap"
has become clear through the discussion (should a summary go in the commit
message?).
I really like Junio's alias (git who). It's certainly helpful. For the
case of "git commit --author key" I think we should not simply go by the
first, possibly non-unique match returned by "git show". Also, being able
to say "git commit --author=nitpicker" may make some days brighter ;)

 Documentation/config.txt     |    8 +++++
 Documentation/git-commit.txt |    5 ++-
 builtin-commit.c             |   67 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 9020675..9bea3a3 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1107,6 +1107,14 @@ user.signingkey::
 	unchanged to gpg's --local-user parameter, so you may specify a key
 	using any method that gpg supports.
 
+user.<author>.email::
+	The email address to be recorded in a newly created commit if you
+	specify the option \--author=<author> to linkgit:git-commit[1].
+
+user.<author>.name::
+	The full name to be recorded in a newly created commit if you
+	specify the option \--author=<author> to linkgit:git-commit[1].
+
 imap::
 	The configuration variables in the 'imap' section are described
 	in linkgit:git-imap-send[1].
diff --git a/Documentation/git-commit.txt b/Documentation/git-commit.txt
index 0e25bb8..1685cf6 100644
--- a/Documentation/git-commit.txt
+++ b/Documentation/git-commit.txt
@@ -76,7 +76,10 @@ OPTIONS
 
 --author=<author>::
 	Override the author name used in the commit.  Use
-	`A U Thor <author@example.com>` format.
+	`A U Thor <author@example.com>` format. Alternatively, if
+	<author> does not contain `<>` then the configuration
+	variables `user.<author>.name` and `user.<author>.email`
+	are used if present (see linkgit:git-config[1]).
 
 -m <msg>::
 --message=<msg>::
diff --git a/builtin-commit.c b/builtin-commit.c
index 649c8be..c36e60f 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -53,6 +53,14 @@ static char *author_name, *author_email, *author_date;
 static int all, edit_flag, also, interactive, only, amend, signoff;
 static int quiet, verbose, no_verify, allow_empty;
 static char *untracked_files_arg;
+struct user {
+	char *name;
+	char *full_name;
+	char *email;
+};
+static struct user **users;
+static int users_alloc;
+static int users_nr;
 /*
  * The default commit message cleanup mode will remove the lines
  * beginning with # (shell comments) and leading and trailing
@@ -406,6 +414,7 @@ static const char sign_off_header[] = "Signed-off-by: ";
 static void determine_author_info(void)
 {
 	char *name, *email, *date;
+	int i;
 
 	name = getenv("GIT_AUTHOR_NAME");
 	email = getenv("GIT_AUTHOR_EMAIL");
@@ -429,10 +438,22 @@ static void determine_author_info(void)
 		date = xstrndup(rb + 2, eol - (rb + 2));
 	}
 
+	author_date = date;
+
 	if (force_author) {
 		const char *lb = strstr(force_author, " <");
 		const char *rb = strchr(force_author, '>');
 
+		if (!lb && !rb) {
+			for (i = 0; i < users_nr; i++) {
+				if (!strcmp(force_author, users[i]->name)) {
+					author_name = xstrdup(users[i]->full_name);
+					author_email = xstrdup(users[i]->email);
+					return;
+				}
+			}
+		}
+
 		if (!lb || !rb)
 			die("malformed --author parameter");
 		name = xstrndup(force_author, lb - force_author);
@@ -441,7 +462,6 @@ static void determine_author_info(void)
 
 	author_name = name;
 	author_email = email;
-	author_date = date;
 }
 
 static int prepare_to_commit(const char *index_file, const char *prefix)
@@ -888,11 +908,56 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 	}
 }
 
+static struct user *make_user(const char *name, int len)
+{
+	struct user *ret;
+	int i;
+
+	for (i = 0; i < users_nr; i++) {
+		if (len ? (!strncmp(name, users[i]->name, len) &&
+			   !users[i]->name[len]) :
+		    !strcmp(name, users[i]->name))
+			return users[i];
+	}
+
+	ALLOC_GROW(users, users_nr + 1, users_alloc);
+	ret = xcalloc(1, sizeof(struct user));
+	users[users_nr++] = ret;
+	if (len)
+		ret->name = xstrndup(name, len);
+	else
+		ret->name = xstrdup(name);
+
+	return ret;
+}
+
 static int git_commit_config(const char *k, const char *v, void *cb)
 {
+	const char *name;
+	const char *subkey;
+	struct user *user;
+
 	if (!strcmp(k, "commit.template"))
 		return git_config_string(&template_file, k, v);
 
+	if (!prefixcmp(k, "user.")) {
+		name = k + 5;
+		subkey = strrchr(name, '.');
+		if (!subkey)
+			return 0;
+		user = make_user(name, subkey - name);
+		if (!strcmp(subkey, ".name")) {
+			if (!v)
+				return config_error_nonbool(k);
+			user->full_name = xstrdup(v);
+		} else if (!strcmp(subkey, ".email")) {
+			if (!v)
+				return config_error_nonbool(k);
+			user->email = xstrdup(v);
+		}
+		return 0;
+	}
+
 	return git_status_config(k, v, cb);
 }
 
-- 
1.6.0

  reply	other threads:[~2008-08-26  8:04 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-21  9:19 [PATCH] allow user aliases for the --author parameter Michael J Gruber
2008-08-21 13:49 ` Miklos Vajna
2008-08-21 14:30   ` Michael J Gruber
2008-08-21 17:41 ` Alex Riesen
2008-08-21 17:49   ` Alex Riesen
2008-08-21 20:02 ` Jeff King
2008-08-22  6:09   ` Junio C Hamano
2008-08-22  8:27   ` Michael J Gruber
2008-08-22 16:50     ` Jeff King
2008-08-22 21:09       ` Junio C Hamano
2008-08-22 21:19         ` Jeff King
2008-08-26  8:02           ` Michael J Gruber [this message]
2008-08-26 23:31             ` [PATCH v2] " Junio C Hamano
2008-08-27  0:19               ` Jeff King
2008-08-27  6:13                 ` Junio C Hamano
2008-08-27  9:36                   ` Michael J Gruber
2008-08-27 12:40                     ` Jeff King
     [not found]                     ` <20080827123656.GB11986@coredump.intra.peff.net>
     [not found]                       ` <7vmyiyqt08.fsf@gitster.siamese.dyndns.org>
2008-08-27 17:18                         ` Jeff King
2008-08-28  8:53                           ` Michael J Gruber
2008-08-28 21:33                             ` Jeff King
     [not found]                     ` <7vr68aqt3h.fsf@gitster.siamese.dyndns.org>
     [not found]                       ` <48B65922.4050005@fastmail.fm>
2008-08-28 21:36                         ` Jeff King
2008-08-27 12:29                   ` Jeff King
2008-08-27 17:19                     ` Junio C Hamano
2008-08-24  9:19         ` [PATCH] " Pedro Melo
2008-08-24 17:21           ` Jeff King
2008-08-25  1:38         ` [PATCH] fix "git log -i --grep" Jeff King
2008-08-25  2:10           ` [PATCH] format-patch: use default diff format even with patch options Jeff King
2008-08-25  4:57             ` Junio C Hamano
2008-08-25  5:12           ` [PATCH] fix "git log -i --grep" Junio C Hamano
2008-08-25  6:15             ` Jeff King
2008-08-25  6:18               ` Jeff King
2008-08-25  6:27               ` Junio C Hamano

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=48B3B8B0.4020609@fastmail.fm \
    --to=michaeljgruber+gmane@fastmail.fm \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /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).