|
| 1 | +import { getCommitMessageType } from '../engine/commit.js'; |
| 2 | +import type { CommitRaw, DifferenceStatistic } from "./types.js"; |
| 3 | + |
| 4 | +interface ParsedRefs { |
| 5 | + branches: string[]; |
| 6 | + tags: string[]; |
| 7 | +} |
| 8 | + |
| 9 | +interface MessageAndDiffStats { |
| 10 | + message: string; |
| 11 | + diffStats: DifferenceStatistic; |
| 12 | +} |
| 13 | + |
| 14 | +const EOL_REGEX = /\r?\n/; |
| 15 | +const COMMIT_SEPARATOR = new RegExp(`${EOL_REGEX.source}{4}`); |
| 16 | +const INDENTATION = " "; |
| 17 | + |
| 18 | +export function splitLogIntoCommits(log: string): string[] { |
| 19 | + return log.substring(2).split(COMMIT_SEPARATOR); |
| 20 | +} |
| 21 | + |
| 22 | +export function extractCommitData(commit: string) { |
| 23 | + const [ |
| 24 | + id, |
| 25 | + parents, |
| 26 | + refs, |
| 27 | + authorName, |
| 28 | + authorEmail, |
| 29 | + authorDate, |
| 30 | + committerName, |
| 31 | + committerEmail, |
| 32 | + committerDate, |
| 33 | + ...messageAndDiffStats |
| 34 | + ] = commit.split(EOL_REGEX); |
| 35 | + |
| 36 | + return { |
| 37 | + id, |
| 38 | + parents, |
| 39 | + refs, |
| 40 | + authorName, |
| 41 | + authorEmail, |
| 42 | + authorDate, |
| 43 | + committerName, |
| 44 | + committerEmail, |
| 45 | + committerDate, |
| 46 | + messageAndDiffStats, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +export function extractBranchesAndTags(refs: string): ParsedRefs { |
| 51 | + if (!refs) return { branches: [], tags: [] }; |
| 52 | + |
| 53 | + const refsArray = refs.replace(" -> ", ", ").split(", "); |
| 54 | + return refsArray.reduce<ParsedRefs>( |
| 55 | + (acc, ref) => { |
| 56 | + if (ref === "") return acc; |
| 57 | + if (ref.startsWith("tag: ")) { |
| 58 | + acc.tags.push(ref.replace("tag: ", "")); |
| 59 | + } else { |
| 60 | + acc.branches.push(ref); |
| 61 | + } |
| 62 | + return acc; |
| 63 | + }, |
| 64 | + { branches: [], tags: [] } |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +export function processDiffStatLine(line: string, diffStats: DifferenceStatistic) { |
| 69 | + const [insertions, deletions, path] = line.split("\t"); |
| 70 | + const numberedInsertions = insertions === "-" ? 0 : Number(insertions); |
| 71 | + const numberedDeletions = deletions === "-" ? 0 : Number(deletions); |
| 72 | + |
| 73 | + diffStats.totalInsertionCount += numberedInsertions; |
| 74 | + diffStats.totalDeletionCount += numberedDeletions; |
| 75 | + diffStats.fileDictionary[path] = { |
| 76 | + insertionCount: numberedInsertions, |
| 77 | + deletionCount: numberedDeletions, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +export function extractMessageAndDiffStats(messageAndDiffStats: string[]): MessageAndDiffStats { |
| 82 | + let messageSubject = ""; |
| 83 | + let messageBody = ""; |
| 84 | + const diffStats: DifferenceStatistic = { |
| 85 | + totalInsertionCount: 0, |
| 86 | + totalDeletionCount: 0, |
| 87 | + fileDictionary: {}, |
| 88 | + }; |
| 89 | + |
| 90 | + for (const [idx, line] of messageAndDiffStats.entries()) { |
| 91 | + if (idx === 0) { |
| 92 | + messageSubject = line; |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if (line.startsWith(INDENTATION)) { |
| 97 | + messageBody += idx === 1 ? line.trim() : `\n${line.trim()}`; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + if (line === "") continue; |
| 102 | + |
| 103 | + processDiffStatLine(line, diffStats); |
| 104 | + } |
| 105 | + |
| 106 | + const message = messageBody === "" ? messageSubject : `${messageSubject}\n${messageBody}`; |
| 107 | + return { message, diffStats }; |
| 108 | +} |
| 109 | + |
| 110 | +export function createCommitRaw( |
| 111 | + commitIdx: number, |
| 112 | + commitData: ReturnType<typeof extractCommitData>, |
| 113 | + parsedRefs: ParsedRefs, |
| 114 | + parsedMessage: ReturnType<typeof extractMessageAndDiffStats> |
| 115 | +): CommitRaw { |
| 116 | + const { id, parents, authorName, authorEmail, authorDate, committerName, committerEmail, committerDate } = commitData; |
| 117 | + const { branches, tags } = parsedRefs; |
| 118 | + const { message, diffStats } = parsedMessage; |
| 119 | + |
| 120 | + return { |
| 121 | + sequence: commitIdx, |
| 122 | + id, |
| 123 | + parents: parents.length === 0 ? [] : parents.split(" "), |
| 124 | + branches, |
| 125 | + tags, |
| 126 | + author: { |
| 127 | + name: authorName, |
| 128 | + email: authorEmail, |
| 129 | + }, |
| 130 | + authorDate: new Date(authorDate), |
| 131 | + committer: { |
| 132 | + name: committerName, |
| 133 | + email: committerEmail, |
| 134 | + }, |
| 135 | + committerDate: new Date(committerDate), |
| 136 | + message, |
| 137 | + commitMessageType: getCommitMessageType(message), |
| 138 | + differenceStatistic: diffStats, |
| 139 | + }; |
| 140 | +} |
| 141 | + |
| 142 | +export default function getCommitRaws(log: string): CommitRaw[] { |
| 143 | + if (!log) return []; |
| 144 | + |
| 145 | + const commits = splitLogIntoCommits(log); |
| 146 | + return commits.map((commit, idx) => { |
| 147 | + const commitData = extractCommitData(commit); |
| 148 | + const parsedRefs = extractBranchesAndTags(commitData.refs); |
| 149 | + const parsedMessage = extractMessageAndDiffStats(commitData.messageAndDiffStats); |
| 150 | + return createCommitRaw(idx, commitData, parsedRefs, parsedMessage); |
| 151 | + }); |
| 152 | +} |
0 commit comments