Skip to content

Commit 46b364a

Browse files
Trotttargos
authored andcommitted
tools: consolidate update-authors.js logic
Use a single regex and fewer logical branches in the code. PR-URL: #41255 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent fe21607 commit 46b364a

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

tools/update-authors.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,13 @@ const mailmap = new CaseIndifferentMap();
3939
line = line.trim();
4040
if (line.startsWith('#') || line === '') continue;
4141

42-
let match;
43-
// Replaced Name <[email protected]>
44-
if (match = line.match(/^([^<]+)\s+(<[^>]+>)$/)) {
45-
mailmap.set(match[2].toLowerCase(), {
46-
author: match[1], email: match[2]
47-
});
48-
49-
} else if (match = line.match(/^<([^>]+)>\s+(<[^>]+>)$/)) {
50-
mailmap.set(match[2].toLowerCase(), { email: match[1] });
51-
52-
} else if (match = line.match(/^([^<]+)\s+(<[^>]+>)\s+(<[^>]+>)$/)) {
53-
mailmap.set(match[3].toLowerCase(), {
54-
author: match[1], email: match[2]
55-
});
56-
// Replaced Name <[email protected]> Original Name <[email protected]>
57-
} else if (match =
58-
line.match(/^([^<]+)\s+(<[^>]+>)\s+([^<]+)\s+(<[^>]+>)$/)) {
59-
mailmap.set(match[3] + '\0' + match[4].toLowerCase(), {
60-
author: match[1], email: match[2]
42+
const match = line.match(/^(?:([^<]+)\s+)?(?:(<[^>]+>)\s+)?(?:([^<]+)\s+)?(<[^>]+>)$/);
43+
if (match) {
44+
const [, replaceName, replaceEmail, originalName, originalEmail] = match;
45+
const key = originalName ? `${originalName}\0${originalEmail.toLocaleLowerCase()}` : originalEmail.toLowerCase();
46+
mailmap.set(key, {
47+
author: replaceName || originalName,
48+
email: replaceEmail || originalEmail,
6149
});
6250
} else {
6351
console.warn('Unknown .mailmap format:', line);
@@ -73,8 +61,8 @@ const previousAuthors = new CaseIndifferentMap();
7361
line = line.trim();
7462
if (line.startsWith('#') || line === '') continue;
7563

76-
let match;
77-
if (match = line.match(/^([^<]+)\s+(<[^>]+>)$/)) {
64+
const match = line.match(/^([^<]+)\s+(<[^>]+>)$/);
65+
if (match) {
7866
const name = match[1];
7967
const email = match[2];
8068
if (previousAuthors.has(name)) {

0 commit comments

Comments
 (0)