Skip to content

Commit

Permalink
fix(git): print faulty line on parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Dec 18, 2024
1 parent d733b41 commit 9a8781f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
30 changes: 17 additions & 13 deletions git/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ export function log(sha: string, { stdout = "", ...options } = {} as LogOptions)
// Parse entries
let entries = []
for (const line of stdout.trim().split("\n")) {
const { sha, author, time, summary } = line.match(/^<<(?<sha>.{40})>> <<(?<time>\d+)>> <<(?<author>.*)>> (?<summary>.*)$/)!.groups!
const match = summary.match(/^(?<type>[^\(\):]+)(?:\((?<scopes>[^\(\):]+)\))?(?<breaking>!?):\s+(?<subject>[\s\S]*)/)?.groups
entries.push({
sha,
author,
time: Number(time),
conventional: !!match,
type: match?.type ?? null,
scopes: match?.scopes?.split(",").map((scope) => scope.trim()) ?? [],
breaking: match ? !!match?.breaking : null,
subject: match?.subject ?? summary,
summary,
})
try {
const { sha, author, time, summary } = line.match(/^<<(?<sha>.{40})>> <<(?<time>\d+)>> <<(?<author>.*)>> (?<summary>.*)$/)!.groups!
const match = summary.match(/^(?<type>[^\(\):]+)(?:\((?<scopes>[^\(\):]+)\))?(?<breaking>!?):\s+(?<subject>[\s\S]*)/)?.groups
entries.push({
sha,
author,
time: Number(time),
conventional: !!match,
type: match?.type ?? null,
scopes: match?.scopes?.split(",").map((scope) => scope.trim()) ?? [],
breaking: match ? !!match?.breaking : null,
subject: match?.subject ?? summary,
summary,
})
} catch (error) {
throw new TypeError(`Failed to parse git log entry: "${line}" (${error})`)
}
}
// Filter entries
if (options?.filter?.conventional) {
Expand Down
4 changes: 4 additions & 0 deletions git/log_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ function format(entries: LogEntry[]) {
test("`log()` parses `git log --pretty=<<%H>> <<%at>> <<%an>> %s`", () => {
expect(log("", { stdout: format(expected) })).toEqual(expected)
})

test("`log()` throws on parsing error", () => {
expect(() => log("", { stdout: "<garbage>" })).toThrow(TypeError, /Failed to parse git log entry/)
})

0 comments on commit 9a8781f

Please sign in to comment.