Skip to content
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

feat: accept the fullname of branch #14

Merged
merged 1 commit into from
Jan 5, 2023
Merged
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
5 changes: 3 additions & 2 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func newCheckoutCommand() (c *cobra.Command) {
flags.StringVarP(&opt.remote, "remote", "", "origin", "The remote name")
flags.StringVarP(&opt.sshPrivateKey, "ssh-private-key", "", "$HOME/.ssh/id_rsa",
"The SSH private key file path")
flags.StringVarP(&opt.branch, "branch", "", "master", "The branch want to checkout")
flags.StringVarP(&opt.branch, "branch", "b", "master", "The branch want to checkout. It could be a short name or fullname. Such as master or refs/heads/master")
flags.StringVarP(&opt.tag, "tag", "", "", "The tag want to checkout")
flags.IntVarP(&opt.pr, "pr", "", -1, "The pr number want to checkout, -1 means do nothing")
flags.IntVarP(&opt.pr, "pr", "p", -1, "The pr number want to checkout, -1 means do nothing")
flags.StringVarP(&opt.target, "target", "", ".", "Clone git repository to the target path")
flags.StringVarP(&opt.versionOutput, "version-output", "", "", "Write the version to target file")
return
Expand All @@ -43,6 +43,7 @@ func (o *checkoutOption) preRunE(c *cobra.Command, args []string) (err error) {
if o.url == "" && len(args) > 0 {
o.url = args[0]
}
o.branch = strings.TrimPrefix(o.branch, "refs/heads/")
return
}

Expand Down
47 changes: 47 additions & 0 deletions cmd/checkout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,50 @@ func TestGetAuth(t *testing.T) {
assert.Nil(t, auth)
assert.Nil(t, err)
}

func TestPreRunE(t *testing.T) {
const sampleGit = "https://github.com/linuxsuren/gogit"
const anotherGit = "https://github.com/linuxsuren/gogit.git"

tests := []struct {
name string
opt *checkoutOption
args []string
expectErr bool
verify func(t *testing.T, opt *checkoutOption)
}{{
name: "url is empty",
opt: &checkoutOption{},
args: []string{sampleGit},
expectErr: false,
verify: func(t *testing.T, opt *checkoutOption) {
assert.Equal(t, sampleGit, opt.url)
},
}, {
name: "url is not empty",
opt: &checkoutOption{url: anotherGit},
args: []string{sampleGit},
expectErr: false,
verify: func(t *testing.T, opt *checkoutOption) {
assert.Equal(t, anotherGit, opt.url)
},
}, {
name: "branch is fullname",
opt: &checkoutOption{branch: "refs/heads/master"},
expectErr: false,
verify: func(t *testing.T, opt *checkoutOption) {
assert.Equal(t, "master", opt.branch)
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.opt.preRunE(nil, tt.args)
if tt.expectErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
tt.verify(t, tt.opt)
})
}
}