git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Fixing up names in blame output.
@ 2007-04-27  7:54 Junio C Hamano
  2007-04-27  7:54 ` [PATCH 1/2] Split out mailmap handling out of shortlog Junio C Hamano
  2007-04-27  7:54 ` [PATCH 2/2] Apply mailmap in git-blame output Junio C Hamano
  0 siblings, 2 replies; 3+ messages in thread
From: Junio C Hamano @ 2007-04-27  7:54 UTC (permalink / raw)
  To: git; +Cc: Andrew Ruder

The first in the series moves existing functions from
builtin-blame.c to a new file, mailmap.c, and cleans its
interface up a little bit, to make it more usable by other
programs.  This does not depend on anything and can go directly
on top of 'master'.

The second one uses it to fix up the names in git-blame output.
I added a new option '-x' to disable mailmap mapping, but it
might not be worth it.  I also considered moving this logic to
pretty_print_commit, but decided against it.  I do not think it
is a good idea to have irreversible data munging at such a low
level in the system.

I'll park this in 'pu' tonight.  I think this is safe enough to
be fast-tracked to v1.5.2-rc1 if people wanted it, but on the
other hand, I do not think this is such an ungent feature
either.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] Split out mailmap handling out of shortlog
  2007-04-27  7:54 [PATCH 0/2] Fixing up names in blame output Junio C Hamano
@ 2007-04-27  7:54 ` Junio C Hamano
  2007-04-27  7:54 ` [PATCH 2/2] Apply mailmap in git-blame output Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2007-04-27  7:54 UTC (permalink / raw)
  To: git

This splits out a few functions to deal with mailmap from
shortlog and makes it a bit more usable from other programs.
Most notably, it does not clobber input e-mail address anymore.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 Makefile           |    4 +-
 builtin-shortlog.c |   84 ++-----------------------------------------------
 mailmap.c          |   88 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 mailmap.h          |    7 ++++
 4 files changed, 101 insertions(+), 82 deletions(-)
 create mode 100644 mailmap.c
 create mode 100644 mailmap.h

diff --git a/Makefile b/Makefile
index 60c41fd..85c0a75 100644
--- a/Makefile
+++ b/Makefile
@@ -288,7 +288,7 @@ LIB_H = \
 	diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
 	run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
 	tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
-	utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h
+	utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h mailmap.h
 
 DIFF_OBJS = \
 	diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -310,7 +310,7 @@ LIB_OBJS = \
 	write_or_die.o trace.o list-objects.o grep.o match-trees.o \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
-	convert.o attr.o decorate.o progress.o
+	convert.o attr.o decorate.o progress.o mailmap.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
diff --git a/builtin-shortlog.c b/builtin-shortlog.c
index 3f93498..b57a88a 100644
--- a/builtin-shortlog.c
+++ b/builtin-shortlog.c
@@ -5,6 +5,7 @@
 #include "path-list.h"
 #include "revision.h"
 #include "utf8.h"
+#include "mailmap.h"
 
 static const char shortlog_usage[] =
 "git-shortlog [-n] [-s] [<commit-id>... ]";
@@ -26,83 +27,6 @@ static int compare_by_number(const void *a1, const void *a2)
 
 static struct path_list mailmap = {NULL, 0, 0, 0};
 
-static int read_mailmap(const char *filename)
-{
-	char buffer[1024];
-	FILE *f = fopen(filename, "r");
-
-	if (f == NULL)
-		return 1;
-	while (fgets(buffer, sizeof(buffer), f) != NULL) {
-		char *end_of_name, *left_bracket, *right_bracket;
-		char *name, *email;
-		int i;
-		if (buffer[0] == '#') {
-			static const char abbrev[] = "# repo-abbrev:";
-			int abblen = sizeof(abbrev) - 1;
-			int len = strlen(buffer);
-
-			if (len && buffer[len - 1] == '\n')
-				buffer[--len] = 0;
-			if (!strncmp(buffer, abbrev, abblen)) {
-				char *cp;
-
-				if (common_repo_prefix)
-					free(common_repo_prefix);
-				common_repo_prefix = xmalloc(len);
-
-				for (cp = buffer + abblen; isspace(*cp); cp++)
-					; /* nothing */
-				strcpy(common_repo_prefix, cp);
-			}
-			continue;
-		}
-		if ((left_bracket = strchr(buffer, '<')) == NULL)
-			continue;
-		if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
-			continue;
-		if (right_bracket == left_bracket + 1)
-			continue;
-		for (end_of_name = left_bracket; end_of_name != buffer
-				&& isspace(end_of_name[-1]); end_of_name--)
-			/* keep on looking */
-		if (end_of_name == buffer)
-			continue;
-		name = xmalloc(end_of_name - buffer + 1);
-		strlcpy(name, buffer, end_of_name - buffer + 1);
-		email = xmalloc(right_bracket - left_bracket);
-		for (i = 0; i < right_bracket - left_bracket - 1; i++)
-			email[i] = tolower(left_bracket[i + 1]);
-		email[right_bracket - left_bracket - 1] = '\0';
-		path_list_insert(email, &mailmap)->util = name;
-	}
-	fclose(f);
-	return 0;
-}
-
-static int map_email(char *email, char *name, int maxlen)
-{
-	char *p;
-	struct path_list_item *item;
-
-	/* autocomplete common developers */
-	p = strchr(email, '>');
-	if (!p)
-		return 0;
-
-	*p = '\0';
-	/* downcase the email address */
-	for (p = email; *p; p++)
-		*p = tolower(*p);
-	item = path_list_lookup(email, &mailmap);
-	if (item != NULL) {
-		const char *realname = (const char *)item->util;
-		strncpy(name, realname, maxlen);
-		return 1;
-	}
-	return 0;
-}
-
 static void insert_author_oneline(struct path_list *list,
 		const char *author, int authorlen,
 		const char *oneline, int onelinelen)
@@ -184,7 +108,7 @@ static void read_from_stdin(struct path_list *list)
 				(bob = strchr(buffer + 7, '<')) != NULL) {
 			char buffer2[1024], offset = 0;
 
-			if (map_email(bob + 1, buffer, sizeof(buffer)))
+			if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer)))
 				bob = buffer + strlen(buffer);
 			else {
 				offset = 8;
@@ -238,7 +162,7 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
 					die("Invalid commit buffer: %s",
 					    sha1_to_hex(commit->object.sha1));
 
-				if (map_email(bracket + 1, scratch,
+				if (map_email(&mailmap, bracket + 1, scratch,
 							sizeof(scratch))) {
 					author = scratch;
 					authorlen = strlen(scratch);
@@ -360,7 +284,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
 		die ("unrecognized argument: %s", argv[1]);
 
 	if (!access(".mailmap", R_OK))
-		read_mailmap(".mailmap");
+		read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
 
 	if (rev.pending.nr == 0) {
 		if (isatty(0))
diff --git a/mailmap.c b/mailmap.c
new file mode 100644
index 0000000..af187a3
--- /dev/null
+++ b/mailmap.c
@@ -0,0 +1,88 @@
+#include "cache.h"
+#include "path-list.h"
+
+int read_mailmap(struct path_list *map, const char *filename, char **repo_abbrev)
+{
+	char buffer[1024];
+	FILE *f = fopen(filename, "r");
+
+	if (f == NULL)
+		return 1;
+	while (fgets(buffer, sizeof(buffer), f) != NULL) {
+		char *end_of_name, *left_bracket, *right_bracket;
+		char *name, *email;
+		int i;
+		if (buffer[0] == '#') {
+			static const char abbrev[] = "# repo-abbrev:";
+			int abblen = sizeof(abbrev) - 1;
+			int len = strlen(buffer);
+
+			if (len && buffer[len - 1] == '\n')
+				buffer[--len] = 0;
+			if (!strncmp(buffer, abbrev, abblen)) {
+				char *cp;
+
+				if (repo_abbrev)
+					free(*repo_abbrev);
+				*repo_abbrev = xmalloc(len);
+
+				for (cp = buffer + abblen; isspace(*cp); cp++)
+					; /* nothing */
+				strcpy(*repo_abbrev, cp);
+			}
+			continue;
+		}
+		if ((left_bracket = strchr(buffer, '<')) == NULL)
+			continue;
+		if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
+			continue;
+		if (right_bracket == left_bracket + 1)
+			continue;
+		for (end_of_name = left_bracket; end_of_name != buffer
+				&& isspace(end_of_name[-1]); end_of_name--)
+			/* keep on looking */
+		if (end_of_name == buffer)
+			continue;
+		name = xmalloc(end_of_name - buffer + 1);
+		strlcpy(name, buffer, end_of_name - buffer + 1);
+		email = xmalloc(right_bracket - left_bracket);
+		for (i = 0; i < right_bracket - left_bracket - 1; i++)
+			email[i] = tolower(left_bracket[i + 1]);
+		email[right_bracket - left_bracket - 1] = '\0';
+		path_list_insert(email, map)->util = name;
+	}
+	fclose(f);
+	return 0;
+}
+
+int map_email(struct path_list *map, const char *email, char *name, int maxlen)
+{
+	char *p;
+	struct path_list_item *item;
+	char buf[1024], *mailbuf;
+	int i;
+
+	/* autocomplete common developers */
+	p = strchr(email, '>');
+	if (!p)
+		return 0;
+	if (p - email + 1 < sizeof(buf))
+		mailbuf = buf;
+	else
+		mailbuf = xmalloc(p - email + 1);
+
+	/* downcase the email address */
+	for (i = 0; i < p - email; i++)
+		mailbuf[i] = tolower(email[i]);
+	mailbuf[i] = 0;
+	item = path_list_lookup(mailbuf, map);
+	if (mailbuf != buf)
+		free(mailbuf);
+	if (item != NULL) {
+		const char *realname = (const char *)item->util;
+		strncpy(name, realname, maxlen);
+		return 1;
+	}
+	return 0;
+}
+
diff --git a/mailmap.h b/mailmap.h
new file mode 100644
index 0000000..3503fd2
--- /dev/null
+++ b/mailmap.h
@@ -0,0 +1,7 @@
+#ifndef MAILMAP_H
+#define MAILMAP_H
+
+int read_mailmap(struct path_list *map, const char *filename, char **repo_abbrev);
+int map_email(struct path_list *mailmap, const char *email, char *name, int maxlen);
+
+#endif
-- 
1.5.2.rc0.746.gcf51

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] Apply mailmap in git-blame output.
  2007-04-27  7:54 [PATCH 0/2] Fixing up names in blame output Junio C Hamano
  2007-04-27  7:54 ` [PATCH 1/2] Split out mailmap handling out of shortlog Junio C Hamano
@ 2007-04-27  7:54 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2007-04-27  7:54 UTC (permalink / raw)
  To: git

This makes git-blame to use the same mailmap used by
git-shortlog.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 builtin-blame.c |   42 +++++++++++++++++++++++++++++++++++++++---
 1 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/builtin-blame.c b/builtin-blame.c
index c2919b3..3d7590f 100644
--- a/builtin-blame.c
+++ b/builtin-blame.c
@@ -17,14 +17,17 @@
 #include "xdiff-interface.h"
 #include "cache-tree.h"
 #include "log-tree.h"
+#include "path-list.h"
+#include "mailmap.h"
 
 static char blame_usage[] =
-"git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
+"git-blame [-c] [-b] [-l] [--root] [-x] [-t] [-f] [-n] [-s] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
 "  -c                  Use the same output mode as git-annotate (Default: off)\n"
 "  -b                  Show blank SHA-1 for boundary commits (Default: off)\n"
 "  -l                  Show long commit SHA1 (Default: off)\n"
 "  --root              Do not treat root commits as boundaries (Default: off)\n"
 "  -t                  Show raw timestamp (Default: off)\n"
+"  -x                  Do not use .mailmap file\n"
 "  -f, --show-name     Show original filename (Default: auto)\n"
 "  -n, --show-number   Show original linenumber (Default: off)\n"
 "  -s                  Suppress author name and timestamp (Default: off)\n"
@@ -45,7 +48,9 @@ static int show_root;
 static int blank_boundary;
 static int incremental;
 static int cmd_is_annotate;
+static int no_mailmap;
 static int log;
+static struct path_list mailmap;
 
 #ifndef DEBUG
 #define DEBUG 0
@@ -1386,8 +1391,8 @@ static void get_ac_line(const char *inbuf, const char *what,
 			int bufsz, char *person, const char **mail,
 			unsigned long *time, const char **tz)
 {
-	int len;
-	char *tmp, *endp;
+	int len, tzlen, maillen;
+	char *tmp, *endp, *timepos;
 
 	tmp = strstr(inbuf, what);
 	if (!tmp)
@@ -1413,17 +1418,42 @@ static void get_ac_line(const char *inbuf, const char *what,
 	while (*tmp != ' ')
 		tmp--;
 	*tz = tmp+1;
+	tzlen = (person+len)-(tmp+1);
 
 	*tmp = 0;
 	while (*tmp != ' ')
 		tmp--;
 	*time = strtoul(tmp, NULL, 10);
+	timepos = tmp;
 
 	*tmp = 0;
 	while (*tmp != ' ')
 		tmp--;
 	*mail = tmp + 1;
 	*tmp = 0;
+	maillen = timepos - tmp;
+
+	if (!mailmap.nr)
+		return;
+
+	/*
+	 * mailmap expansion may make the name longer.
+	 * make room by pushing stuff down.
+	 */
+	tmp = person + bufsz - (tzlen + 1);
+	memmove(tmp, *tz, tzlen);
+	tmp[tzlen] = 0;
+	*tz = tmp;
+
+	tmp = tmp - (maillen + 1);
+	memmove(tmp, *mail, maillen);
+	tmp[maillen] = 0;
+	*mail = tmp;
+
+	/*
+	 * Now, convert e-mail using mailmap
+	 */
+	map_email(&mailmap, tmp + 1, person, tmp-person-1);
 }
 
 static void get_commit_info(struct commit *commit,
@@ -2269,6 +2299,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		else if (!strcmp("-p", arg) ||
 			 !strcmp("--porcelain", arg))
 			output_option |= OUTPUT_PORCELAIN;
+		else if (!strcmp("-x", arg) ||
+			 !strcmp("--no-mailmap", arg))
+			no_mailmap = 1;
 		else if (!strcmp("--", arg)) {
 			seen_dashdash = 1;
 			i++;
@@ -2473,6 +2506,9 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 		die("reading graft file %s failed: %s",
 		    revs_file, strerror(errno));
 
+	if (!no_mailmap && !access(".mailmap", R_OK))
+		read_mailmap(&mailmap, ".mailmap", NULL);
+
 	assign_blame(&sb, &revs, opt);
 
 	if (incremental || log)
-- 
1.5.2.rc0.746.gcf51

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-04-27  7:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-27  7:54 [PATCH 0/2] Fixing up names in blame output Junio C Hamano
2007-04-27  7:54 ` [PATCH 1/2] Split out mailmap handling out of shortlog Junio C Hamano
2007-04-27  7:54 ` [PATCH 2/2] Apply mailmap in git-blame output Junio C Hamano

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).