smart-git-commit
Version:
AI-powered smart commit message generator for Git diffs.
77 lines (66 loc) • 1.71 kB
JavaScript
export default function parseDiff(diff) {
const fileChanges = [];
const lines = diff.split("\n");
let currentFile = null;
let changeType = null;
let additions = [];
let deletions = [];
for (let line of lines) {
// Detect New File Block
if (line.startsWith("diff --git")) {
if (currentFile) {
fileChanges.push({
file: currentFile,
changeType,
additions,
deletions,
});
}
const match = line.match(/a\/(.+?)\s/);
currentFile = match ? match[1] : "unknown";
changeType = "modified"; // Default changeType
additions = [];
deletions = [];
}
// Detect Added File
if (line.startsWith("new file mode")) {
changeType = "added";
}
// Detect Deleted File
if (line.startsWith("deleted file mode")) {
changeType = "deleted";
}
// Detect Renamed File (look for "rename from" line)
if (line.startsWith("rename from")) {
const renamedFrom = line.split("rename from ")[1];
currentFile = renamedFrom;
changeType = "renamed";
}
// Added Lines (skip file header lines)
if (
line.startsWith("+") &&
!line.startsWith("+++") &&
changeType !== "deleted"
) {
additions.push(line.slice(1).trim());
}
// Deleted Lines (skip file header lines)
if (
line.startsWith("-") &&
!line.startsWith("---") &&
changeType !== "added"
) {
deletions.push(line.slice(1).trim());
}
}
// Push last file if exists
if (currentFile) {
fileChanges.push({
file: currentFile,
changeType,
additions,
deletions,
});
}
return fileChanges;
}