Skip to content

Instantly share code, notes, and snippets.

@aztack
Last active March 10, 2024 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/7d5ca991447a09772aa136552abe2b21 to your computer and use it in GitHub Desktop.
Save aztack/7d5ca991447a09772aa136552abe2b21 to your computer and use it in GitHub Desktop.
Print git changes by file name
const { execSync } = require('child_process');
// Replace the array with the usernames of the developers whose commits you want to fetch
const developers = ['developer1', 'developer2', 'developer3'];
function getCommitsByDevelopers() {
const commits = [];
for (const developer of developers) {
const gitCommand = `git log --author="${developer}" --pretty=format:"%h"`;
const commitHashes = execSync(gitCommand).toString().trim().split('\n');
commits.push(...commitHashes);
}
return commits;
}
/*
' ' = unmodified
M = modified
A = added
D = deleted
R = renamed
C = copied
U = updated but unmerged
*/
function getFileChanges(commit) {
const gitCommand = `git show --name-status --pretty=format:"%ci: %h- %s - %an" ${commit}`;
const output = execSync(gitCommand).toString();
const lines = output.split('\n');
const commitMetadata = lines.shift(); // Get commit metadata (hash, message, date, author) from the first line
const changes = {};
const s = {
'A': '+',
'M': '*',
'D': '-',
}
for (const line of lines) {
if (!line) continue;
const [status, file] = line.split('\t');
if (!changes[file]) {
changes[file] = [];
}
const t = s[status] || status;
changes[file].push(`${t} ${commitMetadata.replace(' +0800', '')}`);
}
return changes;
}
function processData() {
const commits = getCommitsByDevelopers();
const allChanges = {};
for (const commit of commits) {
const changes = getFileChanges(commit);
for (const file in changes) {
if (!allChanges[file]) {
allChanges[file] = [];
}
allChanges[file].push(...changes[file]);
}
}
return allChanges;
}
const changes = processData();
// sort changes by key
const sortedChanges = {};
Object.keys(changes).sort().forEach(key => {
sortedChanges[key] = changes[key];
// sort commits
sortedChanges[key].sort((a, b) => {
return a.slice(1).localeCompare(b.slice(1));
}).reverse();
});
console.log(JSON.stringify(sortedChanges, null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment