Skip to content

Commit b70bdce

Browse files
committed
gitrepo: gitCheckout() return typed errors only
gitCheckout() should return typed errors only. This helps prevent error type assertions by the caller to determine how to handle the error. gitCheckout() also sets the appropriate conditions associated with the error, if any. Signed-off-by: Sunny <[email protected]>
1 parent f5a1dd8 commit b70bdce

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

controllers/gitrepository_controller.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
505505

506506
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, optimizedClone)
507507
if err != nil {
508-
e := serror.NewGeneric(
509-
fmt.Errorf("failed to checkout and determine revision: %w", err),
510-
sourcev1.GitOperationFailedReason,
511-
)
512-
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
513-
return sreconcile.ResultEmpty, e
508+
return sreconcile.ResultEmpty, err
514509
}
515510
// Assign the commit to the shared commit reference.
516511
*commit = *c
@@ -544,12 +539,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
544539
// optimization.
545540
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, false)
546541
if err != nil {
547-
e := serror.NewGeneric(
548-
fmt.Errorf("failed to checkout and determine revision: %w", err),
549-
sourcev1.GitOperationFailedReason,
550-
)
551-
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
552-
return sreconcile.ResultEmpty, e
542+
return sreconcile.ResultEmpty, err
553543
}
554544
*commit = *c
555545
}
@@ -773,7 +763,16 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
773763
// Checkout HEAD of reference in object
774764
gitCtx, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
775765
defer cancel()
776-
return checkoutStrategy.Checkout(gitCtx, dir, repoURL, authOpts)
766+
commit, err := checkoutStrategy.Checkout(gitCtx, dir, repoURL, authOpts)
767+
if err != nil {
768+
e := serror.NewGeneric(
769+
fmt.Errorf("failed to checkout and determine revision: %w", err),
770+
sourcev1.GitOperationFailedReason,
771+
)
772+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
773+
return nil, e
774+
}
775+
return commit, nil
777776
}
778777

779778
// fetchIncludes fetches artifact metadata of all the included repos.

0 commit comments

Comments
 (0)