Skip to content

Commit

Permalink
fix #17
Browse files Browse the repository at this point in the history
  • Loading branch information
ShotaKitazawa committed May 8, 2022
1 parent f88cff4 commit 76ae025
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
33 changes: 17 additions & 16 deletions pkg/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"fmt"

"github.com/ShotaKitazawa/isucontinuous/pkg/localrepo"
"go.uber.org/zap"
"k8s.io/utils/exec"

myerrors "github.com/ShotaKitazawa/isucontinuous/pkg/errors"
"github.com/ShotaKitazawa/isucontinuous/pkg/localrepo"
)

type ConfigPush struct {
Expand Down Expand Up @@ -35,32 +37,31 @@ func runPush(
logger.Info("start push")
defer func() { logger.Info("finish push") }()
// Check currentBranch
var isFirstCommit = false
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
return err
} else if currentBranch != conf.GitBranch {
isFirstCommit, err = repo.IsFirstCommit(ctx)
if err != nil {
return err
} else if isFirstCommit {
if currentBranch == "" {
currentBranch = "<detached>"
}
return fmt.Errorf(
"current branch name is %s. Please exec `sync` command first to checkout to %s.",
currentBranch, conf.GitBranch,
)
if currentBranch == "" {
currentBranch = "<detached>"
}
return fmt.Errorf(
"current branch name is %s. Please exec `sync` command first to checkout to %s.",
currentBranch, conf.GitBranch,
)
}
// Fetch
if err := repo.Fetch(ctx); err != nil {
return err
}
// Validate whether ${BRANCH} == remotes/origin/${BRANCH}
if ok, err := repo.DiffWithRemote(ctx); err != nil && !isFirstCommit {
return err
} else if !ok && !isFirstCommit {
if ok, err := repo.DiffWithRemote(ctx); err != nil {
switch err.(type) {
case myerrors.GitBranchIsFirstCommit:
// pass
default:
return err
}
} else if !ok {
return fmt.Errorf("there are differences between %s and remotes/origin/%s", conf.GitBranch, conf.GitBranch)
}
// Execute add, commit, and push
Expand Down
12 changes: 12 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ func NewErrorGitBranchIsDetached() GitBranchIsDetached {
func (e GitBranchIsDetached) Error() string {
return fmt.Sprintf("current branch name is <detached>. Please exec `sync` command first to checkout.")
}

/* GitBranchIsFirstCommit */

type GitBranchIsFirstCommit struct{}

func NewErrorGitBranchIsFirstCommit() GitBranchIsFirstCommit {
return GitBranchIsFirstCommit{}
}

func (e GitBranchIsFirstCommit) Error() string {
return fmt.Sprintf("current branch name is <detached>. Please exec `sync` command first to checkout.")
}
3 changes: 3 additions & 0 deletions pkg/localrepo/localrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ func (l *LocalRepo) DiffWithRemote(ctx context.Context) (bool, error) {
return false, err
}
if stdout, stderr, err := l.shell.Execf(ctx, l.absPath, "git diff origin/%s %s", currentBranch, currentBranch); err != nil {
if isFirstCommit, _ := l.IsFirstCommit(ctx); isFirstCommit {
return false, myerrors.NewErrorGitBranchIsFirstCommit()
}
return false, myerrors.NewErrorCommandExecutionFailed(stderr)
} else if stdout.String() != "" {
return false, nil
Expand Down

0 comments on commit 76ae025

Please sign in to comment.