|
| 1 | +import path from 'path'; |
| 2 | +import { Action, Step } from '../../actions'; |
| 3 | +import { spawnSync } from 'child_process'; |
| 4 | + |
| 5 | +const exec = async (req: any, action: Action): Promise<Action> => { |
| 6 | + const step = new Step('checkHiddenCommits'); |
| 7 | + |
| 8 | + try { |
| 9 | + const repoPath = `${action.proxyGitPath}/${action.repoName}`; |
| 10 | + |
| 11 | + const oldOid = action.commitFrom; |
| 12 | + const newOid = action.commitTo; |
| 13 | + if (!oldOid || !newOid) { |
| 14 | + throw new Error('Both action.commitFrom and action.commitTo must be defined'); |
| 15 | + } |
| 16 | + |
| 17 | + // build introducedCommits set |
| 18 | + const introducedCommits = new Set<string>(); |
| 19 | + const revRange = |
| 20 | + oldOid === '0000000000000000000000000000000000000000' ? newOid : `${oldOid}..${newOid}`; |
| 21 | + const revList = spawnSync('git', ['rev-list', revRange], { cwd: repoPath, encoding: 'utf-8' }) |
| 22 | + .stdout.trim() |
| 23 | + .split('\n') |
| 24 | + .filter(Boolean); |
| 25 | + revList.forEach((sha) => introducedCommits.add(sha)); |
| 26 | + step.log(`Total introduced commits: ${introducedCommits.size}`); |
| 27 | + |
| 28 | + // build packCommits set |
| 29 | + const packPath = path.join('.git', 'objects', 'pack'); |
| 30 | + const packCommits = new Set<string>(); |
| 31 | + (action.newIdxFiles || []).forEach((idxFile) => { |
| 32 | + const idxPath = path.join(packPath, idxFile); |
| 33 | + const out = spawnSync('git', ['verify-pack', '-v', idxPath], { |
| 34 | + cwd: repoPath, |
| 35 | + encoding: 'utf-8', |
| 36 | + }) |
| 37 | + .stdout.trim() |
| 38 | + .split('\n'); |
| 39 | + out.forEach((line) => { |
| 40 | + const [sha, type] = line.split(/\s+/); |
| 41 | + if (type === 'commit') packCommits.add(sha); |
| 42 | + }); |
| 43 | + }); |
| 44 | + step.log(`Total commits in the pack: ${packCommits.size}`); |
| 45 | + |
| 46 | + // subset check |
| 47 | + const isSubset = [...packCommits].every((sha) => introducedCommits.has(sha)); |
| 48 | + if (!isSubset) { |
| 49 | + // build detailed lists |
| 50 | + const [referenced, unreferenced] = [...packCommits].reduce<[string[], string[]]>( |
| 51 | + ([ref, unref], sha) => |
| 52 | + introducedCommits.has(sha) ? [[...ref, sha], unref] : [ref, [...unref, sha]], |
| 53 | + [[], []], |
| 54 | + ); |
| 55 | + |
| 56 | + step.log(`Referenced commits: ${referenced.length}`); |
| 57 | + step.log(`Unreferenced commits: ${unreferenced.length}`); |
| 58 | + |
| 59 | + step.setError( |
| 60 | + `Unreferenced commits in pack (${unreferenced.length}): ${unreferenced.join(', ')}.\n` + |
| 61 | + `This usually happens when a branch was made from a commit that hasn't been approved and pushed to the remote.\n` + |
| 62 | + `Please get approval on the commits, push them and try again.`, |
| 63 | + ); |
| 64 | + action.error = true; |
| 65 | + step.setContent(`Referenced: ${referenced.length}, Unreferenced: ${unreferenced.length}`); |
| 66 | + } else { |
| 67 | + // all good, no logging of individual SHAs needed |
| 68 | + step.log('All pack commits are referenced in the introduced range.'); |
| 69 | + step.setContent(`All ${packCommits.size} pack commits are within introduced commits.`); |
| 70 | + } |
| 71 | + } catch (e: any) { |
| 72 | + step.setError(e.message); |
| 73 | + throw e; |
| 74 | + } finally { |
| 75 | + action.addStep(step); |
| 76 | + } |
| 77 | + |
| 78 | + return action; |
| 79 | +}; |
| 80 | + |
| 81 | +exec.displayName = 'checkHiddenCommits.exec'; |
| 82 | +export { exec }; |
0 commit comments