8000 git: checkout.validate should set to branch only if both branch and hash are empty by GaikwadPratik · Pull Request #1525 · go-git/go-git · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

git: checkout.validate should set to branch only if both branch and hash are empty #1525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: previous-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ type SubmoduleUpdateOptions struct {
var (
ErrBranchHashExclusive = errors.New("Branch and Hash are mutually exclusive")
ErrCreateRequiresBranch = errors.New("Branch is mandatory when Create is used")
ErrForceKeepExclusive = errors.New("Force and branch are mutually exclusive")
)

// CheckoutOptions describes how a checkout operation should be performed.
Expand All @@ -375,18 +376,25 @@ type CheckoutOptions struct {

// Validate validates the fields and sets the default values.
func (o *CheckoutOptions) Validate() error {
// if not create, both hash and branch cannot be provided
if !o.Create && !o.Hash.IsZero() && o.Branch != "" {
return ErrBranchHashExclusive
}

// if create with branch not provided but hash is provided, ask for branch name
if o.Create && o.Branch == "" {
return ErrCreateRequiresBranch
}

if o.Branch == "" {
// if both branch and hash are not provided, set branch to master
if o.Branch == "" && o.Hash.IsZero() {
o.Branch = plumbing.Master
}

if o.Force && o.Keep {
return ErrForceKeepExclusive
}

return nil
}

Expand Down
37 changes: 37 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ func (s *OptionsSuite) writeGlobalConfig(cfg *config.Config) func() {

return func() {
os.Setenv("XDG_CONFIG_HOME", "")
}
}

func (s *OptionsSuite) TestCheckoutOptionsValidate() {
checkoutOpts := CheckoutOptions{
Branch: "test-checkout-branch",
Hash: plumbing.NewHash("ab1b15c6f6487b4db16f10d8ec69bb8bf91dcabd"),
}
err := checkoutOpts.Validate()
s.NotNil(err)
s.ErrorIs(ErrBranchHashExclusive, err)

checkoutOpts = CheckoutOptions{
Create: true,
Hash: plumbing.NewHash("ab1b15c6f6487b4db16f10d8ec69bb8bf91dcabd"),
}
err = checkoutOpts.Validate()
s.NotNil(err)
s.ErrorIs(ErrCreateRequiresBranch, err)

checkoutOpts = CheckoutOptions{}
err = checkoutOpts.Validate()
s.Nil(err)
s.Equal(checkoutOpts.Branch, plumbing.Master)

checkoutOpts = CheckoutOptions{
Branch: "test-checkout-branch",
Force: true,
Keep: true,
}
err = checkoutOpts.Validate()
s.NotNil(err)
s.ErrorIs(ErrForceKeepExclusive, err)

checkoutOpts = CheckoutOptions{
Hash: plumbing.NewHash("ab1b15c6f6487b4db16f10d8ec69bb8bf91dcabd"),
}
err = checkoutOpts.Validate()
s.Nil(err)
}
0