Skip to content

Commit 258cc97

Browse files
fix: handle git users without names (#7)
Sometimes a user in git may specify an e-mail address but no name.
1 parent 65e608e commit 258cc97

3 files changed

Lines changed: 38 additions & 6 deletions

File tree

packages/objects/src/decodeObject.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,15 @@ function decodeTag(body: Uint8Array): TagObject {
179179
};
180180
}
181181

182-
function decodePerson(string: string) {
183-
const match = string.match(/^([^<]*) <([^>]*)> ([^ ]*) (.*)$/);
182+
function decodePerson(string: string): Person {
183+
const match = string.match(/^(?:([^<]*) )?<([^>]*)> ([^ ]*) (.*)$/);
184184
if (!match) throw new Error('Improperly formatted person string');
185185
return {
186-
name: match[1],
187-
email: match[2],
186+
name: match[1] ?? ``,
187+
email: match[2] ?? ``,
188188
date: {
189189
seconds: parseInt(match[3], 10),
190190
offset: (parseInt(match[4], 10) / 100) * -60,
191191
},
192-
} as Person;
192+
};
193193
}

packages/objects/src/encodeObject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ export function encodeCommit(body: CommitBody) {
8686
function formatPerson(person: Person) {
8787
return (
8888
safe(person.name) +
89-
' <' +
89+
(safe(person.name) ? ' ' : '') +
90+
'<' +
9091
safe(person.email) +
9192
'> ' +
9293
formatDate(person.date)

packages/objects/src/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ test(
6969
),
7070
);
7171

72+
test(
73+
'save and load commit with empty person name',
74+
testEncoding(
75+
{
76+
type: Type.commit,
77+
body: {
78+
tree: 'df2b8fc99e1c1d4dbc0a854d9f72157f1d6ea078',
79+
parents: [],
80+
author: {
81+
name: '',
82+
email: 'me@mariusgundersen.net',
83+
date: {
84+
seconds: 1500840368,
85+
offset: -2 * 60,
86+
},
87+
},
88+
committer: {
89+
name: 'Marius Gundersen',
90+
email: 'me@mariusgundersen.net',
91+
date: {
92+
seconds: 1500840368,
93+
offset: -2 * 60,
94+
},
95+
},
96+
message: 'test\n',
97+
},
98+
},
99+
'92f15c00a43ca7bd64a94bcbee32a400f783351a',
100+
),
101+
);
102+
72103
test(
73104
'save and load tag',
74105
testEncoding(

0 commit comments

Comments
 (0)