git-merged-branches
Version:
CLI tool to list all Git branches merged into a base branch with issue link formatting
9 lines • 3.24 kB
JavaScript
import e,{cwd as t}from"node:process";import{execSync as n}from"node:child_process";import{readFileSync as r}from"node:fs";import{join as i}from"node:path";function a(e,t){t instanceof Error?console.warn(`${e}: ${t.message}`):console.warn(`${e}: ${String(t)}`)}function o(e,t){return`${e} ${e===1?t[0]:t[1]}`}function s(){try{return n(`git rev-parse --is-inside-work-tree`,{stdio:`ignore`}),!0}catch{return!1}}function c(){try{return n(`git symbolic-ref --quiet --short HEAD`,{encoding:`utf-8`}).trim()===``}catch{return!0}}function l(e){try{return n(`git show-ref --verify --quiet refs/heads/${e}`),!0}catch{return!1}}function u(){return l(`main`)?`main`:l(`master`)?`master`:null}function d(e){let t=n(`git rev-parse ${e}`,{encoding:`utf-8`}).trim();return n(`git branch --merged ${e}`,{encoding:`utf-8`}).split(`
`).reduce((e,r)=>{let i=r.replace(`*`,``).trim();return!i||n(`git rev-parse ${i}`,{encoding:`utf-8`}).trim()===t?e:[...e,i]},[])}function f(e=`origin`){try{return n(`git ls-remote --heads ${e}`,{encoding:`utf-8`}).split(`
`).map(e=>e.split(` `)[1]).filter(e=>e?.startsWith(`refs/heads/`)).map(e=>e.replace(`refs/heads/`,``))}catch(t){return a(`Could not fetch remote branches from '${e}'`,t),[]}}function p(){try{let e=i(t(),`package.json`);return JSON.parse(r(e,`utf-8`))[`git-merged-branches`]||{}}catch(e){return a(`Could not read package.json`,e),{}}}function m(e){n(`git branch --delete ${e.join(` `)}`,{stdio:`inherit`})}function h(e){n(`git push origin --delete ${e.join(` `)}`,{stdio:`inherit`})}function g(e,t){try{console.info(`
Deleting branches locally…`),m(e),t.length&&(console.info(`
Deleting branches remotely…`),h(t))}catch(e){a(`Failed to delete branches`,e)}}function _(e){if(!e)return!1;try{let t=new URL(e);return t.protocol===`http:`||t.protocol===`https:`}catch{return!1}}function v(e,t,n){for(let r of n){let n=RegExp(`\\b${r}(\\d+)`,`i`),i=e.match(n);if(i)return`${e} <${t.replace(`{{prefix}}`,r).replace(`{{id}}`,i[1])}>`}return e}function y(e,{issueUrlFormat:t,issueUrlPrefix:n}){return!t||!n?e:_(t)?Array.isArray(n)?e.map(e=>v(e,t,n)):(console.warn(`'${n}' is not an array. Skipped formatting.`),e):(console.warn(`'${t}' is not a valid URL. Skipped formatting.`),e)}function b(e,t,n,r={}){if(!e.length)return console.info(`No branches merged into '${t}'.`);let i=o(e.length,[`branch`,`branches`]);console.info(`${i} merged into '${t}':`),console.info(y(e,n).join(`
`));let a=f(`origin`),s=e.filter(e=>a.includes(e));if(r.deleteBranches){g(e,s),console.info(`Branches deleted successfully.`);return}console.info(`\nUse --delete to delete ${i} automatically.`),console.info(`
Delete locally:`),console.info(` git branch --delete ${e.join(` `)}`),s.length&&(console.info(`
Delete remotely:`),console.info(` git push origin --delete ${s.join(` `)}`))}function x(){s()||(console.error(`Not a git repository.`),e.exit(1)),c()&&(console.error(`HEAD is detached (e.g., after checkout of a commit). Please switch to a branch.`),e.exit(1));let t=u();t||(console.error(`No 'master' or 'main' branch found.`),e.exit(1));try{let n=d(t),r=e.argv.includes(`--delete`);b(n,t,p(),{deleteBranches:r})}catch(t){a(`Error executing 'git-merged-branches' command`,t),e.exit(1)}}x();export{};