func checkBranchExistence()

in pkg/git/git.go [787:830]


func checkBranchExistence(user, pass *string, branchName string, r git.Repository) (bool, error) {
	log.Info("checking if branch exist", logBranchNameKey, branchName)

	remote, err := r.Remote("origin")
	if err != nil {
		return false, fmt.Errorf("failed to get GIT remove origin: %w", err)
	}

	glo := &git.ListOptions{}

	if user != nil && pass != nil {
		glo = &git.ListOptions{
			Auth: &http.BasicAuth{
				Username: *user,
				Password: *pass,
			},
		}
	}

	refList, err := remote.List(glo)
	if err != nil {
		return false, fmt.Errorf("failed to get references on the remote repository: %w", err)
	}

	existBranchOrNot := true
	refPrefix := "refs/heads/"

	for _, ref := range refList {
		refName := ref.Name().String()
		if !strings.HasPrefix(refName, refPrefix) {
			continue
		}

		b := refName[len(refPrefix):]
		if b == branchName {
			existBranchOrNot = false
			break
		}
	}

	log.Info("branch existence status", logBranchNameKey, branchName, "existBranchOrNot", existBranchOrNot)

	return existBranchOrNot, nil
}