Skip to content

Instantly share code, notes, and snippets.

@swyxio
Created November 1, 2022 06:10
Show Gist options
  • Save swyxio/89ef86324084e97ec6140ea124ce32aa to your computer and use it in GitHub Desktop.
Save swyxio/89ef86324084e97ec6140ea124ce32aa to your computer and use it in GitHub Desktop.
how to scrape folder start dates from git
const { exec } = require('child_process');
// exec(`
// git log --reverse -- /Users/swyx/Desktop/Work/airbyte/airbyte-integrations/connectors/destination-amazon-sqs | awk 'NR>1 {print last} {last=$0}; /^commit/ && ++c==2{exit}'
// `, (err, stdout, stderr) => {
// if (err) {
// // node couldn't execute the command
// return;
// }
// // the *entire* stdout and stderr (buffered)
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
// })
// stdout: commit 84b3bf55acff30ee0f571e4b1709b90f0278fdc5
// Author: Alasdair Brown <sdairs@users.noreply.github.com>
// Date: Thu Dec 9 22:21:51 2021 +0000
// :tada: Destination Amazon SQS: New connector (#7503)
// * initial commit, working sending single messages
const fs = require('fs');
const path = require('path');
// read names of all directories in the current directory
const dirs = fs.readdirSync('.', { withFileTypes: true }).filter(dirent => dirent.isDirectory()).map(dirent => dirent.name)
// strip all but the directory name
.map(name => name.split('/').pop())
const results = {}
// function that takes a path, exec git log command for that path and grab the date of the most recent comment output
function getMostRecentCommitDate(name) {
const { exec } = require('child_process');
let str = `
git log --reverse -- ${name} | awk 'NR>1 {print last} {last=$0}; /^commit/ && ++c==2{exit}'
`
console.log(str)
// exec the string but silence stdout and stderr
exec(str, { stdio: 'ignore' }, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// extract the date from the git log output
try {
const date = stdout.split('Date:')[1].split('+')[0].split('-')[0].trim()
// convert to javascript date
const jsDate = new Date(date)
results[name.split('/').pop()] = jsDate
// store results in a csv file
fs.writeFileSync('results.csv', JSON.stringify(results, null, 2))
} catch (err) {
console.log('error parsing name', name)
console.log('error parsing stdout', stdout)
console.log(err)
}
})
}
// loop through all directories and run the function
dirs.forEach(dir => {
getMostRecentCommitDate(path.join(__dirname, dir))
})
@swyxio
Copy link
Author

swyxio commented Feb 11, 2023

console.log('hi')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment