зеркало из https://github.com/mislav/hub.git
`Args.InsertParam` resets index to param size if it’s out of bound
`Args.InsertParam` panics about index out of bound if the index is larger than param size. These changes reset the index to param size for such case. This is equivalent to appending new items to the end. This fixes #812.
This commit is contained in:
Родитель
ad2a46893c
Коммит
2884da15b2
|
@ -95,11 +95,15 @@ func (a *Args) HasSubcommand() bool {
|
|||
}
|
||||
|
||||
func (a *Args) InsertParam(i int, items ...string) {
|
||||
if i < 0 || (i != 0 && i > a.ParamsSize()-1) {
|
||||
if i < 0 {
|
||||
panic(fmt.Sprintf("Index %d is out of bound", i))
|
||||
}
|
||||
|
||||
newParams := []string{}
|
||||
if i > a.ParamsSize() {
|
||||
i = a.ParamsSize()
|
||||
}
|
||||
|
||||
newParams := make([]string, 0)
|
||||
newParams = append(newParams, a.Params[:i]...)
|
||||
newParams = append(newParams, items...)
|
||||
newParams = append(newParams, a.Params[i:]...)
|
||||
|
|
|
@ -62,6 +62,19 @@ func TestArgs_Insert(t *testing.T) {
|
|||
|
||||
assert.Equal(t, 5, args.ParamsSize())
|
||||
assert.Equal(t, "foo", args.Params[3])
|
||||
|
||||
args = NewArgs([]string{"checkout", "-b"})
|
||||
args.InsertParam(1, "foo")
|
||||
|
||||
assert.Equal(t, 2, args.ParamsSize())
|
||||
assert.Equal(t, "-b", args.Params[0])
|
||||
assert.Equal(t, "foo", args.Params[1])
|
||||
|
||||
args = NewArgs([]string{"checkout"})
|
||||
args.InsertParam(1, "foo")
|
||||
|
||||
assert.Equal(t, 1, args.ParamsSize())
|
||||
assert.Equal(t, "foo", args.Params[0])
|
||||
}
|
||||
|
||||
func TestArgs_Remove(t *testing.T) {
|
||||
|
|
|
@ -97,9 +97,14 @@ func transformCheckoutArgs(args *Args) error {
|
|||
args.Before("git", "remote", "add", "-f", "-t", branch, user, u)
|
||||
}
|
||||
|
||||
idx := args.IndexOfParam(checkoutURL)
|
||||
args.RemoveParam(idx)
|
||||
args.InsertParam(idx, "--track", "-B", newBranchName, fmt.Sprintf("%s/%s", user, branch))
|
||||
remoteName := fmt.Sprintf("%s/%s", user, branch)
|
||||
replaceCheckoutParam(args, checkoutURL, newBranchName, remoteName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func replaceCheckoutParam(args *Args, checkoutURL, branchName, remoteName string) {
|
||||
idx := args.IndexOfParam(checkoutURL)
|
||||
args.RemoveParam(idx)
|
||||
args.InsertParam(idx, "--track", "-B", branchName, remoteName)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
|
||||
)
|
||||
|
||||
func TestReplaceCheckoutParam(t *testing.T) {
|
||||
checkoutURL := "https://github.com/github/hub/pull/12"
|
||||
args := NewArgs([]string{"checkout", "-b", checkoutURL})
|
||||
replaceCheckoutParam(args, checkoutURL, "jingweno", "origin/master")
|
||||
|
||||
cmd := args.ToCmd()
|
||||
assert.Equal(t, "git checkout -b --track -B jingweno origin/master", cmd.String())
|
||||
}
|
Загрузка…
Ссылка в новой задаче