github.com/pluralsh/plural-cli@v0.9.5/pkg/utils/git/remote.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	gogit "github.com/go-git/go-git/v5"
     9  	"github.com/go-git/go-git/v5/plumbing/transport"
    10  	"github.com/pluralsh/plural-cli/pkg/utils/errors"
    11  )
    12  
    13  func Clone(auth transport.AuthMethod, url, path string) (*gogit.Repository, error) {
    14  	return gogit.PlainClone(path, false, &gogit.CloneOptions{
    15  		Auth:     auth,
    16  		URL:      url,
    17  		Progress: os.Stdout,
    18  	})
    19  }
    20  
    21  func Repair(root string) error {
    22  	_, err := git(root, "diff-index", "--quiet", "HEAD", "--")
    23  	if err == nil {
    24  		return nil
    25  	}
    26  
    27  	diff, _ := git(root, "--no-pager", "diff")
    28  	if strings.TrimSpace(diff) == "" && err != nil {
    29  		return Sync(root, "committing new encrypted files", false)
    30  	}
    31  
    32  	return fmt.Errorf("There were non-repairable changes in your local git repository, you'll need to investigate them manually")
    33  }
    34  
    35  func Sync(root, msg string, force bool) error {
    36  	if res, err := git(root, "add", "."); err != nil {
    37  		return errors.ErrorWrap(fmt.Errorf(res), "`git add .` failed")
    38  	}
    39  
    40  	if res, err := git(root, "commit", "-m", msg); err != nil {
    41  		return errors.ErrorWrap(fmt.Errorf(res), "failed to commit changes")
    42  	}
    43  
    44  	branch, err := CurrentBranch()
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	args := []string{"push", "origin", branch}
    50  	if force {
    51  		args = []string{"push", "-f", "origin", branch}
    52  	}
    53  
    54  	if res, err := git(root, args...); err != nil {
    55  		return errors.ErrorWrap(fmt.Errorf(res), fmt.Sprintf("`git push origin %s` failed", branch))
    56  	}
    57  
    58  	return nil
    59  }