Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@armit/git

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@armit/git - npm Package Compare versions

Comparing version
0.0.36
to
0.0.37
+8
-0
CHANGELOG.md
# @armit/git
## 0.0.37
### Patch Changes
- [`e0520b3`](https://github.com/armitjs/armit/commit/e0520b3853f23f390c26752cb19395ae692ac448) Thanks [@tianyingchun](https://github.com/tianyingchun)! - bump version
- [`fc8398e`](https://github.com/armitjs/armit/commit/fc8398e49ab16c42ab60fc04eb8323c343f33886) Thanks [@tianyingchun](https://github.com/tianyingchun)! - Add `hasChangedFilePendingToCommit` on git module
## 0.0.36

@@ -4,0 +12,0 @@

+16
-1

@@ -54,2 +54,17 @@ /**

export { getAllCommitsBetween, getBranchesBetweenCommits, getCommitIdOfBranch, getDiffFiles, getFileCommitStatus, getLastCommitHash, gitBranchName };
/**
* Check if the filepath has been commited.
* ====
* Note: The -s option stands for "short" and is used to display the output in a compact format.
* - ??: Untracked files (new files that are not yet staged)
* - A: New files that are staged and ready to be committed
* - M: Modified files (changes made to existing files)
* - D: Deleted files (files that have been removed from the repository)
* - R: Renamed files
* - C: Files with copy and rename changes
* - U: Files with merge conflicts
* @returns Return `true` indicates the has changed `files` waiting to be commit.
*/
declare function hasChangedFilePendingToCommit(): Promise<boolean>;
export { getAllCommitsBetween, getBranchesBetweenCommits, getCommitIdOfBranch, getDiffFiles, getFileCommitStatus, getLastCommitHash, gitBranchName, hasChangedFilePendingToCommit };
+2
-2
import { exec } from 'node:child_process';
var m=(r="HEAD",n=!0)=>new Promise(t=>{exec(`git rev-parse ${n?"--short ":""}${r}`,(e,i)=>{if(e)return t(null);t(i.replace(/\r\n|\n|\r/g,""));});});function b(){return new Promise(r=>{exec("git log --pretty=format:'%h' -n 1",(n,t)=>{if(n)return r(null);r(t.replace(/'/g,""));});})}function A(){return new Promise((r,n)=>{exec("git branch | sed -n '/\\* /s///p'",(t,e)=>{if(t)return n(t);r(e.replace(/\r\n|\n|\r/g,""));});})}function q(r){return new Promise((n,t)=>{exec(`git ls-files ${r}`,(e,i)=>{if(e)return t(e);n(i.replace(/\r\n|\n|\r/g,"").length>0);});})}function S(r,n){let t=r&&n?`${n}..${r}`:r;return new Promise(e=>{exec(`git --no-pager diff --name-only ${t}`,(i,s)=>{if(i)return e([]);e(s.split(/[\r\n]/).filter(String));});})}var c=(r,n,t=!0)=>{let e=n&&r?`${n}..${r}`:r;return new Promise(i=>{exec(`git --no-pager log ${t?"-r":""} --oneline ${e} | cut -d " " -f1`,(s,o)=>{if(s)return i([]);i(o.split(/[\r\n|]/).filter(String));});})};function g(r){function n(t,e,i){return i.indexOf(t)===e}return r.filter(n)}var $=(r,n=!0)=>new Promise(t=>{exec(`git --no-pager branch ${n?"-r":""} --contains ${r}`,(e,i)=>{if(e)return t([]);let s=i.split(/[\r\n]/).filter(o=>!~o.toUpperCase().indexOf("(HEAD")&&!!o.trim()).map(o=>o.trim());t(s);});}),z=async(r,n,t=!0)=>{let e=[],i=await c(r,n,t);for(let o of i){let f=await $(o,t);e.push(...f);}let s=[];for(let o of e){let f=await m(o,!0);f&&i.includes(f)&&s.push(o);}return g(s)};
var m=(r="HEAD",n=!0)=>new Promise(t=>{exec(`git rev-parse ${n?"--short ":""}${r}`,(e,i)=>{if(e)return t(null);t(i.replace(/\r\n|\n|\r/g,""));});});function b(){return new Promise(r=>{exec("git log --pretty=format:'%h' -n 1",(n,t)=>{if(n)return r(null);r(t.replace(/'/g,""));});})}function O(){return new Promise((r,n)=>{exec("git branch | sed -n '/\\* /s///p'",(t,e)=>{if(t)return n(t);r(e.replace(/\r\n|\n|\r/g,""));});})}function q(r){return new Promise((n,t)=>{exec(`git ls-files ${r}`,(e,i)=>{if(e)return t(e);n(i.replace(/\r\n|\n|\r/g,"").length>0);});})}function F(r,n){let t=r&&n?`${n}..${r}`:r;return new Promise(e=>{exec(`git --no-pager diff --name-only ${t}`,(i,s)=>{if(i)return e([]);e(s.split(/[\r\n]/).filter(String));});})}var c=(r,n,t=!0)=>{let e=n&&r?`${n}..${r}`:r;return new Promise(i=>{exec(`git --no-pager log ${t?"-r":""} --oneline ${e} | cut -d " " -f1`,(s,o)=>{if(s)return i([]);i(o.split(/[\r\n|]/).filter(String));});})};function g(r){function n(t,e,i){return i.indexOf(t)===e}return r.filter(n)}var d=(r,n=!0)=>new Promise(t=>{exec(`git --no-pager branch ${n?"-r":""} --contains ${r}`,(e,i)=>{if(e)return t([]);let s=i.split(/[\r\n]/).filter(o=>!~o.toUpperCase().indexOf("(HEAD")&&!!o.trim()).map(o=>o.trim());t(s);});}),G=async(r,n,t=!0)=>{let e=[],i=await c(r,n,t);for(let o of i){let f=await d(o,t);e.push(...f);}let s=[];for(let o of e){let f=await m(o,!0);f&&i.includes(f)&&s.push(o);}return g(s)};function M(){return new Promise((r,n)=>{exec("git status -s",(t,e)=>{if(t)return n(t);r(e.replace(/\r\n|\n|\r/g,"").length>0);});})}
export { c as getAllCommitsBetween, z as getBranchesBetweenCommits, m as getCommitIdOfBranch, S as getDiffFiles, q as getFileCommitStatus, b as getLastCommitHash, A as gitBranchName };
export { c as getAllCommitsBetween, G as getBranchesBetweenCommits, m as getCommitIdOfBranch, F as getDiffFiles, q as getFileCommitStatus, b as getLastCommitHash, O as gitBranchName, M as hasChangedFilePendingToCommit };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"sources":["../src/get-commit-id-of-branch.ts","../src/get-last-commit-hash.ts","../src/get-branch-name.ts","../src/get-file-commit-status.ts","../src/get-diff-files.ts","../src/get-all-commits-between.ts","../src/get-branches-between-commits.ts","../src/array-unique.ts"],"names":["exec","getCommitIdOfBranch","branchName","short","resolve","err","out","getLastCommitHash","gitBranchName","reject","getFileCommitStatus","fileName","getDiffFiles","earlyCommit","lastCommit","diff","getAllCommitsBetween","remote","arrayUnique","arr","onlyUnique","value","index","self","getBranchesContainCommitHash","commit","branches","branch","s","getBranchesBetweenCommits","allBranches","allCommits","finalBranches","branchCommitHash"],"mappings":"AAAA,OAAS,QAAAA,MAAY,qBASd,IAAMC,EAAsB,CAACC,EAAa,OAAQC,EAAQ,KACxD,IAAI,QAAwBC,GAAY,CAC7CJ,EACE,iBAAiBG,EAAQ,WAAa,EAAE,GAAGD,CAAU,GACrD,CAACG,EAAKC,IAAQ,CACZ,GAAID,EACF,OAAOD,EAAQ,IAAI,EAErBA,EAAQE,EAAI,QAAQ,cAAe,EAAE,CAAC,CACxC,CACF,CACF,CAAC,ECpBH,OAAS,QAAAN,MAAY,qBAEd,SAASO,GAAoB,CAClC,OAAO,IAAI,QAAwBH,GAAY,CAC7CJ,EAAK,oCAAqC,CAACK,EAAKC,IAAQ,CACtD,GAAID,EACF,OAAOD,EAAQ,IAAI,EAErBA,EAAQE,EAAI,QAAQ,KAAM,EAAE,CAAC,CAC/B,CAAC,CACH,CAAC,CACH,CCXA,OAAS,QAAAN,MAAY,qBAEd,SAASQ,GAAgB,CAC9B,OAAO,IAAI,QAAgB,CAACJ,EAASK,IAAW,CAC9CT,EAAK,oCAAqC,CAACK,EAAKC,IAAQ,CACtD,GAAID,EACF,OAAOI,EAAOJ,CAAG,EAEnBD,EAAQE,EAAI,QAAQ,cAAe,EAAE,CAAC,CACxC,CAAC,CACH,CAAC,CACH,CCXA,OAAS,QAAAN,MAAY,qBAOd,SAASU,EAAoBC,EAAU,CAC5C,OAAO,IAAI,QAAiB,CAACP,EAASK,IAAW,CAC/CT,EAAK,gBAAgBW,CAAQ,GAAI,CAACN,EAAKC,IAAQ,CAC7C,GAAID,EACF,OAAOI,EAAOJ,CAAG,EAEnBD,EAAQE,EAAI,QAAQ,cAAe,EAAE,EAAE,OAAS,CAAC,CACnD,CAAC,CACH,CAAC,CACH,CChBA,OAAS,QAAAN,MAAY,qBAiBd,SAASY,EAAaC,EAAqBC,EAAqB,CACrE,IA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B,EAAQsB,CAAQ,CAClB,CACF,CACF,CAAC,EAUUG,EAA4B,MACvCf,EACAD,EACAI,EAAS,KACN,CACH,IAAMa,EAAwB,CAAC,EACzBC,EAAa,MAAMf,EACvBF,EACAD,EACAI,CACF,EAEA,QAAWQ,KAAUM,EAAY,CAE/B,IAAML,EAAW,MAAMF,EAA6BC,EAAQR,CAAM,EAClEa,EAAY,KAAK,GAAGJ,CAAQ,CAC9B,CAEA,IAAMM,EAA0B,CAAC,EAEjC,QAAWL,KAAUG,EAAa,CAChC,IAAMG,EAAmB,MAAMhC,EAAoB0B,EAAQ,EAAI,EAC3DM,GAAoBF,EAAW,SAASE,CAAgB,GAC1DD,EAAc,KAAKL,CAAM,CAE7B,CAEA,OAAOT,EAAYc,CAAa,CAClC","sourcesContent":["import { exec } from 'node:child_process';\n\n/**\n * https://stackoverflow.com/questions/15677439/command-to-get-latest-git-commit-hash-from-a-branch\n * Finding out the latest commit hash locally, NOTE: no `-r(remote)` argument if need to `remote` using `origin/xxx`\n * @param branchName e.g. `main`, `workspace`,....\n * @param short `ba39827` instead `ba3982746fafc8b1c37fce7692cb28b01044bb5f`\n * @returns The branch commit hash.\n */\nexport const getCommitIdOfBranch = (branchName = 'HEAD', short = true) => {\n return new Promise<string | null>((resolve) => {\n exec(\n `git rev-parse ${short ? '--short ' : ''}${branchName}`,\n (err, out) => {\n if (err) {\n return resolve(null);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, ''));\n }\n );\n });\n};\n","import { exec } from 'node:child_process';\n\nexport function getLastCommitHash() {\n return new Promise<string | null>((resolve) => {\n exec(`git log --pretty=format:'%h' -n 1`, (err, out) => {\n if (err) {\n return resolve(null);\n }\n resolve(out.replace(/'/g, ''));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\nexport function gitBranchName() {\n return new Promise<string>((resolve, reject) => {\n exec(`git branch | sed -n '/\\\\* /s///p'`, (err, out) => {\n if (err) {\n return reject(err);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, ''));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * Check if the filepath has been commited.\n * @param fileName The file name\n * @returns Return true indicates the files has been commited.\n */\nexport function getFileCommitStatus(fileName) {\n return new Promise<boolean>((resolve, reject) => {\n exec(`git ls-files ${fileName}`, (err, out) => {\n if (err) {\n return reject(err);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, '').length > 0);\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgtltcommitgt--ltpathgt82308203\n * Viewing the changes between two arbitrary <commit>, NOTE: no `-r(remote)` argument if need to `remote` using `commitHash` of `origin/xxx`\n * If <commit> on one side is omitted, it will have the same effect as using HEAD instead.\n * @example\n * ```shell\n * git --no-pager diff --name-only 2b2ccff..50164ec\n * git --no-pager diff --name-only 2b2ccff 50164ec\n * git --no-pager diff --name-only 1381f5\n * ```\n * @param earlyCommit The diff based earlier commit hash\n * @param lastCommit If is omitted, it will have the same effect as using HEAD instead.\n * @returns Return files between two commit hash\n */\n\nexport function getDiffFiles(earlyCommit: string, lastCommit?: string) {\n const diff =\n earlyCommit && lastCommit ? `${lastCommit}..${earlyCommit}` : earlyCommit;\n\n return new Promise<string[]>((resolve) => {\n exec(`git --no-pager diff --name-only ${diff}`, (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n resolve(out.split(/[\\r\\n]/).filter(String));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * This will list all commits that contain any of the commits between earlyCommit and lastCommit\n * @param lastCommit The lasted commit `HEAD`\n * @param earlyCommit The early commit `master`\n * @returns All commits\n */\nexport const getAllCommitsBetween = (\n lastCommit: string,\n earlyCommit: string,\n remote = true\n) => {\n // NOTE: The order is from `${earlyCommit} to ${lastComit}`\n const diff =\n earlyCommit && lastCommit ? `${earlyCommit}..${lastCommit}` : lastCommit;\n\n // if only `lastCommit` provider, will list all commits.\n return new Promise<string[]>((resolve) => {\n exec(\n `git --no-pager log ${\n remote ? '-r' : ''\n } --oneline ${diff} | cut -d \" \" -f1`,\n (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n resolve(out.split(/[\\r\\n|]/).filter(String));\n }\n );\n });\n};\n","import { exec } from 'node:child_process';\nimport { arrayUnique } from './array-unique.js';\nimport { getAllCommitsBetween } from './get-all-commits-between.js';\nimport { getCommitIdOfBranch } from './get-commit-id-of-branch.js';\n\nconst getBranchesContainCommitHash = (commit: string, remote = true) => {\n return new Promise<string[]>((resolve) => {\n // This command will list all the branch names that contain the specified commit ID and output it to the terminal\n // Note that other branch nodes checkout based on the current branch will also contain this commitId.\n exec(\n `git --no-pager branch ${remote ? '-r' : ''} --contains ${commit}`,\n (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n const branches = out\n .split(/[\\r\\n]/)\n .filter((branch) => {\n // Remove `* (HEAD detached at b121521)`\n return !~branch.toUpperCase().indexOf('(HEAD') && !!branch.trim();\n })\n .map((s) => s.trim());\n\n resolve(branches);\n }\n );\n });\n};\n\n/**\n * This will list all branches that contain any of the commits between commit1 and commit2\n * @param lastCommit if not will look up to first commit `HEAD`\n * @param earlyCommit The earlier commit `master`\n * @param remote The remote-tracking\n * @returns All commits\n */\nexport const getBranchesBetweenCommits = async (\n lastCommit: string,\n earlyCommit: string,\n remote = true\n) => {\n const allBranches: string[] = [];\n const allCommits = await getAllCommitsBetween(\n lastCommit,\n earlyCommit,\n remote\n );\n\n for (const commit of allCommits) {\n // Note: it may contains `branches` that checkout based on current `commit hash` but out of the `commit` range we want.\n const branches = await getBranchesContainCommitHash(commit, remote);\n allBranches.push(...branches);\n }\n\n const finalBranches: string[] = [];\n // re-loop all branches to check it's commit hash between `${allCommits}`\n for (const branch of allBranches) {\n const branchCommitHash = await getCommitIdOfBranch(branch, true);\n if (branchCommitHash && allCommits.includes(branchCommitHash)) {\n finalBranches.push(branch);\n }\n }\n\n return arrayUnique(finalBranches);\n};\n","export function arrayUnique<T>(arr: T[]): T[] {\n function onlyUnique(value, index, self) {\n return self.indexOf(value) === index;\n }\n return arr.filter(onlyUnique);\n}\n"]}
{"version":3,"sources":["../src/get-commit-id-of-branch.ts","../src/get-last-commit-hash.ts","../src/get-branch-name.ts","../src/get-file-commit-status.ts","../src/get-diff-files.ts","../src/get-all-commits-between.ts","../src/get-branches-between-commits.ts","../src/array-unique.ts","../src/has-changed-file-pending-to-commit.ts"],"names":["exec","getCommitIdOfBranch","branchName","short","resolve","err","out","getLastCommitHash","gitBranchName","reject","getFileCommitStatus","fileName","getDiffFiles","earlyCommit","lastCommit","diff","getAllCommitsBetween","remote","arrayUnique","arr","onlyUnique","value","index","self","getBranchesContainCommitHash","commit","branches","branch","s","getBranchesBetweenCommits","allBranches","allCommits","finalBranches","branchCommitHash","hasChangedFilePendingToCommit"],"mappings":"AAAA,OAAS,QAAAA,MAAY,qBASd,IAAMC,EAAsB,CAACC,EAAa,OAAQC,EAAQ,KACxD,IAAI,QAAwBC,GAAY,CAC7CJ,EACE,iBAAiBG,EAAQ,WAAa,EAAE,GAAGD,CAAU,GACrD,CAACG,EAAKC,IAAQ,CACZ,GAAID,EACF,OAAOD,EAAQ,IAAI,EAErBA,EAAQE,EAAI,QAAQ,cAAe,EAAE,CAAC,CACxC,CACF,CACF,CAAC,ECpBH,OAAS,QAAAN,MAAY,qBAEd,SAASO,GAAoB,CAClC,OAAO,IAAI,QAAwBH,GAAY,CAC7CJ,EAAK,oCAAqC,CAACK,EAAKC,IAAQ,CACtD,GAAID,EACF,OAAOD,EAAQ,IAAI,EAErBA,EAAQE,EAAI,QAAQ,KAAM,EAAE,CAAC,CAC/B,CAAC,CACH,CAAC,CACH,CCXA,OAAS,QAAAN,MAAY,qBAEd,SAASQ,GAAgB,CAC9B,OAAO,IAAI,QAA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AAE,OAAS,CAAC,CACnD,CAAC,CACH,CAAC,CACH","sourcesContent":["import { exec } from 'node:child_process';\n\n/**\n * https://stackoverflow.com/questions/15677439/command-to-get-latest-git-commit-hash-from-a-branch\n * Finding out the latest commit hash locally, NOTE: no `-r(remote)` argument if need to `remote` using `origin/xxx`\n * @param branchName e.g. `main`, `workspace`,....\n * @param short `ba39827` instead `ba3982746fafc8b1c37fce7692cb28b01044bb5f`\n * @returns The branch commit hash.\n */\nexport const getCommitIdOfBranch = (branchName = 'HEAD', short = true) => {\n return new Promise<string | null>((resolve) => {\n exec(\n `git rev-parse ${short ? '--short ' : ''}${branchName}`,\n (err, out) => {\n if (err) {\n return resolve(null);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, ''));\n }\n );\n });\n};\n","import { exec } from 'node:child_process';\n\nexport function getLastCommitHash() {\n return new Promise<string | null>((resolve) => {\n exec(`git log --pretty=format:'%h' -n 1`, (err, out) => {\n if (err) {\n return resolve(null);\n }\n resolve(out.replace(/'/g, ''));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\nexport function gitBranchName() {\n return new Promise<string>((resolve, reject) => {\n exec(`git branch | sed -n '/\\\\* /s///p'`, (err, out) => {\n if (err) {\n return reject(err);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, ''));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * Check if the filepath has been commited.\n * @param fileName The file name\n * @returns Return true indicates the files has been commited.\n */\nexport function getFileCommitStatus(fileName) {\n return new Promise<boolean>((resolve, reject) => {\n exec(`git ls-files ${fileName}`, (err, out) => {\n if (err) {\n return reject(err);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, '').length > 0);\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgtltcommitgt--ltpathgt82308203\n * Viewing the changes between two arbitrary <commit>, NOTE: no `-r(remote)` argument if need to `remote` using `commitHash` of `origin/xxx`\n * If <commit> on one side is omitted, it will have the same effect as using HEAD instead.\n * @example\n * ```shell\n * git --no-pager diff --name-only 2b2ccff..50164ec\n * git --no-pager diff --name-only 2b2ccff 50164ec\n * git --no-pager diff --name-only 1381f5\n * ```\n * @param earlyCommit The diff based earlier commit hash\n * @param lastCommit If is omitted, it will have the same effect as using HEAD instead.\n * @returns Return files between two commit hash\n */\n\nexport function getDiffFiles(earlyCommit: string, lastCommit?: string) {\n const diff =\n earlyCommit && lastCommit ? `${lastCommit}..${earlyCommit}` : earlyCommit;\n\n return new Promise<string[]>((resolve) => {\n exec(`git --no-pager diff --name-only ${diff}`, (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n resolve(out.split(/[\\r\\n]/).filter(String));\n });\n });\n}\n","import { exec } from 'node:child_process';\n\n/**\n * This will list all commits that contain any of the commits between earlyCommit and lastCommit\n * @param lastCommit The lasted commit `HEAD`\n * @param earlyCommit The early commit `master`\n * @returns All commits\n */\nexport const getAllCommitsBetween = (\n lastCommit: string,\n earlyCommit: string,\n remote = true\n) => {\n // NOTE: The order is from `${earlyCommit} to ${lastComit}`\n const diff =\n earlyCommit && lastCommit ? `${earlyCommit}..${lastCommit}` : lastCommit;\n\n // if only `lastCommit` provider, will list all commits.\n return new Promise<string[]>((resolve) => {\n exec(\n `git --no-pager log ${\n remote ? '-r' : ''\n } --oneline ${diff} | cut -d \" \" -f1`,\n (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n resolve(out.split(/[\\r\\n|]/).filter(String));\n }\n );\n });\n};\n","import { exec } from 'node:child_process';\nimport { arrayUnique } from './array-unique.js';\nimport { getAllCommitsBetween } from './get-all-commits-between.js';\nimport { getCommitIdOfBranch } from './get-commit-id-of-branch.js';\n\nconst getBranchesContainCommitHash = (commit: string, remote = true) => {\n return new Promise<string[]>((resolve) => {\n // This command will list all the branch names that contain the specified commit ID and output it to the terminal\n // Note that other branch nodes checkout based on the current branch will also contain this commitId.\n exec(\n `git --no-pager branch ${remote ? '-r' : ''} --contains ${commit}`,\n (err, out) => {\n if (err) {\n // fatal: ambiguous argument 'df3s053': unknown revision or path not in the working tree.\n return resolve([]);\n }\n const branches = out\n .split(/[\\r\\n]/)\n .filter((branch) => {\n // Remove `* (HEAD detached at b121521)`\n return !~branch.toUpperCase().indexOf('(HEAD') && !!branch.trim();\n })\n .map((s) => s.trim());\n\n resolve(branches);\n }\n );\n });\n};\n\n/**\n * This will list all branches that contain any of the commits between commit1 and commit2\n * @param lastCommit if not will look up to first commit `HEAD`\n * @param earlyCommit The earlier commit `master`\n * @param remote The remote-tracking\n * @returns All commits\n */\nexport const getBranchesBetweenCommits = async (\n lastCommit: string,\n earlyCommit: string,\n remote = true\n) => {\n const allBranches: string[] = [];\n const allCommits = await getAllCommitsBetween(\n lastCommit,\n earlyCommit,\n remote\n );\n\n for (const commit of allCommits) {\n // Note: it may contains `branches` that checkout based on current `commit hash` but out of the `commit` range we want.\n const branches = await getBranchesContainCommitHash(commit, remote);\n allBranches.push(...branches);\n }\n\n const finalBranches: string[] = [];\n // re-loop all branches to check it's commit hash between `${allCommits}`\n for (const branch of allBranches) {\n const branchCommitHash = await getCommitIdOfBranch(branch, true);\n if (branchCommitHash && allCommits.includes(branchCommitHash)) {\n finalBranches.push(branch);\n }\n }\n\n return arrayUnique(finalBranches);\n};\n","export function arrayUnique<T>(arr: T[]): T[] {\n function onlyUnique(value, index, self) {\n return self.indexOf(value) === index;\n }\n return arr.filter(onlyUnique);\n}\n","import { exec } from 'node:child_process';\n\n/**\n * Check if the filepath has been commited.\n * ====\n * Note: The -s option stands for \"short\" and is used to display the output in a compact format.\n * - ??: Untracked files (new files that are not yet staged)\n * - A: New files that are staged and ready to be committed\n * - M: Modified files (changes made to existing files)\n * - D: Deleted files (files that have been removed from the repository)\n * - R: Renamed files\n * - C: Files with copy and rename changes\n * - U: Files with merge conflicts\n * @returns Return `true` indicates the has changed `files` waiting to be commit.\n */\nexport function hasChangedFilePendingToCommit() {\n return new Promise<boolean>((resolve, reject) => {\n exec(`git status -s`, (err, out) => {\n if (err) {\n return reject(err);\n }\n resolve(out.replace(/\\r\\n|\\n|\\r/g, '').length > 0);\n });\n });\n}\n"]}
{
"name": "@armit/git",
"version": "0.0.36",
"version": "0.0.37",
"homepage": "https://github.com/armitjs/armit",

@@ -45,16 +45,16 @@ "repository": {

"devDependencies": {
"@armit/eslint-config-bases": "^0.0.23",
"@types/node": "20.4.5",
"@vitest/coverage-istanbul": "0.33.0",
"@vitest/ui": "0.33.0",
"eslint": "8.45.0",
"@armit/eslint-config-bases": "^0.0.24",
"@types/node": "20.4.9",
"@vitest/coverage-istanbul": "0.34.1",
"@vitest/ui": "0.34.1",
"eslint": "8.46.0",
"npm-run-all": "4.1.5",
"prettier": "3.0.0",
"prettier": "3.0.1",
"rimraf": "5.0.1",
"ts-node": "10.9.1",
"tsup": "7.1.0",
"tsup": "7.2.0",
"typescript": "5.1.6",
"vite": "4.4.7",
"vite": "4.4.9",
"vite-tsconfig-paths": "4.2.0",
"vitest": "0.33.0"
"vitest": "0.34.1"
},

@@ -61,0 +61,0 @@ "engines": {