in pkg/git/git.go [556:606]
func (*GitProvider) Checkout(user, pass *string, directory, branchName string, remote bool) error {
log.Info("trying to checkout to branch", logBranchNameKey, branchName)
r, err := git.PlainOpen(directory)
if err != nil {
return fmt.Errorf(errPlainOpenTmpl, directory, err)
}
w, err := r.Worktree()
if err != nil {
return fmt.Errorf("failed to get git worktree: %w", err)
}
createBranchOrNot := true
if remote {
gfo := &git.FetchOptions{RefSpecs: []config.RefSpec{"refs/*:refs/*"}}
if user != nil && pass != nil {
gfo = &git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*"},
Auth: &http.BasicAuth{
Username: *user,
Password: *pass,
},
}
}
err = r.Fetch(gfo)
if err != nil {
if err.Error() != "already up-to-date" {
return fmt.Errorf("failed to fetch: %w", err)
}
}
createBranchOrNot, err = checkBranchExistence(user, pass, branchName, *r)
if err != nil {
return err
}
}
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchName)),
Force: true,
Create: createBranchOrNot,
})
if err != nil {
return fmt.Errorf("failed to checkout git branch: %w", err)
}
return nil
}