Skip to content

Commit

Permalink
Merge pull request #7 from seriouspoop/6-remote-pull-failing
Browse files Browse the repository at this point in the history
6 remote pull failing
  • Loading branch information
seriouspoop authored Oct 18, 2024
2 parents abd8f20 + a4d4678 commit 6f34624
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 97 deletions.
1 change: 0 additions & 1 deletion internal/handler/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func Run(s servicer) *cobra.Command {
}
return err
}
utils.Logger(utils.LOG_SUCCESS, "changes fetched")

// Push changes
utils.Logger(utils.LOG_INFO, "Pushing changes...")
Expand Down
8 changes: 4 additions & 4 deletions repo/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (g *Git) AddRemote(remote *model.Remote) error {
}

func (g *Git) Pull(remote *model.Remote, branch model.Branch, auth *config.Credentials, force bool) error {

if auth == nil {
return g.err.AuthNotFound
}
Expand Down Expand Up @@ -160,8 +161,7 @@ func (g *Git) Pull(remote *model.Remote, branch model.Branch, auth *config.Crede
ReferenceName: plumbing.NewBranchReferenceName(branch.String()),
SingleBranch: true,
Auth: Auth,
Progress: os.Stdin,
Force: false,
Force: force,
})

if err != nil {
Expand Down Expand Up @@ -233,7 +233,7 @@ func (g *Git) AddThenCommit(commitMsg string) error {
return err
}

func (g *Git) Push(remote *model.Remote, branch model.Branch, auth *config.Credentials) error {
func (g *Git) Push(remote *model.Remote, branch model.Branch, auth *config.Credentials, force bool) error {
if auth == nil {
return g.err.AuthNotFound
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func (g *Git) Push(remote *model.Remote, branch model.Branch, auth *config.Crede
// final refspecs
gitCfg.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/heads/%s", branch.String(), branch.String())),
},
Force: true,
Force: force,
Progress: os.Stdout,
Auth: Auth,
})
Expand Down
32 changes: 1 addition & 31 deletions repo/script/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package script

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -33,17 +32,6 @@ func (b *Bash) GetCurrentBranch() (model.Branch, error) {
return model.Branch(string(output[:len(output)-1])), nil
}

func (b *Bash) PullBranch(remoteName string, branch model.Branch, force bool) (string, error) {
var cmd *exec.Cmd
if force {
cmd = exec.Command("git", "pull", remoteName, branch.String(), "--allow-unrelated-histories")
} else {
cmd = exec.Command("git", "pull", remoteName, branch.String())
}
output, err := cmd.CombinedOutput()
return string(output[:len(output)-1]), err
}

func (b *Bash) GenerateMocks() (string, error) {
cmd := exec.Command("go", "generate", "./...")
output, err := cmd.CombinedOutput()
Expand All @@ -65,18 +53,6 @@ func (b *Bash) RunTests() (string, error) {
return string(output[:len(output)-1]), err
}

// TODO -> shift this to git repo
func (b *Bash) Push(branch model.Branch, withUpStream bool) (string, error) {
var cmd *exec.Cmd
if withUpStream {
cmd = exec.Command("git", "push", "-u", "origin", branch.String())
} else {
cmd = exec.Command("git", "push", "origin", branch.String())
}
output, err := cmd.CombinedOutput()
return string(output[:len(output)-1]), err
}

func (b *Bash) Exists(path, name string) bool {
fpath := filepath.Join(path, name)
_, err := os.Stat(fpath)
Expand All @@ -93,20 +69,14 @@ func (b *Bash) CreateDir(path, name string) error {
return os.Mkdir(dpath, os.ModePerm)
}

func (b *Bash) SetUpstream(remoteName string, branch model.Branch) error {
remoteArg := fmt.Sprintf("%s/%s", remoteName, branch)
cmd := exec.Command("git", "branch", "--set-upstream-to", remoteArg)
_, err := cmd.CombinedOutput()
return err
}

func (b *Bash) GenerateSSHKey(path, keyName, mail, passphrase string) error {
filePath := filepath.Join(path, keyName)
cmd := exec.Command("ssh-keygen", "-t", "ed25519", "-C", mail, "-f", filePath, "-P", passphrase)
_, err := cmd.CombinedOutput()
return err
}

// TODO -> migrate this to go-git
func (b *Bash) PullMerge() (string, error) {
cmd := exec.Command("git", "merge")
output, err := cmd.CombinedOutput()
Expand Down
5 changes: 1 addition & 4 deletions svc/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ type gitHelper interface {
ChangeOccured() (bool, error)
AddThenCommit(commitMsg string) error
Pull(remote *model.Remote, branch model.Branch, auth *config.Credentials, force bool) error
Push(remote *model.Remote, branch model.Branch, auth *config.Credentials) error
Push(remote *model.Remote, branch model.Branch, auth *config.Credentials, force bool) error
}

type scriptHelper interface {
GetCurrentBranch() (model.Branch, error)
PullBranch(remoteName string, branch model.Branch, force bool) (string, error)
GenerateMocks() (string, error)
TestsPresent() (bool, error)
RunTests() (string, error)
Push(branch model.Branch, withUpStream bool) (string, error)
Exists(path, name string) bool
CreateFile(path, name string) (*os.File, error)
CreateDir(path, name string) error
SetUpstream(remoteName string, branch model.Branch) error
GenerateSSHKey(path, keyName, mail, passphrase string) error

// TODO -> replace this with go-git merge
Expand Down
104 changes: 47 additions & 57 deletions svc/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (s *Svc) Pull(force bool) error {
utils.Logger(utils.LOG_SUCCESS, "changes merged")
return nil
}
utils.Logger(utils.LOG_SUCCESS, "changes pulled")
return pullErr
}

Expand Down Expand Up @@ -196,77 +197,66 @@ func (s *Svc) StageChanges() error {
return nil
}

func (s *Svc) Push(setUpstreamBranch bool) error {
func (s *Svc) Push(force bool) error {
currBranch, err := s.bash.GetCurrentBranch()
if err != nil {
return err
}
if setUpstreamBranch {
_, err = s.bash.Push(currBranch, true)
if err != nil {
return err
}
} else {
remoteDetails, err := s.git.GetRemoteDetails()
if err != nil {
return err
}

if remoteDetails == nil {
return ErrRemoteNotFound
}
remoteDetails, err := s.git.GetRemoteDetails()
if err != nil {
return err
}

var providerAuth *config.Credentials
if remoteDetails.AuthMode() == model.AuthHTTP {
if s.cfg == nil {
return ErrConfigNotLoaded
}
providerAuth = s.cfg.ProviderAuth(remoteDetails.Provider())
if providerAuth == nil {
return ErrAuthNotFound
}
} else if remoteDetails.AuthMode() == model.AuthSSH {
if !s.passphrase.Valid() {
passphrase, err := utils.Prompt(true, false, "passphrase")
if err != nil {
return err
}
s.passphrase = model.Password(passphrase)
}
providerAuth = &config.Credentials{
Token: s.passphrase.String(),
}
} else {
return ErrInvalidAuthMethod
var providerAuth *config.Credentials
if remoteDetails.AuthMode() == model.AuthHTTP {
if s.cfg == nil {
return ErrConfigNotLoaded
}

providerAuth = s.cfg.ProviderAuth(remoteDetails.Provider())
if providerAuth == nil {
return ErrAuthLoadFailed
return ErrAuthNotFound
}
pushErr := s.git.Push(remoteDetails, currBranch, providerAuth)
for errors.Is(pushErr, ErrInvalidPassphrase) {
passphrase, err := utils.Prompt(true, false, "invalid passphrase")
} else if remoteDetails.AuthMode() == model.AuthSSH {
if !s.passphrase.Valid() {
passphrase, err := utils.Prompt(true, false, "passphrase")
if err != nil {
return err
}
s.passphrase = model.Password(passphrase)
providerAuth = &config.Credentials{
Token: s.passphrase.String(),
}
pushErr = s.git.Push(remoteDetails, currBranch, providerAuth)
}
if pushErr != nil {
if errors.Is(pushErr, ErrKeyNotSupported) {
message := fmt.Sprintf("copy contents of %s.pub and upload the keys on %s", filepath.Join(os.Getenv("HOME"), gopushDir, keyName), remoteDetails.Provider().String())
utils.Logger(utils.LOG_STRICT_INFO, message)
}
if errors.Is(pushErr, ErrAlreadyUpToDate) {
utils.Logger(utils.LOG_SUCCESS, "already up-to-date")
return nil
}
return pushErr
providerAuth = &config.Credentials{
Token: s.passphrase.String(),
}
} else {
return ErrInvalidAuthMethod
}

if providerAuth == nil {
return ErrAuthLoadFailed
}
pushErr := s.git.Push(remoteDetails, currBranch, providerAuth, force)
for errors.Is(pushErr, ErrInvalidPassphrase) {
passphrase, err := utils.Prompt(true, false, "invalid passphrase")
if err != nil {
return err
}
s.passphrase = model.Password(passphrase)
providerAuth = &config.Credentials{
Token: s.passphrase.String(),
}
pushErr = s.git.Push(remoteDetails, currBranch, providerAuth, force)
}
if pushErr != nil {
if errors.Is(pushErr, ErrKeyNotSupported) {
message := fmt.Sprintf("copy contents of %s.pub and upload the keys on %s", filepath.Join(os.Getenv("HOME"), gopushDir, keyName), remoteDetails.Provider().String())
utils.Logger(utils.LOG_STRICT_INFO, message)
}
if errors.Is(pushErr, ErrAlreadyUpToDate) {
utils.Logger(utils.LOG_SUCCESS, "already up-to-date")
return nil
}
utils.Logger(utils.LOG_SUCCESS, "push successful")
return pushErr
}
utils.Logger(utils.LOG_SUCCESS, "push successful")
return err
}

0 comments on commit 6f34624

Please sign in to comment.