summaryrefslogtreecommitdiff
path: root/mailmap.c
blob: d006dad673169dfc727621112aea1ff5a31fb61a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "cache.h"
#include "string-list.h"
#include "mailmap.h"

const char *git_mailmap_file;
static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
{
	char buffer[1024];
	FILE *f = (filename == NULL ? NULL : 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 (!repo_abbrev)
				continue;

			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';
		string_list_insert(email, map)->util = name;
	}
	fclose(f);
	return 0;
}

int read_mailmap(struct string_list *map, char **repo_abbrev)
{
	/* each failure returns 1, so >1 means both calls failed */
	return read_single_mailmap(map, ".mailmap", repo_abbrev) +
	       read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
}

int map_email(struct string_list *map, const char *email, char *name, int maxlen)
{
	char *p;
	struct string_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 = string_list_lookup(mailbuf, map);
	if (mailbuf != buf)
		free(mailbuf);
	if (item != NULL) {
		const char *realname = (const char *)item->util;
		strlcpy(name, realname, maxlen);
		return 1;
	}
	return 0;
}