@@ -2,7 +2,10 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises';
22import path from 'node:path' ;
33import type { SimpleGit } from 'simple-git' ;
44import type { CoreConfig , Report , ReportsDiff } from '@code-pushup/models' ;
5- import { stringifyError } from '@code-pushup/utils' ;
5+ import {
6+ removeUndefinedAndEmptyProps ,
7+ stringifyError ,
8+ } from '@code-pushup/utils' ;
69import {
710 type CommandContext ,
811 createCommandContext ,
@@ -12,12 +15,14 @@ import {
1215 runPrintConfig ,
1316} from './cli/index.js' ;
1417import { parsePersistConfig } from './cli/persist.js' ;
15- import { listChangedFiles } from './git.js' ;
18+ import { DEFAULT_SETTINGS } from './constants.js' ;
19+ import { listChangedFiles , normalizeGitRef } from './git.js' ;
1620import { type SourceFileIssue , filterRelevantIssues } from './issues.js' ;
1721import type {
1822 GitBranch ,
1923 GitRefs ,
2024 Logger ,
25+ Options ,
2126 OutputFiles ,
2227 ProjectRunResult ,
2328 ProviderAPIClient ,
@@ -26,12 +31,17 @@ import type {
2631import type { ProjectConfig } from './monorepo/index.js' ;
2732
2833export type RunEnv = {
29- refs : GitRefs ;
34+ refs : NormalizedGitRefs ;
3035 api : ProviderAPIClient ;
3136 settings : Settings ;
3237 git : SimpleGit ;
3338} ;
3439
40+ type NormalizedGitRefs = {
41+ head : GitBranch ;
42+ base ?: GitBranch ;
43+ } ;
44+
3545export type CompareReportsArgs = {
3646 project : ProjectConfig | null ;
3747 env : RunEnv ;
@@ -53,6 +63,28 @@ export type BaseReportArgs = {
5363 ctx : CommandContext ;
5464} ;
5565
66+ export async function createRunEnv (
67+ refs : GitRefs ,
68+ api : ProviderAPIClient ,
69+ options : Options | undefined ,
70+ git : SimpleGit ,
71+ ) : Promise < RunEnv > {
72+ const [ head , base ] = await Promise . all ( [
73+ normalizeGitRef ( refs . head , git ) ,
74+ refs . base && normalizeGitRef ( refs . base , git ) ,
75+ ] ) ;
76+
77+ return {
78+ refs : { head, ...( base && { base } ) } ,
79+ api,
80+ settings : {
81+ ...DEFAULT_SETTINGS ,
82+ ...( options && removeUndefinedAndEmptyProps ( options ) ) ,
83+ } ,
84+ git,
85+ } ;
86+ }
87+
5688export async function runOnProject (
5789 project : ProjectConfig | null ,
5890 env : RunEnv ,
@@ -81,9 +113,7 @@ export async function runOnProject(
81113
82114 const noDiffOutput = {
83115 name : project ?. name ?? '-' ,
84- files : {
85- report : reportFiles ,
86- } ,
116+ files : { report : reportFiles } ,
87117 } satisfies ProjectRunResult ;
88118
89119 if ( base == null ) {
@@ -223,13 +253,6 @@ export async function loadCachedBaseReport(
223253 return null ;
224254}
225255
226- export async function ensureHeadBranch ( { refs, git } : RunEnv ) : Promise < void > {
227- const { head } = refs ;
228- if ( head . sha !== ( await git . revparse ( 'HEAD' ) ) ) {
229- await git . checkout ( [ '-f' , head . ref ] ) ;
230- }
231- }
232-
233256export async function runInBaseBranch < T > (
234257 base : GitBranch ,
235258 env : RunEnv ,
0 commit comments