( rootDir: string, baseBranch: string, )
| 158 | |
| 159 | /** Get commits on the current branch since it diverged from baseBranch */ |
| 160 | export function getBranchCommits( |
| 161 | rootDir: string, |
| 162 | baseBranch: string, |
| 163 | ): { hash: string; subject: string; body: string }[] { |
| 164 | // Ensure we have the base branch ref |
| 165 | if (!tryRunArgs(['git', 'rev-parse', '--verify', `origin/${baseBranch}`], { cwd: rootDir })) { |
| 166 | tryRunArgs(['git', 'fetch', 'origin', baseBranch, '--depth=1'], { cwd: rootDir }); |
| 167 | } |
| 168 | |
| 169 | const mergeBase = tryRunArgs(['git', 'merge-base', 'HEAD', `origin/${baseBranch}`], { cwd: rootDir }); |
| 170 | const ref = mergeBase || `origin/${baseBranch}`; |
| 171 | |
| 172 | const rawLog = tryRunArgs(['git', 'log', `${ref}..HEAD`, '--format=%H%n%s%n%b%n---END---'], { cwd: rootDir }); |
| 173 | if (!rawLog) return []; |
| 174 | |
| 175 | const commits: { hash: string; subject: string; body: string }[] = []; |
| 176 | const entries = rawLog.split('---END---').filter((e) => e.trim()); |
| 177 | for (const entry of entries) { |
| 178 | const lines = entry.trim().split('\n'); |
| 179 | if (lines.length < 2) continue; |
| 180 | commits.push({ |
| 181 | hash: lines[0]!.trim(), |
| 182 | subject: lines[1]!.trim(), |
| 183 | body: lines.slice(2).join('\n').trim(), |
| 184 | }); |
| 185 | } |
| 186 | return commits; |
| 187 | } |
| 188 | |
| 189 | /** Get files changed in a specific commit */ |
| 190 | export function getFilesChangedInCommit(hash: string, opts?: { cwd?: string }): string[] { |
no test coverage detected
searching dependent graphs…