Skip to content

Commit

Permalink
misc: add files per commit for git-log-json.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
revam committed Sep 10, 2024
1 parent dd54dfe commit 20acd6b
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion .github/workflows/git-log-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ for (const [placeholder, name] of Object.entries(Placeholders)) {
}
}

// Add file-level changes to each commit
for (const commitId of commitOrder) {
const fileStatusOutput = execSync(`git diff --name-status ${commitId}^ ${commitId}`).toString();
const lineChangesOutput = execSync(`git diff --numstat ${commitId}^ ${commitId}`).toString();

const files = [];
const fileStatusLines = fileStatusOutput.split(/\r\n|\r|\n/g).filter(a => a);
const lineChangesLines = lineChangesOutput.split(/\r\n|\r|\n/g).filter(a => a);

for (const [index, line] of fileStatusLines.entries()) {
const [rawStatus, path] = line.split(/\t/);
const status = rawStatus === "M" ?
"modified"
: rawStatus === "A" ?
"added"
: rawStatus === "D" ?
"deleted"
: rawStatus === "R" ?
"renamed"
: "untracked";
const lineChangeParts = lineChangesLines[index].split(/\t/);
const addedLines = parseInt(lineChangeParts[0] || "0", 10);
const removedLines = parseInt(lineChangeParts[1] || "0", 10);

files.push({
path,
status,
addedLines,
removedLines,
});
}

commits[commitId].files = files;
}

// Trim trailing newlines from all values in the commits object
for (const commit of Object.values(commits)) {
for (const key in commit) {
Expand All @@ -81,7 +116,7 @@ for (const commit of Object.values(commits)) {
// Convert commits object to a list of values
const commitsList = commitOrder.reverse()
.map((commitId) => commits[commitId])
.map(({ commit, parents, tree, subject, body, author_name, author_email, author_date, committer_name, committer_email, committer_date }) => ({
.map(({ commit, parents, tree, subject, body, author_name, author_email, author_date, committer_name, committer_email, committer_date, files }) => ({
commit,
parents,
tree,
Expand All @@ -108,6 +143,7 @@ const commitsList = commitOrder.reverse()
date: new Date(committer_date).toISOString(),
timeZone: committer_date.substring(19) === "Z" ? "+00:00" : committer_date.substring(19),
},
files,
}))
.map((commit) => ({
...commit,
Expand Down

0 comments on commit 20acd6b

Please sign in to comment.