Skip to content

Commit

Permalink
fix: fast-forward merge on pull fail
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouspoop committed Oct 5, 2024
1 parent 82614e2 commit a1ba0d9
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

dist/
*_test.go

# All bin files
bin/
26 changes: 13 additions & 13 deletions internal/handler/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ func Run(s servicer) *cobra.Command {
return err
}

utils.Logger(utils.LOG_INFO, "Pulling remote changes...")
err = s.Pull(false)
if err != nil {
if errors.Is(err, svc.ErrAuthNotFound) {
fmt.Println(heredoc.Doc(`
Auth credentials for current remote not found.
Use "gopush init" to generate your config file.
`))
}
return err
}
utils.Logger(utils.LOG_SUCCESS, "changes fetched")
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -109,7 +97,19 @@ func Run(s servicer) *cobra.Command {
return err
}

//TODO -> pull and merge from main
// Pull changes
utils.Logger(utils.LOG_INFO, "Pulling remote changes...")
err = s.Pull(false)
if err != nil {
if errors.Is(err, svc.ErrAuthNotFound) {
fmt.Println(heredoc.Doc(`
Auth credentials for current remote not found.
Use "gopush init" to generate your config file.
`))
}
return err
}
utils.Logger(utils.LOG_SUCCESS, "changes fetched")

// Push changes
utils.Logger(utils.LOG_INFO, "Pushing changes...")
Expand Down
24 changes: 16 additions & 8 deletions repo/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Errors struct {
InvalidPassphrase error
KeyNotSupported error
AlreadyUpToDate error
MergeFailed error
}

type Git struct {
Expand Down Expand Up @@ -159,17 +160,23 @@ 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,
})

if err != nil && strings.Contains(err.Error(), "unable to authenticate") {
return g.err.KeyNotSupported
} else if errors.Is(err, git.ErrNonFastForwardUpdate) {
if err != nil {
if strings.Contains(err.Error(), "unable to authenticate") {
return g.err.KeyNotSupported
}
if errors.Is(err, git.ErrNonFastForwardUpdate) {
return g.err.MergeFailed
}
if errors.Is(err, git.NoErrAlreadyUpToDate) {
return g.err.AlreadyUpToDate
}
return g.err.PullFailed
} else if errors.Is(err, git.NoErrAlreadyUpToDate) {
return g.err.AlreadyUpToDate
}
return err
return nil
}

func (g *Git) GetBranchNames() ([]model.Branch, error) {
Expand Down Expand Up @@ -258,8 +265,9 @@ 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,
Auth: Auth,
Force: true,
Progress: os.Stdout,
Auth: Auth,
})
if err != nil && strings.Contains(err.Error(), "unable to authenticate") {
return g.err.KeyNotSupported
Expand Down
18 changes: 7 additions & 11 deletions repo/script/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ func (b *Bash) Push(branch model.Branch, withUpStream bool) (string, error) {
return string(output[:len(output)-1]), err
}

func (b *Bash) Exists(name, path string) bool {
func (b *Bash) Exists(path, name string) bool {
fpath := filepath.Join(path, name)
_, err := os.Stat(fpath)
return !errors.Is(err, os.ErrNotExist)
}

func (b *Bash) CreateFile(name, path string) (*os.File, error) {
func (b *Bash) CreateFile(path, name string) (*os.File, error) {
fpath := filepath.Join(path, name)
return os.Create(fpath)
}

func (b *Bash) CreateDir(name, path string) error {
func (b *Bash) CreateDir(path, name string) error {
dpath := filepath.Join(path, name)
return os.Mkdir(dpath, os.ModePerm)
}
Expand All @@ -100,19 +100,15 @@ func (b *Bash) SetUpstream(remoteName string, branch model.Branch) error {
return err
}

func (b *Bash) GenerateSSHKey(keyName, path, mail, passphrase string) error {
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
}

func (b *Bash) ShowFileContent(filename, path string) (string, error) {
filePath := filepath.Join(path, filename)
cmd := exec.Command("cat", filePath)
func (b *Bash) PullMerge() (string, error) {
cmd := exec.Command("git", "merge")
output, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
return string(output[:len(output)-1]), nil
return string(output[:len(output)-1]), err
}
18 changes: 11 additions & 7 deletions svc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func (s *Svc) createConfigPath() (string, error) {
if err != nil {
return "", err
}
if !s.bash.Exists(gopushDir, userDir) {
err := s.bash.CreateDir(gopushDir, userDir)
if !s.bash.Exists(userDir, gopushDir) {
err := s.bash.CreateDir(userDir, gopushDir)
if err != nil {
return "", err
}
}

gopushDirPath := filepath.Join(userDir, gopushDir)

if !s.bash.Exists(configFile, gopushDirPath) {
_, err := s.bash.CreateFile(configFile, gopushDirPath)
if !s.bash.Exists(gopushDirPath, configFile) {
_, err := s.bash.CreateFile(gopushDirPath, configFile)
if err != nil {
return "", err
}
Expand All @@ -46,7 +46,7 @@ func (s *Svc) LoadConfig() error {
if err != nil {
return err
}
if !s.bash.Exists(configFile, gopushDirPath) {
if !s.bash.Exists(gopushDirPath, configFile) {
return ErrFileNotFound
}

Expand Down Expand Up @@ -78,6 +78,8 @@ func (s *Svc) SetUserPreference() error {
remoteName = DefaultRemote
}
cfg.DefaultRemote = remoteName
} else {
utils.Logger(utils.LOG_SUCCESS, "remote name found")
}
if cfg.BranchPrefix == "" {
branchPrefix, err := utils.Prompt(false, true, "branch prefix (default=empty)")
Expand All @@ -86,6 +88,8 @@ func (s *Svc) SetUserPreference() error {
}
branchPrefix = strings.TrimSpace(branchPrefix)
cfg.BranchPrefix = branchPrefix
} else {
utils.Logger(utils.LOG_SUCCESS, "branch prefix found")
}
return cfg.Write(configFile, gopushDirPath)
}
Expand Down Expand Up @@ -169,7 +173,7 @@ func (s *Svc) SetRemoteSSHAuth() error {
}

utils.Logger(utils.LOG_INFO, "Gathering ssh keys...")
if !s.bash.Exists(keyName, gopushDirPath) {
if !s.bash.Exists(gopushDirPath, keyName) {
// generate ssh key pair
mail, err := utils.Prompt(false, false, "mail")
if err != nil {
Expand All @@ -179,7 +183,7 @@ func (s *Svc) SetRemoteSSHAuth() error {
if err != nil {
return err
}
err = s.bash.GenerateSSHKey(keyName, gopushDirPath, mail, passphrase)
err = s.bash.GenerateSSHKey(gopushDirPath, keyName, mail, passphrase)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions svc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "errors"

var (
ErrPullFailed = errors.New("remote pull failed")
ErrMergeFailed = errors.New("merge failed")
ErrBranchInvalid = errors.New("invalid branch")
ErrBranchAlreadyExist = errors.New("branch already exist")
ErrTestsFailed = errors.New("tests failed")
Expand Down
14 changes: 7 additions & 7 deletions svc/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
type gitHelper interface {
GetRepo() error
CreateRepo() error
// Fetch() error
// Merge(remoteName string, branchName model.Branch) error
CreateBranch(name model.Branch) error
GetBranchNames() ([]model.Branch, error)
CheckoutBranch(name model.Branch) error
Expand All @@ -31,10 +29,12 @@ type scriptHelper interface {
TestsPresent() (bool, error)
RunTests() (string, error)
Push(branch model.Branch, withUpStream bool) (string, error)
Exists(filename, path string) bool
CreateFile(filename, path string) (*os.File, error)
CreateDir(name, path 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(keyName, path, mail, passphrase string) error
ShowFileContent(filename, path string) (string, error)
GenerateSSHKey(path, keyName, mail, passphrase string) error

// TODO -> replace this with go-git merge
PullMerge() (string, error)
}
17 changes: 13 additions & 4 deletions svc/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ func (s *Svc) Pull(initial bool) error {
return err
}
if initial {
_, err := s.bash.PullBranch(remoteDetails.Name, pullBranch, true)
output, err := s.bash.PullBranch(remoteDetails.Name, pullBranch, true)
if err != nil {
utils.Logger(utils.LOG_FAILURE, output)
}
return err
}

Expand Down Expand Up @@ -118,10 +121,17 @@ func (s *Svc) Pull(initial bool) error {
if errors.Is(pullErr, 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(pullErr, ErrAlreadyUpToDate) {
} else if errors.Is(pullErr, ErrAlreadyUpToDate) {
utils.Logger(utils.LOG_SUCCESS, "already up-to-date")
return nil
} else if errors.Is(pullErr, ErrMergeFailed) {
output, err := s.bash.PullMerge()
if err != nil {
utils.Logger(utils.LOG_FAILURE, output)
return err
}
utils.Logger(utils.LOG_SUCCESS, "changes merged")
return nil
}
return pullErr
}
Expand All @@ -133,7 +143,6 @@ func (s *Svc) SwitchBranchIfExists(branch model.Branch) (bool, error) {
}
for _, br := range branches {
if br.String() == branch.String() {
fmt.Printf("Branch %s already exists. Switching branch...\n", branch.String())
err = s.git.CheckoutBranch(branch)
return true, err
}
Expand Down
2 changes: 1 addition & 1 deletion svc/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (s *Svc) CheckTestsAndRun() (bool, error) {
s.bash.GenerateMocks()
output, err := s.bash.RunTests()
if err != nil {
utils.Logger(utils.LOG_INFO, utils.Faint(output))
utils.Logger(utils.LOG_FAILURE, output)
return false, ErrTestsFailed
}
return true, nil
Expand Down
4 changes: 0 additions & 4 deletions utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func Logger(s log, msg string) {
fmt.Printf("%s%s\n", statusToUnicode[s], msg)
}

func Faint(s string) string {
return faint(s)
}

func ErrorSymbol() string {
return red("\U00002718")
}

0 comments on commit a1ba0d9

Please sign in to comment.