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>
Jeff King committed
Dec 12, 2012 at 05:59 UTC
7c8ce308d383ce6888f69e39f0d32322600c2cc2
1 file changed
+40
-34
mailmap.c
+40
-34
@@ -129,44 +129,50 @@ static char *parse_name_and_email(char *buffer, char **name,
129
return (*right == '\0' ? NULL : right);
130
}
131
132
-static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
132
+static void read_mailmap_line(struct string_list *map, char *buffer,
133
+ char **repo_abbrev)
134
+{
135
+ char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
136
+ if (buffer[0] == '#') {
137
+ static const char abbrev[] = "# repo-abbrev:";
138
+ int abblen = sizeof(abbrev) - 1;
139
+ int len = strlen(buffer);
140
+
141
+ if (!repo_abbrev)
142
+ return;
143
+
144
+ if (len && buffer[len - 1] == '\n')
145
+ buffer[--len] = 0;
146
+ if (!strncmp(buffer, abbrev, abblen)) {
147
+ char *cp;
148
+
149
+ if (repo_abbrev)
150
+ free(*repo_abbrev);
151
+ *repo_abbrev = xmalloc(len);
152
+
153
+ for (cp = buffer + abblen; isspace(*cp); cp++)
154
+ ; /* nothing */
155
+ strcpy(*repo_abbrev, cp);
156
+ }
157
+ return;
158
+ }
159
+ if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
160
+ parse_name_and_email(name2, &name2, &email2, 1);
161
+
162
+ if (email1)
163
+ add_mapping(map, name1, email1, name2, email2);
164
+}
165
+
166
+static int read_mailmap_file(struct string_list *map, const char *filename,
167
+ char **repo_abbrev)
168
{
169
char buffer[1024];
170
FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
171
172
if (f == NULL)
173
return 1;
139
- while (fgets(buffer, sizeof(buffer), f) != NULL) {
140
- char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
141
- if (buffer[0] == '#') {
142
- static const char abbrev[] = "# repo-abbrev:";
143
- int abblen = sizeof(abbrev) - 1;
144
- int len = strlen(buffer);
145
-
146
- if (!repo_abbrev)
147
- continue;
148
-
149
- if (len && buffer[len - 1] == '\n')
150
- buffer[--len] = 0;
151
- if (!strncmp(buffer, abbrev, abblen)) {
152
- char *cp;
153
-
154
- if (repo_abbrev)
155
- free(*repo_abbrev);
156
- *repo_abbrev = xmalloc(len);
157
-
158
- for (cp = buffer + abblen; isspace(*cp); cp++)
159
- ; /* nothing */
160
- strcpy(*repo_abbrev, cp);
161
- }
162
- continue;
163
- }
164
- if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
165
- parse_name_and_email(name2, &name2, &email2, 1);
166
-
167
- if (email1)
168
- add_mapping(map, name1, email1, name2, email2);
169
- }
174
+ while (fgets(buffer, sizeof(buffer), f) != NULL)
175
+ read_mailmap_line(map, buffer, repo_abbrev);
176
fclose(f);
177
return 0;
178
}
@@ -175,8 +181,8 @@ int read_mailmap(struct string_list *map, char **repo_abbrev)
181
{
182
map->strdup_strings = 1;
183
/* each failure returns 1, so >1 means both calls failed */
178
- return read_single_mailmap(map, ".mailmap", repo_abbrev) +
179
- read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
184
+ return read_mailmap_file(map, ".mailmap", repo_abbrev) +
185
+ read_mailmap_file(map, git_mailmap_file, repo_abbrev) > 1;
186
}
187
188
void clear_mailmap(struct string_list *map)