@matheusbsbs / matheus-bagliano / commits / 7c1c6782e0
Fork 0

Split out mailmap handling out of shortlog

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>

Junio C Hamano committed Apr 27, 2007 at 00:41 UTC 7c1c6782e0b88c9366c575fd47e48050070afdd3
4 files changed +101 -82
Makefile
+2 -2
@@ -288,7 +288,7 @@ LIB_H = \
288 diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
289 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
290 tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
291 - utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h
291 + utf8.h reflog-walk.h patch-ids.h attr.h decorate.h progress.h mailmap.h
292
293 DIFF_OBJS = \
294 diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -310,7 +310,7 @@ LIB_OBJS = \
310 write_or_die.o trace.o list-objects.o grep.o match-trees.o \
311 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
312 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
313 - convert.o attr.o decorate.o progress.o
313 + convert.o attr.o decorate.o progress.o mailmap.o
314
315 BUILTIN_OBJS = \
316 builtin-add.o \
builtin-shortlog.c
+4 -80
@@ -5,6 +5,7 @@
5 #include "path-list.h"
6 #include "revision.h"
7 #include "utf8.h"
8 +#include "mailmap.h"
9
10 static const char shortlog_usage[] =
11 "git-shortlog [-n] [-s] [<commit-id>... ]";
@@ -26,83 +27,6 @@ static int compare_by_number(const void *a1, const void *a2)
27
28 static struct path_list mailmap = {NULL, 0, 0, 0};
29
29 -static int read_mailmap(const char *filename)
30 -{
31 - char buffer[1024];
32 - FILE *f = fopen(filename, "r");
33 -
34 - if (f == NULL)
35 - return 1;
36 - while (fgets(buffer, sizeof(buffer), f) != NULL) {
37 - char *end_of_name, *left_bracket, *right_bracket;
38 - char *name, *email;
39 - int i;
40 - if (buffer[0] == '#') {
41 - static const char abbrev[] = "# repo-abbrev:";
42 - int abblen = sizeof(abbrev) - 1;
43 - int len = strlen(buffer);
44 -
45 - if (len && buffer[len - 1] == '\n')
46 - buffer[--len] = 0;
47 - if (!strncmp(buffer, abbrev, abblen)) {
48 - char *cp;
49 -
50 - if (common_repo_prefix)
51 - free(common_repo_prefix);
52 - common_repo_prefix = xmalloc(len);
53 -
54 - for (cp = buffer + abblen; isspace(*cp); cp++)
55 - ; /* nothing */
56 - strcpy(common_repo_prefix, cp);
57 - }
58 - continue;
59 - }
60 - if ((left_bracket = strchr(buffer, '<')) == NULL)
61 - continue;
62 - if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
63 - continue;
64 - if (right_bracket == left_bracket + 1)
65 - continue;
66 - for (end_of_name = left_bracket; end_of_name != buffer
67 - && isspace(end_of_name[-1]); end_of_name--)
68 - /* keep on looking */
69 - if (end_of_name == buffer)
70 - continue;
71 - name = xmalloc(end_of_name - buffer + 1);
72 - strlcpy(name, buffer, end_of_name - buffer + 1);
73 - email = xmalloc(right_bracket - left_bracket);
74 - for (i = 0; i < right_bracket - left_bracket - 1; i++)
75 - email[i] = tolower(left_bracket[i + 1]);
76 - email[right_bracket - left_bracket - 1] = '\0';
77 - path_list_insert(email, &mailmap)->util = name;
78 - }
79 - fclose(f);
80 - return 0;
81 -}
82 -
83 -static int map_email(char *email, char *name, int maxlen)
84 -{
85 - char *p;
86 - struct path_list_item *item;
87 -
88 - /* autocomplete common developers */
89 - p = strchr(email, '>');
90 - if (!p)
91 - return 0;
92 -
93 - *p = '\0';
94 - /* downcase the email address */
95 - for (p = email; *p; p++)
96 - *p = tolower(*p);
97 - item = path_list_lookup(email, &mailmap);
98 - if (item != NULL) {
99 - const char *realname = (const char *)item->util;
100 - strncpy(name, realname, maxlen);
101 - return 1;
102 - }
103 - return 0;
104 -}
105 -
30 static void insert_author_oneline(struct path_list *list,
31 const char *author, int authorlen,
32 const char *oneline, int onelinelen)
@@ -184,7 +108,7 @@ static void read_from_stdin(struct path_list *list)
108 (bob = strchr(buffer + 7, '<')) != NULL) {
109 char buffer2[1024], offset = 0;
110
187 - if (map_email(bob + 1, buffer, sizeof(buffer)))
111 + if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer)))
112 bob = buffer + strlen(buffer);
113 else {
114 offset = 8;
@@ -238,7 +162,7 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
162 die("Invalid commit buffer: %s",
163 sha1_to_hex(commit->object.sha1));
164
241 - if (map_email(bracket + 1, scratch,
165 + if (map_email(&mailmap, bracket + 1, scratch,
166 sizeof(scratch))) {
167 author = scratch;
168 authorlen = strlen(scratch);
@@ -360,7 +284,7 @@ int cmd_shortlog(int argc, const char **argv, const char *prefix)
284 die ("unrecognized argument: %s", argv[1]);
285
286 if (!access(".mailmap", R_OK))
363 - read_mailmap(".mailmap");
287 + read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
288
289 if (rev.pending.nr == 0) {
290 if (isatty(0))
mailmap.c new
+88
@@ -0,0 +1,88 @@
1 +#include "cache.h"
2 +#include "path-list.h"
3 +
4 +int read_mailmap(struct path_list *map, const char *filename, char **repo_abbrev)
5 +{
6 + char buffer[1024];
7 + FILE *f = fopen(filename, "r");
8 +
9 + if (f == NULL)
10 + return 1;
11 + while (fgets(buffer, sizeof(buffer), f) != NULL) {
12 + char *end_of_name, *left_bracket, *right_bracket;
13 + char *name, *email;
14 + int i;
15 + if (buffer[0] == '#') {
16 + static const char abbrev[] = "# repo-abbrev:";
17 + int abblen = sizeof(abbrev) - 1;
18 + int len = strlen(buffer);
19 +
20 + if (len && buffer[len - 1] == '\n')
21 + buffer[--len] = 0;
22 + if (!strncmp(buffer, abbrev, abblen)) {
23 + char *cp;
24 +
25 + if (repo_abbrev)
26 + free(*repo_abbrev);
27 + *repo_abbrev = xmalloc(len);
28 +
29 + for (cp = buffer + abblen; isspace(*cp); cp++)
30 + ; /* nothing */
31 + strcpy(*repo_abbrev, cp);
32 + }
33 + continue;
34 + }
35 + if ((left_bracket = strchr(buffer, '<')) == NULL)
36 + continue;
37 + if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
38 + continue;
39 + if (right_bracket == left_bracket + 1)
40 + continue;
41 + for (end_of_name = left_bracket; end_of_name != buffer
42 + && isspace(end_of_name[-1]); end_of_name--)
43 + /* keep on looking */
44 + if (end_of_name == buffer)
45 + continue;
46 + name = xmalloc(end_of_name - buffer + 1);
47 + strlcpy(name, buffer, end_of_name - buffer + 1);
48 + email = xmalloc(right_bracket - left_bracket);
49 + for (i = 0; i < right_bracket - left_bracket - 1; i++)
50 + email[i] = tolower(left_bracket[i + 1]);
51 + email[right_bracket - left_bracket - 1] = '\0';
52 + path_list_insert(email, map)->util = name;
53 + }
54 + fclose(f);
55 + return 0;
56 +}
57 +
58 +int map_email(struct path_list *map, const char *email, char *name, int maxlen)
59 +{
60 + char *p;
61 + struct path_list_item *item;
62 + char buf[1024], *mailbuf;
63 + int i;
64 +
65 + /* autocomplete common developers */
66 + p = strchr(email, '>');
67 + if (!p)
68 + return 0;
69 + if (p - email + 1 < sizeof(buf))
70 + mailbuf = buf;
71 + else
72 + mailbuf = xmalloc(p - email + 1);
73 +
74 + /* downcase the email address */
75 + for (i = 0; i < p - email; i++)
76 + mailbuf[i] = tolower(email[i]);
77 + mailbuf[i] = 0;
78 + item = path_list_lookup(mailbuf, map);
79 + if (mailbuf != buf)
80 + free(mailbuf);
81 + if (item != NULL) {
82 + const char *realname = (const char *)item->util;
83 + strncpy(name, realname, maxlen);
84 + return 1;
85 + }
86 + return 0;
87 +}
88 +
mailmap.h new
+7
@@ -0,0 +1,7 @@
1 +#ifndef MAILMAP_H
2 +#define MAILMAP_H
3 +
4 +int read_mailmap(struct path_list *map, const char *filename, char **repo_abbrev);
5 +int map_email(struct path_list *mailmap, const char *email, char *name, int maxlen);
6 +
7 +#endif