зеркало из https://github.com/microsoft/git.git
mailmap: refactor mailmap parsing for non-file sources
The read_single_mailmap function opens a mailmap file and parses each line. In preparation for having non-file mailmaps, let's pull out the line-parsing logic into its own function (read_mailmap_line), and rename the file-parsing function to match (read_mailmap_file). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Родитель
75ed918bda
Коммит
7c8ce308d3
74
mailmap.c
74
mailmap.c
|
@ -129,44 +129,50 @@ static char *parse_name_and_email(char *buffer, char **name,
|
||||||
return (*right == '\0' ? NULL : right);
|
return (*right == '\0' ? NULL : right);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
|
static void read_mailmap_line(struct string_list *map, char *buffer,
|
||||||
|
char **repo_abbrev)
|
||||||
|
{
|
||||||
|
char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
|
||||||
|
if (buffer[0] == '#') {
|
||||||
|
static const char abbrev[] = "# repo-abbrev:";
|
||||||
|
int abblen = sizeof(abbrev) - 1;
|
||||||
|
int len = strlen(buffer);
|
||||||
|
|
||||||
|
if (!repo_abbrev)
|
||||||
|
return;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
|
||||||
|
parse_name_and_email(name2, &name2, &email2, 1);
|
||||||
|
|
||||||
|
if (email1)
|
||||||
|
add_mapping(map, name1, email1, name2, email2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int read_mailmap_file(struct string_list *map, const char *filename,
|
||||||
|
char **repo_abbrev)
|
||||||
{
|
{
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
|
FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
|
||||||
|
|
||||||
if (f == NULL)
|
if (f == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
while (fgets(buffer, sizeof(buffer), f) != NULL) {
|
while (fgets(buffer, sizeof(buffer), f) != NULL)
|
||||||
char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
|
read_mailmap_line(map, buffer, repo_abbrev);
|
||||||
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 ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
|
|
||||||
parse_name_and_email(name2, &name2, &email2, 1);
|
|
||||||
|
|
||||||
if (email1)
|
|
||||||
add_mapping(map, name1, email1, name2, email2);
|
|
||||||
}
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -175,8 +181,8 @@ int read_mailmap(struct string_list *map, char **repo_abbrev)
|
||||||
{
|
{
|
||||||
map->strdup_strings = 1;
|
map->strdup_strings = 1;
|
||||||
/* each failure returns 1, so >1 means both calls failed */
|
/* each failure returns 1, so >1 means both calls failed */
|
||||||
return read_single_mailmap(map, ".mailmap", repo_abbrev) +
|
return read_mailmap_file(map, ".mailmap", repo_abbrev) +
|
||||||
read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
|
read_mailmap_file(map, git_mailmap_file, repo_abbrev) > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear_mailmap(struct string_list *map)
|
void clear_mailmap(struct string_list *map)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче