@@ -221,64 +221,80 @@ func (g *FedoraSourcesProviderImpl) renameSpecIfNeeded(dir, upstreamName, compon
221221 return nil
222222}
223223
224- // checkoutTargetCommit determines the appropriate commit to use and checks it out.
225- // Priority order:
226- // 1. Explicit upstream commit hash - specified per-component via upstream-commit
227- // 2. Upstream distro snapshot - snapshot time from the provider's resolved distro
228- // 3. Default - use current HEAD (no checkout needed)
224+ // checkoutTargetCommit resolves the effective commit via [resolveEffectiveCommitHash]
225+ // and checks it out in the cloned repository.
229226func (g * FedoraSourcesProviderImpl ) checkoutTargetCommit (
230227 ctx context.Context ,
231228 upstreamCommit string ,
232229 repoDir string ,
233230) error {
234- // Case 1: Explicit upstream commit hash specified per-component
235- if upstreamCommit != "" {
236- slog . Info ( "Using explicit upstream commit hash" ,
237- "commitHash" , upstreamCommit )
231+ commitHash , err := g . resolveEffectiveCommitHash ( ctx , repoDir , upstreamCommit , slog . LevelInfo )
232+ if err != nil {
233+ return err
234+ }
238235
239- if err := g .gitProvider .Checkout (ctx , repoDir , upstreamCommit ); err != nil {
240- return fmt .Errorf ("failed to checkout upstream commit %#q:\n %w" , upstreamCommit , err )
241- }
236+ if err := g .gitProvider .Checkout (ctx , repoDir , commitHash ); err != nil {
237+ return fmt .Errorf ("failed to checkout commit %#q:\n %w" , commitHash , err )
238+ }
242239
243- return nil
240+ return nil
241+ }
242+
243+ // resolveEffectiveCommitHash is the single source of truth for which commit a
244+ // component should use from a cloned repository.
245+ //
246+ // Priority:
247+ // 1. Explicit upstream commit hash (pinned per-component).
248+ // 2. Snapshot time — commit immediately before the snapshot date.
249+ // 3. Default — current HEAD.
250+ func (g * FedoraSourcesProviderImpl ) resolveEffectiveCommitHash (
251+ ctx context.Context ,
252+ repoDir string ,
253+ upstreamCommit string ,
254+ logLevel slog.Level ,
255+ ) (string , error ) {
256+ // Case 1: Explicit upstream commit hash specified per-component.
257+ if upstreamCommit != "" {
258+ slog .Log (ctx , logLevel , "Using explicit upstream commit hash" , "commitHash" , upstreamCommit )
259+
260+ return upstreamCommit , nil
244261 }
245262
246- // Case 2: Provider has a snapshot time configured from the resolved distro
263+ // Case 2: Provider has a snapshot time configured from the resolved distro.
247264 if g .snapshotTime != "" {
248265 snapshotDateTime , err := time .Parse (time .RFC3339 , g .snapshotTime )
249266 if err != nil {
250- return fmt .Errorf ("invalid snapshot time %#q:\n %w" , g .snapshotTime , err )
267+ return "" , fmt .Errorf ("invalid snapshot time %#q:\n %w" , g .snapshotTime , err )
251268 }
252269
253270 commitHash , err := g .gitProvider .GetCommitHashBeforeDate (ctx , repoDir , snapshotDateTime )
254271 if err != nil {
255- return fmt .Errorf ("failed to get commit hash for snapshot time %s:\n %w" ,
272+ return "" , fmt .Errorf ("resolving commit for snapshot time %s:\n %w" ,
256273 snapshotDateTime .Format (time .RFC3339 ), err )
257274 }
258275
259- slog .Info ( "Using upstream distro snapshot time" ,
276+ slog .Log ( ctx , logLevel , "Using upstream distro snapshot time" ,
260277 "snapshotDateTime" , snapshotDateTime .Format (time .RFC3339 ),
261278 "commitHash" , commitHash )
262279
263- if err := g .gitProvider .Checkout (ctx , repoDir , commitHash ); err != nil {
264- return fmt .Errorf ("failed to checkout snapshot commit %#q:\n %w" , commitHash , err )
265- }
280+ return commitHash , nil
281+ }
266282
267- return nil
283+ // Case 3: Default — use current HEAD.
284+ commitHash , err := g .gitProvider .GetCurrentCommit (ctx , repoDir )
285+ if err != nil {
286+ return "" , fmt .Errorf ("resolving current HEAD commit:\n %w" , err )
268287 }
269288
270- // Case 3: Default - use current HEAD (already checked out by clone)
271- slog .Info ("Using current HEAD (no snapshot time configured)" )
289+ slog .Log (ctx , logLevel , "Using current HEAD" , "commitHash" , commitHash )
272290
273- return nil
291+ return commitHash , nil
274292}
275293
276294// ResolveSourceIdentity implements [SourceIdentityProvider] by resolving the upstream
277- // commit hash for the component. Resolution priority matches [checkoutTargetCommit]:
278- // 1. Explicit upstream commit hash (pinned per-component) — returned directly.
279- // 2. Snapshot time — perform a metadata-only clone of the dist-git branch and use the
280- // local git history to find the commit immediately before the snapshot date.
281- // 3. Default — perform a metadata-only clone of the dist-git branch and use its current HEAD.
295+ // commit hash for the component. Resolution priority is defined by
296+ // [resolveEffectiveCommitHash]. Cases 2 and 3 require a metadata-only clone of the
297+ // dist-git branch; case 1 (pinned commit) is returned directly without a network call.
282298func (g * FedoraSourcesProviderImpl ) ResolveSourceIdentity (
283299 ctx context.Context ,
284300 component components.Component ,
@@ -307,8 +323,8 @@ func (g *FedoraSourcesProviderImpl) ResolveSourceIdentity(
307323 return g .resolveCommit (ctx , gitRepoURL , upstreamName )
308324}
309325
310- // resolveCommit clones the branch and determines the effective commit, either
311- // at the snapshot time, or at the latest commit if no snapshot time is configured .
326+ // resolveCommit clones the branch and determines the effective commit via
327+ // [resolveEffectiveCommitHash] .
312328func (g * FedoraSourcesProviderImpl ) resolveCommit (
313329 ctx context.Context , gitRepoURL string , upstreamName string ,
314330) (string , error ) {
@@ -341,30 +357,12 @@ func (g *FedoraSourcesProviderImpl) resolveCommit(
341357 return "" , fmt .Errorf ("partial clone for identity of %#q:\n %w" , upstreamName , err )
342358 }
343359
344- var commitHash string
345-
346- if g .snapshotTime != "" {
347- snapshotDateTime , parseErr := time .Parse (time .RFC3339 , g .snapshotTime )
348- if parseErr != nil {
349- return "" , fmt .Errorf ("invalid snapshot time %#q:\n %w" , g .snapshotTime , parseErr )
350- }
351-
352- commitHash , err = g .gitProvider .GetCommitHashBeforeDate (ctx , tempDir , snapshotDateTime )
353- if err != nil {
354- return "" , fmt .Errorf ("resolving snapshot commit for %#q at %s:\n %w" ,
355- upstreamName , snapshotDateTime .Format (time .RFC3339 ), err )
356- }
357- } else {
358- commitHash , err = g .gitProvider .GetCurrentCommit (ctx , tempDir )
359- if err != nil {
360- return "" , fmt .Errorf ("resolving current commit for %#q:\n %w" , upstreamName , err )
361- }
360+ // upstreamCommit is "" here — the pinned-commit case is handled by the
361+ // caller (ResolveSourceIdentity) before cloning.
362+ commitHash , err := g .resolveEffectiveCommitHash (ctx , tempDir , "" , slog .LevelDebug )
363+ if err != nil {
364+ return "" , fmt .Errorf ("resolving commit for %#q:\n %w" , upstreamName , err )
362365 }
363366
364- slog .Debug ("Resolved snapshot commit for identity" ,
365- "component" , upstreamName ,
366- "snapshot" , g .snapshotTime ,
367- "commit" , commitHash )
368-
369367 return commitHash , nil
370368}
0 commit comments