Skip to content

Instantly share code, notes, and snippets.

@Kadajett
Created February 2, 2024 17:59
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 Kadajett/bb8522da736e5f060762146b5fa002cf to your computer and use it in GitHub Desktop.
Save Kadajett/bb8522da736e5f060762146b5fa002cf to your computer and use it in GitHub Desktop.
Track commits by team members
const { exec } = require("child_process");
const path = require("path");
// Replace this with the path to your Git repository
const repoPath = "../colony";
function parseGitLog(log, filterNames = []) {
const weeklyCommits = {};
const commitLines = log.split("\n");
commitLines.forEach((line) => {
if (line) {
const [week, author] = line.split("|"); // Split each line by '|'
if (!weeklyCommits[week]) {
weeklyCommits[week] = { totalCommits: 0, authors: {} };
}
if (!filterNames.length || filterNames.includes(author)) {
if (weeklyCommits[week].authors[author]) {
weeklyCommits[week].authors[author] += 1;
} else {
weeklyCommits[week].authors[author] = 1;
}
weeklyCommits[week].totalCommits += 1;
}
}
});
return weeklyCommits;
}
function displayCommitCounts(weeklyCommits) {
const weeks = Object.keys(weeklyCommits).sort(); // Ensure weeks are in order
const currentDate = new Date();
const currentYearWeek = `${currentDate.getFullYear()}-${String(
Math.floor(
(currentDate - new Date(currentDate.getFullYear(), 0, 1)) / 604800000
) + 1
).padStart(2, "0")}`;
weeks.forEach((week) => {
console.log(`Week ${week}:`);
const { totalCommits, authors } = weeklyCommits[week];
Object.entries(authors).forEach(([author, count]) => {
console.log(` ${author}: ${count} commit(s)`);
});
// Display the total commits for the week
console.log(` Total commits this week: ${totalCommits}`);
// Calculate and display average commits per day
const daysInWeek = week === currentYearWeek ? currentDate.getDay() || 7 : 7; // Use current day for the current week, 7 for past weeks
const avgCommitsPerDay = (totalCommits / daysInWeek).toFixed(2);
console.log(` Average commits per day: ${avgCommitsPerDay}\n`);
});
}
function listCommitCounts(filterNames = []) {
// Modified command to include week of the year in the output
const command = 'git log --pretty=format:"%ad|%an" --date=format:"%Y-%W"';
const options = { cwd: path.resolve(repoPath) };
exec(command, options, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
const weeklyCommits = parseGitLog(stdout, filterNames);
displayCommitCounts(weeklyCommits);
});
}
// Call this function with an array of names you want to filter by, or leave it empty to include all names
listCommitCounts(["Jeremy Stover"]); // Replace with actual names or leave empty []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment