-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (112 loc) · 3.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
export type ParsedCommit = {
sha: string;
authorName: string;
authorEmail: string;
date: string;
message: string;
diff: string;
};
export function parseGitPatch(patch: string): ParsedCommit[] {
const lines = patch.split("\n");
const commits: ParsedCommit[] = [];
let currentSha = "";
let currentAuthorName = "";
let currentAuthorEmail = "";
let currentDate = "";
let currentMessageLines: string[] = [];
let currentDiffLines: string[] = [];
let inMessageSection = false;
let inDiffSection = false;
let foundDiffStart = false; // To track when we've hit `diff --git`
const finalizeCommit = () => {
if (!currentSha) return; // No commit started yet
commits.push({
sha: currentSha,
authorName: currentAuthorName,
authorEmail: currentAuthorEmail,
date: currentDate,
message: currentMessageLines.join("\n").trim(),
diff:
currentDiffLines.join("\n") + (currentDiffLines.length > 0 ? "\n" : ""),
});
// Reset for next commit
currentSha = "";
currentAuthorName = "";
currentAuthorEmail = "";
currentDate = "";
currentMessageLines = [];
currentDiffLines = [];
inMessageSection = false;
inDiffSection = false;
foundDiffStart = false;
};
for (const line of lines) {
// Detect the start of a new commit
const fromMatch = line.match(/^From\s+([0-9a-f]{40})\s/);
if (fromMatch) {
finalizeCommit();
currentSha = fromMatch[1];
continue;
}
// Parse author line: From: Name <email>
if (line.startsWith("From: ")) {
const authorLine = line.slice("From: ".length).trim();
const emailMatch = authorLine.match(/<(.*)>/);
if (emailMatch) {
currentAuthorEmail = emailMatch[1];
currentAuthorName = authorLine.slice(0, authorLine.indexOf("<")).trim();
} else {
currentAuthorName = authorLine;
}
continue;
}
// Parse date line: Date: ...
if (line.startsWith("Date: ")) {
currentDate = line.slice("Date: ".length).trim();
continue;
}
// Parse subject line
if (line.startsWith("Subject: ")) {
let subject = line.slice("Subject: ".length).trim();
// Remove leading "[PATCH ...]" if present
subject = subject.replace(/^\[PATCH[^\]]*\]\s*/, "");
currentMessageLines.push(subject);
inMessageSection = true;
continue;
}
// Check if we are transitioning to diff section
if (inMessageSection && line.trim() === "---") {
inMessageSection = false;
inDiffSection = true;
continue;
}
// If we are in the message section, just append lines to message
if (inMessageSection) {
currentMessageLines.push(line);
continue;
}
// If we are in the diff section but haven't found `diff --git` yet
if (inDiffSection && !foundDiffStart) {
// Look for the start of the actual diff
if (line.startsWith("diff --git ")) {
foundDiffStart = true;
currentDiffLines.push(line);
}
// Ignore everything until we find `diff --git`
continue;
}
// If we are in diff section and already found `diff --git`
if (inDiffSection && foundDiffStart) {
// Stop capturing when we hit a line that, after trimming, is `--`
if (line.trim() === "--") {
inDiffSection = false;
foundDiffStart = false;
continue;
}
currentDiffLines.push(line);
continue;
}
}
finalizeCommit();
return commits;
}