Skip to content

Instantly share code, notes, and snippets.

@itaditya
Last active November 2, 2023 09:27
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 itaditya/137567d404de08065b6895dd88cfb19b to your computer and use it in GitHub Desktop.
Save itaditya/137567d404de08065b6895dd88cfb19b to your computer and use it in GitHub Desktop.
get authors in a file from git
async function getAuthors(
filePath: string,
startLine: number,
endLine: number,
): Promise<{ author: string; email: string; commitTime: string }[]> {
const { stdout } = await execAsync(
`git log -s --max-count 10 --author-date-order --pretty='format: { "author": "%an", "email": "%aE", "committedAt":"%aI" }' -L ${startLine},${endLine}:${filePath}`,
);
const authors = stdout.split('\n').map((line) => {
try {
return JSON.parse(line);
} catch (err) {
console.error(
'Failed to parse output of git log when getting authors for file ',
filePath,
);
throw err;
}
});
const uniqueAuthors = [...new Map(authors.map((item) => [item.author, item])).values()];
return uniqueAuthors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment