Skip to content

Commit 76ae025

Browse files
committed
fix #17
1 parent f88cff4 commit 76ae025

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

pkg/cmd/push.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/ShotaKitazawa/isucontinuous/pkg/localrepo"
87
"go.uber.org/zap"
98
"k8s.io/utils/exec"
9+
10+
myerrors "github.com/ShotaKitazawa/isucontinuous/pkg/errors"
11+
"github.com/ShotaKitazawa/isucontinuous/pkg/localrepo"
1012
)
1113

1214
type ConfigPush struct {
@@ -35,32 +37,31 @@ func runPush(
3537
logger.Info("start push")
3638
defer func() { logger.Info("finish push") }()
3739
// Check currentBranch
38-
var isFirstCommit = false
3940
currentBranch, err := repo.CurrentBranch(ctx)
4041
if err != nil {
4142
return err
4243
} else if currentBranch != conf.GitBranch {
43-
isFirstCommit, err = repo.IsFirstCommit(ctx)
44-
if err != nil {
45-
return err
46-
} else if isFirstCommit {
47-
if currentBranch == "" {
48-
currentBranch = "<detached>"
49-
}
50-
return fmt.Errorf(
51-
"current branch name is %s. Please exec `sync` command first to checkout to %s.",
52-
currentBranch, conf.GitBranch,
53-
)
44+
if currentBranch == "" {
45+
currentBranch = "<detached>"
5446
}
47+
return fmt.Errorf(
48+
"current branch name is %s. Please exec `sync` command first to checkout to %s.",
49+
currentBranch, conf.GitBranch,
50+
)
5551
}
5652
// Fetch
5753
if err := repo.Fetch(ctx); err != nil {
5854
return err
5955
}
6056
// Validate whether ${BRANCH} == remotes/origin/${BRANCH}
61-
if ok, err := repo.DiffWithRemote(ctx); err != nil && !isFirstCommit {
62-
return err
63-
} else if !ok && !isFirstCommit {
57+
if ok, err := repo.DiffWithRemote(ctx); err != nil {
58+
switch err.(type) {
59+
case myerrors.GitBranchIsFirstCommit:
60+
// pass
61+
default:
62+
return err
63+
}
64+
} else if !ok {
6465
return fmt.Errorf("there are differences between %s and remotes/origin/%s", conf.GitBranch, conf.GitBranch)
6566
}
6667
// Execute add, commit, and push

pkg/errors/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,15 @@ func NewErrorGitBranchIsDetached() GitBranchIsDetached {
8484
func (e GitBranchIsDetached) Error() string {
8585
return fmt.Sprintf("current branch name is <detached>. Please exec `sync` command first to checkout.")
8686
}
87+
88+
/* GitBranchIsFirstCommit */
89+
90+
type GitBranchIsFirstCommit struct{}
91+
92+
func NewErrorGitBranchIsFirstCommit() GitBranchIsFirstCommit {
93+
return GitBranchIsFirstCommit{}
94+
}
95+
96+
func (e GitBranchIsFirstCommit) Error() string {
97+
return fmt.Sprintf("current branch name is <detached>. Please exec `sync` command first to checkout.")
98+
}

pkg/localrepo/localrepo.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ func (l *LocalRepo) DiffWithRemote(ctx context.Context) (bool, error) {
197197
return false, err
198198
}
199199
if stdout, stderr, err := l.shell.Execf(ctx, l.absPath, "git diff origin/%s %s", currentBranch, currentBranch); err != nil {
200+
if isFirstCommit, _ := l.IsFirstCommit(ctx); isFirstCommit {
201+
return false, myerrors.NewErrorGitBranchIsFirstCommit()
202+
}
200203
return false, myerrors.NewErrorCommandExecutionFailed(stderr)
201204
} else if stdout.String() != "" {
202205
return false, nil

0 commit comments

Comments
 (0)