2 #include "string-list.h"
5 const char *git_mailmap_file
;
6 static int read_single_mailmap(struct string_list
*map
, const char *filename
, char **repo_abbrev
)
9 FILE *f
= (filename
== NULL
? NULL
: fopen(filename
, "r"));
13 while (fgets(buffer
, sizeof(buffer
), f
) != NULL
) {
14 char *end_of_name
, *left_bracket
, *right_bracket
;
17 if (buffer
[0] == '#') {
18 static const char abbrev
[] = "# repo-abbrev:";
19 int abblen
= sizeof(abbrev
) - 1;
20 int len
= strlen(buffer
);
25 if (len
&& buffer
[len
- 1] == '\n')
27 if (!strncmp(buffer
, abbrev
, abblen
)) {
32 *repo_abbrev
= xmalloc(len
);
34 for (cp
= buffer
+ abblen
; isspace(*cp
); cp
++)
36 strcpy(*repo_abbrev
, cp
);
40 if ((left_bracket
= strchr(buffer
, '<')) == NULL
)
42 if ((right_bracket
= strchr(left_bracket
+ 1, '>')) == NULL
)
44 if (right_bracket
== left_bracket
+ 1)
46 for (end_of_name
= left_bracket
;
47 end_of_name
!= buffer
&& isspace(end_of_name
[-1]);
49 ; /* keep on looking */
50 if (end_of_name
== buffer
)
52 name
= xmalloc(end_of_name
- buffer
+ 1);
53 strlcpy(name
, buffer
, end_of_name
- buffer
+ 1);
54 email
= xmalloc(right_bracket
- left_bracket
);
55 for (i
= 0; i
< right_bracket
- left_bracket
- 1; i
++)
56 email
[i
] = tolower(left_bracket
[i
+ 1]);
57 email
[right_bracket
- left_bracket
- 1] = '\0';
58 string_list_insert(email
, map
)->util
= name
;
64 int read_mailmap(struct string_list
*map
, char **repo_abbrev
)
66 /* each failure returns 1, so >1 means both calls failed */
67 return read_single_mailmap(map
, ".mailmap", repo_abbrev
) +
68 read_single_mailmap(map
, git_mailmap_file
, repo_abbrev
) > 1;
71 int map_email(struct string_list
*map
, const char *email
, char *name
, int maxlen
)
74 struct string_list_item
*item
;
75 char buf
[1024], *mailbuf
;
78 /* autocomplete common developers */
79 p
= strchr(email
, '>');
82 if (p
- email
+ 1 < sizeof(buf
))
85 mailbuf
= xmalloc(p
- email
+ 1);
87 /* downcase the email address */
88 for (i
= 0; i
< p
- email
; i
++)
89 mailbuf
[i
] = tolower(email
[i
]);
91 item
= string_list_lookup(mailbuf
, map
);
95 const char *realname
= (const char *)item
->util
;
96 strlcpy(name
, realname
, maxlen
);