Created
February 7, 2023 16:06
-
-
Save educastellano/506bf454c880c6674217ac63f193e7e5 to your computer and use it in GitHub Desktop.
Script to log branches merged into main with squashed commits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Utility to log branches merged into main | |
* | |
* It finds branches from commit messages because of squashed commits (and so 'git branch --merged' doesn't work), | |
* so results might not be accurate, use it at your own risk. | |
*/ | |
import { exec as execCb } from 'child_process' | |
function exec (cmd) { | |
return new Promise((resolve, reject) => { | |
execCb(cmd, (error, stdout, stderr) => { | |
if (error) { | |
reject(stderr) | |
} else { | |
resolve(stdout) | |
} | |
}) | |
}) | |
} | |
async function run (logMerged = true) { | |
try { | |
const current = await exec('git rev-parse --abbrev-ref HEAD') | |
if (current.trim() !== 'main') { | |
console.log('\n 💁 You are not in "main" branch\n') | |
process.exit(1) | |
} | |
const output = await exec('git branch') | |
const branches = output | |
.split('\n') | |
.map(branch => branch.slice(2)) | |
.filter(branch => !!branch && branch !== 'main') | |
for (const branch of branches) { | |
const lastCommit = await exec(`git log -n 1 ${branch}`) | |
const lastCommitId = lastCommit.split('\n')[0].replace('commit ', '') | |
const lastCommitMessage = (await exec(`git log --format=%B -n 1 ${lastCommitId}`)).trim() | |
try { | |
await exec(`git log | grep "${lastCommitMessage.replace(/"/g, '\\"')}"`) | |
if (logMerged) { | |
console.log(lastCommitId.slice(0, 7), branch) | |
} | |
} catch (e) { | |
if (!logMerged) { | |
console.log('Not Merged:', lastCommitId.slice(0, 7), branch) | |
} | |
} | |
} | |
} catch (e) { | |
console.log('💥', e) | |
} | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment